blob: 6bc28ff247356f332b48829c91e97d427c7a630b [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
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040static constexpr int kCurrentMethodStackOffset = 0;
41
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
43static constexpr size_t kRuntimeParameterCoreRegistersLength =
44 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000045static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
46static constexpr size_t kRuntimeParameterFpuRegistersLength =
47 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000048static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000049static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
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 SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
134 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000135
136 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100137 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100139 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
141 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100142 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100143 if (successor_ == nullptr) {
144 __ jmp(GetReturnLabel());
145 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100146 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100147 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 }
149
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100150 Label* GetReturnLabel() {
151 DCHECK(successor_ == nullptr);
152 return &return_label_;
153 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000154
155 private:
156 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 Label return_label_;
159
160 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
161};
162
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100163class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100164 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100165 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
166 Location index_location,
167 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100168 : instruction_(instruction),
169 index_location_(index_location),
170 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171
172 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000174 // We're moving two locations to locations that could overlap, so we need a parallel
175 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000177 codegen->EmitParallelMoves(
178 index_location_,
179 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
180 length_location_,
181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100182 __ gs()->call(Address::Absolute(
183 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100184 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 }
186
187 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100188 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100189 const Location index_location_;
190 const Location length_location_;
191
192 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
193};
194
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000195class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100196 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197 LoadClassSlowPathX86_64(HLoadClass* cls,
198 HInstruction* at,
199 uint32_t dex_pc,
200 bool do_clinit)
201 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
202 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
203 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204
205 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100207 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
208 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 codegen->SaveLiveRegisters(locations);
211
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 __ gs()->call(Address::Absolute((do_clinit_
216 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
217 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
218 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100219
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000220 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000222 if (out.IsValid()) {
223 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
224 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 }
226
227 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 __ jmp(GetExitLabel());
229 }
230
231 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232 // The class this slow path will load.
233 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000235 // The instruction where this slow path is happening.
236 // (Might be the load class or an initialization check).
237 HInstruction* const at_;
238
239 // The dex PC of `at_`.
240 const uint32_t dex_pc_;
241
242 // Whether to initialize the class.
243 const bool do_clinit_;
244
245 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100246};
247
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000248class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
249 public:
250 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
251
252 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
253 LocationSummary* locations = instruction_->GetLocations();
254 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
255
256 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
257 __ Bind(GetEntryLabel());
258 codegen->SaveLiveRegisters(locations);
259
260 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800261 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
262 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000263 Immediate(instruction_->GetStringIndex()));
264 __ gs()->call(Address::Absolute(
265 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
266 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
267 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
268 codegen->RestoreLiveRegisters(locations);
269 __ jmp(GetExitLabel());
270 }
271
272 private:
273 HLoadString* const instruction_;
274
275 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
276};
277
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
279 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 TypeCheckSlowPathX86_64(HInstruction* instruction,
281 Location class_to_check,
282 Location object_class,
283 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000285 class_to_check_(class_to_check),
286 object_class_(object_class),
287 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
289 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
290 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 DCHECK(instruction_->IsCheckCast()
292 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293
294 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
295 __ Bind(GetEntryLabel());
296 codegen->SaveLiveRegisters(locations);
297
298 // We're moving two locations to locations that could overlap, so we need a parallel
299 // move resolver.
300 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000301 codegen->EmitParallelMoves(
302 class_to_check_,
303 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
304 object_class_,
305 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 if (instruction_->IsInstanceOf()) {
308 __ gs()->call(
309 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
310 } else {
311 DCHECK(instruction_->IsCheckCast());
312 __ gs()->call(
313 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
314 }
315 codegen->RecordPcInfo(instruction_, dex_pc_);
316
317 if (instruction_->IsInstanceOf()) {
318 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
319 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 codegen->RestoreLiveRegisters(locations);
322 __ jmp(GetExitLabel());
323 }
324
325 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000326 HInstruction* const instruction_;
327 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
331 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
332};
333
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100334#undef __
335#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
336
Dave Allison20dfc792014-06-16 20:44:29 -0700337inline Condition X86_64Condition(IfCondition cond) {
338 switch (cond) {
339 case kCondEQ: return kEqual;
340 case kCondNE: return kNotEqual;
341 case kCondLT: return kLess;
342 case kCondLE: return kLessEqual;
343 case kCondGT: return kGreater;
344 case kCondGE: return kGreaterEqual;
345 default:
346 LOG(FATAL) << "Unknown if condition";
347 }
348 return kEqual;
349}
350
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800351void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
352 CpuRegister temp) {
353 // All registers are assumed to be correctly set up.
354
355 // TODO: Implement all kinds of calls:
356 // 1) boot -> boot
357 // 2) app -> boot
358 // 3) app -> app
359 //
360 // Currently we implement the app -> app logic, which looks up in the resolve cache.
361
362 // temp = method;
363 LoadCurrentMethod(temp);
364 // temp = temp->dex_cache_resolved_methods_;
365 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
366 // temp = temp[index_in_cache]
367 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
368 // (temp + offset_of_quick_compiled_code)()
369 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
370 kX86_64WordSize).SizeValue()));
371
372 DCHECK(!IsLeafMethod());
373 RecordPcInfo(invoke, invoke->GetDexPc());
374}
375
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100376void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
377 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
378}
379
380void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
381 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
382}
383
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100384size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
385 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
386 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100387}
388
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100389size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
390 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
391 return kX86_64WordSize;
392}
393
394size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
395 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
396 return kX86_64WordSize;
397}
398
399size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
400 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
401 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100402}
403
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000404static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000405// Use a fake return address register to mimic Quick.
406static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000407CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph, const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000408 : CodeGenerator(graph,
409 kNumberOfCpuRegisters,
410 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000411 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000412 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
413 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000414 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000415 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
416 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000417 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100418 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100419 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000420 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000421 move_resolver_(graph->GetArena(), this) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000422 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
423}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100424
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100425InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
426 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100427 : HGraphVisitor(graph),
428 assembler_(codegen->GetAssembler()),
429 codegen_(codegen) {}
430
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100431Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100432 switch (type) {
433 case Primitive::kPrimLong:
434 case Primitive::kPrimByte:
435 case Primitive::kPrimBoolean:
436 case Primitive::kPrimChar:
437 case Primitive::kPrimShort:
438 case Primitive::kPrimInt:
439 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100441 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100442 }
443
444 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100445 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100446 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100447 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100448 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100449
450 case Primitive::kPrimVoid:
451 LOG(FATAL) << "Unreachable type " << type;
452 }
453
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100454 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100455}
456
Nicolas Geoffray98893962015-01-21 12:32:32 +0000457void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100458 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100459 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100460
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000461 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100462 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000463
Nicolas Geoffray98893962015-01-21 12:32:32 +0000464 if (is_baseline) {
465 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
466 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
467 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000468 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
469 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
470 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000471 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100472}
473
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100474void CodeGeneratorX86_64::GenerateFrameEntry() {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100475 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700476 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000477 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100478
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000479 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100480 __ testq(CpuRegister(RAX), Address(
481 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100482 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100483 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000484
Nicolas Geoffray98893962015-01-21 12:32:32 +0000485 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000486 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000487 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000488 __ pushq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000489 }
490 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100491
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000492 __ subq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
493 uint32_t xmm_spill_location = GetFpuSpillStart();
494 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100495
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000496 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
497 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
498 __ movsd(Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)),
499 XmmRegister(kFpuCalleeSaves[i]));
500 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100501 }
502
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100503 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
504}
505
506void CodeGeneratorX86_64::GenerateFrameExit() {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000507 uint32_t xmm_spill_location = GetFpuSpillStart();
508 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
509 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
510 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
511 __ movsd(XmmRegister(kFpuCalleeSaves[i]),
512 Address(CpuRegister(RSP), xmm_spill_location + (xmm_spill_slot_size * i)));
513 }
514 }
515
516 __ addq(CpuRegister(RSP), Immediate(GetFrameSize() - GetCoreSpillSize()));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000517
518 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000519 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000520 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000521 __ popq(CpuRegister(reg));
Nicolas Geoffray98893962015-01-21 12:32:32 +0000522 }
523 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100524}
525
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100526void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
527 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100528}
529
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100530void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100531 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
532}
533
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100534Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
535 switch (load->GetType()) {
536 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100538 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
539 break;
540
541 case Primitive::kPrimInt:
542 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100543 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100545
546 case Primitive::kPrimBoolean:
547 case Primitive::kPrimByte:
548 case Primitive::kPrimChar:
549 case Primitive::kPrimShort:
550 case Primitive::kPrimVoid:
551 LOG(FATAL) << "Unexpected type " << load->GetType();
552 }
553
554 LOG(FATAL) << "Unreachable";
555 return Location();
556}
557
558void CodeGeneratorX86_64::Move(Location destination, Location source) {
559 if (source.Equals(destination)) {
560 return;
561 }
562 if (destination.IsRegister()) {
563 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000564 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100565 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000566 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100567 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000568 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100569 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100570 } else {
571 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000572 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100573 Address(CpuRegister(RSP), source.GetStackIndex()));
574 }
575 } else if (destination.IsFpuRegister()) {
576 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000577 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000579 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100580 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 Address(CpuRegister(RSP), source.GetStackIndex()));
583 } else {
584 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000585 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100587 }
588 } else if (destination.IsStackSlot()) {
589 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000591 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100592 } else if (source.IsFpuRegister()) {
593 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000594 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500595 } else if (source.IsConstant()) {
596 HConstant* constant = source.GetConstant();
597 int32_t value;
598 if (constant->IsFloatConstant()) {
599 value = bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue());
600 } else {
601 DCHECK(constant->IsIntConstant());
602 value = constant->AsIntConstant()->GetValue();
603 }
604 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500606 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000607 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
608 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100609 }
610 } else {
611 DCHECK(destination.IsDoubleStackSlot());
612 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100613 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000614 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100615 } else if (source.IsFpuRegister()) {
616 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000617 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500618 } else if (source.IsConstant()) {
619 HConstant* constant = source.GetConstant();
620 int64_t value = constant->AsLongConstant()->GetValue();
621 if (constant->IsDoubleConstant()) {
622 value = bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue());
623 } else {
624 DCHECK(constant->IsLongConstant());
625 value = constant->AsLongConstant()->GetValue();
626 }
627 __ movq(CpuRegister(TMP), Immediate(value));
628 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100629 } else {
630 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000631 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
632 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100633 }
634 }
635}
636
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100637void CodeGeneratorX86_64::Move(HInstruction* instruction,
638 Location location,
639 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000640 LocationSummary* locations = instruction->GetLocations();
641 if (locations != nullptr && locations->Out().Equals(location)) {
642 return;
643 }
644
645 if (locations != nullptr && locations->Out().IsConstant()) {
646 HConstant* const_to_move = locations->Out().GetConstant();
647 if (const_to_move->IsIntConstant()) {
648 Immediate imm(const_to_move->AsIntConstant()->GetValue());
649 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000651 } else if (location.IsStackSlot()) {
652 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
653 } else {
654 DCHECK(location.IsConstant());
655 DCHECK_EQ(location.GetConstant(), const_to_move);
656 }
657 } else if (const_to_move->IsLongConstant()) {
658 int64_t value = const_to_move->AsLongConstant()->GetValue();
659 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000660 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000661 } else if (location.IsDoubleStackSlot()) {
662 __ movq(CpuRegister(TMP), Immediate(value));
663 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
664 } else {
665 DCHECK(location.IsConstant());
666 DCHECK_EQ(location.GetConstant(), const_to_move);
667 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100668 }
Roland Levillain476df552014-10-09 17:51:36 +0100669 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100670 switch (instruction->GetType()) {
671 case Primitive::kPrimBoolean:
672 case Primitive::kPrimByte:
673 case Primitive::kPrimChar:
674 case Primitive::kPrimShort:
675 case Primitive::kPrimInt:
676 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100677 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100678 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
679 break;
680
681 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100682 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000683 Move(location,
684 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100685 break;
686
687 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100688 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100689 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000690 } else if (instruction->IsTemporary()) {
691 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
692 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100693 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100694 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100695 switch (instruction->GetType()) {
696 case Primitive::kPrimBoolean:
697 case Primitive::kPrimByte:
698 case Primitive::kPrimChar:
699 case Primitive::kPrimShort:
700 case Primitive::kPrimInt:
701 case Primitive::kPrimNot:
702 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100703 case Primitive::kPrimFloat:
704 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000705 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100706 break;
707
708 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100709 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100710 }
711 }
712}
713
714void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
715 got->SetLocations(nullptr);
716}
717
718void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
719 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100720 DCHECK(!successor->IsExitBlock());
721
722 HBasicBlock* block = got->GetBlock();
723 HInstruction* previous = got->GetPrevious();
724
725 HLoopInformation* info = block->GetLoopInformation();
726 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
727 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
728 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
729 return;
730 }
731
732 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
733 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
734 }
735 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100736 __ jmp(codegen_->GetLabelOf(successor));
737 }
738}
739
740void LocationsBuilderX86_64::VisitExit(HExit* exit) {
741 exit->SetLocations(nullptr);
742}
743
744void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700745 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100746 if (kIsDebugBuild) {
747 __ Comment("Unreachable");
748 __ int3();
749 }
750}
751
752void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100753 LocationSummary* locations =
754 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100755 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100756 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100757 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100758 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100759}
760
761void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700762 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100763 if (cond->IsIntConstant()) {
764 // Constant condition, statically compared against 1.
765 int32_t cond_value = cond->AsIntConstant()->GetValue();
766 if (cond_value == 1) {
767 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
768 if_instr->IfTrueSuccessor())) {
769 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100770 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100771 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100772 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100773 DCHECK_EQ(cond_value, 0);
774 }
775 } else {
776 bool materialized =
777 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
778 // Moves do not affect the eflags register, so if the condition is
779 // evaluated just before the if, we don't need to evaluate it
780 // again.
781 bool eflags_set = cond->IsCondition()
782 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
783 if (materialized) {
784 if (!eflags_set) {
785 // Materialized condition, compare against 0.
786 Location lhs = if_instr->GetLocations()->InAt(0);
787 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000788 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100789 } else {
790 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
791 Immediate(0));
792 }
793 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
794 } else {
795 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
796 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
797 }
798 } else {
799 Location lhs = cond->GetLocations()->InAt(0);
800 Location rhs = cond->GetLocations()->InAt(1);
801 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000802 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100803 } else if (rhs.IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000804 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100805 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
806 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000807 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100808 Address(CpuRegister(RSP), rhs.GetStackIndex()));
809 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100810 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
811 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700812 }
Dave Allison20dfc792014-06-16 20:44:29 -0700813 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100814 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
815 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700816 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100817 }
818}
819
820void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
821 local->SetLocations(nullptr);
822}
823
824void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
825 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
826}
827
828void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
829 local->SetLocations(nullptr);
830}
831
832void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
833 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700834 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100835}
836
837void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100838 LocationSummary* locations =
839 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100840 switch (store->InputAt(1)->GetType()) {
841 case Primitive::kPrimBoolean:
842 case Primitive::kPrimByte:
843 case Primitive::kPrimChar:
844 case Primitive::kPrimShort:
845 case Primitive::kPrimInt:
846 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100847 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100848 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
849 break;
850
851 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100852 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100853 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
854 break;
855
856 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100857 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100858 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859}
860
861void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700862 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100863}
864
Dave Allison20dfc792014-06-16 20:44:29 -0700865void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100866 LocationSummary* locations =
867 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100868 locations->SetInAt(0, Location::RequiresRegister());
869 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100870 if (comp->NeedsMaterialization()) {
871 locations->SetOut(Location::RequiresRegister());
872 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100873}
874
Dave Allison20dfc792014-06-16 20:44:29 -0700875void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
876 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100877 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100879 // Clear register: setcc only sets the low byte.
880 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100881 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000882 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
883 locations->InAt(1).AsRegister<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100884 } else if (locations->InAt(1).IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000885 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100886 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
887 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000888 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100889 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
890 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100891 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700892 }
893}
894
895void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
896 VisitCondition(comp);
897}
898
899void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
900 VisitCondition(comp);
901}
902
903void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
904 VisitCondition(comp);
905}
906
907void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
908 VisitCondition(comp);
909}
910
911void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
912 VisitCondition(comp);
913}
914
915void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
916 VisitCondition(comp);
917}
918
919void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
920 VisitCondition(comp);
921}
922
923void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
924 VisitCondition(comp);
925}
926
927void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
928 VisitCondition(comp);
929}
930
931void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
932 VisitCondition(comp);
933}
934
935void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
936 VisitCondition(comp);
937}
938
939void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
940 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100941}
942
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100943void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100944 LocationSummary* locations =
945 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000946 switch (compare->InputAt(0)->GetType()) {
947 case Primitive::kPrimLong: {
948 locations->SetInAt(0, Location::RequiresRegister());
949 locations->SetInAt(1, Location::RequiresRegister());
950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
951 break;
952 }
953 case Primitive::kPrimFloat:
954 case Primitive::kPrimDouble: {
955 locations->SetInAt(0, Location::RequiresFpuRegister());
956 locations->SetInAt(1, Location::RequiresFpuRegister());
957 locations->SetOut(Location::RequiresRegister());
958 break;
959 }
960 default:
961 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
962 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100963}
964
965void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100966 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000967 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000968 Location left = locations->InAt(0);
969 Location right = locations->InAt(1);
970
971 Label less, greater, done;
972 Primitive::Type type = compare->InputAt(0)->GetType();
973 switch (type) {
974 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000975 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100976 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000977 }
978 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000979 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000980 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
981 break;
982 }
983 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000984 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000985 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
986 break;
987 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100988 default:
Calin Juravleddb7df22014-11-25 20:56:51 +0000989 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100990 }
Calin Juravleddb7df22014-11-25 20:56:51 +0000991 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +0000992 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +0000993 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +0000994
Calin Juravle91debbc2014-11-26 19:01:09 +0000995 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +0000996 __ movl(out, Immediate(1));
997 __ jmp(&done);
998
999 __ Bind(&less);
1000 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001001
1002 __ Bind(&done);
1003}
1004
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001005void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001006 LocationSummary* locations =
1007 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001008 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001009}
1010
1011void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001012 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001013 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001014}
1015
1016void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001017 LocationSummary* locations =
1018 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001019 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001020}
1021
1022void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001023 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001024 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001025}
1026
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001027void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1028 LocationSummary* locations =
1029 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1030 locations->SetOut(Location::ConstantLocation(constant));
1031}
1032
1033void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1034 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001035 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001036}
1037
1038void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1039 LocationSummary* locations =
1040 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1041 locations->SetOut(Location::ConstantLocation(constant));
1042}
1043
1044void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1045 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001046 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001047}
1048
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001049void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1050 ret->SetLocations(nullptr);
1051}
1052
1053void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001054 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001055 codegen_->GenerateFrameExit();
1056 __ ret();
1057}
1058
1059void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001060 LocationSummary* locations =
1061 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062 switch (ret->InputAt(0)->GetType()) {
1063 case Primitive::kPrimBoolean:
1064 case Primitive::kPrimByte:
1065 case Primitive::kPrimChar:
1066 case Primitive::kPrimShort:
1067 case Primitive::kPrimInt:
1068 case Primitive::kPrimNot:
1069 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001070 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001071 break;
1072
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 case Primitive::kPrimFloat:
1074 case Primitive::kPrimDouble:
1075 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001076 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001077 break;
1078
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001079 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001082}
1083
1084void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1085 if (kIsDebugBuild) {
1086 switch (ret->InputAt(0)->GetType()) {
1087 case Primitive::kPrimBoolean:
1088 case Primitive::kPrimByte:
1089 case Primitive::kPrimChar:
1090 case Primitive::kPrimShort:
1091 case Primitive::kPrimInt:
1092 case Primitive::kPrimNot:
1093 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001094 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001095 break;
1096
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 case Primitive::kPrimFloat:
1098 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001099 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001100 XMM0);
1101 break;
1102
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001103 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001104 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001105 }
1106 }
1107 codegen_->GenerateFrameExit();
1108 __ ret();
1109}
1110
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001111Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1112 switch (type) {
1113 case Primitive::kPrimBoolean:
1114 case Primitive::kPrimByte:
1115 case Primitive::kPrimChar:
1116 case Primitive::kPrimShort:
1117 case Primitive::kPrimInt:
1118 case Primitive::kPrimNot: {
1119 uint32_t index = gp_index_++;
1120 stack_index_++;
1121 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001122 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001123 } else {
1124 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1125 }
1126 }
1127
1128 case Primitive::kPrimLong: {
1129 uint32_t index = gp_index_;
1130 stack_index_ += 2;
1131 if (index < calling_convention.GetNumberOfRegisters()) {
1132 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001133 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001134 } else {
1135 gp_index_ += 2;
1136 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1137 }
1138 }
1139
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001140 case Primitive::kPrimFloat: {
1141 uint32_t index = fp_index_++;
1142 stack_index_++;
1143 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001144 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001145 } else {
1146 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1147 }
1148 }
1149
1150 case Primitive::kPrimDouble: {
1151 uint32_t index = fp_index_++;
1152 stack_index_ += 2;
1153 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001155 } else {
1156 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1157 }
1158 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001159
1160 case Primitive::kPrimVoid:
1161 LOG(FATAL) << "Unexpected parameter type " << type;
1162 break;
1163 }
1164 return Location();
1165}
1166
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001167void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001168 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1169 if (intrinsic.TryDispatch(invoke)) {
1170 return;
1171 }
1172
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001173 HandleInvoke(invoke);
1174}
1175
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001176static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1177 if (invoke->GetLocations()->Intrinsified()) {
1178 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1179 intrinsic.Dispatch(invoke);
1180 return true;
1181 }
1182 return false;
1183}
1184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001185void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001186 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1187 return;
1188 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001189
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001190 codegen_->GenerateStaticOrDirectCall(
1191 invoke,
1192 invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001193}
1194
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001195void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001196 LocationSummary* locations =
1197 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001198 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001199
1200 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001201 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001202 HInstruction* input = invoke->InputAt(i);
1203 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1204 }
1205
1206 switch (invoke->GetType()) {
1207 case Primitive::kPrimBoolean:
1208 case Primitive::kPrimByte:
1209 case Primitive::kPrimChar:
1210 case Primitive::kPrimShort:
1211 case Primitive::kPrimInt:
1212 case Primitive::kPrimNot:
1213 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001214 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001215 break;
1216
1217 case Primitive::kPrimVoid:
1218 break;
1219
1220 case Primitive::kPrimDouble:
1221 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001222 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001223 break;
1224 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001225}
1226
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001227void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001228 IntrinsicLocationsBuilderX86_64 intrinsic(GetGraph()->GetArena());
1229 if (intrinsic.TryDispatch(invoke)) {
1230 return;
1231 }
1232
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001233 HandleInvoke(invoke);
1234}
1235
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001236void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001237 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1238 return;
1239 }
1240
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001242 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1243 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1244 LocationSummary* locations = invoke->GetLocations();
1245 Location receiver = locations->InAt(0);
1246 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1247 // temp = object->GetClass();
1248 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1250 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001251 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001252 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001253 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001254 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001255 // temp = temp->GetMethodAt(method_offset);
1256 __ movl(temp, Address(temp, method_offset));
1257 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001258 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001259 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001261 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001262 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001263}
1264
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001265void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1266 HandleInvoke(invoke);
1267 // Add the hidden argument.
1268 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1269}
1270
1271void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1272 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001273 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001274 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1275 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1276 LocationSummary* locations = invoke->GetLocations();
1277 Location receiver = locations->InAt(0);
1278 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1279
1280 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001281 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001282 Immediate(invoke->GetDexMethodIndex()));
1283
1284 // temp = object->GetClass();
1285 if (receiver.IsStackSlot()) {
1286 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1287 __ movl(temp, Address(temp, class_offset));
1288 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001290 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001291 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001292 // temp = temp->GetImtEntryAt(method_offset);
1293 __ movl(temp, Address(temp, method_offset));
1294 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001295 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001296 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001297
1298 DCHECK(!codegen_->IsLeafMethod());
1299 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1300}
1301
Roland Levillain88cb1752014-10-20 16:36:47 +01001302void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1303 LocationSummary* locations =
1304 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1305 switch (neg->GetResultType()) {
1306 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001307 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001308 locations->SetInAt(0, Location::RequiresRegister());
1309 locations->SetOut(Location::SameAsFirstInput());
1310 break;
1311
Roland Levillain88cb1752014-10-20 16:36:47 +01001312 case Primitive::kPrimFloat:
1313 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001314 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001315 locations->SetOut(Location::SameAsFirstInput());
1316 locations->AddTemp(Location::RequiresRegister());
1317 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001318 break;
1319
1320 default:
1321 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1322 }
1323}
1324
1325void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1326 LocationSummary* locations = neg->GetLocations();
1327 Location out = locations->Out();
1328 Location in = locations->InAt(0);
1329 switch (neg->GetResultType()) {
1330 case Primitive::kPrimInt:
1331 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001332 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001333 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001334 break;
1335
1336 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001337 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001338 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001339 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001340 break;
1341
Roland Levillain5368c212014-11-27 15:03:41 +00001342 case Primitive::kPrimFloat: {
1343 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001344 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1345 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001346 // Implement float negation with an exclusive or with value
1347 // 0x80000000 (mask for bit 31, representing the sign of a
1348 // single-precision floating-point number).
1349 __ movq(constant, Immediate(INT64_C(0x80000000)));
1350 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001351 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001352 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001353 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001354
Roland Levillain5368c212014-11-27 15:03:41 +00001355 case Primitive::kPrimDouble: {
1356 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001357 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1358 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001359 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001360 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001361 // a double-precision floating-point number).
1362 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1363 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001364 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001365 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001366 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001367
1368 default:
1369 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1370 }
1371}
1372
Roland Levillaindff1f282014-11-05 14:15:05 +00001373void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1374 LocationSummary* locations =
1375 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1376 Primitive::Type result_type = conversion->GetResultType();
1377 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001378 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001379 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001380 case Primitive::kPrimByte:
1381 switch (input_type) {
1382 case Primitive::kPrimShort:
1383 case Primitive::kPrimInt:
1384 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001385 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001386 locations->SetInAt(0, Location::Any());
1387 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1388 break;
1389
1390 default:
1391 LOG(FATAL) << "Unexpected type conversion from " << input_type
1392 << " to " << result_type;
1393 }
1394 break;
1395
Roland Levillain01a8d712014-11-14 16:27:39 +00001396 case Primitive::kPrimShort:
1397 switch (input_type) {
1398 case Primitive::kPrimByte:
1399 case Primitive::kPrimInt:
1400 case Primitive::kPrimChar:
1401 // Processing a Dex `int-to-short' instruction.
1402 locations->SetInAt(0, Location::Any());
1403 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1404 break;
1405
1406 default:
1407 LOG(FATAL) << "Unexpected type conversion from " << input_type
1408 << " to " << result_type;
1409 }
1410 break;
1411
Roland Levillain946e1432014-11-11 17:35:19 +00001412 case Primitive::kPrimInt:
1413 switch (input_type) {
1414 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001415 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001416 locations->SetInAt(0, Location::Any());
1417 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1418 break;
1419
1420 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001421 // Processing a Dex `float-to-int' instruction.
1422 locations->SetInAt(0, Location::RequiresFpuRegister());
1423 locations->SetOut(Location::RequiresRegister());
1424 locations->AddTemp(Location::RequiresFpuRegister());
1425 break;
1426
Roland Levillain946e1432014-11-11 17:35:19 +00001427 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001428 // Processing a Dex `double-to-int' instruction.
1429 locations->SetInAt(0, Location::RequiresFpuRegister());
1430 locations->SetOut(Location::RequiresRegister());
1431 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001432 break;
1433
1434 default:
1435 LOG(FATAL) << "Unexpected type conversion from " << input_type
1436 << " to " << result_type;
1437 }
1438 break;
1439
Roland Levillaindff1f282014-11-05 14:15:05 +00001440 case Primitive::kPrimLong:
1441 switch (input_type) {
1442 case Primitive::kPrimByte:
1443 case Primitive::kPrimShort:
1444 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001445 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001446 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001447 // TODO: We would benefit from a (to-be-implemented)
1448 // Location::RegisterOrStackSlot requirement for this input.
1449 locations->SetInAt(0, Location::RequiresRegister());
1450 locations->SetOut(Location::RequiresRegister());
1451 break;
1452
1453 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001454 // Processing a Dex `float-to-long' instruction.
1455 locations->SetInAt(0, Location::RequiresFpuRegister());
1456 locations->SetOut(Location::RequiresRegister());
1457 locations->AddTemp(Location::RequiresFpuRegister());
1458 break;
1459
Roland Levillaindff1f282014-11-05 14:15:05 +00001460 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001461 // Processing a Dex `double-to-long' instruction.
1462 locations->SetInAt(0, Location::RequiresFpuRegister());
1463 locations->SetOut(Location::RequiresRegister());
1464 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001465 break;
1466
1467 default:
1468 LOG(FATAL) << "Unexpected type conversion from " << input_type
1469 << " to " << result_type;
1470 }
1471 break;
1472
Roland Levillain981e4542014-11-14 11:47:14 +00001473 case Primitive::kPrimChar:
1474 switch (input_type) {
1475 case Primitive::kPrimByte:
1476 case Primitive::kPrimShort:
1477 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001478 // Processing a Dex `int-to-char' instruction.
1479 locations->SetInAt(0, Location::Any());
1480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1481 break;
1482
1483 default:
1484 LOG(FATAL) << "Unexpected type conversion from " << input_type
1485 << " to " << result_type;
1486 }
1487 break;
1488
Roland Levillaindff1f282014-11-05 14:15:05 +00001489 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001490 switch (input_type) {
1491 case Primitive::kPrimByte:
1492 case Primitive::kPrimShort:
1493 case Primitive::kPrimInt:
1494 case Primitive::kPrimChar:
1495 // Processing a Dex `int-to-float' instruction.
1496 locations->SetInAt(0, Location::RequiresRegister());
1497 locations->SetOut(Location::RequiresFpuRegister());
1498 break;
1499
1500 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001501 // Processing a Dex `long-to-float' instruction.
1502 locations->SetInAt(0, Location::RequiresRegister());
1503 locations->SetOut(Location::RequiresFpuRegister());
1504 break;
1505
Roland Levillaincff13742014-11-17 14:32:17 +00001506 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001507 // Processing a Dex `double-to-float' instruction.
1508 locations->SetInAt(0, Location::RequiresFpuRegister());
1509 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001510 break;
1511
1512 default:
1513 LOG(FATAL) << "Unexpected type conversion from " << input_type
1514 << " to " << result_type;
1515 };
1516 break;
1517
Roland Levillaindff1f282014-11-05 14:15:05 +00001518 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001519 switch (input_type) {
1520 case Primitive::kPrimByte:
1521 case Primitive::kPrimShort:
1522 case Primitive::kPrimInt:
1523 case Primitive::kPrimChar:
1524 // Processing a Dex `int-to-double' instruction.
1525 locations->SetInAt(0, Location::RequiresRegister());
1526 locations->SetOut(Location::RequiresFpuRegister());
1527 break;
1528
1529 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001530 // Processing a Dex `long-to-double' instruction.
1531 locations->SetInAt(0, Location::RequiresRegister());
1532 locations->SetOut(Location::RequiresFpuRegister());
1533 break;
1534
Roland Levillaincff13742014-11-17 14:32:17 +00001535 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001536 // Processing a Dex `float-to-double' instruction.
1537 locations->SetInAt(0, Location::RequiresFpuRegister());
1538 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001539 break;
1540
1541 default:
1542 LOG(FATAL) << "Unexpected type conversion from " << input_type
1543 << " to " << result_type;
1544 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001545 break;
1546
1547 default:
1548 LOG(FATAL) << "Unexpected type conversion from " << input_type
1549 << " to " << result_type;
1550 }
1551}
1552
1553void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1554 LocationSummary* locations = conversion->GetLocations();
1555 Location out = locations->Out();
1556 Location in = locations->InAt(0);
1557 Primitive::Type result_type = conversion->GetResultType();
1558 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001559 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001560 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001561 case Primitive::kPrimByte:
1562 switch (input_type) {
1563 case Primitive::kPrimShort:
1564 case Primitive::kPrimInt:
1565 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001566 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001567 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001568 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001569 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001570 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001571 Address(CpuRegister(RSP), in.GetStackIndex()));
1572 } else {
1573 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001574 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001575 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1576 }
1577 break;
1578
1579 default:
1580 LOG(FATAL) << "Unexpected type conversion from " << input_type
1581 << " to " << result_type;
1582 }
1583 break;
1584
Roland Levillain01a8d712014-11-14 16:27:39 +00001585 case Primitive::kPrimShort:
1586 switch (input_type) {
1587 case Primitive::kPrimByte:
1588 case Primitive::kPrimInt:
1589 case Primitive::kPrimChar:
1590 // Processing a Dex `int-to-short' instruction.
1591 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001592 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001593 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001594 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001595 Address(CpuRegister(RSP), in.GetStackIndex()));
1596 } else {
1597 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001598 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001599 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1600 }
1601 break;
1602
1603 default:
1604 LOG(FATAL) << "Unexpected type conversion from " << input_type
1605 << " to " << result_type;
1606 }
1607 break;
1608
Roland Levillain946e1432014-11-11 17:35:19 +00001609 case Primitive::kPrimInt:
1610 switch (input_type) {
1611 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001612 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001613 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001614 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001615 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001616 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001617 Address(CpuRegister(RSP), in.GetStackIndex()));
1618 } else {
1619 DCHECK(in.IsConstant());
1620 DCHECK(in.GetConstant()->IsLongConstant());
1621 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001622 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001623 }
1624 break;
1625
Roland Levillain3f8f9362014-12-02 17:45:01 +00001626 case Primitive::kPrimFloat: {
1627 // Processing a Dex `float-to-int' instruction.
1628 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1629 CpuRegister output = out.AsRegister<CpuRegister>();
1630 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1631 Label done, nan;
1632
1633 __ movl(output, Immediate(kPrimIntMax));
1634 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001635 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001636 // if input >= temp goto done
1637 __ comiss(input, temp);
1638 __ j(kAboveEqual, &done);
1639 // if input == NaN goto nan
1640 __ j(kUnordered, &nan);
1641 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001642 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001643 __ jmp(&done);
1644 __ Bind(&nan);
1645 // output = 0
1646 __ xorl(output, output);
1647 __ Bind(&done);
1648 break;
1649 }
1650
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001651 case Primitive::kPrimDouble: {
1652 // Processing a Dex `double-to-int' instruction.
1653 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1654 CpuRegister output = out.AsRegister<CpuRegister>();
1655 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1656 Label done, nan;
1657
1658 __ movl(output, Immediate(kPrimIntMax));
1659 // temp = int-to-double(output)
1660 __ cvtsi2sd(temp, output);
1661 // if input >= temp goto done
1662 __ comisd(input, temp);
1663 __ j(kAboveEqual, &done);
1664 // if input == NaN goto nan
1665 __ j(kUnordered, &nan);
1666 // output = double-to-int-truncate(input)
1667 __ cvttsd2si(output, input);
1668 __ jmp(&done);
1669 __ Bind(&nan);
1670 // output = 0
1671 __ xorl(output, output);
1672 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001673 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001674 }
Roland Levillain946e1432014-11-11 17:35:19 +00001675
1676 default:
1677 LOG(FATAL) << "Unexpected type conversion from " << input_type
1678 << " to " << result_type;
1679 }
1680 break;
1681
Roland Levillaindff1f282014-11-05 14:15:05 +00001682 case Primitive::kPrimLong:
1683 switch (input_type) {
1684 DCHECK(out.IsRegister());
1685 case Primitive::kPrimByte:
1686 case Primitive::kPrimShort:
1687 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001688 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001689 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001690 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001691 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001692 break;
1693
Roland Levillain624279f2014-12-04 11:54:28 +00001694 case Primitive::kPrimFloat: {
1695 // Processing a Dex `float-to-long' instruction.
1696 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1697 CpuRegister output = out.AsRegister<CpuRegister>();
1698 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1699 Label done, nan;
1700
1701 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001702 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001703 __ cvtsi2ss(temp, output, true);
1704 // if input >= temp goto done
1705 __ comiss(input, temp);
1706 __ j(kAboveEqual, &done);
1707 // if input == NaN goto nan
1708 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001709 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001710 __ cvttss2si(output, input, true);
1711 __ jmp(&done);
1712 __ Bind(&nan);
1713 // output = 0
1714 __ xorq(output, output);
1715 __ Bind(&done);
1716 break;
1717 }
1718
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001719 case Primitive::kPrimDouble: {
1720 // Processing a Dex `double-to-long' instruction.
1721 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1722 CpuRegister output = out.AsRegister<CpuRegister>();
1723 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1724 Label done, nan;
1725
1726 __ movq(output, Immediate(kPrimLongMax));
1727 // temp = long-to-double(output)
1728 __ cvtsi2sd(temp, output, true);
1729 // if input >= temp goto done
1730 __ comisd(input, temp);
1731 __ j(kAboveEqual, &done);
1732 // if input == NaN goto nan
1733 __ j(kUnordered, &nan);
1734 // output = double-to-long-truncate(input)
1735 __ cvttsd2si(output, input, true);
1736 __ jmp(&done);
1737 __ Bind(&nan);
1738 // output = 0
1739 __ xorq(output, output);
1740 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001741 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001742 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001743
1744 default:
1745 LOG(FATAL) << "Unexpected type conversion from " << input_type
1746 << " to " << result_type;
1747 }
1748 break;
1749
Roland Levillain981e4542014-11-14 11:47:14 +00001750 case Primitive::kPrimChar:
1751 switch (input_type) {
1752 case Primitive::kPrimByte:
1753 case Primitive::kPrimShort:
1754 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001755 // Processing a Dex `int-to-char' instruction.
1756 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001757 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001758 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001759 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001760 Address(CpuRegister(RSP), in.GetStackIndex()));
1761 } else {
1762 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001763 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001764 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1765 }
1766 break;
1767
1768 default:
1769 LOG(FATAL) << "Unexpected type conversion from " << input_type
1770 << " to " << result_type;
1771 }
1772 break;
1773
Roland Levillaindff1f282014-11-05 14:15:05 +00001774 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001775 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001776 case Primitive::kPrimByte:
1777 case Primitive::kPrimShort:
1778 case Primitive::kPrimInt:
1779 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001780 // Processing a Dex `int-to-float' instruction.
1781 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001782 break;
1783
1784 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001785 // Processing a Dex `long-to-float' instruction.
1786 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1787 break;
1788
Roland Levillaincff13742014-11-17 14:32:17 +00001789 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001790 // Processing a Dex `double-to-float' instruction.
1791 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001792 break;
1793
1794 default:
1795 LOG(FATAL) << "Unexpected type conversion from " << input_type
1796 << " to " << result_type;
1797 };
1798 break;
1799
Roland Levillaindff1f282014-11-05 14:15:05 +00001800 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001801 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001802 case Primitive::kPrimByte:
1803 case Primitive::kPrimShort:
1804 case Primitive::kPrimInt:
1805 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001806 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001807 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001808 break;
1809
1810 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001811 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001812 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001813 break;
1814
Roland Levillaincff13742014-11-17 14:32:17 +00001815 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001816 // Processing a Dex `float-to-double' instruction.
1817 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001818 break;
1819
1820 default:
1821 LOG(FATAL) << "Unexpected type conversion from " << input_type
1822 << " to " << result_type;
1823 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001824 break;
1825
1826 default:
1827 LOG(FATAL) << "Unexpected type conversion from " << input_type
1828 << " to " << result_type;
1829 }
1830}
1831
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001832void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001833 LocationSummary* locations =
1834 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001835 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001836 case Primitive::kPrimInt: {
1837 locations->SetInAt(0, Location::RequiresRegister());
1838 locations->SetInAt(1, Location::Any());
1839 locations->SetOut(Location::SameAsFirstInput());
1840 break;
1841 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001842
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001843 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001844 locations->SetInAt(0, Location::RequiresRegister());
1845 locations->SetInAt(1, Location::RequiresRegister());
1846 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001847 break;
1848 }
1849
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001850 case Primitive::kPrimDouble:
1851 case Primitive::kPrimFloat: {
1852 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001853 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001854 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001855 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001857
1858 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001859 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001860 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001861}
1862
1863void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1864 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001865 Location first = locations->InAt(0);
1866 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001867 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001868
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001869 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001870 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001871 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001872 __ addl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001873 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001874 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001875 __ addl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001876 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001877 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001878 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001879 break;
1880 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001882 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001883 __ addq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001884 break;
1885 }
1886
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001887 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001888 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001889 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001890 }
1891
1892 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001893 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001894 break;
1895 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001896
1897 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001898 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001899 }
1900}
1901
1902void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001903 LocationSummary* locations =
1904 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001905 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001906 case Primitive::kPrimInt: {
1907 locations->SetInAt(0, Location::RequiresRegister());
1908 locations->SetInAt(1, Location::Any());
1909 locations->SetOut(Location::SameAsFirstInput());
1910 break;
1911 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001912 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001913 locations->SetInAt(0, Location::RequiresRegister());
1914 locations->SetInAt(1, Location::RequiresRegister());
1915 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001916 break;
1917 }
Calin Juravle11351682014-10-23 15:38:15 +01001918 case Primitive::kPrimFloat:
1919 case Primitive::kPrimDouble: {
1920 locations->SetInAt(0, Location::RequiresFpuRegister());
1921 locations->SetInAt(1, Location::RequiresFpuRegister());
1922 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001923 break;
Calin Juravle11351682014-10-23 15:38:15 +01001924 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001925 default:
Calin Juravle11351682014-10-23 15:38:15 +01001926 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001927 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001928}
1929
1930void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1931 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001932 Location first = locations->InAt(0);
1933 Location second = locations->InAt(1);
1934 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001935 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001936 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001937 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001938 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001939 } else if (second.IsConstant()) {
1940 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001941 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001942 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001943 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001944 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001945 break;
1946 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001947 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001948 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001949 break;
1950 }
1951
Calin Juravle11351682014-10-23 15:38:15 +01001952 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001953 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001954 break;
Calin Juravle11351682014-10-23 15:38:15 +01001955 }
1956
1957 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001958 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001959 break;
1960 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001961
1962 default:
Calin Juravle11351682014-10-23 15:38:15 +01001963 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001964 }
1965}
1966
Calin Juravle34bacdf2014-10-07 20:23:36 +01001967void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1968 LocationSummary* locations =
1969 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1970 switch (mul->GetResultType()) {
1971 case Primitive::kPrimInt: {
1972 locations->SetInAt(0, Location::RequiresRegister());
1973 locations->SetInAt(1, Location::Any());
1974 locations->SetOut(Location::SameAsFirstInput());
1975 break;
1976 }
1977 case Primitive::kPrimLong: {
1978 locations->SetInAt(0, Location::RequiresRegister());
1979 locations->SetInAt(1, Location::RequiresRegister());
1980 locations->SetOut(Location::SameAsFirstInput());
1981 break;
1982 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001983 case Primitive::kPrimFloat:
1984 case Primitive::kPrimDouble: {
1985 locations->SetInAt(0, Location::RequiresFpuRegister());
1986 locations->SetInAt(1, Location::RequiresFpuRegister());
1987 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001988 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001989 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001990
1991 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001992 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001993 }
1994}
1995
1996void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1997 LocationSummary* locations = mul->GetLocations();
1998 Location first = locations->InAt(0);
1999 Location second = locations->InAt(1);
2000 DCHECK(first.Equals(locations->Out()));
2001 switch (mul->GetResultType()) {
2002 case Primitive::kPrimInt: {
2003 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002004 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002005 } else if (second.IsConstant()) {
2006 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002007 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002008 } else {
2009 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002010 __ imull(first.AsRegister<CpuRegister>(),
2011 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002012 }
2013 break;
2014 }
2015 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002016 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002017 break;
2018 }
2019
Calin Juravleb5bfa962014-10-21 18:02:24 +01002020 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002021 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002022 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002023 }
2024
2025 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002026 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002027 break;
2028 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002029
2030 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002031 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002032 }
2033}
2034
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002035void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2036 uint32_t stack_adjustment, bool is_float) {
2037 if (source.IsStackSlot()) {
2038 DCHECK(is_float);
2039 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2040 } else if (source.IsDoubleStackSlot()) {
2041 DCHECK(!is_float);
2042 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2043 } else {
2044 // Write the value to the temporary location on the stack and load to FP stack.
2045 if (is_float) {
2046 Location stack_temp = Location::StackSlot(temp_offset);
2047 codegen_->Move(stack_temp, source);
2048 __ flds(Address(CpuRegister(RSP), temp_offset));
2049 } else {
2050 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2051 codegen_->Move(stack_temp, source);
2052 __ fldl(Address(CpuRegister(RSP), temp_offset));
2053 }
2054 }
2055}
2056
2057void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2058 Primitive::Type type = rem->GetResultType();
2059 bool is_float = type == Primitive::kPrimFloat;
2060 size_t elem_size = Primitive::ComponentSize(type);
2061 LocationSummary* locations = rem->GetLocations();
2062 Location first = locations->InAt(0);
2063 Location second = locations->InAt(1);
2064 Location out = locations->Out();
2065
2066 // Create stack space for 2 elements.
2067 // TODO: enhance register allocator to ask for stack temporaries.
2068 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2069
2070 // Load the values to the FP stack in reverse order, using temporaries if needed.
2071 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2072 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2073
2074 // Loop doing FPREM until we stabilize.
2075 Label retry;
2076 __ Bind(&retry);
2077 __ fprem();
2078
2079 // Move FP status to AX.
2080 __ fstsw();
2081
2082 // And see if the argument reduction is complete. This is signaled by the
2083 // C2 FPU flag bit set to 0.
2084 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2085 __ j(kNotEqual, &retry);
2086
2087 // We have settled on the final value. Retrieve it into an XMM register.
2088 // Store FP top of stack to real stack.
2089 if (is_float) {
2090 __ fsts(Address(CpuRegister(RSP), 0));
2091 } else {
2092 __ fstl(Address(CpuRegister(RSP), 0));
2093 }
2094
2095 // Pop the 2 items from the FP stack.
2096 __ fucompp();
2097
2098 // Load the value from the stack into an XMM register.
2099 DCHECK(out.IsFpuRegister()) << out;
2100 if (is_float) {
2101 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2102 } else {
2103 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2104 }
2105
2106 // And remove the temporary stack space we allocated.
2107 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2108}
2109
Calin Juravlebacfec32014-11-14 15:54:36 +00002110void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2111 DCHECK(instruction->IsDiv() || instruction->IsRem());
2112 Primitive::Type type = instruction->GetResultType();
2113 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2114
2115 bool is_div = instruction->IsDiv();
2116 LocationSummary* locations = instruction->GetLocations();
2117
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
2119 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002120
Roland Levillain271ab9c2014-11-27 15:23:57 +00002121 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00002122 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
2123
2124 SlowPathCodeX86_64* slow_path =
2125 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
2126 out_reg.AsRegister(), type, is_div);
2127 codegen_->AddSlowPath(slow_path);
2128
2129 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2130 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2131 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002132 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002133 __ cmpl(second_reg, Immediate(-1));
2134 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002135 // edx:eax <- sign-extended of eax
2136 __ cdq();
2137 // eax = quotient, edx = remainder
2138 __ idivl(second_reg);
2139 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002140 __ cmpq(second_reg, Immediate(-1));
2141 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002142 // rdx:rax <- sign-extended of rax
2143 __ cqo();
2144 // rax = quotient, rdx = remainder
2145 __ idivq(second_reg);
2146 }
2147
2148 __ Bind(slow_path->GetExitLabel());
2149}
2150
Calin Juravle7c4954d2014-10-28 16:57:40 +00002151void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2152 LocationSummary* locations =
2153 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2154 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002155 case Primitive::kPrimInt:
2156 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002157 locations->SetInAt(0, Location::RegisterLocation(RAX));
2158 locations->SetInAt(1, Location::RequiresRegister());
2159 locations->SetOut(Location::SameAsFirstInput());
2160 // Intel uses edx:eax as the dividend.
2161 locations->AddTemp(Location::RegisterLocation(RDX));
2162 break;
2163 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002164
Calin Juravle7c4954d2014-10-28 16:57:40 +00002165 case Primitive::kPrimFloat:
2166 case Primitive::kPrimDouble: {
2167 locations->SetInAt(0, Location::RequiresFpuRegister());
2168 locations->SetInAt(1, Location::RequiresFpuRegister());
2169 locations->SetOut(Location::SameAsFirstInput());
2170 break;
2171 }
2172
2173 default:
2174 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2175 }
2176}
2177
2178void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2179 LocationSummary* locations = div->GetLocations();
2180 Location first = locations->InAt(0);
2181 Location second = locations->InAt(1);
2182 DCHECK(first.Equals(locations->Out()));
2183
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002184 Primitive::Type type = div->GetResultType();
2185 switch (type) {
2186 case Primitive::kPrimInt:
2187 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002188 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002189 break;
2190 }
2191
Calin Juravle7c4954d2014-10-28 16:57:40 +00002192 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002193 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002194 break;
2195 }
2196
2197 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002198 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002199 break;
2200 }
2201
2202 default:
2203 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2204 }
2205}
2206
Calin Juravlebacfec32014-11-14 15:54:36 +00002207void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002208 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002209 LocationSummary* locations =
2210 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002211
2212 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002213 case Primitive::kPrimInt:
2214 case Primitive::kPrimLong: {
2215 locations->SetInAt(0, Location::RegisterLocation(RAX));
2216 locations->SetInAt(1, Location::RequiresRegister());
2217 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2218 locations->SetOut(Location::RegisterLocation(RDX));
2219 break;
2220 }
2221
2222 case Primitive::kPrimFloat:
2223 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002224 locations->SetInAt(0, Location::Any());
2225 locations->SetInAt(1, Location::Any());
2226 locations->SetOut(Location::RequiresFpuRegister());
2227 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002228 break;
2229 }
2230
2231 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002232 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002233 }
2234}
2235
2236void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2237 Primitive::Type type = rem->GetResultType();
2238 switch (type) {
2239 case Primitive::kPrimInt:
2240 case Primitive::kPrimLong: {
2241 GenerateDivRemIntegral(rem);
2242 break;
2243 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002244 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002245 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002246 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002247 break;
2248 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002249 default:
2250 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2251 }
2252}
2253
Calin Juravled0d48522014-11-04 16:40:20 +00002254void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2255 LocationSummary* locations =
2256 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2257 locations->SetInAt(0, Location::Any());
2258 if (instruction->HasUses()) {
2259 locations->SetOut(Location::SameAsFirstInput());
2260 }
2261}
2262
2263void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2264 SlowPathCodeX86_64* slow_path =
2265 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2266 codegen_->AddSlowPath(slow_path);
2267
2268 LocationSummary* locations = instruction->GetLocations();
2269 Location value = locations->InAt(0);
2270
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002271 switch (instruction->GetType()) {
2272 case Primitive::kPrimInt: {
2273 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002274 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002275 __ j(kEqual, slow_path->GetEntryLabel());
2276 } else if (value.IsStackSlot()) {
2277 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2278 __ j(kEqual, slow_path->GetEntryLabel());
2279 } else {
2280 DCHECK(value.IsConstant()) << value;
2281 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2282 __ jmp(slow_path->GetEntryLabel());
2283 }
2284 }
2285 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002286 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002287 case Primitive::kPrimLong: {
2288 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002289 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002290 __ j(kEqual, slow_path->GetEntryLabel());
2291 } else if (value.IsDoubleStackSlot()) {
2292 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2293 __ j(kEqual, slow_path->GetEntryLabel());
2294 } else {
2295 DCHECK(value.IsConstant()) << value;
2296 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2297 __ jmp(slow_path->GetEntryLabel());
2298 }
2299 }
2300 break;
2301 }
2302 default:
2303 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002304 }
Calin Juravled0d48522014-11-04 16:40:20 +00002305}
2306
Calin Juravle9aec02f2014-11-18 23:06:35 +00002307void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2308 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2309
2310 LocationSummary* locations =
2311 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2312
2313 switch (op->GetResultType()) {
2314 case Primitive::kPrimInt:
2315 case Primitive::kPrimLong: {
2316 locations->SetInAt(0, Location::RequiresRegister());
2317 // The shift count needs to be in CL.
2318 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2319 locations->SetOut(Location::SameAsFirstInput());
2320 break;
2321 }
2322 default:
2323 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2324 }
2325}
2326
2327void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2328 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2329
2330 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002332 Location second = locations->InAt(1);
2333
2334 switch (op->GetResultType()) {
2335 case Primitive::kPrimInt: {
2336 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002338 if (op->IsShl()) {
2339 __ shll(first_reg, second_reg);
2340 } else if (op->IsShr()) {
2341 __ sarl(first_reg, second_reg);
2342 } else {
2343 __ shrl(first_reg, second_reg);
2344 }
2345 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002346 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002347 if (op->IsShl()) {
2348 __ shll(first_reg, imm);
2349 } else if (op->IsShr()) {
2350 __ sarl(first_reg, imm);
2351 } else {
2352 __ shrl(first_reg, imm);
2353 }
2354 }
2355 break;
2356 }
2357 case Primitive::kPrimLong: {
2358 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002359 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002360 if (op->IsShl()) {
2361 __ shlq(first_reg, second_reg);
2362 } else if (op->IsShr()) {
2363 __ sarq(first_reg, second_reg);
2364 } else {
2365 __ shrq(first_reg, second_reg);
2366 }
2367 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002368 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002369 if (op->IsShl()) {
2370 __ shlq(first_reg, imm);
2371 } else if (op->IsShr()) {
2372 __ sarq(first_reg, imm);
2373 } else {
2374 __ shrq(first_reg, imm);
2375 }
2376 }
2377 break;
2378 }
2379 default:
2380 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2381 }
2382}
2383
2384void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2385 HandleShift(shl);
2386}
2387
2388void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2389 HandleShift(shl);
2390}
2391
2392void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2393 HandleShift(shr);
2394}
2395
2396void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2397 HandleShift(shr);
2398}
2399
2400void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2401 HandleShift(ushr);
2402}
2403
2404void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2405 HandleShift(ushr);
2406}
2407
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002408void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002409 LocationSummary* locations =
2410 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002411 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002412 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2413 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2414 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002415}
2416
2417void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2418 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002419 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002420 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2421
2422 __ gs()->call(Address::Absolute(
2423 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
2424
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002425 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002426 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002427}
2428
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002429void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2430 LocationSummary* locations =
2431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2432 InvokeRuntimeCallingConvention calling_convention;
2433 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002434 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002435 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002436 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002437}
2438
2439void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2440 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002441 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002442 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2443
2444 __ gs()->call(Address::Absolute(
2445 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
2446
2447 DCHECK(!codegen_->IsLeafMethod());
2448 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2449}
2450
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002451void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002452 LocationSummary* locations =
2453 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002454 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2455 if (location.IsStackSlot()) {
2456 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2457 } else if (location.IsDoubleStackSlot()) {
2458 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2459 }
2460 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002461}
2462
2463void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2464 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002465 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002466}
2467
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002468void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002469 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002470 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002471 locations->SetInAt(0, Location::RequiresRegister());
2472 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002473}
2474
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002475void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2476 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002477 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2478 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002479 Location out = locations->Out();
2480 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002481 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002482 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002483 break;
2484
2485 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002486 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002487 break;
2488
2489 default:
2490 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2491 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002492}
2493
2494void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002495 LocationSummary* locations =
2496 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002497 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2498 locations->SetInAt(i, Location::Any());
2499 }
2500 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002501}
2502
2503void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002504 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002505 LOG(FATAL) << "Unimplemented";
2506}
2507
Calin Juravle52c48962014-12-16 17:02:57 +00002508void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2509 /*
2510 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2511 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2512 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2513 */
2514 switch (kind) {
2515 case MemBarrierKind::kAnyAny: {
2516 __ mfence();
2517 break;
2518 }
2519 case MemBarrierKind::kAnyStore:
2520 case MemBarrierKind::kLoadAny:
2521 case MemBarrierKind::kStoreStore: {
2522 // nop
2523 break;
2524 }
2525 default:
2526 LOG(FATAL) << "Unexpected memory barier " << kind;
2527 }
2528}
2529
2530void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2531 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2532
Nicolas Geoffray39468442014-09-02 15:17:15 +01002533 LocationSummary* locations =
2534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002535 locations->SetInAt(0, Location::RequiresRegister());
2536 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2537}
2538
2539void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2540 const FieldInfo& field_info) {
2541 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2542
2543 LocationSummary* locations = instruction->GetLocations();
2544 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2545 Location out = locations->Out();
2546 bool is_volatile = field_info.IsVolatile();
2547 Primitive::Type field_type = field_info.GetFieldType();
2548 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2549
2550 switch (field_type) {
2551 case Primitive::kPrimBoolean: {
2552 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2553 break;
2554 }
2555
2556 case Primitive::kPrimByte: {
2557 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2558 break;
2559 }
2560
2561 case Primitive::kPrimShort: {
2562 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2563 break;
2564 }
2565
2566 case Primitive::kPrimChar: {
2567 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2568 break;
2569 }
2570
2571 case Primitive::kPrimInt:
2572 case Primitive::kPrimNot: {
2573 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2574 break;
2575 }
2576
2577 case Primitive::kPrimLong: {
2578 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2579 break;
2580 }
2581
2582 case Primitive::kPrimFloat: {
2583 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2584 break;
2585 }
2586
2587 case Primitive::kPrimDouble: {
2588 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2589 break;
2590 }
2591
2592 case Primitive::kPrimVoid:
2593 LOG(FATAL) << "Unreachable type " << field_type;
2594 UNREACHABLE();
2595 }
2596
Calin Juravle77520bc2015-01-12 18:45:46 +00002597 codegen_->MaybeRecordImplicitNullCheck(instruction);
2598
Calin Juravle52c48962014-12-16 17:02:57 +00002599 if (is_volatile) {
2600 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2601 }
2602}
2603
2604void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2605 const FieldInfo& field_info) {
2606 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2607
2608 LocationSummary* locations =
2609 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002610 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002611 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2612
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002613 locations->SetInAt(0, Location::RequiresRegister());
2614 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002615 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002616 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002617 locations->AddTemp(Location::RequiresRegister());
2618 locations->AddTemp(Location::RequiresRegister());
2619 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002620}
2621
Calin Juravle52c48962014-12-16 17:02:57 +00002622void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2623 const FieldInfo& field_info) {
2624 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2625
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002626 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002627 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2628 Location value = locations->InAt(1);
2629 bool is_volatile = field_info.IsVolatile();
2630 Primitive::Type field_type = field_info.GetFieldType();
2631 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2632
2633 if (is_volatile) {
2634 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2635 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002636
2637 switch (field_type) {
2638 case Primitive::kPrimBoolean:
2639 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002640 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002641 break;
2642 }
2643
2644 case Primitive::kPrimShort:
2645 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002646 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002647 break;
2648 }
2649
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002650 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002651 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002652 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002653 break;
2654 }
2655
2656 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002657 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002658 break;
2659 }
2660
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002661 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002662 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002663 break;
2664 }
2665
2666 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002667 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002668 break;
2669 }
2670
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002671 case Primitive::kPrimVoid:
2672 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002673 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002674 }
Calin Juravle52c48962014-12-16 17:02:57 +00002675
Calin Juravle77520bc2015-01-12 18:45:46 +00002676 codegen_->MaybeRecordImplicitNullCheck(instruction);
2677
2678 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2679 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2680 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2681 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
2682 }
2683
Calin Juravle52c48962014-12-16 17:02:57 +00002684 if (is_volatile) {
2685 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2686 }
2687}
2688
2689void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2690 HandleFieldSet(instruction, instruction->GetFieldInfo());
2691}
2692
2693void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2694 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002695}
2696
2697void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002698 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002699}
2700
2701void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002702 HandleFieldGet(instruction, instruction->GetFieldInfo());
2703}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002704
Calin Juravle52c48962014-12-16 17:02:57 +00002705void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2706 HandleFieldGet(instruction);
2707}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002708
Calin Juravle52c48962014-12-16 17:02:57 +00002709void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2710 HandleFieldGet(instruction, instruction->GetFieldInfo());
2711}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002712
Calin Juravle52c48962014-12-16 17:02:57 +00002713void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2714 HandleFieldSet(instruction, instruction->GetFieldInfo());
2715}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002716
Calin Juravle52c48962014-12-16 17:02:57 +00002717void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2718 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002719}
2720
2721void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002722 LocationSummary* locations =
2723 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002724 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2725 ? Location::RequiresRegister()
2726 : Location::Any();
2727 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002728 if (instruction->HasUses()) {
2729 locations->SetOut(Location::SameAsFirstInput());
2730 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002731}
2732
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002733void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002734 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2735 return;
2736 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002737 LocationSummary* locations = instruction->GetLocations();
2738 Location obj = locations->InAt(0);
2739
2740 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
2741 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2742}
2743
2744void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002745 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002746 codegen_->AddSlowPath(slow_path);
2747
2748 LocationSummary* locations = instruction->GetLocations();
2749 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002750
2751 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002752 __ cmpl(obj.AsRegister<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002753 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002754 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002755 } else {
2756 DCHECK(obj.IsConstant()) << obj;
2757 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2758 __ jmp(slow_path->GetEntryLabel());
2759 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002760 }
2761 __ j(kEqual, slow_path->GetEntryLabel());
2762}
2763
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002764void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
2765 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2766 GenerateImplicitNullCheck(instruction);
2767 } else {
2768 GenerateExplicitNullCheck(instruction);
2769 }
2770}
2771
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002772void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002773 LocationSummary* locations =
2774 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002775 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002776 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002777 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2778 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002779}
2780
2781void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2782 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002783 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002784 Location index = locations->InAt(1);
2785
2786 switch (instruction->GetType()) {
2787 case Primitive::kPrimBoolean: {
2788 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002790 if (index.IsConstant()) {
2791 __ movzxb(out, Address(obj,
2792 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2793 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002794 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002795 }
2796 break;
2797 }
2798
2799 case Primitive::kPrimByte: {
2800 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002801 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002802 if (index.IsConstant()) {
2803 __ movsxb(out, Address(obj,
2804 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2805 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002806 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002807 }
2808 break;
2809 }
2810
2811 case Primitive::kPrimShort: {
2812 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002814 if (index.IsConstant()) {
2815 __ movsxw(out, Address(obj,
2816 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2817 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002818 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002819 }
2820 break;
2821 }
2822
2823 case Primitive::kPrimChar: {
2824 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002826 if (index.IsConstant()) {
2827 __ movzxw(out, Address(obj,
2828 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2829 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002831 }
2832 break;
2833 }
2834
2835 case Primitive::kPrimInt:
2836 case Primitive::kPrimNot: {
2837 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2838 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002839 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002840 if (index.IsConstant()) {
2841 __ movl(out, Address(obj,
2842 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2843 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002844 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002845 }
2846 break;
2847 }
2848
2849 case Primitive::kPrimLong: {
2850 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002851 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002852 if (index.IsConstant()) {
2853 __ movq(out, Address(obj,
2854 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2855 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002856 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002857 }
2858 break;
2859 }
2860
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002861 case Primitive::kPrimFloat: {
2862 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002863 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002864 if (index.IsConstant()) {
2865 __ movss(out, Address(obj,
2866 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2867 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002868 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002869 }
2870 break;
2871 }
2872
2873 case Primitive::kPrimDouble: {
2874 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002875 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002876 if (index.IsConstant()) {
2877 __ movsd(out, Address(obj,
2878 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2879 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002880 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002881 }
2882 break;
2883 }
2884
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002885 case Primitive::kPrimVoid:
2886 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002887 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002888 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002889 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002890}
2891
2892void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002893 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002894
2895 bool needs_write_barrier =
2896 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2897 bool needs_runtime_call = instruction->NeedsTypeCheck();
2898
Nicolas Geoffray39468442014-09-02 15:17:15 +01002899 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002900 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2901 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002902 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002903 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2904 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2905 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002906 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002907 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002908 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002909 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2910 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002911 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002912 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002913 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2914 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002915 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002916 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002917 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002918
2919 if (needs_write_barrier) {
2920 // Temporary registers for the write barrier.
2921 locations->AddTemp(Location::RequiresRegister());
2922 locations->AddTemp(Location::RequiresRegister());
2923 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002924 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002925}
2926
2927void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2928 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002929 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002930 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002931 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002932 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002933 bool needs_runtime_call = locations->WillCall();
2934 bool needs_write_barrier =
2935 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002936
2937 switch (value_type) {
2938 case Primitive::kPrimBoolean:
2939 case Primitive::kPrimByte: {
2940 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002941 if (index.IsConstant()) {
2942 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002943 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002944 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002945 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002946 __ movb(Address(obj, offset),
2947 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002948 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002949 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002950 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002951 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2952 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002953 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002954 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002955 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2956 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002957 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002958 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002959 break;
2960 }
2961
2962 case Primitive::kPrimShort:
2963 case Primitive::kPrimChar: {
2964 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002965 if (index.IsConstant()) {
2966 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002967 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002968 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002969 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002970 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00002971 __ movw(Address(obj, offset),
2972 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002973 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002974 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002975 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002976 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002977 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
2978 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002979 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002980 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002982 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2983 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002984 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002985 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002986 break;
2987 }
2988
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002989 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002990 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002991 if (!needs_runtime_call) {
2992 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2993 if (index.IsConstant()) {
2994 size_t offset =
2995 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2996 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002997 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002998 } else {
2999 DCHECK(value.IsConstant()) << value;
3000 __ movl(Address(obj, offset),
3001 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3002 }
3003 } else {
3004 DCHECK(index.IsRegister()) << index;
3005 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003006 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3007 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003008 } else {
3009 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003010 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003011 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3012 }
3013 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003014 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003015 if (needs_write_barrier) {
3016 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003017 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3018 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
3019 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003020 }
3021 } else {
3022 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00003023 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
3024 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003025 DCHECK(!codegen_->IsLeafMethod());
3026 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3027 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003028 break;
3029 }
3030
3031 case Primitive::kPrimLong: {
3032 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003033 if (index.IsConstant()) {
3034 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003035 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003037 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003038 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003039 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3040 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003041 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003042 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003043 break;
3044 }
3045
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003046 case Primitive::kPrimFloat: {
3047 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3048 if (index.IsConstant()) {
3049 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3050 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003052 } else {
3053 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003054 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
3055 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003056 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003057 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003058 break;
3059 }
3060
3061 case Primitive::kPrimDouble: {
3062 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3063 if (index.IsConstant()) {
3064 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3065 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003066 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003067 } else {
3068 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003069 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
3070 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003071 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003072 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003073 break;
3074 }
3075
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003076 case Primitive::kPrimVoid:
3077 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003078 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003079 }
3080}
3081
3082void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003083 LocationSummary* locations =
3084 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003085 locations->SetInAt(0, Location::RequiresRegister());
3086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003087}
3088
3089void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
3090 LocationSummary* locations = instruction->GetLocations();
3091 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003092 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
3093 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003094 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003095 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003096}
3097
3098void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003099 LocationSummary* locations =
3100 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003101 locations->SetInAt(0, Location::RequiresRegister());
3102 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003103 if (instruction->HasUses()) {
3104 locations->SetOut(Location::SameAsFirstInput());
3105 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106}
3107
3108void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
3109 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003110 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003111 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003112 codegen_->AddSlowPath(slow_path);
3113
Roland Levillain271ab9c2014-11-27 15:23:57 +00003114 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
3115 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003116
3117 __ cmpl(index, length);
3118 __ j(kAboveEqual, slow_path->GetEntryLabel());
3119}
3120
3121void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
3122 CpuRegister card,
3123 CpuRegister object,
3124 CpuRegister value) {
3125 Label is_null;
3126 __ testl(value, value);
3127 __ j(kEqual, &is_null);
3128 __ gs()->movq(card, Address::Absolute(
3129 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
3130 __ movq(temp, object);
3131 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
3132 __ movb(Address(temp, card, TIMES_1, 0), card);
3133 __ Bind(&is_null);
3134}
3135
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003136void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
3137 temp->SetLocations(nullptr);
3138}
3139
3140void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
3141 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003142 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003143}
3144
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003145void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003146 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003147 LOG(FATAL) << "Unimplemented";
3148}
3149
3150void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003151 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3152}
3153
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003154void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3155 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3156}
3157
3158void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003159 HBasicBlock* block = instruction->GetBlock();
3160 if (block->GetLoopInformation() != nullptr) {
3161 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3162 // The back edge will generate the suspend check.
3163 return;
3164 }
3165 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3166 // The goto will generate the suspend check.
3167 return;
3168 }
3169 GenerateSuspendCheck(instruction, nullptr);
3170}
3171
3172void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3173 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003174 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003175 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003176 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003177 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003178 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003179 if (successor == nullptr) {
3180 __ j(kNotEqual, slow_path->GetEntryLabel());
3181 __ Bind(slow_path->GetReturnLabel());
3182 } else {
3183 __ j(kEqual, codegen_->GetLabelOf(successor));
3184 __ jmp(slow_path->GetEntryLabel());
3185 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003186}
3187
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003188X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3189 return codegen_->GetAssembler();
3190}
3191
3192void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3193 MoveOperands* move = moves_.Get(index);
3194 Location source = move->GetSource();
3195 Location destination = move->GetDestination();
3196
3197 if (source.IsRegister()) {
3198 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003199 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003200 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003201 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003202 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003203 } else {
3204 DCHECK(destination.IsDoubleStackSlot());
3205 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003206 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003207 }
3208 } else if (source.IsStackSlot()) {
3209 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003210 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003211 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003212 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003213 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003214 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003215 } else {
3216 DCHECK(destination.IsStackSlot());
3217 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3218 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3219 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003220 } else if (source.IsDoubleStackSlot()) {
3221 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003222 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003223 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003224 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003225 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3226 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003227 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003228 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003229 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3230 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3231 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003232 } else if (source.IsConstant()) {
3233 HConstant* constant = source.GetConstant();
3234 if (constant->IsIntConstant()) {
3235 Immediate imm(constant->AsIntConstant()->GetValue());
3236 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003237 __ movl(destination.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003238 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003239 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003240 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3241 }
3242 } else if (constant->IsLongConstant()) {
3243 int64_t value = constant->AsLongConstant()->GetValue();
3244 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003246 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003247 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003248 __ movq(CpuRegister(TMP), Immediate(value));
3249 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3250 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003251 } else if (constant->IsFloatConstant()) {
3252 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3253 if (destination.IsFpuRegister()) {
3254 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003255 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003256 } else {
3257 DCHECK(destination.IsStackSlot()) << destination;
3258 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3259 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003260 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003261 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3262 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3263 if (destination.IsFpuRegister()) {
3264 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003265 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003266 } else {
3267 DCHECK(destination.IsDoubleStackSlot()) << destination;
3268 __ movq(CpuRegister(TMP), imm);
3269 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3270 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003271 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003272 } else if (source.IsFpuRegister()) {
3273 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003274 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003275 } else if (destination.IsStackSlot()) {
3276 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003277 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003278 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003279 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003280 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003281 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003282 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003283 }
3284}
3285
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003286void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003287 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003288 __ movl(Address(CpuRegister(RSP), mem), reg);
3289 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003290}
3291
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003292void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003293 ScratchRegisterScope ensure_scratch(
3294 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3295
3296 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3297 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3298 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3299 Address(CpuRegister(RSP), mem2 + stack_offset));
3300 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3301 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3302 CpuRegister(ensure_scratch.GetRegister()));
3303}
3304
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003305void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3306 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3307 __ movq(Address(CpuRegister(RSP), mem), reg);
3308 __ movq(reg, CpuRegister(TMP));
3309}
3310
3311void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3312 ScratchRegisterScope ensure_scratch(
3313 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3314
3315 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3316 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3317 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3318 Address(CpuRegister(RSP), mem2 + stack_offset));
3319 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3320 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3321 CpuRegister(ensure_scratch.GetRegister()));
3322}
3323
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003324void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3325 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3326 __ movss(Address(CpuRegister(RSP), mem), reg);
3327 __ movd(reg, CpuRegister(TMP));
3328}
3329
3330void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3331 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3332 __ movsd(Address(CpuRegister(RSP), mem), reg);
3333 __ movd(reg, CpuRegister(TMP));
3334}
3335
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003336void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3337 MoveOperands* move = moves_.Get(index);
3338 Location source = move->GetSource();
3339 Location destination = move->GetDestination();
3340
3341 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003342 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003343 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003344 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003345 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003346 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003347 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003348 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3349 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003350 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003351 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003352 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003353 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3354 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003355 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003356 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3357 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3358 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003359 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003360 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003361 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003362 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003363 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003364 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003365 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003366 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003367 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003368 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003369 }
3370}
3371
3372
3373void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3374 __ pushq(CpuRegister(reg));
3375}
3376
3377
3378void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3379 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003380}
3381
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003382void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3383 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3384 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3385 Immediate(mirror::Class::kStatusInitialized));
3386 __ j(kLess, slow_path->GetEntryLabel());
3387 __ Bind(slow_path->GetExitLabel());
3388 // No need for memory fence, thanks to the X86_64 memory model.
3389}
3390
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003391void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003392 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3393 ? LocationSummary::kCallOnSlowPath
3394 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003395 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003396 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003397 locations->SetOut(Location::RequiresRegister());
3398}
3399
3400void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003401 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003402 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003403 DCHECK(!cls->CanCallRuntime());
3404 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003405 codegen_->LoadCurrentMethod(out);
3406 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3407 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003408 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003409 codegen_->LoadCurrentMethod(out);
3410 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3411 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003412 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3413 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3414 codegen_->AddSlowPath(slow_path);
3415 __ testl(out, out);
3416 __ j(kEqual, slow_path->GetEntryLabel());
3417 if (cls->MustGenerateClinitCheck()) {
3418 GenerateClassInitializationCheck(slow_path, out);
3419 } else {
3420 __ Bind(slow_path->GetExitLabel());
3421 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003422 }
3423}
3424
3425void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3426 LocationSummary* locations =
3427 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3428 locations->SetInAt(0, Location::RequiresRegister());
3429 if (check->HasUses()) {
3430 locations->SetOut(Location::SameAsFirstInput());
3431 }
3432}
3433
3434void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003435 // We assume the class to not be null.
3436 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3437 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003438 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003439 GenerateClassInitializationCheck(slow_path,
3440 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003441}
3442
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003443void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3444 LocationSummary* locations =
3445 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3446 locations->SetOut(Location::RequiresRegister());
3447}
3448
3449void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3450 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3451 codegen_->AddSlowPath(slow_path);
3452
Roland Levillain271ab9c2014-11-27 15:23:57 +00003453 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003454 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003455 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3456 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003457 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3458 __ testl(out, out);
3459 __ j(kEqual, slow_path->GetEntryLabel());
3460 __ Bind(slow_path->GetExitLabel());
3461}
3462
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003463void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3464 LocationSummary* locations =
3465 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3466 locations->SetOut(Location::RequiresRegister());
3467}
3468
3469void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3470 Address address = Address::Absolute(
3471 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003472 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003473 __ gs()->movl(address, Immediate(0));
3474}
3475
3476void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3477 LocationSummary* locations =
3478 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3479 InvokeRuntimeCallingConvention calling_convention;
3480 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3481}
3482
3483void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3484 __ gs()->call(
3485 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3486 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3487}
3488
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003489void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003490 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3491 ? LocationSummary::kNoCall
3492 : LocationSummary::kCallOnSlowPath;
3493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3494 locations->SetInAt(0, Location::RequiresRegister());
3495 locations->SetInAt(1, Location::Any());
3496 locations->SetOut(Location::RequiresRegister());
3497}
3498
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003499void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003500 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003501 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003502 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003504 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3505 Label done, zero;
3506 SlowPathCodeX86_64* slow_path = nullptr;
3507
3508 // Return 0 if `obj` is null.
3509 // TODO: avoid this check if we know obj is not null.
3510 __ testl(obj, obj);
3511 __ j(kEqual, &zero);
3512 // Compare the class of `obj` with `cls`.
3513 __ movl(out, Address(obj, class_offset));
3514 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003515 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003516 } else {
3517 DCHECK(cls.IsStackSlot()) << cls;
3518 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3519 }
3520 if (instruction->IsClassFinal()) {
3521 // Classes must be equal for the instanceof to succeed.
3522 __ j(kNotEqual, &zero);
3523 __ movl(out, Immediate(1));
3524 __ jmp(&done);
3525 } else {
3526 // If the classes are not equal, we go into a slow path.
3527 DCHECK(locations->OnlyCallsOnSlowPath());
3528 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003529 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003530 codegen_->AddSlowPath(slow_path);
3531 __ j(kNotEqual, slow_path->GetEntryLabel());
3532 __ movl(out, Immediate(1));
3533 __ jmp(&done);
3534 }
3535 __ Bind(&zero);
3536 __ movl(out, Immediate(0));
3537 if (slow_path != nullptr) {
3538 __ Bind(slow_path->GetExitLabel());
3539 }
3540 __ Bind(&done);
3541}
3542
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003543void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3544 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3545 instruction, LocationSummary::kCallOnSlowPath);
3546 locations->SetInAt(0, Location::RequiresRegister());
3547 locations->SetInAt(1, Location::Any());
3548 locations->AddTemp(Location::RequiresRegister());
3549}
3550
3551void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3552 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003553 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003554 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003555 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003556 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3557 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3558 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3559 codegen_->AddSlowPath(slow_path);
3560
3561 // TODO: avoid this check if we know obj is not null.
3562 __ testl(obj, obj);
3563 __ j(kEqual, slow_path->GetExitLabel());
3564 // Compare the class of `obj` with `cls`.
3565 __ movl(temp, Address(obj, class_offset));
3566 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003567 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003568 } else {
3569 DCHECK(cls.IsStackSlot()) << cls;
3570 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3571 }
3572 // Classes must be equal for the checkcast to succeed.
3573 __ j(kNotEqual, slow_path->GetEntryLabel());
3574 __ Bind(slow_path->GetExitLabel());
3575}
3576
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003577void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3578 LocationSummary* locations =
3579 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3580 InvokeRuntimeCallingConvention calling_convention;
3581 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3582}
3583
3584void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3585 __ gs()->call(Address::Absolute(instruction->IsEnter()
3586 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3587 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3588 true));
3589 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3590}
3591
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003592void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3593void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3594void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3595
3596void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3597 LocationSummary* locations =
3598 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3599 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3600 || instruction->GetResultType() == Primitive::kPrimLong);
3601 locations->SetInAt(0, Location::RequiresRegister());
3602 if (instruction->GetType() == Primitive::kPrimInt) {
3603 locations->SetInAt(1, Location::Any());
3604 } else {
3605 // Request a register to avoid loading a 64bits constant.
3606 locations->SetInAt(1, Location::RequiresRegister());
3607 }
3608 locations->SetOut(Location::SameAsFirstInput());
3609}
3610
3611void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3612 HandleBitwiseOperation(instruction);
3613}
3614
3615void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3616 HandleBitwiseOperation(instruction);
3617}
3618
3619void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3620 HandleBitwiseOperation(instruction);
3621}
3622
3623void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3624 LocationSummary* locations = instruction->GetLocations();
3625 Location first = locations->InAt(0);
3626 Location second = locations->InAt(1);
3627 DCHECK(first.Equals(locations->Out()));
3628
3629 if (instruction->GetResultType() == Primitive::kPrimInt) {
3630 if (second.IsRegister()) {
3631 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003632 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003633 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003635 } else {
3636 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003638 }
3639 } else if (second.IsConstant()) {
3640 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3641 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003642 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003643 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003644 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003645 } else {
3646 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003647 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003648 }
3649 } else {
3650 Address address(CpuRegister(RSP), second.GetStackIndex());
3651 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003653 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003654 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003655 } else {
3656 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003657 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003658 }
3659 }
3660 } else {
3661 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3662 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003663 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003664 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003666 } else {
3667 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003668 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003669 }
3670 }
3671}
3672
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003673} // namespace x86_64
3674} // namespace art