blob: 0edc061e9724f8112184d6821d413099d775d102 [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
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000336static void GenMinMaxFP(HInvoke* invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000337 bool is_min,
338 bool is_double,
339 X86Assembler* assembler,
340 CodeGeneratorX86* codegen) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000341 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -0400342 Location op1_loc = locations->InAt(0);
343 Location op2_loc = locations->InAt(1);
344 Location out_loc = locations->Out();
345 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
346
347 // Shortcut for same input locations.
348 if (op1_loc.Equals(op2_loc)) {
349 DCHECK(out_loc.Equals(op1_loc));
350 return;
351 }
352
353 // (out := op1)
354 // out <=? op2
355 // if Nan jmp Nan_label
356 // if out is min jmp done
357 // if op2 is min jmp op2_label
358 // handle -0/+0
359 // jmp done
360 // Nan_label:
361 // out := NaN
362 // op2_label:
363 // out := op2
364 // done:
365 //
366 // This removes one jmp, but needs to copy one input (op1) to out.
367 //
368 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
369
370 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
371
Mark Mendell0c9497d2015-08-21 09:30:05 -0400372 NearLabel nan, done, op2_label;
Mark Mendell09ed1a32015-03-25 08:30:06 -0400373 if (is_double) {
374 __ ucomisd(out, op2);
375 } else {
376 __ ucomiss(out, op2);
377 }
378
379 __ j(Condition::kParityEven, &nan);
380
381 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
382 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
383
384 // Handle 0.0/-0.0.
385 if (is_min) {
386 if (is_double) {
387 __ orpd(out, op2);
388 } else {
389 __ orps(out, op2);
390 }
391 } else {
392 if (is_double) {
393 __ andpd(out, op2);
394 } else {
395 __ andps(out, op2);
396 }
397 }
398 __ jmp(&done);
399
400 // NaN handling.
401 __ Bind(&nan);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000402 // Do we have a constant area pointer?
Nicolas Geoffray97793072016-02-16 15:33:54 +0000403 if (locations->GetInputCount() == 3 && locations->InAt(2).IsValid()) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000404 HX86ComputeBaseMethodAddress* method_address =
405 invoke->InputAt(2)->AsX86ComputeBaseMethodAddress();
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000406 DCHECK(locations->InAt(2).IsRegister());
407 Register constant_area = locations->InAt(2).AsRegister<Register>();
408 if (is_double) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000409 __ movsd(out, codegen->LiteralInt64Address(kDoubleNaN, method_address, constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000410 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000411 __ movss(out, codegen->LiteralInt32Address(kFloatNaN, method_address, constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000412 }
Mark Mendell09ed1a32015-03-25 08:30:06 -0400413 } else {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000414 if (is_double) {
415 __ pushl(Immediate(kDoubleNaNHigh));
416 __ pushl(Immediate(kDoubleNaNLow));
417 __ movsd(out, Address(ESP, 0));
418 __ addl(ESP, Immediate(8));
419 } else {
420 __ pushl(Immediate(kFloatNaN));
421 __ movss(out, Address(ESP, 0));
422 __ addl(ESP, Immediate(4));
423 }
Mark Mendell09ed1a32015-03-25 08:30:06 -0400424 }
425 __ jmp(&done);
426
427 // out := op2;
428 __ Bind(&op2_label);
429 if (is_double) {
430 __ movsd(out, op2);
431 } else {
432 __ movss(out, op2);
433 }
434
435 // Done.
436 __ Bind(&done);
437}
438
Vladimir Markoca6fff82017-10-03 14:49:14 +0100439static void CreateFPFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
440 LocationSummary* locations =
441 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400442 locations->SetInAt(0, Location::RequiresFpuRegister());
443 locations->SetInAt(1, Location::RequiresFpuRegister());
444 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
445 // the second input to be the output (we can simply swap inputs).
446 locations->SetOut(Location::SameAsFirstInput());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000447 HInvokeStaticOrDirect* static_or_direct = invoke->AsInvokeStaticOrDirect();
448 DCHECK(static_or_direct != nullptr);
Nicolas Geoffray97793072016-02-16 15:33:54 +0000449 if (static_or_direct->HasSpecialInput() &&
450 invoke->InputAt(static_or_direct->GetSpecialInputIndex())->IsX86ComputeBaseMethodAddress()) {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000451 locations->SetInAt(2, Location::RequiresRegister());
452 }
Mark Mendell09ed1a32015-03-25 08:30:06 -0400453}
454
455void IntrinsicLocationsBuilderX86::VisitMathMinDoubleDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100456 CreateFPFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400457}
458
459void IntrinsicCodeGeneratorX86::VisitMathMinDoubleDouble(HInvoke* invoke) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000460 GenMinMaxFP(invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000461 /* is_min */ true,
462 /* is_double */ true,
463 GetAssembler(),
464 codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400465}
466
467void IntrinsicLocationsBuilderX86::VisitMathMinFloatFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100468 CreateFPFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400469}
470
471void IntrinsicCodeGeneratorX86::VisitMathMinFloatFloat(HInvoke* invoke) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000472 GenMinMaxFP(invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000473 /* is_min */ true,
474 /* is_double */ false,
475 GetAssembler(),
476 codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400477}
478
479void IntrinsicLocationsBuilderX86::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100480 CreateFPFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400481}
482
483void IntrinsicCodeGeneratorX86::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000484 GenMinMaxFP(invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000485 /* is_min */ false,
486 /* is_double */ true,
487 GetAssembler(),
488 codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400489}
490
491void IntrinsicLocationsBuilderX86::VisitMathMaxFloatFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100492 CreateFPFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400493}
494
495void IntrinsicCodeGeneratorX86::VisitMathMaxFloatFloat(HInvoke* invoke) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000496 GenMinMaxFP(invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000497 /* is_min */ false,
498 /* is_double */ false,
499 GetAssembler(),
500 codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400501}
502
503static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
504 X86Assembler* assembler) {
505 Location op1_loc = locations->InAt(0);
506 Location op2_loc = locations->InAt(1);
507
508 // Shortcut for same input locations.
509 if (op1_loc.Equals(op2_loc)) {
510 // Can return immediately, as op1_loc == out_loc.
511 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
512 // a copy here.
513 DCHECK(locations->Out().Equals(op1_loc));
514 return;
515 }
516
517 if (is_long) {
518 // Need to perform a subtract to get the sign right.
519 // op1 is already in the same location as the output.
520 Location output = locations->Out();
521 Register output_lo = output.AsRegisterPairLow<Register>();
522 Register output_hi = output.AsRegisterPairHigh<Register>();
523
524 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
525 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
526
527 // Spare register to compute the subtraction to set condition code.
528 Register temp = locations->GetTemp(0).AsRegister<Register>();
529
530 // Subtract off op2_low.
531 __ movl(temp, output_lo);
532 __ subl(temp, op2_lo);
533
534 // Now use the same tempo and the borrow to finish the subtraction of op2_hi.
535 __ movl(temp, output_hi);
536 __ sbbl(temp, op2_hi);
537
538 // Now the condition code is correct.
539 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
540 __ cmovl(cond, output_lo, op2_lo);
541 __ cmovl(cond, output_hi, op2_hi);
542 } else {
543 Register out = locations->Out().AsRegister<Register>();
544 Register op2 = op2_loc.AsRegister<Register>();
545
546 // (out := op1)
547 // out <=? op2
548 // if out is min jmp done
549 // out := op2
550 // done:
551
552 __ cmpl(out, op2);
553 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
554 __ cmovl(cond, out, op2);
555 }
556}
557
Vladimir Markoca6fff82017-10-03 14:49:14 +0100558static void CreateIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
559 LocationSummary* locations =
560 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400561 locations->SetInAt(0, Location::RequiresRegister());
562 locations->SetInAt(1, Location::RequiresRegister());
563 locations->SetOut(Location::SameAsFirstInput());
564}
565
Vladimir Markoca6fff82017-10-03 14:49:14 +0100566static void CreateLongLongToLongLocations(ArenaAllocator* allocator, HInvoke* invoke) {
567 LocationSummary* locations =
568 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400569 locations->SetInAt(0, Location::RequiresRegister());
570 locations->SetInAt(1, Location::RequiresRegister());
571 locations->SetOut(Location::SameAsFirstInput());
572 // Register to use to perform a long subtract to set cc.
573 locations->AddTemp(Location::RequiresRegister());
574}
575
576void IntrinsicLocationsBuilderX86::VisitMathMinIntInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100577 CreateIntIntToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400578}
579
580void IntrinsicCodeGeneratorX86::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000581 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400582}
583
584void IntrinsicLocationsBuilderX86::VisitMathMinLongLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100585 CreateLongLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400586}
587
588void IntrinsicCodeGeneratorX86::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000589 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400590}
591
592void IntrinsicLocationsBuilderX86::VisitMathMaxIntInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100593 CreateIntIntToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400594}
595
596void IntrinsicCodeGeneratorX86::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000597 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400598}
599
600void IntrinsicLocationsBuilderX86::VisitMathMaxLongLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100601 CreateLongLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400602}
603
604void IntrinsicCodeGeneratorX86::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000605 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400606}
607
Vladimir Markoca6fff82017-10-03 14:49:14 +0100608static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
609 LocationSummary* locations =
610 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400611 locations->SetInAt(0, Location::RequiresFpuRegister());
612 locations->SetOut(Location::RequiresFpuRegister());
613}
614
615void IntrinsicLocationsBuilderX86::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100616 CreateFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400617}
618
619void IntrinsicCodeGeneratorX86::VisitMathSqrt(HInvoke* invoke) {
620 LocationSummary* locations = invoke->GetLocations();
621 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
622 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
623
624 GetAssembler()->sqrtsd(out, in);
625}
626
Mark Mendellfb8d2792015-03-31 22:16:59 -0400627static void InvokeOutOfLineIntrinsic(CodeGeneratorX86* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100628 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400629
630 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100631 codegen->GenerateStaticOrDirectCall(invoke->AsInvokeStaticOrDirect(),
632 Location::RegisterLocation(EAX));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400633
634 // Copy the result back to the expected output.
635 Location out = invoke->GetLocations()->Out();
636 if (out.IsValid()) {
637 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700638 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400639 }
640}
641
Vladimir Markoca6fff82017-10-03 14:49:14 +0100642static void CreateSSE41FPToFPLocations(ArenaAllocator* allocator,
643 HInvoke* invoke,
644 CodeGeneratorX86* codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400645 // Do we have instruction support?
646 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100647 CreateFPToFPLocations(allocator, invoke);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400648 return;
649 }
650
651 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100652 LocationSummary* locations =
653 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400654 InvokeRuntimeCallingConvention calling_convention;
655 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
656 locations->SetOut(Location::FpuRegisterLocation(XMM0));
657 // Needs to be EAX for the invoke.
658 locations->AddTemp(Location::RegisterLocation(EAX));
659}
660
661static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86* codegen,
662 HInvoke* invoke,
663 X86Assembler* assembler,
664 int round_mode) {
665 LocationSummary* locations = invoke->GetLocations();
666 if (locations->WillCall()) {
667 InvokeOutOfLineIntrinsic(codegen, invoke);
668 } else {
669 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
670 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
671 __ roundsd(out, in, Immediate(round_mode));
672 }
673}
674
675void IntrinsicLocationsBuilderX86::VisitMathCeil(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100676 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400677}
678
679void IntrinsicCodeGeneratorX86::VisitMathCeil(HInvoke* invoke) {
680 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
681}
682
683void IntrinsicLocationsBuilderX86::VisitMathFloor(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100684 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400685}
686
687void IntrinsicCodeGeneratorX86::VisitMathFloor(HInvoke* invoke) {
688 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
689}
690
691void IntrinsicLocationsBuilderX86::VisitMathRint(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100692 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400693}
694
695void IntrinsicCodeGeneratorX86::VisitMathRint(HInvoke* invoke) {
696 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
697}
698
Mark Mendellfb8d2792015-03-31 22:16:59 -0400699void IntrinsicLocationsBuilderX86::VisitMathRoundFloat(HInvoke* invoke) {
700 // Do we have instruction support?
701 if (codegen_->GetInstructionSetFeatures().HasSSE4_1()) {
Aart Bik2c9f4952016-08-01 16:52:27 -0700702 HInvokeStaticOrDirect* static_or_direct = invoke->AsInvokeStaticOrDirect();
703 DCHECK(static_or_direct != nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100704 LocationSummary* locations =
705 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400706 locations->SetInAt(0, Location::RequiresFpuRegister());
Aart Bik2c9f4952016-08-01 16:52:27 -0700707 if (static_or_direct->HasSpecialInput() &&
708 invoke->InputAt(
709 static_or_direct->GetSpecialInputIndex())->IsX86ComputeBaseMethodAddress()) {
710 locations->SetInAt(1, Location::RequiresRegister());
711 }
Nicolas Geoffrayd9b92402015-04-21 10:02:22 +0100712 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400713 locations->AddTemp(Location::RequiresFpuRegister());
714 locations->AddTemp(Location::RequiresFpuRegister());
715 return;
716 }
717
718 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100719 LocationSummary* locations =
720 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400721 InvokeRuntimeCallingConvention calling_convention;
722 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
723 locations->SetOut(Location::RegisterLocation(EAX));
724 // Needs to be EAX for the invoke.
725 locations->AddTemp(Location::RegisterLocation(EAX));
726}
727
728void IntrinsicCodeGeneratorX86::VisitMathRoundFloat(HInvoke* invoke) {
729 LocationSummary* locations = invoke->GetLocations();
Aart Bik2c9f4952016-08-01 16:52:27 -0700730 if (locations->WillCall()) { // TODO: can we reach this?
Mark Mendellfb8d2792015-03-31 22:16:59 -0400731 InvokeOutOfLineIntrinsic(codegen_, invoke);
732 return;
733 }
734
Mark Mendellfb8d2792015-03-31 22:16:59 -0400735 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
Aart Bik2c9f4952016-08-01 16:52:27 -0700736 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
737 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mark Mendellfb8d2792015-03-31 22:16:59 -0400738 Register out = locations->Out().AsRegister<Register>();
Aart Bik2c9f4952016-08-01 16:52:27 -0700739 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400740 X86Assembler* assembler = GetAssembler();
741
Aart Bik2c9f4952016-08-01 16:52:27 -0700742 // Since no direct x86 rounding instruction matches the required semantics,
743 // this intrinsic is implemented as follows:
744 // result = floor(in);
745 // if (in - result >= 0.5f)
746 // result = result + 1.0f;
747 __ movss(t2, in);
748 __ roundss(t1, in, Immediate(1));
749 __ subss(t2, t1);
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700750 if (locations->GetInputCount() == 2 && locations->InAt(1).IsValid()) {
751 // Direct constant area available.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000752 HX86ComputeBaseMethodAddress* method_address =
753 invoke->InputAt(1)->AsX86ComputeBaseMethodAddress();
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700754 Register constant_area = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000755 __ comiss(t2, codegen_->LiteralInt32Address(bit_cast<int32_t, float>(0.5f),
756 method_address,
757 constant_area));
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700758 __ j(kBelow, &skip_incr);
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000759 __ addss(t1, codegen_->LiteralInt32Address(bit_cast<int32_t, float>(1.0f),
760 method_address,
761 constant_area));
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700762 __ Bind(&skip_incr);
763 } else {
764 // No constant area: go through stack.
765 __ pushl(Immediate(bit_cast<int32_t, float>(0.5f)));
766 __ pushl(Immediate(bit_cast<int32_t, float>(1.0f)));
767 __ comiss(t2, Address(ESP, 4));
768 __ j(kBelow, &skip_incr);
769 __ addss(t1, Address(ESP, 0));
770 __ Bind(&skip_incr);
771 __ addl(ESP, Immediate(8));
772 }
Mark Mendellfb8d2792015-03-31 22:16:59 -0400773
Aart Bik2c9f4952016-08-01 16:52:27 -0700774 // Final conversion to an integer. Unfortunately this also does not have a
775 // direct x86 instruction, since NaN should map to 0 and large positive
776 // values need to be clipped to the extreme value.
Mark Mendellfb8d2792015-03-31 22:16:59 -0400777 __ movl(out, Immediate(kPrimIntMax));
Aart Bik2c9f4952016-08-01 16:52:27 -0700778 __ cvtsi2ss(t2, out);
779 __ comiss(t1, t2);
780 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
781 __ movl(out, Immediate(0)); // does not change flags
782 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
783 __ cvttss2si(out, t1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400784 __ Bind(&done);
785}
786
Vladimir Markoca6fff82017-10-03 14:49:14 +0100787static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
788 LocationSummary* locations =
789 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400790 InvokeRuntimeCallingConvention calling_convention;
791 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
792 locations->SetOut(Location::FpuRegisterLocation(XMM0));
793}
794
795static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86* codegen, QuickEntrypointEnum entry) {
796 LocationSummary* locations = invoke->GetLocations();
797 DCHECK(locations->WillCall());
798 DCHECK(invoke->IsInvokeStaticOrDirect());
799 X86Assembler* assembler = codegen->GetAssembler();
800
801 // We need some place to pass the parameters.
802 __ subl(ESP, Immediate(16));
803 __ cfi().AdjustCFAOffset(16);
804
805 // Pass the parameters at the bottom of the stack.
806 __ movsd(Address(ESP, 0), XMM0);
807
808 // If we have a second parameter, pass it next.
809 if (invoke->GetNumberOfArguments() == 2) {
810 __ movsd(Address(ESP, 8), XMM1);
811 }
812
813 // Now do the actual call.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100814 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Mark Mendella4f12202015-08-06 15:23:34 -0400815
816 // Extract the return value from the FP stack.
817 __ fstpl(Address(ESP, 0));
818 __ movsd(XMM0, Address(ESP, 0));
819
820 // And clean up the stack.
821 __ addl(ESP, Immediate(16));
822 __ cfi().AdjustCFAOffset(-16);
Mark Mendella4f12202015-08-06 15:23:34 -0400823}
824
825void IntrinsicLocationsBuilderX86::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100826 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400827}
828
829void IntrinsicCodeGeneratorX86::VisitMathCos(HInvoke* invoke) {
830 GenFPToFPCall(invoke, codegen_, kQuickCos);
831}
832
833void IntrinsicLocationsBuilderX86::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100834 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400835}
836
837void IntrinsicCodeGeneratorX86::VisitMathSin(HInvoke* invoke) {
838 GenFPToFPCall(invoke, codegen_, kQuickSin);
839}
840
841void IntrinsicLocationsBuilderX86::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100842 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400843}
844
845void IntrinsicCodeGeneratorX86::VisitMathAcos(HInvoke* invoke) {
846 GenFPToFPCall(invoke, codegen_, kQuickAcos);
847}
848
849void IntrinsicLocationsBuilderX86::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100850 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400851}
852
853void IntrinsicCodeGeneratorX86::VisitMathAsin(HInvoke* invoke) {
854 GenFPToFPCall(invoke, codegen_, kQuickAsin);
855}
856
857void IntrinsicLocationsBuilderX86::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100858 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400859}
860
861void IntrinsicCodeGeneratorX86::VisitMathAtan(HInvoke* invoke) {
862 GenFPToFPCall(invoke, codegen_, kQuickAtan);
863}
864
865void IntrinsicLocationsBuilderX86::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100866 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400867}
868
869void IntrinsicCodeGeneratorX86::VisitMathCbrt(HInvoke* invoke) {
870 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
871}
872
873void IntrinsicLocationsBuilderX86::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100874 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400875}
876
877void IntrinsicCodeGeneratorX86::VisitMathCosh(HInvoke* invoke) {
878 GenFPToFPCall(invoke, codegen_, kQuickCosh);
879}
880
881void IntrinsicLocationsBuilderX86::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100882 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400883}
884
885void IntrinsicCodeGeneratorX86::VisitMathExp(HInvoke* invoke) {
886 GenFPToFPCall(invoke, codegen_, kQuickExp);
887}
888
889void IntrinsicLocationsBuilderX86::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100890 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400891}
892
893void IntrinsicCodeGeneratorX86::VisitMathExpm1(HInvoke* invoke) {
894 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
895}
896
897void IntrinsicLocationsBuilderX86::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100898 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400899}
900
901void IntrinsicCodeGeneratorX86::VisitMathLog(HInvoke* invoke) {
902 GenFPToFPCall(invoke, codegen_, kQuickLog);
903}
904
905void IntrinsicLocationsBuilderX86::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100906 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400907}
908
909void IntrinsicCodeGeneratorX86::VisitMathLog10(HInvoke* invoke) {
910 GenFPToFPCall(invoke, codegen_, kQuickLog10);
911}
912
913void IntrinsicLocationsBuilderX86::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100914 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400915}
916
917void IntrinsicCodeGeneratorX86::VisitMathSinh(HInvoke* invoke) {
918 GenFPToFPCall(invoke, codegen_, kQuickSinh);
919}
920
921void IntrinsicLocationsBuilderX86::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100922 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400923}
924
925void IntrinsicCodeGeneratorX86::VisitMathTan(HInvoke* invoke) {
926 GenFPToFPCall(invoke, codegen_, kQuickTan);
927}
928
929void IntrinsicLocationsBuilderX86::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100930 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400931}
932
933void IntrinsicCodeGeneratorX86::VisitMathTanh(HInvoke* invoke) {
934 GenFPToFPCall(invoke, codegen_, kQuickTanh);
935}
936
Vladimir Markoca6fff82017-10-03 14:49:14 +0100937static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
938 LocationSummary* locations =
939 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400940 InvokeRuntimeCallingConvention calling_convention;
941 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
942 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
943 locations->SetOut(Location::FpuRegisterLocation(XMM0));
944}
945
946void IntrinsicLocationsBuilderX86::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100947 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400948}
949
950void IntrinsicCodeGeneratorX86::VisitMathAtan2(HInvoke* invoke) {
951 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
952}
953
Vladimir Marko4d179872018-01-19 14:50:10 +0000954void IntrinsicLocationsBuilderX86::VisitMathPow(HInvoke* invoke) {
955 CreateFPFPToFPCallLocations(allocator_, invoke);
956}
957
958void IntrinsicCodeGeneratorX86::VisitMathPow(HInvoke* invoke) {
959 GenFPToFPCall(invoke, codegen_, kQuickPow);
960}
961
Mark Mendella4f12202015-08-06 15:23:34 -0400962void IntrinsicLocationsBuilderX86::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100963 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400964}
965
966void IntrinsicCodeGeneratorX86::VisitMathHypot(HInvoke* invoke) {
967 GenFPToFPCall(invoke, codegen_, kQuickHypot);
968}
969
970void IntrinsicLocationsBuilderX86::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100971 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400972}
973
974void IntrinsicCodeGeneratorX86::VisitMathNextAfter(HInvoke* invoke) {
975 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
976}
977
Mark Mendell6bc53a92015-07-01 14:26:52 -0400978void IntrinsicLocationsBuilderX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
979 // We need at least two of the positions or length to be an integer constant,
980 // or else we won't have enough free registers.
981 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
982 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
983 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
984
985 int num_constants =
986 ((src_pos != nullptr) ? 1 : 0)
987 + ((dest_pos != nullptr) ? 1 : 0)
988 + ((length != nullptr) ? 1 : 0);
989
990 if (num_constants < 2) {
991 // Not enough free registers.
992 return;
993 }
994
995 // As long as we are checking, we might as well check to see if the src and dest
996 // positions are >= 0.
997 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
998 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
999 // We will have to fail anyways.
1000 return;
1001 }
1002
1003 // And since we are already checking, check the length too.
1004 if (length != nullptr) {
1005 int32_t len = length->GetValue();
1006 if (len < 0) {
1007 // Just call as normal.
1008 return;
1009 }
1010 }
1011
1012 // Okay, it is safe to generate inline code.
1013 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001014 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001015 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
1016 locations->SetInAt(0, Location::RequiresRegister());
1017 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1018 locations->SetInAt(2, Location::RequiresRegister());
1019 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
1020 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
1021
1022 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1023 locations->AddTemp(Location::RegisterLocation(ESI));
1024 locations->AddTemp(Location::RegisterLocation(EDI));
1025 locations->AddTemp(Location::RegisterLocation(ECX));
1026}
1027
1028static void CheckPosition(X86Assembler* assembler,
1029 Location pos,
1030 Register input,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001031 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -07001032 SlowPathCode* slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001033 Register temp,
1034 bool length_is_input_length = false) {
1035 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -04001036 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
1037
1038 if (pos.IsConstant()) {
1039 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
1040 if (pos_const == 0) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001041 if (!length_is_input_length) {
1042 // Check that length(input) >= length.
1043 if (length.IsConstant()) {
1044 __ cmpl(Address(input, length_offset),
1045 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1046 } else {
1047 __ cmpl(Address(input, length_offset), length.AsRegister<Register>());
1048 }
1049 __ j(kLess, slow_path->GetEntryLabel());
1050 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001051 } else {
1052 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001053 __ movl(temp, Address(input, length_offset));
1054 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001055 __ j(kLess, slow_path->GetEntryLabel());
1056
1057 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001058 if (length.IsConstant()) {
1059 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1060 } else {
1061 __ cmpl(temp, length.AsRegister<Register>());
1062 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001063 __ j(kLess, slow_path->GetEntryLabel());
1064 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001065 } else if (length_is_input_length) {
1066 // The only way the copy can succeed is if pos is zero.
1067 Register pos_reg = pos.AsRegister<Register>();
1068 __ testl(pos_reg, pos_reg);
1069 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -04001070 } else {
1071 // Check that pos >= 0.
1072 Register pos_reg = pos.AsRegister<Register>();
1073 __ testl(pos_reg, pos_reg);
1074 __ j(kLess, slow_path->GetEntryLabel());
1075
1076 // Check that pos <= length(input).
1077 __ cmpl(Address(input, length_offset), pos_reg);
1078 __ j(kLess, slow_path->GetEntryLabel());
1079
1080 // Check that (length(input) - pos) >= length.
1081 __ movl(temp, Address(input, length_offset));
1082 __ subl(temp, pos_reg);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001083 if (length.IsConstant()) {
1084 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1085 } else {
1086 __ cmpl(temp, length.AsRegister<Register>());
1087 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001088 __ j(kLess, slow_path->GetEntryLabel());
1089 }
1090}
1091
1092void IntrinsicCodeGeneratorX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
1093 X86Assembler* assembler = GetAssembler();
1094 LocationSummary* locations = invoke->GetLocations();
1095
1096 Register src = locations->InAt(0).AsRegister<Register>();
1097 Location srcPos = locations->InAt(1);
1098 Register dest = locations->InAt(2).AsRegister<Register>();
1099 Location destPos = locations->InAt(3);
1100 Location length = locations->InAt(4);
1101
1102 // Temporaries that we need for MOVSW.
1103 Register src_base = locations->GetTemp(0).AsRegister<Register>();
1104 DCHECK_EQ(src_base, ESI);
1105 Register dest_base = locations->GetTemp(1).AsRegister<Register>();
1106 DCHECK_EQ(dest_base, EDI);
1107 Register count = locations->GetTemp(2).AsRegister<Register>();
1108 DCHECK_EQ(count, ECX);
1109
Vladimir Marko174b2e22017-10-12 13:34:49 +01001110 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001111 codegen_->AddSlowPath(slow_path);
1112
1113 // Bail out if the source and destination are the same (to handle overlap).
1114 __ cmpl(src, dest);
1115 __ j(kEqual, slow_path->GetEntryLabel());
1116
1117 // Bail out if the source is null.
1118 __ testl(src, src);
1119 __ j(kEqual, slow_path->GetEntryLabel());
1120
1121 // Bail out if the destination is null.
1122 __ testl(dest, dest);
1123 __ j(kEqual, slow_path->GetEntryLabel());
1124
1125 // If the length is negative, bail out.
1126 // We have already checked in the LocationsBuilder for the constant case.
1127 if (!length.IsConstant()) {
1128 __ cmpl(length.AsRegister<Register>(), length.AsRegister<Register>());
1129 __ j(kLess, slow_path->GetEntryLabel());
1130 }
1131
1132 // We need the count in ECX.
1133 if (length.IsConstant()) {
1134 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1135 } else {
1136 __ movl(count, length.AsRegister<Register>());
1137 }
1138
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001139 // Validity checks: source. Use src_base as a temporary register.
1140 CheckPosition(assembler, srcPos, src, Location::RegisterLocation(count), slow_path, src_base);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001141
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001142 // Validity checks: dest. Use src_base as a temporary register.
1143 CheckPosition(assembler, destPos, dest, Location::RegisterLocation(count), slow_path, src_base);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001144
1145 // Okay, everything checks out. Finally time to do the copy.
1146 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001147 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001148 DCHECK_EQ(char_size, 2u);
1149
1150 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1151
1152 if (srcPos.IsConstant()) {
1153 int32_t srcPos_const = srcPos.GetConstant()->AsIntConstant()->GetValue();
1154 __ leal(src_base, Address(src, char_size * srcPos_const + data_offset));
1155 } else {
1156 __ leal(src_base, Address(src, srcPos.AsRegister<Register>(),
1157 ScaleFactor::TIMES_2, data_offset));
1158 }
1159 if (destPos.IsConstant()) {
1160 int32_t destPos_const = destPos.GetConstant()->AsIntConstant()->GetValue();
1161
1162 __ leal(dest_base, Address(dest, char_size * destPos_const + data_offset));
1163 } else {
1164 __ leal(dest_base, Address(dest, destPos.AsRegister<Register>(),
1165 ScaleFactor::TIMES_2, data_offset));
1166 }
1167
1168 // Do the move.
1169 __ rep_movsw();
1170
1171 __ Bind(slow_path->GetExitLabel());
1172}
1173
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001174void IntrinsicLocationsBuilderX86::VisitStringCompareTo(HInvoke* invoke) {
1175 // The inputs plus one temp.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001176 LocationSummary* locations = new (allocator_) LocationSummary(
1177 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001178 InvokeRuntimeCallingConvention calling_convention;
1179 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1180 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1181 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001182}
1183
1184void IntrinsicCodeGeneratorX86::VisitStringCompareTo(HInvoke* invoke) {
1185 X86Assembler* assembler = GetAssembler();
1186 LocationSummary* locations = invoke->GetLocations();
1187
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001188 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001189 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001190
1191 Register argument = locations->InAt(1).AsRegister<Register>();
1192 __ testl(argument, argument);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001193 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001194 codegen_->AddSlowPath(slow_path);
1195 __ j(kEqual, slow_path->GetEntryLabel());
1196
Serban Constantinescuba45db02016-07-12 22:53:02 +01001197 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001198 __ Bind(slow_path->GetExitLabel());
1199}
1200
Agi Csakid7138c82015-08-13 17:46:44 -07001201void IntrinsicLocationsBuilderX86::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoda283052017-11-07 21:17:24 +00001202 if (kEmitCompilerReadBarrier &&
1203 !StringEqualsOptimizations(invoke).GetArgumentIsString() &&
1204 !StringEqualsOptimizations(invoke).GetNoReadBarrierForStringClass()) {
1205 // No support for this odd case (String class is moveable, not in the boot image).
1206 return;
1207 }
1208
Vladimir Markoca6fff82017-10-03 14:49:14 +01001209 LocationSummary* locations =
1210 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakid7138c82015-08-13 17:46:44 -07001211 locations->SetInAt(0, Location::RequiresRegister());
1212 locations->SetInAt(1, Location::RequiresRegister());
1213
1214 // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction.
1215 locations->AddTemp(Location::RegisterLocation(ECX));
1216 locations->AddTemp(Location::RegisterLocation(EDI));
1217
1218 // Set output, ESI needed for repe_cmpsl instruction anyways.
1219 locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap);
1220}
1221
1222void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) {
1223 X86Assembler* assembler = GetAssembler();
1224 LocationSummary* locations = invoke->GetLocations();
1225
1226 Register str = locations->InAt(0).AsRegister<Register>();
1227 Register arg = locations->InAt(1).AsRegister<Register>();
1228 Register ecx = locations->GetTemp(0).AsRegister<Register>();
1229 Register edi = locations->GetTemp(1).AsRegister<Register>();
1230 Register esi = locations->Out().AsRegister<Register>();
1231
Mark Mendell0c9497d2015-08-21 09:30:05 -04001232 NearLabel end, return_true, return_false;
Agi Csakid7138c82015-08-13 17:46:44 -07001233
1234 // Get offsets of count, value, and class fields within a string object.
1235 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1236 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1237 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1238
1239 // Note that the null check must have been done earlier.
1240 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1241
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001242 StringEqualsOptimizations optimizations(invoke);
1243 if (!optimizations.GetArgumentNotNull()) {
1244 // Check if input is null, return false if it is.
1245 __ testl(arg, arg);
1246 __ j(kEqual, &return_false);
1247 }
Agi Csakid7138c82015-08-13 17:46:44 -07001248
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001249 if (!optimizations.GetArgumentIsString()) {
Vladimir Marko53b52002016-05-24 19:30:45 +01001250 // Instanceof check for the argument by comparing class fields.
1251 // All string objects must have the same type since String cannot be subclassed.
1252 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1253 // 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 +01001254 __ movl(ecx, Address(str, class_offset));
1255 __ cmpl(ecx, Address(arg, class_offset));
1256 __ j(kNotEqual, &return_false);
1257 }
Agi Csakid7138c82015-08-13 17:46:44 -07001258
1259 // Reference equality check, return true if same reference.
1260 __ cmpl(str, arg);
1261 __ j(kEqual, &return_true);
1262
jessicahandojo4877b792016-09-08 19:49:13 -07001263 // Load length and compression flag of receiver string.
Agi Csakid7138c82015-08-13 17:46:44 -07001264 __ movl(ecx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001265 // Check if lengths and compression flags are equal, return false if they're not.
1266 // Two identical strings will always have same compression style since
1267 // compression style is decided on alloc.
Agi Csakid7138c82015-08-13 17:46:44 -07001268 __ cmpl(ecx, Address(arg, count_offset));
1269 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001270 // Return true if strings are empty. Even with string compression `count == 0` means empty.
1271 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1272 "Expecting 0=compressed, 1=uncompressed");
1273 __ jecxz(&return_true);
Agi Csakid7138c82015-08-13 17:46:44 -07001274
jessicahandojo4877b792016-09-08 19:49:13 -07001275 if (mirror::kUseStringCompression) {
1276 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001277 // Extract length and differentiate between both compressed or both uncompressed.
1278 // Different compression style is cut above.
1279 __ shrl(ecx, Immediate(1));
1280 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001281 // Divide string length by 2, rounding up, and continue as if uncompressed.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001282 __ addl(ecx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001283 __ shrl(ecx, Immediate(1));
1284 __ Bind(&string_uncompressed);
1285 }
Agi Csakid7138c82015-08-13 17:46:44 -07001286 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1287 __ leal(esi, Address(str, value_offset));
1288 __ leal(edi, Address(arg, value_offset));
1289
jessicahandojo4877b792016-09-08 19:49:13 -07001290 // Divide string length by 2 to compare characters 2 at a time and adjust for lengths not
1291 // divisible by 2.
Agi Csakid7138c82015-08-13 17:46:44 -07001292 __ addl(ecx, Immediate(1));
1293 __ shrl(ecx, Immediate(1));
1294
jessicahandojo4877b792016-09-08 19:49:13 -07001295 // Assertions that must hold in order to compare strings 2 characters (uncompressed)
1296 // or 4 characters (compressed) at a time.
Agi Csakid7138c82015-08-13 17:46:44 -07001297 DCHECK_ALIGNED(value_offset, 4);
1298 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1299
1300 // Loop to compare strings two characters at a time starting at the beginning of the string.
1301 __ repe_cmpsl();
1302 // If strings are not equal, zero flag will be cleared.
1303 __ j(kNotEqual, &return_false);
1304
1305 // Return true and exit the function.
1306 // If loop does not result in returning false, we return true.
1307 __ Bind(&return_true);
1308 __ movl(esi, Immediate(1));
1309 __ jmp(&end);
1310
1311 // Return false and exit the function.
1312 __ Bind(&return_false);
1313 __ xorl(esi, esi);
1314 __ Bind(&end);
1315}
1316
Andreas Gampe21030dd2015-05-07 14:46:15 -07001317static void CreateStringIndexOfLocations(HInvoke* invoke,
1318 ArenaAllocator* allocator,
1319 bool start_at_zero) {
1320 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1321 LocationSummary::kCallOnSlowPath,
1322 kIntrinsified);
1323 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1324 locations->SetInAt(0, Location::RegisterLocation(EDI));
1325 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1326 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1327 // of the instruction explicitly.
1328 // Note: This works as we don't clobber EAX anywhere.
1329 locations->SetInAt(1, Location::RegisterLocation(EAX));
1330 if (!start_at_zero) {
1331 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1332 }
1333 // As we clobber EDI during execution anyways, also use it as the output.
1334 locations->SetOut(Location::SameAsFirstInput());
1335
1336 // repne scasw uses ECX as the counter.
1337 locations->AddTemp(Location::RegisterLocation(ECX));
1338 // Need another temporary to be able to compute the result.
1339 locations->AddTemp(Location::RequiresRegister());
jessicahandojo4877b792016-09-08 19:49:13 -07001340 if (mirror::kUseStringCompression) {
1341 // Need another temporary to be able to save unflagged string length.
1342 locations->AddTemp(Location::RequiresRegister());
1343 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001344}
1345
1346static void GenerateStringIndexOf(HInvoke* invoke,
1347 X86Assembler* assembler,
1348 CodeGeneratorX86* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001349 bool start_at_zero) {
1350 LocationSummary* locations = invoke->GetLocations();
1351
1352 // Note that the null check must have been done earlier.
1353 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1354
1355 Register string_obj = locations->InAt(0).AsRegister<Register>();
1356 Register search_value = locations->InAt(1).AsRegister<Register>();
1357 Register counter = locations->GetTemp(0).AsRegister<Register>();
1358 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1359 Register out = locations->Out().AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07001360 // Only used when string compression feature is on.
1361 Register string_length_flagged;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001362
1363 // Check our assumptions for registers.
1364 DCHECK_EQ(string_obj, EDI);
1365 DCHECK_EQ(search_value, EAX);
1366 DCHECK_EQ(counter, ECX);
1367 DCHECK_EQ(out, EDI);
1368
1369 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001370 // 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 -07001371 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001372 HInstruction* code_point = invoke->InputAt(1);
1373 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001374 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001375 std::numeric_limits<uint16_t>::max()) {
1376 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1377 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001378 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001379 codegen->AddSlowPath(slow_path);
1380 __ jmp(slow_path->GetEntryLabel());
1381 __ Bind(slow_path->GetExitLabel());
1382 return;
1383 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001384 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001385 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001386 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001387 codegen->AddSlowPath(slow_path);
1388 __ j(kAbove, slow_path->GetEntryLabel());
1389 }
1390
1391 // From here down, we know that we are looking for a char that fits in 16 bits.
1392 // Location of reference to data array within the String object.
1393 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1394 // Location of count within the String object.
1395 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1396
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001397 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001398 __ movl(string_length, Address(string_obj, count_offset));
1399
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001400 // Do a zero-length check. Even with string compression `count == 0` means empty.
1401 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1402 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe21030dd2015-05-07 14:46:15 -07001403 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001404 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001405 __ testl(string_length, string_length);
1406 __ j(kEqual, &not_found_label);
1407
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001408 if (mirror::kUseStringCompression) {
1409 string_length_flagged = locations->GetTemp(2).AsRegister<Register>();
1410 __ movl(string_length_flagged, string_length);
1411 // Extract the length and shift out the least significant bit used as compression flag.
1412 __ shrl(string_length, Immediate(1));
1413 }
1414
Andreas Gampe21030dd2015-05-07 14:46:15 -07001415 if (start_at_zero) {
1416 // Number of chars to scan is the same as the string length.
1417 __ movl(counter, string_length);
1418
1419 // Move to the start of the string.
1420 __ addl(string_obj, Immediate(value_offset));
1421 } else {
1422 Register start_index = locations->InAt(2).AsRegister<Register>();
1423
1424 // Do a start_index check.
1425 __ cmpl(start_index, string_length);
1426 __ j(kGreaterEqual, &not_found_label);
1427
1428 // Ensure we have a start index >= 0;
1429 __ xorl(counter, counter);
1430 __ cmpl(start_index, Immediate(0));
1431 __ cmovl(kGreater, counter, start_index);
1432
jessicahandojo4877b792016-09-08 19:49:13 -07001433 if (mirror::kUseStringCompression) {
1434 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001435 __ testl(string_length_flagged, Immediate(1));
1436 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001437 // Move to the start of the string: string_obj + value_offset + start_index.
1438 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1439 __ jmp(&modify_counter);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001440
jessicahandojo4877b792016-09-08 19:49:13 -07001441 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1442 __ Bind(&offset_uncompressed_label);
1443 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1444
1445 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1446 // compare.
1447 __ Bind(&modify_counter);
1448 } else {
1449 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1450 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001451 __ negl(counter);
1452 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1453 }
1454
jessicahandojo4877b792016-09-08 19:49:13 -07001455 if (mirror::kUseStringCompression) {
1456 NearLabel uncompressed_string_comparison;
1457 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001458 __ testl(string_length_flagged, Immediate(1));
1459 __ j(kNotZero, &uncompressed_string_comparison);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001460
jessicahandojo4877b792016-09-08 19:49:13 -07001461 // Check if EAX (search_value) is ASCII.
1462 __ cmpl(search_value, Immediate(127));
1463 __ j(kGreater, &not_found_label);
1464 // Comparing byte-per-byte.
1465 __ repne_scasb();
1466 __ jmp(&comparison_done);
1467
1468 // Everything is set up for repne scasw:
1469 // * Comparison address in EDI.
1470 // * Counter in ECX.
1471 __ Bind(&uncompressed_string_comparison);
1472 __ repne_scasw();
1473 __ Bind(&comparison_done);
1474 } else {
1475 __ repne_scasw();
1476 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001477 // Did we find a match?
1478 __ j(kNotEqual, &not_found_label);
1479
1480 // Yes, we matched. Compute the index of the result.
1481 __ subl(string_length, counter);
1482 __ leal(out, Address(string_length, -1));
1483
Mark Mendell0c9497d2015-08-21 09:30:05 -04001484 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001485 __ jmp(&done);
1486
1487 // Failed to match; return -1.
1488 __ Bind(&not_found_label);
1489 __ movl(out, Immediate(-1));
1490
1491 // And join up at the end.
1492 __ Bind(&done);
1493 if (slow_path != nullptr) {
1494 __ Bind(slow_path->GetExitLabel());
1495 }
1496}
1497
1498void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001499 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001500}
1501
1502void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001503 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001504}
1505
1506void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001507 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001508}
1509
1510void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001511 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001512}
1513
Jeff Hao848f70a2014-01-15 13:49:50 -08001514void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001515 LocationSummary* locations = new (allocator_) LocationSummary(
1516 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001517 InvokeRuntimeCallingConvention calling_convention;
1518 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1519 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1520 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1521 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1522 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001523}
1524
1525void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1526 X86Assembler* assembler = GetAssembler();
1527 LocationSummary* locations = invoke->GetLocations();
1528
1529 Register byte_array = locations->InAt(0).AsRegister<Register>();
1530 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001531 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001532 codegen_->AddSlowPath(slow_path);
1533 __ j(kEqual, slow_path->GetEntryLabel());
1534
Serban Constantinescuba45db02016-07-12 22:53:02 +01001535 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001536 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001537 __ Bind(slow_path->GetExitLabel());
1538}
1539
1540void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001541 LocationSummary* locations =
1542 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001543 InvokeRuntimeCallingConvention calling_convention;
1544 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1545 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1546 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1547 locations->SetOut(Location::RegisterLocation(EAX));
1548}
1549
1550void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001551 // No need to emit code checking whether `locations->InAt(2)` is a null
1552 // pointer, as callers of the native method
1553 //
1554 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1555 //
1556 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001557 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001558 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001559}
1560
1561void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001562 LocationSummary* locations = new (allocator_) LocationSummary(
1563 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001564 InvokeRuntimeCallingConvention calling_convention;
1565 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1566 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001567}
1568
1569void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1570 X86Assembler* assembler = GetAssembler();
1571 LocationSummary* locations = invoke->GetLocations();
1572
1573 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1574 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001575 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001576 codegen_->AddSlowPath(slow_path);
1577 __ j(kEqual, slow_path->GetEntryLabel());
1578
Serban Constantinescuba45db02016-07-12 22:53:02 +01001579 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001580 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001581 __ Bind(slow_path->GetExitLabel());
1582}
1583
Mark Mendell8f8926a2015-08-17 11:39:06 -04001584void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1585 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001586 LocationSummary* locations =
1587 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001588 locations->SetInAt(0, Location::RequiresRegister());
1589 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1590 // Place srcEnd in ECX to save a move below.
1591 locations->SetInAt(2, Location::RegisterLocation(ECX));
1592 locations->SetInAt(3, Location::RequiresRegister());
1593 locations->SetInAt(4, Location::RequiresRegister());
1594
1595 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1596 // We don't have enough registers to also grab ECX, so handle below.
1597 locations->AddTemp(Location::RegisterLocation(ESI));
1598 locations->AddTemp(Location::RegisterLocation(EDI));
1599}
1600
1601void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1602 X86Assembler* assembler = GetAssembler();
1603 LocationSummary* locations = invoke->GetLocations();
1604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001605 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001606 // Location of data in char array buffer.
1607 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1608 // Location of char array data in string.
1609 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1610
1611 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1612 Register obj = locations->InAt(0).AsRegister<Register>();
1613 Location srcBegin = locations->InAt(1);
1614 int srcBegin_value =
1615 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1616 Register srcEnd = locations->InAt(2).AsRegister<Register>();
1617 Register dst = locations->InAt(3).AsRegister<Register>();
1618 Register dstBegin = locations->InAt(4).AsRegister<Register>();
1619
1620 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001621 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001622 DCHECK_EQ(char_size, 2u);
1623
Mark Mendell8f8926a2015-08-17 11:39:06 -04001624 // Compute the number of chars (words) to move.
jessicahandojo4877b792016-09-08 19:49:13 -07001625 // Save ECX, since we don't know if it will be used later.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001626 __ pushl(ECX);
1627 int stack_adjust = kX86WordSize;
1628 __ cfi().AdjustCFAOffset(stack_adjust);
1629 DCHECK_EQ(srcEnd, ECX);
1630 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001631 __ subl(ECX, Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001632 } else {
1633 DCHECK(srcBegin.IsRegister());
1634 __ subl(ECX, srcBegin.AsRegister<Register>());
1635 }
1636
jessicahandojo4877b792016-09-08 19:49:13 -07001637 NearLabel done;
1638 if (mirror::kUseStringCompression) {
1639 // Location of count in string
1640 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001641 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001642 DCHECK_EQ(c_char_size, 1u);
1643 __ pushl(EAX);
1644 __ cfi().AdjustCFAOffset(stack_adjust);
1645
1646 NearLabel copy_loop, copy_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001647 __ testl(Address(obj, count_offset), Immediate(1));
1648 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1649 "Expecting 0=compressed, 1=uncompressed");
1650 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001651 // Compute the address of the source string by adding the number of chars from
1652 // the source beginning to the value offset of a string.
1653 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1654
1655 // Start the loop to copy String's value to Array of Char.
1656 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1657 __ Bind(&copy_loop);
1658 __ jecxz(&done);
1659 // Use EAX temporary (convert byte from ESI to word).
1660 // TODO: Use LODSB/STOSW (not supported by X86Assembler) with AH initialized to 0.
1661 __ movzxb(EAX, Address(ESI, 0));
1662 __ movw(Address(EDI, 0), EAX);
1663 __ leal(EDI, Address(EDI, char_size));
1664 __ leal(ESI, Address(ESI, c_char_size));
1665 // TODO: Add support for LOOP to X86Assembler.
1666 __ subl(ECX, Immediate(1));
1667 __ jmp(&copy_loop);
1668 __ Bind(&copy_uncompressed);
1669 }
1670
1671 // Do the copy for uncompressed string.
1672 // Compute the address of the destination buffer.
1673 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1674 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001675 __ rep_movsw();
1676
jessicahandojo4877b792016-09-08 19:49:13 -07001677 __ Bind(&done);
1678 if (mirror::kUseStringCompression) {
1679 // Restore EAX.
1680 __ popl(EAX);
1681 __ cfi().AdjustCFAOffset(-stack_adjust);
1682 }
1683 // Restore ECX.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001684 __ popl(ECX);
1685 __ cfi().AdjustCFAOffset(-stack_adjust);
1686}
1687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001688static void GenPeek(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001689 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1690 Location out_loc = locations->Out();
1691 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1692 // to avoid a SIGBUS.
1693 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001694 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001695 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1696 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001697 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001698 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1699 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001700 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001701 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1702 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001703 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001704 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1705 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1706 break;
1707 default:
1708 LOG(FATAL) << "Type not recognized for peek: " << size;
1709 UNREACHABLE();
1710 }
1711}
1712
1713void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001714 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001715}
1716
1717void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001718 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001719}
1720
1721void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001722 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001723}
1724
1725void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001726 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001727}
1728
1729void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001730 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001731}
1732
1733void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001734 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001735}
1736
1737void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001738 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001739}
1740
1741void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001742 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001743}
1744
Vladimir Markoca6fff82017-10-03 14:49:14 +01001745static void CreateLongIntToVoidLocations(ArenaAllocator* allocator,
1746 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001747 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001748 LocationSummary* locations =
1749 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001750 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001751 HInstruction* value = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001752 if (size == DataType::Type::kInt8) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001753 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1754 } else {
1755 locations->SetInAt(1, Location::RegisterOrConstant(value));
1756 }
1757}
1758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759static void GenPoke(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001760 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1761 Location value_loc = locations->InAt(1);
1762 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1763 // to avoid a SIGBUS.
1764 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001765 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001766 if (value_loc.IsConstant()) {
1767 __ movb(Address(address, 0),
1768 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1769 } else {
1770 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1771 }
1772 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001773 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001774 if (value_loc.IsConstant()) {
1775 __ movw(Address(address, 0),
1776 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1777 } else {
1778 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1779 }
1780 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001781 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001782 if (value_loc.IsConstant()) {
1783 __ movl(Address(address, 0),
1784 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1785 } else {
1786 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1787 }
1788 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001789 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001790 if (value_loc.IsConstant()) {
1791 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1792 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1793 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1794 } else {
1795 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1796 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1797 }
1798 break;
1799 default:
1800 LOG(FATAL) << "Type not recognized for poke: " << size;
1801 UNREACHABLE();
1802 }
1803}
1804
1805void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001806 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt8, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001807}
1808
1809void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001810 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001811}
1812
1813void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001814 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001815}
1816
1817void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001819}
1820
1821void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001822 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001823}
1824
1825void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001826 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001827}
1828
1829void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001830 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt16, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001831}
1832
1833void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001834 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001835}
1836
1837void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001838 LocationSummary* locations =
1839 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001840 locations->SetOut(Location::RequiresRegister());
1841}
1842
1843void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1844 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001845 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86PointerSize>()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001846}
1847
Roland Levillain0d5a2812015-11-13 10:07:31 +00001848static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001849 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001850 bool is_volatile,
1851 CodeGeneratorX86* codegen) {
1852 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
1853 LocationSummary* locations = invoke->GetLocations();
1854 Location base_loc = locations->InAt(1);
1855 Register base = base_loc.AsRegister<Register>();
1856 Location offset_loc = locations->InAt(2);
1857 Register offset = offset_loc.AsRegisterPairLow<Register>();
1858 Location output_loc = locations->Out();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001859
1860 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001861 case DataType::Type::kInt32: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001862 Register output = output_loc.AsRegister<Register>();
1863 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain7c1559a2015-12-15 10:55:36 +00001864 break;
1865 }
1866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001867 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001868 Register output = output_loc.AsRegister<Register>();
1869 if (kEmitCompilerReadBarrier) {
1870 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001871 Address src(base, offset, ScaleFactor::TIMES_1, 0);
1872 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001873 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00001874 } else {
1875 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1876 codegen->GenerateReadBarrierSlow(
1877 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1878 }
1879 } else {
1880 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1881 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001882 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001883 break;
Roland Levillain4d027112015-07-01 15:41:14 +01001884 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001886 case DataType::Type::kInt64: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001887 Register output_lo = output_loc.AsRegisterPairLow<Register>();
1888 Register output_hi = output_loc.AsRegisterPairHigh<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001889 if (is_volatile) {
1890 // Need to use a XMM to read atomically.
1891 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1892 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
1893 __ movd(output_lo, temp);
1894 __ psrlq(temp, Immediate(32));
1895 __ movd(output_hi, temp);
1896 } else {
1897 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
1898 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
1899 }
1900 }
1901 break;
1902
1903 default:
1904 LOG(FATAL) << "Unsupported op size " << type;
1905 UNREACHABLE();
1906 }
1907}
1908
Vladimir Markoca6fff82017-10-03 14:49:14 +01001909static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Roland Levillain7c1559a2015-12-15 10:55:36 +00001910 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001911 DataType::Type type,
Roland Levillain7c1559a2015-12-15 10:55:36 +00001912 bool is_volatile) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001913 bool can_call = kEmitCompilerReadBarrier &&
1914 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1915 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001916 LocationSummary* locations =
1917 new (allocator) LocationSummary(invoke,
1918 can_call
1919 ? LocationSummary::kCallOnSlowPath
1920 : LocationSummary::kNoCall,
1921 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01001922 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001923 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001924 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001925 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1926 locations->SetInAt(1, Location::RequiresRegister());
1927 locations->SetInAt(2, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001928 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001929 if (is_volatile) {
1930 // Need to use XMM to read volatile.
1931 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01001932 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001933 } else {
1934 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1935 }
1936 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001937 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001938 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001939 }
1940}
1941
1942void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001943 CreateIntIntIntToIntLocations(
1944 allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001945}
1946void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001947 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001948}
1949void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001950 CreateIntIntIntToIntLocations(
1951 allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001952}
1953void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001954 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001955}
1956void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001957 CreateIntIntIntToIntLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001958 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001959}
1960void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001961 CreateIntIntIntToIntLocations(
1962 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001963}
1964
1965
1966void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001967 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001968}
1969void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001970 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001971}
1972void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001973 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001974}
1975void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001976 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001977}
1978void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001979 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001980}
1981void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001982 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001983}
1984
1985
Vladimir Markoca6fff82017-10-03 14:49:14 +01001986static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001987 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001988 HInvoke* invoke,
1989 bool is_volatile) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001990 LocationSummary* locations =
1991 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001992 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1993 locations->SetInAt(1, Location::RequiresRegister());
1994 locations->SetInAt(2, Location::RequiresRegister());
1995 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001996 if (type == DataType::Type::kReference) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001997 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001998 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04001999 // Ensure the value is in a byte register.
2000 locations->AddTemp(Location::RegisterLocation(ECX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002001 } else if (type == DataType::Type::kInt64 && is_volatile) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002002 locations->AddTemp(Location::RequiresFpuRegister());
2003 locations->AddTemp(Location::RequiresFpuRegister());
2004 }
2005}
2006
2007void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002008 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002009 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002010}
2011void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002012 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002013 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002014}
2015void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002016 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002017 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002018}
2019void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002020 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002021 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002022}
2023void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002024 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002025 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002026}
2027void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002028 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002029 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002030}
2031void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002032 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002033 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002034}
2035void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002036 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002037 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002038}
2039void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002040 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002041 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002042}
2043
2044// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2045// memory model.
2046static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002047 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04002048 bool is_volatile,
2049 CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002050 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04002051 Register base = locations->InAt(1).AsRegister<Register>();
2052 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2053 Location value_loc = locations->InAt(3);
2054
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002055 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002056 Register value_lo = value_loc.AsRegisterPairLow<Register>();
2057 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
2058 if (is_volatile) {
2059 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2060 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2061 __ movd(temp1, value_lo);
2062 __ movd(temp2, value_hi);
2063 __ punpckldq(temp1, temp2);
2064 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
2065 } else {
2066 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
2067 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
2068 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002070 Register temp = locations->GetTemp(0).AsRegister<Register>();
2071 __ movl(temp, value_loc.AsRegister<Register>());
2072 __ PoisonHeapReference(temp);
2073 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002074 } else {
2075 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
2076 }
2077
2078 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002079 codegen->MemoryFence();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002080 }
2081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002083 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04002084 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
2085 locations->GetTemp(1).AsRegister<Register>(),
2086 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002087 value_loc.AsRegister<Register>(),
2088 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002089 }
2090}
2091
2092void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002093 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002094}
2095void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002096 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002097}
2098void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002099 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002100}
2101void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 GenUnsafePut(
2103 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002104}
2105void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002106 GenUnsafePut(
2107 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002108}
2109void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002110 GenUnsafePut(
2111 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002112}
2113void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002115}
2116void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002117 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002118}
2119void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002121}
2122
Vladimir Markoca6fff82017-10-03 14:49:14 +01002123static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002125 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002126 bool can_call = kEmitCompilerReadBarrier &&
2127 kUseBakerReadBarrier &&
2128 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002129 LocationSummary* locations =
2130 new (allocator) LocationSummary(invoke,
2131 can_call
2132 ? LocationSummary::kCallOnSlowPath
2133 : LocationSummary::kNoCall,
2134 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002135 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2136 locations->SetInAt(1, Location::RequiresRegister());
2137 // Offset is a long, but in 32 bit mode, we only need the low word.
2138 // Can we update the invoke here to remove a TypeConvert to Long?
2139 locations->SetInAt(2, Location::RequiresRegister());
2140 // Expected value must be in EAX or EDX:EAX.
2141 // For long, new value must be in ECX:EBX.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002142 if (type == DataType::Type::kInt64) {
Mark Mendell58d25fd2015-04-03 14:52:31 -04002143 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
2144 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
2145 } else {
2146 locations->SetInAt(3, Location::RegisterLocation(EAX));
2147 locations->SetInAt(4, Location::RequiresRegister());
2148 }
2149
2150 // Force a byte register for the output.
2151 locations->SetOut(Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002152 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002153 // Need temporary registers for card-marking, and possibly for
2154 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002155 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002156 // Need a byte register for marking.
2157 locations->AddTemp(Location::RegisterLocation(ECX));
2158 }
2159}
2160
2161void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002162 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002163}
2164
2165void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002166 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002167}
2168
2169void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002170 // The only read barrier implementation supporting the
2171 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2172 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002173 return;
2174 }
2175
Vladimir Markoca6fff82017-10-03 14:49:14 +01002176 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002177}
2178
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002179static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002180 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002181 LocationSummary* locations = invoke->GetLocations();
2182
2183 Register base = locations->InAt(1).AsRegister<Register>();
2184 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2185 Location out = locations->Out();
2186 DCHECK_EQ(out.AsRegister<Register>(), EAX);
2187
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002188 // The address of the field within the holding object.
2189 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002191 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002192 // The only read barrier implementation supporting the
2193 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2194 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2195
2196 Location temp1_loc = locations->GetTemp(0);
2197 Register temp1 = temp1_loc.AsRegister<Register>();
2198 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2199
Roland Levillain4d027112015-07-01 15:41:14 +01002200 Register expected = locations->InAt(3).AsRegister<Register>();
Roland Levillainb488b782015-10-22 11:38:49 +01002201 // Ensure `expected` is in EAX (required by the CMPXCHG instruction).
Roland Levillain4d027112015-07-01 15:41:14 +01002202 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002203 Register value = locations->InAt(4).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01002204
Roland Levillainb488b782015-10-22 11:38:49 +01002205 // Mark card for object assuming new value is stored.
2206 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002207 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2208
2209 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2210 // Need to make sure the reference stored in the field is a to-space
2211 // one before attempting the CAS or the CAS could fail incorrectly.
2212 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2213 invoke,
2214 temp1_loc, // Unused, used only as a "temporary" within the read barrier.
2215 base,
2216 field_addr,
2217 /* needs_null_check */ false,
2218 /* always_update_field */ true,
2219 &temp2);
2220 }
Roland Levillainb488b782015-10-22 11:38:49 +01002221
2222 bool base_equals_value = (base == value);
2223 if (kPoisonHeapReferences) {
2224 if (base_equals_value) {
2225 // If `base` and `value` are the same register location, move
2226 // `value` to a temporary register. This way, poisoning
2227 // `value` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002228 value = temp1;
Roland Levillainb488b782015-10-22 11:38:49 +01002229 __ movl(value, base);
Roland Levillain4d027112015-07-01 15:41:14 +01002230 }
Roland Levillainb488b782015-10-22 11:38:49 +01002231
2232 // Check that the register allocator did not assign the location
2233 // of `expected` (EAX) to `value` nor to `base`, so that heap
2234 // poisoning (when enabled) works as intended below.
2235 // - If `value` were equal to `expected`, both references would
2236 // be poisoned twice, meaning they would not be poisoned at
2237 // all, as heap poisoning uses address negation.
2238 // - If `base` were equal to `expected`, poisoning `expected`
2239 // would invalidate `base`.
2240 DCHECK_NE(value, expected);
2241 DCHECK_NE(base, expected);
2242
2243 __ PoisonHeapReference(expected);
2244 __ PoisonHeapReference(value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002245 }
2246
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002247 __ LockCmpxchgl(field_addr, value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002248
Roland Levillain0d5a2812015-11-13 10:07:31 +00002249 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002250 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002251
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002252 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002253 __ setb(kZero, out.AsRegister<Register>());
2254 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002255
Roland Levillain391b8662015-12-18 11:43:38 +00002256 // If heap poisoning is enabled, we need to unpoison the values
2257 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002258 if (kPoisonHeapReferences) {
2259 if (base_equals_value) {
2260 // `value` has been moved to a temporary register, no need to
2261 // unpoison it.
2262 } else {
2263 // Ensure `value` is different from `out`, so that unpoisoning
2264 // the former does not invalidate the latter.
2265 DCHECK_NE(value, out.AsRegister<Register>());
2266 __ UnpoisonHeapReference(value);
2267 }
2268 // Do not unpoison the reference contained in register
2269 // `expected`, as it is the same as register `out` (EAX).
2270 }
2271 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002272 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002273 // Ensure the expected value is in EAX (required by the CMPXCHG
2274 // instruction).
2275 DCHECK_EQ(locations->InAt(3).AsRegister<Register>(), EAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002276 __ LockCmpxchgl(field_addr, locations->InAt(4).AsRegister<Register>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002278 // Ensure the expected value is in EAX:EDX and that the new
2279 // value is in EBX:ECX (required by the CMPXCHG8B instruction).
2280 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
2281 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
2282 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
2283 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002284 __ LockCmpxchg8b(field_addr);
Roland Levillainb488b782015-10-22 11:38:49 +01002285 } else {
2286 LOG(FATAL) << "Unexpected CAS type " << type;
2287 }
2288
Roland Levillain0d5a2812015-11-13 10:07:31 +00002289 // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we
2290 // don't need scheduling barriers at this time.
Roland Levillainb488b782015-10-22 11:38:49 +01002291
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002292 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002293 __ setb(kZero, out.AsRegister<Register>());
2294 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002295 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002296}
2297
2298void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002300}
2301
2302void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002304}
2305
2306void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002307 // The only read barrier implementation supporting the
2308 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2309 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002312}
2313
2314void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002315 LocationSummary* locations =
2316 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002317 locations->SetInAt(0, Location::RequiresRegister());
2318 locations->SetOut(Location::SameAsFirstInput());
2319 locations->AddTemp(Location::RequiresRegister());
2320}
2321
2322static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
2323 X86Assembler* assembler) {
2324 Immediate imm_shift(shift);
2325 Immediate imm_mask(mask);
2326 __ movl(temp, reg);
2327 __ shrl(reg, imm_shift);
2328 __ andl(temp, imm_mask);
2329 __ andl(reg, imm_mask);
2330 __ shll(temp, imm_shift);
2331 __ orl(reg, temp);
2332}
2333
2334void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002335 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002336 LocationSummary* locations = invoke->GetLocations();
2337
2338 Register reg = locations->InAt(0).AsRegister<Register>();
2339 Register temp = locations->GetTemp(0).AsRegister<Register>();
2340
2341 /*
2342 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2343 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2344 * compared to generic luni implementation which has 5 rounds of swapping bits.
2345 * x = bswap x
2346 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2347 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2348 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2349 */
2350 __ bswapl(reg);
2351 SwapBits(reg, temp, 1, 0x55555555, assembler);
2352 SwapBits(reg, temp, 2, 0x33333333, assembler);
2353 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2354}
2355
2356void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002357 LocationSummary* locations =
2358 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002359 locations->SetInAt(0, Location::RequiresRegister());
2360 locations->SetOut(Location::SameAsFirstInput());
2361 locations->AddTemp(Location::RequiresRegister());
2362}
2363
2364void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002365 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002366 LocationSummary* locations = invoke->GetLocations();
2367
2368 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
2369 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2370 Register temp = locations->GetTemp(0).AsRegister<Register>();
2371
2372 // We want to swap high/low, then bswap each one, and then do the same
2373 // as a 32 bit reverse.
2374 // Exchange high and low.
2375 __ movl(temp, reg_low);
2376 __ movl(reg_low, reg_high);
2377 __ movl(reg_high, temp);
2378
2379 // bit-reverse low
2380 __ bswapl(reg_low);
2381 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
2382 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
2383 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
2384
2385 // bit-reverse high
2386 __ bswapl(reg_high);
2387 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
2388 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
2389 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
2390}
2391
Aart Bikc39dac12016-01-21 08:59:48 -08002392static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002393 ArenaAllocator* allocator, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002394 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2395 // Do nothing if there is no popcnt support. This results in generating
2396 // a call for the intrinsic rather than direct code.
2397 return;
2398 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002399 LocationSummary* locations =
2400 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc39dac12016-01-21 08:59:48 -08002401 if (is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002402 locations->AddTemp(Location::RequiresRegister());
Aart Bikc39dac12016-01-21 08:59:48 -08002403 }
Aart Bik2a946072016-01-21 12:49:00 -08002404 locations->SetInAt(0, Location::Any());
Aart Bikc39dac12016-01-21 08:59:48 -08002405 locations->SetOut(Location::RequiresRegister());
2406}
2407
Aart Bika19616e2016-02-01 18:57:58 -08002408static void GenBitCount(X86Assembler* assembler,
2409 CodeGeneratorX86* codegen,
2410 HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002411 LocationSummary* locations = invoke->GetLocations();
2412 Location src = locations->InAt(0);
2413 Register out = locations->Out().AsRegister<Register>();
2414
2415 if (invoke->InputAt(0)->IsConstant()) {
2416 // Evaluate this at compile time.
2417 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002418 int32_t result = is_long
Aart Bikc39dac12016-01-21 08:59:48 -08002419 ? POPCOUNT(static_cast<uint64_t>(value))
2420 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002421 codegen->Load32BitValue(out, result);
Aart Bikc39dac12016-01-21 08:59:48 -08002422 return;
2423 }
2424
2425 // Handle the non-constant cases.
2426 if (!is_long) {
2427 if (src.IsRegister()) {
2428 __ popcntl(out, src.AsRegister<Register>());
2429 } else {
2430 DCHECK(src.IsStackSlot());
2431 __ popcntl(out, Address(ESP, src.GetStackIndex()));
2432 }
Aart Bik2a946072016-01-21 12:49:00 -08002433 } else {
2434 // The 64-bit case needs to worry about two parts.
2435 Register temp = locations->GetTemp(0).AsRegister<Register>();
2436 if (src.IsRegisterPair()) {
2437 __ popcntl(temp, src.AsRegisterPairLow<Register>());
2438 __ popcntl(out, src.AsRegisterPairHigh<Register>());
2439 } else {
2440 DCHECK(src.IsDoubleStackSlot());
2441 __ popcntl(temp, Address(ESP, src.GetStackIndex()));
2442 __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize)));
2443 }
2444 __ addl(out, temp);
Aart Bikc39dac12016-01-21 08:59:48 -08002445 }
Aart Bikc39dac12016-01-21 08:59:48 -08002446}
2447
2448void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002449 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002450}
2451
2452void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002453 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002454}
2455
2456void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002457 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002458}
2459
2460void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002461 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002462}
2463
Vladimir Markoca6fff82017-10-03 14:49:14 +01002464static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2465 LocationSummary* locations =
2466 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002467 if (is_long) {
2468 locations->SetInAt(0, Location::RequiresRegister());
2469 } else {
2470 locations->SetInAt(0, Location::Any());
2471 }
2472 locations->SetOut(Location::RequiresRegister());
2473}
2474
Aart Bika19616e2016-02-01 18:57:58 -08002475static void GenLeadingZeros(X86Assembler* assembler,
2476 CodeGeneratorX86* codegen,
2477 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002478 LocationSummary* locations = invoke->GetLocations();
2479 Location src = locations->InAt(0);
2480 Register out = locations->Out().AsRegister<Register>();
2481
2482 if (invoke->InputAt(0)->IsConstant()) {
2483 // Evaluate this at compile time.
2484 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2485 if (value == 0) {
2486 value = is_long ? 64 : 32;
2487 } else {
2488 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2489 }
Aart Bika19616e2016-02-01 18:57:58 -08002490 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002491 return;
2492 }
2493
2494 // Handle the non-constant cases.
2495 if (!is_long) {
2496 if (src.IsRegister()) {
2497 __ bsrl(out, src.AsRegister<Register>());
2498 } else {
2499 DCHECK(src.IsStackSlot());
2500 __ bsrl(out, Address(ESP, src.GetStackIndex()));
2501 }
2502
2503 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002504 NearLabel all_zeroes, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002505 __ j(kEqual, &all_zeroes);
2506
2507 // Correct the result from BSR to get the final CLZ result.
2508 __ xorl(out, Immediate(31));
2509 __ jmp(&done);
2510
2511 // Fix the zero case with the expected result.
2512 __ Bind(&all_zeroes);
2513 __ movl(out, Immediate(32));
2514
2515 __ Bind(&done);
2516 return;
2517 }
2518
2519 // 64 bit case needs to worry about both parts of the register.
2520 DCHECK(src.IsRegisterPair());
2521 Register src_lo = src.AsRegisterPairLow<Register>();
2522 Register src_hi = src.AsRegisterPairHigh<Register>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002523 NearLabel handle_low, done, all_zeroes;
Mark Mendelld5897672015-08-12 21:16:41 -04002524
2525 // Is the high word zero?
2526 __ testl(src_hi, src_hi);
2527 __ j(kEqual, &handle_low);
2528
2529 // High word is not zero. We know that the BSR result is defined in this case.
2530 __ bsrl(out, src_hi);
2531
2532 // Correct the result from BSR to get the final CLZ result.
2533 __ xorl(out, Immediate(31));
2534 __ jmp(&done);
2535
2536 // High word was zero. We have to compute the low word count and add 32.
2537 __ Bind(&handle_low);
2538 __ bsrl(out, src_lo);
2539 __ j(kEqual, &all_zeroes);
2540
2541 // We had a valid result. Use an XOR to both correct the result and add 32.
2542 __ xorl(out, Immediate(63));
2543 __ jmp(&done);
2544
2545 // All zero case.
2546 __ Bind(&all_zeroes);
2547 __ movl(out, Immediate(64));
2548
2549 __ Bind(&done);
2550}
2551
2552void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002553 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002554}
2555
2556void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002557 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002558}
2559
2560void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002561 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002562}
2563
2564void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002565 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002566}
2567
Vladimir Markoca6fff82017-10-03 14:49:14 +01002568static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2569 LocationSummary* locations =
2570 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002571 if (is_long) {
2572 locations->SetInAt(0, Location::RequiresRegister());
2573 } else {
2574 locations->SetInAt(0, Location::Any());
2575 }
2576 locations->SetOut(Location::RequiresRegister());
2577}
2578
Aart Bika19616e2016-02-01 18:57:58 -08002579static void GenTrailingZeros(X86Assembler* assembler,
2580 CodeGeneratorX86* codegen,
2581 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002582 LocationSummary* locations = invoke->GetLocations();
2583 Location src = locations->InAt(0);
2584 Register out = locations->Out().AsRegister<Register>();
2585
2586 if (invoke->InputAt(0)->IsConstant()) {
2587 // Evaluate this at compile time.
2588 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2589 if (value == 0) {
2590 value = is_long ? 64 : 32;
2591 } else {
2592 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2593 }
Aart Bika19616e2016-02-01 18:57:58 -08002594 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002595 return;
2596 }
2597
2598 // Handle the non-constant cases.
2599 if (!is_long) {
2600 if (src.IsRegister()) {
2601 __ bsfl(out, src.AsRegister<Register>());
2602 } else {
2603 DCHECK(src.IsStackSlot());
2604 __ bsfl(out, Address(ESP, src.GetStackIndex()));
2605 }
2606
2607 // BSF sets ZF if the input was zero, and the output is undefined.
2608 NearLabel done;
2609 __ j(kNotEqual, &done);
2610
2611 // Fix the zero case with the expected result.
2612 __ movl(out, Immediate(32));
2613
2614 __ Bind(&done);
2615 return;
2616 }
2617
2618 // 64 bit case needs to worry about both parts of the register.
2619 DCHECK(src.IsRegisterPair());
2620 Register src_lo = src.AsRegisterPairLow<Register>();
2621 Register src_hi = src.AsRegisterPairHigh<Register>();
2622 NearLabel done, all_zeroes;
2623
2624 // If the low word is zero, then ZF will be set. If not, we have the answer.
2625 __ bsfl(out, src_lo);
2626 __ j(kNotEqual, &done);
2627
2628 // Low word was zero. We have to compute the high word count and add 32.
2629 __ bsfl(out, src_hi);
2630 __ j(kEqual, &all_zeroes);
2631
2632 // We had a valid result. Add 32 to account for the low word being zero.
2633 __ addl(out, Immediate(32));
2634 __ jmp(&done);
2635
2636 // All zero case.
2637 __ Bind(&all_zeroes);
2638 __ movl(out, Immediate(64));
2639
2640 __ Bind(&done);
2641}
2642
2643void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002644 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002645}
2646
2647void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002648 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002649}
2650
2651void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002652 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002653}
2654
2655void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002656 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002657}
2658
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002659static bool IsSameInput(HInstruction* instruction, size_t input0, size_t input1) {
2660 return instruction->InputAt(input0) == instruction->InputAt(input1);
2661}
2662
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002663// Compute base address for the System.arraycopy intrinsic in `base`.
2664static void GenSystemArrayCopyBaseAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002665 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002666 const Register& array,
2667 const Location& pos,
2668 const Register& base) {
2669 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002670 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002671 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 DCHECK_EQ(type, DataType::Type::kReference);
2673 const int32_t element_size = DataType::Size(type);
2674 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002675 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
2676
2677 if (pos.IsConstant()) {
2678 int32_t constant = pos.GetConstant()->AsIntConstant()->GetValue();
2679 __ leal(base, Address(array, element_size * constant + data_offset));
2680 } else {
2681 __ leal(base, Address(array, pos.AsRegister<Register>(), scale_factor, data_offset));
2682 }
2683}
2684
2685// Compute end source address for the System.arraycopy intrinsic in `end`.
2686static void GenSystemArrayCopyEndAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002688 const Location& copy_length,
2689 const Register& base,
2690 const Register& end) {
2691 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002692 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002693 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002694 DCHECK_EQ(type, DataType::Type::kReference);
2695 const int32_t element_size = DataType::Size(type);
2696 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002697
2698 if (copy_length.IsConstant()) {
2699 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
2700 __ leal(end, Address(base, element_size * constant));
2701 } else {
2702 __ leal(end, Address(base, copy_length.AsRegister<Register>(), scale_factor, 0));
2703 }
2704}
2705
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002706void IntrinsicLocationsBuilderX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002707 // The only read barrier implementation supporting the
2708 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2709 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002710 return;
2711 }
2712
2713 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2714 if (invoke->GetLocations() != nullptr) {
2715 // Need a byte register for marking.
2716 invoke->GetLocations()->SetTempAt(1, Location::RegisterLocation(ECX));
2717
2718 static constexpr size_t kSrc = 0;
2719 static constexpr size_t kSrcPos = 1;
2720 static constexpr size_t kDest = 2;
2721 static constexpr size_t kDestPos = 3;
2722 static constexpr size_t kLength = 4;
2723
2724 if (!invoke->InputAt(kSrcPos)->IsIntConstant() &&
2725 !invoke->InputAt(kDestPos)->IsIntConstant() &&
2726 !invoke->InputAt(kLength)->IsIntConstant()) {
2727 if (!IsSameInput(invoke, kSrcPos, kDestPos) &&
2728 !IsSameInput(invoke, kSrcPos, kLength) &&
2729 !IsSameInput(invoke, kDestPos, kLength) &&
2730 !IsSameInput(invoke, kSrc, kDest)) {
2731 // Not enough registers, make the length also take a stack slot.
2732 invoke->GetLocations()->SetInAt(kLength, Location::Any());
2733 }
2734 }
2735 }
2736}
2737
2738void IntrinsicCodeGeneratorX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002739 // The only read barrier implementation supporting the
2740 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2741 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002742
2743 X86Assembler* assembler = GetAssembler();
2744 LocationSummary* locations = invoke->GetLocations();
2745
2746 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2747 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2748 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2749 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002750 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002751
2752 Register src = locations->InAt(0).AsRegister<Register>();
2753 Location src_pos = locations->InAt(1);
2754 Register dest = locations->InAt(2).AsRegister<Register>();
2755 Location dest_pos = locations->InAt(3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002756 Location length_arg = locations->InAt(4);
2757 Location length = length_arg;
2758 Location temp1_loc = locations->GetTemp(0);
2759 Register temp1 = temp1_loc.AsRegister<Register>();
2760 Location temp2_loc = locations->GetTemp(1);
2761 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002762
Vladimir Marko174b2e22017-10-12 13:34:49 +01002763 SlowPathCode* intrinsic_slow_path =
2764 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002765 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002766
2767 NearLabel conditions_on_positions_validated;
2768 SystemArrayCopyOptimizations optimizations(invoke);
2769
2770 // If source and destination are the same, we go to slow path if we need to do
2771 // forward copying.
2772 if (src_pos.IsConstant()) {
2773 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2774 if (dest_pos.IsConstant()) {
2775 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2776 if (optimizations.GetDestinationIsSource()) {
2777 // Checked when building locations.
2778 DCHECK_GE(src_pos_constant, dest_pos_constant);
2779 } else if (src_pos_constant < dest_pos_constant) {
2780 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002781 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002782 }
2783 } else {
2784 if (!optimizations.GetDestinationIsSource()) {
2785 __ cmpl(src, dest);
2786 __ j(kNotEqual, &conditions_on_positions_validated);
2787 }
2788 __ cmpl(dest_pos.AsRegister<Register>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002789 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002790 }
2791 } else {
2792 if (!optimizations.GetDestinationIsSource()) {
2793 __ cmpl(src, dest);
2794 __ j(kNotEqual, &conditions_on_positions_validated);
2795 }
2796 if (dest_pos.IsConstant()) {
2797 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2798 __ cmpl(src_pos.AsRegister<Register>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002799 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002800 } else {
2801 __ cmpl(src_pos.AsRegister<Register>(), dest_pos.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002802 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002803 }
2804 }
2805
2806 __ Bind(&conditions_on_positions_validated);
2807
2808 if (!optimizations.GetSourceIsNotNull()) {
2809 // Bail out if the source is null.
2810 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01002811 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002812 }
2813
2814 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2815 // Bail out if the destination is null.
2816 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002817 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002818 }
2819
Roland Levillain0b671c02016-08-19 12:02:34 +01002820 Location temp3_loc = locations->GetTemp(2);
2821 Register temp3 = temp3_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002822 if (length.IsStackSlot()) {
2823 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2824 length = Location::RegisterLocation(temp3);
2825 }
2826
2827 // If the length is negative, bail out.
2828 // We have already checked in the LocationsBuilder for the constant case.
2829 if (!length.IsConstant() &&
2830 !optimizations.GetCountIsSourceLength() &&
2831 !optimizations.GetCountIsDestinationLength()) {
2832 __ testl(length.AsRegister<Register>(), length.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002833 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002834 }
2835
2836 // Validity checks: source.
2837 CheckPosition(assembler,
2838 src_pos,
2839 src,
2840 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002841 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002842 temp1,
2843 optimizations.GetCountIsSourceLength());
2844
2845 // Validity checks: dest.
2846 CheckPosition(assembler,
2847 dest_pos,
2848 dest,
2849 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002850 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002851 temp1,
2852 optimizations.GetCountIsDestinationLength());
2853
2854 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2855 // Check whether all elements of the source array are assignable to the component
2856 // type of the destination array. We do two checks: the classes are the same,
2857 // or the destination is Object[]. If none of these checks succeed, we go to the
2858 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01002859
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002860 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002861 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2862 // /* HeapReference<Class> */ temp1 = src->klass_
2863 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002864 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002865 // Bail out if the source is not a non primitive array.
2866 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2867 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002868 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002869 __ testl(temp1, temp1);
2870 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2871 // If heap poisoning is enabled, `temp1` has been unpoisoned
2872 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2873 } else {
2874 // /* HeapReference<Class> */ temp1 = src->klass_
2875 __ movl(temp1, Address(src, class_offset));
2876 __ MaybeUnpoisonHeapReference(temp1);
2877 // Bail out if the source is not a non primitive array.
2878 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2879 __ movl(temp1, Address(temp1, component_offset));
2880 __ testl(temp1, temp1);
2881 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2882 __ MaybeUnpoisonHeapReference(temp1);
2883 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002884 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01002885 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002886 }
2887
Roland Levillain0b671c02016-08-19 12:02:34 +01002888 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2889 if (length.Equals(Location::RegisterLocation(temp3))) {
2890 // When Baker read barriers are enabled, register `temp3`,
2891 // which in the present case contains the `length` parameter,
2892 // will be overwritten below. Make the `length` location
2893 // reference the original stack location; it will be moved
2894 // back to `temp3` later if necessary.
2895 DCHECK(length_arg.IsStackSlot());
2896 length = length_arg;
2897 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002898
Roland Levillain0b671c02016-08-19 12:02:34 +01002899 // /* HeapReference<Class> */ temp1 = dest->klass_
2900 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002901 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002902
Roland Levillain0b671c02016-08-19 12:02:34 +01002903 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2904 // Bail out if the destination is not a non primitive array.
2905 //
2906 // Register `temp1` is not trashed by the read barrier emitted
2907 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2908 // method produces a call to a ReadBarrierMarkRegX entry point,
2909 // which saves all potentially live registers, including
2910 // temporaries such a `temp1`.
2911 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2912 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002913 invoke, temp2_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002914 __ testl(temp2, temp2);
2915 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2916 // If heap poisoning is enabled, `temp2` has been unpoisoned
2917 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2918 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
2919 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2920 }
2921
2922 // For the same reason given earlier, `temp1` is not trashed by the
2923 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2924 // /* HeapReference<Class> */ temp2 = src->klass_
2925 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002926 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002927 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2928 __ cmpl(temp1, temp2);
2929
2930 if (optimizations.GetDestinationIsTypedObjectArray()) {
2931 NearLabel do_copy;
2932 __ j(kEqual, &do_copy);
2933 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2934 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002935 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002936 // We do not need to emit a read barrier for the following
2937 // heap reference load, as `temp1` is only used in a
2938 // comparison with null below, and this reference is not
2939 // kept afterwards.
2940 __ cmpl(Address(temp1, super_offset), Immediate(0));
2941 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2942 __ Bind(&do_copy);
2943 } else {
2944 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2945 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002946 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002947 // Non read barrier code.
2948
2949 // /* HeapReference<Class> */ temp1 = dest->klass_
2950 __ movl(temp1, Address(dest, class_offset));
2951 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2952 __ MaybeUnpoisonHeapReference(temp1);
2953 // Bail out if the destination is not a non primitive array.
2954 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2955 __ movl(temp2, Address(temp1, component_offset));
2956 __ testl(temp2, temp2);
2957 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2958 __ MaybeUnpoisonHeapReference(temp2);
2959 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
2960 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2961 // Re-poison the heap reference to make the compare instruction below
2962 // compare two poisoned references.
2963 __ PoisonHeapReference(temp1);
2964 }
2965
2966 // Note: if heap poisoning is on, we are comparing two poisoned references here.
2967 __ cmpl(temp1, Address(src, class_offset));
2968
2969 if (optimizations.GetDestinationIsTypedObjectArray()) {
2970 NearLabel do_copy;
2971 __ j(kEqual, &do_copy);
2972 __ MaybeUnpoisonHeapReference(temp1);
2973 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2974 __ movl(temp1, Address(temp1, component_offset));
2975 __ MaybeUnpoisonHeapReference(temp1);
2976 __ cmpl(Address(temp1, super_offset), Immediate(0));
2977 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2978 __ Bind(&do_copy);
2979 } else {
2980 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2981 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002982 }
2983 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2984 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2985 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002986 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2987 // /* HeapReference<Class> */ temp1 = src->klass_
2988 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002989 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002990 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2991 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002992 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002993 __ testl(temp1, temp1);
2994 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2995 // If heap poisoning is enabled, `temp1` has been unpoisoned
2996 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2997 } else {
2998 // /* HeapReference<Class> */ temp1 = src->klass_
2999 __ movl(temp1, Address(src, class_offset));
3000 __ MaybeUnpoisonHeapReference(temp1);
3001 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3002 __ movl(temp1, Address(temp1, component_offset));
3003 __ testl(temp1, temp1);
3004 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3005 __ MaybeUnpoisonHeapReference(temp1);
3006 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003007 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01003008 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003009 }
3010
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003011 const DataType::Type type = DataType::Type::kReference;
3012 const int32_t element_size = DataType::Size(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003013
Roland Levillain0b671c02016-08-19 12:02:34 +01003014 // Compute the base source address in `temp1`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003015 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003016
Roland Levillain0b671c02016-08-19 12:02:34 +01003017 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3018 // If it is needed (in the case of the fast-path loop), the base
3019 // destination address is computed later, as `temp2` is used for
3020 // intermediate computations.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003021
Roland Levillain0b671c02016-08-19 12:02:34 +01003022 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003023 if (length.IsStackSlot()) {
3024 // Location `length` is again pointing at a stack slot, as
3025 // register `temp3` (which was containing the length parameter
3026 // earlier) has been overwritten; restore it now
3027 DCHECK(length.Equals(length_arg));
3028 __ movl(temp3, Address(ESP, length.GetStackIndex()));
3029 length = Location::RegisterLocation(temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01003030 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003031 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003032
Roland Levillain0b671c02016-08-19 12:02:34 +01003033 // SystemArrayCopy implementation for Baker read barriers (see
3034 // also CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier):
3035 //
3036 // if (src_ptr != end_ptr) {
3037 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
3038 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003039 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01003040 // if (is_gray) {
3041 // // Slow-path copy.
3042 // for (size_t i = 0; i != length; ++i) {
3043 // dest_array[dest_pos + i] =
3044 // MaybePoison(ReadBarrier::Mark(MaybeUnpoison(src_array[src_pos + i])));
3045 // }
3046 // } else {
3047 // // Fast-path copy.
3048 // do {
3049 // *dest_ptr++ = *src_ptr++;
3050 // } while (src_ptr != end_ptr)
3051 // }
3052 // }
3053
3054 NearLabel loop, done;
3055
3056 // Don't enter copy loop if `length == 0`.
3057 __ cmpl(temp1, temp3);
3058 __ j(kEqual, &done);
3059
Vladimir Marko953437b2016-08-24 08:30:46 +00003060 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003061 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
3062 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00003063 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
3064 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
3065 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
3066
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003067 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00003068 // goto slow_path;
3069 // At this point, just do the "if" and make sure that flags are preserved until the branch.
3070 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01003071
3072 // Load fence to prevent load-load reordering.
3073 // Note that this is a no-op, thanks to the x86 memory model.
3074 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3075
3076 // Slow path used to copy array when `src` is gray.
3077 SlowPathCode* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003078 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01003079 codegen_->AddSlowPath(read_barrier_slow_path);
3080
Vladimir Marko953437b2016-08-24 08:30:46 +00003081 // We have done the "if" of the gray bit check above, now branch based on the flags.
3082 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01003083
3084 // Fast-path copy.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003085 // Compute the base destination address in `temp2`.
3086 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01003087 // Iterate over the arrays and do a raw copy of the objects. We don't need to
3088 // poison/unpoison.
3089 __ Bind(&loop);
3090 __ pushl(Address(temp1, 0));
3091 __ cfi().AdjustCFAOffset(4);
3092 __ popl(Address(temp2, 0));
3093 __ cfi().AdjustCFAOffset(-4);
3094 __ addl(temp1, Immediate(element_size));
3095 __ addl(temp2, Immediate(element_size));
3096 __ cmpl(temp1, temp3);
3097 __ j(kNotEqual, &loop);
3098
3099 __ Bind(read_barrier_slow_path->GetExitLabel());
3100 __ Bind(&done);
3101 } else {
3102 // Non read barrier code.
Roland Levillain0b671c02016-08-19 12:02:34 +01003103 // Compute the base destination address in `temp2`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003104 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01003105 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003106 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01003107 // Iterate over the arrays and do a raw copy of the objects. We don't need to
3108 // poison/unpoison.
3109 NearLabel loop, done;
3110 __ cmpl(temp1, temp3);
3111 __ j(kEqual, &done);
3112 __ Bind(&loop);
3113 __ pushl(Address(temp1, 0));
3114 __ cfi().AdjustCFAOffset(4);
3115 __ popl(Address(temp2, 0));
3116 __ cfi().AdjustCFAOffset(-4);
3117 __ addl(temp1, Immediate(element_size));
3118 __ addl(temp2, Immediate(element_size));
3119 __ cmpl(temp1, temp3);
3120 __ j(kNotEqual, &loop);
3121 __ Bind(&done);
3122 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003123
3124 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003125 codegen_->MarkGCCard(temp1, temp2, dest, Register(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003126
Roland Levillain0b671c02016-08-19 12:02:34 +01003127 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003128}
3129
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003130void IntrinsicLocationsBuilderX86::VisitIntegerValueOf(HInvoke* invoke) {
3131 InvokeRuntimeCallingConvention calling_convention;
3132 IntrinsicVisitor::ComputeIntegerValueOfLocations(
3133 invoke,
3134 codegen_,
3135 Location::RegisterLocation(EAX),
3136 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3137}
3138
3139void IntrinsicCodeGeneratorX86::VisitIntegerValueOf(HInvoke* invoke) {
3140 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
3141 LocationSummary* locations = invoke->GetLocations();
3142 X86Assembler* assembler = GetAssembler();
3143
3144 Register out = locations->Out().AsRegister<Register>();
3145 InvokeRuntimeCallingConvention calling_convention;
3146 if (invoke->InputAt(0)->IsConstant()) {
3147 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
3148 if (value >= info.low && value <= info.high) {
3149 // Just embed the j.l.Integer in the code.
3150 ScopedObjectAccess soa(Thread::Current());
3151 mirror::Object* boxed = info.cache->Get(value + (-info.low));
3152 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
3153 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
3154 __ movl(out, Immediate(address));
3155 } else {
3156 // Allocate and initialize a new j.l.Integer.
3157 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
3158 // JIT object table.
3159 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3160 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
3161 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3162 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3163 __ movl(Address(out, info.value_offset), Immediate(value));
3164 }
3165 } else {
3166 Register in = locations->InAt(0).AsRegister<Register>();
3167 // Check bounds of our cache.
3168 __ leal(out, Address(in, -info.low));
3169 __ cmpl(out, Immediate(info.high - info.low + 1));
3170 NearLabel allocate, done;
3171 __ j(kAboveEqual, &allocate);
3172 // If the value is within the bounds, load the j.l.Integer directly from the array.
3173 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3174 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3175 __ movl(out, Address(out, TIMES_4, data_offset + address));
3176 __ MaybeUnpoisonHeapReference(out);
3177 __ jmp(&done);
3178 __ Bind(&allocate);
3179 // Otherwise allocate and initialize a new j.l.Integer.
3180 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3181 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
3182 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3183 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3184 __ movl(Address(out, info.value_offset), in);
3185 __ Bind(&done);
3186 }
3187}
3188
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003189void IntrinsicLocationsBuilderX86::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003190 LocationSummary* locations =
3191 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003192 locations->SetOut(Location::RequiresRegister());
3193}
3194
3195void IntrinsicCodeGeneratorX86::VisitThreadInterrupted(HInvoke* invoke) {
3196 X86Assembler* assembler = GetAssembler();
3197 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
3198 Address address = Address::Absolute(Thread::InterruptedOffset<kX86PointerSize>().Int32Value());
3199 NearLabel done;
3200 __ fs()->movl(out, address);
3201 __ testl(out, out);
3202 __ j(kEqual, &done);
3203 __ fs()->movl(address, Immediate(0));
3204 codegen_->MemoryFence();
3205 __ Bind(&done);
3206}
3207
3208
Aart Bik2f9fcc92016-03-01 15:16:54 -08003209UNIMPLEMENTED_INTRINSIC(X86, MathRoundDouble)
Vladimir Marko4ee8e292017-06-02 15:39:30 +00003210UNIMPLEMENTED_INTRINSIC(X86, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003211UNIMPLEMENTED_INTRINSIC(X86, FloatIsInfinite)
3212UNIMPLEMENTED_INTRINSIC(X86, DoubleIsInfinite)
3213UNIMPLEMENTED_INTRINSIC(X86, IntegerHighestOneBit)
3214UNIMPLEMENTED_INTRINSIC(X86, LongHighestOneBit)
3215UNIMPLEMENTED_INTRINSIC(X86, IntegerLowestOneBit)
3216UNIMPLEMENTED_INTRINSIC(X86, LongLowestOneBit)
Mark Mendell09ed1a32015-03-25 08:30:06 -04003217
Aart Bikff7d89c2016-11-07 08:49:28 -08003218UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOf);
3219UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003220UNIMPLEMENTED_INTRINSIC(X86, StringBufferAppend);
3221UNIMPLEMENTED_INTRINSIC(X86, StringBufferLength);
3222UNIMPLEMENTED_INTRINSIC(X86, StringBufferToString);
3223UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppend);
3224UNIMPLEMENTED_INTRINSIC(X86, StringBuilderLength);
3225UNIMPLEMENTED_INTRINSIC(X86, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003226
Aart Bik0e54c012016-03-04 12:08:31 -08003227// 1.8.
3228UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddInt)
3229UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddLong)
3230UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetInt)
3231UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetLong)
3232UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003233
Aart Bik2f9fcc92016-03-01 15:16:54 -08003234UNREACHABLE_INTRINSICS(X86)
Roland Levillain4d027112015-07-01 15:41:14 +01003235
3236#undef __
3237
Mark Mendell09ed1a32015-03-25 08:30:06 -04003238} // namespace x86
3239} // namespace art