blob: 289ef641f07c551b9f9011a87399db6644c8279c [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "intrinsics.h"
25#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "mirror/object_reference.h"
29#include "thread.h"
30#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "utils/x86_64/assembler_x86_64.h"
33#include "utils/x86_64/managed_register_x86_64.h"
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace art {
36
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010037namespace x86_64 {
38
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010039// Some x86_64 instructions require a register to be available as temp.
40static constexpr Register TMP = R11;
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010044
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000045static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000046static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010047
Mark Mendell24f2dfa2015-01-14 19:51:45 -050048static constexpr int kC2ConditionMask = 0x400;
49
Roland Levillain62a46b22015-06-01 18:24:13 +010050#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010051#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Andreas Gampe85b62f22015-09-09 13:15:38 -070053class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Alexandre Rames2ed20af2015-03-06 13:55:35 +000057 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010058 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000060 if (instruction_->CanThrowIntoCatchBlock()) {
61 // Live registers will be restored in the catch block if caught.
62 SaveLiveRegisters(codegen, instruction_->GetLocations());
63 }
Alexandre Rames8158f282015-08-07 10:26:17 +010064 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
65 instruction_,
66 instruction_->GetDexPc(),
67 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 }
69
Alexandre Rames8158f282015-08-07 10:26:17 +010070 bool IsFatal() const OVERRIDE { return true; }
71
Alexandre Rames9931f312015-06-19 14:47:01 +010072 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
73
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
77};
78
Andreas Gampe85b62f22015-09-09 13:15:38 -070079class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000080 public:
81 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
82
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010084 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000085 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000086 if (instruction_->CanThrowIntoCatchBlock()) {
87 // Live registers will be restored in the catch block if caught.
88 SaveLiveRegisters(codegen, instruction_->GetLocations());
89 }
Alexandre Rames8158f282015-08-07 10:26:17 +010090 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
91 instruction_,
92 instruction_->GetDexPc(),
93 this);
Calin Juravled0d48522014-11-04 16:40:20 +000094 }
95
Alexandre Rames8158f282015-08-07 10:26:17 +010096 bool IsFatal() const OVERRIDE { return true; }
97
Alexandre Rames9931f312015-06-19 14:47:01 +010098 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
99
Calin Juravled0d48522014-11-04 16:40:20 +0000100 private:
101 HDivZeroCheck* const instruction_;
102 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
103};
104
Andreas Gampe85b62f22015-09-09 13:15:38 -0700105class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000106 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100107 DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000110 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000112 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(cpu_reg_);
115 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400116 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000117 }
118
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000119 } else {
120 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000121 if (is_div_) {
122 __ negq(cpu_reg_);
123 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400124 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 }
Calin Juravled0d48522014-11-04 16:40:20 +0000127 __ jmp(GetExitLabel());
128 }
129
Alexandre Rames9931f312015-06-19 14:47:01 +0100130 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
131
Calin Juravled0d48522014-11-04 16:40:20 +0000132 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000133 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000134 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000135 const bool is_div_;
136 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000137};
138
Andreas Gampe85b62f22015-09-09 13:15:38 -0700139class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100141 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000144 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100145 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000147 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100148 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
149 instruction_,
150 instruction_->GetDexPc(),
151 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000152 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100153 if (successor_ == nullptr) {
154 __ jmp(GetReturnLabel());
155 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100156 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 }
159
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 Label* GetReturnLabel() {
161 DCHECK(successor_ == nullptr);
162 return &return_label_;
163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100165 HBasicBlock* GetSuccessor() const {
166 return successor_;
167 }
168
Alexandre Rames9931f312015-06-19 14:47:01 +0100169 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
170
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 private:
172 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
177};
178
Andreas Gampe85b62f22015-09-09 13:15:38 -0700179class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100181 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
182 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000184 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100185 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames8158f282015-08-07 10:26:17 +0100186 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000188 if (instruction_->CanThrowIntoCatchBlock()) {
189 // Live registers will be restored in the catch block if caught.
190 SaveLiveRegisters(codegen, instruction_->GetLocations());
191 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000192 // We're moving two locations to locations that could overlap, so we need a parallel
193 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000195 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100196 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000197 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100198 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100199 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100200 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
201 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100202 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
203 instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100204 }
205
Alexandre Rames8158f282015-08-07 10:26:17 +0100206 bool IsFatal() const OVERRIDE { return true; }
207
Alexandre Rames9931f312015-06-19 14:47:01 +0100208 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
209
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100210 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100211 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100212
213 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
214};
215
Andreas Gampe85b62f22015-09-09 13:15:38 -0700216class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100217 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000218 LoadClassSlowPathX86_64(HLoadClass* cls,
219 HInstruction* at,
220 uint32_t dex_pc,
221 bool do_clinit)
222 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
223 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
224 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
229 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000231 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100233 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100235 x64_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
236 : QUICK_ENTRY_POINT(pInitializeType),
237 at_, dex_pc_, this);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100238
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000239 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000241 if (out.IsValid()) {
242 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
243 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 }
245
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000246 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100247 __ jmp(GetExitLabel());
248 }
249
Alexandre Rames9931f312015-06-19 14:47:01 +0100250 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
251
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 // The class this slow path will load.
254 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100255
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 // The instruction where this slow path is happening.
257 // (Might be the load class or an initialization check).
258 HInstruction* const at_;
259
260 // The dex PC of `at_`.
261 const uint32_t dex_pc_;
262
263 // Whether to initialize the class.
264 const bool do_clinit_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100267};
268
Andreas Gampe85b62f22015-09-09 13:15:38 -0700269class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000270 public:
271 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
272
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274 LocationSummary* locations = instruction_->GetLocations();
275 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
276
277 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
278 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000279 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000280
281 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800282 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000283 Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100284 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
285 instruction_,
286 instruction_->GetDexPc(),
287 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000288 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000289 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000290 __ jmp(GetExitLabel());
291 }
292
Alexandre Rames9931f312015-06-19 14:47:01 +0100293 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
294
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000295 private:
296 HLoadString* const instruction_;
297
298 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
299};
300
Andreas Gampe85b62f22015-09-09 13:15:38 -0700301class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302 public:
Nicolas Geoffray75374372015-09-17 17:12:19 +0000303 explicit TypeCheckSlowPathX86_64(HInstruction* instruction)
304 : instruction_(instruction) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000306 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000307 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100308 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
309 : locations->Out();
310 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 DCHECK(instruction_->IsCheckCast()
312 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313
314 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
315 __ Bind(GetEntryLabel());
Nicolas Geoffray75374372015-09-17 17:12:19 +0000316 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
318 // We're moving two locations to locations that could overlap, so we need a parallel
319 // move resolver.
320 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000321 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100322 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000323 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100324 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100326 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
327 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100330 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
331 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100332 dex_pc,
Alexandre Rames8158f282015-08-07 10:26:17 +0100333 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000334 } else {
335 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100336 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
337 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100338 dex_pc,
Alexandre Rames8158f282015-08-07 10:26:17 +0100339 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000340 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000341
Nicolas Geoffray75374372015-09-17 17:12:19 +0000342 if (instruction_->IsInstanceOf()) {
343 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100344 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000345
346 RestoreLiveRegisters(codegen, locations);
347 __ jmp(GetExitLabel());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348 }
349
Alexandre Rames9931f312015-06-19 14:47:01 +0100350 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
351
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000352 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353 HInstruction* const instruction_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000354
355 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
356};
357
Andreas Gampe85b62f22015-09-09 13:15:38 -0700358class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700359 public:
360 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
361 : instruction_(instruction) {}
362
363 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100364 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 __ Bind(GetEntryLabel());
366 SaveLiveRegisters(codegen, instruction_->GetLocations());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 DCHECK(instruction_->IsDeoptimize());
368 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
Alexandre Rames8158f282015-08-07 10:26:17 +0100369 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
370 deoptimize,
371 deoptimize->GetDexPc(),
372 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700373 }
374
Alexandre Rames9931f312015-06-19 14:47:01 +0100375 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
376
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700377 private:
378 HInstruction* const instruction_;
379 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
380};
381
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100382#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100383#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100384
Roland Levillain4fa13f62015-07-06 18:11:54 +0100385inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700386 switch (cond) {
387 case kCondEQ: return kEqual;
388 case kCondNE: return kNotEqual;
389 case kCondLT: return kLess;
390 case kCondLE: return kLessEqual;
391 case kCondGT: return kGreater;
392 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700393 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100394 LOG(FATAL) << "Unreachable";
395 UNREACHABLE();
396}
397
398inline Condition X86_64FPCondition(IfCondition cond) {
399 switch (cond) {
400 case kCondEQ: return kEqual;
401 case kCondNE: return kNotEqual;
402 case kCondLT: return kBelow;
403 case kCondLE: return kBelowEqual;
404 case kCondGT: return kAbove;
405 case kCondGE: return kAboveEqual;
406 };
407 LOG(FATAL) << "Unreachable";
408 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700409}
410
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800411void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100412 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800413 // All registers are assumed to be correctly set up.
414
Vladimir Marko58155012015-08-19 12:49:41 +0000415 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
416 switch (invoke->GetMethodLoadKind()) {
417 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
418 // temp = thread->string_init_entrypoint
419 __ gs()->movl(temp.AsRegister<CpuRegister>(),
420 Address::Absolute(invoke->GetStringInitOffset(), true));
421 break;
422 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
423 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
424 break;
425 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
426 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
427 break;
428 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
429 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
430 method_patches_.emplace_back(invoke->GetTargetMethod());
431 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
432 break;
433 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
434 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
435 invoke->GetDexCacheArrayOffset());
436 __ movq(temp.AsRegister<CpuRegister>(),
437 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
438 // Bind the label at the end of the "movl" insn.
439 __ Bind(&pc_rel_dex_cache_patches_.back().label);
440 break;
441 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
442 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
443 Register method_reg;
444 CpuRegister reg = temp.AsRegister<CpuRegister>();
445 if (current_method.IsRegister()) {
446 method_reg = current_method.AsRegister<Register>();
447 } else {
448 DCHECK(invoke->GetLocations()->Intrinsified());
449 DCHECK(!current_method.IsValid());
450 method_reg = reg.AsRegister();
451 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
452 }
453 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100454 __ movq(reg,
455 Address(CpuRegister(method_reg),
456 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000457 // temp = temp[index_in_cache]
458 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
459 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
460 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100461 }
Vladimir Marko58155012015-08-19 12:49:41 +0000462 }
463
464 switch (invoke->GetCodePtrLocation()) {
465 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
466 __ call(&frame_entry_label_);
467 break;
468 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
469 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
470 Label* label = &relative_call_patches_.back().label;
471 __ call(label); // Bind to the patch label, override at link time.
472 __ Bind(label); // Bind the label at the end of the "call" insn.
473 break;
474 }
475 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
476 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
477 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
478 FALLTHROUGH_INTENDED;
479 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
480 // (callee_method + offset_of_quick_compiled_code)()
481 __ call(Address(callee_method.AsRegister<CpuRegister>(),
482 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
483 kX86_64WordSize).SizeValue()));
484 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000485 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800486
487 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800488}
489
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000490void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
491 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
492 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
493 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
494 LocationSummary* locations = invoke->GetLocations();
495 Location receiver = locations->InAt(0);
496 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
497 // temp = object->GetClass();
498 DCHECK(receiver.IsRegister());
499 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
500 MaybeRecordImplicitNullCheck(invoke);
501 __ MaybeUnpoisonHeapReference(temp);
502 // temp = temp->GetMethodAt(method_offset);
503 __ movq(temp, Address(temp, method_offset));
504 // call temp->GetEntryPoint();
505 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
506 kX86_64WordSize).SizeValue()));
507}
508
Vladimir Marko58155012015-08-19 12:49:41 +0000509void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
510 DCHECK(linker_patches->empty());
511 size_t size =
512 method_patches_.size() + relative_call_patches_.size() + pc_rel_dex_cache_patches_.size();
513 linker_patches->reserve(size);
514 for (const MethodPatchInfo<Label>& info : method_patches_) {
515 // The label points to the end of the "movl" instruction but the literal offset for method
516 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
517 uint32_t literal_offset = info.label.Position() - 4;
518 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
519 info.target_method.dex_file,
520 info.target_method.dex_method_index));
521 }
522 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
523 // The label points to the end of the "call" instruction but the literal offset for method
524 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
525 uint32_t literal_offset = info.label.Position() - 4;
526 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
527 info.target_method.dex_file,
528 info.target_method.dex_method_index));
529 }
530 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
531 // The label points to the end of the "mov" instruction but the literal offset for method
532 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
533 uint32_t literal_offset = info.label.Position() - 4;
534 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
535 &info.target_dex_file,
536 info.label.Position(),
537 info.element_offset));
538 }
539}
540
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100541void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100542 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100543}
544
545void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100546 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100547}
548
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100549size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
550 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
551 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100552}
553
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100554size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
555 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
556 return kX86_64WordSize;
557}
558
559size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
560 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
561 return kX86_64WordSize;
562}
563
564size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
565 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
566 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100567}
568
Calin Juravle175dc732015-08-25 15:42:32 +0100569void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
570 HInstruction* instruction,
571 uint32_t dex_pc,
572 SlowPathCode* slow_path) {
573 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
574 instruction,
575 dex_pc,
576 slow_path);
577}
578
579void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100580 HInstruction* instruction,
581 uint32_t dex_pc,
582 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100583 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100584 __ gs()->call(Address::Absolute(entry_point_offset, true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100585 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100586}
587
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000588static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000589// Use a fake return address register to mimic Quick.
590static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400591CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
592 const X86_64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100593 const CompilerOptions& compiler_options,
594 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000595 : CodeGenerator(graph,
596 kNumberOfCpuRegisters,
597 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000598 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000599 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
600 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000601 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000602 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
603 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100604 compiler_options,
605 stats),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100606 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100607 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000608 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400609 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400610 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000611 constant_area_start_(0),
612 method_patches_(graph->GetArena()->Adapter()),
613 relative_call_patches_(graph->GetArena()->Adapter()),
614 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000615 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
616}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100617
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100618InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
619 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100620 : HGraphVisitor(graph),
621 assembler_(codegen->GetAssembler()),
622 codegen_(codegen) {}
623
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100624Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100625 switch (type) {
626 case Primitive::kPrimLong:
627 case Primitive::kPrimByte:
628 case Primitive::kPrimBoolean:
629 case Primitive::kPrimChar:
630 case Primitive::kPrimShort:
631 case Primitive::kPrimInt:
632 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100633 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100634 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100635 }
636
637 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100638 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100639 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100640 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100642
643 case Primitive::kPrimVoid:
644 LOG(FATAL) << "Unreachable type " << type;
645 }
646
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100647 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100648}
649
Nicolas Geoffray98893962015-01-21 12:32:32 +0000650void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100651 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100652 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100653
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000654 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100655 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000656
Nicolas Geoffray98893962015-01-21 12:32:32 +0000657 if (is_baseline) {
658 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
659 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
660 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000661 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
662 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
663 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000664 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100665}
666
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100667static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100668 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100669}
David Srbecky9d8606d2015-04-12 09:35:32 +0100670
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100671static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100672 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100673}
674
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100675void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100676 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000677 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100678 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700679 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000680 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100681
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000682 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100683 __ testq(CpuRegister(RAX), Address(
684 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100685 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100686 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000687
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000688 if (HasEmptyFrame()) {
689 return;
690 }
691
Nicolas Geoffray98893962015-01-21 12:32:32 +0000692 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000693 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000694 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000695 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100696 __ cfi().AdjustCFAOffset(kX86_64WordSize);
697 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000698 }
699 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100700
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100701 int adjust = GetFrameSize() - GetCoreSpillSize();
702 __ subq(CpuRegister(RSP), Immediate(adjust));
703 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000704 uint32_t xmm_spill_location = GetFpuSpillStart();
705 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100706
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000707 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
708 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100709 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
710 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
711 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000712 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100713 }
714
Mathieu Chartiere401d142015-04-22 13:56:20 -0700715 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100716 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717}
718
719void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100720 __ cfi().RememberState();
721 if (!HasEmptyFrame()) {
722 uint32_t xmm_spill_location = GetFpuSpillStart();
723 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
724 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
725 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
726 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
727 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
728 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
729 }
730 }
731
732 int adjust = GetFrameSize() - GetCoreSpillSize();
733 __ addq(CpuRegister(RSP), Immediate(adjust));
734 __ cfi().AdjustCFAOffset(-adjust);
735
736 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
737 Register reg = kCoreCalleeSaves[i];
738 if (allocated_registers_.ContainsCoreRegister(reg)) {
739 __ popq(CpuRegister(reg));
740 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
741 __ cfi().Restore(DWARFReg(reg));
742 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000743 }
744 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100745 __ ret();
746 __ cfi().RestoreState();
747 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100748}
749
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100750void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
751 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100752}
753
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100754Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
755 switch (load->GetType()) {
756 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100758 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100759
760 case Primitive::kPrimInt:
761 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100762 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100763 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100764
765 case Primitive::kPrimBoolean:
766 case Primitive::kPrimByte:
767 case Primitive::kPrimChar:
768 case Primitive::kPrimShort:
769 case Primitive::kPrimVoid:
770 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700771 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100772 }
773
774 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700775 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100776}
777
778void CodeGeneratorX86_64::Move(Location destination, Location source) {
779 if (source.Equals(destination)) {
780 return;
781 }
782 if (destination.IsRegister()) {
783 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000784 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100785 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000786 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100787 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000788 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100789 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100790 } else {
791 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000792 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100793 Address(CpuRegister(RSP), source.GetStackIndex()));
794 }
795 } else if (destination.IsFpuRegister()) {
796 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000797 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000799 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100800 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000801 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100802 Address(CpuRegister(RSP), source.GetStackIndex()));
803 } else {
804 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000805 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100806 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100807 }
808 } else if (destination.IsStackSlot()) {
809 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100810 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000811 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100812 } else if (source.IsFpuRegister()) {
813 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000814 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500815 } else if (source.IsConstant()) {
816 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000817 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500818 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100819 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500820 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000821 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
822 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100823 }
824 } else {
825 DCHECK(destination.IsDoubleStackSlot());
826 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100827 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000828 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100829 } else if (source.IsFpuRegister()) {
830 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000831 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500832 } else if (source.IsConstant()) {
833 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800834 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500835 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000836 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500837 } else {
838 DCHECK(constant->IsLongConstant());
839 value = constant->AsLongConstant()->GetValue();
840 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400841 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100842 } else {
843 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000844 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
845 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846 }
847 }
848}
849
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100850void CodeGeneratorX86_64::Move(HInstruction* instruction,
851 Location location,
852 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000853 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100854 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700855 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100856 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000857 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100858 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000859 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000860 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
861 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000862 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000863 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000864 } else if (location.IsStackSlot()) {
865 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
866 } else {
867 DCHECK(location.IsConstant());
868 DCHECK_EQ(location.GetConstant(), const_to_move);
869 }
870 } else if (const_to_move->IsLongConstant()) {
871 int64_t value = const_to_move->AsLongConstant()->GetValue();
872 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400873 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000874 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400875 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000876 } else {
877 DCHECK(location.IsConstant());
878 DCHECK_EQ(location.GetConstant(), const_to_move);
879 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100880 }
Roland Levillain476df552014-10-09 17:51:36 +0100881 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100882 switch (instruction->GetType()) {
883 case Primitive::kPrimBoolean:
884 case Primitive::kPrimByte:
885 case Primitive::kPrimChar:
886 case Primitive::kPrimShort:
887 case Primitive::kPrimInt:
888 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100889 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100890 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
891 break;
892
893 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100894 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000895 Move(location,
896 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100897 break;
898
899 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100900 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100901 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000902 } else if (instruction->IsTemporary()) {
903 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
904 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100905 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100906 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100907 switch (instruction->GetType()) {
908 case Primitive::kPrimBoolean:
909 case Primitive::kPrimByte:
910 case Primitive::kPrimChar:
911 case Primitive::kPrimShort:
912 case Primitive::kPrimInt:
913 case Primitive::kPrimNot:
914 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100915 case Primitive::kPrimFloat:
916 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000917 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100918 break;
919
920 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100921 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100922 }
923 }
924}
925
Calin Juravle175dc732015-08-25 15:42:32 +0100926void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
927 DCHECK(location.IsRegister());
928 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
929}
930
David Brazdilfc6a86a2015-06-26 10:33:45 +0000931void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100932 DCHECK(!successor->IsExitBlock());
933
934 HBasicBlock* block = got->GetBlock();
935 HInstruction* previous = got->GetPrevious();
936
937 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000938 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100939 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
940 return;
941 }
942
943 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
944 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
945 }
946 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100947 __ jmp(codegen_->GetLabelOf(successor));
948 }
949}
950
David Brazdilfc6a86a2015-06-26 10:33:45 +0000951void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
952 got->SetLocations(nullptr);
953}
954
955void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
956 HandleGoto(got, got->GetSuccessor());
957}
958
959void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
960 try_boundary->SetLocations(nullptr);
961}
962
963void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
964 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
965 if (!successor->IsExitBlock()) {
966 HandleGoto(try_boundary, successor);
967 }
968}
969
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100970void LocationsBuilderX86_64::VisitExit(HExit* exit) {
971 exit->SetLocations(nullptr);
972}
973
974void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700975 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100976}
977
Mark Mendellc4701932015-04-10 13:18:51 -0400978void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
979 Label* true_label,
980 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100981 if (cond->IsFPConditionTrueIfNaN()) {
982 __ j(kUnordered, true_label);
983 } else if (cond->IsFPConditionFalseIfNaN()) {
984 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400985 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100986 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400987}
988
989void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
990 HCondition* condition,
991 Label* true_target,
992 Label* false_target,
993 Label* always_true_target) {
994 LocationSummary* locations = condition->GetLocations();
995 Location left = locations->InAt(0);
996 Location right = locations->InAt(1);
997
998 // We don't want true_target as a nullptr.
999 if (true_target == nullptr) {
1000 true_target = always_true_target;
1001 }
1002 bool falls_through = (false_target == nullptr);
1003
1004 // FP compares don't like null false_targets.
1005 if (false_target == nullptr) {
1006 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1007 }
1008
1009 Primitive::Type type = condition->InputAt(0)->GetType();
1010 switch (type) {
1011 case Primitive::kPrimLong: {
1012 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1013 if (right.IsConstant()) {
1014 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1015 if (IsInt<32>(value)) {
1016 if (value == 0) {
1017 __ testq(left_reg, left_reg);
1018 } else {
1019 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1020 }
1021 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001022 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001023 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1024 }
1025 } else if (right.IsDoubleStackSlot()) {
1026 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1027 } else {
1028 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1029 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001030 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001031 break;
1032 }
1033 case Primitive::kPrimFloat: {
1034 if (right.IsFpuRegister()) {
1035 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1036 } else if (right.IsConstant()) {
1037 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1038 codegen_->LiteralFloatAddress(
1039 right.GetConstant()->AsFloatConstant()->GetValue()));
1040 } else {
1041 DCHECK(right.IsStackSlot());
1042 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1043 Address(CpuRegister(RSP), right.GetStackIndex()));
1044 }
1045 GenerateFPJumps(condition, true_target, false_target);
1046 break;
1047 }
1048 case Primitive::kPrimDouble: {
1049 if (right.IsFpuRegister()) {
1050 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1051 } else if (right.IsConstant()) {
1052 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1053 codegen_->LiteralDoubleAddress(
1054 right.GetConstant()->AsDoubleConstant()->GetValue()));
1055 } else {
1056 DCHECK(right.IsDoubleStackSlot());
1057 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1058 Address(CpuRegister(RSP), right.GetStackIndex()));
1059 }
1060 GenerateFPJumps(condition, true_target, false_target);
1061 break;
1062 }
1063 default:
1064 LOG(FATAL) << "Unexpected condition type " << type;
1065 }
1066
1067 if (!falls_through) {
1068 __ jmp(false_target);
1069 }
1070}
1071
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001072void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1073 Label* true_target,
1074 Label* false_target,
1075 Label* always_true_target) {
1076 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001077 if (cond->IsIntConstant()) {
1078 // Constant condition, statically compared against 1.
1079 int32_t cond_value = cond->AsIntConstant()->GetValue();
1080 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001081 if (always_true_target != nullptr) {
1082 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001083 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001084 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001085 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001086 DCHECK_EQ(cond_value, 0);
1087 }
1088 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001089 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001090 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1091 // Moves do not affect the eflags register, so if the condition is
1092 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001093 // again. We can't use the eflags on FP conditions if they are
1094 // materialized due to the complex branching.
1095 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001096 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001097 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1098 && !Primitive::IsFloatingPointType(type);
1099
Roland Levillain4fa13f62015-07-06 18:11:54 +01001100 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001101 if (!eflags_set) {
1102 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001103 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001104 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001105 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001106 } else {
1107 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1108 Immediate(0));
1109 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001110 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001111 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001112 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001113 }
1114 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001115 // Condition has not been materialized, use its inputs as the
1116 // comparison and its condition as the branch condition.
1117
Mark Mendellc4701932015-04-10 13:18:51 -04001118 // Is this a long or FP comparison that has been folded into the HCondition?
1119 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001120 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001121 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1122 true_target, false_target, always_true_target);
1123 return;
1124 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001125
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001126 Location lhs = cond->GetLocations()->InAt(0);
1127 Location rhs = cond->GetLocations()->InAt(1);
1128 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001129 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001130 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001131 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001132 if (constant == 0) {
1133 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1134 } else {
1135 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1136 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001137 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001138 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001139 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1140 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001141 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001142 }
Dave Allison20dfc792014-06-16 20:44:29 -07001143 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001144 if (false_target != nullptr) {
1145 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001146 }
1147}
1148
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001149void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1150 LocationSummary* locations =
1151 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1152 HInstruction* cond = if_instr->InputAt(0);
1153 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1154 locations->SetInAt(0, Location::Any());
1155 }
1156}
1157
1158void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1159 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1160 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1161 Label* always_true_target = true_target;
1162 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1163 if_instr->IfTrueSuccessor())) {
1164 always_true_target = nullptr;
1165 }
1166 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1167 if_instr->IfFalseSuccessor())) {
1168 false_target = nullptr;
1169 }
1170 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1171}
1172
1173void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1174 LocationSummary* locations = new (GetGraph()->GetArena())
1175 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1176 HInstruction* cond = deoptimize->InputAt(0);
1177 DCHECK(cond->IsCondition());
1178 if (cond->AsCondition()->NeedsMaterialization()) {
1179 locations->SetInAt(0, Location::Any());
1180 }
1181}
1182
1183void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001184 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001185 DeoptimizationSlowPathX86_64(deoptimize);
1186 codegen_->AddSlowPath(slow_path);
1187 Label* slow_path_entry = slow_path->GetEntryLabel();
1188 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1189}
1190
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001191void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1192 local->SetLocations(nullptr);
1193}
1194
1195void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1196 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1197}
1198
1199void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1200 local->SetLocations(nullptr);
1201}
1202
1203void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1204 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001205 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001206}
1207
1208void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001209 LocationSummary* locations =
1210 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001211 switch (store->InputAt(1)->GetType()) {
1212 case Primitive::kPrimBoolean:
1213 case Primitive::kPrimByte:
1214 case Primitive::kPrimChar:
1215 case Primitive::kPrimShort:
1216 case Primitive::kPrimInt:
1217 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001219 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1220 break;
1221
1222 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001223 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001224 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1225 break;
1226
1227 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001228 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001229 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001230}
1231
1232void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001233 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001234}
1235
Roland Levillain0d37cd02015-05-27 16:39:19 +01001236void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001237 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001238 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001239 // Handle the long/FP comparisons made in instruction simplification.
1240 switch (cond->InputAt(0)->GetType()) {
1241 case Primitive::kPrimLong:
1242 locations->SetInAt(0, Location::RequiresRegister());
1243 locations->SetInAt(1, Location::Any());
1244 break;
1245 case Primitive::kPrimFloat:
1246 case Primitive::kPrimDouble:
1247 locations->SetInAt(0, Location::RequiresFpuRegister());
1248 locations->SetInAt(1, Location::Any());
1249 break;
1250 default:
1251 locations->SetInAt(0, Location::RequiresRegister());
1252 locations->SetInAt(1, Location::Any());
1253 break;
1254 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001255 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001256 locations->SetOut(Location::RequiresRegister());
1257 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001258}
1259
Roland Levillain0d37cd02015-05-27 16:39:19 +01001260void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001261 if (!cond->NeedsMaterialization()) {
1262 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001263 }
Mark Mendellc4701932015-04-10 13:18:51 -04001264
1265 LocationSummary* locations = cond->GetLocations();
1266 Location lhs = locations->InAt(0);
1267 Location rhs = locations->InAt(1);
1268 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1269 Label true_label, false_label;
1270
1271 switch (cond->InputAt(0)->GetType()) {
1272 default:
1273 // Integer case.
1274
1275 // Clear output register: setcc only sets the low byte.
1276 __ xorl(reg, reg);
1277
1278 if (rhs.IsRegister()) {
1279 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1280 } else if (rhs.IsConstant()) {
1281 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1282 if (constant == 0) {
1283 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1284 } else {
1285 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1286 }
1287 } else {
1288 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1289 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001290 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001291 return;
1292 case Primitive::kPrimLong:
1293 // Clear output register: setcc only sets the low byte.
1294 __ xorl(reg, reg);
1295
1296 if (rhs.IsRegister()) {
1297 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1298 } else if (rhs.IsConstant()) {
1299 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1300 if (IsInt<32>(value)) {
1301 if (value == 0) {
1302 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1303 } else {
1304 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1305 }
1306 } else {
1307 // Value won't fit in an int.
1308 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1309 }
1310 } else {
1311 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1312 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001313 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001314 return;
1315 case Primitive::kPrimFloat: {
1316 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1317 if (rhs.IsConstant()) {
1318 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1319 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1320 } else if (rhs.IsStackSlot()) {
1321 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1322 } else {
1323 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1324 }
1325 GenerateFPJumps(cond, &true_label, &false_label);
1326 break;
1327 }
1328 case Primitive::kPrimDouble: {
1329 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1330 if (rhs.IsConstant()) {
1331 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1332 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1333 } else if (rhs.IsDoubleStackSlot()) {
1334 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1335 } else {
1336 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1337 }
1338 GenerateFPJumps(cond, &true_label, &false_label);
1339 break;
1340 }
1341 }
1342
1343 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001344 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001345
Roland Levillain4fa13f62015-07-06 18:11:54 +01001346 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001347 __ Bind(&false_label);
1348 __ xorl(reg, reg);
1349 __ jmp(&done_label);
1350
Roland Levillain4fa13f62015-07-06 18:11:54 +01001351 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001352 __ Bind(&true_label);
1353 __ movl(reg, Immediate(1));
1354 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001355}
1356
1357void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1358 VisitCondition(comp);
1359}
1360
1361void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1362 VisitCondition(comp);
1363}
1364
1365void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1366 VisitCondition(comp);
1367}
1368
1369void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1370 VisitCondition(comp);
1371}
1372
1373void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1374 VisitCondition(comp);
1375}
1376
1377void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1378 VisitCondition(comp);
1379}
1380
1381void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1382 VisitCondition(comp);
1383}
1384
1385void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1386 VisitCondition(comp);
1387}
1388
1389void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1390 VisitCondition(comp);
1391}
1392
1393void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1394 VisitCondition(comp);
1395}
1396
1397void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1398 VisitCondition(comp);
1399}
1400
1401void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1402 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001403}
1404
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001405void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001406 LocationSummary* locations =
1407 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001408 switch (compare->InputAt(0)->GetType()) {
1409 case Primitive::kPrimLong: {
1410 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001411 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1413 break;
1414 }
1415 case Primitive::kPrimFloat:
1416 case Primitive::kPrimDouble: {
1417 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001418 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001419 locations->SetOut(Location::RequiresRegister());
1420 break;
1421 }
1422 default:
1423 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1424 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001425}
1426
1427void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001428 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001429 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001430 Location left = locations->InAt(0);
1431 Location right = locations->InAt(1);
1432
Mark Mendell0c9497d2015-08-21 09:30:05 -04001433 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001434 Primitive::Type type = compare->InputAt(0)->GetType();
1435 switch (type) {
1436 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001437 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1438 if (right.IsConstant()) {
1439 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001440 if (IsInt<32>(value)) {
1441 if (value == 0) {
1442 __ testq(left_reg, left_reg);
1443 } else {
1444 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1445 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001446 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001447 // Value won't fit in an int.
1448 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001449 }
Mark Mendell40741f32015-04-20 22:10:34 -04001450 } else if (right.IsDoubleStackSlot()) {
1451 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001452 } else {
1453 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1454 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001455 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001456 }
1457 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001458 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1459 if (right.IsConstant()) {
1460 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1461 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1462 } else if (right.IsStackSlot()) {
1463 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1464 } else {
1465 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1466 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001467 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1468 break;
1469 }
1470 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001471 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1472 if (right.IsConstant()) {
1473 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1474 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1475 } else if (right.IsDoubleStackSlot()) {
1476 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1477 } else {
1478 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1479 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001480 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1481 break;
1482 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001483 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001484 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001485 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001486 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001487 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001488 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001489
Calin Juravle91debbc2014-11-26 19:01:09 +00001490 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001491 __ movl(out, Immediate(1));
1492 __ jmp(&done);
1493
1494 __ Bind(&less);
1495 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001496
1497 __ Bind(&done);
1498}
1499
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001500void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001501 LocationSummary* locations =
1502 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001503 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001504}
1505
1506void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001507 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001508 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001509}
1510
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001511void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1512 LocationSummary* locations =
1513 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1514 locations->SetOut(Location::ConstantLocation(constant));
1515}
1516
1517void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1518 // Will be generated at use site.
1519 UNUSED(constant);
1520}
1521
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001522void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001523 LocationSummary* locations =
1524 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001525 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001526}
1527
1528void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001529 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001530 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001531}
1532
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001533void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1534 LocationSummary* locations =
1535 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1536 locations->SetOut(Location::ConstantLocation(constant));
1537}
1538
1539void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1540 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001541 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001542}
1543
1544void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1545 LocationSummary* locations =
1546 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1547 locations->SetOut(Location::ConstantLocation(constant));
1548}
1549
1550void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1551 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001552 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001553}
1554
Calin Juravle27df7582015-04-17 19:12:31 +01001555void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1556 memory_barrier->SetLocations(nullptr);
1557}
1558
1559void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1560 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1561}
1562
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001563void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1564 ret->SetLocations(nullptr);
1565}
1566
1567void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001568 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001569 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001570}
1571
1572void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001573 LocationSummary* locations =
1574 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001575 switch (ret->InputAt(0)->GetType()) {
1576 case Primitive::kPrimBoolean:
1577 case Primitive::kPrimByte:
1578 case Primitive::kPrimChar:
1579 case Primitive::kPrimShort:
1580 case Primitive::kPrimInt:
1581 case Primitive::kPrimNot:
1582 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001583 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001584 break;
1585
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001586 case Primitive::kPrimFloat:
1587 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001588 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001589 break;
1590
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001591 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001592 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001593 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001594}
1595
1596void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1597 if (kIsDebugBuild) {
1598 switch (ret->InputAt(0)->GetType()) {
1599 case Primitive::kPrimBoolean:
1600 case Primitive::kPrimByte:
1601 case Primitive::kPrimChar:
1602 case Primitive::kPrimShort:
1603 case Primitive::kPrimInt:
1604 case Primitive::kPrimNot:
1605 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001606 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001607 break;
1608
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001609 case Primitive::kPrimFloat:
1610 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001611 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001612 XMM0);
1613 break;
1614
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001615 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001616 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001617 }
1618 }
1619 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001620}
1621
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001622Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1623 switch (type) {
1624 case Primitive::kPrimBoolean:
1625 case Primitive::kPrimByte:
1626 case Primitive::kPrimChar:
1627 case Primitive::kPrimShort:
1628 case Primitive::kPrimInt:
1629 case Primitive::kPrimNot:
1630 case Primitive::kPrimLong:
1631 return Location::RegisterLocation(RAX);
1632
1633 case Primitive::kPrimVoid:
1634 return Location::NoLocation();
1635
1636 case Primitive::kPrimDouble:
1637 case Primitive::kPrimFloat:
1638 return Location::FpuRegisterLocation(XMM0);
1639 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001640
1641 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001642}
1643
1644Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1645 return Location::RegisterLocation(kMethodRegisterArgument);
1646}
1647
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001648Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001649 switch (type) {
1650 case Primitive::kPrimBoolean:
1651 case Primitive::kPrimByte:
1652 case Primitive::kPrimChar:
1653 case Primitive::kPrimShort:
1654 case Primitive::kPrimInt:
1655 case Primitive::kPrimNot: {
1656 uint32_t index = gp_index_++;
1657 stack_index_++;
1658 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001659 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001660 } else {
1661 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1662 }
1663 }
1664
1665 case Primitive::kPrimLong: {
1666 uint32_t index = gp_index_;
1667 stack_index_ += 2;
1668 if (index < calling_convention.GetNumberOfRegisters()) {
1669 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001670 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001671 } else {
1672 gp_index_ += 2;
1673 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1674 }
1675 }
1676
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001677 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001678 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001679 stack_index_++;
1680 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001681 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001682 } else {
1683 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1684 }
1685 }
1686
1687 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001688 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001689 stack_index_ += 2;
1690 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001691 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001692 } else {
1693 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1694 }
1695 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001696
1697 case Primitive::kPrimVoid:
1698 LOG(FATAL) << "Unexpected parameter type " << type;
1699 break;
1700 }
1701 return Location();
1702}
1703
Calin Juravle175dc732015-08-25 15:42:32 +01001704void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1705 // The trampoline uses the same calling convention as dex calling conventions,
1706 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1707 // the method_idx.
1708 HandleInvoke(invoke);
1709}
1710
1711void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1712 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1713}
1714
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001715void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001716 // When we do not run baseline, explicit clinit checks triggered by static
1717 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1718 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001719
Mark Mendellfb8d2792015-03-31 22:16:59 -04001720 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001721 if (intrinsic.TryDispatch(invoke)) {
1722 return;
1723 }
1724
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001725 HandleInvoke(invoke);
1726}
1727
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001728static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1729 if (invoke->GetLocations()->Intrinsified()) {
1730 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1731 intrinsic.Dispatch(invoke);
1732 return true;
1733 }
1734 return false;
1735}
1736
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001737void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001738 // When we do not run baseline, explicit clinit checks triggered by static
1739 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1740 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001741
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001742 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1743 return;
1744 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001745
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001746 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001747 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001748 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001749 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001750}
1751
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001752void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001753 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001754 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001755}
1756
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001757void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001758 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001759 if (intrinsic.TryDispatch(invoke)) {
1760 return;
1761 }
1762
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001763 HandleInvoke(invoke);
1764}
1765
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001766void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001767 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1768 return;
1769 }
1770
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001771 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001772
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001773 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001774 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001775}
1776
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001777void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1778 HandleInvoke(invoke);
1779 // Add the hidden argument.
1780 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1781}
1782
1783void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1784 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001785 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001786 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1787 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001788 LocationSummary* locations = invoke->GetLocations();
1789 Location receiver = locations->InAt(0);
1790 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1791
1792 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001793 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1794 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001795
1796 // temp = object->GetClass();
1797 if (receiver.IsStackSlot()) {
1798 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1799 __ movl(temp, Address(temp, class_offset));
1800 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001801 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001802 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001803 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001804 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001805 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001806 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001807 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001808 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001809 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001810
1811 DCHECK(!codegen_->IsLeafMethod());
1812 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1813}
1814
Roland Levillain88cb1752014-10-20 16:36:47 +01001815void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1816 LocationSummary* locations =
1817 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1818 switch (neg->GetResultType()) {
1819 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001820 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001821 locations->SetInAt(0, Location::RequiresRegister());
1822 locations->SetOut(Location::SameAsFirstInput());
1823 break;
1824
Roland Levillain88cb1752014-10-20 16:36:47 +01001825 case Primitive::kPrimFloat:
1826 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001827 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001828 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001829 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001830 break;
1831
1832 default:
1833 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1834 }
1835}
1836
1837void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1838 LocationSummary* locations = neg->GetLocations();
1839 Location out = locations->Out();
1840 Location in = locations->InAt(0);
1841 switch (neg->GetResultType()) {
1842 case Primitive::kPrimInt:
1843 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001844 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001845 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001846 break;
1847
1848 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001849 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001850 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001851 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001852 break;
1853
Roland Levillain5368c212014-11-27 15:03:41 +00001854 case Primitive::kPrimFloat: {
1855 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001856 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001857 // Implement float negation with an exclusive or with value
1858 // 0x80000000 (mask for bit 31, representing the sign of a
1859 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001860 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001861 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001862 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001863 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001864
Roland Levillain5368c212014-11-27 15:03:41 +00001865 case Primitive::kPrimDouble: {
1866 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001867 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001868 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001869 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001870 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001871 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001872 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001873 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001874 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001875
1876 default:
1877 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1878 }
1879}
1880
Roland Levillaindff1f282014-11-05 14:15:05 +00001881void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1882 LocationSummary* locations =
1883 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1884 Primitive::Type result_type = conversion->GetResultType();
1885 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001886 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001887
David Brazdilb2bd1c52015-03-25 11:17:37 +00001888 // The Java language does not allow treating boolean as an integral type but
1889 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001890
Roland Levillaindff1f282014-11-05 14:15:05 +00001891 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001892 case Primitive::kPrimByte:
1893 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001894 case Primitive::kPrimBoolean:
1895 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001896 case Primitive::kPrimShort:
1897 case Primitive::kPrimInt:
1898 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001899 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001900 locations->SetInAt(0, Location::Any());
1901 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1902 break;
1903
1904 default:
1905 LOG(FATAL) << "Unexpected type conversion from " << input_type
1906 << " to " << result_type;
1907 }
1908 break;
1909
Roland Levillain01a8d712014-11-14 16:27:39 +00001910 case Primitive::kPrimShort:
1911 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001912 case Primitive::kPrimBoolean:
1913 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001914 case Primitive::kPrimByte:
1915 case Primitive::kPrimInt:
1916 case Primitive::kPrimChar:
1917 // Processing a Dex `int-to-short' instruction.
1918 locations->SetInAt(0, Location::Any());
1919 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1920 break;
1921
1922 default:
1923 LOG(FATAL) << "Unexpected type conversion from " << input_type
1924 << " to " << result_type;
1925 }
1926 break;
1927
Roland Levillain946e1432014-11-11 17:35:19 +00001928 case Primitive::kPrimInt:
1929 switch (input_type) {
1930 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001931 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001932 locations->SetInAt(0, Location::Any());
1933 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1934 break;
1935
1936 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001937 // Processing a Dex `float-to-int' instruction.
1938 locations->SetInAt(0, Location::RequiresFpuRegister());
1939 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001940 break;
1941
Roland Levillain946e1432014-11-11 17:35:19 +00001942 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001943 // Processing a Dex `double-to-int' instruction.
1944 locations->SetInAt(0, Location::RequiresFpuRegister());
1945 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001946 break;
1947
1948 default:
1949 LOG(FATAL) << "Unexpected type conversion from " << input_type
1950 << " to " << result_type;
1951 }
1952 break;
1953
Roland Levillaindff1f282014-11-05 14:15:05 +00001954 case Primitive::kPrimLong:
1955 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001956 case Primitive::kPrimBoolean:
1957 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001958 case Primitive::kPrimByte:
1959 case Primitive::kPrimShort:
1960 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001961 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001962 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001963 // TODO: We would benefit from a (to-be-implemented)
1964 // Location::RegisterOrStackSlot requirement for this input.
1965 locations->SetInAt(0, Location::RequiresRegister());
1966 locations->SetOut(Location::RequiresRegister());
1967 break;
1968
1969 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001970 // Processing a Dex `float-to-long' instruction.
1971 locations->SetInAt(0, Location::RequiresFpuRegister());
1972 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00001973 break;
1974
Roland Levillaindff1f282014-11-05 14:15:05 +00001975 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001976 // Processing a Dex `double-to-long' instruction.
1977 locations->SetInAt(0, Location::RequiresFpuRegister());
1978 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001979 break;
1980
1981 default:
1982 LOG(FATAL) << "Unexpected type conversion from " << input_type
1983 << " to " << result_type;
1984 }
1985 break;
1986
Roland Levillain981e4542014-11-14 11:47:14 +00001987 case Primitive::kPrimChar:
1988 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001989 case Primitive::kPrimBoolean:
1990 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001991 case Primitive::kPrimByte:
1992 case Primitive::kPrimShort:
1993 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001994 // Processing a Dex `int-to-char' instruction.
1995 locations->SetInAt(0, Location::Any());
1996 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1997 break;
1998
1999 default:
2000 LOG(FATAL) << "Unexpected type conversion from " << input_type
2001 << " to " << result_type;
2002 }
2003 break;
2004
Roland Levillaindff1f282014-11-05 14:15:05 +00002005 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002006 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002007 case Primitive::kPrimBoolean:
2008 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002009 case Primitive::kPrimByte:
2010 case Primitive::kPrimShort:
2011 case Primitive::kPrimInt:
2012 case Primitive::kPrimChar:
2013 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002014 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002015 locations->SetOut(Location::RequiresFpuRegister());
2016 break;
2017
2018 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002019 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002020 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002021 locations->SetOut(Location::RequiresFpuRegister());
2022 break;
2023
Roland Levillaincff13742014-11-17 14:32:17 +00002024 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002025 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002026 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002027 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002028 break;
2029
2030 default:
2031 LOG(FATAL) << "Unexpected type conversion from " << input_type
2032 << " to " << result_type;
2033 };
2034 break;
2035
Roland Levillaindff1f282014-11-05 14:15:05 +00002036 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002037 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002038 case Primitive::kPrimBoolean:
2039 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002040 case Primitive::kPrimByte:
2041 case Primitive::kPrimShort:
2042 case Primitive::kPrimInt:
2043 case Primitive::kPrimChar:
2044 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002045 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002046 locations->SetOut(Location::RequiresFpuRegister());
2047 break;
2048
2049 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002050 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002051 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002052 locations->SetOut(Location::RequiresFpuRegister());
2053 break;
2054
Roland Levillaincff13742014-11-17 14:32:17 +00002055 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002056 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002057 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002058 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002059 break;
2060
2061 default:
2062 LOG(FATAL) << "Unexpected type conversion from " << input_type
2063 << " to " << result_type;
2064 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002065 break;
2066
2067 default:
2068 LOG(FATAL) << "Unexpected type conversion from " << input_type
2069 << " to " << result_type;
2070 }
2071}
2072
2073void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2074 LocationSummary* locations = conversion->GetLocations();
2075 Location out = locations->Out();
2076 Location in = locations->InAt(0);
2077 Primitive::Type result_type = conversion->GetResultType();
2078 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002079 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002080 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002081 case Primitive::kPrimByte:
2082 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002083 case Primitive::kPrimBoolean:
2084 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002085 case Primitive::kPrimShort:
2086 case Primitive::kPrimInt:
2087 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002088 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002089 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002090 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002091 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002093 Address(CpuRegister(RSP), in.GetStackIndex()));
2094 } else {
2095 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002096 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002097 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2098 }
2099 break;
2100
2101 default:
2102 LOG(FATAL) << "Unexpected type conversion from " << input_type
2103 << " to " << result_type;
2104 }
2105 break;
2106
Roland Levillain01a8d712014-11-14 16:27:39 +00002107 case Primitive::kPrimShort:
2108 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002109 case Primitive::kPrimBoolean:
2110 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002111 case Primitive::kPrimByte:
2112 case Primitive::kPrimInt:
2113 case Primitive::kPrimChar:
2114 // Processing a Dex `int-to-short' instruction.
2115 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002116 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002117 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002119 Address(CpuRegister(RSP), in.GetStackIndex()));
2120 } else {
2121 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002122 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002123 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2124 }
2125 break;
2126
2127 default:
2128 LOG(FATAL) << "Unexpected type conversion from " << input_type
2129 << " to " << result_type;
2130 }
2131 break;
2132
Roland Levillain946e1432014-11-11 17:35:19 +00002133 case Primitive::kPrimInt:
2134 switch (input_type) {
2135 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002136 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002137 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002139 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002140 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002141 Address(CpuRegister(RSP), in.GetStackIndex()));
2142 } else {
2143 DCHECK(in.IsConstant());
2144 DCHECK(in.GetConstant()->IsLongConstant());
2145 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002146 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002147 }
2148 break;
2149
Roland Levillain3f8f9362014-12-02 17:45:01 +00002150 case Primitive::kPrimFloat: {
2151 // Processing a Dex `float-to-int' instruction.
2152 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2153 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002154 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002155
2156 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002157 // if input >= (float)INT_MAX goto done
2158 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002159 __ j(kAboveEqual, &done);
2160 // if input == NaN goto nan
2161 __ j(kUnordered, &nan);
2162 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002163 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002164 __ jmp(&done);
2165 __ Bind(&nan);
2166 // output = 0
2167 __ xorl(output, output);
2168 __ Bind(&done);
2169 break;
2170 }
2171
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002172 case Primitive::kPrimDouble: {
2173 // Processing a Dex `double-to-int' instruction.
2174 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2175 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002176 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002177
2178 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002179 // if input >= (double)INT_MAX goto done
2180 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002181 __ j(kAboveEqual, &done);
2182 // if input == NaN goto nan
2183 __ j(kUnordered, &nan);
2184 // output = double-to-int-truncate(input)
2185 __ cvttsd2si(output, input);
2186 __ jmp(&done);
2187 __ Bind(&nan);
2188 // output = 0
2189 __ xorl(output, output);
2190 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002191 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002192 }
Roland Levillain946e1432014-11-11 17:35:19 +00002193
2194 default:
2195 LOG(FATAL) << "Unexpected type conversion from " << input_type
2196 << " to " << result_type;
2197 }
2198 break;
2199
Roland Levillaindff1f282014-11-05 14:15:05 +00002200 case Primitive::kPrimLong:
2201 switch (input_type) {
2202 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002203 case Primitive::kPrimBoolean:
2204 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002205 case Primitive::kPrimByte:
2206 case Primitive::kPrimShort:
2207 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002208 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002209 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002210 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002211 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002212 break;
2213
Roland Levillain624279f2014-12-04 11:54:28 +00002214 case Primitive::kPrimFloat: {
2215 // Processing a Dex `float-to-long' instruction.
2216 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2217 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002218 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002219
Mark Mendell92e83bf2015-05-07 11:25:03 -04002220 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002221 // if input >= (float)LONG_MAX goto done
2222 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002223 __ j(kAboveEqual, &done);
2224 // if input == NaN goto nan
2225 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002226 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002227 __ cvttss2si(output, input, true);
2228 __ jmp(&done);
2229 __ Bind(&nan);
2230 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002231 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002232 __ Bind(&done);
2233 break;
2234 }
2235
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002236 case Primitive::kPrimDouble: {
2237 // Processing a Dex `double-to-long' instruction.
2238 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2239 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002240 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002241
Mark Mendell92e83bf2015-05-07 11:25:03 -04002242 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002243 // if input >= (double)LONG_MAX goto done
2244 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002245 __ j(kAboveEqual, &done);
2246 // if input == NaN goto nan
2247 __ j(kUnordered, &nan);
2248 // output = double-to-long-truncate(input)
2249 __ cvttsd2si(output, input, true);
2250 __ jmp(&done);
2251 __ Bind(&nan);
2252 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002253 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002254 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002255 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002256 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002257
2258 default:
2259 LOG(FATAL) << "Unexpected type conversion from " << input_type
2260 << " to " << result_type;
2261 }
2262 break;
2263
Roland Levillain981e4542014-11-14 11:47:14 +00002264 case Primitive::kPrimChar:
2265 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002266 case Primitive::kPrimBoolean:
2267 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002268 case Primitive::kPrimByte:
2269 case Primitive::kPrimShort:
2270 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002271 // Processing a Dex `int-to-char' instruction.
2272 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002273 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002274 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002275 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002276 Address(CpuRegister(RSP), in.GetStackIndex()));
2277 } else {
2278 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002279 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002280 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2281 }
2282 break;
2283
2284 default:
2285 LOG(FATAL) << "Unexpected type conversion from " << input_type
2286 << " to " << result_type;
2287 }
2288 break;
2289
Roland Levillaindff1f282014-11-05 14:15:05 +00002290 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002291 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002292 case Primitive::kPrimBoolean:
2293 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002294 case Primitive::kPrimByte:
2295 case Primitive::kPrimShort:
2296 case Primitive::kPrimInt:
2297 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002298 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002299 if (in.IsRegister()) {
2300 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2301 } else if (in.IsConstant()) {
2302 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2303 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2304 if (v == 0) {
2305 __ xorps(dest, dest);
2306 } else {
2307 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2308 }
2309 } else {
2310 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2311 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2312 }
Roland Levillaincff13742014-11-17 14:32:17 +00002313 break;
2314
2315 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002316 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002317 if (in.IsRegister()) {
2318 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2319 } else if (in.IsConstant()) {
2320 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2321 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2322 if (v == 0) {
2323 __ xorps(dest, dest);
2324 } else {
2325 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2326 }
2327 } else {
2328 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2329 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2330 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002331 break;
2332
Roland Levillaincff13742014-11-17 14:32:17 +00002333 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002334 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002335 if (in.IsFpuRegister()) {
2336 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2337 } else if (in.IsConstant()) {
2338 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2339 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2340 if (bit_cast<int64_t, double>(v) == 0) {
2341 __ xorps(dest, dest);
2342 } else {
2343 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2344 }
2345 } else {
2346 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2347 Address(CpuRegister(RSP), in.GetStackIndex()));
2348 }
Roland Levillaincff13742014-11-17 14:32:17 +00002349 break;
2350
2351 default:
2352 LOG(FATAL) << "Unexpected type conversion from " << input_type
2353 << " to " << result_type;
2354 };
2355 break;
2356
Roland Levillaindff1f282014-11-05 14:15:05 +00002357 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002358 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002359 case Primitive::kPrimBoolean:
2360 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002361 case Primitive::kPrimByte:
2362 case Primitive::kPrimShort:
2363 case Primitive::kPrimInt:
2364 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002365 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002366 if (in.IsRegister()) {
2367 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2368 } else if (in.IsConstant()) {
2369 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2370 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2371 if (v == 0) {
2372 __ xorpd(dest, dest);
2373 } else {
2374 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2375 }
2376 } else {
2377 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2378 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2379 }
Roland Levillaincff13742014-11-17 14:32:17 +00002380 break;
2381
2382 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002383 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002384 if (in.IsRegister()) {
2385 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2386 } else if (in.IsConstant()) {
2387 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2388 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2389 if (v == 0) {
2390 __ xorpd(dest, dest);
2391 } else {
2392 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2393 }
2394 } else {
2395 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2396 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2397 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002398 break;
2399
Roland Levillaincff13742014-11-17 14:32:17 +00002400 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002401 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002402 if (in.IsFpuRegister()) {
2403 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2404 } else if (in.IsConstant()) {
2405 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2406 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2407 if (bit_cast<int32_t, float>(v) == 0) {
2408 __ xorpd(dest, dest);
2409 } else {
2410 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2411 }
2412 } else {
2413 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2414 Address(CpuRegister(RSP), in.GetStackIndex()));
2415 }
Roland Levillaincff13742014-11-17 14:32:17 +00002416 break;
2417
2418 default:
2419 LOG(FATAL) << "Unexpected type conversion from " << input_type
2420 << " to " << result_type;
2421 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002422 break;
2423
2424 default:
2425 LOG(FATAL) << "Unexpected type conversion from " << input_type
2426 << " to " << result_type;
2427 }
2428}
2429
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002430void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002431 LocationSummary* locations =
2432 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002433 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002434 case Primitive::kPrimInt: {
2435 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002436 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2437 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002438 break;
2439 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002440
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002441 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002442 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002443 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002444 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002445 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002446 break;
2447 }
2448
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002449 case Primitive::kPrimDouble:
2450 case Primitive::kPrimFloat: {
2451 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002452 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002453 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002454 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002455 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002456
2457 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002458 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002459 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002460}
2461
2462void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2463 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002464 Location first = locations->InAt(0);
2465 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002466 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002467
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002468 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002469 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002470 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002471 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2472 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002473 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2474 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002475 } else {
2476 __ leal(out.AsRegister<CpuRegister>(), Address(
2477 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2478 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002479 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002480 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2481 __ addl(out.AsRegister<CpuRegister>(),
2482 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2483 } else {
2484 __ leal(out.AsRegister<CpuRegister>(), Address(
2485 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2486 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002487 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002488 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002489 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002490 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002491 break;
2492 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002493
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002494 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002495 if (second.IsRegister()) {
2496 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2497 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002498 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2499 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002500 } else {
2501 __ leaq(out.AsRegister<CpuRegister>(), Address(
2502 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2503 }
2504 } else {
2505 DCHECK(second.IsConstant());
2506 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2507 int32_t int32_value = Low32Bits(value);
2508 DCHECK_EQ(int32_value, value);
2509 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2510 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2511 } else {
2512 __ leaq(out.AsRegister<CpuRegister>(), Address(
2513 first.AsRegister<CpuRegister>(), int32_value));
2514 }
2515 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002516 break;
2517 }
2518
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002519 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002520 if (second.IsFpuRegister()) {
2521 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2522 } else if (second.IsConstant()) {
2523 __ addss(first.AsFpuRegister<XmmRegister>(),
2524 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2525 } else {
2526 DCHECK(second.IsStackSlot());
2527 __ addss(first.AsFpuRegister<XmmRegister>(),
2528 Address(CpuRegister(RSP), second.GetStackIndex()));
2529 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002530 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002531 }
2532
2533 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002534 if (second.IsFpuRegister()) {
2535 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2536 } else if (second.IsConstant()) {
2537 __ addsd(first.AsFpuRegister<XmmRegister>(),
2538 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2539 } else {
2540 DCHECK(second.IsDoubleStackSlot());
2541 __ addsd(first.AsFpuRegister<XmmRegister>(),
2542 Address(CpuRegister(RSP), second.GetStackIndex()));
2543 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002544 break;
2545 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002546
2547 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002548 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002549 }
2550}
2551
2552void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002553 LocationSummary* locations =
2554 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002555 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002556 case Primitive::kPrimInt: {
2557 locations->SetInAt(0, Location::RequiresRegister());
2558 locations->SetInAt(1, Location::Any());
2559 locations->SetOut(Location::SameAsFirstInput());
2560 break;
2561 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002562 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002563 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002564 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002565 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002566 break;
2567 }
Calin Juravle11351682014-10-23 15:38:15 +01002568 case Primitive::kPrimFloat:
2569 case Primitive::kPrimDouble: {
2570 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002571 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002572 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002573 break;
Calin Juravle11351682014-10-23 15:38:15 +01002574 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002575 default:
Calin Juravle11351682014-10-23 15:38:15 +01002576 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002577 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002578}
2579
2580void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2581 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002582 Location first = locations->InAt(0);
2583 Location second = locations->InAt(1);
2584 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002585 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002586 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002587 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002588 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002589 } else if (second.IsConstant()) {
2590 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002591 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002592 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002594 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002595 break;
2596 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002597 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002598 if (second.IsConstant()) {
2599 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2600 DCHECK(IsInt<32>(value));
2601 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2602 } else {
2603 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2604 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002605 break;
2606 }
2607
Calin Juravle11351682014-10-23 15:38:15 +01002608 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002609 if (second.IsFpuRegister()) {
2610 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2611 } else if (second.IsConstant()) {
2612 __ subss(first.AsFpuRegister<XmmRegister>(),
2613 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2614 } else {
2615 DCHECK(second.IsStackSlot());
2616 __ subss(first.AsFpuRegister<XmmRegister>(),
2617 Address(CpuRegister(RSP), second.GetStackIndex()));
2618 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002619 break;
Calin Juravle11351682014-10-23 15:38:15 +01002620 }
2621
2622 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002623 if (second.IsFpuRegister()) {
2624 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2625 } else if (second.IsConstant()) {
2626 __ subsd(first.AsFpuRegister<XmmRegister>(),
2627 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2628 } else {
2629 DCHECK(second.IsDoubleStackSlot());
2630 __ subsd(first.AsFpuRegister<XmmRegister>(),
2631 Address(CpuRegister(RSP), second.GetStackIndex()));
2632 }
Calin Juravle11351682014-10-23 15:38:15 +01002633 break;
2634 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002635
2636 default:
Calin Juravle11351682014-10-23 15:38:15 +01002637 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002638 }
2639}
2640
Calin Juravle34bacdf2014-10-07 20:23:36 +01002641void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2642 LocationSummary* locations =
2643 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2644 switch (mul->GetResultType()) {
2645 case Primitive::kPrimInt: {
2646 locations->SetInAt(0, Location::RequiresRegister());
2647 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002648 if (mul->InputAt(1)->IsIntConstant()) {
2649 // Can use 3 operand multiply.
2650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2651 } else {
2652 locations->SetOut(Location::SameAsFirstInput());
2653 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002654 break;
2655 }
2656 case Primitive::kPrimLong: {
2657 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002658 locations->SetInAt(1, Location::Any());
2659 if (mul->InputAt(1)->IsLongConstant() &&
2660 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002661 // Can use 3 operand multiply.
2662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2663 } else {
2664 locations->SetOut(Location::SameAsFirstInput());
2665 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002666 break;
2667 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002668 case Primitive::kPrimFloat:
2669 case Primitive::kPrimDouble: {
2670 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002671 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002672 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002673 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002674 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002675
2676 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002677 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002678 }
2679}
2680
2681void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2682 LocationSummary* locations = mul->GetLocations();
2683 Location first = locations->InAt(0);
2684 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002685 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002686 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002687 case Primitive::kPrimInt:
2688 // The constant may have ended up in a register, so test explicitly to avoid
2689 // problems where the output may not be the same as the first operand.
2690 if (mul->InputAt(1)->IsIntConstant()) {
2691 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2692 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2693 } else if (second.IsRegister()) {
2694 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002695 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002696 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002697 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002698 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002699 __ imull(first.AsRegister<CpuRegister>(),
2700 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002701 }
2702 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002703 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002704 // The constant may have ended up in a register, so test explicitly to avoid
2705 // problems where the output may not be the same as the first operand.
2706 if (mul->InputAt(1)->IsLongConstant()) {
2707 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2708 if (IsInt<32>(value)) {
2709 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2710 Immediate(static_cast<int32_t>(value)));
2711 } else {
2712 // Have to use the constant area.
2713 DCHECK(first.Equals(out));
2714 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2715 }
2716 } else if (second.IsRegister()) {
2717 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002718 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002719 } else {
2720 DCHECK(second.IsDoubleStackSlot());
2721 DCHECK(first.Equals(out));
2722 __ imulq(first.AsRegister<CpuRegister>(),
2723 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002724 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002725 break;
2726 }
2727
Calin Juravleb5bfa962014-10-21 18:02:24 +01002728 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002729 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002730 if (second.IsFpuRegister()) {
2731 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2732 } else if (second.IsConstant()) {
2733 __ mulss(first.AsFpuRegister<XmmRegister>(),
2734 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2735 } else {
2736 DCHECK(second.IsStackSlot());
2737 __ mulss(first.AsFpuRegister<XmmRegister>(),
2738 Address(CpuRegister(RSP), second.GetStackIndex()));
2739 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002740 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002741 }
2742
2743 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002744 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002745 if (second.IsFpuRegister()) {
2746 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2747 } else if (second.IsConstant()) {
2748 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2749 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2750 } else {
2751 DCHECK(second.IsDoubleStackSlot());
2752 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2753 Address(CpuRegister(RSP), second.GetStackIndex()));
2754 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002755 break;
2756 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002757
2758 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002759 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002760 }
2761}
2762
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002763void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2764 uint32_t stack_adjustment, bool is_float) {
2765 if (source.IsStackSlot()) {
2766 DCHECK(is_float);
2767 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2768 } else if (source.IsDoubleStackSlot()) {
2769 DCHECK(!is_float);
2770 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2771 } else {
2772 // Write the value to the temporary location on the stack and load to FP stack.
2773 if (is_float) {
2774 Location stack_temp = Location::StackSlot(temp_offset);
2775 codegen_->Move(stack_temp, source);
2776 __ flds(Address(CpuRegister(RSP), temp_offset));
2777 } else {
2778 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2779 codegen_->Move(stack_temp, source);
2780 __ fldl(Address(CpuRegister(RSP), temp_offset));
2781 }
2782 }
2783}
2784
2785void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2786 Primitive::Type type = rem->GetResultType();
2787 bool is_float = type == Primitive::kPrimFloat;
2788 size_t elem_size = Primitive::ComponentSize(type);
2789 LocationSummary* locations = rem->GetLocations();
2790 Location first = locations->InAt(0);
2791 Location second = locations->InAt(1);
2792 Location out = locations->Out();
2793
2794 // Create stack space for 2 elements.
2795 // TODO: enhance register allocator to ask for stack temporaries.
2796 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2797
2798 // Load the values to the FP stack in reverse order, using temporaries if needed.
2799 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2800 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2801
2802 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002803 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002804 __ Bind(&retry);
2805 __ fprem();
2806
2807 // Move FP status to AX.
2808 __ fstsw();
2809
2810 // And see if the argument reduction is complete. This is signaled by the
2811 // C2 FPU flag bit set to 0.
2812 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2813 __ j(kNotEqual, &retry);
2814
2815 // We have settled on the final value. Retrieve it into an XMM register.
2816 // Store FP top of stack to real stack.
2817 if (is_float) {
2818 __ fsts(Address(CpuRegister(RSP), 0));
2819 } else {
2820 __ fstl(Address(CpuRegister(RSP), 0));
2821 }
2822
2823 // Pop the 2 items from the FP stack.
2824 __ fucompp();
2825
2826 // Load the value from the stack into an XMM register.
2827 DCHECK(out.IsFpuRegister()) << out;
2828 if (is_float) {
2829 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2830 } else {
2831 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2832 }
2833
2834 // And remove the temporary stack space we allocated.
2835 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2836}
2837
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002838void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2839 DCHECK(instruction->IsDiv() || instruction->IsRem());
2840
2841 LocationSummary* locations = instruction->GetLocations();
2842 Location second = locations->InAt(1);
2843 DCHECK(second.IsConstant());
2844
2845 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2846 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002847 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002848
2849 DCHECK(imm == 1 || imm == -1);
2850
2851 switch (instruction->GetResultType()) {
2852 case Primitive::kPrimInt: {
2853 if (instruction->IsRem()) {
2854 __ xorl(output_register, output_register);
2855 } else {
2856 __ movl(output_register, input_register);
2857 if (imm == -1) {
2858 __ negl(output_register);
2859 }
2860 }
2861 break;
2862 }
2863
2864 case Primitive::kPrimLong: {
2865 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002866 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002867 } else {
2868 __ movq(output_register, input_register);
2869 if (imm == -1) {
2870 __ negq(output_register);
2871 }
2872 }
2873 break;
2874 }
2875
2876 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002877 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002878 }
2879}
2880
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002881void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002882 LocationSummary* locations = instruction->GetLocations();
2883 Location second = locations->InAt(1);
2884
2885 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2886 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2887
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002888 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002889
2890 DCHECK(IsPowerOfTwo(std::abs(imm)));
2891
2892 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2893
2894 if (instruction->GetResultType() == Primitive::kPrimInt) {
2895 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2896 __ testl(numerator, numerator);
2897 __ cmov(kGreaterEqual, tmp, numerator);
2898 int shift = CTZ(imm);
2899 __ sarl(tmp, Immediate(shift));
2900
2901 if (imm < 0) {
2902 __ negl(tmp);
2903 }
2904
2905 __ movl(output_register, tmp);
2906 } else {
2907 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2908 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2909
Mark Mendell92e83bf2015-05-07 11:25:03 -04002910 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002911 __ addq(rdx, numerator);
2912 __ testq(numerator, numerator);
2913 __ cmov(kGreaterEqual, rdx, numerator);
2914 int shift = CTZ(imm);
2915 __ sarq(rdx, Immediate(shift));
2916
2917 if (imm < 0) {
2918 __ negq(rdx);
2919 }
2920
2921 __ movq(output_register, rdx);
2922 }
2923}
2924
2925void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2926 DCHECK(instruction->IsDiv() || instruction->IsRem());
2927
2928 LocationSummary* locations = instruction->GetLocations();
2929 Location second = locations->InAt(1);
2930
2931 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2932 : locations->GetTemp(0).AsRegister<CpuRegister>();
2933 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2934 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2935 : locations->Out().AsRegister<CpuRegister>();
2936 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2937
2938 DCHECK_EQ(RAX, eax.AsRegister());
2939 DCHECK_EQ(RDX, edx.AsRegister());
2940 if (instruction->IsDiv()) {
2941 DCHECK_EQ(RAX, out.AsRegister());
2942 } else {
2943 DCHECK_EQ(RDX, out.AsRegister());
2944 }
2945
2946 int64_t magic;
2947 int shift;
2948
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002949 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002950 if (instruction->GetResultType() == Primitive::kPrimInt) {
2951 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2952
2953 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2954
2955 __ movl(numerator, eax);
2956
Mark Mendell0c9497d2015-08-21 09:30:05 -04002957 NearLabel no_div;
2958 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002959 __ testl(eax, eax);
2960 __ j(kNotEqual, &no_div);
2961
2962 __ xorl(out, out);
2963 __ jmp(&end);
2964
2965 __ Bind(&no_div);
2966
2967 __ movl(eax, Immediate(magic));
2968 __ imull(numerator);
2969
2970 if (imm > 0 && magic < 0) {
2971 __ addl(edx, numerator);
2972 } else if (imm < 0 && magic > 0) {
2973 __ subl(edx, numerator);
2974 }
2975
2976 if (shift != 0) {
2977 __ sarl(edx, Immediate(shift));
2978 }
2979
2980 __ movl(eax, edx);
2981 __ shrl(edx, Immediate(31));
2982 __ addl(edx, eax);
2983
2984 if (instruction->IsRem()) {
2985 __ movl(eax, numerator);
2986 __ imull(edx, Immediate(imm));
2987 __ subl(eax, edx);
2988 __ movl(edx, eax);
2989 } else {
2990 __ movl(eax, edx);
2991 }
2992 __ Bind(&end);
2993 } else {
2994 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2995
2996 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2997
2998 CpuRegister rax = eax;
2999 CpuRegister rdx = edx;
3000
3001 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3002
3003 // Save the numerator.
3004 __ movq(numerator, rax);
3005
3006 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003007 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003008
3009 // RDX:RAX = magic * numerator
3010 __ imulq(numerator);
3011
3012 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003013 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003014 __ addq(rdx, numerator);
3015 } else if (imm < 0 && magic > 0) {
3016 // RDX -= numerator
3017 __ subq(rdx, numerator);
3018 }
3019
3020 // Shift if needed.
3021 if (shift != 0) {
3022 __ sarq(rdx, Immediate(shift));
3023 }
3024
3025 // RDX += 1 if RDX < 0
3026 __ movq(rax, rdx);
3027 __ shrq(rdx, Immediate(63));
3028 __ addq(rdx, rax);
3029
3030 if (instruction->IsRem()) {
3031 __ movq(rax, numerator);
3032
3033 if (IsInt<32>(imm)) {
3034 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3035 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003036 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003037 }
3038
3039 __ subq(rax, rdx);
3040 __ movq(rdx, rax);
3041 } else {
3042 __ movq(rax, rdx);
3043 }
3044 }
3045}
3046
Calin Juravlebacfec32014-11-14 15:54:36 +00003047void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3048 DCHECK(instruction->IsDiv() || instruction->IsRem());
3049 Primitive::Type type = instruction->GetResultType();
3050 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3051
3052 bool is_div = instruction->IsDiv();
3053 LocationSummary* locations = instruction->GetLocations();
3054
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003055 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3056 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003057
Roland Levillain271ab9c2014-11-27 15:23:57 +00003058 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003059 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003060
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003061 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003062 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003063
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003064 if (imm == 0) {
3065 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3066 } else if (imm == 1 || imm == -1) {
3067 DivRemOneOrMinusOne(instruction);
3068 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003069 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003070 } else {
3071 DCHECK(imm <= -2 || imm >= 2);
3072 GenerateDivRemWithAnyConstant(instruction);
3073 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003074 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003075 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003076 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3077 out.AsRegister(), type, is_div);
3078 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003079
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003080 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3081 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3082 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3083 // so it's safe to just use negl instead of more complex comparisons.
3084 if (type == Primitive::kPrimInt) {
3085 __ cmpl(second_reg, Immediate(-1));
3086 __ j(kEqual, slow_path->GetEntryLabel());
3087 // edx:eax <- sign-extended of eax
3088 __ cdq();
3089 // eax = quotient, edx = remainder
3090 __ idivl(second_reg);
3091 } else {
3092 __ cmpq(second_reg, Immediate(-1));
3093 __ j(kEqual, slow_path->GetEntryLabel());
3094 // rdx:rax <- sign-extended of rax
3095 __ cqo();
3096 // rax = quotient, rdx = remainder
3097 __ idivq(second_reg);
3098 }
3099 __ Bind(slow_path->GetExitLabel());
3100 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003101}
3102
Calin Juravle7c4954d2014-10-28 16:57:40 +00003103void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3104 LocationSummary* locations =
3105 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3106 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003107 case Primitive::kPrimInt:
3108 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003109 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003110 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003111 locations->SetOut(Location::SameAsFirstInput());
3112 // Intel uses edx:eax as the dividend.
3113 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003114 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3115 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3116 // output and request another temp.
3117 if (div->InputAt(1)->IsConstant()) {
3118 locations->AddTemp(Location::RequiresRegister());
3119 }
Calin Juravled0d48522014-11-04 16:40:20 +00003120 break;
3121 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003122
Calin Juravle7c4954d2014-10-28 16:57:40 +00003123 case Primitive::kPrimFloat:
3124 case Primitive::kPrimDouble: {
3125 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003126 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003127 locations->SetOut(Location::SameAsFirstInput());
3128 break;
3129 }
3130
3131 default:
3132 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3133 }
3134}
3135
3136void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3137 LocationSummary* locations = div->GetLocations();
3138 Location first = locations->InAt(0);
3139 Location second = locations->InAt(1);
3140 DCHECK(first.Equals(locations->Out()));
3141
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003142 Primitive::Type type = div->GetResultType();
3143 switch (type) {
3144 case Primitive::kPrimInt:
3145 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003146 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003147 break;
3148 }
3149
Calin Juravle7c4954d2014-10-28 16:57:40 +00003150 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003151 if (second.IsFpuRegister()) {
3152 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3153 } else if (second.IsConstant()) {
3154 __ divss(first.AsFpuRegister<XmmRegister>(),
3155 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3156 } else {
3157 DCHECK(second.IsStackSlot());
3158 __ divss(first.AsFpuRegister<XmmRegister>(),
3159 Address(CpuRegister(RSP), second.GetStackIndex()));
3160 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003161 break;
3162 }
3163
3164 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003165 if (second.IsFpuRegister()) {
3166 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3167 } else if (second.IsConstant()) {
3168 __ divsd(first.AsFpuRegister<XmmRegister>(),
3169 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3170 } else {
3171 DCHECK(second.IsDoubleStackSlot());
3172 __ divsd(first.AsFpuRegister<XmmRegister>(),
3173 Address(CpuRegister(RSP), second.GetStackIndex()));
3174 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003175 break;
3176 }
3177
3178 default:
3179 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3180 }
3181}
3182
Calin Juravlebacfec32014-11-14 15:54:36 +00003183void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003184 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003185 LocationSummary* locations =
3186 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003187
3188 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003189 case Primitive::kPrimInt:
3190 case Primitive::kPrimLong: {
3191 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003192 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003193 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3194 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003195 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3196 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3197 // output and request another temp.
3198 if (rem->InputAt(1)->IsConstant()) {
3199 locations->AddTemp(Location::RequiresRegister());
3200 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003201 break;
3202 }
3203
3204 case Primitive::kPrimFloat:
3205 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003206 locations->SetInAt(0, Location::Any());
3207 locations->SetInAt(1, Location::Any());
3208 locations->SetOut(Location::RequiresFpuRegister());
3209 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003210 break;
3211 }
3212
3213 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003214 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003215 }
3216}
3217
3218void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3219 Primitive::Type type = rem->GetResultType();
3220 switch (type) {
3221 case Primitive::kPrimInt:
3222 case Primitive::kPrimLong: {
3223 GenerateDivRemIntegral(rem);
3224 break;
3225 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003226 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003227 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003228 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003229 break;
3230 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003231 default:
3232 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3233 }
3234}
3235
Calin Juravled0d48522014-11-04 16:40:20 +00003236void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003237 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3238 ? LocationSummary::kCallOnSlowPath
3239 : LocationSummary::kNoCall;
3240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003241 locations->SetInAt(0, Location::Any());
3242 if (instruction->HasUses()) {
3243 locations->SetOut(Location::SameAsFirstInput());
3244 }
3245}
3246
3247void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003248 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003249 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3250 codegen_->AddSlowPath(slow_path);
3251
3252 LocationSummary* locations = instruction->GetLocations();
3253 Location value = locations->InAt(0);
3254
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003255 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003256 case Primitive::kPrimByte:
3257 case Primitive::kPrimChar:
3258 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003259 case Primitive::kPrimInt: {
3260 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003261 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003262 __ j(kEqual, slow_path->GetEntryLabel());
3263 } else if (value.IsStackSlot()) {
3264 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3265 __ j(kEqual, slow_path->GetEntryLabel());
3266 } else {
3267 DCHECK(value.IsConstant()) << value;
3268 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3269 __ jmp(slow_path->GetEntryLabel());
3270 }
3271 }
3272 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003273 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003274 case Primitive::kPrimLong: {
3275 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003276 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003277 __ j(kEqual, slow_path->GetEntryLabel());
3278 } else if (value.IsDoubleStackSlot()) {
3279 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3280 __ j(kEqual, slow_path->GetEntryLabel());
3281 } else {
3282 DCHECK(value.IsConstant()) << value;
3283 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3284 __ jmp(slow_path->GetEntryLabel());
3285 }
3286 }
3287 break;
3288 }
3289 default:
3290 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003291 }
Calin Juravled0d48522014-11-04 16:40:20 +00003292}
3293
Calin Juravle9aec02f2014-11-18 23:06:35 +00003294void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3295 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3296
3297 LocationSummary* locations =
3298 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3299
3300 switch (op->GetResultType()) {
3301 case Primitive::kPrimInt:
3302 case Primitive::kPrimLong: {
3303 locations->SetInAt(0, Location::RequiresRegister());
3304 // The shift count needs to be in CL.
3305 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3306 locations->SetOut(Location::SameAsFirstInput());
3307 break;
3308 }
3309 default:
3310 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3311 }
3312}
3313
3314void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3315 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3316
3317 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003318 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003319 Location second = locations->InAt(1);
3320
3321 switch (op->GetResultType()) {
3322 case Primitive::kPrimInt: {
3323 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003324 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003325 if (op->IsShl()) {
3326 __ shll(first_reg, second_reg);
3327 } else if (op->IsShr()) {
3328 __ sarl(first_reg, second_reg);
3329 } else {
3330 __ shrl(first_reg, second_reg);
3331 }
3332 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003333 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003334 if (op->IsShl()) {
3335 __ shll(first_reg, imm);
3336 } else if (op->IsShr()) {
3337 __ sarl(first_reg, imm);
3338 } else {
3339 __ shrl(first_reg, imm);
3340 }
3341 }
3342 break;
3343 }
3344 case Primitive::kPrimLong: {
3345 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003346 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003347 if (op->IsShl()) {
3348 __ shlq(first_reg, second_reg);
3349 } else if (op->IsShr()) {
3350 __ sarq(first_reg, second_reg);
3351 } else {
3352 __ shrq(first_reg, second_reg);
3353 }
3354 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003355 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003356 if (op->IsShl()) {
3357 __ shlq(first_reg, imm);
3358 } else if (op->IsShr()) {
3359 __ sarq(first_reg, imm);
3360 } else {
3361 __ shrq(first_reg, imm);
3362 }
3363 }
3364 break;
3365 }
3366 default:
3367 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3368 }
3369}
3370
3371void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3372 HandleShift(shl);
3373}
3374
3375void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3376 HandleShift(shl);
3377}
3378
3379void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3380 HandleShift(shr);
3381}
3382
3383void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3384 HandleShift(shr);
3385}
3386
3387void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3388 HandleShift(ushr);
3389}
3390
3391void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3392 HandleShift(ushr);
3393}
3394
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003395void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003396 LocationSummary* locations =
3397 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003398 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003399 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003400 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003401 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003402}
3403
3404void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3405 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003406 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3407 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003408 // Note: if heap poisoning is enabled, the entry point takes cares
3409 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003410
Calin Juravle175dc732015-08-25 15:42:32 +01003411 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3412 instruction,
3413 instruction->GetDexPc(),
3414 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003415
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003416 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003417}
3418
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003419void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3420 LocationSummary* locations =
3421 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3422 InvokeRuntimeCallingConvention calling_convention;
3423 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003424 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003425 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003426 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003427}
3428
3429void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3430 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003431 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3432 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003433
Roland Levillain4d027112015-07-01 15:41:14 +01003434 // Note: if heap poisoning is enabled, the entry point takes cares
3435 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003436 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3437 instruction,
3438 instruction->GetDexPc(),
3439 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003440
3441 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003442}
3443
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003444void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003445 LocationSummary* locations =
3446 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003447 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3448 if (location.IsStackSlot()) {
3449 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3450 } else if (location.IsDoubleStackSlot()) {
3451 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3452 }
3453 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003454}
3455
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003456void InstructionCodeGeneratorX86_64::VisitParameterValue(
3457 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003458 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003459}
3460
3461void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3462 LocationSummary* locations =
3463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3464 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3465}
3466
3467void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3468 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3469 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003470}
3471
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003472void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003473 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003474 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003475 locations->SetInAt(0, Location::RequiresRegister());
3476 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003477}
3478
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003479void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3480 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3482 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003483 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003484 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003485 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003486 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003487 break;
3488
3489 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003490 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003491 break;
3492
3493 default:
3494 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3495 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003496}
3497
David Brazdil66d126e2015-04-03 16:02:44 +01003498void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3499 LocationSummary* locations =
3500 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3501 locations->SetInAt(0, Location::RequiresRegister());
3502 locations->SetOut(Location::SameAsFirstInput());
3503}
3504
3505void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003506 LocationSummary* locations = bool_not->GetLocations();
3507 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3508 locations->Out().AsRegister<CpuRegister>().AsRegister());
3509 Location out = locations->Out();
3510 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3511}
3512
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003513void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003514 LocationSummary* locations =
3515 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003516 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3517 locations->SetInAt(i, Location::Any());
3518 }
3519 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003520}
3521
3522void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003523 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003524 LOG(FATAL) << "Unimplemented";
3525}
3526
Calin Juravle52c48962014-12-16 17:02:57 +00003527void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3528 /*
3529 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3530 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3531 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3532 */
3533 switch (kind) {
3534 case MemBarrierKind::kAnyAny: {
3535 __ mfence();
3536 break;
3537 }
3538 case MemBarrierKind::kAnyStore:
3539 case MemBarrierKind::kLoadAny:
3540 case MemBarrierKind::kStoreStore: {
3541 // nop
3542 break;
3543 }
3544 default:
3545 LOG(FATAL) << "Unexpected memory barier " << kind;
3546 }
3547}
3548
3549void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3550 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3551
Nicolas Geoffray39468442014-09-02 15:17:15 +01003552 LocationSummary* locations =
3553 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003554 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003555 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3556 locations->SetOut(Location::RequiresFpuRegister());
3557 } else {
3558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3559 }
Calin Juravle52c48962014-12-16 17:02:57 +00003560}
3561
3562void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3563 const FieldInfo& field_info) {
3564 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3565
3566 LocationSummary* locations = instruction->GetLocations();
3567 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3568 Location out = locations->Out();
3569 bool is_volatile = field_info.IsVolatile();
3570 Primitive::Type field_type = field_info.GetFieldType();
3571 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3572
3573 switch (field_type) {
3574 case Primitive::kPrimBoolean: {
3575 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3576 break;
3577 }
3578
3579 case Primitive::kPrimByte: {
3580 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3581 break;
3582 }
3583
3584 case Primitive::kPrimShort: {
3585 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3586 break;
3587 }
3588
3589 case Primitive::kPrimChar: {
3590 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3591 break;
3592 }
3593
3594 case Primitive::kPrimInt:
3595 case Primitive::kPrimNot: {
3596 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3597 break;
3598 }
3599
3600 case Primitive::kPrimLong: {
3601 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3602 break;
3603 }
3604
3605 case Primitive::kPrimFloat: {
3606 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3607 break;
3608 }
3609
3610 case Primitive::kPrimDouble: {
3611 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3612 break;
3613 }
3614
3615 case Primitive::kPrimVoid:
3616 LOG(FATAL) << "Unreachable type " << field_type;
3617 UNREACHABLE();
3618 }
3619
Calin Juravle77520bc2015-01-12 18:45:46 +00003620 codegen_->MaybeRecordImplicitNullCheck(instruction);
3621
Calin Juravle52c48962014-12-16 17:02:57 +00003622 if (is_volatile) {
3623 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3624 }
Roland Levillain4d027112015-07-01 15:41:14 +01003625
3626 if (field_type == Primitive::kPrimNot) {
3627 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3628 }
Calin Juravle52c48962014-12-16 17:02:57 +00003629}
3630
3631void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3632 const FieldInfo& field_info) {
3633 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3634
3635 LocationSummary* locations =
3636 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003637 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003638 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003639 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003640
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003641 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003642 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3643 locations->SetInAt(1, Location::RequiresFpuRegister());
3644 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003645 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003646 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003647 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003648 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003649 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003650 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003651 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3652 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003653 locations->AddTemp(Location::RequiresRegister());
3654 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003655}
3656
Calin Juravle52c48962014-12-16 17:02:57 +00003657void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003658 const FieldInfo& field_info,
3659 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003660 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3661
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003662 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003663 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3664 Location value = locations->InAt(1);
3665 bool is_volatile = field_info.IsVolatile();
3666 Primitive::Type field_type = field_info.GetFieldType();
3667 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3668
3669 if (is_volatile) {
3670 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3671 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003672
3673 switch (field_type) {
3674 case Primitive::kPrimBoolean:
3675 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003676 if (value.IsConstant()) {
3677 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3678 __ movb(Address(base, offset), Immediate(v));
3679 } else {
3680 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3681 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003682 break;
3683 }
3684
3685 case Primitive::kPrimShort:
3686 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003687 if (value.IsConstant()) {
3688 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3689 __ movw(Address(base, offset), Immediate(v));
3690 } else {
3691 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3692 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003693 break;
3694 }
3695
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003696 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003697 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003698 if (value.IsConstant()) {
3699 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003700 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3701 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3702 // Note: if heap poisoning is enabled, no need to poison
3703 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003704 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003705 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003706 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3707 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3708 __ movl(temp, value.AsRegister<CpuRegister>());
3709 __ PoisonHeapReference(temp);
3710 __ movl(Address(base, offset), temp);
3711 } else {
3712 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3713 }
Mark Mendell40741f32015-04-20 22:10:34 -04003714 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003715 break;
3716 }
3717
3718 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003719 if (value.IsConstant()) {
3720 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3721 DCHECK(IsInt<32>(v));
3722 int32_t v_32 = v;
3723 __ movq(Address(base, offset), Immediate(v_32));
3724 } else {
3725 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3726 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003727 break;
3728 }
3729
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003730 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003731 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003732 break;
3733 }
3734
3735 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003736 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003737 break;
3738 }
3739
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003740 case Primitive::kPrimVoid:
3741 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003742 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003743 }
Calin Juravle52c48962014-12-16 17:02:57 +00003744
Calin Juravle77520bc2015-01-12 18:45:46 +00003745 codegen_->MaybeRecordImplicitNullCheck(instruction);
3746
3747 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3748 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3749 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003750 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003751 }
3752
Calin Juravle52c48962014-12-16 17:02:57 +00003753 if (is_volatile) {
3754 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3755 }
3756}
3757
3758void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3759 HandleFieldSet(instruction, instruction->GetFieldInfo());
3760}
3761
3762void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003763 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003764}
3765
3766void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003767 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003768}
3769
3770void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003771 HandleFieldGet(instruction, instruction->GetFieldInfo());
3772}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003773
Calin Juravle52c48962014-12-16 17:02:57 +00003774void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3775 HandleFieldGet(instruction);
3776}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003777
Calin Juravle52c48962014-12-16 17:02:57 +00003778void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3779 HandleFieldGet(instruction, instruction->GetFieldInfo());
3780}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003781
Calin Juravle52c48962014-12-16 17:02:57 +00003782void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3783 HandleFieldSet(instruction, instruction->GetFieldInfo());
3784}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003785
Calin Juravle52c48962014-12-16 17:02:57 +00003786void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003787 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003788}
3789
3790void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003791 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3792 ? LocationSummary::kCallOnSlowPath
3793 : LocationSummary::kNoCall;
3794 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3795 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003796 ? Location::RequiresRegister()
3797 : Location::Any();
3798 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003799 if (instruction->HasUses()) {
3800 locations->SetOut(Location::SameAsFirstInput());
3801 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003802}
3803
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003804void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003805 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3806 return;
3807 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003808 LocationSummary* locations = instruction->GetLocations();
3809 Location obj = locations->InAt(0);
3810
3811 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3812 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3813}
3814
3815void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003816 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003817 codegen_->AddSlowPath(slow_path);
3818
3819 LocationSummary* locations = instruction->GetLocations();
3820 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003821
3822 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003823 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003824 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003825 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003826 } else {
3827 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00003828 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003829 __ jmp(slow_path->GetEntryLabel());
3830 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003831 }
3832 __ j(kEqual, slow_path->GetEntryLabel());
3833}
3834
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003835void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003836 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003837 GenerateImplicitNullCheck(instruction);
3838 } else {
3839 GenerateExplicitNullCheck(instruction);
3840 }
3841}
3842
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003843void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003844 LocationSummary* locations =
3845 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003846 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003847 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003848 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3849 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3850 } else {
3851 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3852 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003853}
3854
3855void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3856 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003857 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003858 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003859 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003860
Roland Levillain4d027112015-07-01 15:41:14 +01003861 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003862 case Primitive::kPrimBoolean: {
3863 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003864 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003865 if (index.IsConstant()) {
3866 __ movzxb(out, Address(obj,
3867 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3868 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003869 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003870 }
3871 break;
3872 }
3873
3874 case Primitive::kPrimByte: {
3875 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003876 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003877 if (index.IsConstant()) {
3878 __ movsxb(out, Address(obj,
3879 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3880 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003881 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003882 }
3883 break;
3884 }
3885
3886 case Primitive::kPrimShort: {
3887 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003888 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003889 if (index.IsConstant()) {
3890 __ movsxw(out, Address(obj,
3891 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3892 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003893 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003894 }
3895 break;
3896 }
3897
3898 case Primitive::kPrimChar: {
3899 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003900 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003901 if (index.IsConstant()) {
3902 __ movzxw(out, Address(obj,
3903 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3904 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003905 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003906 }
3907 break;
3908 }
3909
3910 case Primitive::kPrimInt:
3911 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003912 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3913 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003914 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003915 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003916 if (index.IsConstant()) {
3917 __ movl(out, Address(obj,
3918 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3919 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003920 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003921 }
3922 break;
3923 }
3924
3925 case Primitive::kPrimLong: {
3926 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003927 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003928 if (index.IsConstant()) {
3929 __ movq(out, Address(obj,
3930 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3931 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003932 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003933 }
3934 break;
3935 }
3936
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003937 case Primitive::kPrimFloat: {
3938 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003939 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003940 if (index.IsConstant()) {
3941 __ movss(out, Address(obj,
3942 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3943 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003944 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003945 }
3946 break;
3947 }
3948
3949 case Primitive::kPrimDouble: {
3950 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003951 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003952 if (index.IsConstant()) {
3953 __ movsd(out, Address(obj,
3954 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3955 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003956 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003957 }
3958 break;
3959 }
3960
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003961 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003962 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003963 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003964 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003965 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003966
3967 if (type == Primitive::kPrimNot) {
3968 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3969 __ MaybeUnpoisonHeapReference(out);
3970 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003971}
3972
3973void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003974 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003975
3976 bool needs_write_barrier =
3977 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3978 bool needs_runtime_call = instruction->NeedsTypeCheck();
3979
Nicolas Geoffray39468442014-09-02 15:17:15 +01003980 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003981 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3982 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003983 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003984 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3985 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3986 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003987 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003988 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003989 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003990 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3991 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003992 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003993 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003994 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3995 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003996 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003997 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003998 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003999
4000 if (needs_write_barrier) {
4001 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004002 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004003 locations->AddTemp(Location::RequiresRegister());
4004 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004005 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004006}
4007
4008void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4009 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004010 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004011 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004012 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004013 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004014 bool needs_runtime_call = locations->WillCall();
4015 bool needs_write_barrier =
4016 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004017
4018 switch (value_type) {
4019 case Primitive::kPrimBoolean:
4020 case Primitive::kPrimByte: {
4021 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004022 if (index.IsConstant()) {
4023 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004024 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004025 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004026 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00004027 __ movb(Address(obj, offset),
4028 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004029 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004030 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004031 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004032 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
4033 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004034 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004035 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004036 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4037 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004038 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004039 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004040 break;
4041 }
4042
4043 case Primitive::kPrimShort:
4044 case Primitive::kPrimChar: {
4045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004046 if (index.IsConstant()) {
4047 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004048 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004049 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004050 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004051 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00004052 __ movw(Address(obj, offset),
4053 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004054 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004055 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004056 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004057 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004058 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
4059 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004060 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004061 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00004062 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004063 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4064 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004065 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004066 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004067 break;
4068 }
4069
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004070 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004071 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004072 if (!needs_runtime_call) {
4073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4074 if (index.IsConstant()) {
4075 size_t offset =
4076 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4077 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004078 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4079 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4080 __ movl(temp, value.AsRegister<CpuRegister>());
4081 __ PoisonHeapReference(temp);
4082 __ movl(Address(obj, offset), temp);
4083 } else {
4084 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
4085 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004086 } else {
4087 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004088 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004089 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4090 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4091 // Note: if heap poisoning is enabled, no need to poison
4092 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04004093 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004094 }
4095 } else {
4096 DCHECK(index.IsRegister()) << index;
4097 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004098 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4099 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4100 __ movl(temp, value.AsRegister<CpuRegister>());
4101 __ PoisonHeapReference(temp);
4102 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4103 } else {
4104 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4105 value.AsRegister<CpuRegister>());
4106 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004107 } else {
4108 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004109 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004110 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4111 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4112 // Note: if heap poisoning is enabled, no need to poison
4113 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004114 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004115 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004116 }
4117 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004118 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004119 if (needs_write_barrier) {
4120 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004121 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4122 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004123 codegen_->MarkGCCard(
4124 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004125 }
4126 } else {
4127 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004128 // Note: if heap poisoning is enabled, pAputObject takes cares
4129 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004130 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4131 instruction,
4132 instruction->GetDexPc(),
4133 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004134 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004135 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004136 break;
4137 }
4138
4139 case Primitive::kPrimLong: {
4140 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004141 if (index.IsConstant()) {
4142 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004143 if (value.IsRegister()) {
4144 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4145 } else {
4146 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4147 DCHECK(IsInt<32>(v));
4148 int32_t v_32 = v;
4149 __ movq(Address(obj, offset), Immediate(v_32));
4150 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004151 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004152 if (value.IsRegister()) {
4153 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4154 value.AsRegister<CpuRegister>());
4155 } else {
4156 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4157 DCHECK(IsInt<32>(v));
4158 int32_t v_32 = v;
4159 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4160 Immediate(v_32));
4161 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004162 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004163 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004164 break;
4165 }
4166
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004167 case Primitive::kPrimFloat: {
4168 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4169 if (index.IsConstant()) {
4170 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4171 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004172 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004173 } else {
4174 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004175 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4176 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004177 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004178 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004179 break;
4180 }
4181
4182 case Primitive::kPrimDouble: {
4183 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4184 if (index.IsConstant()) {
4185 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4186 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004187 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004188 } else {
4189 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004190 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4191 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004192 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004193 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004194 break;
4195 }
4196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004197 case Primitive::kPrimVoid:
4198 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004199 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004200 }
4201}
4202
4203void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004204 LocationSummary* locations =
4205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004206 locations->SetInAt(0, Location::RequiresRegister());
4207 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004208}
4209
4210void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4211 LocationSummary* locations = instruction->GetLocations();
4212 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004213 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4214 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004215 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004216 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004217}
4218
4219void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004220 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4221 ? LocationSummary::kCallOnSlowPath
4222 : LocationSummary::kNoCall;
4223 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004224 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004225 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004226 if (instruction->HasUses()) {
4227 locations->SetOut(Location::SameAsFirstInput());
4228 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004229}
4230
4231void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4232 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004233 Location index_loc = locations->InAt(0);
4234 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004235 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004236 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004237
Mark Mendell99dbd682015-04-22 16:18:52 -04004238 if (length_loc.IsConstant()) {
4239 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4240 if (index_loc.IsConstant()) {
4241 // BCE will remove the bounds check if we are guarenteed to pass.
4242 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4243 if (index < 0 || index >= length) {
4244 codegen_->AddSlowPath(slow_path);
4245 __ jmp(slow_path->GetEntryLabel());
4246 } else {
4247 // Some optimization after BCE may have generated this, and we should not
4248 // generate a bounds check if it is a valid range.
4249 }
4250 return;
4251 }
4252
4253 // We have to reverse the jump condition because the length is the constant.
4254 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4255 __ cmpl(index_reg, Immediate(length));
4256 codegen_->AddSlowPath(slow_path);
4257 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004258 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004259 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4260 if (index_loc.IsConstant()) {
4261 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4262 __ cmpl(length, Immediate(value));
4263 } else {
4264 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4265 }
4266 codegen_->AddSlowPath(slow_path);
4267 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004268 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004269}
4270
4271void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4272 CpuRegister card,
4273 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004274 CpuRegister value,
4275 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004276 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004277 if (value_can_be_null) {
4278 __ testl(value, value);
4279 __ j(kEqual, &is_null);
4280 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004281 __ gs()->movq(card, Address::Absolute(
4282 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4283 __ movq(temp, object);
4284 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004285 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004286 if (value_can_be_null) {
4287 __ Bind(&is_null);
4288 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004289}
4290
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004291void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4292 temp->SetLocations(nullptr);
4293}
4294
4295void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4296 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004297 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004298}
4299
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004300void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004301 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004302 LOG(FATAL) << "Unimplemented";
4303}
4304
4305void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004306 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4307}
4308
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004309void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4310 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4311}
4312
4313void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004314 HBasicBlock* block = instruction->GetBlock();
4315 if (block->GetLoopInformation() != nullptr) {
4316 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4317 // The back edge will generate the suspend check.
4318 return;
4319 }
4320 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4321 // The goto will generate the suspend check.
4322 return;
4323 }
4324 GenerateSuspendCheck(instruction, nullptr);
4325}
4326
4327void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4328 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004329 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004330 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4331 if (slow_path == nullptr) {
4332 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4333 instruction->SetSlowPath(slow_path);
4334 codegen_->AddSlowPath(slow_path);
4335 if (successor != nullptr) {
4336 DCHECK(successor->IsLoopHeader());
4337 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4338 }
4339 } else {
4340 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4341 }
4342
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004343 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004344 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004345 if (successor == nullptr) {
4346 __ j(kNotEqual, slow_path->GetEntryLabel());
4347 __ Bind(slow_path->GetReturnLabel());
4348 } else {
4349 __ j(kEqual, codegen_->GetLabelOf(successor));
4350 __ jmp(slow_path->GetEntryLabel());
4351 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004352}
4353
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004354X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4355 return codegen_->GetAssembler();
4356}
4357
4358void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4359 MoveOperands* move = moves_.Get(index);
4360 Location source = move->GetSource();
4361 Location destination = move->GetDestination();
4362
4363 if (source.IsRegister()) {
4364 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004365 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004366 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004367 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004368 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004369 } else {
4370 DCHECK(destination.IsDoubleStackSlot());
4371 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004372 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004373 }
4374 } else if (source.IsStackSlot()) {
4375 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004376 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004377 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004378 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004379 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004380 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004381 } else {
4382 DCHECK(destination.IsStackSlot());
4383 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4384 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4385 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004386 } else if (source.IsDoubleStackSlot()) {
4387 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004388 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004389 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004390 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004391 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4392 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004393 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004394 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004395 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4396 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4397 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004398 } else if (source.IsConstant()) {
4399 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004400 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4401 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004402 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004403 if (value == 0) {
4404 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4405 } else {
4406 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4407 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004408 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004409 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004410 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004411 }
4412 } else if (constant->IsLongConstant()) {
4413 int64_t value = constant->AsLongConstant()->GetValue();
4414 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004415 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004416 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004417 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004418 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004419 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004420 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004421 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004422 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004423 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004424 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4425 if (value == 0) {
4426 // easy FP 0.0.
4427 __ xorps(dest, dest);
4428 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004429 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004430 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004431 } else {
4432 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004433 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004434 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4435 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004436 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004437 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004438 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004439 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004440 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004441 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4442 if (value == 0) {
4443 __ xorpd(dest, dest);
4444 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004445 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004446 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004447 } else {
4448 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004449 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004450 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004451 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004452 } else if (source.IsFpuRegister()) {
4453 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004454 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004455 } else if (destination.IsStackSlot()) {
4456 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004457 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004458 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004459 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004460 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004461 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004462 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004463 }
4464}
4465
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004466void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004467 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004468 __ movl(Address(CpuRegister(RSP), mem), reg);
4469 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004470}
4471
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004472void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004473 ScratchRegisterScope ensure_scratch(
4474 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4475
4476 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4477 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4478 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4479 Address(CpuRegister(RSP), mem2 + stack_offset));
4480 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4481 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4482 CpuRegister(ensure_scratch.GetRegister()));
4483}
4484
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004485void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4486 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4487 __ movq(Address(CpuRegister(RSP), mem), reg);
4488 __ movq(reg, CpuRegister(TMP));
4489}
4490
4491void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4492 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004493 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004494
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004495 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4496 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4497 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4498 Address(CpuRegister(RSP), mem2 + stack_offset));
4499 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4500 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4501 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004502}
4503
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004504void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4505 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4506 __ movss(Address(CpuRegister(RSP), mem), reg);
4507 __ movd(reg, CpuRegister(TMP));
4508}
4509
4510void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4511 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4512 __ movsd(Address(CpuRegister(RSP), mem), reg);
4513 __ movd(reg, CpuRegister(TMP));
4514}
4515
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004516void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4517 MoveOperands* move = moves_.Get(index);
4518 Location source = move->GetSource();
4519 Location destination = move->GetDestination();
4520
4521 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004522 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004523 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004524 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004525 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004526 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004527 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004528 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4529 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004530 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004531 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004532 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004533 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4534 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004535 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004536 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4537 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4538 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004539 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004540 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004541 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004542 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004543 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004544 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004545 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004546 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004547 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004548 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004549 }
4550}
4551
4552
4553void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4554 __ pushq(CpuRegister(reg));
4555}
4556
4557
4558void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4559 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004560}
4561
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004562void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004563 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004564 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4565 Immediate(mirror::Class::kStatusInitialized));
4566 __ j(kLess, slow_path->GetEntryLabel());
4567 __ Bind(slow_path->GetExitLabel());
4568 // No need for memory fence, thanks to the X86_64 memory model.
4569}
4570
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004571void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004572 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4573 ? LocationSummary::kCallOnSlowPath
4574 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004575 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004576 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004577 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004578 locations->SetOut(Location::RequiresRegister());
4579}
4580
4581void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004582 LocationSummary* locations = cls->GetLocations();
4583 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4584 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004585 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004586 DCHECK(!cls->CanCallRuntime());
4587 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004588 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004589 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004590 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004591 __ movq(out, Address(
4592 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004593 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004594 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004595
Andreas Gampe85b62f22015-09-09 13:15:38 -07004596 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004597 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4598 codegen_->AddSlowPath(slow_path);
4599 __ testl(out, out);
4600 __ j(kEqual, slow_path->GetEntryLabel());
4601 if (cls->MustGenerateClinitCheck()) {
4602 GenerateClassInitializationCheck(slow_path, out);
4603 } else {
4604 __ Bind(slow_path->GetExitLabel());
4605 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004606 }
4607}
4608
4609void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4610 LocationSummary* locations =
4611 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4612 locations->SetInAt(0, Location::RequiresRegister());
4613 if (check->HasUses()) {
4614 locations->SetOut(Location::SameAsFirstInput());
4615 }
4616}
4617
4618void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004619 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004620 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004621 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004622 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004623 GenerateClassInitializationCheck(slow_path,
4624 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004625}
4626
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004627void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4628 LocationSummary* locations =
4629 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004630 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004631 locations->SetOut(Location::RequiresRegister());
4632}
4633
4634void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004635 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004636 codegen_->AddSlowPath(slow_path);
4637
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004638 LocationSummary* locations = load->GetLocations();
4639 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4640 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004641 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004642 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004643 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004644 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004645 __ testl(out, out);
4646 __ j(kEqual, slow_path->GetEntryLabel());
4647 __ Bind(slow_path->GetExitLabel());
4648}
4649
David Brazdilcb1c0552015-08-04 16:22:25 +01004650static Address GetExceptionTlsAddress() {
4651 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4652}
4653
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004654void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4655 LocationSummary* locations =
4656 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4657 locations->SetOut(Location::RequiresRegister());
4658}
4659
4660void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004661 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4662}
4663
4664void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4665 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4666}
4667
4668void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4669 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004670}
4671
4672void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4673 LocationSummary* locations =
4674 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4675 InvokeRuntimeCallingConvention calling_convention;
4676 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4677}
4678
4679void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004680 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4681 instruction,
4682 instruction->GetDexPc(),
4683 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004684}
4685
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004686void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray75374372015-09-17 17:12:19 +00004687 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4688 ? LocationSummary::kNoCall
4689 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray75374372015-09-17 17:12:19 +00004691 locations->SetInAt(0, Location::RequiresRegister());
4692 locations->SetInAt(1, Location::Any());
4693 // Note that TypeCheckSlowPathX86_64 uses this register too.
4694 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004695}
4696
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004697void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004698 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004699 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004700 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004701 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004702 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004703 NearLabel done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07004704 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004705
4706 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004707 // Avoid null check if we know obj is not null.
4708 if (instruction->MustDoNullCheck()) {
4709 __ testl(obj, obj);
4710 __ j(kEqual, &zero);
4711 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004712 // Compare the class of `obj` with `cls`.
4713 __ movl(out, Address(obj, class_offset));
4714 __ MaybeUnpoisonHeapReference(out);
4715 if (cls.IsRegister()) {
4716 __ cmpl(out, cls.AsRegister<CpuRegister>());
4717 } else {
4718 DCHECK(cls.IsStackSlot()) << cls;
4719 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4720 }
4721 if (instruction->IsClassFinal()) {
4722 // Classes must be equal for the instanceof to succeed.
4723 __ j(kNotEqual, &zero);
4724 __ movl(out, Immediate(1));
4725 __ jmp(&done);
4726 } else {
4727 // If the classes are not equal, we go into a slow path.
4728 DCHECK(locations->OnlyCallsOnSlowPath());
4729 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction);
4730 codegen_->AddSlowPath(slow_path);
4731 __ j(kNotEqual, slow_path->GetEntryLabel());
4732 __ movl(out, Immediate(1));
4733 __ jmp(&done);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004734 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004735
Nicolas Geoffray75374372015-09-17 17:12:19 +00004736 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004737 __ Bind(&zero);
Nicolas Geoffray75374372015-09-17 17:12:19 +00004738 __ movl(out, Immediate(0));
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004739 }
4740
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004741 if (slow_path != nullptr) {
4742 __ Bind(slow_path->GetExitLabel());
4743 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004744 __ Bind(&done);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004745}
4746
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004747void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4748 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray75374372015-09-17 17:12:19 +00004749 instruction, LocationSummary::kCallOnSlowPath);
4750 locations->SetInAt(0, Location::RequiresRegister());
4751 locations->SetInAt(1, Location::Any());
4752 // Note that TypeCheckSlowPathX86_64 uses this register too.
4753 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004754}
4755
4756void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4757 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004758 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004759 Location cls = locations->InAt(1);
Nicolas Geoffray75374372015-09-17 17:12:19 +00004760 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004761 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004762 SlowPathCode* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction);
Nicolas Geoffray75374372015-09-17 17:12:19 +00004763 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004764
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004765 // Avoid null check if we know obj is not null.
4766 if (instruction->MustDoNullCheck()) {
4767 __ testl(obj, obj);
Nicolas Geoffray75374372015-09-17 17:12:19 +00004768 __ j(kEqual, slow_path->GetExitLabel());
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004769 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004770 // Compare the class of `obj` with `cls`.
4771 __ movl(temp, Address(obj, class_offset));
4772 __ MaybeUnpoisonHeapReference(temp);
4773 if (cls.IsRegister()) {
4774 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004775 } else {
Nicolas Geoffray75374372015-09-17 17:12:19 +00004776 DCHECK(cls.IsStackSlot()) << cls;
4777 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004778 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004779 // The checkcast succeeds if the classes are equal (fast path).
4780 // Otherwise, we need to go into the slow path to check the types.
4781 __ j(kNotEqual, slow_path->GetEntryLabel());
4782 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004783}
4784
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004785void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4786 LocationSummary* locations =
4787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4788 InvokeRuntimeCallingConvention calling_convention;
4789 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4790}
4791
4792void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004793 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4794 : QUICK_ENTRY_POINT(pUnlockObject),
4795 instruction,
4796 instruction->GetDexPc(),
4797 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004798}
4799
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004800void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4801void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4802void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4803
4804void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4805 LocationSummary* locations =
4806 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4807 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4808 || instruction->GetResultType() == Primitive::kPrimLong);
4809 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004810 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004811 locations->SetOut(Location::SameAsFirstInput());
4812}
4813
4814void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4815 HandleBitwiseOperation(instruction);
4816}
4817
4818void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4819 HandleBitwiseOperation(instruction);
4820}
4821
4822void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4823 HandleBitwiseOperation(instruction);
4824}
4825
4826void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4827 LocationSummary* locations = instruction->GetLocations();
4828 Location first = locations->InAt(0);
4829 Location second = locations->InAt(1);
4830 DCHECK(first.Equals(locations->Out()));
4831
4832 if (instruction->GetResultType() == Primitive::kPrimInt) {
4833 if (second.IsRegister()) {
4834 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004835 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004836 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004837 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004838 } else {
4839 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004840 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004841 }
4842 } else if (second.IsConstant()) {
4843 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4844 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004845 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004846 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004847 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004848 } else {
4849 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004850 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004851 }
4852 } else {
4853 Address address(CpuRegister(RSP), second.GetStackIndex());
4854 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004855 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004856 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004857 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004858 } else {
4859 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004860 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004861 }
4862 }
4863 } else {
4864 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004865 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4866 bool second_is_constant = false;
4867 int64_t value = 0;
4868 if (second.IsConstant()) {
4869 second_is_constant = true;
4870 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004871 }
Mark Mendell40741f32015-04-20 22:10:34 -04004872 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004873
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004874 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004875 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004876 if (is_int32_value) {
4877 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4878 } else {
4879 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4880 }
4881 } else if (second.IsDoubleStackSlot()) {
4882 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004883 } else {
4884 __ andq(first_reg, second.AsRegister<CpuRegister>());
4885 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004886 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004887 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004888 if (is_int32_value) {
4889 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4890 } else {
4891 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4892 }
4893 } else if (second.IsDoubleStackSlot()) {
4894 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004895 } else {
4896 __ orq(first_reg, second.AsRegister<CpuRegister>());
4897 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004898 } else {
4899 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004900 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004901 if (is_int32_value) {
4902 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4903 } else {
4904 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4905 }
4906 } else if (second.IsDoubleStackSlot()) {
4907 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004908 } else {
4909 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4910 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004911 }
4912 }
4913}
4914
Calin Juravleb1498f62015-02-16 13:13:29 +00004915void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4916 // Nothing to do, this should be removed during prepare for register allocator.
4917 UNUSED(instruction);
4918 LOG(FATAL) << "Unreachable";
4919}
4920
4921void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4922 // Nothing to do, this should be removed during prepare for register allocator.
4923 UNUSED(instruction);
4924 LOG(FATAL) << "Unreachable";
4925}
4926
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004927void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
4928 DCHECK(codegen_->IsBaseline());
4929 LocationSummary* locations =
4930 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4931 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4932}
4933
4934void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4935 DCHECK(codegen_->IsBaseline());
4936 // Will be generated at use site.
4937}
4938
Mark Mendell92e83bf2015-05-07 11:25:03 -04004939void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4940 if (value == 0) {
4941 __ xorl(dest, dest);
4942 } else if (value > 0 && IsInt<32>(value)) {
4943 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4944 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4945 } else {
4946 __ movq(dest, Immediate(value));
4947 }
4948}
4949
Mark Mendellcfa410b2015-05-25 16:02:44 -04004950void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
4951 DCHECK(dest.IsDoubleStackSlot());
4952 if (IsInt<32>(value)) {
4953 // Can move directly as an int32 constant.
4954 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
4955 Immediate(static_cast<int32_t>(value)));
4956 } else {
4957 Load64BitValue(CpuRegister(TMP), value);
4958 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
4959 }
4960}
4961
Mark Mendellf55c3e02015-03-26 21:07:46 -04004962void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4963 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004964 X86_64Assembler* assembler = GetAssembler();
4965 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004966 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4967 // byte values. If used for vectors at a later time, this will need to be
4968 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004969 assembler->Align(4, 0);
4970 constant_area_start_ = assembler->CodeSize();
4971 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004972 }
4973
4974 // And finish up.
4975 CodeGenerator::Finalize(allocator);
4976}
4977
4978/**
4979 * Class to handle late fixup of offsets into constant area.
4980 */
4981class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4982 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004983 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004984 : codegen_(codegen), offset_into_constant_area_(offset) {}
4985
4986 private:
4987 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4988 // Patch the correct offset for the instruction. We use the address of the
4989 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4990 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4991 int relative_position = constant_offset - pos;
4992
4993 // Patch in the right value.
4994 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4995 }
4996
Mark Mendell39dcf552015-04-09 20:42:42 -04004997 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004998
4999 // Location in constant area that the fixup refers to.
5000 int offset_into_constant_area_;
5001};
5002
5003Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
5004 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5005 return Address::RIP(fixup);
5006}
5007
5008Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
5009 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5010 return Address::RIP(fixup);
5011}
5012
5013Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
5014 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5015 return Address::RIP(fixup);
5016}
5017
5018Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
5019 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5020 return Address::RIP(fixup);
5021}
5022
Andreas Gampe85b62f22015-09-09 13:15:38 -07005023// TODO: trg as memory.
5024void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5025 if (!trg.IsValid()) {
5026 DCHECK(type == Primitive::kPrimVoid);
5027 return;
5028 }
5029
5030 DCHECK_NE(type, Primitive::kPrimVoid);
5031
5032 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
5033 if (trg.Equals(return_loc)) {
5034 return;
5035 }
5036
5037 // Let the parallel move resolver take care of all of this.
5038 HParallelMove parallel_move(GetGraph()->GetArena());
5039 parallel_move.AddMove(return_loc, trg, type, nullptr);
5040 GetMoveResolver()->EmitNativeCode(&parallel_move);
5041}
5042
Roland Levillain4d027112015-07-01 15:41:14 +01005043#undef __
5044
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005045} // namespace x86_64
5046} // namespace art