blob: acc40bc998558ee503d244209cff1e6231ea47cc [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"
26#include "intrinsics.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070027#include "intrinsics_utils.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040028#include "mirror/array-inl.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040029#include "mirror/string.h"
30#include "thread.h"
31#include "utils/x86/assembler_x86.h"
32#include "utils/x86/constants_x86.h"
33
34namespace art {
35
36namespace x86 {
37
38static constexpr int kDoubleNaNHigh = 0x7FF80000;
39static constexpr int kDoubleNaNLow = 0x00000000;
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +000040static constexpr int kFloatNaN = 0x7FC00000;
Mark Mendell09ed1a32015-03-25 08:30:06 -040041
Mark Mendellfb8d2792015-03-31 22:16:59 -040042IntrinsicLocationsBuilderX86::IntrinsicLocationsBuilderX86(CodeGeneratorX86* codegen)
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +000043 : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -040044}
45
46
Mark Mendell09ed1a32015-03-25 08:30:06 -040047X86Assembler* IntrinsicCodeGeneratorX86::GetAssembler() {
Roland Levillainb488b782015-10-22 11:38:49 +010048 return down_cast<X86Assembler*>(codegen_->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -040049}
50
51ArenaAllocator* IntrinsicCodeGeneratorX86::GetAllocator() {
52 return codegen_->GetGraph()->GetArena();
53}
54
55bool IntrinsicLocationsBuilderX86::TryDispatch(HInvoke* invoke) {
56 Dispatch(invoke);
57 LocationSummary* res = invoke->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +000058 if (res == nullptr) {
59 return false;
60 }
61 if (kEmitCompilerReadBarrier && res->CanCall()) {
62 // Generating an intrinsic for this HInvoke may produce an
63 // IntrinsicSlowPathX86 slow path. Currently this approach
64 // does not work when using read barriers, as the emitted
65 // calling sequence will make use of another slow path
66 // (ReadBarrierForRootSlowPathX86 for HInvokeStaticOrDirect,
67 // ReadBarrierSlowPathX86 for HInvokeVirtual). So we bail
68 // out in this case.
69 //
70 // TODO: Find a way to have intrinsics work with read barriers.
71 invoke->SetLocations(nullptr);
72 return false;
73 }
74 return res->Intrinsified();
Mark Mendell09ed1a32015-03-25 08:30:06 -040075}
76
Roland Levillainec525fc2015-04-28 15:50:20 +010077static void MoveArguments(HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010078 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010079 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Mark Mendell09ed1a32015-03-25 08:30:06 -040080}
81
Andreas Gampe85b62f22015-09-09 13:15:38 -070082using IntrinsicSlowPathX86 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86>;
Mark Mendell09ed1a32015-03-25 08:30:06 -040083
Mark Mendell09ed1a32015-03-25 08:30:06 -040084#define __ assembler->
85
86static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke, bool is64bit) {
87 LocationSummary* locations = new (arena) LocationSummary(invoke,
88 LocationSummary::kNoCall,
89 kIntrinsified);
90 locations->SetInAt(0, Location::RequiresFpuRegister());
91 locations->SetOut(Location::RequiresRegister());
92 if (is64bit) {
93 locations->AddTemp(Location::RequiresFpuRegister());
94 }
95}
96
97static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke, bool is64bit) {
98 LocationSummary* locations = new (arena) LocationSummary(invoke,
99 LocationSummary::kNoCall,
100 kIntrinsified);
101 locations->SetInAt(0, Location::RequiresRegister());
102 locations->SetOut(Location::RequiresFpuRegister());
103 if (is64bit) {
104 locations->AddTemp(Location::RequiresFpuRegister());
105 locations->AddTemp(Location::RequiresFpuRegister());
106 }
107}
108
109static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
110 Location input = locations->InAt(0);
111 Location output = locations->Out();
112 if (is64bit) {
113 // Need to use the temporary.
114 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
115 __ movsd(temp, input.AsFpuRegister<XmmRegister>());
116 __ movd(output.AsRegisterPairLow<Register>(), temp);
117 __ psrlq(temp, Immediate(32));
118 __ movd(output.AsRegisterPairHigh<Register>(), temp);
119 } else {
120 __ movd(output.AsRegister<Register>(), input.AsFpuRegister<XmmRegister>());
121 }
122}
123
124static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
125 Location input = locations->InAt(0);
126 Location output = locations->Out();
127 if (is64bit) {
128 // Need to use the temporary.
129 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
130 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
131 __ movd(temp1, input.AsRegisterPairLow<Register>());
132 __ movd(temp2, input.AsRegisterPairHigh<Register>());
133 __ punpckldq(temp1, temp2);
134 __ movsd(output.AsFpuRegister<XmmRegister>(), temp1);
135 } else {
136 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<Register>());
137 }
138}
139
140void IntrinsicLocationsBuilderX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000141 CreateFPToIntLocations(arena_, invoke, /* is64bit */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400142}
143void IntrinsicLocationsBuilderX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000144 CreateIntToFPLocations(arena_, invoke, /* is64bit */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400145}
146
147void IntrinsicCodeGeneratorX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000148 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400149}
150void IntrinsicCodeGeneratorX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000151 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400152}
153
154void IntrinsicLocationsBuilderX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000155 CreateFPToIntLocations(arena_, invoke, /* is64bit */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400156}
157void IntrinsicLocationsBuilderX86::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000158 CreateIntToFPLocations(arena_, invoke, /* is64bit */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400159}
160
161void IntrinsicCodeGeneratorX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000162 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400163}
164void IntrinsicCodeGeneratorX86::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000165 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400166}
167
168static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
169 LocationSummary* locations = new (arena) LocationSummary(invoke,
170 LocationSummary::kNoCall,
171 kIntrinsified);
172 locations->SetInAt(0, Location::RequiresRegister());
173 locations->SetOut(Location::SameAsFirstInput());
174}
175
176static void CreateLongToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
177 LocationSummary* locations = new (arena) LocationSummary(invoke,
178 LocationSummary::kNoCall,
179 kIntrinsified);
180 locations->SetInAt(0, Location::RequiresRegister());
181 locations->SetOut(Location::RequiresRegister());
182}
183
184static void CreateLongToLongLocations(ArenaAllocator* arena, HInvoke* invoke) {
185 LocationSummary* locations = new (arena) LocationSummary(invoke,
186 LocationSummary::kNoCall,
187 kIntrinsified);
188 locations->SetInAt(0, Location::RequiresRegister());
189 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
190}
191
192static void GenReverseBytes(LocationSummary* locations,
193 Primitive::Type size,
194 X86Assembler* assembler) {
195 Register out = locations->Out().AsRegister<Register>();
196
197 switch (size) {
198 case Primitive::kPrimShort:
199 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
200 __ bswapl(out);
201 __ sarl(out, Immediate(16));
202 break;
203 case Primitive::kPrimInt:
204 __ bswapl(out);
205 break;
206 default:
207 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
208 UNREACHABLE();
209 }
210}
211
212void IntrinsicLocationsBuilderX86::VisitIntegerReverseBytes(HInvoke* invoke) {
213 CreateIntToIntLocations(arena_, invoke);
214}
215
216void IntrinsicCodeGeneratorX86::VisitIntegerReverseBytes(HInvoke* invoke) {
217 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
218}
219
Mark Mendell58d25fd2015-04-03 14:52:31 -0400220void IntrinsicLocationsBuilderX86::VisitLongReverseBytes(HInvoke* invoke) {
221 CreateLongToLongLocations(arena_, invoke);
222}
223
224void IntrinsicCodeGeneratorX86::VisitLongReverseBytes(HInvoke* invoke) {
225 LocationSummary* locations = invoke->GetLocations();
226 Location input = locations->InAt(0);
227 Register input_lo = input.AsRegisterPairLow<Register>();
228 Register input_hi = input.AsRegisterPairHigh<Register>();
229 Location output = locations->Out();
230 Register output_lo = output.AsRegisterPairLow<Register>();
231 Register output_hi = output.AsRegisterPairHigh<Register>();
232
233 X86Assembler* assembler = GetAssembler();
234 // Assign the inputs to the outputs, mixing low/high.
235 __ movl(output_lo, input_hi);
236 __ movl(output_hi, input_lo);
237 __ bswapl(output_lo);
238 __ bswapl(output_hi);
239}
240
Mark Mendell09ed1a32015-03-25 08:30:06 -0400241void IntrinsicLocationsBuilderX86::VisitShortReverseBytes(HInvoke* invoke) {
242 CreateIntToIntLocations(arena_, invoke);
243}
244
245void IntrinsicCodeGeneratorX86::VisitShortReverseBytes(HInvoke* invoke) {
246 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
247}
248
249
250// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
251// need is 64b.
252
253static void CreateFloatToFloat(ArenaAllocator* arena, HInvoke* invoke) {
254 // TODO: Enable memory operations when the assembler supports them.
255 LocationSummary* locations = new (arena) LocationSummary(invoke,
256 LocationSummary::kNoCall,
257 kIntrinsified);
258 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000259 // TODO: Allow x86 to work with memory. This requires assembler support, see below.
260 // locations->SetInAt(0, Location::Any()); // X86 can work on memory directly.
Mark Mendell09ed1a32015-03-25 08:30:06 -0400261 locations->SetOut(Location::SameAsFirstInput());
262}
263
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000264static void MathAbsFP(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -0400265 Location output = locations->Out();
266
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000267 if (output.IsFpuRegister()) {
Mark Mendell09ed1a32015-03-25 08:30:06 -0400268 // Create the right constant on an aligned stack.
269 if (is64bit) {
270 __ subl(ESP, Immediate(8));
271 __ pushl(Immediate(0x7FFFFFFF));
272 __ pushl(Immediate(0xFFFFFFFF));
273 __ andpd(output.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
274 } else {
275 __ subl(ESP, Immediate(12));
276 __ pushl(Immediate(0x7FFFFFFF));
277 __ andps(output.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
278 }
279 __ addl(ESP, Immediate(16));
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000280 } else {
281 // TODO: update when assember support is available.
282 UNIMPLEMENTED(FATAL) << "Needs assembler support.";
283// Once assembler support is available, in-memory operations look like this:
284// if (is64bit) {
285// DCHECK(output.IsDoubleStackSlot());
286// __ andl(Address(Register(RSP), output.GetHighStackIndex(kX86WordSize)),
287// Immediate(0x7FFFFFFF));
288// } else {
289// DCHECK(output.IsStackSlot());
290// // Can use and with a literal directly.
291// __ andl(Address(Register(RSP), output.GetStackIndex()), Immediate(0x7FFFFFFF));
292// }
Mark Mendell09ed1a32015-03-25 08:30:06 -0400293 }
294}
295
296void IntrinsicLocationsBuilderX86::VisitMathAbsDouble(HInvoke* invoke) {
297 CreateFloatToFloat(arena_, invoke);
298}
299
300void IntrinsicCodeGeneratorX86::VisitMathAbsDouble(HInvoke* invoke) {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000301 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400302}
303
304void IntrinsicLocationsBuilderX86::VisitMathAbsFloat(HInvoke* invoke) {
305 CreateFloatToFloat(arena_, invoke);
306}
307
308void IntrinsicCodeGeneratorX86::VisitMathAbsFloat(HInvoke* invoke) {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000309 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400310}
311
312static void CreateAbsIntLocation(ArenaAllocator* arena, HInvoke* invoke) {
313 LocationSummary* locations = new (arena) LocationSummary(invoke,
314 LocationSummary::kNoCall,
315 kIntrinsified);
316 locations->SetInAt(0, Location::RegisterLocation(EAX));
317 locations->SetOut(Location::SameAsFirstInput());
318 locations->AddTemp(Location::RegisterLocation(EDX));
319}
320
321static void GenAbsInteger(LocationSummary* locations, X86Assembler* assembler) {
322 Location output = locations->Out();
323 Register out = output.AsRegister<Register>();
324 DCHECK_EQ(out, EAX);
325 Register temp = locations->GetTemp(0).AsRegister<Register>();
326 DCHECK_EQ(temp, EDX);
327
328 // Sign extend EAX into EDX.
329 __ cdq();
330
331 // XOR EAX with sign.
332 __ xorl(EAX, EDX);
333
334 // Subtract out sign to correct.
335 __ subl(EAX, EDX);
336
337 // The result is in EAX.
338}
339
340static void CreateAbsLongLocation(ArenaAllocator* arena, HInvoke* invoke) {
341 LocationSummary* locations = new (arena) LocationSummary(invoke,
342 LocationSummary::kNoCall,
343 kIntrinsified);
344 locations->SetInAt(0, Location::RequiresRegister());
345 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
346 locations->AddTemp(Location::RequiresRegister());
347}
348
349static void GenAbsLong(LocationSummary* locations, X86Assembler* assembler) {
350 Location input = locations->InAt(0);
351 Register input_lo = input.AsRegisterPairLow<Register>();
352 Register input_hi = input.AsRegisterPairHigh<Register>();
353 Location output = locations->Out();
354 Register output_lo = output.AsRegisterPairLow<Register>();
355 Register output_hi = output.AsRegisterPairHigh<Register>();
356 Register temp = locations->GetTemp(0).AsRegister<Register>();
357
358 // Compute the sign into the temporary.
359 __ movl(temp, input_hi);
360 __ sarl(temp, Immediate(31));
361
362 // Store the sign into the output.
363 __ movl(output_lo, temp);
364 __ movl(output_hi, temp);
365
366 // XOR the input to the output.
367 __ xorl(output_lo, input_lo);
368 __ xorl(output_hi, input_hi);
369
370 // Subtract the sign.
371 __ subl(output_lo, temp);
372 __ sbbl(output_hi, temp);
373}
374
375void IntrinsicLocationsBuilderX86::VisitMathAbsInt(HInvoke* invoke) {
376 CreateAbsIntLocation(arena_, invoke);
377}
378
379void IntrinsicCodeGeneratorX86::VisitMathAbsInt(HInvoke* invoke) {
380 GenAbsInteger(invoke->GetLocations(), GetAssembler());
381}
382
383void IntrinsicLocationsBuilderX86::VisitMathAbsLong(HInvoke* invoke) {
384 CreateAbsLongLocation(arena_, invoke);
385}
386
387void IntrinsicCodeGeneratorX86::VisitMathAbsLong(HInvoke* invoke) {
388 GenAbsLong(invoke->GetLocations(), GetAssembler());
389}
390
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000391static void GenMinMaxFP(LocationSummary* locations, bool is_min, bool is_double,
392 X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -0400393 Location op1_loc = locations->InAt(0);
394 Location op2_loc = locations->InAt(1);
395 Location out_loc = locations->Out();
396 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
397
398 // Shortcut for same input locations.
399 if (op1_loc.Equals(op2_loc)) {
400 DCHECK(out_loc.Equals(op1_loc));
401 return;
402 }
403
404 // (out := op1)
405 // out <=? op2
406 // if Nan jmp Nan_label
407 // if out is min jmp done
408 // if op2 is min jmp op2_label
409 // handle -0/+0
410 // jmp done
411 // Nan_label:
412 // out := NaN
413 // op2_label:
414 // out := op2
415 // done:
416 //
417 // This removes one jmp, but needs to copy one input (op1) to out.
418 //
419 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
420
421 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
422
Mark Mendell0c9497d2015-08-21 09:30:05 -0400423 NearLabel nan, done, op2_label;
Mark Mendell09ed1a32015-03-25 08:30:06 -0400424 if (is_double) {
425 __ ucomisd(out, op2);
426 } else {
427 __ ucomiss(out, op2);
428 }
429
430 __ j(Condition::kParityEven, &nan);
431
432 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
433 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
434
435 // Handle 0.0/-0.0.
436 if (is_min) {
437 if (is_double) {
438 __ orpd(out, op2);
439 } else {
440 __ orps(out, op2);
441 }
442 } else {
443 if (is_double) {
444 __ andpd(out, op2);
445 } else {
446 __ andps(out, op2);
447 }
448 }
449 __ jmp(&done);
450
451 // NaN handling.
452 __ Bind(&nan);
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000453 if (is_double) {
454 __ pushl(Immediate(kDoubleNaNHigh));
455 __ pushl(Immediate(kDoubleNaNLow));
456 __ movsd(out, Address(ESP, 0));
457 __ addl(ESP, Immediate(8));
Mark Mendell09ed1a32015-03-25 08:30:06 -0400458 } else {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000459 __ pushl(Immediate(kFloatNaN));
460 __ movss(out, Address(ESP, 0));
461 __ addl(ESP, Immediate(4));
Mark Mendell09ed1a32015-03-25 08:30:06 -0400462 }
463 __ jmp(&done);
464
465 // out := op2;
466 __ Bind(&op2_label);
467 if (is_double) {
468 __ movsd(out, op2);
469 } else {
470 __ movss(out, op2);
471 }
472
473 // Done.
474 __ Bind(&done);
475}
476
477static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
478 LocationSummary* locations = new (arena) LocationSummary(invoke,
479 LocationSummary::kNoCall,
480 kIntrinsified);
481 locations->SetInAt(0, Location::RequiresFpuRegister());
482 locations->SetInAt(1, Location::RequiresFpuRegister());
483 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
484 // the second input to be the output (we can simply swap inputs).
485 locations->SetOut(Location::SameAsFirstInput());
486}
487
488void IntrinsicLocationsBuilderX86::VisitMathMinDoubleDouble(HInvoke* invoke) {
489 CreateFPFPToFPLocations(arena_, invoke);
490}
491
492void IntrinsicCodeGeneratorX86::VisitMathMinDoubleDouble(HInvoke* invoke) {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000493 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400494}
495
496void IntrinsicLocationsBuilderX86::VisitMathMinFloatFloat(HInvoke* invoke) {
497 CreateFPFPToFPLocations(arena_, invoke);
498}
499
500void IntrinsicCodeGeneratorX86::VisitMathMinFloatFloat(HInvoke* invoke) {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000501 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400502}
503
504void IntrinsicLocationsBuilderX86::VisitMathMaxDoubleDouble(HInvoke* invoke) {
505 CreateFPFPToFPLocations(arena_, invoke);
506}
507
508void IntrinsicCodeGeneratorX86::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000509 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400510}
511
512void IntrinsicLocationsBuilderX86::VisitMathMaxFloatFloat(HInvoke* invoke) {
513 CreateFPFPToFPLocations(arena_, invoke);
514}
515
516void IntrinsicCodeGeneratorX86::VisitMathMaxFloatFloat(HInvoke* invoke) {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +0000517 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400518}
519
520static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
521 X86Assembler* assembler) {
522 Location op1_loc = locations->InAt(0);
523 Location op2_loc = locations->InAt(1);
524
525 // Shortcut for same input locations.
526 if (op1_loc.Equals(op2_loc)) {
527 // Can return immediately, as op1_loc == out_loc.
528 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
529 // a copy here.
530 DCHECK(locations->Out().Equals(op1_loc));
531 return;
532 }
533
534 if (is_long) {
535 // Need to perform a subtract to get the sign right.
536 // op1 is already in the same location as the output.
537 Location output = locations->Out();
538 Register output_lo = output.AsRegisterPairLow<Register>();
539 Register output_hi = output.AsRegisterPairHigh<Register>();
540
541 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
542 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
543
544 // Spare register to compute the subtraction to set condition code.
545 Register temp = locations->GetTemp(0).AsRegister<Register>();
546
547 // Subtract off op2_low.
548 __ movl(temp, output_lo);
549 __ subl(temp, op2_lo);
550
551 // Now use the same tempo and the borrow to finish the subtraction of op2_hi.
552 __ movl(temp, output_hi);
553 __ sbbl(temp, op2_hi);
554
555 // Now the condition code is correct.
556 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
557 __ cmovl(cond, output_lo, op2_lo);
558 __ cmovl(cond, output_hi, op2_hi);
559 } else {
560 Register out = locations->Out().AsRegister<Register>();
561 Register op2 = op2_loc.AsRegister<Register>();
562
563 // (out := op1)
564 // out <=? op2
565 // if out is min jmp done
566 // out := op2
567 // done:
568
569 __ cmpl(out, op2);
570 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
571 __ cmovl(cond, out, op2);
572 }
573}
574
575static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
576 LocationSummary* locations = new (arena) LocationSummary(invoke,
577 LocationSummary::kNoCall,
578 kIntrinsified);
579 locations->SetInAt(0, Location::RequiresRegister());
580 locations->SetInAt(1, Location::RequiresRegister());
581 locations->SetOut(Location::SameAsFirstInput());
582}
583
584static void CreateLongLongToLongLocations(ArenaAllocator* arena, HInvoke* invoke) {
585 LocationSummary* locations = new (arena) LocationSummary(invoke,
586 LocationSummary::kNoCall,
587 kIntrinsified);
588 locations->SetInAt(0, Location::RequiresRegister());
589 locations->SetInAt(1, Location::RequiresRegister());
590 locations->SetOut(Location::SameAsFirstInput());
591 // Register to use to perform a long subtract to set cc.
592 locations->AddTemp(Location::RequiresRegister());
593}
594
595void IntrinsicLocationsBuilderX86::VisitMathMinIntInt(HInvoke* invoke) {
596 CreateIntIntToIntLocations(arena_, invoke);
597}
598
599void IntrinsicCodeGeneratorX86::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000600 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400601}
602
603void IntrinsicLocationsBuilderX86::VisitMathMinLongLong(HInvoke* invoke) {
604 CreateLongLongToLongLocations(arena_, invoke);
605}
606
607void IntrinsicCodeGeneratorX86::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000608 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400609}
610
611void IntrinsicLocationsBuilderX86::VisitMathMaxIntInt(HInvoke* invoke) {
612 CreateIntIntToIntLocations(arena_, invoke);
613}
614
615void IntrinsicCodeGeneratorX86::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000616 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400617}
618
619void IntrinsicLocationsBuilderX86::VisitMathMaxLongLong(HInvoke* invoke) {
620 CreateLongLongToLongLocations(arena_, invoke);
621}
622
623void IntrinsicCodeGeneratorX86::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000624 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400625}
626
627static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
628 LocationSummary* locations = new (arena) LocationSummary(invoke,
629 LocationSummary::kNoCall,
630 kIntrinsified);
631 locations->SetInAt(0, Location::RequiresFpuRegister());
632 locations->SetOut(Location::RequiresFpuRegister());
633}
634
635void IntrinsicLocationsBuilderX86::VisitMathSqrt(HInvoke* invoke) {
636 CreateFPToFPLocations(arena_, invoke);
637}
638
639void IntrinsicCodeGeneratorX86::VisitMathSqrt(HInvoke* invoke) {
640 LocationSummary* locations = invoke->GetLocations();
641 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
642 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
643
644 GetAssembler()->sqrtsd(out, in);
645}
646
Mark Mendellfb8d2792015-03-31 22:16:59 -0400647static void InvokeOutOfLineIntrinsic(CodeGeneratorX86* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100648 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400649
650 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100651 codegen->GenerateStaticOrDirectCall(invoke->AsInvokeStaticOrDirect(),
652 Location::RegisterLocation(EAX));
Mingyao Yange90db122015-04-03 17:56:54 -0700653 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400654
655 // Copy the result back to the expected output.
656 Location out = invoke->GetLocations()->Out();
657 if (out.IsValid()) {
658 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700659 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400660 }
661}
662
663static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
664 HInvoke* invoke,
665 CodeGeneratorX86* codegen) {
666 // Do we have instruction support?
667 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
668 CreateFPToFPLocations(arena, invoke);
669 return;
670 }
671
672 // We have to fall back to a call to the intrinsic.
673 LocationSummary* locations = new (arena) LocationSummary(invoke,
674 LocationSummary::kCall);
675 InvokeRuntimeCallingConvention calling_convention;
676 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
677 locations->SetOut(Location::FpuRegisterLocation(XMM0));
678 // Needs to be EAX for the invoke.
679 locations->AddTemp(Location::RegisterLocation(EAX));
680}
681
682static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86* codegen,
683 HInvoke* invoke,
684 X86Assembler* assembler,
685 int round_mode) {
686 LocationSummary* locations = invoke->GetLocations();
687 if (locations->WillCall()) {
688 InvokeOutOfLineIntrinsic(codegen, invoke);
689 } else {
690 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
691 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
692 __ roundsd(out, in, Immediate(round_mode));
693 }
694}
695
696void IntrinsicLocationsBuilderX86::VisitMathCeil(HInvoke* invoke) {
697 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
698}
699
700void IntrinsicCodeGeneratorX86::VisitMathCeil(HInvoke* invoke) {
701 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
702}
703
704void IntrinsicLocationsBuilderX86::VisitMathFloor(HInvoke* invoke) {
705 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
706}
707
708void IntrinsicCodeGeneratorX86::VisitMathFloor(HInvoke* invoke) {
709 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
710}
711
712void IntrinsicLocationsBuilderX86::VisitMathRint(HInvoke* invoke) {
713 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
714}
715
716void IntrinsicCodeGeneratorX86::VisitMathRint(HInvoke* invoke) {
717 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
718}
719
720// Note that 32 bit x86 doesn't have the capability to inline MathRoundDouble,
721// as it needs 64 bit instructions.
722void IntrinsicLocationsBuilderX86::VisitMathRoundFloat(HInvoke* invoke) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800723 // See intrinsics.h.
724 if (!kRoundIsPlusPointFive) {
725 return;
726 }
727
Mark Mendellfb8d2792015-03-31 22:16:59 -0400728 // Do we have instruction support?
729 if (codegen_->GetInstructionSetFeatures().HasSSE4_1()) {
730 LocationSummary* locations = new (arena_) LocationSummary(invoke,
731 LocationSummary::kNoCall,
732 kIntrinsified);
733 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffrayd9b92402015-04-21 10:02:22 +0100734 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400735 locations->AddTemp(Location::RequiresFpuRegister());
736 locations->AddTemp(Location::RequiresFpuRegister());
737 return;
738 }
739
740 // We have to fall back to a call to the intrinsic.
741 LocationSummary* locations = new (arena_) LocationSummary(invoke,
742 LocationSummary::kCall);
743 InvokeRuntimeCallingConvention calling_convention;
744 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
745 locations->SetOut(Location::RegisterLocation(EAX));
746 // Needs to be EAX for the invoke.
747 locations->AddTemp(Location::RegisterLocation(EAX));
748}
749
750void IntrinsicCodeGeneratorX86::VisitMathRoundFloat(HInvoke* invoke) {
751 LocationSummary* locations = invoke->GetLocations();
752 if (locations->WillCall()) {
753 InvokeOutOfLineIntrinsic(codegen_, invoke);
754 return;
755 }
756
757 // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int.
758 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
759 Register out = locations->Out().AsRegister<Register>();
760 XmmRegister maxInt = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
761 XmmRegister inPlusPointFive = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400762 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400763 X86Assembler* assembler = GetAssembler();
764
765 // Generate 0.5 into inPlusPointFive.
766 __ movl(out, Immediate(bit_cast<int32_t, float>(0.5f)));
767 __ movd(inPlusPointFive, out);
768
769 // Add in the input.
770 __ addss(inPlusPointFive, in);
771
772 // And truncate to an integer.
773 __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1));
774
775 __ movl(out, Immediate(kPrimIntMax));
776 // maxInt = int-to-float(out)
777 __ cvtsi2ss(maxInt, out);
778
779 // if inPlusPointFive >= maxInt goto done
780 __ comiss(inPlusPointFive, maxInt);
781 __ j(kAboveEqual, &done);
782
783 // if input == NaN goto nan
784 __ j(kUnordered, &nan);
785
786 // output = float-to-int-truncate(input)
787 __ cvttss2si(out, inPlusPointFive);
788 __ jmp(&done);
789 __ Bind(&nan);
790
791 // output = 0
792 __ xorl(out, out);
793 __ Bind(&done);
794}
795
Mark Mendella4f12202015-08-06 15:23:34 -0400796static void CreateFPToFPCallLocations(ArenaAllocator* arena,
797 HInvoke* invoke) {
798 LocationSummary* locations = new (arena) LocationSummary(invoke,
799 LocationSummary::kCall,
800 kIntrinsified);
801 InvokeRuntimeCallingConvention calling_convention;
802 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
803 locations->SetOut(Location::FpuRegisterLocation(XMM0));
804}
805
806static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86* codegen, QuickEntrypointEnum entry) {
807 LocationSummary* locations = invoke->GetLocations();
808 DCHECK(locations->WillCall());
809 DCHECK(invoke->IsInvokeStaticOrDirect());
810 X86Assembler* assembler = codegen->GetAssembler();
811
812 // We need some place to pass the parameters.
813 __ subl(ESP, Immediate(16));
814 __ cfi().AdjustCFAOffset(16);
815
816 // Pass the parameters at the bottom of the stack.
817 __ movsd(Address(ESP, 0), XMM0);
818
819 // If we have a second parameter, pass it next.
820 if (invoke->GetNumberOfArguments() == 2) {
821 __ movsd(Address(ESP, 8), XMM1);
822 }
823
824 // Now do the actual call.
825 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(entry)));
826
827 // Extract the return value from the FP stack.
828 __ fstpl(Address(ESP, 0));
829 __ movsd(XMM0, Address(ESP, 0));
830
831 // And clean up the stack.
832 __ addl(ESP, Immediate(16));
833 __ cfi().AdjustCFAOffset(-16);
834
835 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
836}
837
838void IntrinsicLocationsBuilderX86::VisitMathCos(HInvoke* invoke) {
839 CreateFPToFPCallLocations(arena_, invoke);
840}
841
842void IntrinsicCodeGeneratorX86::VisitMathCos(HInvoke* invoke) {
843 GenFPToFPCall(invoke, codegen_, kQuickCos);
844}
845
846void IntrinsicLocationsBuilderX86::VisitMathSin(HInvoke* invoke) {
847 CreateFPToFPCallLocations(arena_, invoke);
848}
849
850void IntrinsicCodeGeneratorX86::VisitMathSin(HInvoke* invoke) {
851 GenFPToFPCall(invoke, codegen_, kQuickSin);
852}
853
854void IntrinsicLocationsBuilderX86::VisitMathAcos(HInvoke* invoke) {
855 CreateFPToFPCallLocations(arena_, invoke);
856}
857
858void IntrinsicCodeGeneratorX86::VisitMathAcos(HInvoke* invoke) {
859 GenFPToFPCall(invoke, codegen_, kQuickAcos);
860}
861
862void IntrinsicLocationsBuilderX86::VisitMathAsin(HInvoke* invoke) {
863 CreateFPToFPCallLocations(arena_, invoke);
864}
865
866void IntrinsicCodeGeneratorX86::VisitMathAsin(HInvoke* invoke) {
867 GenFPToFPCall(invoke, codegen_, kQuickAsin);
868}
869
870void IntrinsicLocationsBuilderX86::VisitMathAtan(HInvoke* invoke) {
871 CreateFPToFPCallLocations(arena_, invoke);
872}
873
874void IntrinsicCodeGeneratorX86::VisitMathAtan(HInvoke* invoke) {
875 GenFPToFPCall(invoke, codegen_, kQuickAtan);
876}
877
878void IntrinsicLocationsBuilderX86::VisitMathCbrt(HInvoke* invoke) {
879 CreateFPToFPCallLocations(arena_, invoke);
880}
881
882void IntrinsicCodeGeneratorX86::VisitMathCbrt(HInvoke* invoke) {
883 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
884}
885
886void IntrinsicLocationsBuilderX86::VisitMathCosh(HInvoke* invoke) {
887 CreateFPToFPCallLocations(arena_, invoke);
888}
889
890void IntrinsicCodeGeneratorX86::VisitMathCosh(HInvoke* invoke) {
891 GenFPToFPCall(invoke, codegen_, kQuickCosh);
892}
893
894void IntrinsicLocationsBuilderX86::VisitMathExp(HInvoke* invoke) {
895 CreateFPToFPCallLocations(arena_, invoke);
896}
897
898void IntrinsicCodeGeneratorX86::VisitMathExp(HInvoke* invoke) {
899 GenFPToFPCall(invoke, codegen_, kQuickExp);
900}
901
902void IntrinsicLocationsBuilderX86::VisitMathExpm1(HInvoke* invoke) {
903 CreateFPToFPCallLocations(arena_, invoke);
904}
905
906void IntrinsicCodeGeneratorX86::VisitMathExpm1(HInvoke* invoke) {
907 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
908}
909
910void IntrinsicLocationsBuilderX86::VisitMathLog(HInvoke* invoke) {
911 CreateFPToFPCallLocations(arena_, invoke);
912}
913
914void IntrinsicCodeGeneratorX86::VisitMathLog(HInvoke* invoke) {
915 GenFPToFPCall(invoke, codegen_, kQuickLog);
916}
917
918void IntrinsicLocationsBuilderX86::VisitMathLog10(HInvoke* invoke) {
919 CreateFPToFPCallLocations(arena_, invoke);
920}
921
922void IntrinsicCodeGeneratorX86::VisitMathLog10(HInvoke* invoke) {
923 GenFPToFPCall(invoke, codegen_, kQuickLog10);
924}
925
926void IntrinsicLocationsBuilderX86::VisitMathSinh(HInvoke* invoke) {
927 CreateFPToFPCallLocations(arena_, invoke);
928}
929
930void IntrinsicCodeGeneratorX86::VisitMathSinh(HInvoke* invoke) {
931 GenFPToFPCall(invoke, codegen_, kQuickSinh);
932}
933
934void IntrinsicLocationsBuilderX86::VisitMathTan(HInvoke* invoke) {
935 CreateFPToFPCallLocations(arena_, invoke);
936}
937
938void IntrinsicCodeGeneratorX86::VisitMathTan(HInvoke* invoke) {
939 GenFPToFPCall(invoke, codegen_, kQuickTan);
940}
941
942void IntrinsicLocationsBuilderX86::VisitMathTanh(HInvoke* invoke) {
943 CreateFPToFPCallLocations(arena_, invoke);
944}
945
946void IntrinsicCodeGeneratorX86::VisitMathTanh(HInvoke* invoke) {
947 GenFPToFPCall(invoke, codegen_, kQuickTanh);
948}
949
950static void CreateFPFPToFPCallLocations(ArenaAllocator* arena,
951 HInvoke* invoke) {
952 LocationSummary* locations = new (arena) LocationSummary(invoke,
953 LocationSummary::kCall,
954 kIntrinsified);
955 InvokeRuntimeCallingConvention calling_convention;
956 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
957 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
958 locations->SetOut(Location::FpuRegisterLocation(XMM0));
959}
960
961void IntrinsicLocationsBuilderX86::VisitMathAtan2(HInvoke* invoke) {
962 CreateFPFPToFPCallLocations(arena_, invoke);
963}
964
965void IntrinsicCodeGeneratorX86::VisitMathAtan2(HInvoke* invoke) {
966 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
967}
968
969void IntrinsicLocationsBuilderX86::VisitMathHypot(HInvoke* invoke) {
970 CreateFPFPToFPCallLocations(arena_, invoke);
971}
972
973void IntrinsicCodeGeneratorX86::VisitMathHypot(HInvoke* invoke) {
974 GenFPToFPCall(invoke, codegen_, kQuickHypot);
975}
976
977void IntrinsicLocationsBuilderX86::VisitMathNextAfter(HInvoke* invoke) {
978 CreateFPFPToFPCallLocations(arena_, invoke);
979}
980
981void IntrinsicCodeGeneratorX86::VisitMathNextAfter(HInvoke* invoke) {
982 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
983}
984
Mark Mendell09ed1a32015-03-25 08:30:06 -0400985void IntrinsicLocationsBuilderX86::VisitStringCharAt(HInvoke* invoke) {
986 // The inputs plus one temp.
987 LocationSummary* locations = new (arena_) LocationSummary(invoke,
988 LocationSummary::kCallOnSlowPath,
989 kIntrinsified);
990 locations->SetInAt(0, Location::RequiresRegister());
991 locations->SetInAt(1, Location::RequiresRegister());
992 locations->SetOut(Location::SameAsFirstInput());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400993}
994
995void IntrinsicCodeGeneratorX86::VisitStringCharAt(HInvoke* invoke) {
996 LocationSummary* locations = invoke->GetLocations();
997
Mark Mendell6bc53a92015-07-01 14:26:52 -0400998 // Location of reference to data array.
Mark Mendell09ed1a32015-03-25 08:30:06 -0400999 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Mark Mendell6bc53a92015-07-01 14:26:52 -04001000 // Location of count.
Mark Mendell09ed1a32015-03-25 08:30:06 -04001001 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001002
1003 Register obj = locations->InAt(0).AsRegister<Register>();
1004 Register idx = locations->InAt(1).AsRegister<Register>();
1005 Register out = locations->Out().AsRegister<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001006
1007 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
1008 // the cost.
1009 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
1010 // we will not optimize the code for constants (which would save a register).
1011
Andreas Gampe85b62f22015-09-09 13:15:38 -07001012 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001013 codegen_->AddSlowPath(slow_path);
1014
1015 X86Assembler* assembler = GetAssembler();
1016
1017 __ cmpl(idx, Address(obj, count_offset));
1018 codegen_->MaybeRecordImplicitNullCheck(invoke);
1019 __ j(kAboveEqual, slow_path->GetEntryLabel());
1020
Jeff Hao848f70a2014-01-15 13:49:50 -08001021 // out = out[2*idx].
1022 __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001023
1024 __ Bind(slow_path->GetExitLabel());
1025}
1026
Mark Mendell6bc53a92015-07-01 14:26:52 -04001027void IntrinsicLocationsBuilderX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
1028 // We need at least two of the positions or length to be an integer constant,
1029 // or else we won't have enough free registers.
1030 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1031 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1032 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1033
1034 int num_constants =
1035 ((src_pos != nullptr) ? 1 : 0)
1036 + ((dest_pos != nullptr) ? 1 : 0)
1037 + ((length != nullptr) ? 1 : 0);
1038
1039 if (num_constants < 2) {
1040 // Not enough free registers.
1041 return;
1042 }
1043
1044 // As long as we are checking, we might as well check to see if the src and dest
1045 // positions are >= 0.
1046 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1047 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
1048 // We will have to fail anyways.
1049 return;
1050 }
1051
1052 // And since we are already checking, check the length too.
1053 if (length != nullptr) {
1054 int32_t len = length->GetValue();
1055 if (len < 0) {
1056 // Just call as normal.
1057 return;
1058 }
1059 }
1060
1061 // Okay, it is safe to generate inline code.
1062 LocationSummary* locations =
1063 new (arena_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
1064 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
1065 locations->SetInAt(0, Location::RequiresRegister());
1066 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1067 locations->SetInAt(2, Location::RequiresRegister());
1068 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
1069 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
1070
1071 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1072 locations->AddTemp(Location::RegisterLocation(ESI));
1073 locations->AddTemp(Location::RegisterLocation(EDI));
1074 locations->AddTemp(Location::RegisterLocation(ECX));
1075}
1076
1077static void CheckPosition(X86Assembler* assembler,
1078 Location pos,
1079 Register input,
1080 Register length,
Andreas Gampe85b62f22015-09-09 13:15:38 -07001081 SlowPathCode* slow_path,
Mark Mendell6bc53a92015-07-01 14:26:52 -04001082 Register input_len,
1083 Register temp) {
1084 // Where is the length in the String?
1085 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
1086
1087 if (pos.IsConstant()) {
1088 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
1089 if (pos_const == 0) {
1090 // Check that length(input) >= length.
1091 __ cmpl(Address(input, length_offset), length);
1092 __ j(kLess, slow_path->GetEntryLabel());
1093 } else {
1094 // Check that length(input) >= pos.
1095 __ movl(input_len, Address(input, length_offset));
1096 __ cmpl(input_len, Immediate(pos_const));
1097 __ j(kLess, slow_path->GetEntryLabel());
1098
1099 // Check that (length(input) - pos) >= length.
1100 __ leal(temp, Address(input_len, -pos_const));
1101 __ cmpl(temp, length);
1102 __ j(kLess, slow_path->GetEntryLabel());
1103 }
1104 } else {
1105 // Check that pos >= 0.
1106 Register pos_reg = pos.AsRegister<Register>();
1107 __ testl(pos_reg, pos_reg);
1108 __ j(kLess, slow_path->GetEntryLabel());
1109
1110 // Check that pos <= length(input).
1111 __ cmpl(Address(input, length_offset), pos_reg);
1112 __ j(kLess, slow_path->GetEntryLabel());
1113
1114 // Check that (length(input) - pos) >= length.
1115 __ movl(temp, Address(input, length_offset));
1116 __ subl(temp, pos_reg);
1117 __ cmpl(temp, length);
1118 __ j(kLess, slow_path->GetEntryLabel());
1119 }
1120}
1121
1122void IntrinsicCodeGeneratorX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
1123 X86Assembler* assembler = GetAssembler();
1124 LocationSummary* locations = invoke->GetLocations();
1125
1126 Register src = locations->InAt(0).AsRegister<Register>();
1127 Location srcPos = locations->InAt(1);
1128 Register dest = locations->InAt(2).AsRegister<Register>();
1129 Location destPos = locations->InAt(3);
1130 Location length = locations->InAt(4);
1131
1132 // Temporaries that we need for MOVSW.
1133 Register src_base = locations->GetTemp(0).AsRegister<Register>();
1134 DCHECK_EQ(src_base, ESI);
1135 Register dest_base = locations->GetTemp(1).AsRegister<Register>();
1136 DCHECK_EQ(dest_base, EDI);
1137 Register count = locations->GetTemp(2).AsRegister<Register>();
1138 DCHECK_EQ(count, ECX);
1139
Andreas Gampe85b62f22015-09-09 13:15:38 -07001140 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001141 codegen_->AddSlowPath(slow_path);
1142
1143 // Bail out if the source and destination are the same (to handle overlap).
1144 __ cmpl(src, dest);
1145 __ j(kEqual, slow_path->GetEntryLabel());
1146
1147 // Bail out if the source is null.
1148 __ testl(src, src);
1149 __ j(kEqual, slow_path->GetEntryLabel());
1150
1151 // Bail out if the destination is null.
1152 __ testl(dest, dest);
1153 __ j(kEqual, slow_path->GetEntryLabel());
1154
1155 // If the length is negative, bail out.
1156 // We have already checked in the LocationsBuilder for the constant case.
1157 if (!length.IsConstant()) {
1158 __ cmpl(length.AsRegister<Register>(), length.AsRegister<Register>());
1159 __ j(kLess, slow_path->GetEntryLabel());
1160 }
1161
1162 // We need the count in ECX.
1163 if (length.IsConstant()) {
1164 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1165 } else {
1166 __ movl(count, length.AsRegister<Register>());
1167 }
1168
1169 // Validity checks: source.
1170 CheckPosition(assembler, srcPos, src, count, slow_path, src_base, dest_base);
1171
1172 // Validity checks: dest.
1173 CheckPosition(assembler, destPos, dest, count, slow_path, src_base, dest_base);
1174
1175 // Okay, everything checks out. Finally time to do the copy.
1176 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1177 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1178 DCHECK_EQ(char_size, 2u);
1179
1180 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1181
1182 if (srcPos.IsConstant()) {
1183 int32_t srcPos_const = srcPos.GetConstant()->AsIntConstant()->GetValue();
1184 __ leal(src_base, Address(src, char_size * srcPos_const + data_offset));
1185 } else {
1186 __ leal(src_base, Address(src, srcPos.AsRegister<Register>(),
1187 ScaleFactor::TIMES_2, data_offset));
1188 }
1189 if (destPos.IsConstant()) {
1190 int32_t destPos_const = destPos.GetConstant()->AsIntConstant()->GetValue();
1191
1192 __ leal(dest_base, Address(dest, char_size * destPos_const + data_offset));
1193 } else {
1194 __ leal(dest_base, Address(dest, destPos.AsRegister<Register>(),
1195 ScaleFactor::TIMES_2, data_offset));
1196 }
1197
1198 // Do the move.
1199 __ rep_movsw();
1200
1201 __ Bind(slow_path->GetExitLabel());
1202}
1203
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001204void IntrinsicLocationsBuilderX86::VisitStringCompareTo(HInvoke* invoke) {
1205 // The inputs plus one temp.
1206 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1207 LocationSummary::kCall,
1208 kIntrinsified);
1209 InvokeRuntimeCallingConvention calling_convention;
1210 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1211 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1212 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001213}
1214
1215void IntrinsicCodeGeneratorX86::VisitStringCompareTo(HInvoke* invoke) {
1216 X86Assembler* assembler = GetAssembler();
1217 LocationSummary* locations = invoke->GetLocations();
1218
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001219 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001220 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001221
1222 Register argument = locations->InAt(1).AsRegister<Register>();
1223 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001224 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001225 codegen_->AddSlowPath(slow_path);
1226 __ j(kEqual, slow_path->GetEntryLabel());
1227
1228 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pStringCompareTo)));
1229 __ Bind(slow_path->GetExitLabel());
1230}
1231
Agi Csakid7138c82015-08-13 17:46:44 -07001232void IntrinsicLocationsBuilderX86::VisitStringEquals(HInvoke* invoke) {
1233 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1234 LocationSummary::kNoCall,
1235 kIntrinsified);
1236 locations->SetInAt(0, Location::RequiresRegister());
1237 locations->SetInAt(1, Location::RequiresRegister());
1238
1239 // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction.
1240 locations->AddTemp(Location::RegisterLocation(ECX));
1241 locations->AddTemp(Location::RegisterLocation(EDI));
1242
1243 // Set output, ESI needed for repe_cmpsl instruction anyways.
1244 locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap);
1245}
1246
1247void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) {
1248 X86Assembler* assembler = GetAssembler();
1249 LocationSummary* locations = invoke->GetLocations();
1250
1251 Register str = locations->InAt(0).AsRegister<Register>();
1252 Register arg = locations->InAt(1).AsRegister<Register>();
1253 Register ecx = locations->GetTemp(0).AsRegister<Register>();
1254 Register edi = locations->GetTemp(1).AsRegister<Register>();
1255 Register esi = locations->Out().AsRegister<Register>();
1256
Mark Mendell0c9497d2015-08-21 09:30:05 -04001257 NearLabel end, return_true, return_false;
Agi Csakid7138c82015-08-13 17:46:44 -07001258
1259 // Get offsets of count, value, and class fields within a string object.
1260 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1261 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1262 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1263
1264 // Note that the null check must have been done earlier.
1265 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1266
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001267 StringEqualsOptimizations optimizations(invoke);
1268 if (!optimizations.GetArgumentNotNull()) {
1269 // Check if input is null, return false if it is.
1270 __ testl(arg, arg);
1271 __ j(kEqual, &return_false);
1272 }
Agi Csakid7138c82015-08-13 17:46:44 -07001273
1274 // Instanceof check for the argument by comparing class fields.
1275 // All string objects must have the same type since String cannot be subclassed.
1276 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1277 // 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 +01001278 if (!optimizations.GetArgumentIsString()) {
1279 __ movl(ecx, Address(str, class_offset));
1280 __ cmpl(ecx, Address(arg, class_offset));
1281 __ j(kNotEqual, &return_false);
1282 }
Agi Csakid7138c82015-08-13 17:46:44 -07001283
1284 // Reference equality check, return true if same reference.
1285 __ cmpl(str, arg);
1286 __ j(kEqual, &return_true);
1287
1288 // Load length of receiver string.
1289 __ movl(ecx, Address(str, count_offset));
1290 // Check if lengths are equal, return false if they're not.
1291 __ cmpl(ecx, Address(arg, count_offset));
1292 __ j(kNotEqual, &return_false);
1293 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001294 __ jecxz(&return_true);
Agi Csakid7138c82015-08-13 17:46:44 -07001295
1296 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1297 __ leal(esi, Address(str, value_offset));
1298 __ leal(edi, Address(arg, value_offset));
1299
1300 // Divide string length by 2 to compare characters 2 at a time and adjust for odd lengths.
1301 __ addl(ecx, Immediate(1));
1302 __ shrl(ecx, Immediate(1));
1303
1304 // Assertions that must hold in order to compare strings 2 characters at a time.
1305 DCHECK_ALIGNED(value_offset, 4);
1306 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1307
1308 // Loop to compare strings two characters at a time starting at the beginning of the string.
1309 __ repe_cmpsl();
1310 // If strings are not equal, zero flag will be cleared.
1311 __ j(kNotEqual, &return_false);
1312
1313 // Return true and exit the function.
1314 // If loop does not result in returning false, we return true.
1315 __ Bind(&return_true);
1316 __ movl(esi, Immediate(1));
1317 __ jmp(&end);
1318
1319 // Return false and exit the function.
1320 __ Bind(&return_false);
1321 __ xorl(esi, esi);
1322 __ Bind(&end);
1323}
1324
Andreas Gampe21030dd2015-05-07 14:46:15 -07001325static void CreateStringIndexOfLocations(HInvoke* invoke,
1326 ArenaAllocator* allocator,
1327 bool start_at_zero) {
1328 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1329 LocationSummary::kCallOnSlowPath,
1330 kIntrinsified);
1331 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1332 locations->SetInAt(0, Location::RegisterLocation(EDI));
1333 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1334 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1335 // of the instruction explicitly.
1336 // Note: This works as we don't clobber EAX anywhere.
1337 locations->SetInAt(1, Location::RegisterLocation(EAX));
1338 if (!start_at_zero) {
1339 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1340 }
1341 // As we clobber EDI during execution anyways, also use it as the output.
1342 locations->SetOut(Location::SameAsFirstInput());
1343
1344 // repne scasw uses ECX as the counter.
1345 locations->AddTemp(Location::RegisterLocation(ECX));
1346 // Need another temporary to be able to compute the result.
1347 locations->AddTemp(Location::RequiresRegister());
1348}
1349
1350static void GenerateStringIndexOf(HInvoke* invoke,
1351 X86Assembler* assembler,
1352 CodeGeneratorX86* codegen,
1353 ArenaAllocator* allocator,
1354 bool start_at_zero) {
1355 LocationSummary* locations = invoke->GetLocations();
1356
1357 // Note that the null check must have been done earlier.
1358 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1359
1360 Register string_obj = locations->InAt(0).AsRegister<Register>();
1361 Register search_value = locations->InAt(1).AsRegister<Register>();
1362 Register counter = locations->GetTemp(0).AsRegister<Register>();
1363 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1364 Register out = locations->Out().AsRegister<Register>();
1365
1366 // Check our assumptions for registers.
1367 DCHECK_EQ(string_obj, EDI);
1368 DCHECK_EQ(search_value, EAX);
1369 DCHECK_EQ(counter, ECX);
1370 DCHECK_EQ(out, EDI);
1371
1372 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1373 // or directly dispatch if we have a constant.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001374 SlowPathCode* slow_path = nullptr;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001375 if (invoke->InputAt(1)->IsIntConstant()) {
1376 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1377 std::numeric_limits<uint16_t>::max()) {
1378 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1379 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1380 slow_path = new (allocator) IntrinsicSlowPathX86(invoke);
1381 codegen->AddSlowPath(slow_path);
1382 __ jmp(slow_path->GetEntryLabel());
1383 __ Bind(slow_path->GetExitLabel());
1384 return;
1385 }
1386 } else {
1387 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1388 slow_path = new (allocator) IntrinsicSlowPathX86(invoke);
1389 codegen->AddSlowPath(slow_path);
1390 __ j(kAbove, slow_path->GetEntryLabel());
1391 }
1392
1393 // From here down, we know that we are looking for a char that fits in 16 bits.
1394 // Location of reference to data array within the String object.
1395 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1396 // Location of count within the String object.
1397 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1398
1399 // Load string length, i.e., the count field of the string.
1400 __ movl(string_length, Address(string_obj, count_offset));
1401
1402 // Do a zero-length check.
1403 // 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
1408 if (start_at_zero) {
1409 // Number of chars to scan is the same as the string length.
1410 __ movl(counter, string_length);
1411
1412 // Move to the start of the string.
1413 __ addl(string_obj, Immediate(value_offset));
1414 } else {
1415 Register start_index = locations->InAt(2).AsRegister<Register>();
1416
1417 // Do a start_index check.
1418 __ cmpl(start_index, string_length);
1419 __ j(kGreaterEqual, &not_found_label);
1420
1421 // Ensure we have a start index >= 0;
1422 __ xorl(counter, counter);
1423 __ cmpl(start_index, Immediate(0));
1424 __ cmovl(kGreater, counter, start_index);
1425
1426 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1427 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1428
1429 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1430 // compare.
1431 __ negl(counter);
1432 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1433 }
1434
1435 // Everything is set up for repne scasw:
1436 // * Comparison address in EDI.
1437 // * Counter in ECX.
1438 __ repne_scasw();
1439
1440 // Did we find a match?
1441 __ j(kNotEqual, &not_found_label);
1442
1443 // Yes, we matched. Compute the index of the result.
1444 __ subl(string_length, counter);
1445 __ leal(out, Address(string_length, -1));
1446
Mark Mendell0c9497d2015-08-21 09:30:05 -04001447 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001448 __ jmp(&done);
1449
1450 // Failed to match; return -1.
1451 __ Bind(&not_found_label);
1452 __ movl(out, Immediate(-1));
1453
1454 // And join up at the end.
1455 __ Bind(&done);
1456 if (slow_path != nullptr) {
1457 __ Bind(slow_path->GetExitLabel());
1458 }
1459}
1460
1461void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001462 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001463}
1464
1465void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001466 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001467}
1468
1469void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001470 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001471}
1472
1473void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001474 GenerateStringIndexOf(
1475 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001476}
1477
Jeff Hao848f70a2014-01-15 13:49:50 -08001478void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1479 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1480 LocationSummary::kCall,
1481 kIntrinsified);
1482 InvokeRuntimeCallingConvention calling_convention;
1483 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1484 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1485 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1486 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1487 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001488}
1489
1490void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1491 X86Assembler* assembler = GetAssembler();
1492 LocationSummary* locations = invoke->GetLocations();
1493
1494 Register byte_array = locations->InAt(0).AsRegister<Register>();
1495 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001496 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001497 codegen_->AddSlowPath(slow_path);
1498 __ j(kEqual, slow_path->GetEntryLabel());
1499
1500 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocStringFromBytes)));
1501 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1502 __ Bind(slow_path->GetExitLabel());
1503}
1504
1505void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
1506 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1507 LocationSummary::kCall,
1508 kIntrinsified);
1509 InvokeRuntimeCallingConvention calling_convention;
1510 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1511 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1512 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1513 locations->SetOut(Location::RegisterLocation(EAX));
1514}
1515
1516void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
1517 X86Assembler* assembler = GetAssembler();
1518
1519 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocStringFromChars)));
1520 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1521}
1522
1523void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
1524 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1525 LocationSummary::kCall,
1526 kIntrinsified);
1527 InvokeRuntimeCallingConvention calling_convention;
1528 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1529 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001530}
1531
1532void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1533 X86Assembler* assembler = GetAssembler();
1534 LocationSummary* locations = invoke->GetLocations();
1535
1536 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1537 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001538 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001539 codegen_->AddSlowPath(slow_path);
1540 __ j(kEqual, slow_path->GetEntryLabel());
1541
1542 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocStringFromString)));
1543 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1544 __ Bind(slow_path->GetExitLabel());
1545}
1546
Mark Mendell8f8926a2015-08-17 11:39:06 -04001547void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1548 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1549 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1550 LocationSummary::kNoCall,
1551 kIntrinsified);
1552 locations->SetInAt(0, Location::RequiresRegister());
1553 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1554 // Place srcEnd in ECX to save a move below.
1555 locations->SetInAt(2, Location::RegisterLocation(ECX));
1556 locations->SetInAt(3, Location::RequiresRegister());
1557 locations->SetInAt(4, Location::RequiresRegister());
1558
1559 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1560 // We don't have enough registers to also grab ECX, so handle below.
1561 locations->AddTemp(Location::RegisterLocation(ESI));
1562 locations->AddTemp(Location::RegisterLocation(EDI));
1563}
1564
1565void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1566 X86Assembler* assembler = GetAssembler();
1567 LocationSummary* locations = invoke->GetLocations();
1568
1569 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1570 // Location of data in char array buffer.
1571 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1572 // Location of char array data in string.
1573 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1574
1575 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1576 Register obj = locations->InAt(0).AsRegister<Register>();
1577 Location srcBegin = locations->InAt(1);
1578 int srcBegin_value =
1579 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1580 Register srcEnd = locations->InAt(2).AsRegister<Register>();
1581 Register dst = locations->InAt(3).AsRegister<Register>();
1582 Register dstBegin = locations->InAt(4).AsRegister<Register>();
1583
1584 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1585 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1586 DCHECK_EQ(char_size, 2u);
1587
1588 // Compute the address of the destination buffer.
1589 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1590
1591 // Compute the address of the source string.
1592 if (srcBegin.IsConstant()) {
1593 // Compute the address of the source string by adding the number of chars from
1594 // the source beginning to the value offset of a string.
1595 __ leal(ESI, Address(obj, srcBegin_value * char_size + value_offset));
1596 } else {
1597 __ leal(ESI, Address(obj, srcBegin.AsRegister<Register>(),
1598 ScaleFactor::TIMES_2, value_offset));
1599 }
1600
1601 // Compute the number of chars (words) to move.
1602 // Now is the time to save ECX, since we don't know if it will be used later.
1603 __ pushl(ECX);
1604 int stack_adjust = kX86WordSize;
1605 __ cfi().AdjustCFAOffset(stack_adjust);
1606 DCHECK_EQ(srcEnd, ECX);
1607 if (srcBegin.IsConstant()) {
1608 if (srcBegin_value != 0) {
1609 __ subl(ECX, Immediate(srcBegin_value));
1610 }
1611 } else {
1612 DCHECK(srcBegin.IsRegister());
1613 __ subl(ECX, srcBegin.AsRegister<Register>());
1614 }
1615
1616 // Do the move.
1617 __ rep_movsw();
1618
1619 // And restore ECX.
1620 __ popl(ECX);
1621 __ cfi().AdjustCFAOffset(-stack_adjust);
1622}
1623
Mark Mendell09ed1a32015-03-25 08:30:06 -04001624static void GenPeek(LocationSummary* locations, Primitive::Type size, X86Assembler* assembler) {
1625 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1626 Location out_loc = locations->Out();
1627 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1628 // to avoid a SIGBUS.
1629 switch (size) {
1630 case Primitive::kPrimByte:
1631 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1632 break;
1633 case Primitive::kPrimShort:
1634 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1635 break;
1636 case Primitive::kPrimInt:
1637 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1638 break;
1639 case Primitive::kPrimLong:
1640 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1641 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1642 break;
1643 default:
1644 LOG(FATAL) << "Type not recognized for peek: " << size;
1645 UNREACHABLE();
1646 }
1647}
1648
1649void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
1650 CreateLongToIntLocations(arena_, invoke);
1651}
1652
1653void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
1654 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1655}
1656
1657void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
1658 CreateLongToIntLocations(arena_, invoke);
1659}
1660
1661void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
1662 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1663}
1664
1665void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
1666 CreateLongToLongLocations(arena_, invoke);
1667}
1668
1669void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
1670 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1671}
1672
1673void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
1674 CreateLongToIntLocations(arena_, invoke);
1675}
1676
1677void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
1678 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1679}
1680
1681static void CreateLongIntToVoidLocations(ArenaAllocator* arena, Primitive::Type size,
1682 HInvoke* invoke) {
1683 LocationSummary* locations = new (arena) LocationSummary(invoke,
1684 LocationSummary::kNoCall,
1685 kIntrinsified);
1686 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001687 HInstruction* value = invoke->InputAt(1);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001688 if (size == Primitive::kPrimByte) {
1689 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1690 } else {
1691 locations->SetInAt(1, Location::RegisterOrConstant(value));
1692 }
1693}
1694
1695static void GenPoke(LocationSummary* locations, Primitive::Type size, X86Assembler* assembler) {
1696 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1697 Location value_loc = locations->InAt(1);
1698 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1699 // to avoid a SIGBUS.
1700 switch (size) {
1701 case Primitive::kPrimByte:
1702 if (value_loc.IsConstant()) {
1703 __ movb(Address(address, 0),
1704 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1705 } else {
1706 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1707 }
1708 break;
1709 case Primitive::kPrimShort:
1710 if (value_loc.IsConstant()) {
1711 __ movw(Address(address, 0),
1712 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1713 } else {
1714 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1715 }
1716 break;
1717 case Primitive::kPrimInt:
1718 if (value_loc.IsConstant()) {
1719 __ movl(Address(address, 0),
1720 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1721 } else {
1722 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1723 }
1724 break;
1725 case Primitive::kPrimLong:
1726 if (value_loc.IsConstant()) {
1727 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1728 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1729 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1730 } else {
1731 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1732 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1733 }
1734 break;
1735 default:
1736 LOG(FATAL) << "Type not recognized for poke: " << size;
1737 UNREACHABLE();
1738 }
1739}
1740
1741void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
1742 CreateLongIntToVoidLocations(arena_, Primitive::kPrimByte, invoke);
1743}
1744
1745void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
1746 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1747}
1748
1749void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
1750 CreateLongIntToVoidLocations(arena_, Primitive::kPrimInt, invoke);
1751}
1752
1753void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
1754 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1755}
1756
1757void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
1758 CreateLongIntToVoidLocations(arena_, Primitive::kPrimLong, invoke);
1759}
1760
1761void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
1762 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1763}
1764
1765void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
1766 CreateLongIntToVoidLocations(arena_, Primitive::kPrimShort, invoke);
1767}
1768
1769void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
1770 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1771}
1772
1773void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
1774 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1775 LocationSummary::kNoCall,
1776 kIntrinsified);
1777 locations->SetOut(Location::RequiresRegister());
1778}
1779
1780void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1781 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1782 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86WordSize>()));
1783}
1784
Roland Levillain0d5a2812015-11-13 10:07:31 +00001785static void GenUnsafeGet(HInvoke* invoke,
1786 Primitive::Type type,
1787 bool is_volatile,
1788 CodeGeneratorX86* codegen) {
1789 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
1790 LocationSummary* locations = invoke->GetLocations();
1791 Location base_loc = locations->InAt(1);
1792 Register base = base_loc.AsRegister<Register>();
1793 Location offset_loc = locations->InAt(2);
1794 Register offset = offset_loc.AsRegisterPairLow<Register>();
1795 Location output_loc = locations->Out();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001796
1797 switch (type) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001798 case Primitive::kPrimInt: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001799 Register output = output_loc.AsRegister<Register>();
1800 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain7c1559a2015-12-15 10:55:36 +00001801 break;
1802 }
1803
1804 case Primitive::kPrimNot: {
1805 Register output = output_loc.AsRegister<Register>();
1806 if (kEmitCompilerReadBarrier) {
1807 if (kUseBakerReadBarrier) {
1808 Location temp = locations->GetTemp(0);
1809 codegen->GenerateArrayLoadWithBakerReadBarrier(
1810 invoke, output_loc, base, 0U, offset_loc, temp, /* needs_null_check */ false);
1811 } else {
1812 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1813 codegen->GenerateReadBarrierSlow(
1814 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1815 }
1816 } else {
1817 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1818 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001819 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001820 break;
Roland Levillain4d027112015-07-01 15:41:14 +01001821 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001822
1823 case Primitive::kPrimLong: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001824 Register output_lo = output_loc.AsRegisterPairLow<Register>();
1825 Register output_hi = output_loc.AsRegisterPairHigh<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001826 if (is_volatile) {
1827 // Need to use a XMM to read atomically.
1828 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1829 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
1830 __ movd(output_lo, temp);
1831 __ psrlq(temp, Immediate(32));
1832 __ movd(output_hi, temp);
1833 } else {
1834 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
1835 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
1836 }
1837 }
1838 break;
1839
1840 default:
1841 LOG(FATAL) << "Unsupported op size " << type;
1842 UNREACHABLE();
1843 }
1844}
1845
Roland Levillain7c1559a2015-12-15 10:55:36 +00001846static void CreateIntIntIntToIntLocations(ArenaAllocator* arena,
1847 HInvoke* invoke,
1848 Primitive::Type type,
1849 bool is_volatile) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001850 bool can_call = kEmitCompilerReadBarrier &&
1851 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1852 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001853 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001854 can_call ?
1855 LocationSummary::kCallOnSlowPath :
1856 LocationSummary::kNoCall,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001857 kIntrinsified);
1858 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1859 locations->SetInAt(1, Location::RequiresRegister());
1860 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00001861 if (type == Primitive::kPrimLong) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001862 if (is_volatile) {
1863 // Need to use XMM to read volatile.
1864 locations->AddTemp(Location::RequiresFpuRegister());
1865 locations->SetOut(Location::RequiresRegister());
1866 } else {
1867 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1868 }
1869 } else {
1870 locations->SetOut(Location::RequiresRegister());
1871 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00001872 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1873 // We need a temporary register for the read barrier marking slow
1874 // path in InstructionCodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
1875 locations->AddTemp(Location::RequiresRegister());
1876 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001877}
1878
1879void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001880 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001881}
1882void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001883 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001884}
1885void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001886 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001887}
1888void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001889 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001890}
1891void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001892 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001893}
1894void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001895 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001896}
1897
1898
1899void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001900 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001901}
1902void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001903 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001904}
1905void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001906 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001907}
1908void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001909 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001910}
1911void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001912 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001913}
1914void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001915 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001916}
1917
1918
1919static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1920 Primitive::Type type,
1921 HInvoke* invoke,
1922 bool is_volatile) {
1923 LocationSummary* locations = new (arena) LocationSummary(invoke,
1924 LocationSummary::kNoCall,
1925 kIntrinsified);
1926 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1927 locations->SetInAt(1, Location::RequiresRegister());
1928 locations->SetInAt(2, Location::RequiresRegister());
1929 locations->SetInAt(3, Location::RequiresRegister());
1930 if (type == Primitive::kPrimNot) {
1931 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001932 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04001933 // Ensure the value is in a byte register.
1934 locations->AddTemp(Location::RegisterLocation(ECX));
1935 } else if (type == Primitive::kPrimLong && is_volatile) {
1936 locations->AddTemp(Location::RequiresFpuRegister());
1937 locations->AddTemp(Location::RequiresFpuRegister());
1938 }
1939}
1940
1941void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001942 CreateIntIntIntIntToVoidPlusTempsLocations(
1943 arena_, Primitive::kPrimInt, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001944}
1945void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001946 CreateIntIntIntIntToVoidPlusTempsLocations(
1947 arena_, Primitive::kPrimInt, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001948}
1949void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001950 CreateIntIntIntIntToVoidPlusTempsLocations(
1951 arena_, Primitive::kPrimInt, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001952}
1953void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001954 CreateIntIntIntIntToVoidPlusTempsLocations(
1955 arena_, Primitive::kPrimNot, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001956}
1957void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001958 CreateIntIntIntIntToVoidPlusTempsLocations(
1959 arena_, Primitive::kPrimNot, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001960}
1961void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001962 CreateIntIntIntIntToVoidPlusTempsLocations(
1963 arena_, Primitive::kPrimNot, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001964}
1965void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001966 CreateIntIntIntIntToVoidPlusTempsLocations(
1967 arena_, Primitive::kPrimLong, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001968}
1969void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001970 CreateIntIntIntIntToVoidPlusTempsLocations(
1971 arena_, Primitive::kPrimLong, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001972}
1973void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001974 CreateIntIntIntIntToVoidPlusTempsLocations(
1975 arena_, Primitive::kPrimLong, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001976}
1977
1978// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1979// memory model.
1980static void GenUnsafePut(LocationSummary* locations,
1981 Primitive::Type type,
1982 bool is_volatile,
1983 CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001984 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001985 Register base = locations->InAt(1).AsRegister<Register>();
1986 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1987 Location value_loc = locations->InAt(3);
1988
1989 if (type == Primitive::kPrimLong) {
1990 Register value_lo = value_loc.AsRegisterPairLow<Register>();
1991 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
1992 if (is_volatile) {
1993 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1994 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1995 __ movd(temp1, value_lo);
1996 __ movd(temp2, value_hi);
1997 __ punpckldq(temp1, temp2);
1998 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
1999 } else {
2000 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
2001 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
2002 }
Roland Levillain4d027112015-07-01 15:41:14 +01002003 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2004 Register temp = locations->GetTemp(0).AsRegister<Register>();
2005 __ movl(temp, value_loc.AsRegister<Register>());
2006 __ PoisonHeapReference(temp);
2007 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002008 } else {
2009 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
2010 }
2011
2012 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002013 codegen->MemoryFence();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002014 }
2015
2016 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002017 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04002018 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
2019 locations->GetTemp(1).AsRegister<Register>(),
2020 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002021 value_loc.AsRegister<Register>(),
2022 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002023 }
2024}
2025
2026void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002027 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002028}
2029void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002030 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002031}
2032void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002033 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002034}
2035void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002036 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002037}
2038void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002039 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002040}
2041void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002042 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002043}
2044void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002045 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002046}
2047void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002048 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002049}
2050void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002051 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002052}
2053
Mark Mendell58d25fd2015-04-03 14:52:31 -04002054static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
2055 HInvoke* invoke) {
2056 LocationSummary* locations = new (arena) LocationSummary(invoke,
2057 LocationSummary::kNoCall,
2058 kIntrinsified);
2059 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2060 locations->SetInAt(1, Location::RequiresRegister());
2061 // Offset is a long, but in 32 bit mode, we only need the low word.
2062 // Can we update the invoke here to remove a TypeConvert to Long?
2063 locations->SetInAt(2, Location::RequiresRegister());
2064 // Expected value must be in EAX or EDX:EAX.
2065 // For long, new value must be in ECX:EBX.
2066 if (type == Primitive::kPrimLong) {
2067 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
2068 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
2069 } else {
2070 locations->SetInAt(3, Location::RegisterLocation(EAX));
2071 locations->SetInAt(4, Location::RequiresRegister());
2072 }
2073
2074 // Force a byte register for the output.
2075 locations->SetOut(Location::RegisterLocation(EAX));
2076 if (type == Primitive::kPrimNot) {
2077 // Need temp registers for card-marking.
Roland Levillainb488b782015-10-22 11:38:49 +01002078 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002079 // Need a byte register for marking.
2080 locations->AddTemp(Location::RegisterLocation(ECX));
2081 }
2082}
2083
2084void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
2085 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
2086}
2087
2088void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
2089 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
2090}
2091
2092void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00002093 // The UnsafeCASObject intrinsic is missing a read barrier, and
2094 // therefore sometimes does not work as expected (b/25883050).
2095 // Turn it off temporarily as a quick fix, until the read barrier is
2096 // implemented.
2097 //
2098 // TODO(rpl): Implement a read barrier in GenCAS below and re-enable
2099 // this intrinsic.
2100 if (kEmitCompilerReadBarrier) {
2101 return;
2102 }
2103
Mark Mendell58d25fd2015-04-03 14:52:31 -04002104 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
2105}
2106
2107static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002108 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002109 LocationSummary* locations = invoke->GetLocations();
2110
2111 Register base = locations->InAt(1).AsRegister<Register>();
2112 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2113 Location out = locations->Out();
2114 DCHECK_EQ(out.AsRegister<Register>(), EAX);
2115
Roland Levillainb488b782015-10-22 11:38:49 +01002116 if (type == Primitive::kPrimNot) {
Roland Levillain4d027112015-07-01 15:41:14 +01002117 Register expected = locations->InAt(3).AsRegister<Register>();
Roland Levillainb488b782015-10-22 11:38:49 +01002118 // Ensure `expected` is in EAX (required by the CMPXCHG instruction).
Roland Levillain4d027112015-07-01 15:41:14 +01002119 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002120 Register value = locations->InAt(4).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01002121
Roland Levillainb488b782015-10-22 11:38:49 +01002122 // Mark card for object assuming new value is stored.
2123 bool value_can_be_null = true; // TODO: Worth finding out this information?
2124 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
2125 locations->GetTemp(1).AsRegister<Register>(),
2126 base,
2127 value,
2128 value_can_be_null);
2129
2130 bool base_equals_value = (base == value);
2131 if (kPoisonHeapReferences) {
2132 if (base_equals_value) {
2133 // If `base` and `value` are the same register location, move
2134 // `value` to a temporary register. This way, poisoning
2135 // `value` won't invalidate `base`.
2136 value = locations->GetTemp(0).AsRegister<Register>();
2137 __ movl(value, base);
Roland Levillain4d027112015-07-01 15:41:14 +01002138 }
Roland Levillainb488b782015-10-22 11:38:49 +01002139
2140 // Check that the register allocator did not assign the location
2141 // of `expected` (EAX) to `value` nor to `base`, so that heap
2142 // poisoning (when enabled) works as intended below.
2143 // - If `value` were equal to `expected`, both references would
2144 // be poisoned twice, meaning they would not be poisoned at
2145 // all, as heap poisoning uses address negation.
2146 // - If `base` were equal to `expected`, poisoning `expected`
2147 // would invalidate `base`.
2148 DCHECK_NE(value, expected);
2149 DCHECK_NE(base, expected);
2150
2151 __ PoisonHeapReference(expected);
2152 __ PoisonHeapReference(value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002153 }
2154
Roland Levillain391b8662015-12-18 11:43:38 +00002155 // TODO: Add a read barrier for the reference stored in the object
2156 // before attempting the CAS, similar to the one in the
2157 // art::Unsafe_compareAndSwapObject JNI implementation.
2158 //
2159 // Note that this code is not (yet) used when read barriers are
2160 // enabled (see IntrinsicLocationsBuilderX86::VisitUnsafeCASObject).
2161 DCHECK(!kEmitCompilerReadBarrier);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002162 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002163
Roland Levillain0d5a2812015-11-13 10:07:31 +00002164 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002165 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002166
Roland Levillainb488b782015-10-22 11:38:49 +01002167 // Convert ZF into the boolean result.
2168 __ setb(kZero, out.AsRegister<Register>());
2169 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002170
Roland Levillain391b8662015-12-18 11:43:38 +00002171 // If heap poisoning is enabled, we need to unpoison the values
2172 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002173 if (kPoisonHeapReferences) {
2174 if (base_equals_value) {
2175 // `value` has been moved to a temporary register, no need to
2176 // unpoison it.
2177 } else {
2178 // Ensure `value` is different from `out`, so that unpoisoning
2179 // the former does not invalidate the latter.
2180 DCHECK_NE(value, out.AsRegister<Register>());
2181 __ UnpoisonHeapReference(value);
2182 }
2183 // Do not unpoison the reference contained in register
2184 // `expected`, as it is the same as register `out` (EAX).
2185 }
2186 } else {
2187 if (type == Primitive::kPrimInt) {
2188 // Ensure the expected value is in EAX (required by the CMPXCHG
2189 // instruction).
2190 DCHECK_EQ(locations->InAt(3).AsRegister<Register>(), EAX);
2191 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0),
2192 locations->InAt(4).AsRegister<Register>());
2193 } else if (type == Primitive::kPrimLong) {
2194 // Ensure the expected value is in EAX:EDX and that the new
2195 // value is in EBX:ECX (required by the CMPXCHG8B instruction).
2196 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
2197 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
2198 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
2199 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
2200 __ LockCmpxchg8b(Address(base, offset, TIMES_1, 0));
2201 } else {
2202 LOG(FATAL) << "Unexpected CAS type " << type;
2203 }
2204
Roland Levillain0d5a2812015-11-13 10:07:31 +00002205 // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we
2206 // don't need scheduling barriers at this time.
Roland Levillainb488b782015-10-22 11:38:49 +01002207
2208 // Convert ZF into the boolean result.
2209 __ setb(kZero, out.AsRegister<Register>());
2210 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002211 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002212}
2213
2214void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
2215 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2216}
2217
2218void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
2219 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2220}
2221
2222void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
2223 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2224}
2225
2226void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
2227 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2228 LocationSummary::kNoCall,
2229 kIntrinsified);
2230 locations->SetInAt(0, Location::RequiresRegister());
2231 locations->SetOut(Location::SameAsFirstInput());
2232 locations->AddTemp(Location::RequiresRegister());
2233}
2234
2235static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
2236 X86Assembler* assembler) {
2237 Immediate imm_shift(shift);
2238 Immediate imm_mask(mask);
2239 __ movl(temp, reg);
2240 __ shrl(reg, imm_shift);
2241 __ andl(temp, imm_mask);
2242 __ andl(reg, imm_mask);
2243 __ shll(temp, imm_shift);
2244 __ orl(reg, temp);
2245}
2246
2247void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
Roland Levillainb488b782015-10-22 11:38:49 +01002248 X86Assembler* assembler = down_cast<X86Assembler*>(codegen_->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002249 LocationSummary* locations = invoke->GetLocations();
2250
2251 Register reg = locations->InAt(0).AsRegister<Register>();
2252 Register temp = locations->GetTemp(0).AsRegister<Register>();
2253
2254 /*
2255 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2256 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2257 * compared to generic luni implementation which has 5 rounds of swapping bits.
2258 * x = bswap x
2259 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2260 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2261 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2262 */
2263 __ bswapl(reg);
2264 SwapBits(reg, temp, 1, 0x55555555, assembler);
2265 SwapBits(reg, temp, 2, 0x33333333, assembler);
2266 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2267}
2268
2269void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
2270 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2271 LocationSummary::kNoCall,
2272 kIntrinsified);
2273 locations->SetInAt(0, Location::RequiresRegister());
2274 locations->SetOut(Location::SameAsFirstInput());
2275 locations->AddTemp(Location::RequiresRegister());
2276}
2277
2278void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
Roland Levillainb488b782015-10-22 11:38:49 +01002279 X86Assembler* assembler = down_cast<X86Assembler*>(codegen_->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002280 LocationSummary* locations = invoke->GetLocations();
2281
2282 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
2283 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2284 Register temp = locations->GetTemp(0).AsRegister<Register>();
2285
2286 // We want to swap high/low, then bswap each one, and then do the same
2287 // as a 32 bit reverse.
2288 // Exchange high and low.
2289 __ movl(temp, reg_low);
2290 __ movl(reg_low, reg_high);
2291 __ movl(reg_high, temp);
2292
2293 // bit-reverse low
2294 __ bswapl(reg_low);
2295 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
2296 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
2297 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
2298
2299 // bit-reverse high
2300 __ bswapl(reg_high);
2301 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
2302 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
2303 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
2304}
2305
Aart Bikc39dac12016-01-21 08:59:48 -08002306static void CreateBitCountLocations(
2307 ArenaAllocator* arena, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) {
2308 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2309 // Do nothing if there is no popcnt support. This results in generating
2310 // a call for the intrinsic rather than direct code.
2311 return;
2312 }
2313 LocationSummary* locations = new (arena) LocationSummary(invoke,
2314 LocationSummary::kNoCall,
2315 kIntrinsified);
2316 if (is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002317 locations->AddTemp(Location::RequiresRegister());
Aart Bikc39dac12016-01-21 08:59:48 -08002318 }
Aart Bik2a946072016-01-21 12:49:00 -08002319 locations->SetInAt(0, Location::Any());
Aart Bikc39dac12016-01-21 08:59:48 -08002320 locations->SetOut(Location::RequiresRegister());
2321}
2322
2323static void GenBitCount(X86Assembler* assembler, HInvoke* invoke, bool is_long) {
2324 LocationSummary* locations = invoke->GetLocations();
2325 Location src = locations->InAt(0);
2326 Register out = locations->Out().AsRegister<Register>();
2327
2328 if (invoke->InputAt(0)->IsConstant()) {
2329 // Evaluate this at compile time.
2330 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2331 value = is_long
2332 ? POPCOUNT(static_cast<uint64_t>(value))
2333 : POPCOUNT(static_cast<uint32_t>(value));
2334 if (value == 0) {
2335 __ xorl(out, out);
2336 } else {
2337 __ movl(out, Immediate(value));
2338 }
2339 return;
2340 }
2341
2342 // Handle the non-constant cases.
2343 if (!is_long) {
2344 if (src.IsRegister()) {
2345 __ popcntl(out, src.AsRegister<Register>());
2346 } else {
2347 DCHECK(src.IsStackSlot());
2348 __ popcntl(out, Address(ESP, src.GetStackIndex()));
2349 }
Aart Bik2a946072016-01-21 12:49:00 -08002350 } else {
2351 // The 64-bit case needs to worry about two parts.
2352 Register temp = locations->GetTemp(0).AsRegister<Register>();
2353 if (src.IsRegisterPair()) {
2354 __ popcntl(temp, src.AsRegisterPairLow<Register>());
2355 __ popcntl(out, src.AsRegisterPairHigh<Register>());
2356 } else {
2357 DCHECK(src.IsDoubleStackSlot());
2358 __ popcntl(temp, Address(ESP, src.GetStackIndex()));
2359 __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize)));
2360 }
2361 __ addl(out, temp);
Aart Bikc39dac12016-01-21 08:59:48 -08002362 }
Aart Bikc39dac12016-01-21 08:59:48 -08002363}
2364
2365void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) {
2366 CreateBitCountLocations(arena_, codegen_, invoke, /* is_long */ false);
2367}
2368
2369void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) {
2370 GenBitCount(GetAssembler(), invoke, /* is_long */ false);
2371}
2372
2373void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) {
2374 CreateBitCountLocations(arena_, codegen_, invoke, /* is_long */ true);
2375}
2376
2377void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) {
2378 GenBitCount(GetAssembler(), invoke, /* is_long */ true);
2379}
2380
Mark Mendelld5897672015-08-12 21:16:41 -04002381static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_long) {
2382 LocationSummary* locations = new (arena) LocationSummary(invoke,
2383 LocationSummary::kNoCall,
2384 kIntrinsified);
2385 if (is_long) {
2386 locations->SetInAt(0, Location::RequiresRegister());
2387 } else {
2388 locations->SetInAt(0, Location::Any());
2389 }
2390 locations->SetOut(Location::RequiresRegister());
2391}
2392
2393static void GenLeadingZeros(X86Assembler* assembler, HInvoke* invoke, bool is_long) {
2394 LocationSummary* locations = invoke->GetLocations();
2395 Location src = locations->InAt(0);
2396 Register out = locations->Out().AsRegister<Register>();
2397
2398 if (invoke->InputAt(0)->IsConstant()) {
2399 // Evaluate this at compile time.
2400 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2401 if (value == 0) {
2402 value = is_long ? 64 : 32;
2403 } else {
2404 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2405 }
2406 if (value == 0) {
2407 __ xorl(out, out);
2408 } else {
2409 __ movl(out, Immediate(value));
2410 }
2411 return;
2412 }
2413
2414 // Handle the non-constant cases.
2415 if (!is_long) {
2416 if (src.IsRegister()) {
2417 __ bsrl(out, src.AsRegister<Register>());
2418 } else {
2419 DCHECK(src.IsStackSlot());
2420 __ bsrl(out, Address(ESP, src.GetStackIndex()));
2421 }
2422
2423 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002424 NearLabel all_zeroes, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002425 __ j(kEqual, &all_zeroes);
2426
2427 // Correct the result from BSR to get the final CLZ result.
2428 __ xorl(out, Immediate(31));
2429 __ jmp(&done);
2430
2431 // Fix the zero case with the expected result.
2432 __ Bind(&all_zeroes);
2433 __ movl(out, Immediate(32));
2434
2435 __ Bind(&done);
2436 return;
2437 }
2438
2439 // 64 bit case needs to worry about both parts of the register.
2440 DCHECK(src.IsRegisterPair());
2441 Register src_lo = src.AsRegisterPairLow<Register>();
2442 Register src_hi = src.AsRegisterPairHigh<Register>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002443 NearLabel handle_low, done, all_zeroes;
Mark Mendelld5897672015-08-12 21:16:41 -04002444
2445 // Is the high word zero?
2446 __ testl(src_hi, src_hi);
2447 __ j(kEqual, &handle_low);
2448
2449 // High word is not zero. We know that the BSR result is defined in this case.
2450 __ bsrl(out, src_hi);
2451
2452 // Correct the result from BSR to get the final CLZ result.
2453 __ xorl(out, Immediate(31));
2454 __ jmp(&done);
2455
2456 // High word was zero. We have to compute the low word count and add 32.
2457 __ Bind(&handle_low);
2458 __ bsrl(out, src_lo);
2459 __ j(kEqual, &all_zeroes);
2460
2461 // We had a valid result. Use an XOR to both correct the result and add 32.
2462 __ xorl(out, Immediate(63));
2463 __ jmp(&done);
2464
2465 // All zero case.
2466 __ Bind(&all_zeroes);
2467 __ movl(out, Immediate(64));
2468
2469 __ Bind(&done);
2470}
2471
2472void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2473 CreateLeadingZeroLocations(arena_, invoke, /* is_long */ false);
2474}
2475
2476void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2477 X86Assembler* assembler = down_cast<X86Assembler*>(codegen_->GetAssembler());
2478 GenLeadingZeros(assembler, invoke, /* is_long */ false);
2479}
2480
2481void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2482 CreateLeadingZeroLocations(arena_, invoke, /* is_long */ true);
2483}
2484
2485void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2486 X86Assembler* assembler = down_cast<X86Assembler*>(codegen_->GetAssembler());
2487 GenLeadingZeros(assembler, invoke, /* is_long */ true);
2488}
2489
Mark Mendell2d554792015-09-15 21:45:18 -04002490static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_long) {
2491 LocationSummary* locations = new (arena) LocationSummary(invoke,
2492 LocationSummary::kNoCall,
2493 kIntrinsified);
2494 if (is_long) {
2495 locations->SetInAt(0, Location::RequiresRegister());
2496 } else {
2497 locations->SetInAt(0, Location::Any());
2498 }
2499 locations->SetOut(Location::RequiresRegister());
2500}
2501
2502static void GenTrailingZeros(X86Assembler* assembler, HInvoke* invoke, bool is_long) {
2503 LocationSummary* locations = invoke->GetLocations();
2504 Location src = locations->InAt(0);
2505 Register out = locations->Out().AsRegister<Register>();
2506
2507 if (invoke->InputAt(0)->IsConstant()) {
2508 // Evaluate this at compile time.
2509 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2510 if (value == 0) {
2511 value = is_long ? 64 : 32;
2512 } else {
2513 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2514 }
2515 if (value == 0) {
2516 __ xorl(out, out);
2517 } else {
2518 __ movl(out, Immediate(value));
2519 }
2520 return;
2521 }
2522
2523 // Handle the non-constant cases.
2524 if (!is_long) {
2525 if (src.IsRegister()) {
2526 __ bsfl(out, src.AsRegister<Register>());
2527 } else {
2528 DCHECK(src.IsStackSlot());
2529 __ bsfl(out, Address(ESP, src.GetStackIndex()));
2530 }
2531
2532 // BSF sets ZF if the input was zero, and the output is undefined.
2533 NearLabel done;
2534 __ j(kNotEqual, &done);
2535
2536 // Fix the zero case with the expected result.
2537 __ movl(out, Immediate(32));
2538
2539 __ Bind(&done);
2540 return;
2541 }
2542
2543 // 64 bit case needs to worry about both parts of the register.
2544 DCHECK(src.IsRegisterPair());
2545 Register src_lo = src.AsRegisterPairLow<Register>();
2546 Register src_hi = src.AsRegisterPairHigh<Register>();
2547 NearLabel done, all_zeroes;
2548
2549 // If the low word is zero, then ZF will be set. If not, we have the answer.
2550 __ bsfl(out, src_lo);
2551 __ j(kNotEqual, &done);
2552
2553 // Low word was zero. We have to compute the high word count and add 32.
2554 __ bsfl(out, src_hi);
2555 __ j(kEqual, &all_zeroes);
2556
2557 // We had a valid result. Add 32 to account for the low word being zero.
2558 __ addl(out, Immediate(32));
2559 __ jmp(&done);
2560
2561 // All zero case.
2562 __ Bind(&all_zeroes);
2563 __ movl(out, Immediate(64));
2564
2565 __ Bind(&done);
2566}
2567
2568void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2569 CreateTrailingZeroLocations(arena_, invoke, /* is_long */ false);
2570}
2571
2572void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2573 X86Assembler* assembler = down_cast<X86Assembler*>(codegen_->GetAssembler());
2574 GenTrailingZeros(assembler, invoke, /* is_long */ false);
2575}
2576
2577void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2578 CreateTrailingZeroLocations(arena_, invoke, /* is_long */ true);
2579}
2580
2581void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2582 X86Assembler* assembler = down_cast<X86Assembler*>(codegen_->GetAssembler());
2583 GenTrailingZeros(assembler, invoke, /* is_long */ true);
2584}
2585
Mark Mendell09ed1a32015-03-25 08:30:06 -04002586// Unimplemented intrinsics.
2587
2588#define UNIMPLEMENTED_INTRINSIC(Name) \
2589void IntrinsicLocationsBuilderX86::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2590} \
2591void IntrinsicCodeGeneratorX86::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2592}
2593
Mark Mendell09ed1a32015-03-25 08:30:06 -04002594UNIMPLEMENTED_INTRINSIC(MathRoundDouble)
Mark Mendell09ed1a32015-03-25 08:30:06 -04002595UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
Aart Bik59c94542016-01-25 14:20:58 -08002596UNIMPLEMENTED_INTRINSIC(SystemArrayCopy)
2597
2598UNIMPLEMENTED_INTRINSIC(FloatIsInfinite)
2599UNIMPLEMENTED_INTRINSIC(DoubleIsInfinite)
2600UNIMPLEMENTED_INTRINSIC(FloatIsNaN)
2601UNIMPLEMENTED_INTRINSIC(DoubleIsNaN)
2602
2603UNIMPLEMENTED_INTRINSIC(IntegerCompare)
2604UNIMPLEMENTED_INTRINSIC(LongCompare)
2605UNIMPLEMENTED_INTRINSIC(IntegerHighestOneBit)
2606UNIMPLEMENTED_INTRINSIC(LongHighestOneBit)
2607UNIMPLEMENTED_INTRINSIC(IntegerLowestOneBit)
2608UNIMPLEMENTED_INTRINSIC(LongLowestOneBit)
2609UNIMPLEMENTED_INTRINSIC(IntegerSignum)
2610UNIMPLEMENTED_INTRINSIC(LongSignum)
2611
2612// Rotate operations are handled as HRor instructions.
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002613UNIMPLEMENTED_INTRINSIC(IntegerRotateLeft)
2614UNIMPLEMENTED_INTRINSIC(IntegerRotateRight)
Scott Wakeling9ee23f42015-07-23 10:44:35 +01002615UNIMPLEMENTED_INTRINSIC(LongRotateRight)
Scott Wakeling9ee23f42015-07-23 10:44:35 +01002616UNIMPLEMENTED_INTRINSIC(LongRotateLeft)
Mark Mendell09ed1a32015-03-25 08:30:06 -04002617
Roland Levillain4d027112015-07-01 15:41:14 +01002618#undef UNIMPLEMENTED_INTRINSIC
2619
2620#undef __
2621
Mark Mendell09ed1a32015-03-25 08:30:06 -04002622} // namespace x86
2623} // namespace art