blob: 5fc24f71e698b5712e3da6a67785e820179d0ac9 [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;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800288 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
289 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000290 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 Geoffray5b4b8982014-12-18 17:45:56 +0000456 // TODO: We currently don't use Quick's callee saved registers.
457 blocked_core_registers_[RBX] = true;
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458 blocked_core_registers_[RBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000459 blocked_core_registers_[R12] = true;
460 blocked_core_registers_[R13] = true;
461 blocked_core_registers_[R14] = true;
462 blocked_core_registers_[R15] = true;
463
464 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 Geoffray4e44c822014-12-17 12:25:12 +00001140 // temp = temp->dex_cache_resolved_methods_;
1141 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1142 // temp = temp[index_in_cache]
1143 __ 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)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002314 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002315 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002316 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002317}
2318
2319void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2320 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002321 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
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
Calin Juravle52c48962014-12-16 17:02:57 +00002392void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
2393 /*
2394 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2395 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2396 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2397 */
2398 switch (kind) {
2399 case MemBarrierKind::kAnyAny: {
2400 __ mfence();
2401 break;
2402 }
2403 case MemBarrierKind::kAnyStore:
2404 case MemBarrierKind::kLoadAny:
2405 case MemBarrierKind::kStoreStore: {
2406 // nop
2407 break;
2408 }
2409 default:
2410 LOG(FATAL) << "Unexpected memory barier " << kind;
2411 }
2412}
2413
2414void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
2415 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2416
Nicolas Geoffray39468442014-09-02 15:17:15 +01002417 LocationSummary* locations =
2418 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00002419 locations->SetInAt(0, Location::RequiresRegister());
2420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2421}
2422
2423void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
2424 const FieldInfo& field_info) {
2425 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2426
2427 LocationSummary* locations = instruction->GetLocations();
2428 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2429 Location out = locations->Out();
2430 bool is_volatile = field_info.IsVolatile();
2431 Primitive::Type field_type = field_info.GetFieldType();
2432 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2433
2434 switch (field_type) {
2435 case Primitive::kPrimBoolean: {
2436 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2437 break;
2438 }
2439
2440 case Primitive::kPrimByte: {
2441 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
2442 break;
2443 }
2444
2445 case Primitive::kPrimShort: {
2446 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2447 break;
2448 }
2449
2450 case Primitive::kPrimChar: {
2451 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
2452 break;
2453 }
2454
2455 case Primitive::kPrimInt:
2456 case Primitive::kPrimNot: {
2457 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
2458 break;
2459 }
2460
2461 case Primitive::kPrimLong: {
2462 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
2463 break;
2464 }
2465
2466 case Primitive::kPrimFloat: {
2467 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2468 break;
2469 }
2470
2471 case Primitive::kPrimDouble: {
2472 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
2473 break;
2474 }
2475
2476 case Primitive::kPrimVoid:
2477 LOG(FATAL) << "Unreachable type " << field_type;
2478 UNREACHABLE();
2479 }
2480
2481 if (is_volatile) {
2482 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2483 }
2484}
2485
2486void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
2487 const FieldInfo& field_info) {
2488 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2489
2490 LocationSummary* locations =
2491 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002492 bool needs_write_barrier =
Calin Juravle52c48962014-12-16 17:02:57 +00002493 CodeGenerator::StoreNeedsWriteBarrier(field_info.GetFieldType(), instruction->InputAt(1));
2494
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002495 locations->SetInAt(0, Location::RequiresRegister());
2496 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002497 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002498 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002499 locations->AddTemp(Location::RequiresRegister());
2500 locations->AddTemp(Location::RequiresRegister());
2501 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002502}
2503
Calin Juravle52c48962014-12-16 17:02:57 +00002504void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
2505 const FieldInfo& field_info) {
2506 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2507
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002508 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00002509 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
2510 Location value = locations->InAt(1);
2511 bool is_volatile = field_info.IsVolatile();
2512 Primitive::Type field_type = field_info.GetFieldType();
2513 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2514
2515 if (is_volatile) {
2516 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2517 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002518
2519 switch (field_type) {
2520 case Primitive::kPrimBoolean:
2521 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002522 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002523 break;
2524 }
2525
2526 case Primitive::kPrimShort:
2527 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002528 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002529 break;
2530 }
2531
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002532 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002533 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002534 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
2535 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002536 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2537 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00002538 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002539 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002540 break;
2541 }
2542
2543 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002544 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002545 break;
2546 }
2547
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002548 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002549 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002550 break;
2551 }
2552
2553 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002554 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002555 break;
2556 }
2557
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002558 case Primitive::kPrimVoid:
2559 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002560 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002561 }
Calin Juravle52c48962014-12-16 17:02:57 +00002562
2563 if (is_volatile) {
2564 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2565 }
2566}
2567
2568void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2569 HandleFieldSet(instruction, instruction->GetFieldInfo());
2570}
2571
2572void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2573 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002574}
2575
2576void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002577 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002578}
2579
2580void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00002581 HandleFieldGet(instruction, instruction->GetFieldInfo());
2582}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002583
Calin Juravle52c48962014-12-16 17:02:57 +00002584void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2585 HandleFieldGet(instruction);
2586}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002587
Calin Juravle52c48962014-12-16 17:02:57 +00002588void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2589 HandleFieldGet(instruction, instruction->GetFieldInfo());
2590}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002591
Calin Juravle52c48962014-12-16 17:02:57 +00002592void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2593 HandleFieldSet(instruction, instruction->GetFieldInfo());
2594}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002595
Calin Juravle52c48962014-12-16 17:02:57 +00002596void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2597 HandleFieldSet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002598}
2599
2600void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002601 LocationSummary* locations =
2602 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002603 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002604 if (instruction->HasUses()) {
2605 locations->SetOut(Location::SameAsFirstInput());
2606 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002607}
2608
2609void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002610 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002611 codegen_->AddSlowPath(slow_path);
2612
2613 LocationSummary* locations = instruction->GetLocations();
2614 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002615
2616 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002617 __ cmpl(obj.AsRegister<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002618 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002619 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002620 } else {
2621 DCHECK(obj.IsConstant()) << obj;
2622 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2623 __ jmp(slow_path->GetEntryLabel());
2624 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002625 }
2626 __ j(kEqual, slow_path->GetEntryLabel());
2627}
2628
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002629void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002630 LocationSummary* locations =
2631 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002632 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002633 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002634 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2635 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002636}
2637
2638void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2639 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002641 Location index = locations->InAt(1);
2642
2643 switch (instruction->GetType()) {
2644 case Primitive::kPrimBoolean: {
2645 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002646 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002647 if (index.IsConstant()) {
2648 __ movzxb(out, Address(obj,
2649 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2650 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002651 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002652 }
2653 break;
2654 }
2655
2656 case Primitive::kPrimByte: {
2657 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002658 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002659 if (index.IsConstant()) {
2660 __ movsxb(out, Address(obj,
2661 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2662 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002663 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002664 }
2665 break;
2666 }
2667
2668 case Primitive::kPrimShort: {
2669 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002671 if (index.IsConstant()) {
2672 __ movsxw(out, Address(obj,
2673 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2674 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002675 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002676 }
2677 break;
2678 }
2679
2680 case Primitive::kPrimChar: {
2681 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002682 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002683 if (index.IsConstant()) {
2684 __ movzxw(out, Address(obj,
2685 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2686 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002688 }
2689 break;
2690 }
2691
2692 case Primitive::kPrimInt:
2693 case Primitive::kPrimNot: {
2694 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2695 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002696 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002697 if (index.IsConstant()) {
2698 __ movl(out, Address(obj,
2699 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2700 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002701 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002702 }
2703 break;
2704 }
2705
2706 case Primitive::kPrimLong: {
2707 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002709 if (index.IsConstant()) {
2710 __ movq(out, Address(obj,
2711 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2712 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002713 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002714 }
2715 break;
2716 }
2717
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002718 case Primitive::kPrimFloat: {
2719 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002720 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002721 if (index.IsConstant()) {
2722 __ movss(out, Address(obj,
2723 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2724 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002725 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002726 }
2727 break;
2728 }
2729
2730 case Primitive::kPrimDouble: {
2731 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002732 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002733 if (index.IsConstant()) {
2734 __ movsd(out, Address(obj,
2735 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2736 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002737 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002738 }
2739 break;
2740 }
2741
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002742 case Primitive::kPrimVoid:
2743 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002744 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002745 }
2746}
2747
2748void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002749 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002750
2751 bool needs_write_barrier =
2752 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2753 bool needs_runtime_call = instruction->NeedsTypeCheck();
2754
Nicolas Geoffray39468442014-09-02 15:17:15 +01002755 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002756 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2757 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002758 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002759 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2760 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2761 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002762 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002763 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002764 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002765 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2766 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002767 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002768 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002769 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2770 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002771 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002772 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002773 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002774
2775 if (needs_write_barrier) {
2776 // Temporary registers for the write barrier.
2777 locations->AddTemp(Location::RequiresRegister());
2778 locations->AddTemp(Location::RequiresRegister());
2779 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002780 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002781}
2782
2783void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2784 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002785 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002786 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002787 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002788 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002789 bool needs_runtime_call = locations->WillCall();
2790 bool needs_write_barrier =
2791 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002792
2793 switch (value_type) {
2794 case Primitive::kPrimBoolean:
2795 case Primitive::kPrimByte: {
2796 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002797 if (index.IsConstant()) {
2798 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002799 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002800 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002801 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002802 __ movb(Address(obj, offset),
2803 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002804 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002805 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002806 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002807 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2808 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002809 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002810 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002811 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2812 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002813 }
2814 break;
2815 }
2816
2817 case Primitive::kPrimShort:
2818 case Primitive::kPrimChar: {
2819 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002820 if (index.IsConstant()) {
2821 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002822 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002823 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002824 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002825 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00002826 __ movw(Address(obj, offset),
2827 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002828 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002829 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002830 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002831 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002832 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
2833 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002834 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002835 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002836 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002837 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2838 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002839 }
2840 break;
2841 }
2842
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002843 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002844 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002845 if (!needs_runtime_call) {
2846 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2847 if (index.IsConstant()) {
2848 size_t offset =
2849 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2850 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002851 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002852 } else {
2853 DCHECK(value.IsConstant()) << value;
2854 __ movl(Address(obj, offset),
2855 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2856 }
2857 } else {
2858 DCHECK(index.IsRegister()) << index;
2859 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002860 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
2861 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002862 } else {
2863 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002864 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002865 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2866 }
2867 }
2868
2869 if (needs_write_barrier) {
2870 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002871 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2872 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2873 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002874 }
2875 } else {
2876 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00002877 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
2878 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002879 DCHECK(!codegen_->IsLeafMethod());
2880 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2881 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002882 break;
2883 }
2884
2885 case Primitive::kPrimLong: {
2886 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002887 if (index.IsConstant()) {
2888 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002889 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002890 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002891 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002892 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002893 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
2894 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002895 }
2896 break;
2897 }
2898
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002899 case Primitive::kPrimFloat: {
2900 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2901 if (index.IsConstant()) {
2902 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2903 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002904 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002905 } else {
2906 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002907 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
2908 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002909 }
2910 break;
2911 }
2912
2913 case Primitive::kPrimDouble: {
2914 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2915 if (index.IsConstant()) {
2916 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2917 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002918 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002919 } else {
2920 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002921 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
2922 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002923 }
2924 break;
2925 }
2926
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002927 case Primitive::kPrimVoid:
2928 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002929 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002930 }
2931}
2932
2933void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002934 LocationSummary* locations =
2935 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002936 locations->SetInAt(0, Location::RequiresRegister());
2937 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002938}
2939
2940void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2941 LocationSummary* locations = instruction->GetLocations();
2942 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002943 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2944 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002945 __ movl(out, Address(obj, offset));
2946}
2947
2948void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002949 LocationSummary* locations =
2950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002951 locations->SetInAt(0, Location::RequiresRegister());
2952 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002953 if (instruction->HasUses()) {
2954 locations->SetOut(Location::SameAsFirstInput());
2955 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002956}
2957
2958void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2959 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002960 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002961 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002962 codegen_->AddSlowPath(slow_path);
2963
Roland Levillain271ab9c2014-11-27 15:23:57 +00002964 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
2965 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002966
2967 __ cmpl(index, length);
2968 __ j(kAboveEqual, slow_path->GetEntryLabel());
2969}
2970
2971void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2972 CpuRegister card,
2973 CpuRegister object,
2974 CpuRegister value) {
2975 Label is_null;
2976 __ testl(value, value);
2977 __ j(kEqual, &is_null);
2978 __ gs()->movq(card, Address::Absolute(
2979 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2980 __ movq(temp, object);
2981 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2982 __ movb(Address(temp, card, TIMES_1, 0), card);
2983 __ Bind(&is_null);
2984}
2985
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002986void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2987 temp->SetLocations(nullptr);
2988}
2989
2990void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2991 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002992 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002993}
2994
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002995void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002996 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002997 LOG(FATAL) << "Unimplemented";
2998}
2999
3000void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003001 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3002}
3003
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003004void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
3005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3006}
3007
3008void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003009 HBasicBlock* block = instruction->GetBlock();
3010 if (block->GetLoopInformation() != nullptr) {
3011 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3012 // The back edge will generate the suspend check.
3013 return;
3014 }
3015 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3016 // The goto will generate the suspend check.
3017 return;
3018 }
3019 GenerateSuspendCheck(instruction, nullptr);
3020}
3021
3022void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
3023 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003024 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003025 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003026 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003027 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003028 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003029 if (successor == nullptr) {
3030 __ j(kNotEqual, slow_path->GetEntryLabel());
3031 __ Bind(slow_path->GetReturnLabel());
3032 } else {
3033 __ j(kEqual, codegen_->GetLabelOf(successor));
3034 __ jmp(slow_path->GetEntryLabel());
3035 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003036}
3037
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003038X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
3039 return codegen_->GetAssembler();
3040}
3041
3042void ParallelMoveResolverX86_64::EmitMove(size_t index) {
3043 MoveOperands* move = moves_.Get(index);
3044 Location source = move->GetSource();
3045 Location destination = move->GetDestination();
3046
3047 if (source.IsRegister()) {
3048 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003049 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003050 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003051 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003052 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003053 } else {
3054 DCHECK(destination.IsDoubleStackSlot());
3055 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003057 }
3058 } else if (source.IsStackSlot()) {
3059 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003060 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003061 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003062 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003064 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003065 } else {
3066 DCHECK(destination.IsStackSlot());
3067 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3068 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3069 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003070 } else if (source.IsDoubleStackSlot()) {
3071 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003073 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003074 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003075 __ movsd(destination.AsFpuRegister<XmmRegister>(),
3076 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003077 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01003078 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003079 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
3080 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3081 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003082 } else if (source.IsConstant()) {
3083 HConstant* constant = source.GetConstant();
3084 if (constant->IsIntConstant()) {
3085 Immediate imm(constant->AsIntConstant()->GetValue());
3086 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003087 __ movl(destination.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003088 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003089 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003090 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3091 }
3092 } else if (constant->IsLongConstant()) {
3093 int64_t value = constant->AsLongConstant()->GetValue();
3094 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003095 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003096 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003097 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003098 __ movq(CpuRegister(TMP), Immediate(value));
3099 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3100 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003101 } else if (constant->IsFloatConstant()) {
3102 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
3103 if (destination.IsFpuRegister()) {
3104 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003106 } else {
3107 DCHECK(destination.IsStackSlot()) << destination;
3108 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
3109 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003110 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003111 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
3112 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
3113 if (destination.IsFpuRegister()) {
3114 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003115 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003116 } else {
3117 DCHECK(destination.IsDoubleStackSlot()) << destination;
3118 __ movq(CpuRegister(TMP), imm);
3119 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
3120 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003121 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003122 } else if (source.IsFpuRegister()) {
3123 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003124 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003125 } else if (destination.IsStackSlot()) {
3126 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003127 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003128 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00003129 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003130 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003131 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003132 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003133 }
3134}
3135
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003136void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003137 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003138 __ movl(Address(CpuRegister(RSP), mem), reg);
3139 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003140}
3141
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003142void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003143 ScratchRegisterScope ensure_scratch(
3144 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3145
3146 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3147 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3148 __ movl(CpuRegister(ensure_scratch.GetRegister()),
3149 Address(CpuRegister(RSP), mem2 + stack_offset));
3150 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3151 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
3152 CpuRegister(ensure_scratch.GetRegister()));
3153}
3154
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003155void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
3156 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3157 __ movq(Address(CpuRegister(RSP), mem), reg);
3158 __ movq(reg, CpuRegister(TMP));
3159}
3160
3161void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
3162 ScratchRegisterScope ensure_scratch(
3163 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
3164
3165 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
3166 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
3167 __ movq(CpuRegister(ensure_scratch.GetRegister()),
3168 Address(CpuRegister(RSP), mem2 + stack_offset));
3169 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
3170 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
3171 CpuRegister(ensure_scratch.GetRegister()));
3172}
3173
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003174void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
3175 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3176 __ movss(Address(CpuRegister(RSP), mem), reg);
3177 __ movd(reg, CpuRegister(TMP));
3178}
3179
3180void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
3181 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
3182 __ movsd(Address(CpuRegister(RSP), mem), reg);
3183 __ movd(reg, CpuRegister(TMP));
3184}
3185
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003186void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
3187 MoveOperands* move = moves_.Get(index);
3188 Location source = move->GetSource();
3189 Location destination = move->GetDestination();
3190
3191 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003192 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003193 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003194 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003195 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003196 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003197 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003198 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3199 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003200 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003201 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003202 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003203 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3204 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003205 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003206 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3207 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3208 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003209 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003210 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003211 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003212 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003213 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003215 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003216 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003217 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003218 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003219 }
3220}
3221
3222
3223void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3224 __ pushq(CpuRegister(reg));
3225}
3226
3227
3228void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3229 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003230}
3231
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003232void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3233 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3234 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3235 Immediate(mirror::Class::kStatusInitialized));
3236 __ j(kLess, slow_path->GetEntryLabel());
3237 __ Bind(slow_path->GetExitLabel());
3238 // No need for memory fence, thanks to the X86_64 memory model.
3239}
3240
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003241void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003242 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3243 ? LocationSummary::kCallOnSlowPath
3244 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003245 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003246 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003247 locations->SetOut(Location::RequiresRegister());
3248}
3249
3250void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003251 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003252 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003253 DCHECK(!cls->CanCallRuntime());
3254 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003255 codegen_->LoadCurrentMethod(out);
3256 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3257 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003258 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003259 codegen_->LoadCurrentMethod(out);
3260 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3261 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003262 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3263 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3264 codegen_->AddSlowPath(slow_path);
3265 __ testl(out, out);
3266 __ j(kEqual, slow_path->GetEntryLabel());
3267 if (cls->MustGenerateClinitCheck()) {
3268 GenerateClassInitializationCheck(slow_path, out);
3269 } else {
3270 __ Bind(slow_path->GetExitLabel());
3271 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003272 }
3273}
3274
3275void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3276 LocationSummary* locations =
3277 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3278 locations->SetInAt(0, Location::RequiresRegister());
3279 if (check->HasUses()) {
3280 locations->SetOut(Location::SameAsFirstInput());
3281 }
3282}
3283
3284void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003285 // We assume the class to not be null.
3286 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3287 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003288 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003289 GenerateClassInitializationCheck(slow_path,
3290 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003291}
3292
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003293void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3294 LocationSummary* locations =
3295 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3296 locations->SetOut(Location::RequiresRegister());
3297}
3298
3299void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3300 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3301 codegen_->AddSlowPath(slow_path);
3302
Roland Levillain271ab9c2014-11-27 15:23:57 +00003303 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003304 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003305 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3306 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003307 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3308 __ testl(out, out);
3309 __ j(kEqual, slow_path->GetEntryLabel());
3310 __ Bind(slow_path->GetExitLabel());
3311}
3312
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003313void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3314 LocationSummary* locations =
3315 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3316 locations->SetOut(Location::RequiresRegister());
3317}
3318
3319void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3320 Address address = Address::Absolute(
3321 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003322 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003323 __ gs()->movl(address, Immediate(0));
3324}
3325
3326void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3327 LocationSummary* locations =
3328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3329 InvokeRuntimeCallingConvention calling_convention;
3330 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3331}
3332
3333void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3334 __ gs()->call(
3335 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3336 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3337}
3338
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003339void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003340 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3341 ? LocationSummary::kNoCall
3342 : LocationSummary::kCallOnSlowPath;
3343 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3344 locations->SetInAt(0, Location::RequiresRegister());
3345 locations->SetInAt(1, Location::Any());
3346 locations->SetOut(Location::RequiresRegister());
3347}
3348
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003349void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003350 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003351 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003352 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003353 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003354 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3355 Label done, zero;
3356 SlowPathCodeX86_64* slow_path = nullptr;
3357
3358 // Return 0 if `obj` is null.
3359 // TODO: avoid this check if we know obj is not null.
3360 __ testl(obj, obj);
3361 __ j(kEqual, &zero);
3362 // Compare the class of `obj` with `cls`.
3363 __ movl(out, Address(obj, class_offset));
3364 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003365 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003366 } else {
3367 DCHECK(cls.IsStackSlot()) << cls;
3368 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3369 }
3370 if (instruction->IsClassFinal()) {
3371 // Classes must be equal for the instanceof to succeed.
3372 __ j(kNotEqual, &zero);
3373 __ movl(out, Immediate(1));
3374 __ jmp(&done);
3375 } else {
3376 // If the classes are not equal, we go into a slow path.
3377 DCHECK(locations->OnlyCallsOnSlowPath());
3378 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003379 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003380 codegen_->AddSlowPath(slow_path);
3381 __ j(kNotEqual, slow_path->GetEntryLabel());
3382 __ movl(out, Immediate(1));
3383 __ jmp(&done);
3384 }
3385 __ Bind(&zero);
3386 __ movl(out, Immediate(0));
3387 if (slow_path != nullptr) {
3388 __ Bind(slow_path->GetExitLabel());
3389 }
3390 __ Bind(&done);
3391}
3392
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003393void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3394 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3395 instruction, LocationSummary::kCallOnSlowPath);
3396 locations->SetInAt(0, Location::RequiresRegister());
3397 locations->SetInAt(1, Location::Any());
3398 locations->AddTemp(Location::RequiresRegister());
3399}
3400
3401void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3402 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003403 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003404 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003405 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003406 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3407 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3408 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3409 codegen_->AddSlowPath(slow_path);
3410
3411 // TODO: avoid this check if we know obj is not null.
3412 __ testl(obj, obj);
3413 __ j(kEqual, slow_path->GetExitLabel());
3414 // Compare the class of `obj` with `cls`.
3415 __ movl(temp, Address(obj, class_offset));
3416 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003417 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003418 } else {
3419 DCHECK(cls.IsStackSlot()) << cls;
3420 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3421 }
3422 // Classes must be equal for the checkcast to succeed.
3423 __ j(kNotEqual, slow_path->GetEntryLabel());
3424 __ Bind(slow_path->GetExitLabel());
3425}
3426
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003427void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3428 LocationSummary* locations =
3429 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3430 InvokeRuntimeCallingConvention calling_convention;
3431 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3432}
3433
3434void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3435 __ gs()->call(Address::Absolute(instruction->IsEnter()
3436 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3437 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3438 true));
3439 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3440}
3441
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003442void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3443void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3444void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3445
3446void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3447 LocationSummary* locations =
3448 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3449 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3450 || instruction->GetResultType() == Primitive::kPrimLong);
3451 locations->SetInAt(0, Location::RequiresRegister());
3452 if (instruction->GetType() == Primitive::kPrimInt) {
3453 locations->SetInAt(1, Location::Any());
3454 } else {
3455 // Request a register to avoid loading a 64bits constant.
3456 locations->SetInAt(1, Location::RequiresRegister());
3457 }
3458 locations->SetOut(Location::SameAsFirstInput());
3459}
3460
3461void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3462 HandleBitwiseOperation(instruction);
3463}
3464
3465void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3466 HandleBitwiseOperation(instruction);
3467}
3468
3469void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3470 HandleBitwiseOperation(instruction);
3471}
3472
3473void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3474 LocationSummary* locations = instruction->GetLocations();
3475 Location first = locations->InAt(0);
3476 Location second = locations->InAt(1);
3477 DCHECK(first.Equals(locations->Out()));
3478
3479 if (instruction->GetResultType() == Primitive::kPrimInt) {
3480 if (second.IsRegister()) {
3481 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003482 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003483 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003484 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003485 } else {
3486 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003488 }
3489 } else if (second.IsConstant()) {
3490 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3491 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003492 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003493 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003494 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003495 } else {
3496 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003497 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003498 }
3499 } else {
3500 Address address(CpuRegister(RSP), second.GetStackIndex());
3501 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003502 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003503 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003504 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003505 } else {
3506 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003507 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003508 }
3509 }
3510 } else {
3511 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3512 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003513 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003514 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003515 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003516 } else {
3517 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003518 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003519 }
3520 }
3521}
3522
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003523} // namespace x86_64
3524} // namespace art