blob: a86a8bfd7452b24fe6526b788473fb36360e88c1 [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 Mendell09ed1a32015-03-25 08:30:06 -040023#include "code_generator_x86.h"
24#include "entrypoints/quick/quick_entrypoints.h"
25#include "intrinsics.h"
26#include "mirror/array-inl.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040027#include "mirror/string.h"
28#include "thread.h"
29#include "utils/x86/assembler_x86.h"
30#include "utils/x86/constants_x86.h"
31
32namespace art {
33
34namespace x86 {
35
36static constexpr int kDoubleNaNHigh = 0x7FF80000;
37static constexpr int kDoubleNaNLow = 0x00000000;
38static constexpr int kFloatNaN = 0x7FC00000;
39
Mark Mendellfb8d2792015-03-31 22:16:59 -040040IntrinsicLocationsBuilderX86::IntrinsicLocationsBuilderX86(CodeGeneratorX86* codegen)
41 : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) {
42}
43
44
Mark Mendell09ed1a32015-03-25 08:30:06 -040045X86Assembler* IntrinsicCodeGeneratorX86::GetAssembler() {
46 return reinterpret_cast<X86Assembler*>(codegen_->GetAssembler());
47}
48
49ArenaAllocator* IntrinsicCodeGeneratorX86::GetAllocator() {
50 return codegen_->GetGraph()->GetArena();
51}
52
53bool IntrinsicLocationsBuilderX86::TryDispatch(HInvoke* invoke) {
54 Dispatch(invoke);
55 LocationSummary* res = invoke->GetLocations();
56 return res != nullptr && res->Intrinsified();
57}
58
59#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
60
61// TODO: target as memory.
62static void MoveFromReturnRegister(Location target,
63 Primitive::Type type,
64 CodeGeneratorX86* codegen) {
65 if (!target.IsValid()) {
66 DCHECK(type == Primitive::kPrimVoid);
67 return;
68 }
69
70 switch (type) {
71 case Primitive::kPrimBoolean:
72 case Primitive::kPrimByte:
73 case Primitive::kPrimChar:
74 case Primitive::kPrimShort:
75 case Primitive::kPrimInt:
76 case Primitive::kPrimNot: {
77 Register target_reg = target.AsRegister<Register>();
78 if (target_reg != EAX) {
79 __ movl(target_reg, EAX);
80 }
81 break;
82 }
83 case Primitive::kPrimLong: {
84 Register target_reg_lo = target.AsRegisterPairLow<Register>();
85 Register target_reg_hi = target.AsRegisterPairHigh<Register>();
86 if (target_reg_lo != EAX) {
87 __ movl(target_reg_lo, EAX);
88 }
89 if (target_reg_hi != EDX) {
90 __ movl(target_reg_hi, EDX);
91 }
92 break;
93 }
94
95 case Primitive::kPrimVoid:
96 LOG(FATAL) << "Unexpected void type for valid location " << target;
97 UNREACHABLE();
98
99 case Primitive::kPrimDouble: {
100 XmmRegister target_reg = target.AsFpuRegister<XmmRegister>();
101 if (target_reg != XMM0) {
102 __ movsd(target_reg, XMM0);
103 }
104 break;
105 }
106 case Primitive::kPrimFloat: {
107 XmmRegister target_reg = target.AsFpuRegister<XmmRegister>();
108 if (target_reg != XMM0) {
109 __ movss(target_reg, XMM0);
110 }
111 break;
112 }
113 }
114}
115
Roland Levillainec525fc2015-04-28 15:50:20 +0100116static void MoveArguments(HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100117 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +0100118 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400119}
120
121// Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified
122// call. This will copy the arguments into the positions for a regular call.
123//
124// Note: The actual parameters are required to be in the locations given by the invoke's location
125// summary. If an intrinsic modifies those locations before a slowpath call, they must be
126// restored!
127class IntrinsicSlowPathX86 : public SlowPathCodeX86 {
128 public:
Andreas Gampe21030dd2015-05-07 14:46:15 -0700129 explicit IntrinsicSlowPathX86(HInvoke* invoke)
130 : invoke_(invoke) { }
Mark Mendell09ed1a32015-03-25 08:30:06 -0400131
132 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
133 CodeGeneratorX86* codegen = down_cast<CodeGeneratorX86*>(codegen_in);
134 __ Bind(GetEntryLabel());
135
136 SaveLiveRegisters(codegen, invoke_->GetLocations());
137
Roland Levillainec525fc2015-04-28 15:50:20 +0100138 MoveArguments(invoke_, codegen);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400139
140 if (invoke_->IsInvokeStaticOrDirect()) {
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100141 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(),
142 Location::RegisterLocation(EAX));
Mingyao Yange90db122015-04-03 17:56:54 -0700143 RecordPcInfo(codegen, invoke_, invoke_->GetDexPc());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400144 } else {
145 UNIMPLEMENTED(FATAL) << "Non-direct intrinsic slow-path not yet implemented";
146 UNREACHABLE();
147 }
148
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
Mark Mendell6bc53a92015-07-01 14:26:52 -0400891 // Location of reference to data array.
Mark Mendell09ed1a32015-03-25 08:30:06 -0400892 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Mark Mendell6bc53a92015-07-01 14:26:52 -0400893 // Location of count.
Mark Mendell09ed1a32015-03-25 08:30:06 -0400894 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
Mark Mendell6bc53a92015-07-01 14:26:52 -0400920void IntrinsicLocationsBuilderX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
921 // We need at least two of the positions or length to be an integer constant,
922 // or else we won't have enough free registers.
923 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
924 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
925 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
926
927 int num_constants =
928 ((src_pos != nullptr) ? 1 : 0)
929 + ((dest_pos != nullptr) ? 1 : 0)
930 + ((length != nullptr) ? 1 : 0);
931
932 if (num_constants < 2) {
933 // Not enough free registers.
934 return;
935 }
936
937 // As long as we are checking, we might as well check to see if the src and dest
938 // positions are >= 0.
939 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
940 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
941 // We will have to fail anyways.
942 return;
943 }
944
945 // And since we are already checking, check the length too.
946 if (length != nullptr) {
947 int32_t len = length->GetValue();
948 if (len < 0) {
949 // Just call as normal.
950 return;
951 }
952 }
953
954 // Okay, it is safe to generate inline code.
955 LocationSummary* locations =
956 new (arena_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
957 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
958 locations->SetInAt(0, Location::RequiresRegister());
959 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
960 locations->SetInAt(2, Location::RequiresRegister());
961 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
962 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
963
964 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
965 locations->AddTemp(Location::RegisterLocation(ESI));
966 locations->AddTemp(Location::RegisterLocation(EDI));
967 locations->AddTemp(Location::RegisterLocation(ECX));
968}
969
970static void CheckPosition(X86Assembler* assembler,
971 Location pos,
972 Register input,
973 Register length,
974 SlowPathCodeX86* slow_path,
975 Register input_len,
976 Register temp) {
977 // Where is the length in the String?
978 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
979
980 if (pos.IsConstant()) {
981 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
982 if (pos_const == 0) {
983 // Check that length(input) >= length.
984 __ cmpl(Address(input, length_offset), length);
985 __ j(kLess, slow_path->GetEntryLabel());
986 } else {
987 // Check that length(input) >= pos.
988 __ movl(input_len, Address(input, length_offset));
989 __ cmpl(input_len, Immediate(pos_const));
990 __ j(kLess, slow_path->GetEntryLabel());
991
992 // Check that (length(input) - pos) >= length.
993 __ leal(temp, Address(input_len, -pos_const));
994 __ cmpl(temp, length);
995 __ j(kLess, slow_path->GetEntryLabel());
996 }
997 } else {
998 // Check that pos >= 0.
999 Register pos_reg = pos.AsRegister<Register>();
1000 __ testl(pos_reg, pos_reg);
1001 __ j(kLess, slow_path->GetEntryLabel());
1002
1003 // Check that pos <= length(input).
1004 __ cmpl(Address(input, length_offset), pos_reg);
1005 __ j(kLess, slow_path->GetEntryLabel());
1006
1007 // Check that (length(input) - pos) >= length.
1008 __ movl(temp, Address(input, length_offset));
1009 __ subl(temp, pos_reg);
1010 __ cmpl(temp, length);
1011 __ j(kLess, slow_path->GetEntryLabel());
1012 }
1013}
1014
1015void IntrinsicCodeGeneratorX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
1016 X86Assembler* assembler = GetAssembler();
1017 LocationSummary* locations = invoke->GetLocations();
1018
1019 Register src = locations->InAt(0).AsRegister<Register>();
1020 Location srcPos = locations->InAt(1);
1021 Register dest = locations->InAt(2).AsRegister<Register>();
1022 Location destPos = locations->InAt(3);
1023 Location length = locations->InAt(4);
1024
1025 // Temporaries that we need for MOVSW.
1026 Register src_base = locations->GetTemp(0).AsRegister<Register>();
1027 DCHECK_EQ(src_base, ESI);
1028 Register dest_base = locations->GetTemp(1).AsRegister<Register>();
1029 DCHECK_EQ(dest_base, EDI);
1030 Register count = locations->GetTemp(2).AsRegister<Register>();
1031 DCHECK_EQ(count, ECX);
1032
1033 SlowPathCodeX86* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
1034 codegen_->AddSlowPath(slow_path);
1035
1036 // Bail out if the source and destination are the same (to handle overlap).
1037 __ cmpl(src, dest);
1038 __ j(kEqual, slow_path->GetEntryLabel());
1039
1040 // Bail out if the source is null.
1041 __ testl(src, src);
1042 __ j(kEqual, slow_path->GetEntryLabel());
1043
1044 // Bail out if the destination is null.
1045 __ testl(dest, dest);
1046 __ j(kEqual, slow_path->GetEntryLabel());
1047
1048 // If the length is negative, bail out.
1049 // We have already checked in the LocationsBuilder for the constant case.
1050 if (!length.IsConstant()) {
1051 __ cmpl(length.AsRegister<Register>(), length.AsRegister<Register>());
1052 __ j(kLess, slow_path->GetEntryLabel());
1053 }
1054
1055 // We need the count in ECX.
1056 if (length.IsConstant()) {
1057 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1058 } else {
1059 __ movl(count, length.AsRegister<Register>());
1060 }
1061
1062 // Validity checks: source.
1063 CheckPosition(assembler, srcPos, src, count, slow_path, src_base, dest_base);
1064
1065 // Validity checks: dest.
1066 CheckPosition(assembler, destPos, dest, count, slow_path, src_base, dest_base);
1067
1068 // Okay, everything checks out. Finally time to do the copy.
1069 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1070 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1071 DCHECK_EQ(char_size, 2u);
1072
1073 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1074
1075 if (srcPos.IsConstant()) {
1076 int32_t srcPos_const = srcPos.GetConstant()->AsIntConstant()->GetValue();
1077 __ leal(src_base, Address(src, char_size * srcPos_const + data_offset));
1078 } else {
1079 __ leal(src_base, Address(src, srcPos.AsRegister<Register>(),
1080 ScaleFactor::TIMES_2, data_offset));
1081 }
1082 if (destPos.IsConstant()) {
1083 int32_t destPos_const = destPos.GetConstant()->AsIntConstant()->GetValue();
1084
1085 __ leal(dest_base, Address(dest, char_size * destPos_const + data_offset));
1086 } else {
1087 __ leal(dest_base, Address(dest, destPos.AsRegister<Register>(),
1088 ScaleFactor::TIMES_2, data_offset));
1089 }
1090
1091 // Do the move.
1092 __ rep_movsw();
1093
1094 __ Bind(slow_path->GetExitLabel());
1095}
1096
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001097void IntrinsicLocationsBuilderX86::VisitStringCompareTo(HInvoke* invoke) {
1098 // The inputs plus one temp.
1099 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1100 LocationSummary::kCall,
1101 kIntrinsified);
1102 InvokeRuntimeCallingConvention calling_convention;
1103 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1104 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1105 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001106}
1107
1108void IntrinsicCodeGeneratorX86::VisitStringCompareTo(HInvoke* invoke) {
1109 X86Assembler* assembler = GetAssembler();
1110 LocationSummary* locations = invoke->GetLocations();
1111
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001112 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001113 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001114
1115 Register argument = locations->InAt(1).AsRegister<Register>();
1116 __ testl(argument, argument);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001117 SlowPathCodeX86* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001118 codegen_->AddSlowPath(slow_path);
1119 __ j(kEqual, slow_path->GetEntryLabel());
1120
1121 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pStringCompareTo)));
1122 __ Bind(slow_path->GetExitLabel());
1123}
1124
Andreas Gampe21030dd2015-05-07 14:46:15 -07001125static void CreateStringIndexOfLocations(HInvoke* invoke,
1126 ArenaAllocator* allocator,
1127 bool start_at_zero) {
1128 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1129 LocationSummary::kCallOnSlowPath,
1130 kIntrinsified);
1131 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1132 locations->SetInAt(0, Location::RegisterLocation(EDI));
1133 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1134 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1135 // of the instruction explicitly.
1136 // Note: This works as we don't clobber EAX anywhere.
1137 locations->SetInAt(1, Location::RegisterLocation(EAX));
1138 if (!start_at_zero) {
1139 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1140 }
1141 // As we clobber EDI during execution anyways, also use it as the output.
1142 locations->SetOut(Location::SameAsFirstInput());
1143
1144 // repne scasw uses ECX as the counter.
1145 locations->AddTemp(Location::RegisterLocation(ECX));
1146 // Need another temporary to be able to compute the result.
1147 locations->AddTemp(Location::RequiresRegister());
1148}
1149
1150static void GenerateStringIndexOf(HInvoke* invoke,
1151 X86Assembler* assembler,
1152 CodeGeneratorX86* codegen,
1153 ArenaAllocator* allocator,
1154 bool start_at_zero) {
1155 LocationSummary* locations = invoke->GetLocations();
1156
1157 // Note that the null check must have been done earlier.
1158 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1159
1160 Register string_obj = locations->InAt(0).AsRegister<Register>();
1161 Register search_value = locations->InAt(1).AsRegister<Register>();
1162 Register counter = locations->GetTemp(0).AsRegister<Register>();
1163 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1164 Register out = locations->Out().AsRegister<Register>();
1165
1166 // Check our assumptions for registers.
1167 DCHECK_EQ(string_obj, EDI);
1168 DCHECK_EQ(search_value, EAX);
1169 DCHECK_EQ(counter, ECX);
1170 DCHECK_EQ(out, EDI);
1171
1172 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1173 // or directly dispatch if we have a constant.
1174 SlowPathCodeX86* slow_path = nullptr;
1175 if (invoke->InputAt(1)->IsIntConstant()) {
1176 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1177 std::numeric_limits<uint16_t>::max()) {
1178 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1179 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1180 slow_path = new (allocator) IntrinsicSlowPathX86(invoke);
1181 codegen->AddSlowPath(slow_path);
1182 __ jmp(slow_path->GetEntryLabel());
1183 __ Bind(slow_path->GetExitLabel());
1184 return;
1185 }
1186 } else {
1187 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1188 slow_path = new (allocator) IntrinsicSlowPathX86(invoke);
1189 codegen->AddSlowPath(slow_path);
1190 __ j(kAbove, slow_path->GetEntryLabel());
1191 }
1192
1193 // From here down, we know that we are looking for a char that fits in 16 bits.
1194 // Location of reference to data array within the String object.
1195 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1196 // Location of count within the String object.
1197 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1198
1199 // Load string length, i.e., the count field of the string.
1200 __ movl(string_length, Address(string_obj, count_offset));
1201
1202 // Do a zero-length check.
1203 // TODO: Support jecxz.
1204 Label not_found_label;
1205 __ testl(string_length, string_length);
1206 __ j(kEqual, &not_found_label);
1207
1208 if (start_at_zero) {
1209 // Number of chars to scan is the same as the string length.
1210 __ movl(counter, string_length);
1211
1212 // Move to the start of the string.
1213 __ addl(string_obj, Immediate(value_offset));
1214 } else {
1215 Register start_index = locations->InAt(2).AsRegister<Register>();
1216
1217 // Do a start_index check.
1218 __ cmpl(start_index, string_length);
1219 __ j(kGreaterEqual, &not_found_label);
1220
1221 // Ensure we have a start index >= 0;
1222 __ xorl(counter, counter);
1223 __ cmpl(start_index, Immediate(0));
1224 __ cmovl(kGreater, counter, start_index);
1225
1226 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1227 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1228
1229 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1230 // compare.
1231 __ negl(counter);
1232 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1233 }
1234
1235 // Everything is set up for repne scasw:
1236 // * Comparison address in EDI.
1237 // * Counter in ECX.
1238 __ repne_scasw();
1239
1240 // Did we find a match?
1241 __ j(kNotEqual, &not_found_label);
1242
1243 // Yes, we matched. Compute the index of the result.
1244 __ subl(string_length, counter);
1245 __ leal(out, Address(string_length, -1));
1246
1247 Label done;
1248 __ jmp(&done);
1249
1250 // Failed to match; return -1.
1251 __ Bind(&not_found_label);
1252 __ movl(out, Immediate(-1));
1253
1254 // And join up at the end.
1255 __ Bind(&done);
1256 if (slow_path != nullptr) {
1257 __ Bind(slow_path->GetExitLabel());
1258 }
1259}
1260
1261void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
1262 CreateStringIndexOfLocations(invoke, arena_, true);
1263}
1264
1265void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
1266 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1267}
1268
1269void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
1270 CreateStringIndexOfLocations(invoke, arena_, false);
1271}
1272
1273void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
1274 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1275}
1276
Jeff Hao848f70a2014-01-15 13:49:50 -08001277void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1278 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1279 LocationSummary::kCall,
1280 kIntrinsified);
1281 InvokeRuntimeCallingConvention calling_convention;
1282 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1283 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1284 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1285 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1286 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001287}
1288
1289void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1290 X86Assembler* assembler = GetAssembler();
1291 LocationSummary* locations = invoke->GetLocations();
1292
1293 Register byte_array = locations->InAt(0).AsRegister<Register>();
1294 __ testl(byte_array, byte_array);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001295 SlowPathCodeX86* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001296 codegen_->AddSlowPath(slow_path);
1297 __ j(kEqual, slow_path->GetEntryLabel());
1298
1299 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocStringFromBytes)));
1300 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1301 __ Bind(slow_path->GetExitLabel());
1302}
1303
1304void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
1305 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1306 LocationSummary::kCall,
1307 kIntrinsified);
1308 InvokeRuntimeCallingConvention calling_convention;
1309 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1310 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1311 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1312 locations->SetOut(Location::RegisterLocation(EAX));
1313}
1314
1315void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
1316 X86Assembler* assembler = GetAssembler();
1317
1318 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocStringFromChars)));
1319 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1320}
1321
1322void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
1323 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1324 LocationSummary::kCall,
1325 kIntrinsified);
1326 InvokeRuntimeCallingConvention calling_convention;
1327 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1328 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001329}
1330
1331void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1332 X86Assembler* assembler = GetAssembler();
1333 LocationSummary* locations = invoke->GetLocations();
1334
1335 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1336 __ testl(string_to_copy, string_to_copy);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001337 SlowPathCodeX86* slow_path = new (GetAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001338 codegen_->AddSlowPath(slow_path);
1339 __ j(kEqual, slow_path->GetEntryLabel());
1340
1341 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocStringFromString)));
1342 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1343 __ Bind(slow_path->GetExitLabel());
1344}
1345
Mark Mendell09ed1a32015-03-25 08:30:06 -04001346static void GenPeek(LocationSummary* locations, Primitive::Type size, X86Assembler* assembler) {
1347 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1348 Location out_loc = locations->Out();
1349 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1350 // to avoid a SIGBUS.
1351 switch (size) {
1352 case Primitive::kPrimByte:
1353 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1354 break;
1355 case Primitive::kPrimShort:
1356 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1357 break;
1358 case Primitive::kPrimInt:
1359 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1360 break;
1361 case Primitive::kPrimLong:
1362 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1363 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1364 break;
1365 default:
1366 LOG(FATAL) << "Type not recognized for peek: " << size;
1367 UNREACHABLE();
1368 }
1369}
1370
1371void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
1372 CreateLongToIntLocations(arena_, invoke);
1373}
1374
1375void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
1376 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1377}
1378
1379void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
1380 CreateLongToIntLocations(arena_, invoke);
1381}
1382
1383void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
1384 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1385}
1386
1387void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
1388 CreateLongToLongLocations(arena_, invoke);
1389}
1390
1391void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
1392 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1393}
1394
1395void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
1396 CreateLongToIntLocations(arena_, invoke);
1397}
1398
1399void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
1400 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1401}
1402
1403static void CreateLongIntToVoidLocations(ArenaAllocator* arena, Primitive::Type size,
1404 HInvoke* invoke) {
1405 LocationSummary* locations = new (arena) LocationSummary(invoke,
1406 LocationSummary::kNoCall,
1407 kIntrinsified);
1408 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001409 HInstruction* value = invoke->InputAt(1);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001410 if (size == Primitive::kPrimByte) {
1411 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1412 } else {
1413 locations->SetInAt(1, Location::RegisterOrConstant(value));
1414 }
1415}
1416
1417static void GenPoke(LocationSummary* locations, Primitive::Type size, X86Assembler* assembler) {
1418 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1419 Location value_loc = locations->InAt(1);
1420 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1421 // to avoid a SIGBUS.
1422 switch (size) {
1423 case Primitive::kPrimByte:
1424 if (value_loc.IsConstant()) {
1425 __ movb(Address(address, 0),
1426 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1427 } else {
1428 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1429 }
1430 break;
1431 case Primitive::kPrimShort:
1432 if (value_loc.IsConstant()) {
1433 __ movw(Address(address, 0),
1434 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1435 } else {
1436 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1437 }
1438 break;
1439 case Primitive::kPrimInt:
1440 if (value_loc.IsConstant()) {
1441 __ movl(Address(address, 0),
1442 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1443 } else {
1444 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1445 }
1446 break;
1447 case Primitive::kPrimLong:
1448 if (value_loc.IsConstant()) {
1449 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1450 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1451 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1452 } else {
1453 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1454 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1455 }
1456 break;
1457 default:
1458 LOG(FATAL) << "Type not recognized for poke: " << size;
1459 UNREACHABLE();
1460 }
1461}
1462
1463void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
1464 CreateLongIntToVoidLocations(arena_, Primitive::kPrimByte, invoke);
1465}
1466
1467void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
1468 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1469}
1470
1471void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
1472 CreateLongIntToVoidLocations(arena_, Primitive::kPrimInt, invoke);
1473}
1474
1475void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
1476 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1477}
1478
1479void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
1480 CreateLongIntToVoidLocations(arena_, Primitive::kPrimLong, invoke);
1481}
1482
1483void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
1484 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1485}
1486
1487void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
1488 CreateLongIntToVoidLocations(arena_, Primitive::kPrimShort, invoke);
1489}
1490
1491void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
1492 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1493}
1494
1495void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
1496 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1497 LocationSummary::kNoCall,
1498 kIntrinsified);
1499 locations->SetOut(Location::RequiresRegister());
1500}
1501
1502void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1503 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
1504 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86WordSize>()));
1505}
1506
1507static void GenUnsafeGet(LocationSummary* locations, Primitive::Type type,
1508 bool is_volatile, X86Assembler* assembler) {
1509 Register base = locations->InAt(1).AsRegister<Register>();
1510 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1511 Location output = locations->Out();
1512
1513 switch (type) {
1514 case Primitive::kPrimInt:
Roland Levillain4d027112015-07-01 15:41:14 +01001515 case Primitive::kPrimNot: {
1516 Register output_reg = output.AsRegister<Register>();
1517 __ movl(output_reg, Address(base, offset, ScaleFactor::TIMES_1, 0));
1518 if (type == Primitive::kPrimNot) {
1519 __ MaybeUnpoisonHeapReference(output_reg);
1520 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001521 break;
Roland Levillain4d027112015-07-01 15:41:14 +01001522 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001523
1524 case Primitive::kPrimLong: {
1525 Register output_lo = output.AsRegisterPairLow<Register>();
1526 Register output_hi = output.AsRegisterPairHigh<Register>();
1527 if (is_volatile) {
1528 // Need to use a XMM to read atomically.
1529 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1530 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
1531 __ movd(output_lo, temp);
1532 __ psrlq(temp, Immediate(32));
1533 __ movd(output_hi, temp);
1534 } else {
1535 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
1536 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
1537 }
1538 }
1539 break;
1540
1541 default:
1542 LOG(FATAL) << "Unsupported op size " << type;
1543 UNREACHABLE();
1544 }
1545}
1546
1547static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke,
1548 bool is_long, bool is_volatile) {
1549 LocationSummary* locations = new (arena) LocationSummary(invoke,
1550 LocationSummary::kNoCall,
1551 kIntrinsified);
1552 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1553 locations->SetInAt(1, Location::RequiresRegister());
1554 locations->SetInAt(2, Location::RequiresRegister());
1555 if (is_long) {
1556 if (is_volatile) {
1557 // Need to use XMM to read volatile.
1558 locations->AddTemp(Location::RequiresFpuRegister());
1559 locations->SetOut(Location::RequiresRegister());
1560 } else {
1561 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1562 }
1563 } else {
1564 locations->SetOut(Location::RequiresRegister());
1565 }
1566}
1567
1568void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
1569 CreateIntIntIntToIntLocations(arena_, invoke, false, false);
1570}
1571void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
1572 CreateIntIntIntToIntLocations(arena_, invoke, false, true);
1573}
1574void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
1575 CreateIntIntIntToIntLocations(arena_, invoke, false, false);
1576}
1577void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1578 CreateIntIntIntToIntLocations(arena_, invoke, true, true);
1579}
1580void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
1581 CreateIntIntIntToIntLocations(arena_, invoke, false, false);
1582}
1583void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1584 CreateIntIntIntToIntLocations(arena_, invoke, false, true);
1585}
1586
1587
1588void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
1589 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, false, GetAssembler());
1590}
1591void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
1592 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, true, GetAssembler());
1593}
1594void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
1595 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, false, GetAssembler());
1596}
1597void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1598 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, true, GetAssembler());
1599}
1600void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
1601 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, false, GetAssembler());
1602}
1603void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1604 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, true, GetAssembler());
1605}
1606
1607
1608static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1609 Primitive::Type type,
1610 HInvoke* invoke,
1611 bool is_volatile) {
1612 LocationSummary* locations = new (arena) LocationSummary(invoke,
1613 LocationSummary::kNoCall,
1614 kIntrinsified);
1615 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1616 locations->SetInAt(1, Location::RequiresRegister());
1617 locations->SetInAt(2, Location::RequiresRegister());
1618 locations->SetInAt(3, Location::RequiresRegister());
1619 if (type == Primitive::kPrimNot) {
1620 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001621 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04001622 // Ensure the value is in a byte register.
1623 locations->AddTemp(Location::RegisterLocation(ECX));
1624 } else if (type == Primitive::kPrimLong && is_volatile) {
1625 locations->AddTemp(Location::RequiresFpuRegister());
1626 locations->AddTemp(Location::RequiresFpuRegister());
1627 }
1628}
1629
1630void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
1631 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke, false);
1632}
1633void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
1634 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke, false);
1635}
1636void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
1637 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke, true);
1638}
1639void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
1640 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke, false);
1641}
1642void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1643 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke, false);
1644}
1645void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1646 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke, true);
1647}
1648void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
1649 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke, false);
1650}
1651void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1652 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke, false);
1653}
1654void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1655 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke, true);
1656}
1657
1658// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1659// memory model.
1660static void GenUnsafePut(LocationSummary* locations,
1661 Primitive::Type type,
1662 bool is_volatile,
1663 CodeGeneratorX86* codegen) {
1664 X86Assembler* assembler = reinterpret_cast<X86Assembler*>(codegen->GetAssembler());
1665 Register base = locations->InAt(1).AsRegister<Register>();
1666 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1667 Location value_loc = locations->InAt(3);
1668
1669 if (type == Primitive::kPrimLong) {
1670 Register value_lo = value_loc.AsRegisterPairLow<Register>();
1671 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
1672 if (is_volatile) {
1673 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1674 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1675 __ movd(temp1, value_lo);
1676 __ movd(temp2, value_hi);
1677 __ punpckldq(temp1, temp2);
1678 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
1679 } else {
1680 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
1681 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
1682 }
Roland Levillain4d027112015-07-01 15:41:14 +01001683 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1684 Register temp = locations->GetTemp(0).AsRegister<Register>();
1685 __ movl(temp, value_loc.AsRegister<Register>());
1686 __ PoisonHeapReference(temp);
1687 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001688 } else {
1689 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
1690 }
1691
1692 if (is_volatile) {
1693 __ mfence();
1694 }
1695
1696 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001697 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04001698 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
1699 locations->GetTemp(1).AsRegister<Register>(),
1700 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001701 value_loc.AsRegister<Register>(),
1702 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001703 }
1704}
1705
1706void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
1707 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1708}
1709void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
1710 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1711}
1712void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
1713 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_);
1714}
1715void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
1716 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1717}
1718void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1719 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1720}
1721void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1722 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_);
1723}
1724void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
1725 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1726}
1727void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1728 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1729}
1730void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1731 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_);
1732}
1733
Mark Mendell58d25fd2015-04-03 14:52:31 -04001734static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
1735 HInvoke* invoke) {
1736 LocationSummary* locations = new (arena) LocationSummary(invoke,
1737 LocationSummary::kNoCall,
1738 kIntrinsified);
1739 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1740 locations->SetInAt(1, Location::RequiresRegister());
1741 // Offset is a long, but in 32 bit mode, we only need the low word.
1742 // Can we update the invoke here to remove a TypeConvert to Long?
1743 locations->SetInAt(2, Location::RequiresRegister());
1744 // Expected value must be in EAX or EDX:EAX.
1745 // For long, new value must be in ECX:EBX.
1746 if (type == Primitive::kPrimLong) {
1747 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
1748 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
1749 } else {
1750 locations->SetInAt(3, Location::RegisterLocation(EAX));
1751 locations->SetInAt(4, Location::RequiresRegister());
1752 }
1753
1754 // Force a byte register for the output.
1755 locations->SetOut(Location::RegisterLocation(EAX));
1756 if (type == Primitive::kPrimNot) {
1757 // Need temp registers for card-marking.
1758 locations->AddTemp(Location::RequiresRegister());
1759 // Need a byte register for marking.
1760 locations->AddTemp(Location::RegisterLocation(ECX));
1761 }
1762}
1763
1764void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
1765 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
1766}
1767
1768void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
1769 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
1770}
1771
1772void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
1773 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
1774}
1775
1776static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
1777 X86Assembler* assembler =
1778 reinterpret_cast<X86Assembler*>(codegen->GetAssembler());
1779 LocationSummary* locations = invoke->GetLocations();
1780
1781 Register base = locations->InAt(1).AsRegister<Register>();
1782 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1783 Location out = locations->Out();
1784 DCHECK_EQ(out.AsRegister<Register>(), EAX);
1785
1786 if (type == Primitive::kPrimLong) {
1787 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
1788 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
1789 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
1790 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
1791 __ LockCmpxchg8b(Address(base, offset, TIMES_1, 0));
1792 } else {
1793 // Integer or object.
Roland Levillain4d027112015-07-01 15:41:14 +01001794 Register expected = locations->InAt(3).AsRegister<Register>();
1795 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001796 Register value = locations->InAt(4).AsRegister<Register>();
1797 if (type == Primitive::kPrimNot) {
1798 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001799 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell58d25fd2015-04-03 14:52:31 -04001800 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
1801 locations->GetTemp(1).AsRegister<Register>(),
1802 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001803 value,
1804 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01001805
1806 if (kPoisonHeapReferences) {
1807 __ PoisonHeapReference(expected);
1808 __ PoisonHeapReference(value);
1809 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001810 }
1811
1812 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
1813 }
1814
1815 // locked cmpxchg has full barrier semantics, and we don't need scheduling
1816 // barriers at this time.
1817
1818 // Convert ZF into the boolean result.
1819 __ setb(kZero, out.AsRegister<Register>());
1820 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01001821
1822 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1823 Register value = locations->InAt(4).AsRegister<Register>();
1824 __ UnpoisonHeapReference(value);
1825 // Do not unpoison the reference contained in register `expected`,
1826 // as it is the same as register `out`.
1827 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001828}
1829
1830void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
1831 GenCAS(Primitive::kPrimInt, invoke, codegen_);
1832}
1833
1834void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
1835 GenCAS(Primitive::kPrimLong, invoke, codegen_);
1836}
1837
1838void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
1839 GenCAS(Primitive::kPrimNot, invoke, codegen_);
1840}
1841
1842void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
1843 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1844 LocationSummary::kNoCall,
1845 kIntrinsified);
1846 locations->SetInAt(0, Location::RequiresRegister());
1847 locations->SetOut(Location::SameAsFirstInput());
1848 locations->AddTemp(Location::RequiresRegister());
1849}
1850
1851static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
1852 X86Assembler* assembler) {
1853 Immediate imm_shift(shift);
1854 Immediate imm_mask(mask);
1855 __ movl(temp, reg);
1856 __ shrl(reg, imm_shift);
1857 __ andl(temp, imm_mask);
1858 __ andl(reg, imm_mask);
1859 __ shll(temp, imm_shift);
1860 __ orl(reg, temp);
1861}
1862
1863void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
1864 X86Assembler* assembler =
1865 reinterpret_cast<X86Assembler*>(codegen_->GetAssembler());
1866 LocationSummary* locations = invoke->GetLocations();
1867
1868 Register reg = locations->InAt(0).AsRegister<Register>();
1869 Register temp = locations->GetTemp(0).AsRegister<Register>();
1870
1871 /*
1872 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
1873 * swapping bits to reverse bits in a number x. Using bswap to save instructions
1874 * compared to generic luni implementation which has 5 rounds of swapping bits.
1875 * x = bswap x
1876 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
1877 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
1878 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
1879 */
1880 __ bswapl(reg);
1881 SwapBits(reg, temp, 1, 0x55555555, assembler);
1882 SwapBits(reg, temp, 2, 0x33333333, assembler);
1883 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
1884}
1885
1886void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
1887 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1888 LocationSummary::kNoCall,
1889 kIntrinsified);
1890 locations->SetInAt(0, Location::RequiresRegister());
1891 locations->SetOut(Location::SameAsFirstInput());
1892 locations->AddTemp(Location::RequiresRegister());
1893}
1894
1895void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
1896 X86Assembler* assembler =
1897 reinterpret_cast<X86Assembler*>(codegen_->GetAssembler());
1898 LocationSummary* locations = invoke->GetLocations();
1899
1900 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
1901 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1902 Register temp = locations->GetTemp(0).AsRegister<Register>();
1903
1904 // We want to swap high/low, then bswap each one, and then do the same
1905 // as a 32 bit reverse.
1906 // Exchange high and low.
1907 __ movl(temp, reg_low);
1908 __ movl(reg_low, reg_high);
1909 __ movl(reg_high, temp);
1910
1911 // bit-reverse low
1912 __ bswapl(reg_low);
1913 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
1914 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
1915 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
1916
1917 // bit-reverse high
1918 __ bswapl(reg_high);
1919 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
1920 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
1921 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
1922}
1923
Mark Mendell09ed1a32015-03-25 08:30:06 -04001924// Unimplemented intrinsics.
1925
1926#define UNIMPLEMENTED_INTRINSIC(Name) \
1927void IntrinsicLocationsBuilderX86::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1928} \
1929void IntrinsicCodeGeneratorX86::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1930}
1931
Mark Mendell09ed1a32015-03-25 08:30:06 -04001932UNIMPLEMENTED_INTRINSIC(MathRoundDouble)
Jeff Hao848f70a2014-01-15 13:49:50 -08001933UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
Mark Mendell09ed1a32015-03-25 08:30:06 -04001934UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
Scott Wakeling611d3392015-07-10 11:42:06 +01001935UNIMPLEMENTED_INTRINSIC(IntegerNumberOfLeadingZeros)
1936UNIMPLEMENTED_INTRINSIC(LongNumberOfLeadingZeros)
agicsaki7da072f2015-08-12 20:30:17 -07001937UNIMPLEMENTED_INTRINSIC(StringEquals)
Mark Mendell09ed1a32015-03-25 08:30:06 -04001938
Roland Levillain4d027112015-07-01 15:41:14 +01001939#undef UNIMPLEMENTED_INTRINSIC
1940
1941#undef __
1942
Mark Mendell09ed1a32015-03-25 08:30:06 -04001943} // namespace x86
1944} // namespace art