blob: ff7fcdcbac5040bc7310f1750b0c2ad136937d5d [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"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "mirror/object_reference.h"
25#include "thread.h"
26#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "utils/x86_64/assembler_x86_64.h"
29#include "utils/x86_64/managed_register_x86_64.h"
30
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031namespace art {
32
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033namespace x86_64 {
34
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010035static constexpr bool kExplicitStackOverflowCheck = false;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036
37// Some x86_64 instructions require a register to be available as temp.
38static constexpr Register TMP = R11;
39
40static constexpr int kNumberOfPushedRegistersAtEntry = 1;
41static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
Calin Juravled2ec87d2014-12-08 14:24:46 +000046static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1 };
47static constexpr size_t kRuntimeParameterFpuRegistersLength =
48 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010050class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051 public:
52 InvokeRuntimeCallingConvention()
53 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010054 kRuntimeParameterCoreRegistersLength,
55 kRuntimeParameterFpuRegisters,
56 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
58 private:
59 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
60};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010061
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
63
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010064class SlowPathCodeX86_64 : public SlowPathCode {
65 public:
66 SlowPathCodeX86_64() : entry_label_(), exit_label_() {}
67
68 Label* GetEntryLabel() { return &entry_label_; }
69 Label* GetExitLabel() { return &exit_label_; }
70
71 private:
72 Label entry_label_;
73 Label exit_label_;
74
75 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86_64);
76};
77
78class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081
82 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
83 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010084 __ gs()->call(
85 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010086 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 }
88
89 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010090 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010091 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
92};
93
Calin Juravled0d48522014-11-04 16:40:20 +000094class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
95 public:
96 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
97
98 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
99 __ Bind(GetEntryLabel());
100 __ gs()->call(
101 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
102 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
103 }
104
105 private:
106 HDivZeroCheck* const instruction_;
107 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
108};
109
Calin Juravlebacfec32014-11-14 15:54:36 +0000110class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
113 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000114
115 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
116 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000117 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 if (is_div_) {
119 __ negl(cpu_reg_);
120 } else {
121 __ movl(cpu_reg_, Immediate(0));
122 }
123
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000124 } else {
125 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 if (is_div_) {
127 __ negq(cpu_reg_);
128 } else {
129 __ movq(cpu_reg_, Immediate(0));
130 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000131 }
Calin Juravled0d48522014-11-04 16:40:20 +0000132 __ jmp(GetExitLabel());
133 }
134
135 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000136 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000137 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 const bool is_div_;
139 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000140};
141
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100142class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100143 public:
144 StackOverflowCheckSlowPathX86_64() {}
145
146 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
147 __ Bind(GetEntryLabel());
148 __ addq(CpuRegister(RSP),
149 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
150 __ gs()->jmp(
151 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
152 }
153
154 private:
155 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
156};
157
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100158class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
161 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162
163 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100164 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100166 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
168 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100169 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 if (successor_ == nullptr) {
171 __ jmp(GetReturnLabel());
172 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100174 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 }
176
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 Label* GetReturnLabel() {
178 DCHECK(successor_ == nullptr);
179 return &return_label_;
180 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181
182 private:
183 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 Label return_label_;
186
187 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
188};
189
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100190class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100192 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
193 Location index_location,
194 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100195 : instruction_(instruction),
196 index_location_(index_location),
197 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198
199 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100200 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000201 // We're moving two locations to locations that could overlap, so we need a parallel
202 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100203 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000204 codegen->EmitParallelMoves(
205 index_location_,
206 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
207 length_location_,
208 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100209 __ gs()->call(Address::Absolute(
210 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100211 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100212 }
213
214 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100215 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100216 const Location index_location_;
217 const Location length_location_;
218
219 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
220};
221
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100223 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 LoadClassSlowPathX86_64(HLoadClass* cls,
225 HInstruction* at,
226 uint32_t dex_pc,
227 bool do_clinit)
228 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
229 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
230 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100231
232 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
235 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100236
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 codegen->SaveLiveRegisters(locations);
238
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100241 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000242 __ gs()->call(Address::Absolute((do_clinit_
243 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
244 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
245 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100246
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000247 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000249 if (out.IsValid()) {
250 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
251 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 }
253
254 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100255 __ jmp(GetExitLabel());
256 }
257
258 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000259 // The class this slow path will load.
260 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100261
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 // The instruction where this slow path is happening.
263 // (Might be the load class or an initialization check).
264 HInstruction* const at_;
265
266 // The dex PC of `at_`.
267 const uint32_t dex_pc_;
268
269 // Whether to initialize the class.
270 const bool do_clinit_;
271
272 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100273};
274
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000275class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
276 public:
277 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
278
279 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
280 LocationSummary* locations = instruction_->GetLocations();
281 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
282
283 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
284 __ Bind(GetEntryLabel());
285 codegen->SaveLiveRegisters(locations);
286
287 InvokeRuntimeCallingConvention calling_convention;
288 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(0)));
289 __ movl(CpuRegister(calling_convention.GetRegisterAt(1)),
290 Immediate(instruction_->GetStringIndex()));
291 __ gs()->call(Address::Absolute(
292 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
293 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
294 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
295 codegen->RestoreLiveRegisters(locations);
296 __ jmp(GetExitLabel());
297 }
298
299 private:
300 HLoadString* const instruction_;
301
302 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
303};
304
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
306 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 TypeCheckSlowPathX86_64(HInstruction* instruction,
308 Location class_to_check,
309 Location object_class,
310 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000312 class_to_check_(class_to_check),
313 object_class_(object_class),
314 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315
316 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
317 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000318 DCHECK(instruction_->IsCheckCast()
319 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
322 __ Bind(GetEntryLabel());
323 codegen->SaveLiveRegisters(locations);
324
325 // We're moving two locations to locations that could overlap, so we need a parallel
326 // move resolver.
327 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000328 codegen->EmitParallelMoves(
329 class_to_check_,
330 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
331 object_class_,
332 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000334 if (instruction_->IsInstanceOf()) {
335 __ gs()->call(
336 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
337 } else {
338 DCHECK(instruction_->IsCheckCast());
339 __ gs()->call(
340 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
341 }
342 codegen->RecordPcInfo(instruction_, dex_pc_);
343
344 if (instruction_->IsInstanceOf()) {
345 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
346 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347
348 codegen->RestoreLiveRegisters(locations);
349 __ jmp(GetExitLabel());
350 }
351
352 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353 HInstruction* const instruction_;
354 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000355 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000357
358 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
359};
360
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100361#undef __
362#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
363
Dave Allison20dfc792014-06-16 20:44:29 -0700364inline Condition X86_64Condition(IfCondition cond) {
365 switch (cond) {
366 case kCondEQ: return kEqual;
367 case kCondNE: return kNotEqual;
368 case kCondLT: return kLess;
369 case kCondLE: return kLessEqual;
370 case kCondGT: return kGreater;
371 case kCondGE: return kGreaterEqual;
372 default:
373 LOG(FATAL) << "Unknown if condition";
374 }
375 return kEqual;
376}
377
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100378void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
379 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
380}
381
382void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
383 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
384}
385
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100386size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
387 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
388 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100389}
390
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100391size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
392 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
393 return kX86_64WordSize;
394}
395
396size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
397 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
398 return kX86_64WordSize;
399}
400
401size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
402 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
403 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100404}
405
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100406CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100407 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100408 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100409 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000410 instruction_visitor_(graph, this),
411 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100412
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100413size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
414 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
415}
416
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100417InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
418 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100419 : HGraphVisitor(graph),
420 assembler_(codegen->GetAssembler()),
421 codegen_(codegen) {}
422
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100423Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100424 switch (type) {
425 case Primitive::kPrimLong:
426 case Primitive::kPrimByte:
427 case Primitive::kPrimBoolean:
428 case Primitive::kPrimChar:
429 case Primitive::kPrimShort:
430 case Primitive::kPrimInt:
431 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100433 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100434 }
435
436 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100437 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100438 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100439 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100440 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100441
442 case Primitive::kPrimVoid:
443 LOG(FATAL) << "Unreachable type " << type;
444 }
445
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100446 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100447}
448
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100449void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100450 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100451 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100452
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000453 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100454 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000455
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100456 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100457 blocked_core_registers_[RBX] = true;
458 blocked_core_registers_[RBP] = true;
459 blocked_core_registers_[R12] = true;
460 blocked_core_registers_[R13] = true;
461 blocked_core_registers_[R14] = true;
462 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100463
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100464 blocked_fpu_registers_[XMM12] = true;
465 blocked_fpu_registers_[XMM13] = true;
466 blocked_fpu_registers_[XMM14] = true;
467 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100468}
469
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100470void CodeGeneratorX86_64::GenerateFrameEntry() {
471 // Create a fake register to mimic Quick.
472 static const int kFakeReturnRegister = 16;
473 core_spill_mask_ |= (1 << kFakeReturnRegister);
474
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 Geoffrayf6e206c2014-08-07 20:25:41 +0100477
478 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
479 __ testq(CpuRegister(RAX), Address(
480 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100481 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100482 }
483
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100484 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100485 __ subq(CpuRegister(RSP),
486 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
487
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100488 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100489 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100490 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100491
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100492 __ gs()->cmpq(CpuRegister(RSP),
493 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
494 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100495 }
496
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100497 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
498}
499
500void CodeGeneratorX86_64::GenerateFrameExit() {
501 __ addq(CpuRegister(RSP),
502 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
503}
504
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100505void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
506 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100507}
508
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100509void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100510 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
511}
512
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100513Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
514 switch (load->GetType()) {
515 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100516 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100517 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
518 break;
519
520 case Primitive::kPrimInt:
521 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100522 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100523 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100524
525 case Primitive::kPrimBoolean:
526 case Primitive::kPrimByte:
527 case Primitive::kPrimChar:
528 case Primitive::kPrimShort:
529 case Primitive::kPrimVoid:
530 LOG(FATAL) << "Unexpected type " << load->GetType();
531 }
532
533 LOG(FATAL) << "Unreachable";
534 return Location();
535}
536
537void CodeGeneratorX86_64::Move(Location destination, Location source) {
538 if (source.Equals(destination)) {
539 return;
540 }
541 if (destination.IsRegister()) {
542 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000543 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000545 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100546 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000547 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100548 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100549 } else {
550 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000551 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100552 Address(CpuRegister(RSP), source.GetStackIndex()));
553 }
554 } else if (destination.IsFpuRegister()) {
555 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000556 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100557 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000558 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100559 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000560 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100561 Address(CpuRegister(RSP), source.GetStackIndex()));
562 } else {
563 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000564 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100565 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100566 }
567 } else if (destination.IsStackSlot()) {
568 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100569 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000570 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100571 } else if (source.IsFpuRegister()) {
572 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000573 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100574 } else {
575 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000576 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
577 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100578 }
579 } else {
580 DCHECK(destination.IsDoubleStackSlot());
581 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000583 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100584 } else if (source.IsFpuRegister()) {
585 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000586 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100587 } else {
588 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000589 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
590 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591 }
592 }
593}
594
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100595void CodeGeneratorX86_64::Move(HInstruction* instruction,
596 Location location,
597 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000598 LocationSummary* locations = instruction->GetLocations();
599 if (locations != nullptr && locations->Out().Equals(location)) {
600 return;
601 }
602
603 if (locations != nullptr && locations->Out().IsConstant()) {
604 HConstant* const_to_move = locations->Out().GetConstant();
605 if (const_to_move->IsIntConstant()) {
606 Immediate imm(const_to_move->AsIntConstant()->GetValue());
607 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000608 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000609 } else if (location.IsStackSlot()) {
610 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
611 } else {
612 DCHECK(location.IsConstant());
613 DCHECK_EQ(location.GetConstant(), const_to_move);
614 }
615 } else if (const_to_move->IsLongConstant()) {
616 int64_t value = const_to_move->AsLongConstant()->GetValue();
617 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000618 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000619 } else if (location.IsDoubleStackSlot()) {
620 __ movq(CpuRegister(TMP), Immediate(value));
621 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
622 } else {
623 DCHECK(location.IsConstant());
624 DCHECK_EQ(location.GetConstant(), const_to_move);
625 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100626 }
Roland Levillain476df552014-10-09 17:51:36 +0100627 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100628 switch (instruction->GetType()) {
629 case Primitive::kPrimBoolean:
630 case Primitive::kPrimByte:
631 case Primitive::kPrimChar:
632 case Primitive::kPrimShort:
633 case Primitive::kPrimInt:
634 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100636 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
637 break;
638
639 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100640 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000641 Move(location,
642 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100643 break;
644
645 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100646 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100647 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000648 } else if (instruction->IsTemporary()) {
649 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
650 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100651 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100652 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100653 switch (instruction->GetType()) {
654 case Primitive::kPrimBoolean:
655 case Primitive::kPrimByte:
656 case Primitive::kPrimChar:
657 case Primitive::kPrimShort:
658 case Primitive::kPrimInt:
659 case Primitive::kPrimNot:
660 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100661 case Primitive::kPrimFloat:
662 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000663 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100664 break;
665
666 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100667 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100668 }
669 }
670}
671
672void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
673 got->SetLocations(nullptr);
674}
675
676void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
677 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100678 DCHECK(!successor->IsExitBlock());
679
680 HBasicBlock* block = got->GetBlock();
681 HInstruction* previous = got->GetPrevious();
682
683 HLoopInformation* info = block->GetLoopInformation();
684 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
685 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
686 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
687 return;
688 }
689
690 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
691 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
692 }
693 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100694 __ jmp(codegen_->GetLabelOf(successor));
695 }
696}
697
698void LocationsBuilderX86_64::VisitExit(HExit* exit) {
699 exit->SetLocations(nullptr);
700}
701
702void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700703 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704 if (kIsDebugBuild) {
705 __ Comment("Unreachable");
706 __ int3();
707 }
708}
709
710void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100711 LocationSummary* locations =
712 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100713 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100714 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100715 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100716 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717}
718
719void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700720 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100721 if (cond->IsIntConstant()) {
722 // Constant condition, statically compared against 1.
723 int32_t cond_value = cond->AsIntConstant()->GetValue();
724 if (cond_value == 1) {
725 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
726 if_instr->IfTrueSuccessor())) {
727 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100728 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100729 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100730 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100731 DCHECK_EQ(cond_value, 0);
732 }
733 } else {
734 bool materialized =
735 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
736 // Moves do not affect the eflags register, so if the condition is
737 // evaluated just before the if, we don't need to evaluate it
738 // again.
739 bool eflags_set = cond->IsCondition()
740 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
741 if (materialized) {
742 if (!eflags_set) {
743 // Materialized condition, compare against 0.
744 Location lhs = if_instr->GetLocations()->InAt(0);
745 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000746 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100747 } else {
748 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
749 Immediate(0));
750 }
751 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
752 } else {
753 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
754 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
755 }
756 } else {
757 Location lhs = cond->GetLocations()->InAt(0);
758 Location rhs = cond->GetLocations()->InAt(1);
759 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000760 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100761 } else if (rhs.IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000762 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100763 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
764 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000765 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100766 Address(CpuRegister(RSP), rhs.GetStackIndex()));
767 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100768 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
769 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700770 }
Dave Allison20dfc792014-06-16 20:44:29 -0700771 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100772 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
773 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700774 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100775 }
776}
777
778void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
779 local->SetLocations(nullptr);
780}
781
782void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
783 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
784}
785
786void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
787 local->SetLocations(nullptr);
788}
789
790void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
791 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700792 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100793}
794
795void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100796 LocationSummary* locations =
797 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100798 switch (store->InputAt(1)->GetType()) {
799 case Primitive::kPrimBoolean:
800 case Primitive::kPrimByte:
801 case Primitive::kPrimChar:
802 case Primitive::kPrimShort:
803 case Primitive::kPrimInt:
804 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100805 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100806 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
807 break;
808
809 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100810 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100811 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
812 break;
813
814 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100815 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100816 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100817}
818
819void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700820 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100821}
822
Dave Allison20dfc792014-06-16 20:44:29 -0700823void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100824 LocationSummary* locations =
825 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100826 locations->SetInAt(0, Location::RequiresRegister());
827 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100828 if (comp->NeedsMaterialization()) {
829 locations->SetOut(Location::RequiresRegister());
830 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100831}
832
Dave Allison20dfc792014-06-16 20:44:29 -0700833void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
834 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100835 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000836 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100837 // Clear register: setcc only sets the low byte.
838 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100839 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000840 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
841 locations->InAt(1).AsRegister<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100842 } else if (locations->InAt(1).IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000843 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100844 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
845 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000846 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100847 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
848 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100849 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700850 }
851}
852
853void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
854 VisitCondition(comp);
855}
856
857void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
858 VisitCondition(comp);
859}
860
861void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
862 VisitCondition(comp);
863}
864
865void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
866 VisitCondition(comp);
867}
868
869void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
870 VisitCondition(comp);
871}
872
873void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
874 VisitCondition(comp);
875}
876
877void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
878 VisitCondition(comp);
879}
880
881void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
882 VisitCondition(comp);
883}
884
885void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
886 VisitCondition(comp);
887}
888
889void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
890 VisitCondition(comp);
891}
892
893void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
894 VisitCondition(comp);
895}
896
897void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
898 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100899}
900
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100901void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100902 LocationSummary* locations =
903 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000904 switch (compare->InputAt(0)->GetType()) {
905 case Primitive::kPrimLong: {
906 locations->SetInAt(0, Location::RequiresRegister());
907 locations->SetInAt(1, Location::RequiresRegister());
908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
909 break;
910 }
911 case Primitive::kPrimFloat:
912 case Primitive::kPrimDouble: {
913 locations->SetInAt(0, Location::RequiresFpuRegister());
914 locations->SetInAt(1, Location::RequiresFpuRegister());
915 locations->SetOut(Location::RequiresRegister());
916 break;
917 }
918 default:
919 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
920 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100921}
922
923void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100924 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000925 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000926 Location left = locations->InAt(0);
927 Location right = locations->InAt(1);
928
929 Label less, greater, done;
930 Primitive::Type type = compare->InputAt(0)->GetType();
931 switch (type) {
932 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000933 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100934 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000935 }
936 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000937 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000938 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
939 break;
940 }
941 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000942 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000943 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
944 break;
945 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100946 default:
Calin Juravleddb7df22014-11-25 20:56:51 +0000947 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100948 }
Calin Juravleddb7df22014-11-25 20:56:51 +0000949 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +0000950 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +0000951 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +0000952
Calin Juravle91debbc2014-11-26 19:01:09 +0000953 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +0000954 __ movl(out, Immediate(1));
955 __ jmp(&done);
956
957 __ Bind(&less);
958 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100959
960 __ Bind(&done);
961}
962
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100963void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100964 LocationSummary* locations =
965 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100966 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100967}
968
969void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100970 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700971 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100972}
973
974void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100975 LocationSummary* locations =
976 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100977 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100978}
979
980void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100981 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700982 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100983}
984
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100985void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
986 LocationSummary* locations =
987 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
988 locations->SetOut(Location::ConstantLocation(constant));
989}
990
991void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
992 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700993 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100994}
995
996void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
997 LocationSummary* locations =
998 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
999 locations->SetOut(Location::ConstantLocation(constant));
1000}
1001
1002void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1003 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001004 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001005}
1006
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001007void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1008 ret->SetLocations(nullptr);
1009}
1010
1011void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001012 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001013 codegen_->GenerateFrameExit();
1014 __ ret();
1015}
1016
1017void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001018 LocationSummary* locations =
1019 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001020 switch (ret->InputAt(0)->GetType()) {
1021 case Primitive::kPrimBoolean:
1022 case Primitive::kPrimByte:
1023 case Primitive::kPrimChar:
1024 case Primitive::kPrimShort:
1025 case Primitive::kPrimInt:
1026 case Primitive::kPrimNot:
1027 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001028 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001029 break;
1030
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001031 case Primitive::kPrimFloat:
1032 case Primitive::kPrimDouble:
1033 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001034 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001035 break;
1036
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001037 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001038 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001039 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001040}
1041
1042void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1043 if (kIsDebugBuild) {
1044 switch (ret->InputAt(0)->GetType()) {
1045 case Primitive::kPrimBoolean:
1046 case Primitive::kPrimByte:
1047 case Primitive::kPrimChar:
1048 case Primitive::kPrimShort:
1049 case Primitive::kPrimInt:
1050 case Primitive::kPrimNot:
1051 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001052 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001053 break;
1054
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001055 case Primitive::kPrimFloat:
1056 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001057 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 XMM0);
1059 break;
1060
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001061 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001062 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001063 }
1064 }
1065 codegen_->GenerateFrameExit();
1066 __ ret();
1067}
1068
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001069Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1070 switch (type) {
1071 case Primitive::kPrimBoolean:
1072 case Primitive::kPrimByte:
1073 case Primitive::kPrimChar:
1074 case Primitive::kPrimShort:
1075 case Primitive::kPrimInt:
1076 case Primitive::kPrimNot: {
1077 uint32_t index = gp_index_++;
1078 stack_index_++;
1079 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001080 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081 } else {
1082 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1083 }
1084 }
1085
1086 case Primitive::kPrimLong: {
1087 uint32_t index = gp_index_;
1088 stack_index_ += 2;
1089 if (index < calling_convention.GetNumberOfRegisters()) {
1090 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001091 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001092 } else {
1093 gp_index_ += 2;
1094 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1095 }
1096 }
1097
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001098 case Primitive::kPrimFloat: {
1099 uint32_t index = fp_index_++;
1100 stack_index_++;
1101 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 } else {
1104 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1105 }
1106 }
1107
1108 case Primitive::kPrimDouble: {
1109 uint32_t index = fp_index_++;
1110 stack_index_ += 2;
1111 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001112 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 } else {
1114 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1115 }
1116 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001117
1118 case Primitive::kPrimVoid:
1119 LOG(FATAL) << "Unexpected parameter type " << type;
1120 break;
1121 }
1122 return Location();
1123}
1124
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001125void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001126 HandleInvoke(invoke);
1127}
1128
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001129void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001130 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001131 // TODO: Implement all kinds of calls:
1132 // 1) boot -> boot
1133 // 2) app -> boot
1134 // 3) app -> app
1135 //
1136 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1137
1138 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001139 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001140 // temp = temp->dex_cache_resolved_methods_;
1141 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1142 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001143 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001144 // (temp + offset_of_quick_compiled_code)()
Mathieu Chartier2d721012014-11-10 11:08:06 -08001145 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001146 kX86_64WordSize).SizeValue()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001147
1148 DCHECK(!codegen_->IsLeafMethod());
1149 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1150}
1151
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001152void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001153 LocationSummary* locations =
1154 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001156
1157 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001158 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001159 HInstruction* input = invoke->InputAt(i);
1160 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1161 }
1162
1163 switch (invoke->GetType()) {
1164 case Primitive::kPrimBoolean:
1165 case Primitive::kPrimByte:
1166 case Primitive::kPrimChar:
1167 case Primitive::kPrimShort:
1168 case Primitive::kPrimInt:
1169 case Primitive::kPrimNot:
1170 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001171 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001172 break;
1173
1174 case Primitive::kPrimVoid:
1175 break;
1176
1177 case Primitive::kPrimDouble:
1178 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001179 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001180 break;
1181 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001182}
1183
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001184void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1185 HandleInvoke(invoke);
1186}
1187
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001188void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001189 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001190 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1191 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1192 LocationSummary* locations = invoke->GetLocations();
1193 Location receiver = locations->InAt(0);
1194 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1195 // temp = object->GetClass();
1196 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001197 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1198 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001199 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001200 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001201 }
1202 // temp = temp->GetMethodAt(method_offset);
1203 __ movl(temp, Address(temp, method_offset));
1204 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001205 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001206 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001207
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001208 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001209 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001210}
1211
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001212void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1213 HandleInvoke(invoke);
1214 // Add the hidden argument.
1215 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1216}
1217
1218void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1219 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001220 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001221 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1222 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1223 LocationSummary* locations = invoke->GetLocations();
1224 Location receiver = locations->InAt(0);
1225 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1226
1227 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001229 Immediate(invoke->GetDexMethodIndex()));
1230
1231 // temp = object->GetClass();
1232 if (receiver.IsStackSlot()) {
1233 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1234 __ movl(temp, Address(temp, class_offset));
1235 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001237 }
1238 // temp = temp->GetImtEntryAt(method_offset);
1239 __ movl(temp, Address(temp, method_offset));
1240 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001241 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001242 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001243
1244 DCHECK(!codegen_->IsLeafMethod());
1245 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1246}
1247
Roland Levillain88cb1752014-10-20 16:36:47 +01001248void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1249 LocationSummary* locations =
1250 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1251 switch (neg->GetResultType()) {
1252 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001253 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001254 locations->SetInAt(0, Location::RequiresRegister());
1255 locations->SetOut(Location::SameAsFirstInput());
1256 break;
1257
Roland Levillain88cb1752014-10-20 16:36:47 +01001258 case Primitive::kPrimFloat:
1259 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001260 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001261 locations->SetOut(Location::SameAsFirstInput());
1262 locations->AddTemp(Location::RequiresRegister());
1263 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001264 break;
1265
1266 default:
1267 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1268 }
1269}
1270
1271void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1272 LocationSummary* locations = neg->GetLocations();
1273 Location out = locations->Out();
1274 Location in = locations->InAt(0);
1275 switch (neg->GetResultType()) {
1276 case Primitive::kPrimInt:
1277 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001278 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001279 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001280 break;
1281
1282 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001283 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001284 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001285 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001286 break;
1287
Roland Levillain5368c212014-11-27 15:03:41 +00001288 case Primitive::kPrimFloat: {
1289 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001290 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1291 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001292 // Implement float negation with an exclusive or with value
1293 // 0x80000000 (mask for bit 31, representing the sign of a
1294 // single-precision floating-point number).
1295 __ movq(constant, Immediate(INT64_C(0x80000000)));
1296 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001297 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001298 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001299 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001300
Roland Levillain5368c212014-11-27 15:03:41 +00001301 case Primitive::kPrimDouble: {
1302 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001303 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1304 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001305 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001306 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001307 // a double-precision floating-point number).
1308 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1309 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001311 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001312 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001313
1314 default:
1315 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1316 }
1317}
1318
Roland Levillaindff1f282014-11-05 14:15:05 +00001319void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1320 LocationSummary* locations =
1321 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1322 Primitive::Type result_type = conversion->GetResultType();
1323 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001324 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001325 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001326 case Primitive::kPrimByte:
1327 switch (input_type) {
1328 case Primitive::kPrimShort:
1329 case Primitive::kPrimInt:
1330 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001331 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001332 locations->SetInAt(0, Location::Any());
1333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1334 break;
1335
1336 default:
1337 LOG(FATAL) << "Unexpected type conversion from " << input_type
1338 << " to " << result_type;
1339 }
1340 break;
1341
Roland Levillain01a8d712014-11-14 16:27:39 +00001342 case Primitive::kPrimShort:
1343 switch (input_type) {
1344 case Primitive::kPrimByte:
1345 case Primitive::kPrimInt:
1346 case Primitive::kPrimChar:
1347 // Processing a Dex `int-to-short' instruction.
1348 locations->SetInAt(0, Location::Any());
1349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1350 break;
1351
1352 default:
1353 LOG(FATAL) << "Unexpected type conversion from " << input_type
1354 << " to " << result_type;
1355 }
1356 break;
1357
Roland Levillain946e1432014-11-11 17:35:19 +00001358 case Primitive::kPrimInt:
1359 switch (input_type) {
1360 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001361 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001362 locations->SetInAt(0, Location::Any());
1363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1364 break;
1365
1366 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001367 // Processing a Dex `float-to-int' instruction.
1368 locations->SetInAt(0, Location::RequiresFpuRegister());
1369 locations->SetOut(Location::RequiresRegister());
1370 locations->AddTemp(Location::RequiresFpuRegister());
1371 break;
1372
Roland Levillain946e1432014-11-11 17:35:19 +00001373 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001374 // Processing a Dex `double-to-int' instruction.
1375 locations->SetInAt(0, Location::RequiresFpuRegister());
1376 locations->SetOut(Location::RequiresRegister());
1377 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001378 break;
1379
1380 default:
1381 LOG(FATAL) << "Unexpected type conversion from " << input_type
1382 << " to " << result_type;
1383 }
1384 break;
1385
Roland Levillaindff1f282014-11-05 14:15:05 +00001386 case Primitive::kPrimLong:
1387 switch (input_type) {
1388 case Primitive::kPrimByte:
1389 case Primitive::kPrimShort:
1390 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001391 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001392 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001393 // TODO: We would benefit from a (to-be-implemented)
1394 // Location::RegisterOrStackSlot requirement for this input.
1395 locations->SetInAt(0, Location::RequiresRegister());
1396 locations->SetOut(Location::RequiresRegister());
1397 break;
1398
1399 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001400 // Processing a Dex `float-to-long' instruction.
1401 locations->SetInAt(0, Location::RequiresFpuRegister());
1402 locations->SetOut(Location::RequiresRegister());
1403 locations->AddTemp(Location::RequiresFpuRegister());
1404 break;
1405
Roland Levillaindff1f282014-11-05 14:15:05 +00001406 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001407 // Processing a Dex `double-to-long' instruction.
1408 locations->SetInAt(0, Location::RequiresFpuRegister());
1409 locations->SetOut(Location::RequiresRegister());
1410 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001411 break;
1412
1413 default:
1414 LOG(FATAL) << "Unexpected type conversion from " << input_type
1415 << " to " << result_type;
1416 }
1417 break;
1418
Roland Levillain981e4542014-11-14 11:47:14 +00001419 case Primitive::kPrimChar:
1420 switch (input_type) {
1421 case Primitive::kPrimByte:
1422 case Primitive::kPrimShort:
1423 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001424 // Processing a Dex `int-to-char' instruction.
1425 locations->SetInAt(0, Location::Any());
1426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1427 break;
1428
1429 default:
1430 LOG(FATAL) << "Unexpected type conversion from " << input_type
1431 << " to " << result_type;
1432 }
1433 break;
1434
Roland Levillaindff1f282014-11-05 14:15:05 +00001435 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001436 switch (input_type) {
1437 case Primitive::kPrimByte:
1438 case Primitive::kPrimShort:
1439 case Primitive::kPrimInt:
1440 case Primitive::kPrimChar:
1441 // Processing a Dex `int-to-float' instruction.
1442 locations->SetInAt(0, Location::RequiresRegister());
1443 locations->SetOut(Location::RequiresFpuRegister());
1444 break;
1445
1446 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001447 // Processing a Dex `long-to-float' instruction.
1448 locations->SetInAt(0, Location::RequiresRegister());
1449 locations->SetOut(Location::RequiresFpuRegister());
1450 break;
1451
Roland Levillaincff13742014-11-17 14:32:17 +00001452 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001453 // Processing a Dex `double-to-float' instruction.
1454 locations->SetInAt(0, Location::RequiresFpuRegister());
1455 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001456 break;
1457
1458 default:
1459 LOG(FATAL) << "Unexpected type conversion from " << input_type
1460 << " to " << result_type;
1461 };
1462 break;
1463
Roland Levillaindff1f282014-11-05 14:15:05 +00001464 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001465 switch (input_type) {
1466 case Primitive::kPrimByte:
1467 case Primitive::kPrimShort:
1468 case Primitive::kPrimInt:
1469 case Primitive::kPrimChar:
1470 // Processing a Dex `int-to-double' instruction.
1471 locations->SetInAt(0, Location::RequiresRegister());
1472 locations->SetOut(Location::RequiresFpuRegister());
1473 break;
1474
1475 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001476 // Processing a Dex `long-to-double' instruction.
1477 locations->SetInAt(0, Location::RequiresRegister());
1478 locations->SetOut(Location::RequiresFpuRegister());
1479 break;
1480
Roland Levillaincff13742014-11-17 14:32:17 +00001481 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001482 // Processing a Dex `float-to-double' instruction.
1483 locations->SetInAt(0, Location::RequiresFpuRegister());
1484 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001485 break;
1486
1487 default:
1488 LOG(FATAL) << "Unexpected type conversion from " << input_type
1489 << " to " << result_type;
1490 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001491 break;
1492
1493 default:
1494 LOG(FATAL) << "Unexpected type conversion from " << input_type
1495 << " to " << result_type;
1496 }
1497}
1498
1499void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1500 LocationSummary* locations = conversion->GetLocations();
1501 Location out = locations->Out();
1502 Location in = locations->InAt(0);
1503 Primitive::Type result_type = conversion->GetResultType();
1504 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001505 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001506 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001507 case Primitive::kPrimByte:
1508 switch (input_type) {
1509 case Primitive::kPrimShort:
1510 case Primitive::kPrimInt:
1511 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001512 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001513 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001514 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001515 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001516 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001517 Address(CpuRegister(RSP), in.GetStackIndex()));
1518 } else {
1519 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001520 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001521 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1522 }
1523 break;
1524
1525 default:
1526 LOG(FATAL) << "Unexpected type conversion from " << input_type
1527 << " to " << result_type;
1528 }
1529 break;
1530
Roland Levillain01a8d712014-11-14 16:27:39 +00001531 case Primitive::kPrimShort:
1532 switch (input_type) {
1533 case Primitive::kPrimByte:
1534 case Primitive::kPrimInt:
1535 case Primitive::kPrimChar:
1536 // Processing a Dex `int-to-short' instruction.
1537 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001538 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001539 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001540 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001541 Address(CpuRegister(RSP), in.GetStackIndex()));
1542 } else {
1543 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001544 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001545 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1546 }
1547 break;
1548
1549 default:
1550 LOG(FATAL) << "Unexpected type conversion from " << input_type
1551 << " to " << result_type;
1552 }
1553 break;
1554
Roland Levillain946e1432014-11-11 17:35:19 +00001555 case Primitive::kPrimInt:
1556 switch (input_type) {
1557 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001558 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001559 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001560 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001561 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001562 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001563 Address(CpuRegister(RSP), in.GetStackIndex()));
1564 } else {
1565 DCHECK(in.IsConstant());
1566 DCHECK(in.GetConstant()->IsLongConstant());
1567 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001568 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001569 }
1570 break;
1571
Roland Levillain3f8f9362014-12-02 17:45:01 +00001572 case Primitive::kPrimFloat: {
1573 // Processing a Dex `float-to-int' instruction.
1574 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1575 CpuRegister output = out.AsRegister<CpuRegister>();
1576 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1577 Label done, nan;
1578
1579 __ movl(output, Immediate(kPrimIntMax));
1580 // temp = int-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001581 __ cvtsi2ss(temp, output, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001582 // if input >= temp goto done
1583 __ comiss(input, temp);
1584 __ j(kAboveEqual, &done);
1585 // if input == NaN goto nan
1586 __ j(kUnordered, &nan);
1587 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001588 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00001589 __ jmp(&done);
1590 __ Bind(&nan);
1591 // output = 0
1592 __ xorl(output, output);
1593 __ Bind(&done);
1594 break;
1595 }
1596
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001597 case Primitive::kPrimDouble: {
1598 // Processing a Dex `double-to-int' instruction.
1599 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1600 CpuRegister output = out.AsRegister<CpuRegister>();
1601 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1602 Label done, nan;
1603
1604 __ movl(output, Immediate(kPrimIntMax));
1605 // temp = int-to-double(output)
1606 __ cvtsi2sd(temp, output);
1607 // if input >= temp goto done
1608 __ comisd(input, temp);
1609 __ j(kAboveEqual, &done);
1610 // if input == NaN goto nan
1611 __ j(kUnordered, &nan);
1612 // output = double-to-int-truncate(input)
1613 __ cvttsd2si(output, input);
1614 __ jmp(&done);
1615 __ Bind(&nan);
1616 // output = 0
1617 __ xorl(output, output);
1618 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001619 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001620 }
Roland Levillain946e1432014-11-11 17:35:19 +00001621
1622 default:
1623 LOG(FATAL) << "Unexpected type conversion from " << input_type
1624 << " to " << result_type;
1625 }
1626 break;
1627
Roland Levillaindff1f282014-11-05 14:15:05 +00001628 case Primitive::kPrimLong:
1629 switch (input_type) {
1630 DCHECK(out.IsRegister());
1631 case Primitive::kPrimByte:
1632 case Primitive::kPrimShort:
1633 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001634 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001635 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001636 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001637 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001638 break;
1639
Roland Levillain624279f2014-12-04 11:54:28 +00001640 case Primitive::kPrimFloat: {
1641 // Processing a Dex `float-to-long' instruction.
1642 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1643 CpuRegister output = out.AsRegister<CpuRegister>();
1644 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1645 Label done, nan;
1646
1647 __ movq(output, Immediate(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001648 // temp = long-to-float(output)
Roland Levillain624279f2014-12-04 11:54:28 +00001649 __ cvtsi2ss(temp, output, true);
1650 // if input >= temp goto done
1651 __ comiss(input, temp);
1652 __ j(kAboveEqual, &done);
1653 // if input == NaN goto nan
1654 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001655 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00001656 __ cvttss2si(output, input, true);
1657 __ jmp(&done);
1658 __ Bind(&nan);
1659 // output = 0
1660 __ xorq(output, output);
1661 __ Bind(&done);
1662 break;
1663 }
1664
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001665 case Primitive::kPrimDouble: {
1666 // Processing a Dex `double-to-long' instruction.
1667 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1668 CpuRegister output = out.AsRegister<CpuRegister>();
1669 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1670 Label done, nan;
1671
1672 __ movq(output, Immediate(kPrimLongMax));
1673 // temp = long-to-double(output)
1674 __ cvtsi2sd(temp, output, true);
1675 // if input >= temp goto done
1676 __ comisd(input, temp);
1677 __ j(kAboveEqual, &done);
1678 // if input == NaN goto nan
1679 __ j(kUnordered, &nan);
1680 // output = double-to-long-truncate(input)
1681 __ cvttsd2si(output, input, true);
1682 __ jmp(&done);
1683 __ Bind(&nan);
1684 // output = 0
1685 __ xorq(output, output);
1686 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00001687 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001688 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001689
1690 default:
1691 LOG(FATAL) << "Unexpected type conversion from " << input_type
1692 << " to " << result_type;
1693 }
1694 break;
1695
Roland Levillain981e4542014-11-14 11:47:14 +00001696 case Primitive::kPrimChar:
1697 switch (input_type) {
1698 case Primitive::kPrimByte:
1699 case Primitive::kPrimShort:
1700 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001701 // Processing a Dex `int-to-char' instruction.
1702 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001703 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001704 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001705 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001706 Address(CpuRegister(RSP), in.GetStackIndex()));
1707 } else {
1708 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001709 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001710 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1711 }
1712 break;
1713
1714 default:
1715 LOG(FATAL) << "Unexpected type conversion from " << input_type
1716 << " to " << result_type;
1717 }
1718 break;
1719
Roland Levillaindff1f282014-11-05 14:15:05 +00001720 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001721 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001722 case Primitive::kPrimByte:
1723 case Primitive::kPrimShort:
1724 case Primitive::kPrimInt:
1725 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001726 // Processing a Dex `int-to-float' instruction.
1727 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001728 break;
1729
1730 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001731 // Processing a Dex `long-to-float' instruction.
1732 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
1733 break;
1734
Roland Levillaincff13742014-11-17 14:32:17 +00001735 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001736 // Processing a Dex `double-to-float' instruction.
1737 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001738 break;
1739
1740 default:
1741 LOG(FATAL) << "Unexpected type conversion from " << input_type
1742 << " to " << result_type;
1743 };
1744 break;
1745
Roland Levillaindff1f282014-11-05 14:15:05 +00001746 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001747 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001748 case Primitive::kPrimByte:
1749 case Primitive::kPrimShort:
1750 case Primitive::kPrimInt:
1751 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001752 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001753 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001754 break;
1755
1756 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001757 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001758 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001759 break;
1760
Roland Levillaincff13742014-11-17 14:32:17 +00001761 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001762 // Processing a Dex `float-to-double' instruction.
1763 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001764 break;
1765
1766 default:
1767 LOG(FATAL) << "Unexpected type conversion from " << input_type
1768 << " to " << result_type;
1769 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001770 break;
1771
1772 default:
1773 LOG(FATAL) << "Unexpected type conversion from " << input_type
1774 << " to " << result_type;
1775 }
1776}
1777
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001778void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001779 LocationSummary* locations =
1780 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001781 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001782 case Primitive::kPrimInt: {
1783 locations->SetInAt(0, Location::RequiresRegister());
1784 locations->SetInAt(1, Location::Any());
1785 locations->SetOut(Location::SameAsFirstInput());
1786 break;
1787 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001788
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001789 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001790 locations->SetInAt(0, Location::RequiresRegister());
1791 locations->SetInAt(1, Location::RequiresRegister());
1792 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001793 break;
1794 }
1795
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001796 case Primitive::kPrimDouble:
1797 case Primitive::kPrimFloat: {
1798 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001799 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001800 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001801 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001802 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001803
1804 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001805 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001806 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001807}
1808
1809void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1810 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001811 Location first = locations->InAt(0);
1812 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001813 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001814
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001815 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001816 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001817 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001818 __ addl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001819 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001820 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001821 __ addl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001822 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001823 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001824 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001825 break;
1826 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001827
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001828 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001829 __ addq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001830 break;
1831 }
1832
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001833 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001834 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001835 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001836 }
1837
1838 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001839 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001840 break;
1841 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001842
1843 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001844 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001845 }
1846}
1847
1848void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001849 LocationSummary* locations =
1850 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001851 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001852 case Primitive::kPrimInt: {
1853 locations->SetInAt(0, Location::RequiresRegister());
1854 locations->SetInAt(1, Location::Any());
1855 locations->SetOut(Location::SameAsFirstInput());
1856 break;
1857 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001858 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001859 locations->SetInAt(0, Location::RequiresRegister());
1860 locations->SetInAt(1, Location::RequiresRegister());
1861 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001862 break;
1863 }
Calin Juravle11351682014-10-23 15:38:15 +01001864 case Primitive::kPrimFloat:
1865 case Primitive::kPrimDouble: {
1866 locations->SetInAt(0, Location::RequiresFpuRegister());
1867 locations->SetInAt(1, Location::RequiresFpuRegister());
1868 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001869 break;
Calin Juravle11351682014-10-23 15:38:15 +01001870 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001871 default:
Calin Juravle11351682014-10-23 15:38:15 +01001872 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001873 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001874}
1875
1876void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1877 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001878 Location first = locations->InAt(0);
1879 Location second = locations->InAt(1);
1880 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001881 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001882 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001883 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001884 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001885 } else if (second.IsConstant()) {
1886 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001887 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001888 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001889 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001890 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001891 break;
1892 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001893 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001894 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001895 break;
1896 }
1897
Calin Juravle11351682014-10-23 15:38:15 +01001898 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001899 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001900 break;
Calin Juravle11351682014-10-23 15:38:15 +01001901 }
1902
1903 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001904 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001905 break;
1906 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001907
1908 default:
Calin Juravle11351682014-10-23 15:38:15 +01001909 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001910 }
1911}
1912
Calin Juravle34bacdf2014-10-07 20:23:36 +01001913void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1914 LocationSummary* locations =
1915 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1916 switch (mul->GetResultType()) {
1917 case Primitive::kPrimInt: {
1918 locations->SetInAt(0, Location::RequiresRegister());
1919 locations->SetInAt(1, Location::Any());
1920 locations->SetOut(Location::SameAsFirstInput());
1921 break;
1922 }
1923 case Primitive::kPrimLong: {
1924 locations->SetInAt(0, Location::RequiresRegister());
1925 locations->SetInAt(1, Location::RequiresRegister());
1926 locations->SetOut(Location::SameAsFirstInput());
1927 break;
1928 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001929 case Primitive::kPrimFloat:
1930 case Primitive::kPrimDouble: {
1931 locations->SetInAt(0, Location::RequiresFpuRegister());
1932 locations->SetInAt(1, Location::RequiresFpuRegister());
1933 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001934 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001935 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001936
1937 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001938 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001939 }
1940}
1941
1942void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1943 LocationSummary* locations = mul->GetLocations();
1944 Location first = locations->InAt(0);
1945 Location second = locations->InAt(1);
1946 DCHECK(first.Equals(locations->Out()));
1947 switch (mul->GetResultType()) {
1948 case Primitive::kPrimInt: {
1949 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001950 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001951 } else if (second.IsConstant()) {
1952 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001953 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001954 } else {
1955 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00001956 __ imull(first.AsRegister<CpuRegister>(),
1957 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001958 }
1959 break;
1960 }
1961 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001963 break;
1964 }
1965
Calin Juravleb5bfa962014-10-21 18:02:24 +01001966 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001967 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001968 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001969 }
1970
1971 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001972 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01001973 break;
1974 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001975
1976 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001977 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001978 }
1979}
1980
Calin Juravlebacfec32014-11-14 15:54:36 +00001981void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1982 DCHECK(instruction->IsDiv() || instruction->IsRem());
1983 Primitive::Type type = instruction->GetResultType();
1984 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1985
1986 bool is_div = instruction->IsDiv();
1987 LocationSummary* locations = instruction->GetLocations();
1988
Roland Levillain271ab9c2014-11-27 15:23:57 +00001989 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
1990 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00001991
Roland Levillain271ab9c2014-11-27 15:23:57 +00001992 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00001993 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
1994
1995 SlowPathCodeX86_64* slow_path =
1996 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
1997 out_reg.AsRegister(), type, is_div);
1998 codegen_->AddSlowPath(slow_path);
1999
2000 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
2001 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
2002 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002003 if (type == Primitive::kPrimInt) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002004 __ cmpl(second_reg, Immediate(-1));
2005 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002006 // edx:eax <- sign-extended of eax
2007 __ cdq();
2008 // eax = quotient, edx = remainder
2009 __ idivl(second_reg);
2010 } else {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002011 __ cmpq(second_reg, Immediate(-1));
2012 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002013 // rdx:rax <- sign-extended of rax
2014 __ cqo();
2015 // rax = quotient, rdx = remainder
2016 __ idivq(second_reg);
2017 }
2018
2019 __ Bind(slow_path->GetExitLabel());
2020}
2021
Calin Juravle7c4954d2014-10-28 16:57:40 +00002022void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
2023 LocationSummary* locations =
2024 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2025 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002026 case Primitive::kPrimInt:
2027 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00002028 locations->SetInAt(0, Location::RegisterLocation(RAX));
2029 locations->SetInAt(1, Location::RequiresRegister());
2030 locations->SetOut(Location::SameAsFirstInput());
2031 // Intel uses edx:eax as the dividend.
2032 locations->AddTemp(Location::RegisterLocation(RDX));
2033 break;
2034 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002035
Calin Juravle7c4954d2014-10-28 16:57:40 +00002036 case Primitive::kPrimFloat:
2037 case Primitive::kPrimDouble: {
2038 locations->SetInAt(0, Location::RequiresFpuRegister());
2039 locations->SetInAt(1, Location::RequiresFpuRegister());
2040 locations->SetOut(Location::SameAsFirstInput());
2041 break;
2042 }
2043
2044 default:
2045 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2046 }
2047}
2048
2049void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
2050 LocationSummary* locations = div->GetLocations();
2051 Location first = locations->InAt(0);
2052 Location second = locations->InAt(1);
2053 DCHECK(first.Equals(locations->Out()));
2054
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002055 Primitive::Type type = div->GetResultType();
2056 switch (type) {
2057 case Primitive::kPrimInt:
2058 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002059 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00002060 break;
2061 }
2062
Calin Juravle7c4954d2014-10-28 16:57:40 +00002063 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002064 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002065 break;
2066 }
2067
2068 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002069 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002070 break;
2071 }
2072
2073 default:
2074 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2075 }
2076}
2077
Calin Juravlebacfec32014-11-14 15:54:36 +00002078void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002079 Primitive::Type type = rem->GetResultType();
2080 LocationSummary::CallKind call_kind =
2081 (type == Primitive::kPrimInt) || (type == Primitive::kPrimLong)
2082 ? LocationSummary::kNoCall
2083 : LocationSummary::kCall;
2084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2085
2086 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002087 case Primitive::kPrimInt:
2088 case Primitive::kPrimLong: {
2089 locations->SetInAt(0, Location::RegisterLocation(RAX));
2090 locations->SetInAt(1, Location::RequiresRegister());
2091 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
2092 locations->SetOut(Location::RegisterLocation(RDX));
2093 break;
2094 }
2095
2096 case Primitive::kPrimFloat:
2097 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002098 InvokeRuntimeCallingConvention calling_convention;
2099 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2100 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2101 // The runtime helper puts the result in XMM0.
2102 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Calin Juravlebacfec32014-11-14 15:54:36 +00002103 break;
2104 }
2105
2106 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002107 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002108 }
2109}
2110
2111void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
2112 Primitive::Type type = rem->GetResultType();
2113 switch (type) {
2114 case Primitive::kPrimInt:
2115 case Primitive::kPrimLong: {
2116 GenerateDivRemIntegral(rem);
2117 break;
2118 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002119 case Primitive::kPrimFloat: {
2120 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pFmodf), true));
2121 codegen_->RecordPcInfo(rem, rem->GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00002122 break;
2123 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002124 case Primitive::kPrimDouble: {
2125 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pFmod), true));
2126 codegen_->RecordPcInfo(rem, rem->GetDexPc());
2127 break;
2128 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002129 default:
2130 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2131 }
2132}
2133
Calin Juravled0d48522014-11-04 16:40:20 +00002134void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2135 LocationSummary* locations =
2136 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2137 locations->SetInAt(0, Location::Any());
2138 if (instruction->HasUses()) {
2139 locations->SetOut(Location::SameAsFirstInput());
2140 }
2141}
2142
2143void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2144 SlowPathCodeX86_64* slow_path =
2145 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2146 codegen_->AddSlowPath(slow_path);
2147
2148 LocationSummary* locations = instruction->GetLocations();
2149 Location value = locations->InAt(0);
2150
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002151 switch (instruction->GetType()) {
2152 case Primitive::kPrimInt: {
2153 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002154 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002155 __ j(kEqual, slow_path->GetEntryLabel());
2156 } else if (value.IsStackSlot()) {
2157 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2158 __ j(kEqual, slow_path->GetEntryLabel());
2159 } else {
2160 DCHECK(value.IsConstant()) << value;
2161 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2162 __ jmp(slow_path->GetEntryLabel());
2163 }
2164 }
2165 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002166 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002167 case Primitive::kPrimLong: {
2168 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002170 __ j(kEqual, slow_path->GetEntryLabel());
2171 } else if (value.IsDoubleStackSlot()) {
2172 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2173 __ j(kEqual, slow_path->GetEntryLabel());
2174 } else {
2175 DCHECK(value.IsConstant()) << value;
2176 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2177 __ jmp(slow_path->GetEntryLabel());
2178 }
2179 }
2180 break;
2181 }
2182 default:
2183 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002184 }
Calin Juravled0d48522014-11-04 16:40:20 +00002185}
2186
Calin Juravle9aec02f2014-11-18 23:06:35 +00002187void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2188 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2189
2190 LocationSummary* locations =
2191 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2192
2193 switch (op->GetResultType()) {
2194 case Primitive::kPrimInt:
2195 case Primitive::kPrimLong: {
2196 locations->SetInAt(0, Location::RequiresRegister());
2197 // The shift count needs to be in CL.
2198 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2199 locations->SetOut(Location::SameAsFirstInput());
2200 break;
2201 }
2202 default:
2203 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2204 }
2205}
2206
2207void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2208 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2209
2210 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002211 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002212 Location second = locations->InAt(1);
2213
2214 switch (op->GetResultType()) {
2215 case Primitive::kPrimInt: {
2216 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002217 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002218 if (op->IsShl()) {
2219 __ shll(first_reg, second_reg);
2220 } else if (op->IsShr()) {
2221 __ sarl(first_reg, second_reg);
2222 } else {
2223 __ shrl(first_reg, second_reg);
2224 }
2225 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002226 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002227 if (op->IsShl()) {
2228 __ shll(first_reg, imm);
2229 } else if (op->IsShr()) {
2230 __ sarl(first_reg, imm);
2231 } else {
2232 __ shrl(first_reg, imm);
2233 }
2234 }
2235 break;
2236 }
2237 case Primitive::kPrimLong: {
2238 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002239 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002240 if (op->IsShl()) {
2241 __ shlq(first_reg, second_reg);
2242 } else if (op->IsShr()) {
2243 __ sarq(first_reg, second_reg);
2244 } else {
2245 __ shrq(first_reg, second_reg);
2246 }
2247 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002248 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002249 if (op->IsShl()) {
2250 __ shlq(first_reg, imm);
2251 } else if (op->IsShr()) {
2252 __ sarq(first_reg, imm);
2253 } else {
2254 __ shrq(first_reg, imm);
2255 }
2256 }
2257 break;
2258 }
2259 default:
2260 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2261 }
2262}
2263
2264void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2265 HandleShift(shl);
2266}
2267
2268void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2269 HandleShift(shl);
2270}
2271
2272void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2273 HandleShift(shr);
2274}
2275
2276void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2277 HandleShift(shr);
2278}
2279
2280void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2281 HandleShift(ushr);
2282}
2283
2284void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2285 HandleShift(ushr);
2286}
2287
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002288void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002289 LocationSummary* locations =
2290 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002291 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002292 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2293 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2294 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002295}
2296
2297void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2298 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002299 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002300 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2301
2302 __ gs()->call(Address::Absolute(
2303 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
2304
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002305 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002306 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002307}
2308
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002309void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2310 LocationSummary* locations =
2311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2312 InvokeRuntimeCallingConvention calling_convention;
2313 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2314 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2315 locations->SetOut(Location::RegisterLocation(RAX));
2316 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2317}
2318
2319void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2320 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002321 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002322 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2323
2324 __ gs()->call(Address::Absolute(
2325 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
2326
2327 DCHECK(!codegen_->IsLeafMethod());
2328 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2329}
2330
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002331void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002332 LocationSummary* locations =
2333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002334 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2335 if (location.IsStackSlot()) {
2336 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2337 } else if (location.IsDoubleStackSlot()) {
2338 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2339 }
2340 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002341}
2342
2343void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2344 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002345 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002346}
2347
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002348void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002349 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002350 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002351 locations->SetInAt(0, Location::RequiresRegister());
2352 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002353}
2354
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002355void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2356 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002357 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2358 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002359 Location out = locations->Out();
2360 switch (not_->InputAt(0)->GetType()) {
2361 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002362 __ xorq(out.AsRegister<CpuRegister>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002363 break;
2364
2365 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002366 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002367 break;
2368
2369 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002370 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002371 break;
2372
2373 default:
2374 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2375 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002376}
2377
2378void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002379 LocationSummary* locations =
2380 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002381 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2382 locations->SetInAt(i, Location::Any());
2383 }
2384 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002385}
2386
2387void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002388 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002389 LOG(FATAL) << "Unimplemented";
2390}
2391
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002392void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002393 LocationSummary* locations =
2394 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002395 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002396 bool needs_write_barrier =
2397 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002398 locations->SetInAt(0, Location::RequiresRegister());
2399 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002400 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002401 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002402 locations->AddTemp(Location::RequiresRegister());
2403 locations->AddTemp(Location::RequiresRegister());
2404 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002405}
2406
2407void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2408 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002409 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002410 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002411 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002412
2413 switch (field_type) {
2414 case Primitive::kPrimBoolean:
2415 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002416 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002417 __ movb(Address(obj, offset), value);
2418 break;
2419 }
2420
2421 case Primitive::kPrimShort:
2422 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002423 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002424 __ movw(Address(obj, offset), value);
2425 break;
2426 }
2427
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002428 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002429 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002430 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002431 __ movl(Address(obj, offset), value);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002432 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue())) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002433 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2434 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002435 codegen_->MarkGCCard(temp, card, obj, value);
2436 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002437 break;
2438 }
2439
2440 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002442 __ movq(Address(obj, offset), value);
2443 break;
2444 }
2445
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002446 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002447 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002448 __ movss(Address(obj, offset), value);
2449 break;
2450 }
2451
2452 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002453 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002454 __ movsd(Address(obj, offset), value);
2455 break;
2456 }
2457
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002458 case Primitive::kPrimVoid:
2459 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002460 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002461 }
2462}
2463
2464void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002465 LocationSummary* locations =
2466 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002467 locations->SetInAt(0, Location::RequiresRegister());
2468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002469}
2470
2471void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2472 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002473 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002474 size_t offset = instruction->GetFieldOffset().SizeValue();
2475
2476 switch (instruction->GetType()) {
2477 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002478 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002479 __ movzxb(out, Address(obj, offset));
2480 break;
2481 }
2482
2483 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002484 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002485 __ movsxb(out, Address(obj, offset));
2486 break;
2487 }
2488
2489 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002490 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002491 __ movsxw(out, Address(obj, offset));
2492 break;
2493 }
2494
2495 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002497 __ movzxw(out, Address(obj, offset));
2498 break;
2499 }
2500
2501 case Primitive::kPrimInt:
2502 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002503 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002504 __ movl(out, Address(obj, offset));
2505 break;
2506 }
2507
2508 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002509 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002510 __ movq(out, Address(obj, offset));
2511 break;
2512 }
2513
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002514 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002516 __ movss(out, Address(obj, offset));
2517 break;
2518 }
2519
2520 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002522 __ movsd(out, Address(obj, offset));
2523 break;
2524 }
2525
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002526 case Primitive::kPrimVoid:
2527 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002528 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002529 }
2530}
2531
2532void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002533 LocationSummary* locations =
2534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002535 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002536 if (instruction->HasUses()) {
2537 locations->SetOut(Location::SameAsFirstInput());
2538 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002539}
2540
2541void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002542 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002543 codegen_->AddSlowPath(slow_path);
2544
2545 LocationSummary* locations = instruction->GetLocations();
2546 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002547
2548 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002549 __ cmpl(obj.AsRegister<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002550 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002551 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002552 } else {
2553 DCHECK(obj.IsConstant()) << obj;
2554 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2555 __ jmp(slow_path->GetEntryLabel());
2556 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002557 }
2558 __ j(kEqual, slow_path->GetEntryLabel());
2559}
2560
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002561void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002562 LocationSummary* locations =
2563 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002564 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002565 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002566 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2567 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002568}
2569
2570void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2571 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002572 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002573 Location index = locations->InAt(1);
2574
2575 switch (instruction->GetType()) {
2576 case Primitive::kPrimBoolean: {
2577 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002578 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002579 if (index.IsConstant()) {
2580 __ movzxb(out, Address(obj,
2581 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2582 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002583 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002584 }
2585 break;
2586 }
2587
2588 case Primitive::kPrimByte: {
2589 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002590 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002591 if (index.IsConstant()) {
2592 __ movsxb(out, Address(obj,
2593 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2594 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002595 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002596 }
2597 break;
2598 }
2599
2600 case Primitive::kPrimShort: {
2601 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002602 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002603 if (index.IsConstant()) {
2604 __ movsxw(out, Address(obj,
2605 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2606 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002607 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002608 }
2609 break;
2610 }
2611
2612 case Primitive::kPrimChar: {
2613 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002614 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002615 if (index.IsConstant()) {
2616 __ movzxw(out, Address(obj,
2617 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2618 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002619 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002620 }
2621 break;
2622 }
2623
2624 case Primitive::kPrimInt:
2625 case Primitive::kPrimNot: {
2626 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2627 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002628 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002629 if (index.IsConstant()) {
2630 __ movl(out, Address(obj,
2631 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2632 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002633 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002634 }
2635 break;
2636 }
2637
2638 case Primitive::kPrimLong: {
2639 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002641 if (index.IsConstant()) {
2642 __ movq(out, Address(obj,
2643 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2644 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002645 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002646 }
2647 break;
2648 }
2649
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002650 case Primitive::kPrimFloat: {
2651 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002652 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002653 if (index.IsConstant()) {
2654 __ movss(out, Address(obj,
2655 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2656 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002657 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002658 }
2659 break;
2660 }
2661
2662 case Primitive::kPrimDouble: {
2663 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002665 if (index.IsConstant()) {
2666 __ movsd(out, Address(obj,
2667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2668 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002669 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002670 }
2671 break;
2672 }
2673
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002674 case Primitive::kPrimVoid:
2675 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002676 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002677 }
2678}
2679
2680void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002681 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002682
2683 bool needs_write_barrier =
2684 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2685 bool needs_runtime_call = instruction->NeedsTypeCheck();
2686
Nicolas Geoffray39468442014-09-02 15:17:15 +01002687 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002688 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2689 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002690 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002691 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2692 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2693 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002694 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002695 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002696 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002697 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2698 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002699 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002700 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002701 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2702 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002703 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002704 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002705 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002706
2707 if (needs_write_barrier) {
2708 // Temporary registers for the write barrier.
2709 locations->AddTemp(Location::RequiresRegister());
2710 locations->AddTemp(Location::RequiresRegister());
2711 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002712 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002713}
2714
2715void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2716 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002718 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002719 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002720 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002721 bool needs_runtime_call = locations->WillCall();
2722 bool needs_write_barrier =
2723 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002724
2725 switch (value_type) {
2726 case Primitive::kPrimBoolean:
2727 case Primitive::kPrimByte: {
2728 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002729 if (index.IsConstant()) {
2730 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002731 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002732 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002733 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002734 __ movb(Address(obj, offset),
2735 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002736 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002737 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002738 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002739 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2740 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002741 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002742 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002743 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2744 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002745 }
2746 break;
2747 }
2748
2749 case Primitive::kPrimShort:
2750 case Primitive::kPrimChar: {
2751 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002752 if (index.IsConstant()) {
2753 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002754 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002755 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002756 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002757 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00002758 __ movw(Address(obj, offset),
2759 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002760 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002761 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002762 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002763 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002764 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
2765 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002766 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002767 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002768 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002769 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2770 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002771 }
2772 break;
2773 }
2774
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002775 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002776 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002777 if (!needs_runtime_call) {
2778 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2779 if (index.IsConstant()) {
2780 size_t offset =
2781 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2782 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002783 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002784 } else {
2785 DCHECK(value.IsConstant()) << value;
2786 __ movl(Address(obj, offset),
2787 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2788 }
2789 } else {
2790 DCHECK(index.IsRegister()) << index;
2791 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002792 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
2793 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002794 } else {
2795 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002796 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002797 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2798 }
2799 }
2800
2801 if (needs_write_barrier) {
2802 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002803 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2804 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2805 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002806 }
2807 } else {
2808 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00002809 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
2810 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002811 DCHECK(!codegen_->IsLeafMethod());
2812 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2813 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002814 break;
2815 }
2816
2817 case Primitive::kPrimLong: {
2818 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002819 if (index.IsConstant()) {
2820 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002821 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002822 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002823 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002824 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
2826 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002827 }
2828 break;
2829 }
2830
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002831 case Primitive::kPrimFloat: {
2832 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2833 if (index.IsConstant()) {
2834 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2835 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002836 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002837 } else {
2838 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002839 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
2840 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002841 }
2842 break;
2843 }
2844
2845 case Primitive::kPrimDouble: {
2846 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2847 if (index.IsConstant()) {
2848 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2849 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002850 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002851 } else {
2852 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002853 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
2854 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002855 }
2856 break;
2857 }
2858
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002859 case Primitive::kPrimVoid:
2860 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002861 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002862 }
2863}
2864
2865void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002866 LocationSummary* locations =
2867 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002868 locations->SetInAt(0, Location::RequiresRegister());
2869 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002870}
2871
2872void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2873 LocationSummary* locations = instruction->GetLocations();
2874 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002875 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2876 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002877 __ movl(out, Address(obj, offset));
2878}
2879
2880void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002881 LocationSummary* locations =
2882 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002883 locations->SetInAt(0, Location::RequiresRegister());
2884 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002885 if (instruction->HasUses()) {
2886 locations->SetOut(Location::SameAsFirstInput());
2887 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002888}
2889
2890void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2891 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002892 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002893 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002894 codegen_->AddSlowPath(slow_path);
2895
Roland Levillain271ab9c2014-11-27 15:23:57 +00002896 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
2897 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002898
2899 __ cmpl(index, length);
2900 __ j(kAboveEqual, slow_path->GetEntryLabel());
2901}
2902
2903void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2904 CpuRegister card,
2905 CpuRegister object,
2906 CpuRegister value) {
2907 Label is_null;
2908 __ testl(value, value);
2909 __ j(kEqual, &is_null);
2910 __ gs()->movq(card, Address::Absolute(
2911 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2912 __ movq(temp, object);
2913 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2914 __ movb(Address(temp, card, TIMES_1, 0), card);
2915 __ Bind(&is_null);
2916}
2917
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002918void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2919 temp->SetLocations(nullptr);
2920}
2921
2922void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2923 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002924 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002925}
2926
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002927void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002928 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002929 LOG(FATAL) << "Unimplemented";
2930}
2931
2932void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002933 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2934}
2935
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002936void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2937 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2938}
2939
2940void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002941 HBasicBlock* block = instruction->GetBlock();
2942 if (block->GetLoopInformation() != nullptr) {
2943 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2944 // The back edge will generate the suspend check.
2945 return;
2946 }
2947 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2948 // The goto will generate the suspend check.
2949 return;
2950 }
2951 GenerateSuspendCheck(instruction, nullptr);
2952}
2953
2954void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2955 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002956 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002957 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002958 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002959 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002960 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002961 if (successor == nullptr) {
2962 __ j(kNotEqual, slow_path->GetEntryLabel());
2963 __ Bind(slow_path->GetReturnLabel());
2964 } else {
2965 __ j(kEqual, codegen_->GetLabelOf(successor));
2966 __ jmp(slow_path->GetEntryLabel());
2967 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002968}
2969
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002970X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2971 return codegen_->GetAssembler();
2972}
2973
2974void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2975 MoveOperands* move = moves_.Get(index);
2976 Location source = move->GetSource();
2977 Location destination = move->GetDestination();
2978
2979 if (source.IsRegister()) {
2980 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002982 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002983 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002984 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002985 } else {
2986 DCHECK(destination.IsDoubleStackSlot());
2987 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002988 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002989 }
2990 } else if (source.IsStackSlot()) {
2991 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002992 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002993 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002994 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002995 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002996 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002997 } else {
2998 DCHECK(destination.IsStackSlot());
2999 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3000 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3001 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003002 } else if (source.IsDoubleStackSlot()) {
3003 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003004 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003005 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003006 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003007 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3008 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003009 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003010 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003011 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3012 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3013 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003014 } else if (source.IsConstant()) {
3015 HConstant* constant = source.GetConstant();
3016 if (constant->IsIntConstant()) {
3017 Immediate imm(constant->AsIntConstant()->GetValue());
3018 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003019 __ movl(destination.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003020 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003021 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003022 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3023 }
3024 } else if (constant->IsLongConstant()) {
3025 int64_t value = constant->AsLongConstant()->GetValue();
3026 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003027 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003028 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003029 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003030 __ movq(CpuRegister(TMP), Immediate(value));
3031 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3032 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003033 } else if (constant->IsFloatConstant()) {
3034 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3035 if (destination.IsFpuRegister()) {
3036 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003038 } else {
3039 DCHECK(destination.IsStackSlot()) << destination;
3040 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3041 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003042 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003043 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3044 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3045 if (destination.IsFpuRegister()) {
3046 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003047 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003048 } else {
3049 DCHECK(destination.IsDoubleStackSlot()) << destination;
3050 __ movq(CpuRegister(TMP), imm);
3051 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3052 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003053 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003054 } else if (source.IsFpuRegister()) {
3055 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003057 } else if (destination.IsStackSlot()) {
3058 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003059 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003060 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003061 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003062 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003064 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003065 }
3066}
3067
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003068void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003069 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003070 __ movl(Address(CpuRegister(RSP), mem), reg);
3071 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003072}
3073
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003074void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003075 ScratchRegisterScope ensure_scratch(
3076 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3077
3078 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3079 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3080 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3081 Address(CpuRegister(RSP), mem2 + stack_offset));
3082 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3083 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3084 CpuRegister(ensure_scratch.GetRegister()));
3085}
3086
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003087void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3088 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3089 __ movq(Address(CpuRegister(RSP), mem), reg);
3090 __ movq(reg, CpuRegister(TMP));
3091}
3092
3093void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3094 ScratchRegisterScope ensure_scratch(
3095 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3096
3097 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3098 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3099 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3100 Address(CpuRegister(RSP), mem2 + stack_offset));
3101 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3102 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3103 CpuRegister(ensure_scratch.GetRegister()));
3104}
3105
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003106void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3107 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3108 __ movss(Address(CpuRegister(RSP), mem), reg);
3109 __ movd(reg, CpuRegister(TMP));
3110}
3111
3112void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3113 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3114 __ movsd(Address(CpuRegister(RSP), mem), reg);
3115 __ movd(reg, CpuRegister(TMP));
3116}
3117
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003118void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3119 MoveOperands* move = moves_.Get(index);
3120 Location source = move->GetSource();
3121 Location destination = move->GetDestination();
3122
3123 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003124 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003125 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003126 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003127 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003129 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003130 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3131 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003132 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003133 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003134 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003135 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3136 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003137 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003138 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3139 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3140 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003141 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003142 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003143 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003144 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003145 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003146 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003147 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003148 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003149 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003150 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003151 }
3152}
3153
3154
3155void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3156 __ pushq(CpuRegister(reg));
3157}
3158
3159
3160void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3161 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003162}
3163
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003164void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3165 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3166 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3167 Immediate(mirror::Class::kStatusInitialized));
3168 __ j(kLess, slow_path->GetEntryLabel());
3169 __ Bind(slow_path->GetExitLabel());
3170 // No need for memory fence, thanks to the X86_64 memory model.
3171}
3172
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003173void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003174 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3175 ? LocationSummary::kCallOnSlowPath
3176 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003177 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003178 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003179 locations->SetOut(Location::RequiresRegister());
3180}
3181
3182void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003183 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003184 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003185 DCHECK(!cls->CanCallRuntime());
3186 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003187 codegen_->LoadCurrentMethod(out);
3188 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3189 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003190 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003191 codegen_->LoadCurrentMethod(out);
3192 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3193 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003194 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3195 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3196 codegen_->AddSlowPath(slow_path);
3197 __ testl(out, out);
3198 __ j(kEqual, slow_path->GetEntryLabel());
3199 if (cls->MustGenerateClinitCheck()) {
3200 GenerateClassInitializationCheck(slow_path, out);
3201 } else {
3202 __ Bind(slow_path->GetExitLabel());
3203 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003204 }
3205}
3206
3207void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3208 LocationSummary* locations =
3209 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3210 locations->SetInAt(0, Location::RequiresRegister());
3211 if (check->HasUses()) {
3212 locations->SetOut(Location::SameAsFirstInput());
3213 }
3214}
3215
3216void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003217 // We assume the class to not be null.
3218 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3219 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003220 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003221 GenerateClassInitializationCheck(slow_path,
3222 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003223}
3224
3225void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3226 LocationSummary* locations =
3227 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3228 locations->SetInAt(0, Location::RequiresRegister());
3229 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3230}
3231
3232void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3233 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003234 CpuRegister cls = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003235 size_t offset = instruction->GetFieldOffset().SizeValue();
3236
3237 switch (instruction->GetType()) {
3238 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003239 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003240 __ movzxb(out, Address(cls, offset));
3241 break;
3242 }
3243
3244 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003246 __ movsxb(out, Address(cls, offset));
3247 break;
3248 }
3249
3250 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003251 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003252 __ movsxw(out, Address(cls, offset));
3253 break;
3254 }
3255
3256 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003257 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003258 __ movzxw(out, Address(cls, offset));
3259 break;
3260 }
3261
3262 case Primitive::kPrimInt:
3263 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003264 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003265 __ movl(out, Address(cls, offset));
3266 break;
3267 }
3268
3269 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003270 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003271 __ movq(out, Address(cls, offset));
3272 break;
3273 }
3274
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003275 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003276 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003277 __ movss(out, Address(cls, offset));
3278 break;
3279 }
3280
3281 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003282 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003283 __ movsd(out, Address(cls, offset));
3284 break;
3285 }
3286
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003287 case Primitive::kPrimVoid:
3288 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3289 UNREACHABLE();
3290 }
3291}
3292
3293void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3294 LocationSummary* locations =
3295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3296 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003297 bool needs_write_barrier =
3298 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003299 locations->SetInAt(0, Location::RequiresRegister());
3300 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003301 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003302 // Temporary registers for the write barrier.
3303 locations->AddTemp(Location::RequiresRegister());
3304 locations->AddTemp(Location::RequiresRegister());
3305 }
3306}
3307
3308void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3309 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003310 CpuRegister cls = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003311 size_t offset = instruction->GetFieldOffset().SizeValue();
3312 Primitive::Type field_type = instruction->GetFieldType();
3313
3314 switch (field_type) {
3315 case Primitive::kPrimBoolean:
3316 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003317 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003318 __ movb(Address(cls, offset), value);
3319 break;
3320 }
3321
3322 case Primitive::kPrimShort:
3323 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003324 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003325 __ movw(Address(cls, offset), value);
3326 break;
3327 }
3328
3329 case Primitive::kPrimInt:
3330 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003331 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003332 __ movl(Address(cls, offset), value);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003333 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue())) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003334 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3335 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003336 codegen_->MarkGCCard(temp, card, cls, value);
3337 }
3338 break;
3339 }
3340
3341 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003342 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003343 __ movq(Address(cls, offset), value);
3344 break;
3345 }
3346
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003347 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003348 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003349 __ movss(Address(cls, offset), value);
3350 break;
3351 }
3352
3353 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003354 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003355 __ movsd(Address(cls, offset), value);
3356 break;
3357 }
3358
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003359 case Primitive::kPrimVoid:
3360 LOG(FATAL) << "Unreachable type " << field_type;
3361 UNREACHABLE();
3362 }
3363}
3364
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003365void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3366 LocationSummary* locations =
3367 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3368 locations->SetOut(Location::RequiresRegister());
3369}
3370
3371void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3372 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3373 codegen_->AddSlowPath(slow_path);
3374
Roland Levillain271ab9c2014-11-27 15:23:57 +00003375 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003376 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003377 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3378 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003379 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3380 __ testl(out, out);
3381 __ j(kEqual, slow_path->GetEntryLabel());
3382 __ Bind(slow_path->GetExitLabel());
3383}
3384
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003385void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3386 LocationSummary* locations =
3387 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3388 locations->SetOut(Location::RequiresRegister());
3389}
3390
3391void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3392 Address address = Address::Absolute(
3393 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003394 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003395 __ gs()->movl(address, Immediate(0));
3396}
3397
3398void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3399 LocationSummary* locations =
3400 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3401 InvokeRuntimeCallingConvention calling_convention;
3402 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3403}
3404
3405void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3406 __ gs()->call(
3407 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3408 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3409}
3410
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003411void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003412 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3413 ? LocationSummary::kNoCall
3414 : LocationSummary::kCallOnSlowPath;
3415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3416 locations->SetInAt(0, Location::RequiresRegister());
3417 locations->SetInAt(1, Location::Any());
3418 locations->SetOut(Location::RequiresRegister());
3419}
3420
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003421void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003422 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003423 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003424 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003425 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003426 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3427 Label done, zero;
3428 SlowPathCodeX86_64* slow_path = nullptr;
3429
3430 // Return 0 if `obj` is null.
3431 // TODO: avoid this check if we know obj is not null.
3432 __ testl(obj, obj);
3433 __ j(kEqual, &zero);
3434 // Compare the class of `obj` with `cls`.
3435 __ movl(out, Address(obj, class_offset));
3436 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003437 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003438 } else {
3439 DCHECK(cls.IsStackSlot()) << cls;
3440 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3441 }
3442 if (instruction->IsClassFinal()) {
3443 // Classes must be equal for the instanceof to succeed.
3444 __ j(kNotEqual, &zero);
3445 __ movl(out, Immediate(1));
3446 __ jmp(&done);
3447 } else {
3448 // If the classes are not equal, we go into a slow path.
3449 DCHECK(locations->OnlyCallsOnSlowPath());
3450 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003451 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003452 codegen_->AddSlowPath(slow_path);
3453 __ j(kNotEqual, slow_path->GetEntryLabel());
3454 __ movl(out, Immediate(1));
3455 __ jmp(&done);
3456 }
3457 __ Bind(&zero);
3458 __ movl(out, Immediate(0));
3459 if (slow_path != nullptr) {
3460 __ Bind(slow_path->GetExitLabel());
3461 }
3462 __ Bind(&done);
3463}
3464
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003465void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3466 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3467 instruction, LocationSummary::kCallOnSlowPath);
3468 locations->SetInAt(0, Location::RequiresRegister());
3469 locations->SetInAt(1, Location::Any());
3470 locations->AddTemp(Location::RequiresRegister());
3471}
3472
3473void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3474 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003475 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003476 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003477 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003478 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3479 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3480 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3481 codegen_->AddSlowPath(slow_path);
3482
3483 // TODO: avoid this check if we know obj is not null.
3484 __ testl(obj, obj);
3485 __ j(kEqual, slow_path->GetExitLabel());
3486 // Compare the class of `obj` with `cls`.
3487 __ movl(temp, Address(obj, class_offset));
3488 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003489 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003490 } else {
3491 DCHECK(cls.IsStackSlot()) << cls;
3492 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3493 }
3494 // Classes must be equal for the checkcast to succeed.
3495 __ j(kNotEqual, slow_path->GetEntryLabel());
3496 __ Bind(slow_path->GetExitLabel());
3497}
3498
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003499void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3500 LocationSummary* locations =
3501 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3502 InvokeRuntimeCallingConvention calling_convention;
3503 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3504}
3505
3506void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3507 __ gs()->call(Address::Absolute(instruction->IsEnter()
3508 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3509 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3510 true));
3511 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3512}
3513
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003514void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3515void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3516void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3517
3518void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3519 LocationSummary* locations =
3520 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3521 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3522 || instruction->GetResultType() == Primitive::kPrimLong);
3523 locations->SetInAt(0, Location::RequiresRegister());
3524 if (instruction->GetType() == Primitive::kPrimInt) {
3525 locations->SetInAt(1, Location::Any());
3526 } else {
3527 // Request a register to avoid loading a 64bits constant.
3528 locations->SetInAt(1, Location::RequiresRegister());
3529 }
3530 locations->SetOut(Location::SameAsFirstInput());
3531}
3532
3533void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3534 HandleBitwiseOperation(instruction);
3535}
3536
3537void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3538 HandleBitwiseOperation(instruction);
3539}
3540
3541void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3542 HandleBitwiseOperation(instruction);
3543}
3544
3545void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3546 LocationSummary* locations = instruction->GetLocations();
3547 Location first = locations->InAt(0);
3548 Location second = locations->InAt(1);
3549 DCHECK(first.Equals(locations->Out()));
3550
3551 if (instruction->GetResultType() == Primitive::kPrimInt) {
3552 if (second.IsRegister()) {
3553 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003554 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003555 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003556 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003557 } else {
3558 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003559 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003560 }
3561 } else if (second.IsConstant()) {
3562 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3563 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003564 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003565 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003567 } else {
3568 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003569 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003570 }
3571 } else {
3572 Address address(CpuRegister(RSP), second.GetStackIndex());
3573 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003574 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003575 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003576 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003577 } else {
3578 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003579 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003580 }
3581 }
3582 } else {
3583 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3584 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003585 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003586 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003587 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003588 } else {
3589 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003590 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003591 }
3592 }
3593}
3594
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003595} // namespace x86_64
3596} // namespace art