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