blob: baa410b884edf2888ff7698a91ab985fefd46abf [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 Markoda283052017-11-07 21:17:24 +00001348 if (kEmitCompilerReadBarrier &&
1349 !StringEqualsOptimizations(invoke).GetArgumentIsString() &&
1350 !StringEqualsOptimizations(invoke).GetNoReadBarrierForStringClass()) {
1351 // No support for this odd case (String class is moveable, not in the boot image).
1352 return;
1353 }
1354
Vladimir Markoca6fff82017-10-03 14:49:14 +01001355 LocationSummary* locations =
1356 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakid7138c82015-08-13 17:46:44 -07001357 locations->SetInAt(0, Location::RequiresRegister());
1358 locations->SetInAt(1, Location::RequiresRegister());
1359
1360 // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction.
1361 locations->AddTemp(Location::RegisterLocation(ECX));
1362 locations->AddTemp(Location::RegisterLocation(EDI));
1363
1364 // Set output, ESI needed for repe_cmpsl instruction anyways.
1365 locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap);
1366}
1367
1368void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) {
1369 X86Assembler* assembler = GetAssembler();
1370 LocationSummary* locations = invoke->GetLocations();
1371
1372 Register str = locations->InAt(0).AsRegister<Register>();
1373 Register arg = locations->InAt(1).AsRegister<Register>();
1374 Register ecx = locations->GetTemp(0).AsRegister<Register>();
1375 Register edi = locations->GetTemp(1).AsRegister<Register>();
1376 Register esi = locations->Out().AsRegister<Register>();
1377
Mark Mendell0c9497d2015-08-21 09:30:05 -04001378 NearLabel end, return_true, return_false;
Agi Csakid7138c82015-08-13 17:46:44 -07001379
1380 // Get offsets of count, value, and class fields within a string object.
1381 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1382 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1383 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1384
1385 // Note that the null check must have been done earlier.
1386 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1387
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001388 StringEqualsOptimizations optimizations(invoke);
1389 if (!optimizations.GetArgumentNotNull()) {
1390 // Check if input is null, return false if it is.
1391 __ testl(arg, arg);
1392 __ j(kEqual, &return_false);
1393 }
Agi Csakid7138c82015-08-13 17:46:44 -07001394
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001395 if (!optimizations.GetArgumentIsString()) {
Vladimir Marko53b52002016-05-24 19:30:45 +01001396 // Instanceof check for the argument by comparing class fields.
1397 // All string objects must have the same type since String cannot be subclassed.
1398 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1399 // 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 +01001400 __ movl(ecx, Address(str, class_offset));
1401 __ cmpl(ecx, Address(arg, class_offset));
1402 __ j(kNotEqual, &return_false);
1403 }
Agi Csakid7138c82015-08-13 17:46:44 -07001404
1405 // Reference equality check, return true if same reference.
1406 __ cmpl(str, arg);
1407 __ j(kEqual, &return_true);
1408
jessicahandojo4877b792016-09-08 19:49:13 -07001409 // Load length and compression flag of receiver string.
Agi Csakid7138c82015-08-13 17:46:44 -07001410 __ movl(ecx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001411 // Check if lengths and compression flags are equal, return false if they're not.
1412 // Two identical strings will always have same compression style since
1413 // compression style is decided on alloc.
Agi Csakid7138c82015-08-13 17:46:44 -07001414 __ cmpl(ecx, Address(arg, count_offset));
1415 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001416 // Return true if strings are empty. Even with string compression `count == 0` means empty.
1417 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1418 "Expecting 0=compressed, 1=uncompressed");
1419 __ jecxz(&return_true);
Agi Csakid7138c82015-08-13 17:46:44 -07001420
jessicahandojo4877b792016-09-08 19:49:13 -07001421 if (mirror::kUseStringCompression) {
1422 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001423 // Extract length and differentiate between both compressed or both uncompressed.
1424 // Different compression style is cut above.
1425 __ shrl(ecx, Immediate(1));
1426 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001427 // Divide string length by 2, rounding up, and continue as if uncompressed.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001428 __ addl(ecx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001429 __ shrl(ecx, Immediate(1));
1430 __ Bind(&string_uncompressed);
1431 }
Agi Csakid7138c82015-08-13 17:46:44 -07001432 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1433 __ leal(esi, Address(str, value_offset));
1434 __ leal(edi, Address(arg, value_offset));
1435
jessicahandojo4877b792016-09-08 19:49:13 -07001436 // Divide string length by 2 to compare characters 2 at a time and adjust for lengths not
1437 // divisible by 2.
Agi Csakid7138c82015-08-13 17:46:44 -07001438 __ addl(ecx, Immediate(1));
1439 __ shrl(ecx, Immediate(1));
1440
jessicahandojo4877b792016-09-08 19:49:13 -07001441 // Assertions that must hold in order to compare strings 2 characters (uncompressed)
1442 // or 4 characters (compressed) at a time.
Agi Csakid7138c82015-08-13 17:46:44 -07001443 DCHECK_ALIGNED(value_offset, 4);
1444 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1445
1446 // Loop to compare strings two characters at a time starting at the beginning of the string.
1447 __ repe_cmpsl();
1448 // If strings are not equal, zero flag will be cleared.
1449 __ j(kNotEqual, &return_false);
1450
1451 // Return true and exit the function.
1452 // If loop does not result in returning false, we return true.
1453 __ Bind(&return_true);
1454 __ movl(esi, Immediate(1));
1455 __ jmp(&end);
1456
1457 // Return false and exit the function.
1458 __ Bind(&return_false);
1459 __ xorl(esi, esi);
1460 __ Bind(&end);
1461}
1462
Andreas Gampe21030dd2015-05-07 14:46:15 -07001463static void CreateStringIndexOfLocations(HInvoke* invoke,
1464 ArenaAllocator* allocator,
1465 bool start_at_zero) {
1466 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1467 LocationSummary::kCallOnSlowPath,
1468 kIntrinsified);
1469 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1470 locations->SetInAt(0, Location::RegisterLocation(EDI));
1471 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1472 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1473 // of the instruction explicitly.
1474 // Note: This works as we don't clobber EAX anywhere.
1475 locations->SetInAt(1, Location::RegisterLocation(EAX));
1476 if (!start_at_zero) {
1477 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1478 }
1479 // As we clobber EDI during execution anyways, also use it as the output.
1480 locations->SetOut(Location::SameAsFirstInput());
1481
1482 // repne scasw uses ECX as the counter.
1483 locations->AddTemp(Location::RegisterLocation(ECX));
1484 // Need another temporary to be able to compute the result.
1485 locations->AddTemp(Location::RequiresRegister());
jessicahandojo4877b792016-09-08 19:49:13 -07001486 if (mirror::kUseStringCompression) {
1487 // Need another temporary to be able to save unflagged string length.
1488 locations->AddTemp(Location::RequiresRegister());
1489 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001490}
1491
1492static void GenerateStringIndexOf(HInvoke* invoke,
1493 X86Assembler* assembler,
1494 CodeGeneratorX86* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001495 bool start_at_zero) {
1496 LocationSummary* locations = invoke->GetLocations();
1497
1498 // Note that the null check must have been done earlier.
1499 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1500
1501 Register string_obj = locations->InAt(0).AsRegister<Register>();
1502 Register search_value = locations->InAt(1).AsRegister<Register>();
1503 Register counter = locations->GetTemp(0).AsRegister<Register>();
1504 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1505 Register out = locations->Out().AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07001506 // Only used when string compression feature is on.
1507 Register string_length_flagged;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001508
1509 // Check our assumptions for registers.
1510 DCHECK_EQ(string_obj, EDI);
1511 DCHECK_EQ(search_value, EAX);
1512 DCHECK_EQ(counter, ECX);
1513 DCHECK_EQ(out, EDI);
1514
1515 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001516 // 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 -07001517 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001518 HInstruction* code_point = invoke->InputAt(1);
1519 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001520 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001521 std::numeric_limits<uint16_t>::max()) {
1522 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1523 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001524 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001525 codegen->AddSlowPath(slow_path);
1526 __ jmp(slow_path->GetEntryLabel());
1527 __ Bind(slow_path->GetExitLabel());
1528 return;
1529 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001530 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001531 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001532 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001533 codegen->AddSlowPath(slow_path);
1534 __ j(kAbove, slow_path->GetEntryLabel());
1535 }
1536
1537 // From here down, we know that we are looking for a char that fits in 16 bits.
1538 // Location of reference to data array within the String object.
1539 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1540 // Location of count within the String object.
1541 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1542
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001543 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001544 __ movl(string_length, Address(string_obj, count_offset));
1545
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001546 // Do a zero-length check. Even with string compression `count == 0` means empty.
1547 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1548 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe21030dd2015-05-07 14:46:15 -07001549 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001550 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001551 __ testl(string_length, string_length);
1552 __ j(kEqual, &not_found_label);
1553
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001554 if (mirror::kUseStringCompression) {
1555 string_length_flagged = locations->GetTemp(2).AsRegister<Register>();
1556 __ movl(string_length_flagged, string_length);
1557 // Extract the length and shift out the least significant bit used as compression flag.
1558 __ shrl(string_length, Immediate(1));
1559 }
1560
Andreas Gampe21030dd2015-05-07 14:46:15 -07001561 if (start_at_zero) {
1562 // Number of chars to scan is the same as the string length.
1563 __ movl(counter, string_length);
1564
1565 // Move to the start of the string.
1566 __ addl(string_obj, Immediate(value_offset));
1567 } else {
1568 Register start_index = locations->InAt(2).AsRegister<Register>();
1569
1570 // Do a start_index check.
1571 __ cmpl(start_index, string_length);
1572 __ j(kGreaterEqual, &not_found_label);
1573
1574 // Ensure we have a start index >= 0;
1575 __ xorl(counter, counter);
1576 __ cmpl(start_index, Immediate(0));
1577 __ cmovl(kGreater, counter, start_index);
1578
jessicahandojo4877b792016-09-08 19:49:13 -07001579 if (mirror::kUseStringCompression) {
1580 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001581 __ testl(string_length_flagged, Immediate(1));
1582 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001583 // Move to the start of the string: string_obj + value_offset + start_index.
1584 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1585 __ jmp(&modify_counter);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001586
jessicahandojo4877b792016-09-08 19:49:13 -07001587 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1588 __ Bind(&offset_uncompressed_label);
1589 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1590
1591 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1592 // compare.
1593 __ Bind(&modify_counter);
1594 } else {
1595 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1596 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001597 __ negl(counter);
1598 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1599 }
1600
jessicahandojo4877b792016-09-08 19:49:13 -07001601 if (mirror::kUseStringCompression) {
1602 NearLabel uncompressed_string_comparison;
1603 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001604 __ testl(string_length_flagged, Immediate(1));
1605 __ j(kNotZero, &uncompressed_string_comparison);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001606
jessicahandojo4877b792016-09-08 19:49:13 -07001607 // Check if EAX (search_value) is ASCII.
1608 __ cmpl(search_value, Immediate(127));
1609 __ j(kGreater, &not_found_label);
1610 // Comparing byte-per-byte.
1611 __ repne_scasb();
1612 __ jmp(&comparison_done);
1613
1614 // Everything is set up for repne scasw:
1615 // * Comparison address in EDI.
1616 // * Counter in ECX.
1617 __ Bind(&uncompressed_string_comparison);
1618 __ repne_scasw();
1619 __ Bind(&comparison_done);
1620 } else {
1621 __ repne_scasw();
1622 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001623 // Did we find a match?
1624 __ j(kNotEqual, &not_found_label);
1625
1626 // Yes, we matched. Compute the index of the result.
1627 __ subl(string_length, counter);
1628 __ leal(out, Address(string_length, -1));
1629
Mark Mendell0c9497d2015-08-21 09:30:05 -04001630 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001631 __ jmp(&done);
1632
1633 // Failed to match; return -1.
1634 __ Bind(&not_found_label);
1635 __ movl(out, Immediate(-1));
1636
1637 // And join up at the end.
1638 __ Bind(&done);
1639 if (slow_path != nullptr) {
1640 __ Bind(slow_path->GetExitLabel());
1641 }
1642}
1643
1644void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001645 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001646}
1647
1648void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001649 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001650}
1651
1652void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001653 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001654}
1655
1656void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001657 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001658}
1659
Jeff Hao848f70a2014-01-15 13:49:50 -08001660void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001661 LocationSummary* locations = new (allocator_) LocationSummary(
1662 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001663 InvokeRuntimeCallingConvention calling_convention;
1664 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1665 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1666 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1667 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1668 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001669}
1670
1671void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1672 X86Assembler* assembler = GetAssembler();
1673 LocationSummary* locations = invoke->GetLocations();
1674
1675 Register byte_array = locations->InAt(0).AsRegister<Register>();
1676 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001677 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001678 codegen_->AddSlowPath(slow_path);
1679 __ j(kEqual, slow_path->GetEntryLabel());
1680
Serban Constantinescuba45db02016-07-12 22:53:02 +01001681 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001682 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001683 __ Bind(slow_path->GetExitLabel());
1684}
1685
1686void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001687 LocationSummary* locations =
1688 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001689 InvokeRuntimeCallingConvention calling_convention;
1690 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1691 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1692 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1693 locations->SetOut(Location::RegisterLocation(EAX));
1694}
1695
1696void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001697 // No need to emit code checking whether `locations->InAt(2)` is a null
1698 // pointer, as callers of the native method
1699 //
1700 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1701 //
1702 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001703 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001704 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001705}
1706
1707void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001708 LocationSummary* locations = new (allocator_) LocationSummary(
1709 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001710 InvokeRuntimeCallingConvention calling_convention;
1711 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1712 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001713}
1714
1715void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1716 X86Assembler* assembler = GetAssembler();
1717 LocationSummary* locations = invoke->GetLocations();
1718
1719 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1720 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001721 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001722 codegen_->AddSlowPath(slow_path);
1723 __ j(kEqual, slow_path->GetEntryLabel());
1724
Serban Constantinescuba45db02016-07-12 22:53:02 +01001725 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001726 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001727 __ Bind(slow_path->GetExitLabel());
1728}
1729
Mark Mendell8f8926a2015-08-17 11:39:06 -04001730void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1731 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001732 LocationSummary* locations =
1733 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001734 locations->SetInAt(0, Location::RequiresRegister());
1735 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1736 // Place srcEnd in ECX to save a move below.
1737 locations->SetInAt(2, Location::RegisterLocation(ECX));
1738 locations->SetInAt(3, Location::RequiresRegister());
1739 locations->SetInAt(4, Location::RequiresRegister());
1740
1741 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1742 // We don't have enough registers to also grab ECX, so handle below.
1743 locations->AddTemp(Location::RegisterLocation(ESI));
1744 locations->AddTemp(Location::RegisterLocation(EDI));
1745}
1746
1747void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1748 X86Assembler* assembler = GetAssembler();
1749 LocationSummary* locations = invoke->GetLocations();
1750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001751 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001752 // Location of data in char array buffer.
1753 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1754 // Location of char array data in string.
1755 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1756
1757 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1758 Register obj = locations->InAt(0).AsRegister<Register>();
1759 Location srcBegin = locations->InAt(1);
1760 int srcBegin_value =
1761 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1762 Register srcEnd = locations->InAt(2).AsRegister<Register>();
1763 Register dst = locations->InAt(3).AsRegister<Register>();
1764 Register dstBegin = locations->InAt(4).AsRegister<Register>();
1765
1766 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001767 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001768 DCHECK_EQ(char_size, 2u);
1769
Mark Mendell8f8926a2015-08-17 11:39:06 -04001770 // Compute the number of chars (words) to move.
jessicahandojo4877b792016-09-08 19:49:13 -07001771 // Save ECX, since we don't know if it will be used later.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001772 __ pushl(ECX);
1773 int stack_adjust = kX86WordSize;
1774 __ cfi().AdjustCFAOffset(stack_adjust);
1775 DCHECK_EQ(srcEnd, ECX);
1776 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001777 __ subl(ECX, Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001778 } else {
1779 DCHECK(srcBegin.IsRegister());
1780 __ subl(ECX, srcBegin.AsRegister<Register>());
1781 }
1782
jessicahandojo4877b792016-09-08 19:49:13 -07001783 NearLabel done;
1784 if (mirror::kUseStringCompression) {
1785 // Location of count in string
1786 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001787 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001788 DCHECK_EQ(c_char_size, 1u);
1789 __ pushl(EAX);
1790 __ cfi().AdjustCFAOffset(stack_adjust);
1791
1792 NearLabel copy_loop, copy_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001793 __ testl(Address(obj, count_offset), Immediate(1));
1794 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1795 "Expecting 0=compressed, 1=uncompressed");
1796 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001797 // Compute the address of the source string by adding the number of chars from
1798 // the source beginning to the value offset of a string.
1799 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1800
1801 // Start the loop to copy String's value to Array of Char.
1802 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1803 __ Bind(&copy_loop);
1804 __ jecxz(&done);
1805 // Use EAX temporary (convert byte from ESI to word).
1806 // TODO: Use LODSB/STOSW (not supported by X86Assembler) with AH initialized to 0.
1807 __ movzxb(EAX, Address(ESI, 0));
1808 __ movw(Address(EDI, 0), EAX);
1809 __ leal(EDI, Address(EDI, char_size));
1810 __ leal(ESI, Address(ESI, c_char_size));
1811 // TODO: Add support for LOOP to X86Assembler.
1812 __ subl(ECX, Immediate(1));
1813 __ jmp(&copy_loop);
1814 __ Bind(&copy_uncompressed);
1815 }
1816
1817 // Do the copy for uncompressed string.
1818 // Compute the address of the destination buffer.
1819 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1820 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001821 __ rep_movsw();
1822
jessicahandojo4877b792016-09-08 19:49:13 -07001823 __ Bind(&done);
1824 if (mirror::kUseStringCompression) {
1825 // Restore EAX.
1826 __ popl(EAX);
1827 __ cfi().AdjustCFAOffset(-stack_adjust);
1828 }
1829 // Restore ECX.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001830 __ popl(ECX);
1831 __ cfi().AdjustCFAOffset(-stack_adjust);
1832}
1833
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001834static void GenPeek(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001835 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1836 Location out_loc = locations->Out();
1837 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1838 // to avoid a SIGBUS.
1839 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001840 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001841 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1842 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001843 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001844 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1845 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001846 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001847 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1848 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001849 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001850 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1851 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1852 break;
1853 default:
1854 LOG(FATAL) << "Type not recognized for peek: " << size;
1855 UNREACHABLE();
1856 }
1857}
1858
1859void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001860 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001861}
1862
1863void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001864 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001865}
1866
1867void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001868 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001869}
1870
1871void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001872 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001873}
1874
1875void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001876 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001877}
1878
1879void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001880 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001881}
1882
1883void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001884 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001885}
1886
1887void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001888 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001889}
1890
Vladimir Markoca6fff82017-10-03 14:49:14 +01001891static void CreateLongIntToVoidLocations(ArenaAllocator* allocator,
1892 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001893 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001894 LocationSummary* locations =
1895 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001896 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001897 HInstruction* value = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001898 if (size == DataType::Type::kInt8) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001899 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1900 } else {
1901 locations->SetInAt(1, Location::RegisterOrConstant(value));
1902 }
1903}
1904
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001905static void GenPoke(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001906 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1907 Location value_loc = locations->InAt(1);
1908 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1909 // to avoid a SIGBUS.
1910 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001911 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001912 if (value_loc.IsConstant()) {
1913 __ movb(Address(address, 0),
1914 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1915 } else {
1916 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1917 }
1918 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001919 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001920 if (value_loc.IsConstant()) {
1921 __ movw(Address(address, 0),
1922 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1923 } else {
1924 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1925 }
1926 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001927 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001928 if (value_loc.IsConstant()) {
1929 __ movl(Address(address, 0),
1930 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1931 } else {
1932 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1933 }
1934 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001936 if (value_loc.IsConstant()) {
1937 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1938 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1939 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1940 } else {
1941 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1942 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1943 }
1944 break;
1945 default:
1946 LOG(FATAL) << "Type not recognized for poke: " << size;
1947 UNREACHABLE();
1948 }
1949}
1950
1951void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001952 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt8, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001953}
1954
1955void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001956 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001957}
1958
1959void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001960 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001961}
1962
1963void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001964 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001965}
1966
1967void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001968 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001969}
1970
1971void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001972 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001973}
1974
1975void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001976 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt16, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001977}
1978
1979void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001980 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001981}
1982
1983void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001984 LocationSummary* locations =
1985 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001986 locations->SetOut(Location::RequiresRegister());
1987}
1988
1989void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1990 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001991 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86PointerSize>()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001992}
1993
Roland Levillain0d5a2812015-11-13 10:07:31 +00001994static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001995 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001996 bool is_volatile,
1997 CodeGeneratorX86* codegen) {
1998 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
1999 LocationSummary* locations = invoke->GetLocations();
2000 Location base_loc = locations->InAt(1);
2001 Register base = base_loc.AsRegister<Register>();
2002 Location offset_loc = locations->InAt(2);
2003 Register offset = offset_loc.AsRegisterPairLow<Register>();
2004 Location output_loc = locations->Out();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002005
2006 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002007 case DataType::Type::kInt32: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002008 Register output = output_loc.AsRegister<Register>();
2009 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain7c1559a2015-12-15 10:55:36 +00002010 break;
2011 }
2012
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002013 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002014 Register output = output_loc.AsRegister<Register>();
2015 if (kEmitCompilerReadBarrier) {
2016 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002017 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2018 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002019 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00002020 } else {
2021 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2022 codegen->GenerateReadBarrierSlow(
2023 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2024 }
2025 } else {
2026 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2027 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002028 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002029 break;
Roland Levillain4d027112015-07-01 15:41:14 +01002030 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002032 case DataType::Type::kInt64: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002033 Register output_lo = output_loc.AsRegisterPairLow<Register>();
2034 Register output_hi = output_loc.AsRegisterPairHigh<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002035 if (is_volatile) {
2036 // Need to use a XMM to read atomically.
2037 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2038 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
2039 __ movd(output_lo, temp);
2040 __ psrlq(temp, Immediate(32));
2041 __ movd(output_hi, temp);
2042 } else {
2043 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
2044 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
2045 }
2046 }
2047 break;
2048
2049 default:
2050 LOG(FATAL) << "Unsupported op size " << type;
2051 UNREACHABLE();
2052 }
2053}
2054
Vladimir Markoca6fff82017-10-03 14:49:14 +01002055static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Roland Levillain7c1559a2015-12-15 10:55:36 +00002056 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002057 DataType::Type type,
Roland Levillain7c1559a2015-12-15 10:55:36 +00002058 bool is_volatile) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002059 bool can_call = kEmitCompilerReadBarrier &&
2060 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2061 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002062 LocationSummary* locations =
2063 new (allocator) LocationSummary(invoke,
2064 can_call
2065 ? LocationSummary::kCallOnSlowPath
2066 : LocationSummary::kNoCall,
2067 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01002068 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002069 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002070 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002071 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2072 locations->SetInAt(1, Location::RequiresRegister());
2073 locations->SetInAt(2, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002075 if (is_volatile) {
2076 // Need to use XMM to read volatile.
2077 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002079 } else {
2080 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2081 }
2082 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01002083 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002084 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Mark Mendell09ed1a32015-03-25 08:30:06 -04002085 }
2086}
2087
2088void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002089 CreateIntIntIntToIntLocations(
2090 allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002091}
2092void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002093 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002094}
2095void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002096 CreateIntIntIntToIntLocations(
2097 allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002098}
2099void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002100 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002101}
2102void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002103 CreateIntIntIntToIntLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002104 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002105}
2106void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002107 CreateIntIntIntToIntLocations(
2108 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002109}
2110
2111
2112void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002113 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002114}
2115void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002116 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002117}
2118void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002120}
2121void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002122 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002123}
2124void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002125 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002126}
2127void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002129}
2130
2131
Vladimir Markoca6fff82017-10-03 14:49:14 +01002132static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002133 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04002134 HInvoke* invoke,
2135 bool is_volatile) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002136 LocationSummary* locations =
2137 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002138 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2139 locations->SetInAt(1, Location::RequiresRegister());
2140 locations->SetInAt(2, Location::RequiresRegister());
2141 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002142 if (type == DataType::Type::kReference) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002143 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002144 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04002145 // Ensure the value is in a byte register.
2146 locations->AddTemp(Location::RegisterLocation(ECX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002147 } else if (type == DataType::Type::kInt64 && is_volatile) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002148 locations->AddTemp(Location::RequiresFpuRegister());
2149 locations->AddTemp(Location::RequiresFpuRegister());
2150 }
2151}
2152
2153void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002154 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002155 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002156}
2157void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002158 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002159 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002160}
2161void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002162 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002163 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002164}
2165void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002166 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002167 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002168}
2169void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002170 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002171 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002172}
2173void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002174 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002175 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002176}
2177void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002178 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002179 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002180}
2181void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002182 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002183 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002184}
2185void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002186 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002187 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002188}
2189
2190// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2191// memory model.
2192static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002193 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04002194 bool is_volatile,
2195 CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002196 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04002197 Register base = locations->InAt(1).AsRegister<Register>();
2198 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2199 Location value_loc = locations->InAt(3);
2200
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002201 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002202 Register value_lo = value_loc.AsRegisterPairLow<Register>();
2203 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
2204 if (is_volatile) {
2205 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2206 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2207 __ movd(temp1, value_lo);
2208 __ movd(temp2, value_hi);
2209 __ punpckldq(temp1, temp2);
2210 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
2211 } else {
2212 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
2213 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
2214 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002215 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002216 Register temp = locations->GetTemp(0).AsRegister<Register>();
2217 __ movl(temp, value_loc.AsRegister<Register>());
2218 __ PoisonHeapReference(temp);
2219 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002220 } else {
2221 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
2222 }
2223
2224 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002225 codegen->MemoryFence();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002226 }
2227
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002228 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002229 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04002230 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
2231 locations->GetTemp(1).AsRegister<Register>(),
2232 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002233 value_loc.AsRegister<Register>(),
2234 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002235 }
2236}
2237
2238void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002239 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002240}
2241void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002243}
2244void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002246}
2247void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002248 GenUnsafePut(
2249 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002250}
2251void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002252 GenUnsafePut(
2253 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002254}
2255void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002256 GenUnsafePut(
2257 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002258}
2259void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002260 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002261}
2262void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002263 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002264}
2265void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002267}
2268
Vladimir Markoca6fff82017-10-03 14:49:14 +01002269static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002270 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002271 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002272 bool can_call = kEmitCompilerReadBarrier &&
2273 kUseBakerReadBarrier &&
2274 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002275 LocationSummary* locations =
2276 new (allocator) LocationSummary(invoke,
2277 can_call
2278 ? LocationSummary::kCallOnSlowPath
2279 : LocationSummary::kNoCall,
2280 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002281 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2282 locations->SetInAt(1, Location::RequiresRegister());
2283 // Offset is a long, but in 32 bit mode, we only need the low word.
2284 // Can we update the invoke here to remove a TypeConvert to Long?
2285 locations->SetInAt(2, Location::RequiresRegister());
2286 // Expected value must be in EAX or EDX:EAX.
2287 // For long, new value must be in ECX:EBX.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 if (type == DataType::Type::kInt64) {
Mark Mendell58d25fd2015-04-03 14:52:31 -04002289 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
2290 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
2291 } else {
2292 locations->SetInAt(3, Location::RegisterLocation(EAX));
2293 locations->SetInAt(4, Location::RequiresRegister());
2294 }
2295
2296 // Force a byte register for the output.
2297 locations->SetOut(Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002299 // Need temporary registers for card-marking, and possibly for
2300 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002301 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002302 // Need a byte register for marking.
2303 locations->AddTemp(Location::RegisterLocation(ECX));
2304 }
2305}
2306
2307void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002308 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002309}
2310
2311void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002312 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002313}
2314
2315void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002316 // The only read barrier implementation supporting the
2317 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2318 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002319 return;
2320 }
2321
Vladimir Markoca6fff82017-10-03 14:49:14 +01002322 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002323}
2324
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002325static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002326 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002327 LocationSummary* locations = invoke->GetLocations();
2328
2329 Register base = locations->InAt(1).AsRegister<Register>();
2330 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2331 Location out = locations->Out();
2332 DCHECK_EQ(out.AsRegister<Register>(), EAX);
2333
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002334 // The address of the field within the holding object.
2335 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002337 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002338 // The only read barrier implementation supporting the
2339 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2340 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2341
2342 Location temp1_loc = locations->GetTemp(0);
2343 Register temp1 = temp1_loc.AsRegister<Register>();
2344 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2345
Roland Levillain4d027112015-07-01 15:41:14 +01002346 Register expected = locations->InAt(3).AsRegister<Register>();
Roland Levillainb488b782015-10-22 11:38:49 +01002347 // Ensure `expected` is in EAX (required by the CMPXCHG instruction).
Roland Levillain4d027112015-07-01 15:41:14 +01002348 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002349 Register value = locations->InAt(4).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01002350
Roland Levillainb488b782015-10-22 11:38:49 +01002351 // Mark card for object assuming new value is stored.
2352 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002353 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2354
2355 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2356 // Need to make sure the reference stored in the field is a to-space
2357 // one before attempting the CAS or the CAS could fail incorrectly.
2358 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2359 invoke,
2360 temp1_loc, // Unused, used only as a "temporary" within the read barrier.
2361 base,
2362 field_addr,
2363 /* needs_null_check */ false,
2364 /* always_update_field */ true,
2365 &temp2);
2366 }
Roland Levillainb488b782015-10-22 11:38:49 +01002367
2368 bool base_equals_value = (base == value);
2369 if (kPoisonHeapReferences) {
2370 if (base_equals_value) {
2371 // If `base` and `value` are the same register location, move
2372 // `value` to a temporary register. This way, poisoning
2373 // `value` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002374 value = temp1;
Roland Levillainb488b782015-10-22 11:38:49 +01002375 __ movl(value, base);
Roland Levillain4d027112015-07-01 15:41:14 +01002376 }
Roland Levillainb488b782015-10-22 11:38:49 +01002377
2378 // Check that the register allocator did not assign the location
2379 // of `expected` (EAX) to `value` nor to `base`, so that heap
2380 // poisoning (when enabled) works as intended below.
2381 // - If `value` were equal to `expected`, both references would
2382 // be poisoned twice, meaning they would not be poisoned at
2383 // all, as heap poisoning uses address negation.
2384 // - If `base` were equal to `expected`, poisoning `expected`
2385 // would invalidate `base`.
2386 DCHECK_NE(value, expected);
2387 DCHECK_NE(base, expected);
2388
2389 __ PoisonHeapReference(expected);
2390 __ PoisonHeapReference(value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002391 }
2392
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002393 __ LockCmpxchgl(field_addr, value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002394
Roland Levillain0d5a2812015-11-13 10:07:31 +00002395 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002396 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002397
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002398 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002399 __ setb(kZero, out.AsRegister<Register>());
2400 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002401
Roland Levillain391b8662015-12-18 11:43:38 +00002402 // If heap poisoning is enabled, we need to unpoison the values
2403 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002404 if (kPoisonHeapReferences) {
2405 if (base_equals_value) {
2406 // `value` has been moved to a temporary register, no need to
2407 // unpoison it.
2408 } else {
2409 // Ensure `value` is different from `out`, so that unpoisoning
2410 // the former does not invalidate the latter.
2411 DCHECK_NE(value, out.AsRegister<Register>());
2412 __ UnpoisonHeapReference(value);
2413 }
2414 // Do not unpoison the reference contained in register
2415 // `expected`, as it is the same as register `out` (EAX).
2416 }
2417 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002418 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002419 // Ensure the expected value is in EAX (required by the CMPXCHG
2420 // instruction).
2421 DCHECK_EQ(locations->InAt(3).AsRegister<Register>(), EAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002422 __ LockCmpxchgl(field_addr, locations->InAt(4).AsRegister<Register>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002424 // Ensure the expected value is in EAX:EDX and that the new
2425 // value is in EBX:ECX (required by the CMPXCHG8B instruction).
2426 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
2427 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
2428 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
2429 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002430 __ LockCmpxchg8b(field_addr);
Roland Levillainb488b782015-10-22 11:38:49 +01002431 } else {
2432 LOG(FATAL) << "Unexpected CAS type " << type;
2433 }
2434
Roland Levillain0d5a2812015-11-13 10:07:31 +00002435 // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we
2436 // don't need scheduling barriers at this time.
Roland Levillainb488b782015-10-22 11:38:49 +01002437
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002438 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002439 __ setb(kZero, out.AsRegister<Register>());
2440 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002441 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002442}
2443
2444void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002445 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002446}
2447
2448void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002449 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002450}
2451
2452void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002453 // The only read barrier implementation supporting the
2454 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2455 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002456
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002457 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002458}
2459
2460void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002461 LocationSummary* locations =
2462 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002463 locations->SetInAt(0, Location::RequiresRegister());
2464 locations->SetOut(Location::SameAsFirstInput());
2465 locations->AddTemp(Location::RequiresRegister());
2466}
2467
2468static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
2469 X86Assembler* assembler) {
2470 Immediate imm_shift(shift);
2471 Immediate imm_mask(mask);
2472 __ movl(temp, reg);
2473 __ shrl(reg, imm_shift);
2474 __ andl(temp, imm_mask);
2475 __ andl(reg, imm_mask);
2476 __ shll(temp, imm_shift);
2477 __ orl(reg, temp);
2478}
2479
2480void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002481 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002482 LocationSummary* locations = invoke->GetLocations();
2483
2484 Register reg = locations->InAt(0).AsRegister<Register>();
2485 Register temp = locations->GetTemp(0).AsRegister<Register>();
2486
2487 /*
2488 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2489 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2490 * compared to generic luni implementation which has 5 rounds of swapping bits.
2491 * x = bswap x
2492 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2493 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2494 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2495 */
2496 __ bswapl(reg);
2497 SwapBits(reg, temp, 1, 0x55555555, assembler);
2498 SwapBits(reg, temp, 2, 0x33333333, assembler);
2499 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2500}
2501
2502void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002503 LocationSummary* locations =
2504 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002505 locations->SetInAt(0, Location::RequiresRegister());
2506 locations->SetOut(Location::SameAsFirstInput());
2507 locations->AddTemp(Location::RequiresRegister());
2508}
2509
2510void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002511 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002512 LocationSummary* locations = invoke->GetLocations();
2513
2514 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
2515 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2516 Register temp = locations->GetTemp(0).AsRegister<Register>();
2517
2518 // We want to swap high/low, then bswap each one, and then do the same
2519 // as a 32 bit reverse.
2520 // Exchange high and low.
2521 __ movl(temp, reg_low);
2522 __ movl(reg_low, reg_high);
2523 __ movl(reg_high, temp);
2524
2525 // bit-reverse low
2526 __ bswapl(reg_low);
2527 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
2528 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
2529 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
2530
2531 // bit-reverse high
2532 __ bswapl(reg_high);
2533 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
2534 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
2535 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
2536}
2537
Aart Bikc39dac12016-01-21 08:59:48 -08002538static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002539 ArenaAllocator* allocator, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002540 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2541 // Do nothing if there is no popcnt support. This results in generating
2542 // a call for the intrinsic rather than direct code.
2543 return;
2544 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002545 LocationSummary* locations =
2546 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc39dac12016-01-21 08:59:48 -08002547 if (is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002548 locations->AddTemp(Location::RequiresRegister());
Aart Bikc39dac12016-01-21 08:59:48 -08002549 }
Aart Bik2a946072016-01-21 12:49:00 -08002550 locations->SetInAt(0, Location::Any());
Aart Bikc39dac12016-01-21 08:59:48 -08002551 locations->SetOut(Location::RequiresRegister());
2552}
2553
Aart Bika19616e2016-02-01 18:57:58 -08002554static void GenBitCount(X86Assembler* assembler,
2555 CodeGeneratorX86* codegen,
2556 HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002557 LocationSummary* locations = invoke->GetLocations();
2558 Location src = locations->InAt(0);
2559 Register out = locations->Out().AsRegister<Register>();
2560
2561 if (invoke->InputAt(0)->IsConstant()) {
2562 // Evaluate this at compile time.
2563 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002564 int32_t result = is_long
Aart Bikc39dac12016-01-21 08:59:48 -08002565 ? POPCOUNT(static_cast<uint64_t>(value))
2566 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002567 codegen->Load32BitValue(out, result);
Aart Bikc39dac12016-01-21 08:59:48 -08002568 return;
2569 }
2570
2571 // Handle the non-constant cases.
2572 if (!is_long) {
2573 if (src.IsRegister()) {
2574 __ popcntl(out, src.AsRegister<Register>());
2575 } else {
2576 DCHECK(src.IsStackSlot());
2577 __ popcntl(out, Address(ESP, src.GetStackIndex()));
2578 }
Aart Bik2a946072016-01-21 12:49:00 -08002579 } else {
2580 // The 64-bit case needs to worry about two parts.
2581 Register temp = locations->GetTemp(0).AsRegister<Register>();
2582 if (src.IsRegisterPair()) {
2583 __ popcntl(temp, src.AsRegisterPairLow<Register>());
2584 __ popcntl(out, src.AsRegisterPairHigh<Register>());
2585 } else {
2586 DCHECK(src.IsDoubleStackSlot());
2587 __ popcntl(temp, Address(ESP, src.GetStackIndex()));
2588 __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize)));
2589 }
2590 __ addl(out, temp);
Aart Bikc39dac12016-01-21 08:59:48 -08002591 }
Aart Bikc39dac12016-01-21 08:59:48 -08002592}
2593
2594void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002595 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002596}
2597
2598void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002599 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002600}
2601
2602void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002603 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002604}
2605
2606void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002607 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002608}
2609
Vladimir Markoca6fff82017-10-03 14:49:14 +01002610static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2611 LocationSummary* locations =
2612 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002613 if (is_long) {
2614 locations->SetInAt(0, Location::RequiresRegister());
2615 } else {
2616 locations->SetInAt(0, Location::Any());
2617 }
2618 locations->SetOut(Location::RequiresRegister());
2619}
2620
Aart Bika19616e2016-02-01 18:57:58 -08002621static void GenLeadingZeros(X86Assembler* assembler,
2622 CodeGeneratorX86* codegen,
2623 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002624 LocationSummary* locations = invoke->GetLocations();
2625 Location src = locations->InAt(0);
2626 Register out = locations->Out().AsRegister<Register>();
2627
2628 if (invoke->InputAt(0)->IsConstant()) {
2629 // Evaluate this at compile time.
2630 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2631 if (value == 0) {
2632 value = is_long ? 64 : 32;
2633 } else {
2634 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2635 }
Aart Bika19616e2016-02-01 18:57:58 -08002636 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002637 return;
2638 }
2639
2640 // Handle the non-constant cases.
2641 if (!is_long) {
2642 if (src.IsRegister()) {
2643 __ bsrl(out, src.AsRegister<Register>());
2644 } else {
2645 DCHECK(src.IsStackSlot());
2646 __ bsrl(out, Address(ESP, src.GetStackIndex()));
2647 }
2648
2649 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002650 NearLabel all_zeroes, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002651 __ j(kEqual, &all_zeroes);
2652
2653 // Correct the result from BSR to get the final CLZ result.
2654 __ xorl(out, Immediate(31));
2655 __ jmp(&done);
2656
2657 // Fix the zero case with the expected result.
2658 __ Bind(&all_zeroes);
2659 __ movl(out, Immediate(32));
2660
2661 __ Bind(&done);
2662 return;
2663 }
2664
2665 // 64 bit case needs to worry about both parts of the register.
2666 DCHECK(src.IsRegisterPair());
2667 Register src_lo = src.AsRegisterPairLow<Register>();
2668 Register src_hi = src.AsRegisterPairHigh<Register>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002669 NearLabel handle_low, done, all_zeroes;
Mark Mendelld5897672015-08-12 21:16:41 -04002670
2671 // Is the high word zero?
2672 __ testl(src_hi, src_hi);
2673 __ j(kEqual, &handle_low);
2674
2675 // High word is not zero. We know that the BSR result is defined in this case.
2676 __ bsrl(out, src_hi);
2677
2678 // Correct the result from BSR to get the final CLZ result.
2679 __ xorl(out, Immediate(31));
2680 __ jmp(&done);
2681
2682 // High word was zero. We have to compute the low word count and add 32.
2683 __ Bind(&handle_low);
2684 __ bsrl(out, src_lo);
2685 __ j(kEqual, &all_zeroes);
2686
2687 // We had a valid result. Use an XOR to both correct the result and add 32.
2688 __ xorl(out, Immediate(63));
2689 __ jmp(&done);
2690
2691 // All zero case.
2692 __ Bind(&all_zeroes);
2693 __ movl(out, Immediate(64));
2694
2695 __ Bind(&done);
2696}
2697
2698void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002699 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002700}
2701
2702void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002703 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002704}
2705
2706void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002707 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002708}
2709
2710void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002711 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002712}
2713
Vladimir Markoca6fff82017-10-03 14:49:14 +01002714static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2715 LocationSummary* locations =
2716 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002717 if (is_long) {
2718 locations->SetInAt(0, Location::RequiresRegister());
2719 } else {
2720 locations->SetInAt(0, Location::Any());
2721 }
2722 locations->SetOut(Location::RequiresRegister());
2723}
2724
Aart Bika19616e2016-02-01 18:57:58 -08002725static void GenTrailingZeros(X86Assembler* assembler,
2726 CodeGeneratorX86* codegen,
2727 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002728 LocationSummary* locations = invoke->GetLocations();
2729 Location src = locations->InAt(0);
2730 Register out = locations->Out().AsRegister<Register>();
2731
2732 if (invoke->InputAt(0)->IsConstant()) {
2733 // Evaluate this at compile time.
2734 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2735 if (value == 0) {
2736 value = is_long ? 64 : 32;
2737 } else {
2738 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2739 }
Aart Bika19616e2016-02-01 18:57:58 -08002740 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002741 return;
2742 }
2743
2744 // Handle the non-constant cases.
2745 if (!is_long) {
2746 if (src.IsRegister()) {
2747 __ bsfl(out, src.AsRegister<Register>());
2748 } else {
2749 DCHECK(src.IsStackSlot());
2750 __ bsfl(out, Address(ESP, src.GetStackIndex()));
2751 }
2752
2753 // BSF sets ZF if the input was zero, and the output is undefined.
2754 NearLabel done;
2755 __ j(kNotEqual, &done);
2756
2757 // Fix the zero case with the expected result.
2758 __ movl(out, Immediate(32));
2759
2760 __ Bind(&done);
2761 return;
2762 }
2763
2764 // 64 bit case needs to worry about both parts of the register.
2765 DCHECK(src.IsRegisterPair());
2766 Register src_lo = src.AsRegisterPairLow<Register>();
2767 Register src_hi = src.AsRegisterPairHigh<Register>();
2768 NearLabel done, all_zeroes;
2769
2770 // If the low word is zero, then ZF will be set. If not, we have the answer.
2771 __ bsfl(out, src_lo);
2772 __ j(kNotEqual, &done);
2773
2774 // Low word was zero. We have to compute the high word count and add 32.
2775 __ bsfl(out, src_hi);
2776 __ j(kEqual, &all_zeroes);
2777
2778 // We had a valid result. Add 32 to account for the low word being zero.
2779 __ addl(out, Immediate(32));
2780 __ jmp(&done);
2781
2782 // All zero case.
2783 __ Bind(&all_zeroes);
2784 __ movl(out, Immediate(64));
2785
2786 __ Bind(&done);
2787}
2788
2789void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002790 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002791}
2792
2793void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002794 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002795}
2796
2797void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002798 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002799}
2800
2801void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002802 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002803}
2804
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002805static bool IsSameInput(HInstruction* instruction, size_t input0, size_t input1) {
2806 return instruction->InputAt(input0) == instruction->InputAt(input1);
2807}
2808
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002809// Compute base address for the System.arraycopy intrinsic in `base`.
2810static void GenSystemArrayCopyBaseAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002812 const Register& array,
2813 const Location& pos,
2814 const Register& base) {
2815 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002816 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002817 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 DCHECK_EQ(type, DataType::Type::kReference);
2819 const int32_t element_size = DataType::Size(type);
2820 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002821 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
2822
2823 if (pos.IsConstant()) {
2824 int32_t constant = pos.GetConstant()->AsIntConstant()->GetValue();
2825 __ leal(base, Address(array, element_size * constant + data_offset));
2826 } else {
2827 __ leal(base, Address(array, pos.AsRegister<Register>(), scale_factor, data_offset));
2828 }
2829}
2830
2831// Compute end source address for the System.arraycopy intrinsic in `end`.
2832static void GenSystemArrayCopyEndAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002834 const Location& copy_length,
2835 const Register& base,
2836 const Register& end) {
2837 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002838 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002839 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 DCHECK_EQ(type, DataType::Type::kReference);
2841 const int32_t element_size = DataType::Size(type);
2842 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002843
2844 if (copy_length.IsConstant()) {
2845 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
2846 __ leal(end, Address(base, element_size * constant));
2847 } else {
2848 __ leal(end, Address(base, copy_length.AsRegister<Register>(), scale_factor, 0));
2849 }
2850}
2851
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002852void IntrinsicLocationsBuilderX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002853 // The only read barrier implementation supporting the
2854 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2855 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002856 return;
2857 }
2858
2859 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2860 if (invoke->GetLocations() != nullptr) {
2861 // Need a byte register for marking.
2862 invoke->GetLocations()->SetTempAt(1, Location::RegisterLocation(ECX));
2863
2864 static constexpr size_t kSrc = 0;
2865 static constexpr size_t kSrcPos = 1;
2866 static constexpr size_t kDest = 2;
2867 static constexpr size_t kDestPos = 3;
2868 static constexpr size_t kLength = 4;
2869
2870 if (!invoke->InputAt(kSrcPos)->IsIntConstant() &&
2871 !invoke->InputAt(kDestPos)->IsIntConstant() &&
2872 !invoke->InputAt(kLength)->IsIntConstant()) {
2873 if (!IsSameInput(invoke, kSrcPos, kDestPos) &&
2874 !IsSameInput(invoke, kSrcPos, kLength) &&
2875 !IsSameInput(invoke, kDestPos, kLength) &&
2876 !IsSameInput(invoke, kSrc, kDest)) {
2877 // Not enough registers, make the length also take a stack slot.
2878 invoke->GetLocations()->SetInAt(kLength, Location::Any());
2879 }
2880 }
2881 }
2882}
2883
2884void IntrinsicCodeGeneratorX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002885 // The only read barrier implementation supporting the
2886 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2887 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002888
2889 X86Assembler* assembler = GetAssembler();
2890 LocationSummary* locations = invoke->GetLocations();
2891
2892 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2893 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2894 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2895 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002896 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002897
2898 Register src = locations->InAt(0).AsRegister<Register>();
2899 Location src_pos = locations->InAt(1);
2900 Register dest = locations->InAt(2).AsRegister<Register>();
2901 Location dest_pos = locations->InAt(3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002902 Location length_arg = locations->InAt(4);
2903 Location length = length_arg;
2904 Location temp1_loc = locations->GetTemp(0);
2905 Register temp1 = temp1_loc.AsRegister<Register>();
2906 Location temp2_loc = locations->GetTemp(1);
2907 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002908
Vladimir Marko174b2e22017-10-12 13:34:49 +01002909 SlowPathCode* intrinsic_slow_path =
2910 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002911 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002912
2913 NearLabel conditions_on_positions_validated;
2914 SystemArrayCopyOptimizations optimizations(invoke);
2915
2916 // If source and destination are the same, we go to slow path if we need to do
2917 // forward copying.
2918 if (src_pos.IsConstant()) {
2919 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2920 if (dest_pos.IsConstant()) {
2921 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2922 if (optimizations.GetDestinationIsSource()) {
2923 // Checked when building locations.
2924 DCHECK_GE(src_pos_constant, dest_pos_constant);
2925 } else if (src_pos_constant < dest_pos_constant) {
2926 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002927 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002928 }
2929 } else {
2930 if (!optimizations.GetDestinationIsSource()) {
2931 __ cmpl(src, dest);
2932 __ j(kNotEqual, &conditions_on_positions_validated);
2933 }
2934 __ cmpl(dest_pos.AsRegister<Register>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002935 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002936 }
2937 } else {
2938 if (!optimizations.GetDestinationIsSource()) {
2939 __ cmpl(src, dest);
2940 __ j(kNotEqual, &conditions_on_positions_validated);
2941 }
2942 if (dest_pos.IsConstant()) {
2943 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2944 __ cmpl(src_pos.AsRegister<Register>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002945 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002946 } else {
2947 __ cmpl(src_pos.AsRegister<Register>(), dest_pos.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002948 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002949 }
2950 }
2951
2952 __ Bind(&conditions_on_positions_validated);
2953
2954 if (!optimizations.GetSourceIsNotNull()) {
2955 // Bail out if the source is null.
2956 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01002957 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002958 }
2959
2960 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2961 // Bail out if the destination is null.
2962 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002963 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002964 }
2965
Roland Levillain0b671c02016-08-19 12:02:34 +01002966 Location temp3_loc = locations->GetTemp(2);
2967 Register temp3 = temp3_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002968 if (length.IsStackSlot()) {
2969 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2970 length = Location::RegisterLocation(temp3);
2971 }
2972
2973 // If the length is negative, bail out.
2974 // We have already checked in the LocationsBuilder for the constant case.
2975 if (!length.IsConstant() &&
2976 !optimizations.GetCountIsSourceLength() &&
2977 !optimizations.GetCountIsDestinationLength()) {
2978 __ testl(length.AsRegister<Register>(), length.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002979 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002980 }
2981
2982 // Validity checks: source.
2983 CheckPosition(assembler,
2984 src_pos,
2985 src,
2986 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002987 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002988 temp1,
2989 optimizations.GetCountIsSourceLength());
2990
2991 // Validity checks: dest.
2992 CheckPosition(assembler,
2993 dest_pos,
2994 dest,
2995 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002996 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002997 temp1,
2998 optimizations.GetCountIsDestinationLength());
2999
3000 if (!optimizations.GetDoesNotNeedTypeCheck()) {
3001 // Check whether all elements of the source array are assignable to the component
3002 // type of the destination array. We do two checks: the classes are the same,
3003 // or the destination is Object[]. If none of these checks succeed, we go to the
3004 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01003005
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003006 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01003007 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3008 // /* HeapReference<Class> */ temp1 = src->klass_
3009 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003010 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003011 // Bail out if the source is not a non primitive array.
3012 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3013 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003014 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003015 __ testl(temp1, temp1);
3016 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3017 // If heap poisoning is enabled, `temp1` has been unpoisoned
3018 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3019 } else {
3020 // /* HeapReference<Class> */ temp1 = src->klass_
3021 __ movl(temp1, Address(src, class_offset));
3022 __ MaybeUnpoisonHeapReference(temp1);
3023 // Bail out if the source is not a non primitive array.
3024 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3025 __ movl(temp1, Address(temp1, component_offset));
3026 __ testl(temp1, temp1);
3027 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3028 __ MaybeUnpoisonHeapReference(temp1);
3029 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003030 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01003031 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003032 }
3033
Roland Levillain0b671c02016-08-19 12:02:34 +01003034 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3035 if (length.Equals(Location::RegisterLocation(temp3))) {
3036 // When Baker read barriers are enabled, register `temp3`,
3037 // which in the present case contains the `length` parameter,
3038 // will be overwritten below. Make the `length` location
3039 // reference the original stack location; it will be moved
3040 // back to `temp3` later if necessary.
3041 DCHECK(length_arg.IsStackSlot());
3042 length = length_arg;
3043 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003044
Roland Levillain0b671c02016-08-19 12:02:34 +01003045 // /* HeapReference<Class> */ temp1 = dest->klass_
3046 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003047 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003048
Roland Levillain0b671c02016-08-19 12:02:34 +01003049 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
3050 // Bail out if the destination is not a non primitive array.
3051 //
3052 // Register `temp1` is not trashed by the read barrier emitted
3053 // by GenerateFieldLoadWithBakerReadBarrier below, as that
3054 // method produces a call to a ReadBarrierMarkRegX entry point,
3055 // which saves all potentially live registers, including
3056 // temporaries such a `temp1`.
3057 // /* HeapReference<Class> */ temp2 = temp1->component_type_
3058 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003059 invoke, temp2_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003060 __ testl(temp2, temp2);
3061 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3062 // If heap poisoning is enabled, `temp2` has been unpoisoned
3063 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3064 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
3065 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3066 }
3067
3068 // For the same reason given earlier, `temp1` is not trashed by the
3069 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
3070 // /* HeapReference<Class> */ temp2 = src->klass_
3071 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003072 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003073 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
3074 __ cmpl(temp1, temp2);
3075
3076 if (optimizations.GetDestinationIsTypedObjectArray()) {
3077 NearLabel do_copy;
3078 __ j(kEqual, &do_copy);
3079 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3080 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003081 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003082 // We do not need to emit a read barrier for the following
3083 // heap reference load, as `temp1` is only used in a
3084 // comparison with null below, and this reference is not
3085 // kept afterwards.
3086 __ cmpl(Address(temp1, super_offset), Immediate(0));
3087 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3088 __ Bind(&do_copy);
3089 } else {
3090 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3091 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003092 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01003093 // Non read barrier code.
3094
3095 // /* HeapReference<Class> */ temp1 = dest->klass_
3096 __ movl(temp1, Address(dest, class_offset));
3097 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
3098 __ MaybeUnpoisonHeapReference(temp1);
3099 // Bail out if the destination is not a non primitive array.
3100 // /* HeapReference<Class> */ temp2 = temp1->component_type_
3101 __ movl(temp2, Address(temp1, component_offset));
3102 __ testl(temp2, temp2);
3103 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3104 __ MaybeUnpoisonHeapReference(temp2);
3105 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
3106 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3107 // Re-poison the heap reference to make the compare instruction below
3108 // compare two poisoned references.
3109 __ PoisonHeapReference(temp1);
3110 }
3111
3112 // Note: if heap poisoning is on, we are comparing two poisoned references here.
3113 __ cmpl(temp1, Address(src, class_offset));
3114
3115 if (optimizations.GetDestinationIsTypedObjectArray()) {
3116 NearLabel do_copy;
3117 __ j(kEqual, &do_copy);
3118 __ MaybeUnpoisonHeapReference(temp1);
3119 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3120 __ movl(temp1, Address(temp1, component_offset));
3121 __ MaybeUnpoisonHeapReference(temp1);
3122 __ cmpl(Address(temp1, super_offset), Immediate(0));
3123 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3124 __ Bind(&do_copy);
3125 } else {
3126 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3127 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003128 }
3129 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
3130 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
3131 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01003132 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3133 // /* HeapReference<Class> */ temp1 = src->klass_
3134 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003135 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003136 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3137 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003138 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003139 __ testl(temp1, temp1);
3140 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3141 // If heap poisoning is enabled, `temp1` has been unpoisoned
3142 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3143 } else {
3144 // /* HeapReference<Class> */ temp1 = src->klass_
3145 __ movl(temp1, Address(src, class_offset));
3146 __ MaybeUnpoisonHeapReference(temp1);
3147 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3148 __ movl(temp1, Address(temp1, component_offset));
3149 __ testl(temp1, temp1);
3150 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3151 __ MaybeUnpoisonHeapReference(temp1);
3152 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003153 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01003154 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003155 }
3156
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003157 const DataType::Type type = DataType::Type::kReference;
3158 const int32_t element_size = DataType::Size(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003159
Roland Levillain0b671c02016-08-19 12:02:34 +01003160 // Compute the base source address in `temp1`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003161 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003162
Roland Levillain0b671c02016-08-19 12:02:34 +01003163 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3164 // If it is needed (in the case of the fast-path loop), the base
3165 // destination address is computed later, as `temp2` is used for
3166 // intermediate computations.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003167
Roland Levillain0b671c02016-08-19 12:02:34 +01003168 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003169 if (length.IsStackSlot()) {
3170 // Location `length` is again pointing at a stack slot, as
3171 // register `temp3` (which was containing the length parameter
3172 // earlier) has been overwritten; restore it now
3173 DCHECK(length.Equals(length_arg));
3174 __ movl(temp3, Address(ESP, length.GetStackIndex()));
3175 length = Location::RegisterLocation(temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01003176 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003177 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003178
Roland Levillain0b671c02016-08-19 12:02:34 +01003179 // SystemArrayCopy implementation for Baker read barriers (see
3180 // also CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier):
3181 //
3182 // if (src_ptr != end_ptr) {
3183 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
3184 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003185 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01003186 // if (is_gray) {
3187 // // Slow-path copy.
3188 // for (size_t i = 0; i != length; ++i) {
3189 // dest_array[dest_pos + i] =
3190 // MaybePoison(ReadBarrier::Mark(MaybeUnpoison(src_array[src_pos + i])));
3191 // }
3192 // } else {
3193 // // Fast-path copy.
3194 // do {
3195 // *dest_ptr++ = *src_ptr++;
3196 // } while (src_ptr != end_ptr)
3197 // }
3198 // }
3199
3200 NearLabel loop, done;
3201
3202 // Don't enter copy loop if `length == 0`.
3203 __ cmpl(temp1, temp3);
3204 __ j(kEqual, &done);
3205
Vladimir Marko953437b2016-08-24 08:30:46 +00003206 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003207 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
3208 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00003209 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
3210 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
3211 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
3212
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003213 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00003214 // goto slow_path;
3215 // At this point, just do the "if" and make sure that flags are preserved until the branch.
3216 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01003217
3218 // Load fence to prevent load-load reordering.
3219 // Note that this is a no-op, thanks to the x86 memory model.
3220 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3221
3222 // Slow path used to copy array when `src` is gray.
3223 SlowPathCode* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003224 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01003225 codegen_->AddSlowPath(read_barrier_slow_path);
3226
Vladimir Marko953437b2016-08-24 08:30:46 +00003227 // We have done the "if" of the gray bit check above, now branch based on the flags.
3228 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01003229
3230 // Fast-path copy.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003231 // Compute the base destination address in `temp2`.
3232 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01003233 // Iterate over the arrays and do a raw copy of the objects. We don't need to
3234 // poison/unpoison.
3235 __ Bind(&loop);
3236 __ pushl(Address(temp1, 0));
3237 __ cfi().AdjustCFAOffset(4);
3238 __ popl(Address(temp2, 0));
3239 __ cfi().AdjustCFAOffset(-4);
3240 __ addl(temp1, Immediate(element_size));
3241 __ addl(temp2, Immediate(element_size));
3242 __ cmpl(temp1, temp3);
3243 __ j(kNotEqual, &loop);
3244
3245 __ Bind(read_barrier_slow_path->GetExitLabel());
3246 __ Bind(&done);
3247 } else {
3248 // Non read barrier code.
Roland Levillain0b671c02016-08-19 12:02:34 +01003249 // Compute the base destination address in `temp2`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003250 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01003251 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003252 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01003253 // Iterate over the arrays and do a raw copy of the objects. We don't need to
3254 // poison/unpoison.
3255 NearLabel loop, done;
3256 __ cmpl(temp1, temp3);
3257 __ j(kEqual, &done);
3258 __ Bind(&loop);
3259 __ pushl(Address(temp1, 0));
3260 __ cfi().AdjustCFAOffset(4);
3261 __ popl(Address(temp2, 0));
3262 __ cfi().AdjustCFAOffset(-4);
3263 __ addl(temp1, Immediate(element_size));
3264 __ addl(temp2, Immediate(element_size));
3265 __ cmpl(temp1, temp3);
3266 __ j(kNotEqual, &loop);
3267 __ Bind(&done);
3268 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003269
3270 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003271 codegen_->MarkGCCard(temp1, temp2, dest, Register(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003272
Roland Levillain0b671c02016-08-19 12:02:34 +01003273 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003274}
3275
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003276void IntrinsicLocationsBuilderX86::VisitIntegerValueOf(HInvoke* invoke) {
3277 InvokeRuntimeCallingConvention calling_convention;
3278 IntrinsicVisitor::ComputeIntegerValueOfLocations(
3279 invoke,
3280 codegen_,
3281 Location::RegisterLocation(EAX),
3282 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3283}
3284
3285void IntrinsicCodeGeneratorX86::VisitIntegerValueOf(HInvoke* invoke) {
3286 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
3287 LocationSummary* locations = invoke->GetLocations();
3288 X86Assembler* assembler = GetAssembler();
3289
3290 Register out = locations->Out().AsRegister<Register>();
3291 InvokeRuntimeCallingConvention calling_convention;
3292 if (invoke->InputAt(0)->IsConstant()) {
3293 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
3294 if (value >= info.low && value <= info.high) {
3295 // Just embed the j.l.Integer in the code.
3296 ScopedObjectAccess soa(Thread::Current());
3297 mirror::Object* boxed = info.cache->Get(value + (-info.low));
3298 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
3299 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
3300 __ movl(out, Immediate(address));
3301 } else {
3302 // Allocate and initialize a new j.l.Integer.
3303 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
3304 // JIT object table.
3305 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3306 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
3307 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3308 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3309 __ movl(Address(out, info.value_offset), Immediate(value));
3310 }
3311 } else {
3312 Register in = locations->InAt(0).AsRegister<Register>();
3313 // Check bounds of our cache.
3314 __ leal(out, Address(in, -info.low));
3315 __ cmpl(out, Immediate(info.high - info.low + 1));
3316 NearLabel allocate, done;
3317 __ j(kAboveEqual, &allocate);
3318 // If the value is within the bounds, load the j.l.Integer directly from the array.
3319 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3320 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3321 __ movl(out, Address(out, TIMES_4, data_offset + address));
3322 __ MaybeUnpoisonHeapReference(out);
3323 __ jmp(&done);
3324 __ Bind(&allocate);
3325 // Otherwise allocate and initialize a new j.l.Integer.
3326 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3327 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
3328 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3329 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3330 __ movl(Address(out, info.value_offset), in);
3331 __ Bind(&done);
3332 }
3333}
3334
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003335void IntrinsicLocationsBuilderX86::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003336 LocationSummary* locations =
3337 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003338 locations->SetOut(Location::RequiresRegister());
3339}
3340
3341void IntrinsicCodeGeneratorX86::VisitThreadInterrupted(HInvoke* invoke) {
3342 X86Assembler* assembler = GetAssembler();
3343 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
3344 Address address = Address::Absolute(Thread::InterruptedOffset<kX86PointerSize>().Int32Value());
3345 NearLabel done;
3346 __ fs()->movl(out, address);
3347 __ testl(out, out);
3348 __ j(kEqual, &done);
3349 __ fs()->movl(address, Immediate(0));
3350 codegen_->MemoryFence();
3351 __ Bind(&done);
3352}
3353
3354
Aart Bik2f9fcc92016-03-01 15:16:54 -08003355UNIMPLEMENTED_INTRINSIC(X86, MathRoundDouble)
Vladimir Marko4ee8e292017-06-02 15:39:30 +00003356UNIMPLEMENTED_INTRINSIC(X86, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003357UNIMPLEMENTED_INTRINSIC(X86, FloatIsInfinite)
3358UNIMPLEMENTED_INTRINSIC(X86, DoubleIsInfinite)
3359UNIMPLEMENTED_INTRINSIC(X86, IntegerHighestOneBit)
3360UNIMPLEMENTED_INTRINSIC(X86, LongHighestOneBit)
3361UNIMPLEMENTED_INTRINSIC(X86, IntegerLowestOneBit)
3362UNIMPLEMENTED_INTRINSIC(X86, LongLowestOneBit)
Mark Mendell09ed1a32015-03-25 08:30:06 -04003363
Aart Bikff7d89c2016-11-07 08:49:28 -08003364UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOf);
3365UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003366UNIMPLEMENTED_INTRINSIC(X86, StringBufferAppend);
3367UNIMPLEMENTED_INTRINSIC(X86, StringBufferLength);
3368UNIMPLEMENTED_INTRINSIC(X86, StringBufferToString);
3369UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppend);
3370UNIMPLEMENTED_INTRINSIC(X86, StringBuilderLength);
3371UNIMPLEMENTED_INTRINSIC(X86, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003372
Aart Bik0e54c012016-03-04 12:08:31 -08003373// 1.8.
3374UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddInt)
3375UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddLong)
3376UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetInt)
3377UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetLong)
3378UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003379
Aart Bik2f9fcc92016-03-01 15:16:54 -08003380UNREACHABLE_INTRINSICS(X86)
Roland Levillain4d027112015-07-01 15:41:14 +01003381
3382#undef __
3383
Mark Mendell09ed1a32015-03-25 08:30:06 -04003384} // namespace x86
3385} // namespace art