blob: daf56d05efaacfe342c9c69cf94ed2571c627d45 [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"
27#include "mirror/array-inl.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040028#include "mirror/string.h"
29#include "thread.h"
30#include "utils/x86/assembler_x86.h"
31#include "utils/x86/constants_x86.h"
32
33namespace art {
34
35namespace x86 {
36
37static constexpr int kDoubleNaNHigh = 0x7FF80000;
38static constexpr int kDoubleNaNLow = 0x00000000;
39static constexpr int kFloatNaN = 0x7FC00000;
40
Mark Mendellfb8d2792015-03-31 22:16:59 -040041IntrinsicLocationsBuilderX86::IntrinsicLocationsBuilderX86(CodeGeneratorX86* codegen)
42 : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) {
43}
44
45
Mark Mendell09ed1a32015-03-25 08:30:06 -040046X86Assembler* IntrinsicCodeGeneratorX86::GetAssembler() {
47 return reinterpret_cast<X86Assembler*>(codegen_->GetAssembler());
48}
49
50ArenaAllocator* IntrinsicCodeGeneratorX86::GetAllocator() {
51 return codegen_->GetGraph()->GetArena();
52}
53
54bool IntrinsicLocationsBuilderX86::TryDispatch(HInvoke* invoke) {
55 Dispatch(invoke);
56 LocationSummary* res = invoke->GetLocations();
57 return res != nullptr && res->Intrinsified();
58}
59
60#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
61
62// TODO: target as memory.
63static void MoveFromReturnRegister(Location target,
64 Primitive::Type type,
65 CodeGeneratorX86* codegen) {
66 if (!target.IsValid()) {
67 DCHECK(type == Primitive::kPrimVoid);
68 return;
69 }
70
71 switch (type) {
72 case Primitive::kPrimBoolean:
73 case Primitive::kPrimByte:
74 case Primitive::kPrimChar:
75 case Primitive::kPrimShort:
76 case Primitive::kPrimInt:
77 case Primitive::kPrimNot: {
78 Register target_reg = target.AsRegister<Register>();
79 if (target_reg != EAX) {
80 __ movl(target_reg, EAX);
81 }
82 break;
83 }
84 case Primitive::kPrimLong: {
85 Register target_reg_lo = target.AsRegisterPairLow<Register>();
86 Register target_reg_hi = target.AsRegisterPairHigh<Register>();
87 if (target_reg_lo != EAX) {
88 __ movl(target_reg_lo, EAX);
89 }
90 if (target_reg_hi != EDX) {
91 __ movl(target_reg_hi, EDX);
92 }
93 break;
94 }
95
96 case Primitive::kPrimVoid:
97 LOG(FATAL) << "Unexpected void type for valid location " << target;
98 UNREACHABLE();
99
100 case Primitive::kPrimDouble: {
101 XmmRegister target_reg = target.AsFpuRegister<XmmRegister>();
102 if (target_reg != XMM0) {
103 __ movsd(target_reg, XMM0);
104 }
105 break;
106 }
107 case Primitive::kPrimFloat: {
108 XmmRegister target_reg = target.AsFpuRegister<XmmRegister>();
109 if (target_reg != XMM0) {
110 __ movss(target_reg, XMM0);
111 }
112 break;
113 }
114 }
115}
116
Roland Levillainec525fc2015-04-28 15:50:20 +0100117static void MoveArguments(HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100118 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +0100119 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400120}
121
122// Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified
123// call. This will copy the arguments into the positions for a regular call.
124//
125// Note: The actual parameters are required to be in the locations given by the invoke's location
126// summary. If an intrinsic modifies those locations before a slowpath call, they must be
127// restored!
128class IntrinsicSlowPathX86 : public SlowPathCodeX86 {
129 public:
Andreas Gampe21030dd2015-05-07 14:46:15 -0700130 explicit IntrinsicSlowPathX86(HInvoke* invoke)
131 : invoke_(invoke) { }
Mark Mendell09ed1a32015-03-25 08:30:06 -0400132
133 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
134 CodeGeneratorX86* codegen = down_cast<CodeGeneratorX86*>(codegen_in);
135 __ Bind(GetEntryLabel());
136
137 SaveLiveRegisters(codegen, invoke_->GetLocations());
138
Roland Levillainec525fc2015-04-28 15:50:20 +0100139 MoveArguments(invoke_, codegen);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400140
141 if (invoke_->IsInvokeStaticOrDirect()) {
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100142 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(),
143 Location::RegisterLocation(EAX));
Mark Mendell09ed1a32015-03-25 08:30:06 -0400144 } else {
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000145 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), Location::RegisterLocation(EAX));
Mark Mendell09ed1a32015-03-25 08:30:06 -0400146 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000147 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400148
149 // Copy the result back to the expected output.
150 Location out = invoke_->GetLocations()->Out();
151 if (out.IsValid()) {
152 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
153 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
154 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
155 }
156
157 RestoreLiveRegisters(codegen, invoke_->GetLocations());
158 __ jmp(GetExitLabel());
159 }
160
Alexandre Rames9931f312015-06-19 14:47:01 +0100161 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathX86"; }
162
Mark Mendell09ed1a32015-03-25 08:30:06 -0400163 private:
164 // The instruction where this slow path is happening.
165 HInvoke* const invoke_;
166
167 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathX86);
168};
169
170#undef __
171#define __ assembler->
172
173static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke, bool is64bit) {
174 LocationSummary* locations = new (arena) LocationSummary(invoke,
175 LocationSummary::kNoCall,
176 kIntrinsified);
177 locations->SetInAt(0, Location::RequiresFpuRegister());
178 locations->SetOut(Location::RequiresRegister());
179 if (is64bit) {
180 locations->AddTemp(Location::RequiresFpuRegister());
181 }
182}
183
184static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke, bool is64bit) {
185 LocationSummary* locations = new (arena) LocationSummary(invoke,
186 LocationSummary::kNoCall,
187 kIntrinsified);
188 locations->SetInAt(0, Location::RequiresRegister());
189 locations->SetOut(Location::RequiresFpuRegister());
190 if (is64bit) {
191 locations->AddTemp(Location::RequiresFpuRegister());
192 locations->AddTemp(Location::RequiresFpuRegister());
193 }
194}
195
196static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
197 Location input = locations->InAt(0);
198 Location output = locations->Out();
199 if (is64bit) {
200 // Need to use the temporary.
201 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
202 __ movsd(temp, input.AsFpuRegister<XmmRegister>());
203 __ movd(output.AsRegisterPairLow<Register>(), temp);
204 __ psrlq(temp, Immediate(32));
205 __ movd(output.AsRegisterPairHigh<Register>(), temp);
206 } else {
207 __ movd(output.AsRegister<Register>(), input.AsFpuRegister<XmmRegister>());
208 }
209}
210
211static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
212 Location input = locations->InAt(0);
213 Location output = locations->Out();
214 if (is64bit) {
215 // Need to use the temporary.
216 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
217 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
218 __ movd(temp1, input.AsRegisterPairLow<Register>());
219 __ movd(temp2, input.AsRegisterPairHigh<Register>());
220 __ punpckldq(temp1, temp2);
221 __ movsd(output.AsFpuRegister<XmmRegister>(), temp1);
222 } else {
223 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<Register>());
224 }
225}
226
227void IntrinsicLocationsBuilderX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
228 CreateFPToIntLocations(arena_, invoke, true);
229}
230void IntrinsicLocationsBuilderX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
231 CreateIntToFPLocations(arena_, invoke, true);
232}
233
234void IntrinsicCodeGeneratorX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
235 MoveFPToInt(invoke->GetLocations(), true, GetAssembler());
236}
237void IntrinsicCodeGeneratorX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
238 MoveIntToFP(invoke->GetLocations(), true, GetAssembler());
239}
240
241void IntrinsicLocationsBuilderX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
242 CreateFPToIntLocations(arena_, invoke, false);
243}
244void IntrinsicLocationsBuilderX86::VisitFloatIntBitsToFloat(HInvoke* invoke) {
245 CreateIntToFPLocations(arena_, invoke, false);
246}
247
248void IntrinsicCodeGeneratorX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
249 MoveFPToInt(invoke->GetLocations(), false, GetAssembler());
250}
251void IntrinsicCodeGeneratorX86::VisitFloatIntBitsToFloat(HInvoke* invoke) {
252 MoveIntToFP(invoke->GetLocations(), false, GetAssembler());
253}
254
255static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
256 LocationSummary* locations = new (arena) LocationSummary(invoke,
257 LocationSummary::kNoCall,
258 kIntrinsified);
259 locations->SetInAt(0, Location::RequiresRegister());
260 locations->SetOut(Location::SameAsFirstInput());
261}
262
263static void CreateLongToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
264 LocationSummary* locations = new (arena) LocationSummary(invoke,
265 LocationSummary::kNoCall,
266 kIntrinsified);
267 locations->SetInAt(0, Location::RequiresRegister());
268 locations->SetOut(Location::RequiresRegister());
269}
270
271static void CreateLongToLongLocations(ArenaAllocator* arena, HInvoke* invoke) {
272 LocationSummary* locations = new (arena) LocationSummary(invoke,
273 LocationSummary::kNoCall,
274 kIntrinsified);
275 locations->SetInAt(0, Location::RequiresRegister());
276 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
277}
278
279static void GenReverseBytes(LocationSummary* locations,
280 Primitive::Type size,
281 X86Assembler* assembler) {
282 Register out = locations->Out().AsRegister<Register>();
283
284 switch (size) {
285 case Primitive::kPrimShort:
286 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
287 __ bswapl(out);
288 __ sarl(out, Immediate(16));
289 break;
290 case Primitive::kPrimInt:
291 __ bswapl(out);
292 break;
293 default:
294 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
295 UNREACHABLE();
296 }
297}
298
299void IntrinsicLocationsBuilderX86::VisitIntegerReverseBytes(HInvoke* invoke) {
300 CreateIntToIntLocations(arena_, invoke);
301}
302
303void IntrinsicCodeGeneratorX86::VisitIntegerReverseBytes(HInvoke* invoke) {
304 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
305}
306
Mark Mendell58d25fd2015-04-03 14:52:31 -0400307void IntrinsicLocationsBuilderX86::VisitLongReverseBytes(HInvoke* invoke) {
308 CreateLongToLongLocations(arena_, invoke);
309}
310
311void IntrinsicCodeGeneratorX86::VisitLongReverseBytes(HInvoke* invoke) {
312 LocationSummary* locations = invoke->GetLocations();
313 Location input = locations->InAt(0);
314 Register input_lo = input.AsRegisterPairLow<Register>();
315 Register input_hi = input.AsRegisterPairHigh<Register>();
316 Location output = locations->Out();
317 Register output_lo = output.AsRegisterPairLow<Register>();
318 Register output_hi = output.AsRegisterPairHigh<Register>();
319
320 X86Assembler* assembler = GetAssembler();
321 // Assign the inputs to the outputs, mixing low/high.
322 __ movl(output_lo, input_hi);
323 __ movl(output_hi, input_lo);
324 __ bswapl(output_lo);
325 __ bswapl(output_hi);
326}
327
Mark Mendell09ed1a32015-03-25 08:30:06 -0400328void IntrinsicLocationsBuilderX86::VisitShortReverseBytes(HInvoke* invoke) {
329 CreateIntToIntLocations(arena_, invoke);
330}
331
332void IntrinsicCodeGeneratorX86::VisitShortReverseBytes(HInvoke* invoke) {
333 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
334}
335
336
337// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
338// need is 64b.
339
340static void CreateFloatToFloat(ArenaAllocator* arena, HInvoke* invoke) {
341 // TODO: Enable memory operations when the assembler supports them.
342 LocationSummary* locations = new (arena) LocationSummary(invoke,
343 LocationSummary::kNoCall,
344 kIntrinsified);
345 locations->SetInAt(0, Location::RequiresFpuRegister());
346 // TODO: Allow x86 to work with memory. This requires assembler support, see below.
347 // locations->SetInAt(0, Location::Any()); // X86 can work on memory directly.
348 locations->SetOut(Location::SameAsFirstInput());
349}
350
351static void MathAbsFP(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
352 Location output = locations->Out();
353
354 if (output.IsFpuRegister()) {
355 // Create the right constant on an aligned stack.
356 if (is64bit) {
357 __ subl(ESP, Immediate(8));
358 __ pushl(Immediate(0x7FFFFFFF));
359 __ pushl(Immediate(0xFFFFFFFF));
360 __ andpd(output.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
361 } else {
362 __ subl(ESP, Immediate(12));
363 __ pushl(Immediate(0x7FFFFFFF));
364 __ andps(output.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
365 }
366 __ addl(ESP, Immediate(16));
367 } else {
368 // TODO: update when assember support is available.
369 UNIMPLEMENTED(FATAL) << "Needs assembler support.";
370// Once assembler support is available, in-memory operations look like this:
371// if (is64bit) {
372// DCHECK(output.IsDoubleStackSlot());
373// __ andl(Address(Register(RSP), output.GetHighStackIndex(kX86WordSize)),
374// Immediate(0x7FFFFFFF));
375// } else {
376// DCHECK(output.IsStackSlot());
377// // Can use and with a literal directly.
378// __ andl(Address(Register(RSP), output.GetStackIndex()), Immediate(0x7FFFFFFF));
379// }
380 }
381}
382
383void IntrinsicLocationsBuilderX86::VisitMathAbsDouble(HInvoke* invoke) {
384 CreateFloatToFloat(arena_, invoke);
385}
386
387void IntrinsicCodeGeneratorX86::VisitMathAbsDouble(HInvoke* invoke) {
388 MathAbsFP(invoke->GetLocations(), true, GetAssembler());
389}
390
391void IntrinsicLocationsBuilderX86::VisitMathAbsFloat(HInvoke* invoke) {
392 CreateFloatToFloat(arena_, invoke);
393}
394
395void IntrinsicCodeGeneratorX86::VisitMathAbsFloat(HInvoke* invoke) {
396 MathAbsFP(invoke->GetLocations(), false, GetAssembler());
397}
398
399static void CreateAbsIntLocation(ArenaAllocator* arena, HInvoke* invoke) {
400 LocationSummary* locations = new (arena) LocationSummary(invoke,
401 LocationSummary::kNoCall,
402 kIntrinsified);
403 locations->SetInAt(0, Location::RegisterLocation(EAX));
404 locations->SetOut(Location::SameAsFirstInput());
405 locations->AddTemp(Location::RegisterLocation(EDX));
406}
407
408static void GenAbsInteger(LocationSummary* locations, X86Assembler* assembler) {
409 Location output = locations->Out();
410 Register out = output.AsRegister<Register>();
411 DCHECK_EQ(out, EAX);
412 Register temp = locations->GetTemp(0).AsRegister<Register>();
413 DCHECK_EQ(temp, EDX);
414
415 // Sign extend EAX into EDX.
416 __ cdq();
417
418 // XOR EAX with sign.
419 __ xorl(EAX, EDX);
420
421 // Subtract out sign to correct.
422 __ subl(EAX, EDX);
423
424 // The result is in EAX.
425}
426
427static void CreateAbsLongLocation(ArenaAllocator* arena, HInvoke* invoke) {
428 LocationSummary* locations = new (arena) LocationSummary(invoke,
429 LocationSummary::kNoCall,
430 kIntrinsified);
431 locations->SetInAt(0, Location::RequiresRegister());
432 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
433 locations->AddTemp(Location::RequiresRegister());
434}
435
436static void GenAbsLong(LocationSummary* locations, X86Assembler* assembler) {
437 Location input = locations->InAt(0);
438 Register input_lo = input.AsRegisterPairLow<Register>();
439 Register input_hi = input.AsRegisterPairHigh<Register>();
440 Location output = locations->Out();
441 Register output_lo = output.AsRegisterPairLow<Register>();
442 Register output_hi = output.AsRegisterPairHigh<Register>();
443 Register temp = locations->GetTemp(0).AsRegister<Register>();
444
445 // Compute the sign into the temporary.
446 __ movl(temp, input_hi);
447 __ sarl(temp, Immediate(31));
448
449 // Store the sign into the output.
450 __ movl(output_lo, temp);
451 __ movl(output_hi, temp);
452
453 // XOR the input to the output.
454 __ xorl(output_lo, input_lo);
455 __ xorl(output_hi, input_hi);
456
457 // Subtract the sign.
458 __ subl(output_lo, temp);
459 __ sbbl(output_hi, temp);
460}
461
462void IntrinsicLocationsBuilderX86::VisitMathAbsInt(HInvoke* invoke) {
463 CreateAbsIntLocation(arena_, invoke);
464}
465
466void IntrinsicCodeGeneratorX86::VisitMathAbsInt(HInvoke* invoke) {
467 GenAbsInteger(invoke->GetLocations(), GetAssembler());
468}
469
470void IntrinsicLocationsBuilderX86::VisitMathAbsLong(HInvoke* invoke) {
471 CreateAbsLongLocation(arena_, invoke);
472}
473
474void IntrinsicCodeGeneratorX86::VisitMathAbsLong(HInvoke* invoke) {
475 GenAbsLong(invoke->GetLocations(), GetAssembler());
476}
477
478static void GenMinMaxFP(LocationSummary* locations, bool is_min, bool is_double,
479 X86Assembler* assembler) {
480 Location op1_loc = locations->InAt(0);
481 Location op2_loc = locations->InAt(1);
482 Location out_loc = locations->Out();
483 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
484
485 // Shortcut for same input locations.
486 if (op1_loc.Equals(op2_loc)) {
487 DCHECK(out_loc.Equals(op1_loc));
488 return;
489 }
490
491 // (out := op1)
492 // out <=? op2
493 // if Nan jmp Nan_label
494 // if out is min jmp done
495 // if op2 is min jmp op2_label
496 // handle -0/+0
497 // jmp done
498 // Nan_label:
499 // out := NaN
500 // op2_label:
501 // out := op2
502 // done:
503 //
504 // This removes one jmp, but needs to copy one input (op1) to out.
505 //
506 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
507
508 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
509
510 Label nan, done, op2_label;
511 if (is_double) {
512 __ ucomisd(out, op2);
513 } else {
514 __ ucomiss(out, op2);
515 }
516
517 __ j(Condition::kParityEven, &nan);
518
519 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
520 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
521
522 // Handle 0.0/-0.0.
523 if (is_min) {
524 if (is_double) {
525 __ orpd(out, op2);
526 } else {
527 __ orps(out, op2);
528 }
529 } else {
530 if (is_double) {
531 __ andpd(out, op2);
532 } else {
533 __ andps(out, op2);
534 }
535 }
536 __ jmp(&done);
537
538 // NaN handling.
539 __ Bind(&nan);
540 if (is_double) {
541 __ pushl(Immediate(kDoubleNaNHigh));
542 __ pushl(Immediate(kDoubleNaNLow));
543 __ movsd(out, Address(ESP, 0));
544 __ addl(ESP, Immediate(8));
545 } else {
546 __ pushl(Immediate(kFloatNaN));
547 __ movss(out, Address(ESP, 0));
548 __ addl(ESP, Immediate(4));
549 }
550 __ jmp(&done);
551
552 // out := op2;
553 __ Bind(&op2_label);
554 if (is_double) {
555 __ movsd(out, op2);
556 } else {
557 __ movss(out, op2);
558 }
559
560 // Done.
561 __ Bind(&done);
562}
563
564static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
565 LocationSummary* locations = new (arena) LocationSummary(invoke,
566 LocationSummary::kNoCall,
567 kIntrinsified);
568 locations->SetInAt(0, Location::RequiresFpuRegister());
569 locations->SetInAt(1, Location::RequiresFpuRegister());
570 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
571 // the second input to be the output (we can simply swap inputs).
572 locations->SetOut(Location::SameAsFirstInput());
573}
574
575void IntrinsicLocationsBuilderX86::VisitMathMinDoubleDouble(HInvoke* invoke) {
576 CreateFPFPToFPLocations(arena_, invoke);
577}
578
579void IntrinsicCodeGeneratorX86::VisitMathMinDoubleDouble(HInvoke* invoke) {
580 GenMinMaxFP(invoke->GetLocations(), true, true, GetAssembler());
581}
582
583void IntrinsicLocationsBuilderX86::VisitMathMinFloatFloat(HInvoke* invoke) {
584 CreateFPFPToFPLocations(arena_, invoke);
585}
586
587void IntrinsicCodeGeneratorX86::VisitMathMinFloatFloat(HInvoke* invoke) {
588 GenMinMaxFP(invoke->GetLocations(), true, false, GetAssembler());
589}
590
591void IntrinsicLocationsBuilderX86::VisitMathMaxDoubleDouble(HInvoke* invoke) {
592 CreateFPFPToFPLocations(arena_, invoke);
593}
594
595void IntrinsicCodeGeneratorX86::VisitMathMaxDoubleDouble(HInvoke* invoke) {
596 GenMinMaxFP(invoke->GetLocations(), false, true, GetAssembler());
597}
598
599void IntrinsicLocationsBuilderX86::VisitMathMaxFloatFloat(HInvoke* invoke) {
600 CreateFPFPToFPLocations(arena_, invoke);
601}
602
603void IntrinsicCodeGeneratorX86::VisitMathMaxFloatFloat(HInvoke* invoke) {
604 GenMinMaxFP(invoke->GetLocations(), false, false, GetAssembler());
605}
606
607static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
608 X86Assembler* assembler) {
609 Location op1_loc = locations->InAt(0);
610 Location op2_loc = locations->InAt(1);
611
612 // Shortcut for same input locations.
613 if (op1_loc.Equals(op2_loc)) {
614 // Can return immediately, as op1_loc == out_loc.
615 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
616 // a copy here.
617 DCHECK(locations->Out().Equals(op1_loc));
618 return;
619 }
620
621 if (is_long) {
622 // Need to perform a subtract to get the sign right.
623 // op1 is already in the same location as the output.
624 Location output = locations->Out();
625 Register output_lo = output.AsRegisterPairLow<Register>();
626 Register output_hi = output.AsRegisterPairHigh<Register>();
627
628 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
629 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
630
631 // Spare register to compute the subtraction to set condition code.
632 Register temp = locations->GetTemp(0).AsRegister<Register>();
633
634 // Subtract off op2_low.
635 __ movl(temp, output_lo);
636 __ subl(temp, op2_lo);
637
638 // Now use the same tempo and the borrow to finish the subtraction of op2_hi.
639 __ movl(temp, output_hi);
640 __ sbbl(temp, op2_hi);
641
642 // Now the condition code is correct.
643 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
644 __ cmovl(cond, output_lo, op2_lo);
645 __ cmovl(cond, output_hi, op2_hi);
646 } else {
647 Register out = locations->Out().AsRegister<Register>();
648 Register op2 = op2_loc.AsRegister<Register>();
649
650 // (out := op1)
651 // out <=? op2
652 // if out is min jmp done
653 // out := op2
654 // done:
655
656 __ cmpl(out, op2);
657 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
658 __ cmovl(cond, out, op2);
659 }
660}
661
662static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
663 LocationSummary* locations = new (arena) LocationSummary(invoke,
664 LocationSummary::kNoCall,
665 kIntrinsified);
666 locations->SetInAt(0, Location::RequiresRegister());
667 locations->SetInAt(1, Location::RequiresRegister());
668 locations->SetOut(Location::SameAsFirstInput());
669}
670
671static void CreateLongLongToLongLocations(ArenaAllocator* arena, HInvoke* invoke) {
672 LocationSummary* locations = new (arena) LocationSummary(invoke,
673 LocationSummary::kNoCall,
674 kIntrinsified);
675 locations->SetInAt(0, Location::RequiresRegister());
676 locations->SetInAt(1, Location::RequiresRegister());
677 locations->SetOut(Location::SameAsFirstInput());
678 // Register to use to perform a long subtract to set cc.
679 locations->AddTemp(Location::RequiresRegister());
680}
681
682void IntrinsicLocationsBuilderX86::VisitMathMinIntInt(HInvoke* invoke) {
683 CreateIntIntToIntLocations(arena_, invoke);
684}
685
686void IntrinsicCodeGeneratorX86::VisitMathMinIntInt(HInvoke* invoke) {
687 GenMinMax(invoke->GetLocations(), true, false, GetAssembler());
688}
689
690void IntrinsicLocationsBuilderX86::VisitMathMinLongLong(HInvoke* invoke) {
691 CreateLongLongToLongLocations(arena_, invoke);
692}
693
694void IntrinsicCodeGeneratorX86::VisitMathMinLongLong(HInvoke* invoke) {
695 GenMinMax(invoke->GetLocations(), true, true, GetAssembler());
696}
697
698void IntrinsicLocationsBuilderX86::VisitMathMaxIntInt(HInvoke* invoke) {
699 CreateIntIntToIntLocations(arena_, invoke);
700}
701
702void IntrinsicCodeGeneratorX86::VisitMathMaxIntInt(HInvoke* invoke) {
703 GenMinMax(invoke->GetLocations(), false, false, GetAssembler());
704}
705
706void IntrinsicLocationsBuilderX86::VisitMathMaxLongLong(HInvoke* invoke) {
707 CreateLongLongToLongLocations(arena_, invoke);
708}
709
710void IntrinsicCodeGeneratorX86::VisitMathMaxLongLong(HInvoke* invoke) {
711 GenMinMax(invoke->GetLocations(), false, true, GetAssembler());
712}
713
714static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
715 LocationSummary* locations = new (arena) LocationSummary(invoke,
716 LocationSummary::kNoCall,
717 kIntrinsified);
718 locations->SetInAt(0, Location::RequiresFpuRegister());
719 locations->SetOut(Location::RequiresFpuRegister());
720}
721
722void IntrinsicLocationsBuilderX86::VisitMathSqrt(HInvoke* invoke) {
723 CreateFPToFPLocations(arena_, invoke);
724}
725
726void IntrinsicCodeGeneratorX86::VisitMathSqrt(HInvoke* invoke) {
727 LocationSummary* locations = invoke->GetLocations();
728 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
729 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
730
731 GetAssembler()->sqrtsd(out, in);
732}
733
Mark Mendellfb8d2792015-03-31 22:16:59 -0400734static void InvokeOutOfLineIntrinsic(CodeGeneratorX86* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100735 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400736
737 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100738 codegen->GenerateStaticOrDirectCall(invoke->AsInvokeStaticOrDirect(),
739 Location::RegisterLocation(EAX));
Mingyao Yange90db122015-04-03 17:56:54 -0700740 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400741
742 // Copy the result back to the expected output.
743 Location out = invoke->GetLocations()->Out();
744 if (out.IsValid()) {
745 DCHECK(out.IsRegister());
746 MoveFromReturnRegister(out, invoke->GetType(), codegen);
747 }
748}
749
750static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
751 HInvoke* invoke,
752 CodeGeneratorX86* codegen) {
753 // Do we have instruction support?
754 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
755 CreateFPToFPLocations(arena, invoke);
756 return;
757 }
758
759 // We have to fall back to a call to the intrinsic.
760 LocationSummary* locations = new (arena) LocationSummary(invoke,
761 LocationSummary::kCall);
762 InvokeRuntimeCallingConvention calling_convention;
763 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
764 locations->SetOut(Location::FpuRegisterLocation(XMM0));
765 // Needs to be EAX for the invoke.
766 locations->AddTemp(Location::RegisterLocation(EAX));
767}
768
769static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86* codegen,
770 HInvoke* invoke,
771 X86Assembler* assembler,
772 int round_mode) {
773 LocationSummary* locations = invoke->GetLocations();
774 if (locations->WillCall()) {
775 InvokeOutOfLineIntrinsic(codegen, invoke);
776 } else {
777 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
778 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
779 __ roundsd(out, in, Immediate(round_mode));
780 }
781}
782
783void IntrinsicLocationsBuilderX86::VisitMathCeil(HInvoke* invoke) {
784 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
785}
786
787void IntrinsicCodeGeneratorX86::VisitMathCeil(HInvoke* invoke) {
788 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
789}
790
791void IntrinsicLocationsBuilderX86::VisitMathFloor(HInvoke* invoke) {
792 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
793}
794
795void IntrinsicCodeGeneratorX86::VisitMathFloor(HInvoke* invoke) {
796 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
797}
798
799void IntrinsicLocationsBuilderX86::VisitMathRint(HInvoke* invoke) {
800 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
801}
802
803void IntrinsicCodeGeneratorX86::VisitMathRint(HInvoke* invoke) {
804 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
805}
806
807// Note that 32 bit x86 doesn't have the capability to inline MathRoundDouble,
808// as it needs 64 bit instructions.
809void IntrinsicLocationsBuilderX86::VisitMathRoundFloat(HInvoke* invoke) {
810 // Do we have instruction support?
811 if (codegen_->GetInstructionSetFeatures().HasSSE4_1()) {
812 LocationSummary* locations = new (arena_) LocationSummary(invoke,
813 LocationSummary::kNoCall,
814 kIntrinsified);
815 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffrayd9b92402015-04-21 10:02:22 +0100816 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400817 locations->AddTemp(Location::RequiresFpuRegister());
818 locations->AddTemp(Location::RequiresFpuRegister());
819 return;
820 }
821
822 // We have to fall back to a call to the intrinsic.
823 LocationSummary* locations = new (arena_) LocationSummary(invoke,
824 LocationSummary::kCall);
825 InvokeRuntimeCallingConvention calling_convention;
826 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
827 locations->SetOut(Location::RegisterLocation(EAX));
828 // Needs to be EAX for the invoke.
829 locations->AddTemp(Location::RegisterLocation(EAX));
830}
831
832void IntrinsicCodeGeneratorX86::VisitMathRoundFloat(HInvoke* invoke) {
833 LocationSummary* locations = invoke->GetLocations();
834 if (locations->WillCall()) {
835 InvokeOutOfLineIntrinsic(codegen_, invoke);
836 return;
837 }
838
839 // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int.
840 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
841 Register out = locations->Out().AsRegister<Register>();
842 XmmRegister maxInt = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
843 XmmRegister inPlusPointFive = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
844 Label done, nan;
845 X86Assembler* assembler = GetAssembler();
846
847 // Generate 0.5 into inPlusPointFive.
848 __ movl(out, Immediate(bit_cast<int32_t, float>(0.5f)));
849 __ movd(inPlusPointFive, out);
850
851 // Add in the input.
852 __ addss(inPlusPointFive, in);
853
854 // And truncate to an integer.
855 __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1));
856
857 __ movl(out, Immediate(kPrimIntMax));
858 // maxInt = int-to-float(out)
859 __ cvtsi2ss(maxInt, out);
860
861 // if inPlusPointFive >= maxInt goto done
862 __ comiss(inPlusPointFive, maxInt);
863 __ j(kAboveEqual, &done);
864
865 // if input == NaN goto nan
866 __ j(kUnordered, &nan);
867
868 // output = float-to-int-truncate(input)
869 __ cvttss2si(out, inPlusPointFive);
870 __ jmp(&done);
871 __ Bind(&nan);
872
873 // output = 0
874 __ xorl(out, out);
875 __ Bind(&done);
876}
877
Mark Mendell09ed1a32015-03-25 08:30:06 -0400878void IntrinsicLocationsBuilderX86::VisitStringCharAt(HInvoke* invoke) {
879 // The inputs plus one temp.
880 LocationSummary* locations = new (arena_) LocationSummary(invoke,
881 LocationSummary::kCallOnSlowPath,
882 kIntrinsified);
883 locations->SetInAt(0, Location::RequiresRegister());
884 locations->SetInAt(1, Location::RequiresRegister());
885 locations->SetOut(Location::SameAsFirstInput());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400886}
887
888void IntrinsicCodeGeneratorX86::VisitStringCharAt(HInvoke* invoke) {
889 LocationSummary* locations = invoke->GetLocations();
890
891 // Location of reference to data array
892 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
893 // Location of count
894 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Mark Mendell09ed1a32015-03-25 08:30:06 -0400895
896 Register obj = locations->InAt(0).AsRegister<Register>();
897 Register idx = locations->InAt(1).AsRegister<Register>();
898 Register out = locations->Out().AsRegister<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -0400899
900 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
901 // the cost.
902 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
903 // we will not optimize the code for constants (which would save a register).
904
Andreas Gampe21030dd2015-05-07 14:46:15 -0700905 SlowPathCodeX86* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400906 codegen_->AddSlowPath(slow_path);
907
908 X86Assembler* assembler = GetAssembler();
909
910 __ cmpl(idx, Address(obj, count_offset));
911 codegen_->MaybeRecordImplicitNullCheck(invoke);
912 __ j(kAboveEqual, slow_path->GetEntryLabel());
913
Jeff Hao848f70a2014-01-15 13:49:50 -0800914 // out = out[2*idx].
915 __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset));
Mark Mendell09ed1a32015-03-25 08:30:06 -0400916
917 __ Bind(slow_path->GetExitLabel());
918}
919
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000920void IntrinsicLocationsBuilderX86::VisitStringCompareTo(HInvoke* invoke) {
921 // The inputs plus one temp.
922 LocationSummary* locations = new (arena_) LocationSummary(invoke,
923 LocationSummary::kCall,
924 kIntrinsified);
925 InvokeRuntimeCallingConvention calling_convention;
926 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
927 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
928 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000929}
930
931void IntrinsicCodeGeneratorX86::VisitStringCompareTo(HInvoke* invoke) {
932 X86Assembler* assembler = GetAssembler();
933 LocationSummary* locations = invoke->GetLocations();
934
Nicolas Geoffray512e04d2015-03-27 17:21:24 +0000935 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +0100936 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000937
938 Register argument = locations->InAt(1).AsRegister<Register>();
939 __ testl(argument, argument);
Andreas Gampe21030dd2015-05-07 14:46:15 -0700940 SlowPathCodeX86* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000941 codegen_->AddSlowPath(slow_path);
942 __ j(kEqual, slow_path->GetEntryLabel());
943
944 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pStringCompareTo)));
945 __ Bind(slow_path->GetExitLabel());
946}
947
Agi Csakid7138c82015-08-13 17:46:44 -0700948void IntrinsicLocationsBuilderX86::VisitStringEquals(HInvoke* invoke) {
949 LocationSummary* locations = new (arena_) LocationSummary(invoke,
950 LocationSummary::kNoCall,
951 kIntrinsified);
952 locations->SetInAt(0, Location::RequiresRegister());
953 locations->SetInAt(1, Location::RequiresRegister());
954
955 // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction.
956 locations->AddTemp(Location::RegisterLocation(ECX));
957 locations->AddTemp(Location::RegisterLocation(EDI));
958
959 // Set output, ESI needed for repe_cmpsl instruction anyways.
960 locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap);
961}
962
963void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) {
964 X86Assembler* assembler = GetAssembler();
965 LocationSummary* locations = invoke->GetLocations();
966
967 Register str = locations->InAt(0).AsRegister<Register>();
968 Register arg = locations->InAt(1).AsRegister<Register>();
969 Register ecx = locations->GetTemp(0).AsRegister<Register>();
970 Register edi = locations->GetTemp(1).AsRegister<Register>();
971 Register esi = locations->Out().AsRegister<Register>();
972
973 Label end;
974 Label return_true;
975 Label return_false;
976
977 // Get offsets of count, value, and class fields within a string object.
978 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
979 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
980 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
981
982 // Note that the null check must have been done earlier.
983 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
984
985 // Check if input is null, return false if it is.
986 __ testl(arg, arg);
987 __ j(kEqual, &return_false);
988
989 // Instanceof check for the argument by comparing class fields.
990 // All string objects must have the same type since String cannot be subclassed.
991 // Receiver must be a string object, so its class field is equal to all strings' class fields.
992 // If the argument is a string object, its class field must be equal to receiver's class field.
993 __ movl(ecx, Address(str, class_offset));
994 __ cmpl(ecx, Address(arg, class_offset));
995 __ j(kNotEqual, &return_false);
996
997 // Reference equality check, return true if same reference.
998 __ cmpl(str, arg);
999 __ j(kEqual, &return_true);
1000
1001 // Load length of receiver string.
1002 __ movl(ecx, Address(str, count_offset));
1003 // Check if lengths are equal, return false if they're not.
1004 __ cmpl(ecx, Address(arg, count_offset));
1005 __ j(kNotEqual, &return_false);
1006 // Return true if both strings are empty.
1007 __ testl(ecx, ecx);
1008 __ j(kEqual, &return_true);
1009
1010 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1011 __ leal(esi, Address(str, value_offset));
1012 __ leal(edi, Address(arg, value_offset));
1013
1014 // Divide string length by 2 to compare characters 2 at a time and adjust for odd lengths.
1015 __ addl(ecx, Immediate(1));
1016 __ shrl(ecx, Immediate(1));
1017
1018 // Assertions that must hold in order to compare strings 2 characters at a time.
1019 DCHECK_ALIGNED(value_offset, 4);
1020 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1021
1022 // Loop to compare strings two characters at a time starting at the beginning of the string.
1023 __ repe_cmpsl();
1024 // If strings are not equal, zero flag will be cleared.
1025 __ j(kNotEqual, &return_false);
1026
1027 // Return true and exit the function.
1028 // If loop does not result in returning false, we return true.
1029 __ Bind(&return_true);
1030 __ movl(esi, Immediate(1));
1031 __ jmp(&end);
1032
1033 // Return false and exit the function.
1034 __ Bind(&return_false);
1035 __ xorl(esi, esi);
1036 __ Bind(&end);
1037}
1038
Andreas Gampe21030dd2015-05-07 14:46:15 -07001039static void CreateStringIndexOfLocations(HInvoke* invoke,
1040 ArenaAllocator* allocator,
1041 bool start_at_zero) {
1042 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1043 LocationSummary::kCallOnSlowPath,
1044 kIntrinsified);
1045 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1046 locations->SetInAt(0, Location::RegisterLocation(EDI));
1047 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1048 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1049 // of the instruction explicitly.
1050 // Note: This works as we don't clobber EAX anywhere.
1051 locations->SetInAt(1, Location::RegisterLocation(EAX));
1052 if (!start_at_zero) {
1053 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1054 }
1055 // As we clobber EDI during execution anyways, also use it as the output.
1056 locations->SetOut(Location::SameAsFirstInput());
1057
1058 // repne scasw uses ECX as the counter.
1059 locations->AddTemp(Location::RegisterLocation(ECX));
1060 // Need another temporary to be able to compute the result.
1061 locations->AddTemp(Location::RequiresRegister());
1062}
1063
1064static void GenerateStringIndexOf(HInvoke* invoke,
1065 X86Assembler* assembler,
1066 CodeGeneratorX86* codegen,
1067 ArenaAllocator* allocator,
1068 bool start_at_zero) {
1069 LocationSummary* locations = invoke->GetLocations();
1070
1071 // Note that the null check must have been done earlier.
1072 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1073
1074 Register string_obj = locations->InAt(0).AsRegister<Register>();
1075 Register search_value = locations->InAt(1).AsRegister<Register>();
1076 Register counter = locations->GetTemp(0).AsRegister<Register>();
1077 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1078 Register out = locations->Out().AsRegister<Register>();
1079
1080 // Check our assumptions for registers.
1081 DCHECK_EQ(string_obj, EDI);
1082 DCHECK_EQ(search_value, EAX);
1083 DCHECK_EQ(counter, ECX);
1084 DCHECK_EQ(out, EDI);
1085
1086 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1087 // or directly dispatch if we have a constant.
1088 SlowPathCodeX86* slow_path = nullptr;
1089 if (invoke->InputAt(1)->IsIntConstant()) {
1090 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1091 std::numeric_limits<uint16_t>::max()) {
1092 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1093 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1094 slow_path = new (allocator) IntrinsicSlowPathX86(invoke);
1095 codegen->AddSlowPath(slow_path);
1096 __ jmp(slow_path->GetEntryLabel());
1097 __ Bind(slow_path->GetExitLabel());
1098 return;
1099 }
1100 } else {
1101 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1102 slow_path = new (allocator) IntrinsicSlowPathX86(invoke);
1103 codegen->AddSlowPath(slow_path);
1104 __ j(kAbove, slow_path->GetEntryLabel());
1105 }
1106
1107 // From here down, we know that we are looking for a char that fits in 16 bits.
1108 // Location of reference to data array within the String object.
1109 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1110 // Location of count within the String object.
1111 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1112
1113 // Load string length, i.e., the count field of the string.
1114 __ movl(string_length, Address(string_obj, count_offset));
1115
1116 // Do a zero-length check.
1117 // TODO: Support jecxz.
1118 Label not_found_label;
1119 __ testl(string_length, string_length);
1120 __ j(kEqual, &not_found_label);
1121
1122 if (start_at_zero) {
1123 // Number of chars to scan is the same as the string length.
1124 __ movl(counter, string_length);
1125
1126 // Move to the start of the string.
1127 __ addl(string_obj, Immediate(value_offset));
1128 } else {
1129 Register start_index = locations->InAt(2).AsRegister<Register>();
1130
1131 // Do a start_index check.
1132 __ cmpl(start_index, string_length);
1133 __ j(kGreaterEqual, &not_found_label);
1134
1135 // Ensure we have a start index >= 0;
1136 __ xorl(counter, counter);
1137 __ cmpl(start_index, Immediate(0));
1138 __ cmovl(kGreater, counter, start_index);
1139
1140 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1141 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1142
1143 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1144 // compare.
1145 __ negl(counter);
1146 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1147 }
1148
1149 // Everything is set up for repne scasw:
1150 // * Comparison address in EDI.
1151 // * Counter in ECX.
1152 __ repne_scasw();
1153
1154 // Did we find a match?
1155 __ j(kNotEqual, &not_found_label);
1156
1157 // Yes, we matched. Compute the index of the result.
1158 __ subl(string_length, counter);
1159 __ leal(out, Address(string_length, -1));
1160
1161 Label done;
1162 __ jmp(&done);
1163
1164 // Failed to match; return -1.
1165 __ Bind(&not_found_label);
1166 __ movl(out, Immediate(-1));
1167
1168 // And join up at the end.
1169 __ Bind(&done);
1170 if (slow_path != nullptr) {
1171 __ Bind(slow_path->GetExitLabel());
1172 }
1173}
1174
1175void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
1176 CreateStringIndexOfLocations(invoke, arena_, true);
1177}
1178
1179void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
1180 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1181}
1182
1183void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
1184 CreateStringIndexOfLocations(invoke, arena_, false);
1185}
1186
1187void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
1188 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1189}
1190
Jeff Hao848f70a2014-01-15 13:49:50 -08001191void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1192 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1193 LocationSummary::kCall,
1194 kIntrinsified);
1195 InvokeRuntimeCallingConvention calling_convention;
1196 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1197 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1198 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1199 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1200 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001201}
1202
1203void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1204 X86Assembler* assembler = GetAssembler();
1205 LocationSummary* locations = invoke->GetLocations();
1206
1207 Register byte_array = locations->InAt(0).AsRegister<Register>();
1208 __ testl(byte_array, byte_array);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001209 SlowPathCodeX86* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001210 codegen_->AddSlowPath(slow_path);
1211 __ j(kEqual, slow_path->GetEntryLabel());
1212
1213 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocStringFromBytes)));
1214 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1215 __ Bind(slow_path->GetExitLabel());
1216}
1217
1218void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
1219 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1220 LocationSummary::kCall,
1221 kIntrinsified);
1222 InvokeRuntimeCallingConvention calling_convention;
1223 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1224 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1225 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1226 locations->SetOut(Location::RegisterLocation(EAX));
1227}
1228
1229void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
1230 X86Assembler* assembler = GetAssembler();
1231
1232 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocStringFromChars)));
1233 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1234}
1235
1236void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
1237 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1238 LocationSummary::kCall,
1239 kIntrinsified);
1240 InvokeRuntimeCallingConvention calling_convention;
1241 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1242 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001243}
1244
1245void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1246 X86Assembler* assembler = GetAssembler();
1247 LocationSummary* locations = invoke->GetLocations();
1248
1249 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1250 __ testl(string_to_copy, string_to_copy);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001251 SlowPathCodeX86* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001252 codegen_->AddSlowPath(slow_path);
1253 __ j(kEqual, slow_path->GetEntryLabel());
1254
1255 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocStringFromString)));
1256 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1257 __ Bind(slow_path->GetExitLabel());
1258}
1259
Mark Mendell09ed1a32015-03-25 08:30:06 -04001260static void GenPeek(LocationSummary* locations, Primitive::Type size, X86Assembler* assembler) {
1261 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1262 Location out_loc = locations->Out();
1263 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1264 // to avoid a SIGBUS.
1265 switch (size) {
1266 case Primitive::kPrimByte:
1267 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1268 break;
1269 case Primitive::kPrimShort:
1270 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1271 break;
1272 case Primitive::kPrimInt:
1273 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1274 break;
1275 case Primitive::kPrimLong:
1276 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1277 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1278 break;
1279 default:
1280 LOG(FATAL) << "Type not recognized for peek: " << size;
1281 UNREACHABLE();
1282 }
1283}
1284
1285void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
1286 CreateLongToIntLocations(arena_, invoke);
1287}
1288
1289void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
1290 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1291}
1292
1293void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
1294 CreateLongToIntLocations(arena_, invoke);
1295}
1296
1297void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
1298 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1299}
1300
1301void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
1302 CreateLongToLongLocations(arena_, invoke);
1303}
1304
1305void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
1306 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1307}
1308
1309void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
1310 CreateLongToIntLocations(arena_, invoke);
1311}
1312
1313void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
1314 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1315}
1316
1317static void CreateLongIntToVoidLocations(ArenaAllocator* arena, Primitive::Type size,
1318 HInvoke* invoke) {
1319 LocationSummary* locations = new (arena) LocationSummary(invoke,
1320 LocationSummary::kNoCall,
1321 kIntrinsified);
1322 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001323 HInstruction* value = invoke->InputAt(1);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001324 if (size == Primitive::kPrimByte) {
1325 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1326 } else {
1327 locations->SetInAt(1, Location::RegisterOrConstant(value));
1328 }
1329}
1330
1331static void GenPoke(LocationSummary* locations, Primitive::Type size, X86Assembler* assembler) {
1332 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1333 Location value_loc = locations->InAt(1);
1334 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1335 // to avoid a SIGBUS.
1336 switch (size) {
1337 case Primitive::kPrimByte:
1338 if (value_loc.IsConstant()) {
1339 __ movb(Address(address, 0),
1340 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1341 } else {
1342 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1343 }
1344 break;
1345 case Primitive::kPrimShort:
1346 if (value_loc.IsConstant()) {
1347 __ movw(Address(address, 0),
1348 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1349 } else {
1350 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1351 }
1352 break;
1353 case Primitive::kPrimInt:
1354 if (value_loc.IsConstant()) {
1355 __ movl(Address(address, 0),
1356 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1357 } else {
1358 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1359 }
1360 break;
1361 case Primitive::kPrimLong:
1362 if (value_loc.IsConstant()) {
1363 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1364 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1365 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1366 } else {
1367 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1368 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1369 }
1370 break;
1371 default:
1372 LOG(FATAL) << "Type not recognized for poke: " << size;
1373 UNREACHABLE();
1374 }
1375}
1376
1377void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
1378 CreateLongIntToVoidLocations(arena_, Primitive::kPrimByte, invoke);
1379}
1380
1381void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
1382 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1383}
1384
1385void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
1386 CreateLongIntToVoidLocations(arena_, Primitive::kPrimInt, invoke);
1387}
1388
1389void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
1390 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1391}
1392
1393void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
1394 CreateLongIntToVoidLocations(arena_, Primitive::kPrimLong, invoke);
1395}
1396
1397void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
1398 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1399}
1400
1401void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
1402 CreateLongIntToVoidLocations(arena_, Primitive::kPrimShort, invoke);
1403}
1404
1405void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
1406 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1407}
1408
1409void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
1410 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1411 LocationSummary::kNoCall,
1412 kIntrinsified);
1413 locations->SetOut(Location::RequiresRegister());
1414}
1415
1416void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1417 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1418 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86WordSize>()));
1419}
1420
1421static void GenUnsafeGet(LocationSummary* locations, Primitive::Type type,
1422 bool is_volatile, X86Assembler* assembler) {
1423 Register base = locations->InAt(1).AsRegister<Register>();
1424 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1425 Location output = locations->Out();
1426
1427 switch (type) {
1428 case Primitive::kPrimInt:
Roland Levillain4d027112015-07-01 15:41:14 +01001429 case Primitive::kPrimNot: {
1430 Register output_reg = output.AsRegister<Register>();
1431 __ movl(output_reg, Address(base, offset, ScaleFactor::TIMES_1, 0));
1432 if (type == Primitive::kPrimNot) {
1433 __ MaybeUnpoisonHeapReference(output_reg);
1434 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001435 break;
Roland Levillain4d027112015-07-01 15:41:14 +01001436 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001437
1438 case Primitive::kPrimLong: {
1439 Register output_lo = output.AsRegisterPairLow<Register>();
1440 Register output_hi = output.AsRegisterPairHigh<Register>();
1441 if (is_volatile) {
1442 // Need to use a XMM to read atomically.
1443 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1444 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
1445 __ movd(output_lo, temp);
1446 __ psrlq(temp, Immediate(32));
1447 __ movd(output_hi, temp);
1448 } else {
1449 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
1450 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
1451 }
1452 }
1453 break;
1454
1455 default:
1456 LOG(FATAL) << "Unsupported op size " << type;
1457 UNREACHABLE();
1458 }
1459}
1460
1461static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke,
1462 bool is_long, bool is_volatile) {
1463 LocationSummary* locations = new (arena) LocationSummary(invoke,
1464 LocationSummary::kNoCall,
1465 kIntrinsified);
1466 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1467 locations->SetInAt(1, Location::RequiresRegister());
1468 locations->SetInAt(2, Location::RequiresRegister());
1469 if (is_long) {
1470 if (is_volatile) {
1471 // Need to use XMM to read volatile.
1472 locations->AddTemp(Location::RequiresFpuRegister());
1473 locations->SetOut(Location::RequiresRegister());
1474 } else {
1475 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1476 }
1477 } else {
1478 locations->SetOut(Location::RequiresRegister());
1479 }
1480}
1481
1482void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
1483 CreateIntIntIntToIntLocations(arena_, invoke, false, false);
1484}
1485void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
1486 CreateIntIntIntToIntLocations(arena_, invoke, false, true);
1487}
1488void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
1489 CreateIntIntIntToIntLocations(arena_, invoke, false, false);
1490}
1491void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1492 CreateIntIntIntToIntLocations(arena_, invoke, true, true);
1493}
1494void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
1495 CreateIntIntIntToIntLocations(arena_, invoke, false, false);
1496}
1497void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1498 CreateIntIntIntToIntLocations(arena_, invoke, false, true);
1499}
1500
1501
1502void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
1503 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, false, GetAssembler());
1504}
1505void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
1506 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, true, GetAssembler());
1507}
1508void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
1509 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, false, GetAssembler());
1510}
1511void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1512 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, true, GetAssembler());
1513}
1514void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
1515 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, false, GetAssembler());
1516}
1517void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1518 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, true, GetAssembler());
1519}
1520
1521
1522static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1523 Primitive::Type type,
1524 HInvoke* invoke,
1525 bool is_volatile) {
1526 LocationSummary* locations = new (arena) LocationSummary(invoke,
1527 LocationSummary::kNoCall,
1528 kIntrinsified);
1529 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1530 locations->SetInAt(1, Location::RequiresRegister());
1531 locations->SetInAt(2, Location::RequiresRegister());
1532 locations->SetInAt(3, Location::RequiresRegister());
1533 if (type == Primitive::kPrimNot) {
1534 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001535 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04001536 // Ensure the value is in a byte register.
1537 locations->AddTemp(Location::RegisterLocation(ECX));
1538 } else if (type == Primitive::kPrimLong && is_volatile) {
1539 locations->AddTemp(Location::RequiresFpuRegister());
1540 locations->AddTemp(Location::RequiresFpuRegister());
1541 }
1542}
1543
1544void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
1545 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke, false);
1546}
1547void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
1548 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke, false);
1549}
1550void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
1551 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke, true);
1552}
1553void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
1554 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke, false);
1555}
1556void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1557 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke, false);
1558}
1559void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1560 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke, true);
1561}
1562void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
1563 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke, false);
1564}
1565void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1566 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke, false);
1567}
1568void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1569 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke, true);
1570}
1571
1572// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1573// memory model.
1574static void GenUnsafePut(LocationSummary* locations,
1575 Primitive::Type type,
1576 bool is_volatile,
1577 CodeGeneratorX86* codegen) {
1578 X86Assembler* assembler = reinterpret_cast<X86Assembler*>(codegen->GetAssembler());
1579 Register base = locations->InAt(1).AsRegister<Register>();
1580 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1581 Location value_loc = locations->InAt(3);
1582
1583 if (type == Primitive::kPrimLong) {
1584 Register value_lo = value_loc.AsRegisterPairLow<Register>();
1585 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
1586 if (is_volatile) {
1587 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1588 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1589 __ movd(temp1, value_lo);
1590 __ movd(temp2, value_hi);
1591 __ punpckldq(temp1, temp2);
1592 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
1593 } else {
1594 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
1595 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
1596 }
Roland Levillain4d027112015-07-01 15:41:14 +01001597 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1598 Register temp = locations->GetTemp(0).AsRegister<Register>();
1599 __ movl(temp, value_loc.AsRegister<Register>());
1600 __ PoisonHeapReference(temp);
1601 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001602 } else {
1603 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
1604 }
1605
1606 if (is_volatile) {
1607 __ mfence();
1608 }
1609
1610 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001611 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04001612 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
1613 locations->GetTemp(1).AsRegister<Register>(),
1614 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001615 value_loc.AsRegister<Register>(),
1616 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001617 }
1618}
1619
1620void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
1621 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1622}
1623void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
1624 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1625}
1626void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
1627 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_);
1628}
1629void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
1630 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1631}
1632void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1633 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1634}
1635void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1636 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_);
1637}
1638void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
1639 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1640}
1641void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1642 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1643}
1644void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1645 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_);
1646}
1647
Mark Mendell58d25fd2015-04-03 14:52:31 -04001648static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
1649 HInvoke* invoke) {
1650 LocationSummary* locations = new (arena) LocationSummary(invoke,
1651 LocationSummary::kNoCall,
1652 kIntrinsified);
1653 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1654 locations->SetInAt(1, Location::RequiresRegister());
1655 // Offset is a long, but in 32 bit mode, we only need the low word.
1656 // Can we update the invoke here to remove a TypeConvert to Long?
1657 locations->SetInAt(2, Location::RequiresRegister());
1658 // Expected value must be in EAX or EDX:EAX.
1659 // For long, new value must be in ECX:EBX.
1660 if (type == Primitive::kPrimLong) {
1661 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
1662 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
1663 } else {
1664 locations->SetInAt(3, Location::RegisterLocation(EAX));
1665 locations->SetInAt(4, Location::RequiresRegister());
1666 }
1667
1668 // Force a byte register for the output.
1669 locations->SetOut(Location::RegisterLocation(EAX));
1670 if (type == Primitive::kPrimNot) {
1671 // Need temp registers for card-marking.
1672 locations->AddTemp(Location::RequiresRegister());
1673 // Need a byte register for marking.
1674 locations->AddTemp(Location::RegisterLocation(ECX));
1675 }
1676}
1677
1678void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
1679 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
1680}
1681
1682void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
1683 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
1684}
1685
1686void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
1687 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
1688}
1689
1690static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
1691 X86Assembler* assembler =
1692 reinterpret_cast<X86Assembler*>(codegen->GetAssembler());
1693 LocationSummary* locations = invoke->GetLocations();
1694
1695 Register base = locations->InAt(1).AsRegister<Register>();
1696 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1697 Location out = locations->Out();
1698 DCHECK_EQ(out.AsRegister<Register>(), EAX);
1699
1700 if (type == Primitive::kPrimLong) {
1701 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
1702 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
1703 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
1704 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
1705 __ LockCmpxchg8b(Address(base, offset, TIMES_1, 0));
1706 } else {
1707 // Integer or object.
Roland Levillain4d027112015-07-01 15:41:14 +01001708 Register expected = locations->InAt(3).AsRegister<Register>();
1709 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001710 Register value = locations->InAt(4).AsRegister<Register>();
1711 if (type == Primitive::kPrimNot) {
1712 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001713 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell58d25fd2015-04-03 14:52:31 -04001714 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
1715 locations->GetTemp(1).AsRegister<Register>(),
1716 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001717 value,
1718 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01001719
1720 if (kPoisonHeapReferences) {
1721 __ PoisonHeapReference(expected);
1722 __ PoisonHeapReference(value);
1723 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001724 }
1725
1726 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
1727 }
1728
1729 // locked cmpxchg has full barrier semantics, and we don't need scheduling
1730 // barriers at this time.
1731
1732 // Convert ZF into the boolean result.
1733 __ setb(kZero, out.AsRegister<Register>());
1734 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01001735
1736 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1737 Register value = locations->InAt(4).AsRegister<Register>();
1738 __ UnpoisonHeapReference(value);
1739 // Do not unpoison the reference contained in register `expected`,
1740 // as it is the same as register `out`.
1741 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001742}
1743
1744void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
1745 GenCAS(Primitive::kPrimInt, invoke, codegen_);
1746}
1747
1748void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
1749 GenCAS(Primitive::kPrimLong, invoke, codegen_);
1750}
1751
1752void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
1753 GenCAS(Primitive::kPrimNot, invoke, codegen_);
1754}
1755
1756void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
1757 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1758 LocationSummary::kNoCall,
1759 kIntrinsified);
1760 locations->SetInAt(0, Location::RequiresRegister());
1761 locations->SetOut(Location::SameAsFirstInput());
1762 locations->AddTemp(Location::RequiresRegister());
1763}
1764
1765static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
1766 X86Assembler* assembler) {
1767 Immediate imm_shift(shift);
1768 Immediate imm_mask(mask);
1769 __ movl(temp, reg);
1770 __ shrl(reg, imm_shift);
1771 __ andl(temp, imm_mask);
1772 __ andl(reg, imm_mask);
1773 __ shll(temp, imm_shift);
1774 __ orl(reg, temp);
1775}
1776
1777void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
1778 X86Assembler* assembler =
1779 reinterpret_cast<X86Assembler*>(codegen_->GetAssembler());
1780 LocationSummary* locations = invoke->GetLocations();
1781
1782 Register reg = locations->InAt(0).AsRegister<Register>();
1783 Register temp = locations->GetTemp(0).AsRegister<Register>();
1784
1785 /*
1786 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
1787 * swapping bits to reverse bits in a number x. Using bswap to save instructions
1788 * compared to generic luni implementation which has 5 rounds of swapping bits.
1789 * x = bswap x
1790 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
1791 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
1792 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
1793 */
1794 __ bswapl(reg);
1795 SwapBits(reg, temp, 1, 0x55555555, assembler);
1796 SwapBits(reg, temp, 2, 0x33333333, assembler);
1797 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
1798}
1799
1800void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
1801 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1802 LocationSummary::kNoCall,
1803 kIntrinsified);
1804 locations->SetInAt(0, Location::RequiresRegister());
1805 locations->SetOut(Location::SameAsFirstInput());
1806 locations->AddTemp(Location::RequiresRegister());
1807}
1808
1809void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
1810 X86Assembler* assembler =
1811 reinterpret_cast<X86Assembler*>(codegen_->GetAssembler());
1812 LocationSummary* locations = invoke->GetLocations();
1813
1814 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
1815 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1816 Register temp = locations->GetTemp(0).AsRegister<Register>();
1817
1818 // We want to swap high/low, then bswap each one, and then do the same
1819 // as a 32 bit reverse.
1820 // Exchange high and low.
1821 __ movl(temp, reg_low);
1822 __ movl(reg_low, reg_high);
1823 __ movl(reg_high, temp);
1824
1825 // bit-reverse low
1826 __ bswapl(reg_low);
1827 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
1828 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
1829 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
1830
1831 // bit-reverse high
1832 __ bswapl(reg_high);
1833 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
1834 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
1835 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
1836}
1837
Mark Mendelld5897672015-08-12 21:16:41 -04001838static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_long) {
1839 LocationSummary* locations = new (arena) LocationSummary(invoke,
1840 LocationSummary::kNoCall,
1841 kIntrinsified);
1842 if (is_long) {
1843 locations->SetInAt(0, Location::RequiresRegister());
1844 } else {
1845 locations->SetInAt(0, Location::Any());
1846 }
1847 locations->SetOut(Location::RequiresRegister());
1848}
1849
1850static void GenLeadingZeros(X86Assembler* assembler, HInvoke* invoke, bool is_long) {
1851 LocationSummary* locations = invoke->GetLocations();
1852 Location src = locations->InAt(0);
1853 Register out = locations->Out().AsRegister<Register>();
1854
1855 if (invoke->InputAt(0)->IsConstant()) {
1856 // Evaluate this at compile time.
1857 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
1858 if (value == 0) {
1859 value = is_long ? 64 : 32;
1860 } else {
1861 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
1862 }
1863 if (value == 0) {
1864 __ xorl(out, out);
1865 } else {
1866 __ movl(out, Immediate(value));
1867 }
1868 return;
1869 }
1870
1871 // Handle the non-constant cases.
1872 if (!is_long) {
1873 if (src.IsRegister()) {
1874 __ bsrl(out, src.AsRegister<Register>());
1875 } else {
1876 DCHECK(src.IsStackSlot());
1877 __ bsrl(out, Address(ESP, src.GetStackIndex()));
1878 }
1879
1880 // BSR sets ZF if the input was zero, and the output is undefined.
1881 Label all_zeroes, done;
1882 __ j(kEqual, &all_zeroes);
1883
1884 // Correct the result from BSR to get the final CLZ result.
1885 __ xorl(out, Immediate(31));
1886 __ jmp(&done);
1887
1888 // Fix the zero case with the expected result.
1889 __ Bind(&all_zeroes);
1890 __ movl(out, Immediate(32));
1891
1892 __ Bind(&done);
1893 return;
1894 }
1895
1896 // 64 bit case needs to worry about both parts of the register.
1897 DCHECK(src.IsRegisterPair());
1898 Register src_lo = src.AsRegisterPairLow<Register>();
1899 Register src_hi = src.AsRegisterPairHigh<Register>();
1900 Label handle_low, done, all_zeroes;
1901
1902 // Is the high word zero?
1903 __ testl(src_hi, src_hi);
1904 __ j(kEqual, &handle_low);
1905
1906 // High word is not zero. We know that the BSR result is defined in this case.
1907 __ bsrl(out, src_hi);
1908
1909 // Correct the result from BSR to get the final CLZ result.
1910 __ xorl(out, Immediate(31));
1911 __ jmp(&done);
1912
1913 // High word was zero. We have to compute the low word count and add 32.
1914 __ Bind(&handle_low);
1915 __ bsrl(out, src_lo);
1916 __ j(kEqual, &all_zeroes);
1917
1918 // We had a valid result. Use an XOR to both correct the result and add 32.
1919 __ xorl(out, Immediate(63));
1920 __ jmp(&done);
1921
1922 // All zero case.
1923 __ Bind(&all_zeroes);
1924 __ movl(out, Immediate(64));
1925
1926 __ Bind(&done);
1927}
1928
1929void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
1930 CreateLeadingZeroLocations(arena_, invoke, /* is_long */ false);
1931}
1932
1933void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
1934 X86Assembler* assembler = down_cast<X86Assembler*>(codegen_->GetAssembler());
1935 GenLeadingZeros(assembler, invoke, /* is_long */ false);
1936}
1937
1938void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
1939 CreateLeadingZeroLocations(arena_, invoke, /* is_long */ true);
1940}
1941
1942void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
1943 X86Assembler* assembler = down_cast<X86Assembler*>(codegen_->GetAssembler());
1944 GenLeadingZeros(assembler, invoke, /* is_long */ true);
1945}
1946
Mark Mendell09ed1a32015-03-25 08:30:06 -04001947// Unimplemented intrinsics.
1948
1949#define UNIMPLEMENTED_INTRINSIC(Name) \
1950void IntrinsicLocationsBuilderX86::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1951} \
1952void IntrinsicCodeGeneratorX86::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1953}
1954
Mark Mendell09ed1a32015-03-25 08:30:06 -04001955UNIMPLEMENTED_INTRINSIC(MathRoundDouble)
Jeff Hao848f70a2014-01-15 13:49:50 -08001956UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
Mark Mendell09ed1a32015-03-25 08:30:06 -04001957UNIMPLEMENTED_INTRINSIC(SystemArrayCopyChar)
Mark Mendell09ed1a32015-03-25 08:30:06 -04001958UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
1959
Roland Levillain4d027112015-07-01 15:41:14 +01001960#undef UNIMPLEMENTED_INTRINSIC
1961
1962#undef __
1963
Mark Mendell09ed1a32015-03-25 08:30:06 -04001964} // namespace x86
1965} // namespace art