blob: 285003de58ee74b54f27191da79edbf9015554f9 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 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 "code_generator_x86_64.h"
18
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080021#include "intrinsics.h"
22#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070023#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010025#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010026#include "mirror/object_reference.h"
27#include "thread.h"
28#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010029#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/x86_64/assembler_x86_64.h"
31#include "utils/x86_64/managed_register_x86_64.h"
32
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033namespace art {
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace x86_64 {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037// Some x86_64 instructions require a register to be available as temp.
38static constexpr Register TMP = R11;
39
40static constexpr int kNumberOfPushedRegistersAtEntry = 1;
41static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000046static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
47static constexpr size_t kRuntimeParameterFpuRegistersLength =
48 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray98893962015-01-21 12:32:32 +000049static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050
Mark Mendell24f2dfa2015-01-14 19:51:45 -050051static constexpr int kC2ConditionMask = 0x400;
52
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010054 public:
55 InvokeRuntimeCallingConvention()
56 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010057 kRuntimeParameterCoreRegistersLength,
58 kRuntimeParameterFpuRegisters,
59 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010060
61 private:
62 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
63};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010064
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
66
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010067class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010069 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070
71 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
72 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010073 __ gs()->call(
74 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 }
77
78 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
81};
82
Calin Juravled0d48522014-11-04 16:40:20 +000083class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
84 public:
85 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
86
87 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
88 __ Bind(GetEntryLabel());
89 __ gs()->call(
90 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
91 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
92 }
93
94 private:
95 HDivZeroCheck* const instruction_;
96 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
97};
98
Calin Juravlebacfec32014-11-14 15:54:36 +000099class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000101 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
102 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
104 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
105 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000106 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000107 if (is_div_) {
108 __ negl(cpu_reg_);
109 } else {
110 __ movl(cpu_reg_, Immediate(0));
111 }
112
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000113 } else {
114 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000115 if (is_div_) {
116 __ negq(cpu_reg_);
117 } else {
118 __ movq(cpu_reg_, Immediate(0));
119 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000120 }
Calin Juravled0d48522014-11-04 16:40:20 +0000121 __ jmp(GetExitLabel());
122 }
123
124 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 const bool is_div_;
128 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000129};
130
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100132 public:
133 StackOverflowCheckSlowPathX86_64() {}
134
135 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
136 __ Bind(GetEntryLabel());
137 __ addq(CpuRegister(RSP),
138 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
139 __ gs()->jmp(
140 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
141 }
142
143 private:
144 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
145};
146
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
150 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151
152 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100153 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000154 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100155 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
157 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100158 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 if (successor_ == nullptr) {
160 __ jmp(GetReturnLabel());
161 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100162 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 }
165
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 Label* GetReturnLabel() {
167 DCHECK(successor_ == nullptr);
168 return &return_label_;
169 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170
171 private:
172 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
177};
178
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100179class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100181 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
182 Location index_location,
183 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100184 : instruction_(instruction),
185 index_location_(index_location),
186 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187
188 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100189 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000190 // We're moving two locations to locations that could overlap, so we need a parallel
191 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000193 codegen->EmitParallelMoves(
194 index_location_,
195 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
196 length_location_,
197 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198 __ gs()->call(Address::Absolute(
199 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100200 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100201 }
202
203 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100204 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100205 const Location index_location_;
206 const Location length_location_;
207
208 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
209};
210
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 LoadClassSlowPathX86_64(HLoadClass* cls,
214 HInstruction* at,
215 uint32_t dex_pc,
216 bool do_clinit)
217 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
218 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
219 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100220
221 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100223 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
224 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 codegen->SaveLiveRegisters(locations);
227
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 __ gs()->call(Address::Absolute((do_clinit_
232 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
233 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
234 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000236 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000238 if (out.IsValid()) {
239 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
240 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 }
242
243 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100244 __ jmp(GetExitLabel());
245 }
246
247 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 // The class this slow path will load.
249 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251 // The instruction where this slow path is happening.
252 // (Might be the load class or an initialization check).
253 HInstruction* const at_;
254
255 // The dex PC of `at_`.
256 const uint32_t dex_pc_;
257
258 // Whether to initialize the class.
259 const bool do_clinit_;
260
261 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100262};
263
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000264class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
265 public:
266 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
267
268 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
269 LocationSummary* locations = instruction_->GetLocations();
270 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
271
272 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
273 __ Bind(GetEntryLabel());
274 codegen->SaveLiveRegisters(locations);
275
276 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800277 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
278 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000279 Immediate(instruction_->GetStringIndex()));
280 __ gs()->call(Address::Absolute(
281 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
282 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
283 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
284 codegen->RestoreLiveRegisters(locations);
285 __ jmp(GetExitLabel());
286 }
287
288 private:
289 HLoadString* const instruction_;
290
291 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
292};
293
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
295 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 TypeCheckSlowPathX86_64(HInstruction* instruction,
297 Location class_to_check,
298 Location object_class,
299 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000301 class_to_check_(class_to_check),
302 object_class_(object_class),
303 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
305 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
306 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 DCHECK(instruction_->IsCheckCast()
308 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309
310 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
311 __ Bind(GetEntryLabel());
312 codegen->SaveLiveRegisters(locations);
313
314 // We're moving two locations to locations that could overlap, so we need a parallel
315 // move resolver.
316 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000317 codegen->EmitParallelMoves(
318 class_to_check_,
319 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
320 object_class_,
321 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 if (instruction_->IsInstanceOf()) {
324 __ gs()->call(
325 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
326 } else {
327 DCHECK(instruction_->IsCheckCast());
328 __ gs()->call(
329 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
330 }
331 codegen->RecordPcInfo(instruction_, dex_pc_);
332
333 if (instruction_->IsInstanceOf()) {
334 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
335 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336
337 codegen->RestoreLiveRegisters(locations);
338 __ jmp(GetExitLabel());
339 }
340
341 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000342 HInstruction* const instruction_;
343 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000344 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
347 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
348};
349
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100350#undef __
351#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
352
Dave Allison20dfc792014-06-16 20:44:29 -0700353inline Condition X86_64Condition(IfCondition cond) {
354 switch (cond) {
355 case kCondEQ: return kEqual;
356 case kCondNE: return kNotEqual;
357 case kCondLT: return kLess;
358 case kCondLE: return kLessEqual;
359 case kCondGT: return kGreater;
360 case kCondGE: return kGreaterEqual;
361 default:
362 LOG(FATAL) << "Unknown if condition";
363 }
364 return kEqual;
365}
366
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800367void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
368 CpuRegister temp) {
369 // All registers are assumed to be correctly set up.
370
371 // TODO: Implement all kinds of calls:
372 // 1) boot -> boot
373 // 2) app -> boot
374 // 3) app -> app
375 //
376 // Currently we implement the app -> app logic, which looks up in the resolve cache.
377
378 // temp = method;
379 LoadCurrentMethod(temp);
380 // temp = temp->dex_cache_resolved_methods_;
381 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
382 // temp = temp[index_in_cache]
383 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
384 // (temp + offset_of_quick_compiled_code)()
385 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
386 kX86_64WordSize).SizeValue()));
387
388 DCHECK(!IsLeafMethod());
389 RecordPcInfo(invoke, invoke->GetDexPc());
390}
391
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100392void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
393 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
394}
395
396void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
397 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
398}
399
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100400size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
401 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
402 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100403}
404
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100405size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
406 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
407 return kX86_64WordSize;
408}
409
410size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
411 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
412 return kX86_64WordSize;
413}
414
415size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
416 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
417 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100418}
419
Nicolas Geoffray98893962015-01-21 12:32:32 +0000420static uint32_t ComputeCoreCalleeSaveMask() {
421 uint32_t mask = 0;
422 for (size_t i = 0, e = arraysize(kCoreCalleeSaves); i < e; ++i) {
423 mask |= (1 << kCoreCalleeSaves[i]);
424 }
425 return mask;
426}
427
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000428CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph, const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000429 : CodeGenerator(graph,
430 kNumberOfCpuRegisters,
431 kNumberOfFloatRegisters,
432 0,
433 ComputeCoreCalleeSaveMask(),
434 0,
435 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100436 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100437 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000438 instruction_visitor_(graph, this),
439 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100440
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100441InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
442 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100443 : HGraphVisitor(graph),
444 assembler_(codegen->GetAssembler()),
445 codegen_(codegen) {}
446
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100447Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100448 switch (type) {
449 case Primitive::kPrimLong:
450 case Primitive::kPrimByte:
451 case Primitive::kPrimBoolean:
452 case Primitive::kPrimChar:
453 case Primitive::kPrimShort:
454 case Primitive::kPrimInt:
455 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100456 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100457 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100458 }
459
460 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100461 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100462 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100463 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100464 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100465
466 case Primitive::kPrimVoid:
467 LOG(FATAL) << "Unreachable type " << type;
468 }
469
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100470 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100471}
472
Nicolas Geoffray98893962015-01-21 12:32:32 +0000473size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
474 uint32_t mask = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
475 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize
476 + __builtin_popcount(mask) * kX86_64WordSize;
477}
478
479void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100480 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100481 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100482
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000483 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100484 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000485
Nicolas Geoffray98893962015-01-21 12:32:32 +0000486 if (is_baseline) {
487 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
488 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
489 }
490 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000491
Nicolas Geoffray98893962015-01-21 12:32:32 +0000492 // TODO: We currently don't use Quick's FP callee saved registers.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000493 blocked_fpu_registers_[XMM12] = true;
494 blocked_fpu_registers_[XMM13] = true;
495 blocked_fpu_registers_[XMM14] = true;
496 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100497}
498
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100499void CodeGeneratorX86_64::GenerateFrameEntry() {
500 // Create a fake register to mimic Quick.
501 static const int kFakeReturnRegister = 16;
502 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000503 core_spill_mask_ |= (allocated_registers_.GetCoreRegisters() & core_callee_save_mask_);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100504
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100505 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700506 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Calin Juravle93edf732015-01-20 20:14:07 +0000507 bool implicitStackOverflowChecks = GetCompilerOptions().GetImplicitStackOverflowChecks();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100508
Calin Juravle93edf732015-01-20 20:14:07 +0000509 if (!skip_overflow_check && implicitStackOverflowChecks) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100510 __ testq(CpuRegister(RAX), Address(
511 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100512 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100513 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000514
515 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
516 if (allocated_registers_.ContainsCoreRegister(kCoreCalleeSaves[i])) {
517 __ pushq(CpuRegister(kCoreCalleeSaves[i]));
518 }
519 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100520
Nicolas Geoffray98893962015-01-21 12:32:32 +0000521 __ subq(CpuRegister(RSP), Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100522
Calin Juravle93edf732015-01-20 20:14:07 +0000523 if (!skip_overflow_check && !implicitStackOverflowChecks) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100524 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100525 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100526
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100527 __ gs()->cmpq(CpuRegister(RSP),
528 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
529 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100530 }
531
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100532 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
533}
534
535void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffray98893962015-01-21 12:32:32 +0000536 __ addq(CpuRegister(RSP), Immediate(GetFrameSize() - FrameEntrySpillSize()));
537
538 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
539 if (allocated_registers_.ContainsCoreRegister(kCoreCalleeSaves[i])) {
540 __ popq(CpuRegister(kCoreCalleeSaves[i]));
541 }
542 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100543}
544
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100545void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
546 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100547}
548
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100549void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100550 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
551}
552
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100553Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
554 switch (load->GetType()) {
555 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100556 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100557 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
558 break;
559
560 case Primitive::kPrimInt:
561 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100562 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100563 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100564
565 case Primitive::kPrimBoolean:
566 case Primitive::kPrimByte:
567 case Primitive::kPrimChar:
568 case Primitive::kPrimShort:
569 case Primitive::kPrimVoid:
570 LOG(FATAL) << "Unexpected type " << load->GetType();
571 }
572
573 LOG(FATAL) << "Unreachable";
574 return Location();
575}
576
577void CodeGeneratorX86_64::Move(Location destination, Location source) {
578 if (source.Equals(destination)) {
579 return;
580 }
581 if (destination.IsRegister()) {
582 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000583 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100584 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000585 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100586 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000587 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100588 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100589 } else {
590 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000591 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100592 Address(CpuRegister(RSP), source.GetStackIndex()));
593 }
594 } else if (destination.IsFpuRegister()) {
595 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000596 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100597 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000598 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000600 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100601 Address(CpuRegister(RSP), source.GetStackIndex()));
602 } else {
603 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000604 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100605 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100606 }
607 } else if (destination.IsStackSlot()) {
608 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100609 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000610 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100611 } else if (source.IsFpuRegister()) {
612 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000613 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500614 } else if (source.IsConstant()) {
615 HConstant* constant = source.GetConstant();
616 int32_t value;
617 if (constant->IsFloatConstant()) {
618 value = bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue());
619 } else {
620 DCHECK(constant->IsIntConstant());
621 value = constant->AsIntConstant()->GetValue();
622 }
623 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100624 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500625 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000626 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
627 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100628 }
629 } else {
630 DCHECK(destination.IsDoubleStackSlot());
631 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000633 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 } else if (source.IsFpuRegister()) {
635 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500637 } else if (source.IsConstant()) {
638 HConstant* constant = source.GetConstant();
639 int64_t value = constant->AsLongConstant()->GetValue();
640 if (constant->IsDoubleConstant()) {
641 value = bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue());
642 } else {
643 DCHECK(constant->IsLongConstant());
644 value = constant->AsLongConstant()->GetValue();
645 }
646 __ movq(CpuRegister(TMP), Immediate(value));
647 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100648 } else {
649 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000650 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
651 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100652 }
653 }
654}
655
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100656void CodeGeneratorX86_64::Move(HInstruction* instruction,
657 Location location,
658 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000659 LocationSummary* locations = instruction->GetLocations();
660 if (locations != nullptr && locations->Out().Equals(location)) {
661 return;
662 }
663
664 if (locations != nullptr && locations->Out().IsConstant()) {
665 HConstant* const_to_move = locations->Out().GetConstant();
666 if (const_to_move->IsIntConstant()) {
667 Immediate imm(const_to_move->AsIntConstant()->GetValue());
668 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000669 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000670 } else if (location.IsStackSlot()) {
671 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
672 } else {
673 DCHECK(location.IsConstant());
674 DCHECK_EQ(location.GetConstant(), const_to_move);
675 }
676 } else if (const_to_move->IsLongConstant()) {
677 int64_t value = const_to_move->AsLongConstant()->GetValue();
678 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000679 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000680 } else if (location.IsDoubleStackSlot()) {
681 __ movq(CpuRegister(TMP), Immediate(value));
682 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
683 } else {
684 DCHECK(location.IsConstant());
685 DCHECK_EQ(location.GetConstant(), const_to_move);
686 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100687 }
Roland Levillain476df552014-10-09 17:51:36 +0100688 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100689 switch (instruction->GetType()) {
690 case Primitive::kPrimBoolean:
691 case Primitive::kPrimByte:
692 case Primitive::kPrimChar:
693 case Primitive::kPrimShort:
694 case Primitive::kPrimInt:
695 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100696 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100697 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
698 break;
699
700 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100701 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000702 Move(location,
703 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704 break;
705
706 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100707 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100708 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000709 } else if (instruction->IsTemporary()) {
710 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
711 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100712 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100713 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100714 switch (instruction->GetType()) {
715 case Primitive::kPrimBoolean:
716 case Primitive::kPrimByte:
717 case Primitive::kPrimChar:
718 case Primitive::kPrimShort:
719 case Primitive::kPrimInt:
720 case Primitive::kPrimNot:
721 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100722 case Primitive::kPrimFloat:
723 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000724 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725 break;
726
727 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100728 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100729 }
730 }
731}
732
733void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
734 got->SetLocations(nullptr);
735}
736
737void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
738 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100739 DCHECK(!successor->IsExitBlock());
740
741 HBasicBlock* block = got->GetBlock();
742 HInstruction* previous = got->GetPrevious();
743
744 HLoopInformation* info = block->GetLoopInformation();
745 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
746 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
747 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
748 return;
749 }
750
751 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
752 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
753 }
754 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100755 __ jmp(codegen_->GetLabelOf(successor));
756 }
757}
758
759void LocationsBuilderX86_64::VisitExit(HExit* exit) {
760 exit->SetLocations(nullptr);
761}
762
763void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700764 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100765 if (kIsDebugBuild) {
766 __ Comment("Unreachable");
767 __ int3();
768 }
769}
770
771void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100772 LocationSummary* locations =
773 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100774 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100775 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100776 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100777 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100778}
779
780void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700781 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100782 if (cond->IsIntConstant()) {
783 // Constant condition, statically compared against 1.
784 int32_t cond_value = cond->AsIntConstant()->GetValue();
785 if (cond_value == 1) {
786 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
787 if_instr->IfTrueSuccessor())) {
788 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100789 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100790 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100791 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100792 DCHECK_EQ(cond_value, 0);
793 }
794 } else {
795 bool materialized =
796 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
797 // Moves do not affect the eflags register, so if the condition is
798 // evaluated just before the if, we don't need to evaluate it
799 // again.
800 bool eflags_set = cond->IsCondition()
801 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
802 if (materialized) {
803 if (!eflags_set) {
804 // Materialized condition, compare against 0.
805 Location lhs = if_instr->GetLocations()->InAt(0);
806 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000807 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100808 } else {
809 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
810 Immediate(0));
811 }
812 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
813 } else {
814 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
815 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
816 }
817 } else {
818 Location lhs = cond->GetLocations()->InAt(0);
819 Location rhs = cond->GetLocations()->InAt(1);
820 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000821 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100822 } else if (rhs.IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000823 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100824 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
825 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000826 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100827 Address(CpuRegister(RSP), rhs.GetStackIndex()));
828 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100829 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
830 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700831 }
Dave Allison20dfc792014-06-16 20:44:29 -0700832 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100833 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
834 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700835 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100836 }
837}
838
839void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
840 local->SetLocations(nullptr);
841}
842
843void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
844 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
845}
846
847void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
848 local->SetLocations(nullptr);
849}
850
851void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
852 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700853 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100854}
855
856void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100857 LocationSummary* locations =
858 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859 switch (store->InputAt(1)->GetType()) {
860 case Primitive::kPrimBoolean:
861 case Primitive::kPrimByte:
862 case Primitive::kPrimChar:
863 case Primitive::kPrimShort:
864 case Primitive::kPrimInt:
865 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100866 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100867 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
868 break;
869
870 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100871 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100872 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
873 break;
874
875 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100876 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100877 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100878}
879
880void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700881 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100882}
883
Dave Allison20dfc792014-06-16 20:44:29 -0700884void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100885 LocationSummary* locations =
886 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100887 locations->SetInAt(0, Location::RequiresRegister());
888 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100889 if (comp->NeedsMaterialization()) {
890 locations->SetOut(Location::RequiresRegister());
891 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100892}
893
Dave Allison20dfc792014-06-16 20:44:29 -0700894void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
895 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100896 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000897 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100898 // Clear register: setcc only sets the low byte.
899 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100900 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000901 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
902 locations->InAt(1).AsRegister<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100903 } else if (locations->InAt(1).IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000904 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100905 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
906 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000907 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100908 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
909 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100910 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700911 }
912}
913
914void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
915 VisitCondition(comp);
916}
917
918void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
919 VisitCondition(comp);
920}
921
922void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
923 VisitCondition(comp);
924}
925
926void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
927 VisitCondition(comp);
928}
929
930void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
931 VisitCondition(comp);
932}
933
934void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
935 VisitCondition(comp);
936}
937
938void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
939 VisitCondition(comp);
940}
941
942void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
943 VisitCondition(comp);
944}
945
946void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
947 VisitCondition(comp);
948}
949
950void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
951 VisitCondition(comp);
952}
953
954void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
955 VisitCondition(comp);
956}
957
958void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
959 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100960}
961
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100962void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100963 LocationSummary* locations =
964 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000965 switch (compare->InputAt(0)->GetType()) {
966 case Primitive::kPrimLong: {
967 locations->SetInAt(0, Location::RequiresRegister());
968 locations->SetInAt(1, Location::RequiresRegister());
969 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
970 break;
971 }
972 case Primitive::kPrimFloat:
973 case Primitive::kPrimDouble: {
974 locations->SetInAt(0, Location::RequiresFpuRegister());
975 locations->SetInAt(1, Location::RequiresFpuRegister());
976 locations->SetOut(Location::RequiresRegister());
977 break;
978 }
979 default:
980 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
981 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100982}
983
984void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100985 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000986 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000987 Location left = locations->InAt(0);
988 Location right = locations->InAt(1);
989
990 Label less, greater, done;
991 Primitive::Type type = compare->InputAt(0)->GetType();
992 switch (type) {
993 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000994 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100995 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000996 }
997 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000998 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000999 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1000 break;
1001 }
1002 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001003 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00001004 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1005 break;
1006 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001007 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001008 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001009 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001010 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001011 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001012 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001013
Calin Juravle91debbc2014-11-26 19:01:09 +00001014 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001015 __ movl(out, Immediate(1));
1016 __ jmp(&done);
1017
1018 __ Bind(&less);
1019 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001020
1021 __ Bind(&done);
1022}
1023
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001024void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001025 LocationSummary* locations =
1026 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001027 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001028}
1029
1030void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001031 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001032 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001033}
1034
1035void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001036 LocationSummary* locations =
1037 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001038 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001039}
1040
1041void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001042 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001043 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001044}
1045
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001046void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1047 LocationSummary* locations =
1048 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1049 locations->SetOut(Location::ConstantLocation(constant));
1050}
1051
1052void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1053 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001054 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001055}
1056
1057void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1058 LocationSummary* locations =
1059 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1060 locations->SetOut(Location::ConstantLocation(constant));
1061}
1062
1063void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1064 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001065 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001066}
1067
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1069 ret->SetLocations(nullptr);
1070}
1071
1072void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001073 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001074 codegen_->GenerateFrameExit();
1075 __ ret();
1076}
1077
1078void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001079 LocationSummary* locations =
1080 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081 switch (ret->InputAt(0)->GetType()) {
1082 case Primitive::kPrimBoolean:
1083 case Primitive::kPrimByte:
1084 case Primitive::kPrimChar:
1085 case Primitive::kPrimShort:
1086 case Primitive::kPrimInt:
1087 case Primitive::kPrimNot:
1088 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001089 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001090 break;
1091
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 case Primitive::kPrimFloat:
1093 case Primitive::kPrimDouble:
1094 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001095 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001096 break;
1097
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001099 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001100 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001101}
1102
1103void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1104 if (kIsDebugBuild) {
1105 switch (ret->InputAt(0)->GetType()) {
1106 case Primitive::kPrimBoolean:
1107 case Primitive::kPrimByte:
1108 case Primitive::kPrimChar:
1109 case Primitive::kPrimShort:
1110 case Primitive::kPrimInt:
1111 case Primitive::kPrimNot:
1112 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001113 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001114 break;
1115
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001116 case Primitive::kPrimFloat:
1117 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001118 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001119 XMM0);
1120 break;
1121
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001122 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001123 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001124 }
1125 }
1126 codegen_->GenerateFrameExit();
1127 __ ret();
1128}
1129
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001130Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1131 switch (type) {
1132 case Primitive::kPrimBoolean:
1133 case Primitive::kPrimByte:
1134 case Primitive::kPrimChar:
1135 case Primitive::kPrimShort:
1136 case Primitive::kPrimInt:
1137 case Primitive::kPrimNot: {
1138 uint32_t index = gp_index_++;
1139 stack_index_++;
1140 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001141 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001142 } else {
1143 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1144 }
1145 }
1146
1147 case Primitive::kPrimLong: {
1148 uint32_t index = gp_index_;
1149 stack_index_ += 2;
1150 if (index < calling_convention.GetNumberOfRegisters()) {
1151 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001153 } else {
1154 gp_index_ += 2;
1155 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1156 }
1157 }
1158
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001159 case Primitive::kPrimFloat: {
1160 uint32_t index = fp_index_++;
1161 stack_index_++;
1162 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001163 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001164 } else {
1165 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1166 }
1167 }
1168
1169 case Primitive::kPrimDouble: {
1170 uint32_t index = fp_index_++;
1171 stack_index_ += 2;
1172 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001173 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001174 } else {
1175 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1176 }
1177 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001178
1179 case Primitive::kPrimVoid:
1180 LOG(FATAL) << "Unexpected parameter type " << type;
1181 break;
1182 }
1183 return Location();
1184}
1185
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001186void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001187 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1188 if (intrinsic.TryDispatch(invoke)) {
1189 return;
1190 }
1191
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001192 HandleInvoke(invoke);
1193}
1194
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001195static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1196 if (invoke->GetLocations()->Intrinsified()) {
1197 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1198 intrinsic.Dispatch(invoke);
1199 return true;
1200 }
1201 return false;
1202}
1203
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001204void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001205 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1206 return;
1207 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001208
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001209 codegen_->GenerateStaticOrDirectCall(
1210 invoke,
1211 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001212}
1213
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001214void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001215 LocationSummary* locations =
1216 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001217 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001218
1219 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001220 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001221 HInstruction* input = invoke->InputAt(i);
1222 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1223 }
1224
1225 switch (invoke->GetType()) {
1226 case Primitive::kPrimBoolean:
1227 case Primitive::kPrimByte:
1228 case Primitive::kPrimChar:
1229 case Primitive::kPrimShort:
1230 case Primitive::kPrimInt:
1231 case Primitive::kPrimNot:
1232 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001233 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001234 break;
1235
1236 case Primitive::kPrimVoid:
1237 break;
1238
1239 case Primitive::kPrimDouble:
1240 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001241 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001242 break;
1243 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001244}
1245
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001246void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001247 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1248 if (intrinsic.TryDispatch(invoke)) {
1249 return;
1250 }
1251
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001252 HandleInvoke(invoke);
1253}
1254
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001255void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001256 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1257 return;
1258 }
1259
Roland Levillain271ab9c2014-11-27 15:23:57 +00001260 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001261 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1262 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1263 LocationSummary* locations = invoke->GetLocations();
1264 Location receiver = locations->InAt(0);
1265 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1266 // temp = object->GetClass();
1267 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001268 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1269 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001270 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001271 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001272 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001273 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001274 // temp = temp->GetMethodAt(method_offset);
1275 __ movl(temp, Address(temp, method_offset));
1276 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001277 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001278 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001279
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001280 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001281 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001282}
1283
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001284void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1285 HandleInvoke(invoke);
1286 // Add the hidden argument.
1287 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1288}
1289
1290void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1291 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001292 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001293 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1294 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1295 LocationSummary* locations = invoke->GetLocations();
1296 Location receiver = locations->InAt(0);
1297 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1298
1299 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001300 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001301 Immediate(invoke->GetDexMethodIndex()));
1302
1303 // temp = object->GetClass();
1304 if (receiver.IsStackSlot()) {
1305 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1306 __ movl(temp, Address(temp, class_offset));
1307 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001308 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001309 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001310 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001311 // temp = temp->GetImtEntryAt(method_offset);
1312 __ movl(temp, Address(temp, method_offset));
1313 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001314 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001315 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001316
1317 DCHECK(!codegen_->IsLeafMethod());
1318 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1319}
1320
Roland Levillain88cb1752014-10-20 16:36:47 +01001321void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1322 LocationSummary* locations =
1323 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1324 switch (neg->GetResultType()) {
1325 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001326 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001327 locations->SetInAt(0, Location::RequiresRegister());
1328 locations->SetOut(Location::SameAsFirstInput());
1329 break;
1330
Roland Levillain88cb1752014-10-20 16:36:47 +01001331 case Primitive::kPrimFloat:
1332 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001333 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001334 locations->SetOut(Location::SameAsFirstInput());
1335 locations->AddTemp(Location::RequiresRegister());
1336 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001337 break;
1338
1339 default:
1340 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1341 }
1342}
1343
1344void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1345 LocationSummary* locations = neg->GetLocations();
1346 Location out = locations->Out();
1347 Location in = locations->InAt(0);
1348 switch (neg->GetResultType()) {
1349 case Primitive::kPrimInt:
1350 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001351 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001352 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001353 break;
1354
1355 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001356 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001357 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001358 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001359 break;
1360
Roland Levillain5368c212014-11-27 15:03:41 +00001361 case Primitive::kPrimFloat: {
1362 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001363 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1364 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001365 // Implement float negation with an exclusive or with value
1366 // 0x80000000 (mask for bit 31, representing the sign of a
1367 // single-precision floating-point number).
1368 __ movq(constant, Immediate(INT64_C(0x80000000)));
1369 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001370 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001371 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001372 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001373
Roland Levillain5368c212014-11-27 15:03:41 +00001374 case Primitive::kPrimDouble: {
1375 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001376 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1377 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001378 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001379 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001380 // a double-precision floating-point number).
1381 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1382 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001383 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001384 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001385 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001386
1387 default:
1388 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1389 }
1390}
1391
Roland Levillaindff1f282014-11-05 14:15:05 +00001392void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1393 LocationSummary* locations =
1394 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1395 Primitive::Type result_type = conversion->GetResultType();
1396 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001397 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001398 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001399 case Primitive::kPrimByte:
1400 switch (input_type) {
1401 case Primitive::kPrimShort:
1402 case Primitive::kPrimInt:
1403 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001404 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001405 locations->SetInAt(0, Location::Any());
1406 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1407 break;
1408
1409 default:
1410 LOG(FATAL) << "Unexpected type conversion from " << input_type
1411 << " to " << result_type;
1412 }
1413 break;
1414
Roland Levillain01a8d712014-11-14 16:27:39 +00001415 case Primitive::kPrimShort:
1416 switch (input_type) {
1417 case Primitive::kPrimByte:
1418 case Primitive::kPrimInt:
1419 case Primitive::kPrimChar:
1420 // Processing a Dex `int-to-short' instruction.
1421 locations->SetInAt(0, Location::Any());
1422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1423 break;
1424
1425 default:
1426 LOG(FATAL) << "Unexpected type conversion from " << input_type
1427 << " to " << result_type;
1428 }
1429 break;
1430
Roland Levillain946e1432014-11-11 17:35:19 +00001431 case Primitive::kPrimInt:
1432 switch (input_type) {
1433 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001434 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001435 locations->SetInAt(0, Location::Any());
1436 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1437 break;
1438
1439 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001440 // Processing a Dex `float-to-int' instruction.
1441 locations->SetInAt(0, Location::RequiresFpuRegister());
1442 locations->SetOut(Location::RequiresRegister());
1443 locations->AddTemp(Location::RequiresFpuRegister());
1444 break;
1445
Roland Levillain946e1432014-11-11 17:35:19 +00001446 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001447 // Processing a Dex `double-to-int' instruction.
1448 locations->SetInAt(0, Location::RequiresFpuRegister());
1449 locations->SetOut(Location::RequiresRegister());
1450 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001451 break;
1452
1453 default:
1454 LOG(FATAL) << "Unexpected type conversion from " << input_type
1455 << " to " << result_type;
1456 }
1457 break;
1458
Roland Levillaindff1f282014-11-05 14:15:05 +00001459 case Primitive::kPrimLong:
1460 switch (input_type) {
1461 case Primitive::kPrimByte:
1462 case Primitive::kPrimShort:
1463 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001464 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001465 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001466 // TODO: We would benefit from a (to-be-implemented)
1467 // Location::RegisterOrStackSlot requirement for this input.
1468 locations->SetInAt(0, Location::RequiresRegister());
1469 locations->SetOut(Location::RequiresRegister());
1470 break;
1471
1472 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001473 // Processing a Dex `float-to-long' instruction.
1474 locations->SetInAt(0, Location::RequiresFpuRegister());
1475 locations->SetOut(Location::RequiresRegister());
1476 locations->AddTemp(Location::RequiresFpuRegister());
1477 break;
1478
Roland Levillaindff1f282014-11-05 14:15:05 +00001479 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001480 // Processing a Dex `double-to-long' instruction.
1481 locations->SetInAt(0, Location::RequiresFpuRegister());
1482 locations->SetOut(Location::RequiresRegister());
1483 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001484 break;
1485
1486 default:
1487 LOG(FATAL) << "Unexpected type conversion from " << input_type
1488 << " to " << result_type;
1489 }
1490 break;
1491
Roland Levillain981e4542014-11-14 11:47:14 +00001492 case Primitive::kPrimChar:
1493 switch (input_type) {
1494 case Primitive::kPrimByte:
1495 case Primitive::kPrimShort:
1496 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001497 // Processing a Dex `int-to-char' instruction.
1498 locations->SetInAt(0, Location::Any());
1499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1500 break;
1501
1502 default:
1503 LOG(FATAL) << "Unexpected type conversion from " << input_type
1504 << " to " << result_type;
1505 }
1506 break;
1507
Roland Levillaindff1f282014-11-05 14:15:05 +00001508 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001509 switch (input_type) {
1510 case Primitive::kPrimByte:
1511 case Primitive::kPrimShort:
1512 case Primitive::kPrimInt:
1513 case Primitive::kPrimChar:
1514 // Processing a Dex `int-to-float' instruction.
1515 locations->SetInAt(0, Location::RequiresRegister());
1516 locations->SetOut(Location::RequiresFpuRegister());
1517 break;
1518
1519 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001520 // Processing a Dex `long-to-float' instruction.
1521 locations->SetInAt(0, Location::RequiresRegister());
1522 locations->SetOut(Location::RequiresFpuRegister());
1523 break;
1524
Roland Levillaincff13742014-11-17 14:32:17 +00001525 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001526 // Processing a Dex `double-to-float' instruction.
1527 locations->SetInAt(0, Location::RequiresFpuRegister());
1528 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001529 break;
1530
1531 default:
1532 LOG(FATAL) << "Unexpected type conversion from " << input_type
1533 << " to " << result_type;
1534 };
1535 break;
1536
Roland Levillaindff1f282014-11-05 14:15:05 +00001537 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001538 switch (input_type) {
1539 case Primitive::kPrimByte:
1540 case Primitive::kPrimShort:
1541 case Primitive::kPrimInt:
1542 case Primitive::kPrimChar:
1543 // Processing a Dex `int-to-double' instruction.
1544 locations->SetInAt(0, Location::RequiresRegister());
1545 locations->SetOut(Location::RequiresFpuRegister());
1546 break;
1547
1548 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001549 // Processing a Dex `long-to-double' instruction.
1550 locations->SetInAt(0, Location::RequiresRegister());
1551 locations->SetOut(Location::RequiresFpuRegister());
1552 break;
1553
Roland Levillaincff13742014-11-17 14:32:17 +00001554 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001555 // Processing a Dex `float-to-double' instruction.
1556 locations->SetInAt(0, Location::RequiresFpuRegister());
1557 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001558 break;
1559
1560 default:
1561 LOG(FATAL) << "Unexpected type conversion from " << input_type
1562 << " to " << result_type;
1563 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001564 break;
1565
1566 default:
1567 LOG(FATAL) << "Unexpected type conversion from " << input_type
1568 << " to " << result_type;
1569 }
1570}
1571
1572void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1573 LocationSummary* locations = conversion->GetLocations();
1574 Location out = locations->Out();
1575 Location in = locations->InAt(0);
1576 Primitive::Type result_type = conversion->GetResultType();
1577 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001578 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001579 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001580 case Primitive::kPrimByte:
1581 switch (input_type) {
1582 case Primitive::kPrimShort:
1583 case Primitive::kPrimInt:
1584 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001585 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001586 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001587 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001588 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001589 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001590 Address(CpuRegister(RSP), in.GetStackIndex()));
1591 } else {
1592 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001593 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001594 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1595 }
1596 break;
1597
1598 default:
1599 LOG(FATAL) << "Unexpected type conversion from " << input_type
1600 << " to " << result_type;
1601 }
1602 break;
1603
Roland Levillain01a8d712014-11-14 16:27:39 +00001604 case Primitive::kPrimShort:
1605 switch (input_type) {
1606 case Primitive::kPrimByte:
1607 case Primitive::kPrimInt:
1608 case Primitive::kPrimChar:
1609 // Processing a Dex `int-to-short' instruction.
1610 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001611 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001612 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001613 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001614 Address(CpuRegister(RSP), in.GetStackIndex()));
1615 } else {
1616 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001617 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001618 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1619 }
1620 break;
1621
1622 default:
1623 LOG(FATAL) << "Unexpected type conversion from " << input_type
1624 << " to " << result_type;
1625 }
1626 break;
1627
Roland Levillain946e1432014-11-11 17:35:19 +00001628 case Primitive::kPrimInt:
1629 switch (input_type) {
1630 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001631 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001632 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001633 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001634 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001635 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001636 Address(CpuRegister(RSP), in.GetStackIndex()));
1637 } else {
1638 DCHECK(in.IsConstant());
1639 DCHECK(in.GetConstant()->IsLongConstant());
1640 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001641 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001642 }
1643 break;
1644
Roland Levillain3f8f9362014-12-02 17:45:01 +00001645 case Primitive::kPrimFloat: {
1646 // Processing a Dex `float-to-int' instruction.
1647 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1648 CpuRegister output = out.AsRegister<CpuRegister>();
1649 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1650 Label done, nan;
1651
1652 __ movl(output, Immediate(kPrimIntMax));
1653 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001654 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001655 // if input >= temp goto done
1656 __ comiss(input, temp);
1657 __ j(kAboveEqual, &done);
1658 // if input == NaN goto nan
1659 __ j(kUnordered, &nan);
1660 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001661 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001662 __ jmp(&done);
1663 __ Bind(&nan);
1664 // output = 0
1665 __ xorl(output, output);
1666 __ Bind(&done);
1667 break;
1668 }
1669
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001670 case Primitive::kPrimDouble: {
1671 // Processing a Dex `double-to-int' instruction.
1672 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1673 CpuRegister output = out.AsRegister<CpuRegister>();
1674 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1675 Label done, nan;
1676
1677 __ movl(output, Immediate(kPrimIntMax));
1678 // temp = int-to-double(output)
1679 __ cvtsi2sd(temp, output);
1680 // if input >= temp goto done
1681 __ comisd(input, temp);
1682 __ j(kAboveEqual, &done);
1683 // if input == NaN goto nan
1684 __ j(kUnordered, &nan);
1685 // output = double-to-int-truncate(input)
1686 __ cvttsd2si(output, input);
1687 __ jmp(&done);
1688 __ Bind(&nan);
1689 // output = 0
1690 __ xorl(output, output);
1691 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001692 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001693 }
Roland Levillain946e1432014-11-11 17:35:19 +00001694
1695 default:
1696 LOG(FATAL) << "Unexpected type conversion from " << input_type
1697 << " to " << result_type;
1698 }
1699 break;
1700
Roland Levillaindff1f282014-11-05 14:15:05 +00001701 case Primitive::kPrimLong:
1702 switch (input_type) {
1703 DCHECK(out.IsRegister());
1704 case Primitive::kPrimByte:
1705 case Primitive::kPrimShort:
1706 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001707 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001708 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001709 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001710 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001711 break;
1712
Roland Levillain624279f2014-12-04 11:54:28 +00001713 case Primitive::kPrimFloat: {
1714 // Processing a Dex `float-to-long' instruction.
1715 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1716 CpuRegister output = out.AsRegister<CpuRegister>();
1717 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1718 Label done, nan;
1719
1720 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001721 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001722 __ cvtsi2ss(temp, output, true);
1723 // if input >= temp goto done
1724 __ comiss(input, temp);
1725 __ j(kAboveEqual, &done);
1726 // if input == NaN goto nan
1727 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001728 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001729 __ cvttss2si(output, input, true);
1730 __ jmp(&done);
1731 __ Bind(&nan);
1732 // output = 0
1733 __ xorq(output, output);
1734 __ Bind(&done);
1735 break;
1736 }
1737
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001738 case Primitive::kPrimDouble: {
1739 // Processing a Dex `double-to-long' instruction.
1740 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1741 CpuRegister output = out.AsRegister<CpuRegister>();
1742 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1743 Label done, nan;
1744
1745 __ movq(output, Immediate(kPrimLongMax));
1746 // temp = long-to-double(output)
1747 __ cvtsi2sd(temp, output, true);
1748 // if input >= temp goto done
1749 __ comisd(input, temp);
1750 __ j(kAboveEqual, &done);
1751 // if input == NaN goto nan
1752 __ j(kUnordered, &nan);
1753 // output = double-to-long-truncate(input)
1754 __ cvttsd2si(output, input, true);
1755 __ jmp(&done);
1756 __ Bind(&nan);
1757 // output = 0
1758 __ xorq(output, output);
1759 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001760 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001761 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001762
1763 default:
1764 LOG(FATAL) << "Unexpected type conversion from " << input_type
1765 << " to " << result_type;
1766 }
1767 break;
1768
Roland Levillain981e4542014-11-14 11:47:14 +00001769 case Primitive::kPrimChar:
1770 switch (input_type) {
1771 case Primitive::kPrimByte:
1772 case Primitive::kPrimShort:
1773 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001774 // Processing a Dex `int-to-char' instruction.
1775 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001776 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001777 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001778 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001779 Address(CpuRegister(RSP), in.GetStackIndex()));
1780 } else {
1781 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001782 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001783 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1784 }
1785 break;
1786
1787 default:
1788 LOG(FATAL) << "Unexpected type conversion from " << input_type
1789 << " to " << result_type;
1790 }
1791 break;
1792
Roland Levillaindff1f282014-11-05 14:15:05 +00001793 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001794 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001795 case Primitive::kPrimByte:
1796 case Primitive::kPrimShort:
1797 case Primitive::kPrimInt:
1798 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001799 // Processing a Dex `int-to-float' instruction.
1800 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001801 break;
1802
1803 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001804 // Processing a Dex `long-to-float' instruction.
1805 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1806 break;
1807
Roland Levillaincff13742014-11-17 14:32:17 +00001808 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001809 // Processing a Dex `double-to-float' instruction.
1810 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001811 break;
1812
1813 default:
1814 LOG(FATAL) << "Unexpected type conversion from " << input_type
1815 << " to " << result_type;
1816 };
1817 break;
1818
Roland Levillaindff1f282014-11-05 14:15:05 +00001819 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001820 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001821 case Primitive::kPrimByte:
1822 case Primitive::kPrimShort:
1823 case Primitive::kPrimInt:
1824 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001825 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001826 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001827 break;
1828
1829 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001830 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001831 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001832 break;
1833
Roland Levillaincff13742014-11-17 14:32:17 +00001834 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001835 // Processing a Dex `float-to-double' instruction.
1836 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001837 break;
1838
1839 default:
1840 LOG(FATAL) << "Unexpected type conversion from " << input_type
1841 << " to " << result_type;
1842 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001843 break;
1844
1845 default:
1846 LOG(FATAL) << "Unexpected type conversion from " << input_type
1847 << " to " << result_type;
1848 }
1849}
1850
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001851void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001852 LocationSummary* locations =
1853 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001854 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001855 case Primitive::kPrimInt: {
1856 locations->SetInAt(0, Location::RequiresRegister());
1857 locations->SetInAt(1, Location::Any());
1858 locations->SetOut(Location::SameAsFirstInput());
1859 break;
1860 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001861
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001862 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001863 locations->SetInAt(0, Location::RequiresRegister());
1864 locations->SetInAt(1, Location::RequiresRegister());
1865 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001866 break;
1867 }
1868
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001869 case Primitive::kPrimDouble:
1870 case Primitive::kPrimFloat: {
1871 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001872 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001873 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001874 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001875 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001876
1877 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001878 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001879 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001880}
1881
1882void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1883 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001884 Location first = locations->InAt(0);
1885 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001886 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001887
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001888 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001889 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001890 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001891 __ addl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001892 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001893 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001894 __ addl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001895 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001896 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001897 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001898 break;
1899 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001900
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001901 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001902 __ addq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001903 break;
1904 }
1905
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001906 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001907 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001908 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001909 }
1910
1911 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001912 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001913 break;
1914 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001915
1916 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001917 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001918 }
1919}
1920
1921void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001922 LocationSummary* locations =
1923 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001924 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001925 case Primitive::kPrimInt: {
1926 locations->SetInAt(0, Location::RequiresRegister());
1927 locations->SetInAt(1, Location::Any());
1928 locations->SetOut(Location::SameAsFirstInput());
1929 break;
1930 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001931 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001932 locations->SetInAt(0, Location::RequiresRegister());
1933 locations->SetInAt(1, Location::RequiresRegister());
1934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001935 break;
1936 }
Calin Juravle11351682014-10-23 15:38:15 +01001937 case Primitive::kPrimFloat:
1938 case Primitive::kPrimDouble: {
1939 locations->SetInAt(0, Location::RequiresFpuRegister());
1940 locations->SetInAt(1, Location::RequiresFpuRegister());
1941 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001942 break;
Calin Juravle11351682014-10-23 15:38:15 +01001943 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001944 default:
Calin Juravle11351682014-10-23 15:38:15 +01001945 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001946 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001947}
1948
1949void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1950 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001951 Location first = locations->InAt(0);
1952 Location second = locations->InAt(1);
1953 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001954 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001955 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001956 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001957 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001958 } else if (second.IsConstant()) {
1959 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001960 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001961 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001963 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001964 break;
1965 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001966 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001967 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001968 break;
1969 }
1970
Calin Juravle11351682014-10-23 15:38:15 +01001971 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001972 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001973 break;
Calin Juravle11351682014-10-23 15:38:15 +01001974 }
1975
1976 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001977 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001978 break;
1979 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001980
1981 default:
Calin Juravle11351682014-10-23 15:38:15 +01001982 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001983 }
1984}
1985
Calin Juravle34bacdf2014-10-07 20:23:36 +01001986void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1987 LocationSummary* locations =
1988 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1989 switch (mul->GetResultType()) {
1990 case Primitive::kPrimInt: {
1991 locations->SetInAt(0, Location::RequiresRegister());
1992 locations->SetInAt(1, Location::Any());
1993 locations->SetOut(Location::SameAsFirstInput());
1994 break;
1995 }
1996 case Primitive::kPrimLong: {
1997 locations->SetInAt(0, Location::RequiresRegister());
1998 locations->SetInAt(1, Location::RequiresRegister());
1999 locations->SetOut(Location::SameAsFirstInput());
2000 break;
2001 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002002 case Primitive::kPrimFloat:
2003 case Primitive::kPrimDouble: {
2004 locations->SetInAt(0, Location::RequiresFpuRegister());
2005 locations->SetInAt(1, Location::RequiresFpuRegister());
2006 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002007 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002008 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002009
2010 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002011 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002012 }
2013}
2014
2015void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2016 LocationSummary* locations = mul->GetLocations();
2017 Location first = locations->InAt(0);
2018 Location second = locations->InAt(1);
2019 DCHECK(first.Equals(locations->Out()));
2020 switch (mul->GetResultType()) {
2021 case Primitive::kPrimInt: {
2022 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002023 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002024 } else if (second.IsConstant()) {
2025 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002026 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002027 } else {
2028 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002029 __ imull(first.AsRegister<CpuRegister>(),
2030 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002031 }
2032 break;
2033 }
2034 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002035 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002036 break;
2037 }
2038
Calin Juravleb5bfa962014-10-21 18:02:24 +01002039 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002040 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002041 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002042 }
2043
2044 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002046 break;
2047 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002048
2049 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002050 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002051 }
2052}
2053
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002054void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2055 uint32_t stack_adjustment, bool is_float) {
2056 if (source.IsStackSlot()) {
2057 DCHECK(is_float);
2058 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2059 } else if (source.IsDoubleStackSlot()) {
2060 DCHECK(!is_float);
2061 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2062 } else {
2063 // Write the value to the temporary location on the stack and load to FP stack.
2064 if (is_float) {
2065 Location stack_temp = Location::StackSlot(temp_offset);
2066 codegen_->Move(stack_temp, source);
2067 __ flds(Address(CpuRegister(RSP), temp_offset));
2068 } else {
2069 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2070 codegen_->Move(stack_temp, source);
2071 __ fldl(Address(CpuRegister(RSP), temp_offset));
2072 }
2073 }
2074}
2075
2076void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2077 Primitive::Type type = rem->GetResultType();
2078 bool is_float = type == Primitive::kPrimFloat;
2079 size_t elem_size = Primitive::ComponentSize(type);
2080 LocationSummary* locations = rem->GetLocations();
2081 Location first = locations->InAt(0);
2082 Location second = locations->InAt(1);
2083 Location out = locations->Out();
2084
2085 // Create stack space for 2 elements.
2086 // TODO: enhance register allocator to ask for stack temporaries.
2087 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2088
2089 // Load the values to the FP stack in reverse order, using temporaries if needed.
2090 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2091 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2092
2093 // Loop doing FPREM until we stabilize.
2094 Label retry;
2095 __ Bind(&retry);
2096 __ fprem();
2097
2098 // Move FP status to AX.
2099 __ fstsw();
2100
2101 // And see if the argument reduction is complete. This is signaled by the
2102 // C2 FPU flag bit set to 0.
2103 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2104 __ j(kNotEqual, &retry);
2105
2106 // We have settled on the final value. Retrieve it into an XMM register.
2107 // Store FP top of stack to real stack.
2108 if (is_float) {
2109 __ fsts(Address(CpuRegister(RSP), 0));
2110 } else {
2111 __ fstl(Address(CpuRegister(RSP), 0));
2112 }
2113
2114 // Pop the 2 items from the FP stack.
2115 __ fucompp();
2116
2117 // Load the value from the stack into an XMM register.
2118 DCHECK(out.IsFpuRegister()) << out;
2119 if (is_float) {
2120 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2121 } else {
2122 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2123 }
2124
2125 // And remove the temporary stack space we allocated.
2126 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2127}
2128
Calin Juravlebacfec32014-11-14 15:54:36 +00002129void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2130 DCHECK(instruction->IsDiv() || instruction->IsRem());
2131 Primitive::Type type = instruction->GetResultType();
2132 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2133
2134 bool is_div = instruction->IsDiv();
2135 LocationSummary* locations = instruction->GetLocations();
2136
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2138 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002139
Roland Levillain271ab9c2014-11-27 15:23:57 +00002140 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002141 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2142
2143 SlowPathCodeX86_64* slow_path =
2144 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2145 out_reg.AsRegister(), type, is_div);
2146 codegen_->AddSlowPath(slow_path);
2147
2148 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2149 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2150 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002151 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002152 __ cmpl(second_reg, Immediate(-1));
2153 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002154 // edx:eax <- sign-extended of eax
2155 __ cdq();
2156 // eax = quotient, edx = remainder
2157 __ idivl(second_reg);
2158 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002159 __ cmpq(second_reg, Immediate(-1));
2160 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002161 // rdx:rax <- sign-extended of rax
2162 __ cqo();
2163 // rax = quotient, rdx = remainder
2164 __ idivq(second_reg);
2165 }
2166
2167 __ Bind(slow_path->GetExitLabel());
2168}
2169
Calin Juravle7c4954d2014-10-28 16:57:40 +00002170void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2171 LocationSummary* locations =
2172 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2173 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002174 case Primitive::kPrimInt:
2175 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002176 locations->SetInAt(0, Location::RegisterLocation(RAX));
2177 locations->SetInAt(1, Location::RequiresRegister());
2178 locations->SetOut(Location::SameAsFirstInput());
2179 // Intel uses edx:eax as the dividend.
2180 locations->AddTemp(Location::RegisterLocation(RDX));
2181 break;
2182 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002183
Calin Juravle7c4954d2014-10-28 16:57:40 +00002184 case Primitive::kPrimFloat:
2185 case Primitive::kPrimDouble: {
2186 locations->SetInAt(0, Location::RequiresFpuRegister());
2187 locations->SetInAt(1, Location::RequiresFpuRegister());
2188 locations->SetOut(Location::SameAsFirstInput());
2189 break;
2190 }
2191
2192 default:
2193 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2194 }
2195}
2196
2197void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2198 LocationSummary* locations = div->GetLocations();
2199 Location first = locations->InAt(0);
2200 Location second = locations->InAt(1);
2201 DCHECK(first.Equals(locations->Out()));
2202
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002203 Primitive::Type type = div->GetResultType();
2204 switch (type) {
2205 case Primitive::kPrimInt:
2206 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002207 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002208 break;
2209 }
2210
Calin Juravle7c4954d2014-10-28 16:57:40 +00002211 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002212 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002213 break;
2214 }
2215
2216 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002217 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002218 break;
2219 }
2220
2221 default:
2222 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2223 }
2224}
2225
Calin Juravlebacfec32014-11-14 15:54:36 +00002226void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002227 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002228 LocationSummary* locations =
2229 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002230
2231 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002232 case Primitive::kPrimInt:
2233 case Primitive::kPrimLong: {
2234 locations->SetInAt(0, Location::RegisterLocation(RAX));
2235 locations->SetInAt(1, Location::RequiresRegister());
2236 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2237 locations->SetOut(Location::RegisterLocation(RDX));
2238 break;
2239 }
2240
2241 case Primitive::kPrimFloat:
2242 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002243 locations->SetInAt(0, Location::Any());
2244 locations->SetInAt(1, Location::Any());
2245 locations->SetOut(Location::RequiresFpuRegister());
2246 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002247 break;
2248 }
2249
2250 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002251 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002252 }
2253}
2254
2255void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2256 Primitive::Type type = rem->GetResultType();
2257 switch (type) {
2258 case Primitive::kPrimInt:
2259 case Primitive::kPrimLong: {
2260 GenerateDivRemIntegral(rem);
2261 break;
2262 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002263 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002264 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002265 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002266 break;
2267 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002268 default:
2269 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2270 }
2271}
2272
Calin Juravled0d48522014-11-04 16:40:20 +00002273void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2274 LocationSummary* locations =
2275 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2276 locations->SetInAt(0, Location::Any());
2277 if (instruction->HasUses()) {
2278 locations->SetOut(Location::SameAsFirstInput());
2279 }
2280}
2281
2282void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2283 SlowPathCodeX86_64* slow_path =
2284 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2285 codegen_->AddSlowPath(slow_path);
2286
2287 LocationSummary* locations = instruction->GetLocations();
2288 Location value = locations->InAt(0);
2289
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002290 switch (instruction->GetType()) {
2291 case Primitive::kPrimInt: {
2292 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002293 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002294 __ j(kEqual, slow_path->GetEntryLabel());
2295 } else if (value.IsStackSlot()) {
2296 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2297 __ j(kEqual, slow_path->GetEntryLabel());
2298 } else {
2299 DCHECK(value.IsConstant()) << value;
2300 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2301 __ jmp(slow_path->GetEntryLabel());
2302 }
2303 }
2304 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002305 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002306 case Primitive::kPrimLong: {
2307 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002308 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002309 __ j(kEqual, slow_path->GetEntryLabel());
2310 } else if (value.IsDoubleStackSlot()) {
2311 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2312 __ j(kEqual, slow_path->GetEntryLabel());
2313 } else {
2314 DCHECK(value.IsConstant()) << value;
2315 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2316 __ jmp(slow_path->GetEntryLabel());
2317 }
2318 }
2319 break;
2320 }
2321 default:
2322 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002323 }
Calin Juravled0d48522014-11-04 16:40:20 +00002324}
2325
Calin Juravle9aec02f2014-11-18 23:06:35 +00002326void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2327 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2328
2329 LocationSummary* locations =
2330 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2331
2332 switch (op->GetResultType()) {
2333 case Primitive::kPrimInt:
2334 case Primitive::kPrimLong: {
2335 locations->SetInAt(0, Location::RequiresRegister());
2336 // The shift count needs to be in CL.
2337 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2338 locations->SetOut(Location::SameAsFirstInput());
2339 break;
2340 }
2341 default:
2342 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2343 }
2344}
2345
2346void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2347 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2348
2349 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002351 Location second = locations->InAt(1);
2352
2353 switch (op->GetResultType()) {
2354 case Primitive::kPrimInt: {
2355 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002356 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002357 if (op->IsShl()) {
2358 __ shll(first_reg, second_reg);
2359 } else if (op->IsShr()) {
2360 __ sarl(first_reg, second_reg);
2361 } else {
2362 __ shrl(first_reg, second_reg);
2363 }
2364 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002365 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002366 if (op->IsShl()) {
2367 __ shll(first_reg, imm);
2368 } else if (op->IsShr()) {
2369 __ sarl(first_reg, imm);
2370 } else {
2371 __ shrl(first_reg, imm);
2372 }
2373 }
2374 break;
2375 }
2376 case Primitive::kPrimLong: {
2377 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002378 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002379 if (op->IsShl()) {
2380 __ shlq(first_reg, second_reg);
2381 } else if (op->IsShr()) {
2382 __ sarq(first_reg, second_reg);
2383 } else {
2384 __ shrq(first_reg, second_reg);
2385 }
2386 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002387 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002388 if (op->IsShl()) {
2389 __ shlq(first_reg, imm);
2390 } else if (op->IsShr()) {
2391 __ sarq(first_reg, imm);
2392 } else {
2393 __ shrq(first_reg, imm);
2394 }
2395 }
2396 break;
2397 }
2398 default:
2399 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2400 }
2401}
2402
2403void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2404 HandleShift(shl);
2405}
2406
2407void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2408 HandleShift(shl);
2409}
2410
2411void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2412 HandleShift(shr);
2413}
2414
2415void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2416 HandleShift(shr);
2417}
2418
2419void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2420 HandleShift(ushr);
2421}
2422
2423void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2424 HandleShift(ushr);
2425}
2426
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002427void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002428 LocationSummary* locations =
2429 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002430 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002431 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2432 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2433 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002434}
2435
2436void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2437 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002438 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002439 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2440
2441 __ gs()->call(Address::Absolute(
2442 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
2443
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002444 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002445 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002446}
2447
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002448void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2449 LocationSummary* locations =
2450 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2451 InvokeRuntimeCallingConvention calling_convention;
2452 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002453 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002454 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002455 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002456}
2457
2458void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2459 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002460 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002461 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2462
2463 __ gs()->call(Address::Absolute(
2464 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
2465
2466 DCHECK(!codegen_->IsLeafMethod());
2467 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2468}
2469
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002470void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002471 LocationSummary* locations =
2472 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002473 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2474 if (location.IsStackSlot()) {
2475 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2476 } else if (location.IsDoubleStackSlot()) {
2477 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2478 }
2479 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002480}
2481
2482void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2483 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002484 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002485}
2486
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002487void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002488 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002489 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002490 locations->SetInAt(0, Location::RequiresRegister());
2491 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002492}
2493
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002494void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2495 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2497 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002498 Location out = locations->Out();
2499 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002500 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002501 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002502 break;
2503
2504 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002505 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002506 break;
2507
2508 default:
2509 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2510 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002511}
2512
2513void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002514 LocationSummary* locations =
2515 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002516 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2517 locations->SetInAt(i, Location::Any());
2518 }
2519 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002520}
2521
2522void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002523 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002524 LOG(FATAL) << "Unimplemented";
2525}
2526
Calin Juravle52c48962014-12-16 17:02:57 +00002527void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2528 /*
2529 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2530 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2531 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2532 */
2533 switch (kind) {
2534 case MemBarrierKind::kAnyAny: {
2535 __ mfence();
2536 break;
2537 }
2538 case MemBarrierKind::kAnyStore:
2539 case MemBarrierKind::kLoadAny:
2540 case MemBarrierKind::kStoreStore: {
2541 // nop
2542 break;
2543 }
2544 default:
2545 LOG(FATAL) << "Unexpected memory barier " << kind;
2546 }
2547}
2548
2549void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2550 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2551
Nicolas Geoffray39468442014-09-02 15:17:15 +01002552 LocationSummary* locations =
2553 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002554 locations->SetInAt(0, Location::RequiresRegister());
2555 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2556}
2557
2558void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2559 const FieldInfo& field_info) {
2560 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2561
2562 LocationSummary* locations = instruction->GetLocations();
2563 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2564 Location out = locations->Out();
2565 bool is_volatile = field_info.IsVolatile();
2566 Primitive::Type field_type = field_info.GetFieldType();
2567 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2568
2569 switch (field_type) {
2570 case Primitive::kPrimBoolean: {
2571 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2572 break;
2573 }
2574
2575 case Primitive::kPrimByte: {
2576 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2577 break;
2578 }
2579
2580 case Primitive::kPrimShort: {
2581 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2582 break;
2583 }
2584
2585 case Primitive::kPrimChar: {
2586 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2587 break;
2588 }
2589
2590 case Primitive::kPrimInt:
2591 case Primitive::kPrimNot: {
2592 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2593 break;
2594 }
2595
2596 case Primitive::kPrimLong: {
2597 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2598 break;
2599 }
2600
2601 case Primitive::kPrimFloat: {
2602 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2603 break;
2604 }
2605
2606 case Primitive::kPrimDouble: {
2607 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2608 break;
2609 }
2610
2611 case Primitive::kPrimVoid:
2612 LOG(FATAL) << "Unreachable type " << field_type;
2613 UNREACHABLE();
2614 }
2615
Calin Juravle77520bc2015-01-12 18:45:46 +00002616 codegen_->MaybeRecordImplicitNullCheck(instruction);
2617
Calin Juravle52c48962014-12-16 17:02:57 +00002618 if (is_volatile) {
2619 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2620 }
2621}
2622
2623void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2624 const FieldInfo& field_info) {
2625 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2626
2627 LocationSummary* locations =
2628 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002629 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002630 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2631
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002632 locations->SetInAt(0, Location::RequiresRegister());
2633 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002634 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002635 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002636 locations->AddTemp(Location::RequiresRegister());
2637 locations->AddTemp(Location::RequiresRegister());
2638 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002639}
2640
Calin Juravle52c48962014-12-16 17:02:57 +00002641void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2642 const FieldInfo& field_info) {
2643 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2644
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002645 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002646 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2647 Location value = locations->InAt(1);
2648 bool is_volatile = field_info.IsVolatile();
2649 Primitive::Type field_type = field_info.GetFieldType();
2650 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2651
2652 if (is_volatile) {
2653 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2654 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002655
2656 switch (field_type) {
2657 case Primitive::kPrimBoolean:
2658 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002659 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002660 break;
2661 }
2662
2663 case Primitive::kPrimShort:
2664 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002665 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002666 break;
2667 }
2668
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002669 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002670 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002671 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002672 break;
2673 }
2674
2675 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002676 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002677 break;
2678 }
2679
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002680 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002681 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002682 break;
2683 }
2684
2685 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002686 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002687 break;
2688 }
2689
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002690 case Primitive::kPrimVoid:
2691 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002692 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002693 }
Calin Juravle52c48962014-12-16 17:02:57 +00002694
Calin Juravle77520bc2015-01-12 18:45:46 +00002695 codegen_->MaybeRecordImplicitNullCheck(instruction);
2696
2697 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2698 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2699 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2700 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
2701 }
2702
Calin Juravle52c48962014-12-16 17:02:57 +00002703 if (is_volatile) {
2704 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2705 }
2706}
2707
2708void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2709 HandleFieldSet(instruction, instruction->GetFieldInfo());
2710}
2711
2712void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2713 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002714}
2715
2716void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002717 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002718}
2719
2720void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002721 HandleFieldGet(instruction, instruction->GetFieldInfo());
2722}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002723
Calin Juravle52c48962014-12-16 17:02:57 +00002724void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2725 HandleFieldGet(instruction);
2726}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002727
Calin Juravle52c48962014-12-16 17:02:57 +00002728void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2729 HandleFieldGet(instruction, instruction->GetFieldInfo());
2730}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002731
Calin Juravle52c48962014-12-16 17:02:57 +00002732void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2733 HandleFieldSet(instruction, instruction->GetFieldInfo());
2734}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002735
Calin Juravle52c48962014-12-16 17:02:57 +00002736void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2737 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002738}
2739
2740void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002741 LocationSummary* locations =
2742 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002743 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2744 ? Location::RequiresRegister()
2745 : Location::Any();
2746 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002747 if (instruction->HasUses()) {
2748 locations->SetOut(Location::SameAsFirstInput());
2749 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002750}
2751
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002752void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002753 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2754 return;
2755 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002756 LocationSummary* locations = instruction->GetLocations();
2757 Location obj = locations->InAt(0);
2758
2759 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2760 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2761}
2762
2763void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002764 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002765 codegen_->AddSlowPath(slow_path);
2766
2767 LocationSummary* locations = instruction->GetLocations();
2768 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002769
2770 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ cmpl(obj.AsRegister<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002772 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002773 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002774 } else {
2775 DCHECK(obj.IsConstant()) << obj;
2776 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2777 __ jmp(slow_path->GetEntryLabel());
2778 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002779 }
2780 __ j(kEqual, slow_path->GetEntryLabel());
2781}
2782
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002783void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2784 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2785 GenerateImplicitNullCheck(instruction);
2786 } else {
2787 GenerateExplicitNullCheck(instruction);
2788 }
2789}
2790
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002791void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002792 LocationSummary* locations =
2793 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002794 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002795 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002796 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2797 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002798}
2799
2800void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2801 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002802 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002803 Location index = locations->InAt(1);
2804
2805 switch (instruction->GetType()) {
2806 case Primitive::kPrimBoolean: {
2807 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002808 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002809 if (index.IsConstant()) {
2810 __ movzxb(out, Address(obj,
2811 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2812 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002814 }
2815 break;
2816 }
2817
2818 case Primitive::kPrimByte: {
2819 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002820 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002821 if (index.IsConstant()) {
2822 __ movsxb(out, Address(obj,
2823 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2824 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002826 }
2827 break;
2828 }
2829
2830 case Primitive::kPrimShort: {
2831 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002832 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002833 if (index.IsConstant()) {
2834 __ movsxw(out, Address(obj,
2835 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2836 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002837 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002838 }
2839 break;
2840 }
2841
2842 case Primitive::kPrimChar: {
2843 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002844 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002845 if (index.IsConstant()) {
2846 __ movzxw(out, Address(obj,
2847 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2848 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002849 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002850 }
2851 break;
2852 }
2853
2854 case Primitive::kPrimInt:
2855 case Primitive::kPrimNot: {
2856 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2857 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002858 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002859 if (index.IsConstant()) {
2860 __ movl(out, Address(obj,
2861 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2862 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002863 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002864 }
2865 break;
2866 }
2867
2868 case Primitive::kPrimLong: {
2869 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002870 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002871 if (index.IsConstant()) {
2872 __ movq(out, Address(obj,
2873 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2874 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002875 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002876 }
2877 break;
2878 }
2879
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002880 case Primitive::kPrimFloat: {
2881 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002882 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002883 if (index.IsConstant()) {
2884 __ movss(out, Address(obj,
2885 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2886 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002887 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002888 }
2889 break;
2890 }
2891
2892 case Primitive::kPrimDouble: {
2893 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002894 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002895 if (index.IsConstant()) {
2896 __ movsd(out, Address(obj,
2897 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2898 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002899 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002900 }
2901 break;
2902 }
2903
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002904 case Primitive::kPrimVoid:
2905 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002906 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002907 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002908 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002909}
2910
2911void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002912 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002913
2914 bool needs_write_barrier =
2915 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2916 bool needs_runtime_call = instruction->NeedsTypeCheck();
2917
Nicolas Geoffray39468442014-09-02 15:17:15 +01002918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002919 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2920 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002921 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002922 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2923 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2924 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002925 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002926 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002927 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002928 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2929 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002930 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002931 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002932 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2933 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002934 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002935 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002936 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002937
2938 if (needs_write_barrier) {
2939 // Temporary registers for the write barrier.
2940 locations->AddTemp(Location::RequiresRegister());
2941 locations->AddTemp(Location::RequiresRegister());
2942 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002943 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002944}
2945
2946void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2947 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002948 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002949 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002950 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002951 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002952 bool needs_runtime_call = locations->WillCall();
2953 bool needs_write_barrier =
2954 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002955
2956 switch (value_type) {
2957 case Primitive::kPrimBoolean:
2958 case Primitive::kPrimByte: {
2959 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002960 if (index.IsConstant()) {
2961 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002962 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002963 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002964 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002965 __ movb(Address(obj, offset),
2966 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002967 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002968 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002969 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002970 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2971 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002972 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002973 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002974 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2975 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002976 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002977 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002978 break;
2979 }
2980
2981 case Primitive::kPrimShort:
2982 case Primitive::kPrimChar: {
2983 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002984 if (index.IsConstant()) {
2985 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002986 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002987 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002988 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002989 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00002990 __ movw(Address(obj, offset),
2991 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002992 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002993 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002994 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002995 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
2997 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002998 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002999 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003000 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003001 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3002 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003003 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003004 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003005 break;
3006 }
3007
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003008 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003009 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003010 if (!needs_runtime_call) {
3011 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3012 if (index.IsConstant()) {
3013 size_t offset =
3014 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3015 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003016 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003017 } else {
3018 DCHECK(value.IsConstant()) << value;
3019 __ movl(Address(obj, offset),
3020 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3021 }
3022 } else {
3023 DCHECK(index.IsRegister()) << index;
3024 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003025 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3026 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003027 } else {
3028 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003029 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003030 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3031 }
3032 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003033 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003034 if (needs_write_barrier) {
3035 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3037 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3038 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003039 }
3040 } else {
3041 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003042 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3043 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003044 DCHECK(!codegen_->IsLeafMethod());
3045 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3046 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003047 break;
3048 }
3049
3050 case Primitive::kPrimLong: {
3051 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003052 if (index.IsConstant()) {
3053 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003054 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003055 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003056 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003057 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003058 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3059 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003060 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003061 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003062 break;
3063 }
3064
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003065 case Primitive::kPrimFloat: {
3066 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3067 if (index.IsConstant()) {
3068 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3069 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003070 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003071 } else {
3072 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003073 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3074 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003075 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003076 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003077 break;
3078 }
3079
3080 case Primitive::kPrimDouble: {
3081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3082 if (index.IsConstant()) {
3083 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3084 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003085 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003086 } else {
3087 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003088 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3089 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003090 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003091 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003092 break;
3093 }
3094
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003095 case Primitive::kPrimVoid:
3096 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003097 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003098 }
3099}
3100
3101void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003102 LocationSummary* locations =
3103 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003104 locations->SetInAt(0, Location::RequiresRegister());
3105 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106}
3107
3108void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3109 LocationSummary* locations = instruction->GetLocations();
3110 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003111 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3112 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003113 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003114 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003115}
3116
3117void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003118 LocationSummary* locations =
3119 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003120 locations->SetInAt(0, Location::RequiresRegister());
3121 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003122 if (instruction->HasUses()) {
3123 locations->SetOut(Location::SameAsFirstInput());
3124 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003125}
3126
3127void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3128 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003129 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003130 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003131 codegen_->AddSlowPath(slow_path);
3132
Roland Levillain271ab9c2014-11-27 15:23:57 +00003133 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
3134 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003135
3136 __ cmpl(index, length);
3137 __ j(kAboveEqual, slow_path->GetEntryLabel());
3138}
3139
3140void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3141 CpuRegister card,
3142 CpuRegister object,
3143 CpuRegister value) {
3144 Label is_null;
3145 __ testl(value, value);
3146 __ j(kEqual, &is_null);
3147 __ gs()->movq(card, Address::Absolute(
3148 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3149 __ movq(temp, object);
3150 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3151 __ movb(Address(temp, card, TIMES_1, 0), card);
3152 __ Bind(&is_null);
3153}
3154
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003155void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3156 temp->SetLocations(nullptr);
3157}
3158
3159void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3160 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003161 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003162}
3163
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003164void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003165 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003166 LOG(FATAL) << "Unimplemented";
3167}
3168
3169void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003170 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3171}
3172
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003173void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3175}
3176
3177void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003178 HBasicBlock* block = instruction->GetBlock();
3179 if (block->GetLoopInformation() != nullptr) {
3180 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3181 // The back edge will generate the suspend check.
3182 return;
3183 }
3184 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3185 // The goto will generate the suspend check.
3186 return;
3187 }
3188 GenerateSuspendCheck(instruction, nullptr);
3189}
3190
3191void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3192 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003193 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003194 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003195 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003196 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003197 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003198 if (successor == nullptr) {
3199 __ j(kNotEqual, slow_path->GetEntryLabel());
3200 __ Bind(slow_path->GetReturnLabel());
3201 } else {
3202 __ j(kEqual, codegen_->GetLabelOf(successor));
3203 __ jmp(slow_path->GetEntryLabel());
3204 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003205}
3206
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003207X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3208 return codegen_->GetAssembler();
3209}
3210
3211void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3212 MoveOperands* move = moves_.Get(index);
3213 Location source = move->GetSource();
3214 Location destination = move->GetDestination();
3215
3216 if (source.IsRegister()) {
3217 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003218 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003219 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003220 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003221 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003222 } else {
3223 DCHECK(destination.IsDoubleStackSlot());
3224 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003225 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003226 }
3227 } else if (source.IsStackSlot()) {
3228 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003230 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003231 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003232 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003233 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003234 } else {
3235 DCHECK(destination.IsStackSlot());
3236 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3237 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3238 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003239 } else if (source.IsDoubleStackSlot()) {
3240 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003241 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003242 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003243 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003244 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3245 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003246 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003247 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003248 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3249 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3250 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003251 } else if (source.IsConstant()) {
3252 HConstant* constant = source.GetConstant();
3253 if (constant->IsIntConstant()) {
3254 Immediate imm(constant->AsIntConstant()->GetValue());
3255 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003256 __ movl(destination.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003257 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003258 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003259 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3260 }
3261 } else if (constant->IsLongConstant()) {
3262 int64_t value = constant->AsLongConstant()->GetValue();
3263 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003264 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003265 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003266 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003267 __ movq(CpuRegister(TMP), Immediate(value));
3268 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3269 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003270 } else if (constant->IsFloatConstant()) {
3271 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3272 if (destination.IsFpuRegister()) {
3273 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003274 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003275 } else {
3276 DCHECK(destination.IsStackSlot()) << destination;
3277 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3278 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003279 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003280 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3281 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3282 if (destination.IsFpuRegister()) {
3283 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003284 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003285 } else {
3286 DCHECK(destination.IsDoubleStackSlot()) << destination;
3287 __ movq(CpuRegister(TMP), imm);
3288 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3289 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003290 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003291 } else if (source.IsFpuRegister()) {
3292 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003293 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003294 } else if (destination.IsStackSlot()) {
3295 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003296 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003297 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003298 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003299 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003300 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003301 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003302 }
3303}
3304
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003305void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003306 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003307 __ movl(Address(CpuRegister(RSP), mem), reg);
3308 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003309}
3310
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003311void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003312 ScratchRegisterScope ensure_scratch(
3313 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3314
3315 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3316 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3317 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3318 Address(CpuRegister(RSP), mem2 + stack_offset));
3319 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3320 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3321 CpuRegister(ensure_scratch.GetRegister()));
3322}
3323
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003324void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3325 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3326 __ movq(Address(CpuRegister(RSP), mem), reg);
3327 __ movq(reg, CpuRegister(TMP));
3328}
3329
3330void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3331 ScratchRegisterScope ensure_scratch(
3332 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3333
3334 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3335 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3336 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3337 Address(CpuRegister(RSP), mem2 + stack_offset));
3338 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3339 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3340 CpuRegister(ensure_scratch.GetRegister()));
3341}
3342
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003343void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3344 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3345 __ movss(Address(CpuRegister(RSP), mem), reg);
3346 __ movd(reg, CpuRegister(TMP));
3347}
3348
3349void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3350 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3351 __ movsd(Address(CpuRegister(RSP), mem), reg);
3352 __ movd(reg, CpuRegister(TMP));
3353}
3354
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003355void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3356 MoveOperands* move = moves_.Get(index);
3357 Location source = move->GetSource();
3358 Location destination = move->GetDestination();
3359
3360 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003361 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003362 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003363 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003364 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003365 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003366 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003367 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3368 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003369 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003370 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003371 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003372 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3373 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003374 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003375 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3376 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3377 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003378 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003379 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003380 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003381 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003382 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003383 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003384 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003385 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003386 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003387 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003388 }
3389}
3390
3391
3392void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3393 __ pushq(CpuRegister(reg));
3394}
3395
3396
3397void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3398 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003399}
3400
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003401void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3402 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3403 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3404 Immediate(mirror::Class::kStatusInitialized));
3405 __ j(kLess, slow_path->GetEntryLabel());
3406 __ Bind(slow_path->GetExitLabel());
3407 // No need for memory fence, thanks to the X86_64 memory model.
3408}
3409
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003410void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003411 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3412 ? LocationSummary::kCallOnSlowPath
3413 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003414 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003415 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003416 locations->SetOut(Location::RequiresRegister());
3417}
3418
3419void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003420 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003421 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003422 DCHECK(!cls->CanCallRuntime());
3423 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003424 codegen_->LoadCurrentMethod(out);
3425 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3426 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003427 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003428 codegen_->LoadCurrentMethod(out);
3429 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3430 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003431 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3432 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3433 codegen_->AddSlowPath(slow_path);
3434 __ testl(out, out);
3435 __ j(kEqual, slow_path->GetEntryLabel());
3436 if (cls->MustGenerateClinitCheck()) {
3437 GenerateClassInitializationCheck(slow_path, out);
3438 } else {
3439 __ Bind(slow_path->GetExitLabel());
3440 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003441 }
3442}
3443
3444void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3445 LocationSummary* locations =
3446 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3447 locations->SetInAt(0, Location::RequiresRegister());
3448 if (check->HasUses()) {
3449 locations->SetOut(Location::SameAsFirstInput());
3450 }
3451}
3452
3453void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003454 // We assume the class to not be null.
3455 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3456 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003457 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003458 GenerateClassInitializationCheck(slow_path,
3459 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003460}
3461
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003462void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3463 LocationSummary* locations =
3464 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3465 locations->SetOut(Location::RequiresRegister());
3466}
3467
3468void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3469 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3470 codegen_->AddSlowPath(slow_path);
3471
Roland Levillain271ab9c2014-11-27 15:23:57 +00003472 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003473 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003474 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3475 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003476 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3477 __ testl(out, out);
3478 __ j(kEqual, slow_path->GetEntryLabel());
3479 __ Bind(slow_path->GetExitLabel());
3480}
3481
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003482void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3483 LocationSummary* locations =
3484 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3485 locations->SetOut(Location::RequiresRegister());
3486}
3487
3488void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3489 Address address = Address::Absolute(
3490 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003491 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003492 __ gs()->movl(address, Immediate(0));
3493}
3494
3495void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3496 LocationSummary* locations =
3497 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3498 InvokeRuntimeCallingConvention calling_convention;
3499 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3500}
3501
3502void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3503 __ gs()->call(
3504 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3505 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3506}
3507
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003508void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003509 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3510 ? LocationSummary::kNoCall
3511 : LocationSummary::kCallOnSlowPath;
3512 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3513 locations->SetInAt(0, Location::RequiresRegister());
3514 locations->SetInAt(1, Location::Any());
3515 locations->SetOut(Location::RequiresRegister());
3516}
3517
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003518void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003519 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003520 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003521 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003522 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003523 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3524 Label done, zero;
3525 SlowPathCodeX86_64* slow_path = nullptr;
3526
3527 // Return 0 if `obj` is null.
3528 // TODO: avoid this check if we know obj is not null.
3529 __ testl(obj, obj);
3530 __ j(kEqual, &zero);
3531 // Compare the class of `obj` with `cls`.
3532 __ movl(out, Address(obj, class_offset));
3533 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003534 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003535 } else {
3536 DCHECK(cls.IsStackSlot()) << cls;
3537 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3538 }
3539 if (instruction->IsClassFinal()) {
3540 // Classes must be equal for the instanceof to succeed.
3541 __ j(kNotEqual, &zero);
3542 __ movl(out, Immediate(1));
3543 __ jmp(&done);
3544 } else {
3545 // If the classes are not equal, we go into a slow path.
3546 DCHECK(locations->OnlyCallsOnSlowPath());
3547 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003548 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003549 codegen_->AddSlowPath(slow_path);
3550 __ j(kNotEqual, slow_path->GetEntryLabel());
3551 __ movl(out, Immediate(1));
3552 __ jmp(&done);
3553 }
3554 __ Bind(&zero);
3555 __ movl(out, Immediate(0));
3556 if (slow_path != nullptr) {
3557 __ Bind(slow_path->GetExitLabel());
3558 }
3559 __ Bind(&done);
3560}
3561
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003562void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3563 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3564 instruction, LocationSummary::kCallOnSlowPath);
3565 locations->SetInAt(0, Location::RequiresRegister());
3566 locations->SetInAt(1, Location::Any());
3567 locations->AddTemp(Location::RequiresRegister());
3568}
3569
3570void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3571 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003572 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003573 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003574 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003575 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3576 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3577 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3578 codegen_->AddSlowPath(slow_path);
3579
3580 // TODO: avoid this check if we know obj is not null.
3581 __ testl(obj, obj);
3582 __ j(kEqual, slow_path->GetExitLabel());
3583 // Compare the class of `obj` with `cls`.
3584 __ movl(temp, Address(obj, class_offset));
3585 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003586 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003587 } else {
3588 DCHECK(cls.IsStackSlot()) << cls;
3589 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3590 }
3591 // Classes must be equal for the checkcast to succeed.
3592 __ j(kNotEqual, slow_path->GetEntryLabel());
3593 __ Bind(slow_path->GetExitLabel());
3594}
3595
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003596void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3597 LocationSummary* locations =
3598 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3599 InvokeRuntimeCallingConvention calling_convention;
3600 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3601}
3602
3603void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3604 __ gs()->call(Address::Absolute(instruction->IsEnter()
3605 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3606 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3607 true));
3608 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3609}
3610
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003611void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3612void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3613void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3614
3615void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3616 LocationSummary* locations =
3617 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3618 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3619 || instruction->GetResultType() == Primitive::kPrimLong);
3620 locations->SetInAt(0, Location::RequiresRegister());
3621 if (instruction->GetType() == Primitive::kPrimInt) {
3622 locations->SetInAt(1, Location::Any());
3623 } else {
3624 // Request a register to avoid loading a 64bits constant.
3625 locations->SetInAt(1, Location::RequiresRegister());
3626 }
3627 locations->SetOut(Location::SameAsFirstInput());
3628}
3629
3630void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3631 HandleBitwiseOperation(instruction);
3632}
3633
3634void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3635 HandleBitwiseOperation(instruction);
3636}
3637
3638void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3639 HandleBitwiseOperation(instruction);
3640}
3641
3642void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3643 LocationSummary* locations = instruction->GetLocations();
3644 Location first = locations->InAt(0);
3645 Location second = locations->InAt(1);
3646 DCHECK(first.Equals(locations->Out()));
3647
3648 if (instruction->GetResultType() == Primitive::kPrimInt) {
3649 if (second.IsRegister()) {
3650 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003651 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003652 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003653 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003654 } else {
3655 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003656 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003657 }
3658 } else if (second.IsConstant()) {
3659 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3660 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003661 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003662 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003663 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003664 } else {
3665 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003666 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003667 }
3668 } else {
3669 Address address(CpuRegister(RSP), second.GetStackIndex());
3670 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003671 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003672 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003673 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003674 } else {
3675 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003676 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003677 }
3678 }
3679 } else {
3680 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3681 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003682 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003683 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003684 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003685 } else {
3686 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003687 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003688 }
3689 }
3690}
3691
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003692} // namespace x86_64
3693} // namespace art