blob: 8b389ba8769bc7ced552e049edc453641263130c [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
Andreas Gampe85b62f22015-09-09 13:15:38 -07001256 SlowPathCode* slow_path = new (GetAllocator()) 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);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001339 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001340 codegen_->AddSlowPath(slow_path);
1341 __ j(kEqual, slow_path->GetEntryLabel());
1342
Serban Constantinescuba45db02016-07-12 22:53:02 +01001343 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001344 __ Bind(slow_path->GetExitLabel());
1345}
1346
Agi Csakid7138c82015-08-13 17:46:44 -07001347void IntrinsicLocationsBuilderX86::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001348 LocationSummary* locations =
1349 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakid7138c82015-08-13 17:46:44 -07001350 locations->SetInAt(0, Location::RequiresRegister());
1351 locations->SetInAt(1, Location::RequiresRegister());
1352
1353 // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction.
1354 locations->AddTemp(Location::RegisterLocation(ECX));
1355 locations->AddTemp(Location::RegisterLocation(EDI));
1356
1357 // Set output, ESI needed for repe_cmpsl instruction anyways.
1358 locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap);
1359}
1360
1361void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) {
1362 X86Assembler* assembler = GetAssembler();
1363 LocationSummary* locations = invoke->GetLocations();
1364
1365 Register str = locations->InAt(0).AsRegister<Register>();
1366 Register arg = locations->InAt(1).AsRegister<Register>();
1367 Register ecx = locations->GetTemp(0).AsRegister<Register>();
1368 Register edi = locations->GetTemp(1).AsRegister<Register>();
1369 Register esi = locations->Out().AsRegister<Register>();
1370
Mark Mendell0c9497d2015-08-21 09:30:05 -04001371 NearLabel end, return_true, return_false;
Agi Csakid7138c82015-08-13 17:46:44 -07001372
1373 // Get offsets of count, value, and class fields within a string object.
1374 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1375 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1376 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1377
1378 // Note that the null check must have been done earlier.
1379 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1380
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001381 StringEqualsOptimizations optimizations(invoke);
1382 if (!optimizations.GetArgumentNotNull()) {
1383 // Check if input is null, return false if it is.
1384 __ testl(arg, arg);
1385 __ j(kEqual, &return_false);
1386 }
Agi Csakid7138c82015-08-13 17:46:44 -07001387
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001388 if (!optimizations.GetArgumentIsString()) {
Vladimir Marko53b52002016-05-24 19:30:45 +01001389 // Instanceof check for the argument by comparing class fields.
1390 // All string objects must have the same type since String cannot be subclassed.
1391 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1392 // If the argument is a string object, its class field must be equal to receiver's class field.
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001393 __ movl(ecx, Address(str, class_offset));
1394 __ cmpl(ecx, Address(arg, class_offset));
1395 __ j(kNotEqual, &return_false);
1396 }
Agi Csakid7138c82015-08-13 17:46:44 -07001397
1398 // Reference equality check, return true if same reference.
1399 __ cmpl(str, arg);
1400 __ j(kEqual, &return_true);
1401
jessicahandojo4877b792016-09-08 19:49:13 -07001402 // Load length and compression flag of receiver string.
Agi Csakid7138c82015-08-13 17:46:44 -07001403 __ movl(ecx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001404 // Check if lengths and compression flags are equal, return false if they're not.
1405 // Two identical strings will always have same compression style since
1406 // compression style is decided on alloc.
Agi Csakid7138c82015-08-13 17:46:44 -07001407 __ cmpl(ecx, Address(arg, count_offset));
1408 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001409 // Return true if strings are empty. Even with string compression `count == 0` means empty.
1410 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1411 "Expecting 0=compressed, 1=uncompressed");
1412 __ jecxz(&return_true);
Agi Csakid7138c82015-08-13 17:46:44 -07001413
jessicahandojo4877b792016-09-08 19:49:13 -07001414 if (mirror::kUseStringCompression) {
1415 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001416 // Extract length and differentiate between both compressed or both uncompressed.
1417 // Different compression style is cut above.
1418 __ shrl(ecx, Immediate(1));
1419 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001420 // Divide string length by 2, rounding up, and continue as if uncompressed.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001421 __ addl(ecx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001422 __ shrl(ecx, Immediate(1));
1423 __ Bind(&string_uncompressed);
1424 }
Agi Csakid7138c82015-08-13 17:46:44 -07001425 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1426 __ leal(esi, Address(str, value_offset));
1427 __ leal(edi, Address(arg, value_offset));
1428
jessicahandojo4877b792016-09-08 19:49:13 -07001429 // Divide string length by 2 to compare characters 2 at a time and adjust for lengths not
1430 // divisible by 2.
Agi Csakid7138c82015-08-13 17:46:44 -07001431 __ addl(ecx, Immediate(1));
1432 __ shrl(ecx, Immediate(1));
1433
jessicahandojo4877b792016-09-08 19:49:13 -07001434 // Assertions that must hold in order to compare strings 2 characters (uncompressed)
1435 // or 4 characters (compressed) at a time.
Agi Csakid7138c82015-08-13 17:46:44 -07001436 DCHECK_ALIGNED(value_offset, 4);
1437 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1438
1439 // Loop to compare strings two characters at a time starting at the beginning of the string.
1440 __ repe_cmpsl();
1441 // If strings are not equal, zero flag will be cleared.
1442 __ j(kNotEqual, &return_false);
1443
1444 // Return true and exit the function.
1445 // If loop does not result in returning false, we return true.
1446 __ Bind(&return_true);
1447 __ movl(esi, Immediate(1));
1448 __ jmp(&end);
1449
1450 // Return false and exit the function.
1451 __ Bind(&return_false);
1452 __ xorl(esi, esi);
1453 __ Bind(&end);
1454}
1455
Andreas Gampe21030dd2015-05-07 14:46:15 -07001456static void CreateStringIndexOfLocations(HInvoke* invoke,
1457 ArenaAllocator* allocator,
1458 bool start_at_zero) {
1459 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1460 LocationSummary::kCallOnSlowPath,
1461 kIntrinsified);
1462 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1463 locations->SetInAt(0, Location::RegisterLocation(EDI));
1464 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1465 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1466 // of the instruction explicitly.
1467 // Note: This works as we don't clobber EAX anywhere.
1468 locations->SetInAt(1, Location::RegisterLocation(EAX));
1469 if (!start_at_zero) {
1470 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1471 }
1472 // As we clobber EDI during execution anyways, also use it as the output.
1473 locations->SetOut(Location::SameAsFirstInput());
1474
1475 // repne scasw uses ECX as the counter.
1476 locations->AddTemp(Location::RegisterLocation(ECX));
1477 // Need another temporary to be able to compute the result.
1478 locations->AddTemp(Location::RequiresRegister());
jessicahandojo4877b792016-09-08 19:49:13 -07001479 if (mirror::kUseStringCompression) {
1480 // Need another temporary to be able to save unflagged string length.
1481 locations->AddTemp(Location::RequiresRegister());
1482 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001483}
1484
1485static void GenerateStringIndexOf(HInvoke* invoke,
1486 X86Assembler* assembler,
1487 CodeGeneratorX86* codegen,
1488 ArenaAllocator* allocator,
1489 bool start_at_zero) {
1490 LocationSummary* locations = invoke->GetLocations();
1491
1492 // Note that the null check must have been done earlier.
1493 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1494
1495 Register string_obj = locations->InAt(0).AsRegister<Register>();
1496 Register search_value = locations->InAt(1).AsRegister<Register>();
1497 Register counter = locations->GetTemp(0).AsRegister<Register>();
1498 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1499 Register out = locations->Out().AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07001500 // Only used when string compression feature is on.
1501 Register string_length_flagged;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001502
1503 // Check our assumptions for registers.
1504 DCHECK_EQ(string_obj, EDI);
1505 DCHECK_EQ(search_value, EAX);
1506 DCHECK_EQ(counter, ECX);
1507 DCHECK_EQ(out, EDI);
1508
1509 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001510 // 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 -07001511 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001512 HInstruction* code_point = invoke->InputAt(1);
1513 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001514 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001515 std::numeric_limits<uint16_t>::max()) {
1516 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1517 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1518 slow_path = new (allocator) IntrinsicSlowPathX86(invoke);
1519 codegen->AddSlowPath(slow_path);
1520 __ jmp(slow_path->GetEntryLabel());
1521 __ Bind(slow_path->GetExitLabel());
1522 return;
1523 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001524 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001525 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1526 slow_path = new (allocator) IntrinsicSlowPathX86(invoke);
1527 codegen->AddSlowPath(slow_path);
1528 __ j(kAbove, slow_path->GetEntryLabel());
1529 }
1530
1531 // From here down, we know that we are looking for a char that fits in 16 bits.
1532 // Location of reference to data array within the String object.
1533 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1534 // Location of count within the String object.
1535 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1536
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001537 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001538 __ movl(string_length, Address(string_obj, count_offset));
1539
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001540 // Do a zero-length check. Even with string compression `count == 0` means empty.
1541 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1542 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe21030dd2015-05-07 14:46:15 -07001543 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001544 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001545 __ testl(string_length, string_length);
1546 __ j(kEqual, &not_found_label);
1547
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001548 if (mirror::kUseStringCompression) {
1549 string_length_flagged = locations->GetTemp(2).AsRegister<Register>();
1550 __ movl(string_length_flagged, string_length);
1551 // Extract the length and shift out the least significant bit used as compression flag.
1552 __ shrl(string_length, Immediate(1));
1553 }
1554
Andreas Gampe21030dd2015-05-07 14:46:15 -07001555 if (start_at_zero) {
1556 // Number of chars to scan is the same as the string length.
1557 __ movl(counter, string_length);
1558
1559 // Move to the start of the string.
1560 __ addl(string_obj, Immediate(value_offset));
1561 } else {
1562 Register start_index = locations->InAt(2).AsRegister<Register>();
1563
1564 // Do a start_index check.
1565 __ cmpl(start_index, string_length);
1566 __ j(kGreaterEqual, &not_found_label);
1567
1568 // Ensure we have a start index >= 0;
1569 __ xorl(counter, counter);
1570 __ cmpl(start_index, Immediate(0));
1571 __ cmovl(kGreater, counter, start_index);
1572
jessicahandojo4877b792016-09-08 19:49:13 -07001573 if (mirror::kUseStringCompression) {
1574 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001575 __ testl(string_length_flagged, Immediate(1));
1576 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001577 // Move to the start of the string: string_obj + value_offset + start_index.
1578 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1579 __ jmp(&modify_counter);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001580
jessicahandojo4877b792016-09-08 19:49:13 -07001581 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1582 __ Bind(&offset_uncompressed_label);
1583 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1584
1585 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1586 // compare.
1587 __ Bind(&modify_counter);
1588 } else {
1589 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1590 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001591 __ negl(counter);
1592 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1593 }
1594
jessicahandojo4877b792016-09-08 19:49:13 -07001595 if (mirror::kUseStringCompression) {
1596 NearLabel uncompressed_string_comparison;
1597 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001598 __ testl(string_length_flagged, Immediate(1));
1599 __ j(kNotZero, &uncompressed_string_comparison);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001600
jessicahandojo4877b792016-09-08 19:49:13 -07001601 // Check if EAX (search_value) is ASCII.
1602 __ cmpl(search_value, Immediate(127));
1603 __ j(kGreater, &not_found_label);
1604 // Comparing byte-per-byte.
1605 __ repne_scasb();
1606 __ jmp(&comparison_done);
1607
1608 // Everything is set up for repne scasw:
1609 // * Comparison address in EDI.
1610 // * Counter in ECX.
1611 __ Bind(&uncompressed_string_comparison);
1612 __ repne_scasw();
1613 __ Bind(&comparison_done);
1614 } else {
1615 __ repne_scasw();
1616 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001617 // Did we find a match?
1618 __ j(kNotEqual, &not_found_label);
1619
1620 // Yes, we matched. Compute the index of the result.
1621 __ subl(string_length, counter);
1622 __ leal(out, Address(string_length, -1));
1623
Mark Mendell0c9497d2015-08-21 09:30:05 -04001624 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001625 __ jmp(&done);
1626
1627 // Failed to match; return -1.
1628 __ Bind(&not_found_label);
1629 __ movl(out, Immediate(-1));
1630
1631 // And join up at the end.
1632 __ Bind(&done);
1633 if (slow_path != nullptr) {
1634 __ Bind(slow_path->GetExitLabel());
1635 }
1636}
1637
1638void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001639 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001640}
1641
1642void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001643 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001644}
1645
1646void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001647 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001648}
1649
1650void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001651 GenerateStringIndexOf(
1652 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001653}
1654
Jeff Hao848f70a2014-01-15 13:49:50 -08001655void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001656 LocationSummary* locations = new (allocator_) LocationSummary(
1657 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001658 InvokeRuntimeCallingConvention calling_convention;
1659 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1660 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1661 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1662 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1663 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001664}
1665
1666void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1667 X86Assembler* assembler = GetAssembler();
1668 LocationSummary* locations = invoke->GetLocations();
1669
1670 Register byte_array = locations->InAt(0).AsRegister<Register>();
1671 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001672 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001673 codegen_->AddSlowPath(slow_path);
1674 __ j(kEqual, slow_path->GetEntryLabel());
1675
Serban Constantinescuba45db02016-07-12 22:53:02 +01001676 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001677 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001678 __ Bind(slow_path->GetExitLabel());
1679}
1680
1681void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001682 LocationSummary* locations =
1683 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001684 InvokeRuntimeCallingConvention calling_convention;
1685 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1686 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1687 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1688 locations->SetOut(Location::RegisterLocation(EAX));
1689}
1690
1691void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001692 // No need to emit code checking whether `locations->InAt(2)` is a null
1693 // pointer, as callers of the native method
1694 //
1695 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1696 //
1697 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001698 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001699 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001700}
1701
1702void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001703 LocationSummary* locations = new (allocator_) LocationSummary(
1704 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001705 InvokeRuntimeCallingConvention calling_convention;
1706 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1707 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001708}
1709
1710void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1711 X86Assembler* assembler = GetAssembler();
1712 LocationSummary* locations = invoke->GetLocations();
1713
1714 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1715 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001716 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001717 codegen_->AddSlowPath(slow_path);
1718 __ j(kEqual, slow_path->GetEntryLabel());
1719
Serban Constantinescuba45db02016-07-12 22:53:02 +01001720 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001721 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001722 __ Bind(slow_path->GetExitLabel());
1723}
1724
Mark Mendell8f8926a2015-08-17 11:39:06 -04001725void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1726 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001727 LocationSummary* locations =
1728 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001729 locations->SetInAt(0, Location::RequiresRegister());
1730 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1731 // Place srcEnd in ECX to save a move below.
1732 locations->SetInAt(2, Location::RegisterLocation(ECX));
1733 locations->SetInAt(3, Location::RequiresRegister());
1734 locations->SetInAt(4, Location::RequiresRegister());
1735
1736 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1737 // We don't have enough registers to also grab ECX, so handle below.
1738 locations->AddTemp(Location::RegisterLocation(ESI));
1739 locations->AddTemp(Location::RegisterLocation(EDI));
1740}
1741
1742void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1743 X86Assembler* assembler = GetAssembler();
1744 LocationSummary* locations = invoke->GetLocations();
1745
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001746 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001747 // Location of data in char array buffer.
1748 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1749 // Location of char array data in string.
1750 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1751
1752 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1753 Register obj = locations->InAt(0).AsRegister<Register>();
1754 Location srcBegin = locations->InAt(1);
1755 int srcBegin_value =
1756 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1757 Register srcEnd = locations->InAt(2).AsRegister<Register>();
1758 Register dst = locations->InAt(3).AsRegister<Register>();
1759 Register dstBegin = locations->InAt(4).AsRegister<Register>();
1760
1761 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001762 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001763 DCHECK_EQ(char_size, 2u);
1764
Mark Mendell8f8926a2015-08-17 11:39:06 -04001765 // Compute the number of chars (words) to move.
jessicahandojo4877b792016-09-08 19:49:13 -07001766 // Save ECX, since we don't know if it will be used later.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001767 __ pushl(ECX);
1768 int stack_adjust = kX86WordSize;
1769 __ cfi().AdjustCFAOffset(stack_adjust);
1770 DCHECK_EQ(srcEnd, ECX);
1771 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001772 __ subl(ECX, Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001773 } else {
1774 DCHECK(srcBegin.IsRegister());
1775 __ subl(ECX, srcBegin.AsRegister<Register>());
1776 }
1777
jessicahandojo4877b792016-09-08 19:49:13 -07001778 NearLabel done;
1779 if (mirror::kUseStringCompression) {
1780 // Location of count in string
1781 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001782 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001783 DCHECK_EQ(c_char_size, 1u);
1784 __ pushl(EAX);
1785 __ cfi().AdjustCFAOffset(stack_adjust);
1786
1787 NearLabel copy_loop, copy_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001788 __ testl(Address(obj, count_offset), Immediate(1));
1789 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1790 "Expecting 0=compressed, 1=uncompressed");
1791 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001792 // Compute the address of the source string by adding the number of chars from
1793 // the source beginning to the value offset of a string.
1794 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1795
1796 // Start the loop to copy String's value to Array of Char.
1797 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1798 __ Bind(&copy_loop);
1799 __ jecxz(&done);
1800 // Use EAX temporary (convert byte from ESI to word).
1801 // TODO: Use LODSB/STOSW (not supported by X86Assembler) with AH initialized to 0.
1802 __ movzxb(EAX, Address(ESI, 0));
1803 __ movw(Address(EDI, 0), EAX);
1804 __ leal(EDI, Address(EDI, char_size));
1805 __ leal(ESI, Address(ESI, c_char_size));
1806 // TODO: Add support for LOOP to X86Assembler.
1807 __ subl(ECX, Immediate(1));
1808 __ jmp(&copy_loop);
1809 __ Bind(&copy_uncompressed);
1810 }
1811
1812 // Do the copy for uncompressed string.
1813 // Compute the address of the destination buffer.
1814 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1815 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001816 __ rep_movsw();
1817
jessicahandojo4877b792016-09-08 19:49:13 -07001818 __ Bind(&done);
1819 if (mirror::kUseStringCompression) {
1820 // Restore EAX.
1821 __ popl(EAX);
1822 __ cfi().AdjustCFAOffset(-stack_adjust);
1823 }
1824 // Restore ECX.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001825 __ popl(ECX);
1826 __ cfi().AdjustCFAOffset(-stack_adjust);
1827}
1828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001829static void GenPeek(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001830 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1831 Location out_loc = locations->Out();
1832 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1833 // to avoid a SIGBUS.
1834 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001835 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001836 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1837 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001838 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001839 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1840 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001841 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001842 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1843 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001844 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001845 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1846 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1847 break;
1848 default:
1849 LOG(FATAL) << "Type not recognized for peek: " << size;
1850 UNREACHABLE();
1851 }
1852}
1853
1854void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001855 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001856}
1857
1858void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001859 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001860}
1861
1862void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001863 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001864}
1865
1866void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001867 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001868}
1869
1870void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001871 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001872}
1873
1874void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001875 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001876}
1877
1878void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001879 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001880}
1881
1882void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001883 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001884}
1885
Vladimir Markoca6fff82017-10-03 14:49:14 +01001886static void CreateLongIntToVoidLocations(ArenaAllocator* allocator,
1887 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001888 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001889 LocationSummary* locations =
1890 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001891 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001892 HInstruction* value = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001893 if (size == DataType::Type::kInt8) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001894 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1895 } else {
1896 locations->SetInAt(1, Location::RegisterOrConstant(value));
1897 }
1898}
1899
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001900static void GenPoke(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001901 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1902 Location value_loc = locations->InAt(1);
1903 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1904 // to avoid a SIGBUS.
1905 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001906 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001907 if (value_loc.IsConstant()) {
1908 __ movb(Address(address, 0),
1909 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1910 } else {
1911 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1912 }
1913 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001914 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001915 if (value_loc.IsConstant()) {
1916 __ movw(Address(address, 0),
1917 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1918 } else {
1919 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1920 }
1921 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001922 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001923 if (value_loc.IsConstant()) {
1924 __ movl(Address(address, 0),
1925 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1926 } else {
1927 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1928 }
1929 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001930 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001931 if (value_loc.IsConstant()) {
1932 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1933 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1934 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1935 } else {
1936 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1937 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1938 }
1939 break;
1940 default:
1941 LOG(FATAL) << "Type not recognized for poke: " << size;
1942 UNREACHABLE();
1943 }
1944}
1945
1946void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001947 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt8, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001948}
1949
1950void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001951 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001952}
1953
1954void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001955 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001956}
1957
1958void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001959 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001960}
1961
1962void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001963 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001964}
1965
1966void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001967 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001968}
1969
1970void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001971 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt16, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001972}
1973
1974void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001975 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001976}
1977
1978void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001979 LocationSummary* locations =
1980 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001981 locations->SetOut(Location::RequiresRegister());
1982}
1983
1984void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1985 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001986 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86PointerSize>()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001987}
1988
Roland Levillain0d5a2812015-11-13 10:07:31 +00001989static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001990 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001991 bool is_volatile,
1992 CodeGeneratorX86* codegen) {
1993 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
1994 LocationSummary* locations = invoke->GetLocations();
1995 Location base_loc = locations->InAt(1);
1996 Register base = base_loc.AsRegister<Register>();
1997 Location offset_loc = locations->InAt(2);
1998 Register offset = offset_loc.AsRegisterPairLow<Register>();
1999 Location output_loc = locations->Out();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002000
2001 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 case DataType::Type::kInt32: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002003 Register output = output_loc.AsRegister<Register>();
2004 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain7c1559a2015-12-15 10:55:36 +00002005 break;
2006 }
2007
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002008 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002009 Register output = output_loc.AsRegister<Register>();
2010 if (kEmitCompilerReadBarrier) {
2011 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002012 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2013 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002014 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00002015 } else {
2016 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2017 codegen->GenerateReadBarrierSlow(
2018 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2019 }
2020 } else {
2021 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2022 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002023 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002024 break;
Roland Levillain4d027112015-07-01 15:41:14 +01002025 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 case DataType::Type::kInt64: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002028 Register output_lo = output_loc.AsRegisterPairLow<Register>();
2029 Register output_hi = output_loc.AsRegisterPairHigh<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002030 if (is_volatile) {
2031 // Need to use a XMM to read atomically.
2032 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2033 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
2034 __ movd(output_lo, temp);
2035 __ psrlq(temp, Immediate(32));
2036 __ movd(output_hi, temp);
2037 } else {
2038 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
2039 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
2040 }
2041 }
2042 break;
2043
2044 default:
2045 LOG(FATAL) << "Unsupported op size " << type;
2046 UNREACHABLE();
2047 }
2048}
2049
Vladimir Markoca6fff82017-10-03 14:49:14 +01002050static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Roland Levillain7c1559a2015-12-15 10:55:36 +00002051 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002052 DataType::Type type,
Roland Levillain7c1559a2015-12-15 10:55:36 +00002053 bool is_volatile) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002054 bool can_call = kEmitCompilerReadBarrier &&
2055 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2056 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002057 LocationSummary* locations =
2058 new (allocator) LocationSummary(invoke,
2059 can_call
2060 ? LocationSummary::kCallOnSlowPath
2061 : LocationSummary::kNoCall,
2062 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01002063 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002064 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002065 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002066 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2067 locations->SetInAt(1, Location::RequiresRegister());
2068 locations->SetInAt(2, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002070 if (is_volatile) {
2071 // Need to use XMM to read volatile.
2072 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002074 } else {
2075 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2076 }
2077 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01002078 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002079 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Mark Mendell09ed1a32015-03-25 08:30:06 -04002080 }
2081}
2082
2083void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002084 CreateIntIntIntToIntLocations(
2085 allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002086}
2087void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002088 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002089}
2090void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002091 CreateIntIntIntToIntLocations(
2092 allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002093}
2094void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002095 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002096}
2097void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002098 CreateIntIntIntToIntLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002099 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002100}
2101void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002102 CreateIntIntIntToIntLocations(
2103 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002104}
2105
2106
2107void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002108 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002109}
2110void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002111 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002112}
2113void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002115}
2116void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002117 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002118}
2119void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002121}
2122void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002123 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002124}
2125
2126
Vladimir Markoca6fff82017-10-03 14:49:14 +01002127static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04002129 HInvoke* invoke,
2130 bool is_volatile) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002131 LocationSummary* locations =
2132 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002133 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2134 locations->SetInAt(1, Location::RequiresRegister());
2135 locations->SetInAt(2, Location::RequiresRegister());
2136 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002137 if (type == DataType::Type::kReference) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002138 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002139 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04002140 // Ensure the value is in a byte register.
2141 locations->AddTemp(Location::RegisterLocation(ECX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002142 } else if (type == DataType::Type::kInt64 && is_volatile) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002143 locations->AddTemp(Location::RequiresFpuRegister());
2144 locations->AddTemp(Location::RequiresFpuRegister());
2145 }
2146}
2147
2148void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002149 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002150 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002151}
2152void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002153 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002154 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002155}
2156void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002157 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002158 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002159}
2160void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002161 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002162 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002163}
2164void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002165 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002166 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002167}
2168void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002169 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002170 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002171}
2172void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002173 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002174 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002175}
2176void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002177 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002178 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002179}
2180void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002181 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002182 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002183}
2184
2185// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2186// memory model.
2187static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002188 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04002189 bool is_volatile,
2190 CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002191 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04002192 Register base = locations->InAt(1).AsRegister<Register>();
2193 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2194 Location value_loc = locations->InAt(3);
2195
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002196 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002197 Register value_lo = value_loc.AsRegisterPairLow<Register>();
2198 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
2199 if (is_volatile) {
2200 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2201 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2202 __ movd(temp1, value_lo);
2203 __ movd(temp2, value_hi);
2204 __ punpckldq(temp1, temp2);
2205 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
2206 } else {
2207 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
2208 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
2209 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002210 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002211 Register temp = locations->GetTemp(0).AsRegister<Register>();
2212 __ movl(temp, value_loc.AsRegister<Register>());
2213 __ PoisonHeapReference(temp);
2214 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002215 } else {
2216 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
2217 }
2218
2219 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002220 codegen->MemoryFence();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002221 }
2222
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002223 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002224 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04002225 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
2226 locations->GetTemp(1).AsRegister<Register>(),
2227 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002228 value_loc.AsRegister<Register>(),
2229 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002230 }
2231}
2232
2233void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002234 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002235}
2236void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002237 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002238}
2239void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002240 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002241}
2242void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 GenUnsafePut(
2244 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002245}
2246void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002247 GenUnsafePut(
2248 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002249}
2250void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002251 GenUnsafePut(
2252 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002253}
2254void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002255 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002256}
2257void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002258 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002259}
2260void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002261 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002262}
2263
Vladimir Markoca6fff82017-10-03 14:49:14 +01002264static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002265 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002266 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002267 bool can_call = kEmitCompilerReadBarrier &&
2268 kUseBakerReadBarrier &&
2269 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002270 LocationSummary* locations =
2271 new (allocator) LocationSummary(invoke,
2272 can_call
2273 ? LocationSummary::kCallOnSlowPath
2274 : LocationSummary::kNoCall,
2275 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002276 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2277 locations->SetInAt(1, Location::RequiresRegister());
2278 // Offset is a long, but in 32 bit mode, we only need the low word.
2279 // Can we update the invoke here to remove a TypeConvert to Long?
2280 locations->SetInAt(2, Location::RequiresRegister());
2281 // Expected value must be in EAX or EDX:EAX.
2282 // For long, new value must be in ECX:EBX.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002283 if (type == DataType::Type::kInt64) {
Mark Mendell58d25fd2015-04-03 14:52:31 -04002284 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
2285 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
2286 } else {
2287 locations->SetInAt(3, Location::RegisterLocation(EAX));
2288 locations->SetInAt(4, Location::RequiresRegister());
2289 }
2290
2291 // Force a byte register for the output.
2292 locations->SetOut(Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002294 // Need temporary registers for card-marking, and possibly for
2295 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002296 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002297 // Need a byte register for marking.
2298 locations->AddTemp(Location::RegisterLocation(ECX));
2299 }
2300}
2301
2302void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002303 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002304}
2305
2306void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002307 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002308}
2309
2310void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002311 // The only read barrier implementation supporting the
2312 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2313 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002314 return;
2315 }
2316
Vladimir Markoca6fff82017-10-03 14:49:14 +01002317 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002318}
2319
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002321 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002322 LocationSummary* locations = invoke->GetLocations();
2323
2324 Register base = locations->InAt(1).AsRegister<Register>();
2325 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2326 Location out = locations->Out();
2327 DCHECK_EQ(out.AsRegister<Register>(), EAX);
2328
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002329 // The address of the field within the holding object.
2330 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2331
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002332 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002333 // The only read barrier implementation supporting the
2334 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2335 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2336
2337 Location temp1_loc = locations->GetTemp(0);
2338 Register temp1 = temp1_loc.AsRegister<Register>();
2339 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2340
Roland Levillain4d027112015-07-01 15:41:14 +01002341 Register expected = locations->InAt(3).AsRegister<Register>();
Roland Levillainb488b782015-10-22 11:38:49 +01002342 // Ensure `expected` is in EAX (required by the CMPXCHG instruction).
Roland Levillain4d027112015-07-01 15:41:14 +01002343 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002344 Register value = locations->InAt(4).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01002345
Roland Levillainb488b782015-10-22 11:38:49 +01002346 // Mark card for object assuming new value is stored.
2347 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002348 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2349
2350 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2351 // Need to make sure the reference stored in the field is a to-space
2352 // one before attempting the CAS or the CAS could fail incorrectly.
2353 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2354 invoke,
2355 temp1_loc, // Unused, used only as a "temporary" within the read barrier.
2356 base,
2357 field_addr,
2358 /* needs_null_check */ false,
2359 /* always_update_field */ true,
2360 &temp2);
2361 }
Roland Levillainb488b782015-10-22 11:38:49 +01002362
2363 bool base_equals_value = (base == value);
2364 if (kPoisonHeapReferences) {
2365 if (base_equals_value) {
2366 // If `base` and `value` are the same register location, move
2367 // `value` to a temporary register. This way, poisoning
2368 // `value` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002369 value = temp1;
Roland Levillainb488b782015-10-22 11:38:49 +01002370 __ movl(value, base);
Roland Levillain4d027112015-07-01 15:41:14 +01002371 }
Roland Levillainb488b782015-10-22 11:38:49 +01002372
2373 // Check that the register allocator did not assign the location
2374 // of `expected` (EAX) to `value` nor to `base`, so that heap
2375 // poisoning (when enabled) works as intended below.
2376 // - If `value` were equal to `expected`, both references would
2377 // be poisoned twice, meaning they would not be poisoned at
2378 // all, as heap poisoning uses address negation.
2379 // - If `base` were equal to `expected`, poisoning `expected`
2380 // would invalidate `base`.
2381 DCHECK_NE(value, expected);
2382 DCHECK_NE(base, expected);
2383
2384 __ PoisonHeapReference(expected);
2385 __ PoisonHeapReference(value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002386 }
2387
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002388 __ LockCmpxchgl(field_addr, value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002389
Roland Levillain0d5a2812015-11-13 10:07:31 +00002390 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002391 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002392
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002393 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002394 __ setb(kZero, out.AsRegister<Register>());
2395 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002396
Roland Levillain391b8662015-12-18 11:43:38 +00002397 // If heap poisoning is enabled, we need to unpoison the values
2398 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002399 if (kPoisonHeapReferences) {
2400 if (base_equals_value) {
2401 // `value` has been moved to a temporary register, no need to
2402 // unpoison it.
2403 } else {
2404 // Ensure `value` is different from `out`, so that unpoisoning
2405 // the former does not invalidate the latter.
2406 DCHECK_NE(value, out.AsRegister<Register>());
2407 __ UnpoisonHeapReference(value);
2408 }
2409 // Do not unpoison the reference contained in register
2410 // `expected`, as it is the same as register `out` (EAX).
2411 }
2412 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002413 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002414 // Ensure the expected value is in EAX (required by the CMPXCHG
2415 // instruction).
2416 DCHECK_EQ(locations->InAt(3).AsRegister<Register>(), EAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002417 __ LockCmpxchgl(field_addr, locations->InAt(4).AsRegister<Register>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002418 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002419 // Ensure the expected value is in EAX:EDX and that the new
2420 // value is in EBX:ECX (required by the CMPXCHG8B instruction).
2421 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
2422 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
2423 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
2424 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002425 __ LockCmpxchg8b(field_addr);
Roland Levillainb488b782015-10-22 11:38:49 +01002426 } else {
2427 LOG(FATAL) << "Unexpected CAS type " << type;
2428 }
2429
Roland Levillain0d5a2812015-11-13 10:07:31 +00002430 // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we
2431 // don't need scheduling barriers at this time.
Roland Levillainb488b782015-10-22 11:38:49 +01002432
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002433 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002434 __ setb(kZero, out.AsRegister<Register>());
2435 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002436 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002437}
2438
2439void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002440 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002441}
2442
2443void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002444 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002445}
2446
2447void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002448 // The only read barrier implementation supporting the
2449 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2450 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002451
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002453}
2454
2455void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002456 LocationSummary* locations =
2457 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002458 locations->SetInAt(0, Location::RequiresRegister());
2459 locations->SetOut(Location::SameAsFirstInput());
2460 locations->AddTemp(Location::RequiresRegister());
2461}
2462
2463static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
2464 X86Assembler* assembler) {
2465 Immediate imm_shift(shift);
2466 Immediate imm_mask(mask);
2467 __ movl(temp, reg);
2468 __ shrl(reg, imm_shift);
2469 __ andl(temp, imm_mask);
2470 __ andl(reg, imm_mask);
2471 __ shll(temp, imm_shift);
2472 __ orl(reg, temp);
2473}
2474
2475void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002476 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002477 LocationSummary* locations = invoke->GetLocations();
2478
2479 Register reg = locations->InAt(0).AsRegister<Register>();
2480 Register temp = locations->GetTemp(0).AsRegister<Register>();
2481
2482 /*
2483 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2484 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2485 * compared to generic luni implementation which has 5 rounds of swapping bits.
2486 * x = bswap x
2487 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2488 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2489 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2490 */
2491 __ bswapl(reg);
2492 SwapBits(reg, temp, 1, 0x55555555, assembler);
2493 SwapBits(reg, temp, 2, 0x33333333, assembler);
2494 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2495}
2496
2497void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002498 LocationSummary* locations =
2499 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002500 locations->SetInAt(0, Location::RequiresRegister());
2501 locations->SetOut(Location::SameAsFirstInput());
2502 locations->AddTemp(Location::RequiresRegister());
2503}
2504
2505void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002506 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002507 LocationSummary* locations = invoke->GetLocations();
2508
2509 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
2510 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2511 Register temp = locations->GetTemp(0).AsRegister<Register>();
2512
2513 // We want to swap high/low, then bswap each one, and then do the same
2514 // as a 32 bit reverse.
2515 // Exchange high and low.
2516 __ movl(temp, reg_low);
2517 __ movl(reg_low, reg_high);
2518 __ movl(reg_high, temp);
2519
2520 // bit-reverse low
2521 __ bswapl(reg_low);
2522 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
2523 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
2524 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
2525
2526 // bit-reverse high
2527 __ bswapl(reg_high);
2528 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
2529 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
2530 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
2531}
2532
Aart Bikc39dac12016-01-21 08:59:48 -08002533static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002534 ArenaAllocator* allocator, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002535 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2536 // Do nothing if there is no popcnt support. This results in generating
2537 // a call for the intrinsic rather than direct code.
2538 return;
2539 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002540 LocationSummary* locations =
2541 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc39dac12016-01-21 08:59:48 -08002542 if (is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002543 locations->AddTemp(Location::RequiresRegister());
Aart Bikc39dac12016-01-21 08:59:48 -08002544 }
Aart Bik2a946072016-01-21 12:49:00 -08002545 locations->SetInAt(0, Location::Any());
Aart Bikc39dac12016-01-21 08:59:48 -08002546 locations->SetOut(Location::RequiresRegister());
2547}
2548
Aart Bika19616e2016-02-01 18:57:58 -08002549static void GenBitCount(X86Assembler* assembler,
2550 CodeGeneratorX86* codegen,
2551 HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002552 LocationSummary* locations = invoke->GetLocations();
2553 Location src = locations->InAt(0);
2554 Register out = locations->Out().AsRegister<Register>();
2555
2556 if (invoke->InputAt(0)->IsConstant()) {
2557 // Evaluate this at compile time.
2558 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002559 int32_t result = is_long
Aart Bikc39dac12016-01-21 08:59:48 -08002560 ? POPCOUNT(static_cast<uint64_t>(value))
2561 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002562 codegen->Load32BitValue(out, result);
Aart Bikc39dac12016-01-21 08:59:48 -08002563 return;
2564 }
2565
2566 // Handle the non-constant cases.
2567 if (!is_long) {
2568 if (src.IsRegister()) {
2569 __ popcntl(out, src.AsRegister<Register>());
2570 } else {
2571 DCHECK(src.IsStackSlot());
2572 __ popcntl(out, Address(ESP, src.GetStackIndex()));
2573 }
Aart Bik2a946072016-01-21 12:49:00 -08002574 } else {
2575 // The 64-bit case needs to worry about two parts.
2576 Register temp = locations->GetTemp(0).AsRegister<Register>();
2577 if (src.IsRegisterPair()) {
2578 __ popcntl(temp, src.AsRegisterPairLow<Register>());
2579 __ popcntl(out, src.AsRegisterPairHigh<Register>());
2580 } else {
2581 DCHECK(src.IsDoubleStackSlot());
2582 __ popcntl(temp, Address(ESP, src.GetStackIndex()));
2583 __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize)));
2584 }
2585 __ addl(out, temp);
Aart Bikc39dac12016-01-21 08:59:48 -08002586 }
Aart Bikc39dac12016-01-21 08:59:48 -08002587}
2588
2589void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002590 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002591}
2592
2593void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002594 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002595}
2596
2597void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002598 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002599}
2600
2601void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002602 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002603}
2604
Vladimir Markoca6fff82017-10-03 14:49:14 +01002605static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2606 LocationSummary* locations =
2607 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002608 if (is_long) {
2609 locations->SetInAt(0, Location::RequiresRegister());
2610 } else {
2611 locations->SetInAt(0, Location::Any());
2612 }
2613 locations->SetOut(Location::RequiresRegister());
2614}
2615
Aart Bika19616e2016-02-01 18:57:58 -08002616static void GenLeadingZeros(X86Assembler* assembler,
2617 CodeGeneratorX86* codegen,
2618 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002619 LocationSummary* locations = invoke->GetLocations();
2620 Location src = locations->InAt(0);
2621 Register out = locations->Out().AsRegister<Register>();
2622
2623 if (invoke->InputAt(0)->IsConstant()) {
2624 // Evaluate this at compile time.
2625 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2626 if (value == 0) {
2627 value = is_long ? 64 : 32;
2628 } else {
2629 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2630 }
Aart Bika19616e2016-02-01 18:57:58 -08002631 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002632 return;
2633 }
2634
2635 // Handle the non-constant cases.
2636 if (!is_long) {
2637 if (src.IsRegister()) {
2638 __ bsrl(out, src.AsRegister<Register>());
2639 } else {
2640 DCHECK(src.IsStackSlot());
2641 __ bsrl(out, Address(ESP, src.GetStackIndex()));
2642 }
2643
2644 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002645 NearLabel all_zeroes, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002646 __ j(kEqual, &all_zeroes);
2647
2648 // Correct the result from BSR to get the final CLZ result.
2649 __ xorl(out, Immediate(31));
2650 __ jmp(&done);
2651
2652 // Fix the zero case with the expected result.
2653 __ Bind(&all_zeroes);
2654 __ movl(out, Immediate(32));
2655
2656 __ Bind(&done);
2657 return;
2658 }
2659
2660 // 64 bit case needs to worry about both parts of the register.
2661 DCHECK(src.IsRegisterPair());
2662 Register src_lo = src.AsRegisterPairLow<Register>();
2663 Register src_hi = src.AsRegisterPairHigh<Register>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002664 NearLabel handle_low, done, all_zeroes;
Mark Mendelld5897672015-08-12 21:16:41 -04002665
2666 // Is the high word zero?
2667 __ testl(src_hi, src_hi);
2668 __ j(kEqual, &handle_low);
2669
2670 // High word is not zero. We know that the BSR result is defined in this case.
2671 __ bsrl(out, src_hi);
2672
2673 // Correct the result from BSR to get the final CLZ result.
2674 __ xorl(out, Immediate(31));
2675 __ jmp(&done);
2676
2677 // High word was zero. We have to compute the low word count and add 32.
2678 __ Bind(&handle_low);
2679 __ bsrl(out, src_lo);
2680 __ j(kEqual, &all_zeroes);
2681
2682 // We had a valid result. Use an XOR to both correct the result and add 32.
2683 __ xorl(out, Immediate(63));
2684 __ jmp(&done);
2685
2686 // All zero case.
2687 __ Bind(&all_zeroes);
2688 __ movl(out, Immediate(64));
2689
2690 __ Bind(&done);
2691}
2692
2693void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002694 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002695}
2696
2697void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002698 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002699}
2700
2701void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002702 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002703}
2704
2705void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002706 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002707}
2708
Vladimir Markoca6fff82017-10-03 14:49:14 +01002709static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2710 LocationSummary* locations =
2711 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002712 if (is_long) {
2713 locations->SetInAt(0, Location::RequiresRegister());
2714 } else {
2715 locations->SetInAt(0, Location::Any());
2716 }
2717 locations->SetOut(Location::RequiresRegister());
2718}
2719
Aart Bika19616e2016-02-01 18:57:58 -08002720static void GenTrailingZeros(X86Assembler* assembler,
2721 CodeGeneratorX86* codegen,
2722 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002723 LocationSummary* locations = invoke->GetLocations();
2724 Location src = locations->InAt(0);
2725 Register out = locations->Out().AsRegister<Register>();
2726
2727 if (invoke->InputAt(0)->IsConstant()) {
2728 // Evaluate this at compile time.
2729 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2730 if (value == 0) {
2731 value = is_long ? 64 : 32;
2732 } else {
2733 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2734 }
Aart Bika19616e2016-02-01 18:57:58 -08002735 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002736 return;
2737 }
2738
2739 // Handle the non-constant cases.
2740 if (!is_long) {
2741 if (src.IsRegister()) {
2742 __ bsfl(out, src.AsRegister<Register>());
2743 } else {
2744 DCHECK(src.IsStackSlot());
2745 __ bsfl(out, Address(ESP, src.GetStackIndex()));
2746 }
2747
2748 // BSF sets ZF if the input was zero, and the output is undefined.
2749 NearLabel done;
2750 __ j(kNotEqual, &done);
2751
2752 // Fix the zero case with the expected result.
2753 __ movl(out, Immediate(32));
2754
2755 __ Bind(&done);
2756 return;
2757 }
2758
2759 // 64 bit case needs to worry about both parts of the register.
2760 DCHECK(src.IsRegisterPair());
2761 Register src_lo = src.AsRegisterPairLow<Register>();
2762 Register src_hi = src.AsRegisterPairHigh<Register>();
2763 NearLabel done, all_zeroes;
2764
2765 // If the low word is zero, then ZF will be set. If not, we have the answer.
2766 __ bsfl(out, src_lo);
2767 __ j(kNotEqual, &done);
2768
2769 // Low word was zero. We have to compute the high word count and add 32.
2770 __ bsfl(out, src_hi);
2771 __ j(kEqual, &all_zeroes);
2772
2773 // We had a valid result. Add 32 to account for the low word being zero.
2774 __ addl(out, Immediate(32));
2775 __ jmp(&done);
2776
2777 // All zero case.
2778 __ Bind(&all_zeroes);
2779 __ movl(out, Immediate(64));
2780
2781 __ Bind(&done);
2782}
2783
2784void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002785 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002786}
2787
2788void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002789 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002790}
2791
2792void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002793 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002794}
2795
2796void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002797 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002798}
2799
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002800static bool IsSameInput(HInstruction* instruction, size_t input0, size_t input1) {
2801 return instruction->InputAt(input0) == instruction->InputAt(input1);
2802}
2803
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002804// Compute base address for the System.arraycopy intrinsic in `base`.
2805static void GenSystemArrayCopyBaseAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002806 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002807 const Register& array,
2808 const Location& pos,
2809 const Register& base) {
2810 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002812 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 DCHECK_EQ(type, DataType::Type::kReference);
2814 const int32_t element_size = DataType::Size(type);
2815 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002816 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
2817
2818 if (pos.IsConstant()) {
2819 int32_t constant = pos.GetConstant()->AsIntConstant()->GetValue();
2820 __ leal(base, Address(array, element_size * constant + data_offset));
2821 } else {
2822 __ leal(base, Address(array, pos.AsRegister<Register>(), scale_factor, data_offset));
2823 }
2824}
2825
2826// Compute end source address for the System.arraycopy intrinsic in `end`.
2827static void GenSystemArrayCopyEndAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002829 const Location& copy_length,
2830 const Register& base,
2831 const Register& end) {
2832 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002834 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 DCHECK_EQ(type, DataType::Type::kReference);
2836 const int32_t element_size = DataType::Size(type);
2837 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002838
2839 if (copy_length.IsConstant()) {
2840 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
2841 __ leal(end, Address(base, element_size * constant));
2842 } else {
2843 __ leal(end, Address(base, copy_length.AsRegister<Register>(), scale_factor, 0));
2844 }
2845}
2846
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002847void IntrinsicLocationsBuilderX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002848 // The only read barrier implementation supporting the
2849 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2850 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002851 return;
2852 }
2853
2854 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2855 if (invoke->GetLocations() != nullptr) {
2856 // Need a byte register for marking.
2857 invoke->GetLocations()->SetTempAt(1, Location::RegisterLocation(ECX));
2858
2859 static constexpr size_t kSrc = 0;
2860 static constexpr size_t kSrcPos = 1;
2861 static constexpr size_t kDest = 2;
2862 static constexpr size_t kDestPos = 3;
2863 static constexpr size_t kLength = 4;
2864
2865 if (!invoke->InputAt(kSrcPos)->IsIntConstant() &&
2866 !invoke->InputAt(kDestPos)->IsIntConstant() &&
2867 !invoke->InputAt(kLength)->IsIntConstant()) {
2868 if (!IsSameInput(invoke, kSrcPos, kDestPos) &&
2869 !IsSameInput(invoke, kSrcPos, kLength) &&
2870 !IsSameInput(invoke, kDestPos, kLength) &&
2871 !IsSameInput(invoke, kSrc, kDest)) {
2872 // Not enough registers, make the length also take a stack slot.
2873 invoke->GetLocations()->SetInAt(kLength, Location::Any());
2874 }
2875 }
2876 }
2877}
2878
2879void IntrinsicCodeGeneratorX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002880 // The only read barrier implementation supporting the
2881 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2882 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002883
2884 X86Assembler* assembler = GetAssembler();
2885 LocationSummary* locations = invoke->GetLocations();
2886
2887 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2888 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2889 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2890 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002891 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002892
2893 Register src = locations->InAt(0).AsRegister<Register>();
2894 Location src_pos = locations->InAt(1);
2895 Register dest = locations->InAt(2).AsRegister<Register>();
2896 Location dest_pos = locations->InAt(3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002897 Location length_arg = locations->InAt(4);
2898 Location length = length_arg;
2899 Location temp1_loc = locations->GetTemp(0);
2900 Register temp1 = temp1_loc.AsRegister<Register>();
2901 Location temp2_loc = locations->GetTemp(1);
2902 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002903
Roland Levillain0b671c02016-08-19 12:02:34 +01002904 SlowPathCode* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
2905 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002906
2907 NearLabel conditions_on_positions_validated;
2908 SystemArrayCopyOptimizations optimizations(invoke);
2909
2910 // If source and destination are the same, we go to slow path if we need to do
2911 // forward copying.
2912 if (src_pos.IsConstant()) {
2913 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2914 if (dest_pos.IsConstant()) {
2915 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2916 if (optimizations.GetDestinationIsSource()) {
2917 // Checked when building locations.
2918 DCHECK_GE(src_pos_constant, dest_pos_constant);
2919 } else if (src_pos_constant < dest_pos_constant) {
2920 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002921 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002922 }
2923 } else {
2924 if (!optimizations.GetDestinationIsSource()) {
2925 __ cmpl(src, dest);
2926 __ j(kNotEqual, &conditions_on_positions_validated);
2927 }
2928 __ cmpl(dest_pos.AsRegister<Register>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002929 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002930 }
2931 } else {
2932 if (!optimizations.GetDestinationIsSource()) {
2933 __ cmpl(src, dest);
2934 __ j(kNotEqual, &conditions_on_positions_validated);
2935 }
2936 if (dest_pos.IsConstant()) {
2937 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2938 __ cmpl(src_pos.AsRegister<Register>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002939 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002940 } else {
2941 __ cmpl(src_pos.AsRegister<Register>(), dest_pos.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002942 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002943 }
2944 }
2945
2946 __ Bind(&conditions_on_positions_validated);
2947
2948 if (!optimizations.GetSourceIsNotNull()) {
2949 // Bail out if the source is null.
2950 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01002951 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002952 }
2953
2954 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2955 // Bail out if the destination is null.
2956 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002957 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002958 }
2959
Roland Levillain0b671c02016-08-19 12:02:34 +01002960 Location temp3_loc = locations->GetTemp(2);
2961 Register temp3 = temp3_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002962 if (length.IsStackSlot()) {
2963 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2964 length = Location::RegisterLocation(temp3);
2965 }
2966
2967 // If the length is negative, bail out.
2968 // We have already checked in the LocationsBuilder for the constant case.
2969 if (!length.IsConstant() &&
2970 !optimizations.GetCountIsSourceLength() &&
2971 !optimizations.GetCountIsDestinationLength()) {
2972 __ testl(length.AsRegister<Register>(), length.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002973 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002974 }
2975
2976 // Validity checks: source.
2977 CheckPosition(assembler,
2978 src_pos,
2979 src,
2980 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002981 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002982 temp1,
2983 optimizations.GetCountIsSourceLength());
2984
2985 // Validity checks: dest.
2986 CheckPosition(assembler,
2987 dest_pos,
2988 dest,
2989 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002990 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002991 temp1,
2992 optimizations.GetCountIsDestinationLength());
2993
2994 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2995 // Check whether all elements of the source array are assignable to the component
2996 // type of the destination array. We do two checks: the classes are the same,
2997 // or the destination is Object[]. If none of these checks succeed, we go to the
2998 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01002999
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003000 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01003001 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3002 // /* HeapReference<Class> */ temp1 = src->klass_
3003 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003004 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003005 // Bail out if the source is not a non primitive array.
3006 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3007 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003008 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003009 __ testl(temp1, temp1);
3010 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3011 // If heap poisoning is enabled, `temp1` has been unpoisoned
3012 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3013 } else {
3014 // /* HeapReference<Class> */ temp1 = src->klass_
3015 __ movl(temp1, Address(src, class_offset));
3016 __ MaybeUnpoisonHeapReference(temp1);
3017 // Bail out if the source is not a non primitive array.
3018 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3019 __ movl(temp1, Address(temp1, component_offset));
3020 __ testl(temp1, temp1);
3021 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3022 __ MaybeUnpoisonHeapReference(temp1);
3023 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003024 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01003025 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003026 }
3027
Roland Levillain0b671c02016-08-19 12:02:34 +01003028 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3029 if (length.Equals(Location::RegisterLocation(temp3))) {
3030 // When Baker read barriers are enabled, register `temp3`,
3031 // which in the present case contains the `length` parameter,
3032 // will be overwritten below. Make the `length` location
3033 // reference the original stack location; it will be moved
3034 // back to `temp3` later if necessary.
3035 DCHECK(length_arg.IsStackSlot());
3036 length = length_arg;
3037 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003038
Roland Levillain0b671c02016-08-19 12:02:34 +01003039 // /* HeapReference<Class> */ temp1 = dest->klass_
3040 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003041 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003042
Roland Levillain0b671c02016-08-19 12:02:34 +01003043 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
3044 // Bail out if the destination is not a non primitive array.
3045 //
3046 // Register `temp1` is not trashed by the read barrier emitted
3047 // by GenerateFieldLoadWithBakerReadBarrier below, as that
3048 // method produces a call to a ReadBarrierMarkRegX entry point,
3049 // which saves all potentially live registers, including
3050 // temporaries such a `temp1`.
3051 // /* HeapReference<Class> */ temp2 = temp1->component_type_
3052 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003053 invoke, temp2_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003054 __ testl(temp2, temp2);
3055 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3056 // If heap poisoning is enabled, `temp2` has been unpoisoned
3057 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3058 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
3059 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3060 }
3061
3062 // For the same reason given earlier, `temp1` is not trashed by the
3063 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
3064 // /* HeapReference<Class> */ temp2 = src->klass_
3065 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003066 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003067 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
3068 __ cmpl(temp1, temp2);
3069
3070 if (optimizations.GetDestinationIsTypedObjectArray()) {
3071 NearLabel do_copy;
3072 __ j(kEqual, &do_copy);
3073 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3074 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003075 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003076 // We do not need to emit a read barrier for the following
3077 // heap reference load, as `temp1` is only used in a
3078 // comparison with null below, and this reference is not
3079 // kept afterwards.
3080 __ cmpl(Address(temp1, super_offset), Immediate(0));
3081 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3082 __ Bind(&do_copy);
3083 } else {
3084 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3085 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003086 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01003087 // Non read barrier code.
3088
3089 // /* HeapReference<Class> */ temp1 = dest->klass_
3090 __ movl(temp1, Address(dest, class_offset));
3091 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
3092 __ MaybeUnpoisonHeapReference(temp1);
3093 // Bail out if the destination is not a non primitive array.
3094 // /* HeapReference<Class> */ temp2 = temp1->component_type_
3095 __ movl(temp2, Address(temp1, component_offset));
3096 __ testl(temp2, temp2);
3097 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3098 __ MaybeUnpoisonHeapReference(temp2);
3099 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
3100 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3101 // Re-poison the heap reference to make the compare instruction below
3102 // compare two poisoned references.
3103 __ PoisonHeapReference(temp1);
3104 }
3105
3106 // Note: if heap poisoning is on, we are comparing two poisoned references here.
3107 __ cmpl(temp1, Address(src, class_offset));
3108
3109 if (optimizations.GetDestinationIsTypedObjectArray()) {
3110 NearLabel do_copy;
3111 __ j(kEqual, &do_copy);
3112 __ MaybeUnpoisonHeapReference(temp1);
3113 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3114 __ movl(temp1, Address(temp1, component_offset));
3115 __ MaybeUnpoisonHeapReference(temp1);
3116 __ cmpl(Address(temp1, super_offset), Immediate(0));
3117 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3118 __ Bind(&do_copy);
3119 } else {
3120 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3121 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003122 }
3123 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
3124 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
3125 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01003126 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3127 // /* HeapReference<Class> */ temp1 = src->klass_
3128 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003129 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003130 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3131 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003132 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003133 __ testl(temp1, temp1);
3134 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3135 // If heap poisoning is enabled, `temp1` has been unpoisoned
3136 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3137 } else {
3138 // /* HeapReference<Class> */ temp1 = src->klass_
3139 __ movl(temp1, Address(src, class_offset));
3140 __ MaybeUnpoisonHeapReference(temp1);
3141 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3142 __ movl(temp1, Address(temp1, component_offset));
3143 __ testl(temp1, temp1);
3144 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3145 __ MaybeUnpoisonHeapReference(temp1);
3146 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003147 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01003148 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003149 }
3150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003151 const DataType::Type type = DataType::Type::kReference;
3152 const int32_t element_size = DataType::Size(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003153
Roland Levillain0b671c02016-08-19 12:02:34 +01003154 // Compute the base source address in `temp1`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003155 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003156
Roland Levillain0b671c02016-08-19 12:02:34 +01003157 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3158 // If it is needed (in the case of the fast-path loop), the base
3159 // destination address is computed later, as `temp2` is used for
3160 // intermediate computations.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003161
Roland Levillain0b671c02016-08-19 12:02:34 +01003162 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003163 if (length.IsStackSlot()) {
3164 // Location `length` is again pointing at a stack slot, as
3165 // register `temp3` (which was containing the length parameter
3166 // earlier) has been overwritten; restore it now
3167 DCHECK(length.Equals(length_arg));
3168 __ movl(temp3, Address(ESP, length.GetStackIndex()));
3169 length = Location::RegisterLocation(temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01003170 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003171 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003172
Roland Levillain0b671c02016-08-19 12:02:34 +01003173 // SystemArrayCopy implementation for Baker read barriers (see
3174 // also CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier):
3175 //
3176 // if (src_ptr != end_ptr) {
3177 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
3178 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003179 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01003180 // if (is_gray) {
3181 // // Slow-path copy.
3182 // for (size_t i = 0; i != length; ++i) {
3183 // dest_array[dest_pos + i] =
3184 // MaybePoison(ReadBarrier::Mark(MaybeUnpoison(src_array[src_pos + i])));
3185 // }
3186 // } else {
3187 // // Fast-path copy.
3188 // do {
3189 // *dest_ptr++ = *src_ptr++;
3190 // } while (src_ptr != end_ptr)
3191 // }
3192 // }
3193
3194 NearLabel loop, done;
3195
3196 // Don't enter copy loop if `length == 0`.
3197 __ cmpl(temp1, temp3);
3198 __ j(kEqual, &done);
3199
Vladimir Marko953437b2016-08-24 08:30:46 +00003200 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003201 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
3202 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00003203 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
3204 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
3205 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
3206
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003207 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00003208 // goto slow_path;
3209 // At this point, just do the "if" and make sure that flags are preserved until the branch.
3210 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01003211
3212 // Load fence to prevent load-load reordering.
3213 // Note that this is a no-op, thanks to the x86 memory model.
3214 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3215
3216 // Slow path used to copy array when `src` is gray.
3217 SlowPathCode* read_barrier_slow_path =
3218 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathX86(invoke);
3219 codegen_->AddSlowPath(read_barrier_slow_path);
3220
Vladimir Marko953437b2016-08-24 08:30:46 +00003221 // We have done the "if" of the gray bit check above, now branch based on the flags.
3222 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01003223
3224 // Fast-path copy.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003225 // Compute the base destination address in `temp2`.
3226 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01003227 // Iterate over the arrays and do a raw copy of the objects. We don't need to
3228 // poison/unpoison.
3229 __ Bind(&loop);
3230 __ pushl(Address(temp1, 0));
3231 __ cfi().AdjustCFAOffset(4);
3232 __ popl(Address(temp2, 0));
3233 __ cfi().AdjustCFAOffset(-4);
3234 __ addl(temp1, Immediate(element_size));
3235 __ addl(temp2, Immediate(element_size));
3236 __ cmpl(temp1, temp3);
3237 __ j(kNotEqual, &loop);
3238
3239 __ Bind(read_barrier_slow_path->GetExitLabel());
3240 __ Bind(&done);
3241 } else {
3242 // Non read barrier code.
Roland Levillain0b671c02016-08-19 12:02:34 +01003243 // Compute the base destination address in `temp2`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003244 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01003245 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003246 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01003247 // Iterate over the arrays and do a raw copy of the objects. We don't need to
3248 // poison/unpoison.
3249 NearLabel loop, done;
3250 __ cmpl(temp1, temp3);
3251 __ j(kEqual, &done);
3252 __ Bind(&loop);
3253 __ pushl(Address(temp1, 0));
3254 __ cfi().AdjustCFAOffset(4);
3255 __ popl(Address(temp2, 0));
3256 __ cfi().AdjustCFAOffset(-4);
3257 __ addl(temp1, Immediate(element_size));
3258 __ addl(temp2, Immediate(element_size));
3259 __ cmpl(temp1, temp3);
3260 __ j(kNotEqual, &loop);
3261 __ Bind(&done);
3262 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003263
3264 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003265 codegen_->MarkGCCard(temp1, temp2, dest, Register(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003266
Roland Levillain0b671c02016-08-19 12:02:34 +01003267 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003268}
3269
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003270void IntrinsicLocationsBuilderX86::VisitIntegerValueOf(HInvoke* invoke) {
3271 InvokeRuntimeCallingConvention calling_convention;
3272 IntrinsicVisitor::ComputeIntegerValueOfLocations(
3273 invoke,
3274 codegen_,
3275 Location::RegisterLocation(EAX),
3276 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3277}
3278
3279void IntrinsicCodeGeneratorX86::VisitIntegerValueOf(HInvoke* invoke) {
3280 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
3281 LocationSummary* locations = invoke->GetLocations();
3282 X86Assembler* assembler = GetAssembler();
3283
3284 Register out = locations->Out().AsRegister<Register>();
3285 InvokeRuntimeCallingConvention calling_convention;
3286 if (invoke->InputAt(0)->IsConstant()) {
3287 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
3288 if (value >= info.low && value <= info.high) {
3289 // Just embed the j.l.Integer in the code.
3290 ScopedObjectAccess soa(Thread::Current());
3291 mirror::Object* boxed = info.cache->Get(value + (-info.low));
3292 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
3293 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
3294 __ movl(out, Immediate(address));
3295 } else {
3296 // Allocate and initialize a new j.l.Integer.
3297 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
3298 // JIT object table.
3299 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3300 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
3301 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3302 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3303 __ movl(Address(out, info.value_offset), Immediate(value));
3304 }
3305 } else {
3306 Register in = locations->InAt(0).AsRegister<Register>();
3307 // Check bounds of our cache.
3308 __ leal(out, Address(in, -info.low));
3309 __ cmpl(out, Immediate(info.high - info.low + 1));
3310 NearLabel allocate, done;
3311 __ j(kAboveEqual, &allocate);
3312 // If the value is within the bounds, load the j.l.Integer directly from the array.
3313 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3314 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3315 __ movl(out, Address(out, TIMES_4, data_offset + address));
3316 __ MaybeUnpoisonHeapReference(out);
3317 __ jmp(&done);
3318 __ Bind(&allocate);
3319 // Otherwise allocate and initialize a new j.l.Integer.
3320 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3321 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
3322 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3323 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3324 __ movl(Address(out, info.value_offset), in);
3325 __ Bind(&done);
3326 }
3327}
3328
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003329void IntrinsicLocationsBuilderX86::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003330 LocationSummary* locations =
3331 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003332 locations->SetOut(Location::RequiresRegister());
3333}
3334
3335void IntrinsicCodeGeneratorX86::VisitThreadInterrupted(HInvoke* invoke) {
3336 X86Assembler* assembler = GetAssembler();
3337 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
3338 Address address = Address::Absolute(Thread::InterruptedOffset<kX86PointerSize>().Int32Value());
3339 NearLabel done;
3340 __ fs()->movl(out, address);
3341 __ testl(out, out);
3342 __ j(kEqual, &done);
3343 __ fs()->movl(address, Immediate(0));
3344 codegen_->MemoryFence();
3345 __ Bind(&done);
3346}
3347
3348
Aart Bik2f9fcc92016-03-01 15:16:54 -08003349UNIMPLEMENTED_INTRINSIC(X86, MathRoundDouble)
Vladimir Marko4ee8e292017-06-02 15:39:30 +00003350UNIMPLEMENTED_INTRINSIC(X86, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003351UNIMPLEMENTED_INTRINSIC(X86, FloatIsInfinite)
3352UNIMPLEMENTED_INTRINSIC(X86, DoubleIsInfinite)
3353UNIMPLEMENTED_INTRINSIC(X86, IntegerHighestOneBit)
3354UNIMPLEMENTED_INTRINSIC(X86, LongHighestOneBit)
3355UNIMPLEMENTED_INTRINSIC(X86, IntegerLowestOneBit)
3356UNIMPLEMENTED_INTRINSIC(X86, LongLowestOneBit)
Mark Mendell09ed1a32015-03-25 08:30:06 -04003357
Aart Bikff7d89c2016-11-07 08:49:28 -08003358UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOf);
3359UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003360UNIMPLEMENTED_INTRINSIC(X86, StringBufferAppend);
3361UNIMPLEMENTED_INTRINSIC(X86, StringBufferLength);
3362UNIMPLEMENTED_INTRINSIC(X86, StringBufferToString);
3363UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppend);
3364UNIMPLEMENTED_INTRINSIC(X86, StringBuilderLength);
3365UNIMPLEMENTED_INTRINSIC(X86, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003366
Aart Bik0e54c012016-03-04 12:08:31 -08003367// 1.8.
3368UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddInt)
3369UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddLong)
3370UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetInt)
3371UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetLong)
3372UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003373
Aart Bik2f9fcc92016-03-01 15:16:54 -08003374UNREACHABLE_INTRINSICS(X86)
Roland Levillain4d027112015-07-01 15:41:14 +01003375
3376#undef __
3377
Mark Mendell09ed1a32015-03-25 08:30:06 -04003378} // namespace x86
3379} // namespace art