blob: ef20b7122ff96736ec7365bb5e51e105cf2c75b1 [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
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010053class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
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
Calin Juravled0d48522014-11-04 16:40:20 +000079class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
80 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
Calin Juravlebacfec32014-11-14 15:54:36 +0000105class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
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
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100139class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
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
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100179class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
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
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
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
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000269class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
270 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
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
302 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
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
359 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
Calin Juravle23a8e352015-09-08 19:56:31 +0100931void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
932 if (location.IsRegister()) {
933 locations->AddTemp(location);
934 } else {
935 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
936 }
937}
938
939void CodeGeneratorX86_64::MoveLocationToTemp(Location source,
940 const LocationSummary& locations,
941 int temp_index,
942 Primitive::Type type) {
943 if (!Primitive::IsFloatingPointType(type)) {
944 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
945 }
946
947 DCHECK(source.IsFpuRegister()) << source;
948 __ movd(locations.GetTemp(temp_index).AsRegister<CpuRegister>(),
949 source.AsFpuRegister<XmmRegister>(),
950 Primitive::Is64BitType(type));
951}
952
953void CodeGeneratorX86_64::MoveTempToLocation(const LocationSummary& locations,
954 int temp_index,
955 Location destination,
956 Primitive::Type type) {
957 if (!Primitive::IsFloatingPointType(type)) {
958 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
959 }
960
961 DCHECK(destination.IsFpuRegister()) << destination;
962 __ movd(destination.AsFpuRegister<XmmRegister>(),
963 locations.GetTemp(temp_index).AsRegister<CpuRegister>(),
964 Primitive::Is64BitType(type));
965}
966
David Brazdilfc6a86a2015-06-26 10:33:45 +0000967void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100968 DCHECK(!successor->IsExitBlock());
969
970 HBasicBlock* block = got->GetBlock();
971 HInstruction* previous = got->GetPrevious();
972
973 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000974 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100975 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
976 return;
977 }
978
979 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
980 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
981 }
982 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100983 __ jmp(codegen_->GetLabelOf(successor));
984 }
985}
986
David Brazdilfc6a86a2015-06-26 10:33:45 +0000987void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
988 got->SetLocations(nullptr);
989}
990
991void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
992 HandleGoto(got, got->GetSuccessor());
993}
994
995void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
996 try_boundary->SetLocations(nullptr);
997}
998
999void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1000 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1001 if (!successor->IsExitBlock()) {
1002 HandleGoto(try_boundary, successor);
1003 }
1004}
1005
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001006void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1007 exit->SetLocations(nullptr);
1008}
1009
1010void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001011 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001012}
1013
Mark Mendellc4701932015-04-10 13:18:51 -04001014void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
1015 Label* true_label,
1016 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001017 if (cond->IsFPConditionTrueIfNaN()) {
1018 __ j(kUnordered, true_label);
1019 } else if (cond->IsFPConditionFalseIfNaN()) {
1020 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001021 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001022 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001023}
1024
1025void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
1026 HCondition* condition,
1027 Label* true_target,
1028 Label* false_target,
1029 Label* always_true_target) {
1030 LocationSummary* locations = condition->GetLocations();
1031 Location left = locations->InAt(0);
1032 Location right = locations->InAt(1);
1033
1034 // We don't want true_target as a nullptr.
1035 if (true_target == nullptr) {
1036 true_target = always_true_target;
1037 }
1038 bool falls_through = (false_target == nullptr);
1039
1040 // FP compares don't like null false_targets.
1041 if (false_target == nullptr) {
1042 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1043 }
1044
1045 Primitive::Type type = condition->InputAt(0)->GetType();
1046 switch (type) {
1047 case Primitive::kPrimLong: {
1048 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1049 if (right.IsConstant()) {
1050 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1051 if (IsInt<32>(value)) {
1052 if (value == 0) {
1053 __ testq(left_reg, left_reg);
1054 } else {
1055 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1056 }
1057 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001058 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001059 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1060 }
1061 } else if (right.IsDoubleStackSlot()) {
1062 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1063 } else {
1064 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1065 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001066 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001067 break;
1068 }
1069 case Primitive::kPrimFloat: {
1070 if (right.IsFpuRegister()) {
1071 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1072 } else if (right.IsConstant()) {
1073 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1074 codegen_->LiteralFloatAddress(
1075 right.GetConstant()->AsFloatConstant()->GetValue()));
1076 } else {
1077 DCHECK(right.IsStackSlot());
1078 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1079 Address(CpuRegister(RSP), right.GetStackIndex()));
1080 }
1081 GenerateFPJumps(condition, true_target, false_target);
1082 break;
1083 }
1084 case Primitive::kPrimDouble: {
1085 if (right.IsFpuRegister()) {
1086 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1087 } else if (right.IsConstant()) {
1088 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1089 codegen_->LiteralDoubleAddress(
1090 right.GetConstant()->AsDoubleConstant()->GetValue()));
1091 } else {
1092 DCHECK(right.IsDoubleStackSlot());
1093 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1094 Address(CpuRegister(RSP), right.GetStackIndex()));
1095 }
1096 GenerateFPJumps(condition, true_target, false_target);
1097 break;
1098 }
1099 default:
1100 LOG(FATAL) << "Unexpected condition type " << type;
1101 }
1102
1103 if (!falls_through) {
1104 __ jmp(false_target);
1105 }
1106}
1107
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001108void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1109 Label* true_target,
1110 Label* false_target,
1111 Label* always_true_target) {
1112 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001113 if (cond->IsIntConstant()) {
1114 // Constant condition, statically compared against 1.
1115 int32_t cond_value = cond->AsIntConstant()->GetValue();
1116 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001117 if (always_true_target != nullptr) {
1118 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001119 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001120 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001121 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001122 DCHECK_EQ(cond_value, 0);
1123 }
1124 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001125 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001126 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1127 // Moves do not affect the eflags register, so if the condition is
1128 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001129 // again. We can't use the eflags on FP conditions if they are
1130 // materialized due to the complex branching.
1131 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001132 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001133 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1134 && !Primitive::IsFloatingPointType(type);
1135
Roland Levillain4fa13f62015-07-06 18:11:54 +01001136 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001137 if (!eflags_set) {
1138 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001139 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001140 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001141 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001142 } else {
1143 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1144 Immediate(0));
1145 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001146 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001147 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001148 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001149 }
1150 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001151 // Condition has not been materialized, use its inputs as the
1152 // comparison and its condition as the branch condition.
1153
Mark Mendellc4701932015-04-10 13:18:51 -04001154 // Is this a long or FP comparison that has been folded into the HCondition?
1155 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001156 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001157 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1158 true_target, false_target, always_true_target);
1159 return;
1160 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001161
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001162 Location lhs = cond->GetLocations()->InAt(0);
1163 Location rhs = cond->GetLocations()->InAt(1);
1164 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001165 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001166 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001167 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001168 if (constant == 0) {
1169 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1170 } else {
1171 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1172 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001173 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001174 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001175 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1176 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001177 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001178 }
Dave Allison20dfc792014-06-16 20:44:29 -07001179 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001180 if (false_target != nullptr) {
1181 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001182 }
1183}
1184
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001185void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1186 LocationSummary* locations =
1187 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1188 HInstruction* cond = if_instr->InputAt(0);
1189 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1190 locations->SetInAt(0, Location::Any());
1191 }
1192}
1193
1194void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1195 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1196 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1197 Label* always_true_target = true_target;
1198 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1199 if_instr->IfTrueSuccessor())) {
1200 always_true_target = nullptr;
1201 }
1202 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1203 if_instr->IfFalseSuccessor())) {
1204 false_target = nullptr;
1205 }
1206 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1207}
1208
1209void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1210 LocationSummary* locations = new (GetGraph()->GetArena())
1211 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1212 HInstruction* cond = deoptimize->InputAt(0);
1213 DCHECK(cond->IsCondition());
1214 if (cond->AsCondition()->NeedsMaterialization()) {
1215 locations->SetInAt(0, Location::Any());
1216 }
1217}
1218
1219void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1220 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
1221 DeoptimizationSlowPathX86_64(deoptimize);
1222 codegen_->AddSlowPath(slow_path);
1223 Label* slow_path_entry = slow_path->GetEntryLabel();
1224 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1225}
1226
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001227void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1228 local->SetLocations(nullptr);
1229}
1230
1231void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1232 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1233}
1234
1235void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1236 local->SetLocations(nullptr);
1237}
1238
1239void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1240 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001241 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001242}
1243
1244void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001245 LocationSummary* locations =
1246 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001247 switch (store->InputAt(1)->GetType()) {
1248 case Primitive::kPrimBoolean:
1249 case Primitive::kPrimByte:
1250 case Primitive::kPrimChar:
1251 case Primitive::kPrimShort:
1252 case Primitive::kPrimInt:
1253 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001254 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001255 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1256 break;
1257
1258 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001259 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1261 break;
1262
1263 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001264 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001265 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001266}
1267
1268void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001269 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001270}
1271
Roland Levillain0d37cd02015-05-27 16:39:19 +01001272void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001273 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001274 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001275 // Handle the long/FP comparisons made in instruction simplification.
1276 switch (cond->InputAt(0)->GetType()) {
1277 case Primitive::kPrimLong:
1278 locations->SetInAt(0, Location::RequiresRegister());
1279 locations->SetInAt(1, Location::Any());
1280 break;
1281 case Primitive::kPrimFloat:
1282 case Primitive::kPrimDouble:
1283 locations->SetInAt(0, Location::RequiresFpuRegister());
1284 locations->SetInAt(1, Location::Any());
1285 break;
1286 default:
1287 locations->SetInAt(0, Location::RequiresRegister());
1288 locations->SetInAt(1, Location::Any());
1289 break;
1290 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001291 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001292 locations->SetOut(Location::RequiresRegister());
1293 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001294}
1295
Roland Levillain0d37cd02015-05-27 16:39:19 +01001296void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001297 if (!cond->NeedsMaterialization()) {
1298 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001299 }
Mark Mendellc4701932015-04-10 13:18:51 -04001300
1301 LocationSummary* locations = cond->GetLocations();
1302 Location lhs = locations->InAt(0);
1303 Location rhs = locations->InAt(1);
1304 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1305 Label true_label, false_label;
1306
1307 switch (cond->InputAt(0)->GetType()) {
1308 default:
1309 // Integer case.
1310
1311 // Clear output register: setcc only sets the low byte.
1312 __ xorl(reg, reg);
1313
1314 if (rhs.IsRegister()) {
1315 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1316 } else if (rhs.IsConstant()) {
1317 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1318 if (constant == 0) {
1319 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1320 } else {
1321 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1322 }
1323 } else {
1324 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1325 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001326 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001327 return;
1328 case Primitive::kPrimLong:
1329 // Clear output register: setcc only sets the low byte.
1330 __ xorl(reg, reg);
1331
1332 if (rhs.IsRegister()) {
1333 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1334 } else if (rhs.IsConstant()) {
1335 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1336 if (IsInt<32>(value)) {
1337 if (value == 0) {
1338 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1339 } else {
1340 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1341 }
1342 } else {
1343 // Value won't fit in an int.
1344 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1345 }
1346 } else {
1347 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1348 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001349 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001350 return;
1351 case Primitive::kPrimFloat: {
1352 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1353 if (rhs.IsConstant()) {
1354 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1355 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1356 } else if (rhs.IsStackSlot()) {
1357 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1358 } else {
1359 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1360 }
1361 GenerateFPJumps(cond, &true_label, &false_label);
1362 break;
1363 }
1364 case Primitive::kPrimDouble: {
1365 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1366 if (rhs.IsConstant()) {
1367 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1368 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1369 } else if (rhs.IsDoubleStackSlot()) {
1370 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1371 } else {
1372 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1373 }
1374 GenerateFPJumps(cond, &true_label, &false_label);
1375 break;
1376 }
1377 }
1378
1379 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001380 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001381
Roland Levillain4fa13f62015-07-06 18:11:54 +01001382 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001383 __ Bind(&false_label);
1384 __ xorl(reg, reg);
1385 __ jmp(&done_label);
1386
Roland Levillain4fa13f62015-07-06 18:11:54 +01001387 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001388 __ Bind(&true_label);
1389 __ movl(reg, Immediate(1));
1390 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001391}
1392
1393void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1394 VisitCondition(comp);
1395}
1396
1397void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1398 VisitCondition(comp);
1399}
1400
1401void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1402 VisitCondition(comp);
1403}
1404
1405void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1406 VisitCondition(comp);
1407}
1408
1409void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1410 VisitCondition(comp);
1411}
1412
1413void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1414 VisitCondition(comp);
1415}
1416
1417void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1418 VisitCondition(comp);
1419}
1420
1421void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1422 VisitCondition(comp);
1423}
1424
1425void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1426 VisitCondition(comp);
1427}
1428
1429void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1430 VisitCondition(comp);
1431}
1432
1433void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1434 VisitCondition(comp);
1435}
1436
1437void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1438 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001439}
1440
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001441void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001442 LocationSummary* locations =
1443 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001444 switch (compare->InputAt(0)->GetType()) {
1445 case Primitive::kPrimLong: {
1446 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001447 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1449 break;
1450 }
1451 case Primitive::kPrimFloat:
1452 case Primitive::kPrimDouble: {
1453 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001454 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001455 locations->SetOut(Location::RequiresRegister());
1456 break;
1457 }
1458 default:
1459 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1460 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001461}
1462
1463void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001464 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001465 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001466 Location left = locations->InAt(0);
1467 Location right = locations->InAt(1);
1468
Mark Mendell0c9497d2015-08-21 09:30:05 -04001469 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001470 Primitive::Type type = compare->InputAt(0)->GetType();
1471 switch (type) {
1472 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001473 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1474 if (right.IsConstant()) {
1475 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001476 if (IsInt<32>(value)) {
1477 if (value == 0) {
1478 __ testq(left_reg, left_reg);
1479 } else {
1480 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1481 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001482 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001483 // Value won't fit in an int.
1484 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001485 }
Mark Mendell40741f32015-04-20 22:10:34 -04001486 } else if (right.IsDoubleStackSlot()) {
1487 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001488 } else {
1489 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1490 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001491 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001492 }
1493 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001494 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1495 if (right.IsConstant()) {
1496 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1497 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1498 } else if (right.IsStackSlot()) {
1499 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1500 } else {
1501 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1502 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001503 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1504 break;
1505 }
1506 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001507 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1508 if (right.IsConstant()) {
1509 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1510 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1511 } else if (right.IsDoubleStackSlot()) {
1512 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1513 } else {
1514 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1515 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001516 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1517 break;
1518 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001519 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001520 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001521 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001522 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001523 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001524 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001525
Calin Juravle91debbc2014-11-26 19:01:09 +00001526 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001527 __ movl(out, Immediate(1));
1528 __ jmp(&done);
1529
1530 __ Bind(&less);
1531 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001532
1533 __ Bind(&done);
1534}
1535
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001536void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001537 LocationSummary* locations =
1538 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001539 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001540}
1541
1542void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001543 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001544 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001545}
1546
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001547void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1548 LocationSummary* locations =
1549 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1550 locations->SetOut(Location::ConstantLocation(constant));
1551}
1552
1553void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1554 // Will be generated at use site.
1555 UNUSED(constant);
1556}
1557
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001558void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001559 LocationSummary* locations =
1560 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001561 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001562}
1563
1564void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001565 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001566 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001567}
1568
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001569void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1570 LocationSummary* locations =
1571 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1572 locations->SetOut(Location::ConstantLocation(constant));
1573}
1574
1575void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1576 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001577 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001578}
1579
1580void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1581 LocationSummary* locations =
1582 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1583 locations->SetOut(Location::ConstantLocation(constant));
1584}
1585
1586void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1587 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001588 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001589}
1590
Calin Juravle27df7582015-04-17 19:12:31 +01001591void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1592 memory_barrier->SetLocations(nullptr);
1593}
1594
1595void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1596 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1597}
1598
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001599void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1600 ret->SetLocations(nullptr);
1601}
1602
1603void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001604 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001605 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001606}
1607
1608void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001609 LocationSummary* locations =
1610 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001611 switch (ret->InputAt(0)->GetType()) {
1612 case Primitive::kPrimBoolean:
1613 case Primitive::kPrimByte:
1614 case Primitive::kPrimChar:
1615 case Primitive::kPrimShort:
1616 case Primitive::kPrimInt:
1617 case Primitive::kPrimNot:
1618 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001619 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001620 break;
1621
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001622 case Primitive::kPrimFloat:
1623 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001624 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001625 break;
1626
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001627 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001628 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001629 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001630}
1631
1632void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1633 if (kIsDebugBuild) {
1634 switch (ret->InputAt(0)->GetType()) {
1635 case Primitive::kPrimBoolean:
1636 case Primitive::kPrimByte:
1637 case Primitive::kPrimChar:
1638 case Primitive::kPrimShort:
1639 case Primitive::kPrimInt:
1640 case Primitive::kPrimNot:
1641 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001642 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001643 break;
1644
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001645 case Primitive::kPrimFloat:
1646 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001647 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001648 XMM0);
1649 break;
1650
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001651 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001652 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001653 }
1654 }
1655 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001656}
1657
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001658Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1659 switch (type) {
1660 case Primitive::kPrimBoolean:
1661 case Primitive::kPrimByte:
1662 case Primitive::kPrimChar:
1663 case Primitive::kPrimShort:
1664 case Primitive::kPrimInt:
1665 case Primitive::kPrimNot:
1666 case Primitive::kPrimLong:
1667 return Location::RegisterLocation(RAX);
1668
1669 case Primitive::kPrimVoid:
1670 return Location::NoLocation();
1671
1672 case Primitive::kPrimDouble:
1673 case Primitive::kPrimFloat:
1674 return Location::FpuRegisterLocation(XMM0);
1675 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001676
1677 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001678}
1679
1680Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1681 return Location::RegisterLocation(kMethodRegisterArgument);
1682}
1683
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001684Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001685 switch (type) {
1686 case Primitive::kPrimBoolean:
1687 case Primitive::kPrimByte:
1688 case Primitive::kPrimChar:
1689 case Primitive::kPrimShort:
1690 case Primitive::kPrimInt:
1691 case Primitive::kPrimNot: {
1692 uint32_t index = gp_index_++;
1693 stack_index_++;
1694 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001695 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001696 } else {
1697 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1698 }
1699 }
1700
1701 case Primitive::kPrimLong: {
1702 uint32_t index = gp_index_;
1703 stack_index_ += 2;
1704 if (index < calling_convention.GetNumberOfRegisters()) {
1705 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001706 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001707 } else {
1708 gp_index_ += 2;
1709 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1710 }
1711 }
1712
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001713 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001714 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001715 stack_index_++;
1716 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001717 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001718 } else {
1719 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1720 }
1721 }
1722
1723 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001724 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001725 stack_index_ += 2;
1726 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001727 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001728 } else {
1729 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1730 }
1731 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001732
1733 case Primitive::kPrimVoid:
1734 LOG(FATAL) << "Unexpected parameter type " << type;
1735 break;
1736 }
1737 return Location();
1738}
1739
Calin Juravle175dc732015-08-25 15:42:32 +01001740void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1741 // The trampoline uses the same calling convention as dex calling conventions,
1742 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1743 // the method_idx.
1744 HandleInvoke(invoke);
1745}
1746
1747void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1748 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1749}
1750
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001751void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001752 // When we do not run baseline, explicit clinit checks triggered by static
1753 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1754 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001755
Mark Mendellfb8d2792015-03-31 22:16:59 -04001756 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001757 if (intrinsic.TryDispatch(invoke)) {
1758 return;
1759 }
1760
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001761 HandleInvoke(invoke);
1762}
1763
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001764static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1765 if (invoke->GetLocations()->Intrinsified()) {
1766 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1767 intrinsic.Dispatch(invoke);
1768 return true;
1769 }
1770 return false;
1771}
1772
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001773void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001774 // When we do not run baseline, explicit clinit checks triggered by static
1775 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1776 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001777
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001778 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1779 return;
1780 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001781
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001782 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001783 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001784 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001785 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001786}
1787
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001788void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001789 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001790 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001791}
1792
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001793void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001794 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001795 if (intrinsic.TryDispatch(invoke)) {
1796 return;
1797 }
1798
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001799 HandleInvoke(invoke);
1800}
1801
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001802void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001803 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1804 return;
1805 }
1806
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001807 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001808
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001809 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001810 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001811}
1812
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001813void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1814 HandleInvoke(invoke);
1815 // Add the hidden argument.
1816 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1817}
1818
1819void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1820 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001821 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001822 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1823 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001824 LocationSummary* locations = invoke->GetLocations();
1825 Location receiver = locations->InAt(0);
1826 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1827
1828 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001829 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1830 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001831
1832 // temp = object->GetClass();
1833 if (receiver.IsStackSlot()) {
1834 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1835 __ movl(temp, Address(temp, class_offset));
1836 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001837 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001838 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001839 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001840 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001841 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001842 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001843 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001844 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001845 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001846
1847 DCHECK(!codegen_->IsLeafMethod());
1848 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1849}
1850
Roland Levillain88cb1752014-10-20 16:36:47 +01001851void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1852 LocationSummary* locations =
1853 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1854 switch (neg->GetResultType()) {
1855 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001856 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001857 locations->SetInAt(0, Location::RequiresRegister());
1858 locations->SetOut(Location::SameAsFirstInput());
1859 break;
1860
Roland Levillain88cb1752014-10-20 16:36:47 +01001861 case Primitive::kPrimFloat:
1862 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001863 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001864 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001865 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001866 break;
1867
1868 default:
1869 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1870 }
1871}
1872
1873void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1874 LocationSummary* locations = neg->GetLocations();
1875 Location out = locations->Out();
1876 Location in = locations->InAt(0);
1877 switch (neg->GetResultType()) {
1878 case Primitive::kPrimInt:
1879 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001880 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001881 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001882 break;
1883
1884 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001885 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001886 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001887 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001888 break;
1889
Roland Levillain5368c212014-11-27 15:03:41 +00001890 case Primitive::kPrimFloat: {
1891 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001892 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001893 // Implement float negation with an exclusive or with value
1894 // 0x80000000 (mask for bit 31, representing the sign of a
1895 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001896 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001897 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001898 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001899 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001900
Roland Levillain5368c212014-11-27 15:03:41 +00001901 case Primitive::kPrimDouble: {
1902 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001903 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001904 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001905 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001906 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001907 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001908 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001909 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001910 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001911
1912 default:
1913 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1914 }
1915}
1916
Roland Levillaindff1f282014-11-05 14:15:05 +00001917void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1918 LocationSummary* locations =
1919 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1920 Primitive::Type result_type = conversion->GetResultType();
1921 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001922 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001923
David Brazdilb2bd1c52015-03-25 11:17:37 +00001924 // The Java language does not allow treating boolean as an integral type but
1925 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001926
Roland Levillaindff1f282014-11-05 14:15:05 +00001927 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001928 case Primitive::kPrimByte:
1929 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001930 case Primitive::kPrimBoolean:
1931 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001932 case Primitive::kPrimShort:
1933 case Primitive::kPrimInt:
1934 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001935 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001936 locations->SetInAt(0, Location::Any());
1937 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1938 break;
1939
1940 default:
1941 LOG(FATAL) << "Unexpected type conversion from " << input_type
1942 << " to " << result_type;
1943 }
1944 break;
1945
Roland Levillain01a8d712014-11-14 16:27:39 +00001946 case Primitive::kPrimShort:
1947 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001948 case Primitive::kPrimBoolean:
1949 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001950 case Primitive::kPrimByte:
1951 case Primitive::kPrimInt:
1952 case Primitive::kPrimChar:
1953 // Processing a Dex `int-to-short' instruction.
1954 locations->SetInAt(0, Location::Any());
1955 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1956 break;
1957
1958 default:
1959 LOG(FATAL) << "Unexpected type conversion from " << input_type
1960 << " to " << result_type;
1961 }
1962 break;
1963
Roland Levillain946e1432014-11-11 17:35:19 +00001964 case Primitive::kPrimInt:
1965 switch (input_type) {
1966 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001967 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001968 locations->SetInAt(0, Location::Any());
1969 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1970 break;
1971
1972 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001973 // Processing a Dex `float-to-int' instruction.
1974 locations->SetInAt(0, Location::RequiresFpuRegister());
1975 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001976 break;
1977
Roland Levillain946e1432014-11-11 17:35:19 +00001978 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001979 // Processing a Dex `double-to-int' instruction.
1980 locations->SetInAt(0, Location::RequiresFpuRegister());
1981 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001982 break;
1983
1984 default:
1985 LOG(FATAL) << "Unexpected type conversion from " << input_type
1986 << " to " << result_type;
1987 }
1988 break;
1989
Roland Levillaindff1f282014-11-05 14:15:05 +00001990 case Primitive::kPrimLong:
1991 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001992 case Primitive::kPrimBoolean:
1993 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001994 case Primitive::kPrimByte:
1995 case Primitive::kPrimShort:
1996 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001997 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001998 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001999 // TODO: We would benefit from a (to-be-implemented)
2000 // Location::RegisterOrStackSlot requirement for this input.
2001 locations->SetInAt(0, Location::RequiresRegister());
2002 locations->SetOut(Location::RequiresRegister());
2003 break;
2004
2005 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002006 // Processing a Dex `float-to-long' instruction.
2007 locations->SetInAt(0, Location::RequiresFpuRegister());
2008 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002009 break;
2010
Roland Levillaindff1f282014-11-05 14:15:05 +00002011 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002012 // Processing a Dex `double-to-long' instruction.
2013 locations->SetInAt(0, Location::RequiresFpuRegister());
2014 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002015 break;
2016
2017 default:
2018 LOG(FATAL) << "Unexpected type conversion from " << input_type
2019 << " to " << result_type;
2020 }
2021 break;
2022
Roland Levillain981e4542014-11-14 11:47:14 +00002023 case Primitive::kPrimChar:
2024 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002025 case Primitive::kPrimBoolean:
2026 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002027 case Primitive::kPrimByte:
2028 case Primitive::kPrimShort:
2029 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002030 // Processing a Dex `int-to-char' instruction.
2031 locations->SetInAt(0, Location::Any());
2032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2033 break;
2034
2035 default:
2036 LOG(FATAL) << "Unexpected type conversion from " << input_type
2037 << " to " << result_type;
2038 }
2039 break;
2040
Roland Levillaindff1f282014-11-05 14:15:05 +00002041 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002042 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002043 case Primitive::kPrimBoolean:
2044 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002045 case Primitive::kPrimByte:
2046 case Primitive::kPrimShort:
2047 case Primitive::kPrimInt:
2048 case Primitive::kPrimChar:
2049 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002050 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002051 locations->SetOut(Location::RequiresFpuRegister());
2052 break;
2053
2054 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002055 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002056 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002057 locations->SetOut(Location::RequiresFpuRegister());
2058 break;
2059
Roland Levillaincff13742014-11-17 14:32:17 +00002060 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002061 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002062 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002063 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002064 break;
2065
2066 default:
2067 LOG(FATAL) << "Unexpected type conversion from " << input_type
2068 << " to " << result_type;
2069 };
2070 break;
2071
Roland Levillaindff1f282014-11-05 14:15:05 +00002072 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002073 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002074 case Primitive::kPrimBoolean:
2075 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002076 case Primitive::kPrimByte:
2077 case Primitive::kPrimShort:
2078 case Primitive::kPrimInt:
2079 case Primitive::kPrimChar:
2080 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002081 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002082 locations->SetOut(Location::RequiresFpuRegister());
2083 break;
2084
2085 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002086 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002087 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002088 locations->SetOut(Location::RequiresFpuRegister());
2089 break;
2090
Roland Levillaincff13742014-11-17 14:32:17 +00002091 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002092 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002093 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002094 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002095 break;
2096
2097 default:
2098 LOG(FATAL) << "Unexpected type conversion from " << input_type
2099 << " to " << result_type;
2100 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002101 break;
2102
2103 default:
2104 LOG(FATAL) << "Unexpected type conversion from " << input_type
2105 << " to " << result_type;
2106 }
2107}
2108
2109void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2110 LocationSummary* locations = conversion->GetLocations();
2111 Location out = locations->Out();
2112 Location in = locations->InAt(0);
2113 Primitive::Type result_type = conversion->GetResultType();
2114 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002115 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002116 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002117 case Primitive::kPrimByte:
2118 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002119 case Primitive::kPrimBoolean:
2120 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002121 case Primitive::kPrimShort:
2122 case Primitive::kPrimInt:
2123 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002124 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002125 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002126 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002127 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002128 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002129 Address(CpuRegister(RSP), in.GetStackIndex()));
2130 } else {
2131 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002133 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2134 }
2135 break;
2136
2137 default:
2138 LOG(FATAL) << "Unexpected type conversion from " << input_type
2139 << " to " << result_type;
2140 }
2141 break;
2142
Roland Levillain01a8d712014-11-14 16:27:39 +00002143 case Primitive::kPrimShort:
2144 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002145 case Primitive::kPrimBoolean:
2146 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002147 case Primitive::kPrimByte:
2148 case Primitive::kPrimInt:
2149 case Primitive::kPrimChar:
2150 // Processing a Dex `int-to-short' instruction.
2151 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002152 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002153 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002154 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002155 Address(CpuRegister(RSP), in.GetStackIndex()));
2156 } else {
2157 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002158 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002159 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2160 }
2161 break;
2162
2163 default:
2164 LOG(FATAL) << "Unexpected type conversion from " << input_type
2165 << " to " << result_type;
2166 }
2167 break;
2168
Roland Levillain946e1432014-11-11 17:35:19 +00002169 case Primitive::kPrimInt:
2170 switch (input_type) {
2171 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002172 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002173 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002174 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002175 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002176 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002177 Address(CpuRegister(RSP), in.GetStackIndex()));
2178 } else {
2179 DCHECK(in.IsConstant());
2180 DCHECK(in.GetConstant()->IsLongConstant());
2181 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002182 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002183 }
2184 break;
2185
Roland Levillain3f8f9362014-12-02 17:45:01 +00002186 case Primitive::kPrimFloat: {
2187 // Processing a Dex `float-to-int' instruction.
2188 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2189 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002190 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002191
2192 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002193 // if input >= (float)INT_MAX goto done
2194 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002195 __ j(kAboveEqual, &done);
2196 // if input == NaN goto nan
2197 __ j(kUnordered, &nan);
2198 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002199 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002200 __ jmp(&done);
2201 __ Bind(&nan);
2202 // output = 0
2203 __ xorl(output, output);
2204 __ Bind(&done);
2205 break;
2206 }
2207
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002208 case Primitive::kPrimDouble: {
2209 // Processing a Dex `double-to-int' instruction.
2210 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2211 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002212 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002213
2214 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002215 // if input >= (double)INT_MAX goto done
2216 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002217 __ j(kAboveEqual, &done);
2218 // if input == NaN goto nan
2219 __ j(kUnordered, &nan);
2220 // output = double-to-int-truncate(input)
2221 __ cvttsd2si(output, input);
2222 __ jmp(&done);
2223 __ Bind(&nan);
2224 // output = 0
2225 __ xorl(output, output);
2226 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002227 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002228 }
Roland Levillain946e1432014-11-11 17:35:19 +00002229
2230 default:
2231 LOG(FATAL) << "Unexpected type conversion from " << input_type
2232 << " to " << result_type;
2233 }
2234 break;
2235
Roland Levillaindff1f282014-11-05 14:15:05 +00002236 case Primitive::kPrimLong:
2237 switch (input_type) {
2238 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002239 case Primitive::kPrimBoolean:
2240 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002241 case Primitive::kPrimByte:
2242 case Primitive::kPrimShort:
2243 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002244 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002245 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002246 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002247 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002248 break;
2249
Roland Levillain624279f2014-12-04 11:54:28 +00002250 case Primitive::kPrimFloat: {
2251 // Processing a Dex `float-to-long' instruction.
2252 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2253 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002254 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002255
Mark Mendell92e83bf2015-05-07 11:25:03 -04002256 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002257 // if input >= (float)LONG_MAX goto done
2258 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002259 __ j(kAboveEqual, &done);
2260 // if input == NaN goto nan
2261 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002262 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002263 __ cvttss2si(output, input, true);
2264 __ jmp(&done);
2265 __ Bind(&nan);
2266 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002267 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002268 __ Bind(&done);
2269 break;
2270 }
2271
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002272 case Primitive::kPrimDouble: {
2273 // Processing a Dex `double-to-long' instruction.
2274 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2275 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002276 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002277
Mark Mendell92e83bf2015-05-07 11:25:03 -04002278 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002279 // if input >= (double)LONG_MAX goto done
2280 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002281 __ j(kAboveEqual, &done);
2282 // if input == NaN goto nan
2283 __ j(kUnordered, &nan);
2284 // output = double-to-long-truncate(input)
2285 __ cvttsd2si(output, input, true);
2286 __ jmp(&done);
2287 __ Bind(&nan);
2288 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002289 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002290 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002291 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002292 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002293
2294 default:
2295 LOG(FATAL) << "Unexpected type conversion from " << input_type
2296 << " to " << result_type;
2297 }
2298 break;
2299
Roland Levillain981e4542014-11-14 11:47:14 +00002300 case Primitive::kPrimChar:
2301 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002302 case Primitive::kPrimBoolean:
2303 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002304 case Primitive::kPrimByte:
2305 case Primitive::kPrimShort:
2306 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002307 // Processing a Dex `int-to-char' instruction.
2308 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002309 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002310 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002311 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002312 Address(CpuRegister(RSP), in.GetStackIndex()));
2313 } else {
2314 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002315 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002316 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2317 }
2318 break;
2319
2320 default:
2321 LOG(FATAL) << "Unexpected type conversion from " << input_type
2322 << " to " << result_type;
2323 }
2324 break;
2325
Roland Levillaindff1f282014-11-05 14:15:05 +00002326 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002327 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002328 case Primitive::kPrimBoolean:
2329 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002330 case Primitive::kPrimByte:
2331 case Primitive::kPrimShort:
2332 case Primitive::kPrimInt:
2333 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002334 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002335 if (in.IsRegister()) {
2336 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2337 } else if (in.IsConstant()) {
2338 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2339 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2340 if (v == 0) {
2341 __ xorps(dest, dest);
2342 } else {
2343 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2344 }
2345 } else {
2346 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2347 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2348 }
Roland Levillaincff13742014-11-17 14:32:17 +00002349 break;
2350
2351 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002352 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002353 if (in.IsRegister()) {
2354 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2355 } else if (in.IsConstant()) {
2356 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2357 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2358 if (v == 0) {
2359 __ xorps(dest, dest);
2360 } else {
2361 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2362 }
2363 } else {
2364 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2365 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2366 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002367 break;
2368
Roland Levillaincff13742014-11-17 14:32:17 +00002369 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002370 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002371 if (in.IsFpuRegister()) {
2372 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2373 } else if (in.IsConstant()) {
2374 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2375 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2376 if (bit_cast<int64_t, double>(v) == 0) {
2377 __ xorps(dest, dest);
2378 } else {
2379 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2380 }
2381 } else {
2382 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2383 Address(CpuRegister(RSP), in.GetStackIndex()));
2384 }
Roland Levillaincff13742014-11-17 14:32:17 +00002385 break;
2386
2387 default:
2388 LOG(FATAL) << "Unexpected type conversion from " << input_type
2389 << " to " << result_type;
2390 };
2391 break;
2392
Roland Levillaindff1f282014-11-05 14:15:05 +00002393 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002394 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002395 case Primitive::kPrimBoolean:
2396 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002397 case Primitive::kPrimByte:
2398 case Primitive::kPrimShort:
2399 case Primitive::kPrimInt:
2400 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002401 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002402 if (in.IsRegister()) {
2403 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2404 } else if (in.IsConstant()) {
2405 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2406 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2407 if (v == 0) {
2408 __ xorpd(dest, dest);
2409 } else {
2410 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2411 }
2412 } else {
2413 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2414 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2415 }
Roland Levillaincff13742014-11-17 14:32:17 +00002416 break;
2417
2418 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002419 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002420 if (in.IsRegister()) {
2421 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2422 } else if (in.IsConstant()) {
2423 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2424 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2425 if (v == 0) {
2426 __ xorpd(dest, dest);
2427 } else {
2428 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2429 }
2430 } else {
2431 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2432 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2433 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002434 break;
2435
Roland Levillaincff13742014-11-17 14:32:17 +00002436 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002437 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002438 if (in.IsFpuRegister()) {
2439 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2440 } else if (in.IsConstant()) {
2441 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2442 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2443 if (bit_cast<int32_t, float>(v) == 0) {
2444 __ xorpd(dest, dest);
2445 } else {
2446 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2447 }
2448 } else {
2449 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2450 Address(CpuRegister(RSP), in.GetStackIndex()));
2451 }
Roland Levillaincff13742014-11-17 14:32:17 +00002452 break;
2453
2454 default:
2455 LOG(FATAL) << "Unexpected type conversion from " << input_type
2456 << " to " << result_type;
2457 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002458 break;
2459
2460 default:
2461 LOG(FATAL) << "Unexpected type conversion from " << input_type
2462 << " to " << result_type;
2463 }
2464}
2465
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002466void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002467 LocationSummary* locations =
2468 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002469 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002470 case Primitive::kPrimInt: {
2471 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002472 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2473 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002474 break;
2475 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002476
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002477 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002478 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002479 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002480 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002482 break;
2483 }
2484
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002485 case Primitive::kPrimDouble:
2486 case Primitive::kPrimFloat: {
2487 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002488 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002489 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002490 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002491 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002492
2493 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002494 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002495 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002496}
2497
2498void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2499 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002500 Location first = locations->InAt(0);
2501 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002502 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002503
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002504 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002505 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002506 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002507 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2508 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002509 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2510 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002511 } else {
2512 __ leal(out.AsRegister<CpuRegister>(), Address(
2513 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2514 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002515 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002516 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2517 __ addl(out.AsRegister<CpuRegister>(),
2518 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2519 } else {
2520 __ leal(out.AsRegister<CpuRegister>(), Address(
2521 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2522 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002523 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002524 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002525 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002526 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002527 break;
2528 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002529
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002530 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002531 if (second.IsRegister()) {
2532 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2533 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002534 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2535 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002536 } else {
2537 __ leaq(out.AsRegister<CpuRegister>(), Address(
2538 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2539 }
2540 } else {
2541 DCHECK(second.IsConstant());
2542 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2543 int32_t int32_value = Low32Bits(value);
2544 DCHECK_EQ(int32_value, value);
2545 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2546 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2547 } else {
2548 __ leaq(out.AsRegister<CpuRegister>(), Address(
2549 first.AsRegister<CpuRegister>(), int32_value));
2550 }
2551 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002552 break;
2553 }
2554
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002555 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002556 if (second.IsFpuRegister()) {
2557 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2558 } else if (second.IsConstant()) {
2559 __ addss(first.AsFpuRegister<XmmRegister>(),
2560 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2561 } else {
2562 DCHECK(second.IsStackSlot());
2563 __ addss(first.AsFpuRegister<XmmRegister>(),
2564 Address(CpuRegister(RSP), second.GetStackIndex()));
2565 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002566 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002567 }
2568
2569 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002570 if (second.IsFpuRegister()) {
2571 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2572 } else if (second.IsConstant()) {
2573 __ addsd(first.AsFpuRegister<XmmRegister>(),
2574 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2575 } else {
2576 DCHECK(second.IsDoubleStackSlot());
2577 __ addsd(first.AsFpuRegister<XmmRegister>(),
2578 Address(CpuRegister(RSP), second.GetStackIndex()));
2579 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002580 break;
2581 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002582
2583 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002584 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002585 }
2586}
2587
2588void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002589 LocationSummary* locations =
2590 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002591 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002592 case Primitive::kPrimInt: {
2593 locations->SetInAt(0, Location::RequiresRegister());
2594 locations->SetInAt(1, Location::Any());
2595 locations->SetOut(Location::SameAsFirstInput());
2596 break;
2597 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002598 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002599 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002600 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002601 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002602 break;
2603 }
Calin Juravle11351682014-10-23 15:38:15 +01002604 case Primitive::kPrimFloat:
2605 case Primitive::kPrimDouble: {
2606 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002607 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002608 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002609 break;
Calin Juravle11351682014-10-23 15:38:15 +01002610 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002611 default:
Calin Juravle11351682014-10-23 15:38:15 +01002612 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002613 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002614}
2615
2616void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2617 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002618 Location first = locations->InAt(0);
2619 Location second = locations->InAt(1);
2620 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002621 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002622 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002623 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002624 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002625 } else if (second.IsConstant()) {
2626 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002627 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002628 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002629 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002630 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002631 break;
2632 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002633 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002634 if (second.IsConstant()) {
2635 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2636 DCHECK(IsInt<32>(value));
2637 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2638 } else {
2639 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2640 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002641 break;
2642 }
2643
Calin Juravle11351682014-10-23 15:38:15 +01002644 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002645 if (second.IsFpuRegister()) {
2646 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2647 } else if (second.IsConstant()) {
2648 __ subss(first.AsFpuRegister<XmmRegister>(),
2649 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2650 } else {
2651 DCHECK(second.IsStackSlot());
2652 __ subss(first.AsFpuRegister<XmmRegister>(),
2653 Address(CpuRegister(RSP), second.GetStackIndex()));
2654 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002655 break;
Calin Juravle11351682014-10-23 15:38:15 +01002656 }
2657
2658 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002659 if (second.IsFpuRegister()) {
2660 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2661 } else if (second.IsConstant()) {
2662 __ subsd(first.AsFpuRegister<XmmRegister>(),
2663 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2664 } else {
2665 DCHECK(second.IsDoubleStackSlot());
2666 __ subsd(first.AsFpuRegister<XmmRegister>(),
2667 Address(CpuRegister(RSP), second.GetStackIndex()));
2668 }
Calin Juravle11351682014-10-23 15:38:15 +01002669 break;
2670 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002671
2672 default:
Calin Juravle11351682014-10-23 15:38:15 +01002673 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002674 }
2675}
2676
Calin Juravle34bacdf2014-10-07 20:23:36 +01002677void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2678 LocationSummary* locations =
2679 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2680 switch (mul->GetResultType()) {
2681 case Primitive::kPrimInt: {
2682 locations->SetInAt(0, Location::RequiresRegister());
2683 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002684 if (mul->InputAt(1)->IsIntConstant()) {
2685 // Can use 3 operand multiply.
2686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2687 } else {
2688 locations->SetOut(Location::SameAsFirstInput());
2689 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002690 break;
2691 }
2692 case Primitive::kPrimLong: {
2693 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002694 locations->SetInAt(1, Location::Any());
2695 if (mul->InputAt(1)->IsLongConstant() &&
2696 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002697 // Can use 3 operand multiply.
2698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2699 } else {
2700 locations->SetOut(Location::SameAsFirstInput());
2701 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002702 break;
2703 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002704 case Primitive::kPrimFloat:
2705 case Primitive::kPrimDouble: {
2706 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002707 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002708 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002709 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002710 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002711
2712 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002713 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002714 }
2715}
2716
2717void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2718 LocationSummary* locations = mul->GetLocations();
2719 Location first = locations->InAt(0);
2720 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002721 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002722 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002723 case Primitive::kPrimInt:
2724 // The constant may have ended up in a register, so test explicitly to avoid
2725 // problems where the output may not be the same as the first operand.
2726 if (mul->InputAt(1)->IsIntConstant()) {
2727 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2728 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2729 } else if (second.IsRegister()) {
2730 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002731 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002732 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002733 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002734 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002735 __ imull(first.AsRegister<CpuRegister>(),
2736 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002737 }
2738 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002739 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002740 // The constant may have ended up in a register, so test explicitly to avoid
2741 // problems where the output may not be the same as the first operand.
2742 if (mul->InputAt(1)->IsLongConstant()) {
2743 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2744 if (IsInt<32>(value)) {
2745 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2746 Immediate(static_cast<int32_t>(value)));
2747 } else {
2748 // Have to use the constant area.
2749 DCHECK(first.Equals(out));
2750 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2751 }
2752 } else if (second.IsRegister()) {
2753 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002754 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002755 } else {
2756 DCHECK(second.IsDoubleStackSlot());
2757 DCHECK(first.Equals(out));
2758 __ imulq(first.AsRegister<CpuRegister>(),
2759 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002760 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002761 break;
2762 }
2763
Calin Juravleb5bfa962014-10-21 18:02:24 +01002764 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002765 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002766 if (second.IsFpuRegister()) {
2767 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2768 } else if (second.IsConstant()) {
2769 __ mulss(first.AsFpuRegister<XmmRegister>(),
2770 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2771 } else {
2772 DCHECK(second.IsStackSlot());
2773 __ mulss(first.AsFpuRegister<XmmRegister>(),
2774 Address(CpuRegister(RSP), second.GetStackIndex()));
2775 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002776 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002777 }
2778
2779 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002780 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002781 if (second.IsFpuRegister()) {
2782 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2783 } else if (second.IsConstant()) {
2784 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2785 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2786 } else {
2787 DCHECK(second.IsDoubleStackSlot());
2788 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2789 Address(CpuRegister(RSP), second.GetStackIndex()));
2790 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002791 break;
2792 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002793
2794 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002795 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002796 }
2797}
2798
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002799void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2800 uint32_t stack_adjustment, bool is_float) {
2801 if (source.IsStackSlot()) {
2802 DCHECK(is_float);
2803 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2804 } else if (source.IsDoubleStackSlot()) {
2805 DCHECK(!is_float);
2806 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2807 } else {
2808 // Write the value to the temporary location on the stack and load to FP stack.
2809 if (is_float) {
2810 Location stack_temp = Location::StackSlot(temp_offset);
2811 codegen_->Move(stack_temp, source);
2812 __ flds(Address(CpuRegister(RSP), temp_offset));
2813 } else {
2814 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2815 codegen_->Move(stack_temp, source);
2816 __ fldl(Address(CpuRegister(RSP), temp_offset));
2817 }
2818 }
2819}
2820
2821void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2822 Primitive::Type type = rem->GetResultType();
2823 bool is_float = type == Primitive::kPrimFloat;
2824 size_t elem_size = Primitive::ComponentSize(type);
2825 LocationSummary* locations = rem->GetLocations();
2826 Location first = locations->InAt(0);
2827 Location second = locations->InAt(1);
2828 Location out = locations->Out();
2829
2830 // Create stack space for 2 elements.
2831 // TODO: enhance register allocator to ask for stack temporaries.
2832 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2833
2834 // Load the values to the FP stack in reverse order, using temporaries if needed.
2835 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2836 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2837
2838 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002839 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002840 __ Bind(&retry);
2841 __ fprem();
2842
2843 // Move FP status to AX.
2844 __ fstsw();
2845
2846 // And see if the argument reduction is complete. This is signaled by the
2847 // C2 FPU flag bit set to 0.
2848 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2849 __ j(kNotEqual, &retry);
2850
2851 // We have settled on the final value. Retrieve it into an XMM register.
2852 // Store FP top of stack to real stack.
2853 if (is_float) {
2854 __ fsts(Address(CpuRegister(RSP), 0));
2855 } else {
2856 __ fstl(Address(CpuRegister(RSP), 0));
2857 }
2858
2859 // Pop the 2 items from the FP stack.
2860 __ fucompp();
2861
2862 // Load the value from the stack into an XMM register.
2863 DCHECK(out.IsFpuRegister()) << out;
2864 if (is_float) {
2865 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2866 } else {
2867 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2868 }
2869
2870 // And remove the temporary stack space we allocated.
2871 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2872}
2873
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002874void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2875 DCHECK(instruction->IsDiv() || instruction->IsRem());
2876
2877 LocationSummary* locations = instruction->GetLocations();
2878 Location second = locations->InAt(1);
2879 DCHECK(second.IsConstant());
2880
2881 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2882 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002883 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002884
2885 DCHECK(imm == 1 || imm == -1);
2886
2887 switch (instruction->GetResultType()) {
2888 case Primitive::kPrimInt: {
2889 if (instruction->IsRem()) {
2890 __ xorl(output_register, output_register);
2891 } else {
2892 __ movl(output_register, input_register);
2893 if (imm == -1) {
2894 __ negl(output_register);
2895 }
2896 }
2897 break;
2898 }
2899
2900 case Primitive::kPrimLong: {
2901 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002902 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002903 } else {
2904 __ movq(output_register, input_register);
2905 if (imm == -1) {
2906 __ negq(output_register);
2907 }
2908 }
2909 break;
2910 }
2911
2912 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002913 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002914 }
2915}
2916
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002917void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002918 LocationSummary* locations = instruction->GetLocations();
2919 Location second = locations->InAt(1);
2920
2921 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2922 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2923
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002924 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002925
2926 DCHECK(IsPowerOfTwo(std::abs(imm)));
2927
2928 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2929
2930 if (instruction->GetResultType() == Primitive::kPrimInt) {
2931 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2932 __ testl(numerator, numerator);
2933 __ cmov(kGreaterEqual, tmp, numerator);
2934 int shift = CTZ(imm);
2935 __ sarl(tmp, Immediate(shift));
2936
2937 if (imm < 0) {
2938 __ negl(tmp);
2939 }
2940
2941 __ movl(output_register, tmp);
2942 } else {
2943 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2944 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2945
Mark Mendell92e83bf2015-05-07 11:25:03 -04002946 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002947 __ addq(rdx, numerator);
2948 __ testq(numerator, numerator);
2949 __ cmov(kGreaterEqual, rdx, numerator);
2950 int shift = CTZ(imm);
2951 __ sarq(rdx, Immediate(shift));
2952
2953 if (imm < 0) {
2954 __ negq(rdx);
2955 }
2956
2957 __ movq(output_register, rdx);
2958 }
2959}
2960
2961void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2962 DCHECK(instruction->IsDiv() || instruction->IsRem());
2963
2964 LocationSummary* locations = instruction->GetLocations();
2965 Location second = locations->InAt(1);
2966
2967 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2968 : locations->GetTemp(0).AsRegister<CpuRegister>();
2969 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2970 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2971 : locations->Out().AsRegister<CpuRegister>();
2972 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2973
2974 DCHECK_EQ(RAX, eax.AsRegister());
2975 DCHECK_EQ(RDX, edx.AsRegister());
2976 if (instruction->IsDiv()) {
2977 DCHECK_EQ(RAX, out.AsRegister());
2978 } else {
2979 DCHECK_EQ(RDX, out.AsRegister());
2980 }
2981
2982 int64_t magic;
2983 int shift;
2984
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002985 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002986 if (instruction->GetResultType() == Primitive::kPrimInt) {
2987 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2988
2989 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2990
2991 __ movl(numerator, eax);
2992
Mark Mendell0c9497d2015-08-21 09:30:05 -04002993 NearLabel no_div;
2994 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002995 __ testl(eax, eax);
2996 __ j(kNotEqual, &no_div);
2997
2998 __ xorl(out, out);
2999 __ jmp(&end);
3000
3001 __ Bind(&no_div);
3002
3003 __ movl(eax, Immediate(magic));
3004 __ imull(numerator);
3005
3006 if (imm > 0 && magic < 0) {
3007 __ addl(edx, numerator);
3008 } else if (imm < 0 && magic > 0) {
3009 __ subl(edx, numerator);
3010 }
3011
3012 if (shift != 0) {
3013 __ sarl(edx, Immediate(shift));
3014 }
3015
3016 __ movl(eax, edx);
3017 __ shrl(edx, Immediate(31));
3018 __ addl(edx, eax);
3019
3020 if (instruction->IsRem()) {
3021 __ movl(eax, numerator);
3022 __ imull(edx, Immediate(imm));
3023 __ subl(eax, edx);
3024 __ movl(edx, eax);
3025 } else {
3026 __ movl(eax, edx);
3027 }
3028 __ Bind(&end);
3029 } else {
3030 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3031
3032 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3033
3034 CpuRegister rax = eax;
3035 CpuRegister rdx = edx;
3036
3037 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3038
3039 // Save the numerator.
3040 __ movq(numerator, rax);
3041
3042 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003043 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003044
3045 // RDX:RAX = magic * numerator
3046 __ imulq(numerator);
3047
3048 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003049 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003050 __ addq(rdx, numerator);
3051 } else if (imm < 0 && magic > 0) {
3052 // RDX -= numerator
3053 __ subq(rdx, numerator);
3054 }
3055
3056 // Shift if needed.
3057 if (shift != 0) {
3058 __ sarq(rdx, Immediate(shift));
3059 }
3060
3061 // RDX += 1 if RDX < 0
3062 __ movq(rax, rdx);
3063 __ shrq(rdx, Immediate(63));
3064 __ addq(rdx, rax);
3065
3066 if (instruction->IsRem()) {
3067 __ movq(rax, numerator);
3068
3069 if (IsInt<32>(imm)) {
3070 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3071 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003072 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003073 }
3074
3075 __ subq(rax, rdx);
3076 __ movq(rdx, rax);
3077 } else {
3078 __ movq(rax, rdx);
3079 }
3080 }
3081}
3082
Calin Juravlebacfec32014-11-14 15:54:36 +00003083void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3084 DCHECK(instruction->IsDiv() || instruction->IsRem());
3085 Primitive::Type type = instruction->GetResultType();
3086 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3087
3088 bool is_div = instruction->IsDiv();
3089 LocationSummary* locations = instruction->GetLocations();
3090
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003091 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3092 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003093
Roland Levillain271ab9c2014-11-27 15:23:57 +00003094 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003095 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003096
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003097 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003098 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003099
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003100 if (imm == 0) {
3101 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3102 } else if (imm == 1 || imm == -1) {
3103 DivRemOneOrMinusOne(instruction);
3104 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003105 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003106 } else {
3107 DCHECK(imm <= -2 || imm >= 2);
3108 GenerateDivRemWithAnyConstant(instruction);
3109 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003110 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003111 SlowPathCodeX86_64* slow_path =
3112 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3113 out.AsRegister(), type, is_div);
3114 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003115
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003116 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3117 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3118 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3119 // so it's safe to just use negl instead of more complex comparisons.
3120 if (type == Primitive::kPrimInt) {
3121 __ cmpl(second_reg, Immediate(-1));
3122 __ j(kEqual, slow_path->GetEntryLabel());
3123 // edx:eax <- sign-extended of eax
3124 __ cdq();
3125 // eax = quotient, edx = remainder
3126 __ idivl(second_reg);
3127 } else {
3128 __ cmpq(second_reg, Immediate(-1));
3129 __ j(kEqual, slow_path->GetEntryLabel());
3130 // rdx:rax <- sign-extended of rax
3131 __ cqo();
3132 // rax = quotient, rdx = remainder
3133 __ idivq(second_reg);
3134 }
3135 __ Bind(slow_path->GetExitLabel());
3136 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003137}
3138
Calin Juravle7c4954d2014-10-28 16:57:40 +00003139void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3140 LocationSummary* locations =
3141 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3142 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003143 case Primitive::kPrimInt:
3144 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003145 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003146 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003147 locations->SetOut(Location::SameAsFirstInput());
3148 // Intel uses edx:eax as the dividend.
3149 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003150 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3151 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3152 // output and request another temp.
3153 if (div->InputAt(1)->IsConstant()) {
3154 locations->AddTemp(Location::RequiresRegister());
3155 }
Calin Juravled0d48522014-11-04 16:40:20 +00003156 break;
3157 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003158
Calin Juravle7c4954d2014-10-28 16:57:40 +00003159 case Primitive::kPrimFloat:
3160 case Primitive::kPrimDouble: {
3161 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003162 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003163 locations->SetOut(Location::SameAsFirstInput());
3164 break;
3165 }
3166
3167 default:
3168 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3169 }
3170}
3171
3172void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3173 LocationSummary* locations = div->GetLocations();
3174 Location first = locations->InAt(0);
3175 Location second = locations->InAt(1);
3176 DCHECK(first.Equals(locations->Out()));
3177
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003178 Primitive::Type type = div->GetResultType();
3179 switch (type) {
3180 case Primitive::kPrimInt:
3181 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003182 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003183 break;
3184 }
3185
Calin Juravle7c4954d2014-10-28 16:57:40 +00003186 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003187 if (second.IsFpuRegister()) {
3188 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3189 } else if (second.IsConstant()) {
3190 __ divss(first.AsFpuRegister<XmmRegister>(),
3191 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3192 } else {
3193 DCHECK(second.IsStackSlot());
3194 __ divss(first.AsFpuRegister<XmmRegister>(),
3195 Address(CpuRegister(RSP), second.GetStackIndex()));
3196 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003197 break;
3198 }
3199
3200 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003201 if (second.IsFpuRegister()) {
3202 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3203 } else if (second.IsConstant()) {
3204 __ divsd(first.AsFpuRegister<XmmRegister>(),
3205 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3206 } else {
3207 DCHECK(second.IsDoubleStackSlot());
3208 __ divsd(first.AsFpuRegister<XmmRegister>(),
3209 Address(CpuRegister(RSP), second.GetStackIndex()));
3210 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003211 break;
3212 }
3213
3214 default:
3215 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3216 }
3217}
3218
Calin Juravlebacfec32014-11-14 15:54:36 +00003219void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003220 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003221 LocationSummary* locations =
3222 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003223
3224 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003225 case Primitive::kPrimInt:
3226 case Primitive::kPrimLong: {
3227 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003228 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003229 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3230 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003231 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3232 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3233 // output and request another temp.
3234 if (rem->InputAt(1)->IsConstant()) {
3235 locations->AddTemp(Location::RequiresRegister());
3236 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003237 break;
3238 }
3239
3240 case Primitive::kPrimFloat:
3241 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003242 locations->SetInAt(0, Location::Any());
3243 locations->SetInAt(1, Location::Any());
3244 locations->SetOut(Location::RequiresFpuRegister());
3245 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003246 break;
3247 }
3248
3249 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003250 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003251 }
3252}
3253
3254void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3255 Primitive::Type type = rem->GetResultType();
3256 switch (type) {
3257 case Primitive::kPrimInt:
3258 case Primitive::kPrimLong: {
3259 GenerateDivRemIntegral(rem);
3260 break;
3261 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003262 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003263 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003264 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003265 break;
3266 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003267 default:
3268 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3269 }
3270}
3271
Calin Juravled0d48522014-11-04 16:40:20 +00003272void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003273 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3274 ? LocationSummary::kCallOnSlowPath
3275 : LocationSummary::kNoCall;
3276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003277 locations->SetInAt(0, Location::Any());
3278 if (instruction->HasUses()) {
3279 locations->SetOut(Location::SameAsFirstInput());
3280 }
3281}
3282
3283void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3284 SlowPathCodeX86_64* slow_path =
3285 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3286 codegen_->AddSlowPath(slow_path);
3287
3288 LocationSummary* locations = instruction->GetLocations();
3289 Location value = locations->InAt(0);
3290
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003291 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003292 case Primitive::kPrimByte:
3293 case Primitive::kPrimChar:
3294 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003295 case Primitive::kPrimInt: {
3296 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003297 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003298 __ j(kEqual, slow_path->GetEntryLabel());
3299 } else if (value.IsStackSlot()) {
3300 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3301 __ j(kEqual, slow_path->GetEntryLabel());
3302 } else {
3303 DCHECK(value.IsConstant()) << value;
3304 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3305 __ jmp(slow_path->GetEntryLabel());
3306 }
3307 }
3308 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003309 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003310 case Primitive::kPrimLong: {
3311 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003312 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003313 __ j(kEqual, slow_path->GetEntryLabel());
3314 } else if (value.IsDoubleStackSlot()) {
3315 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3316 __ j(kEqual, slow_path->GetEntryLabel());
3317 } else {
3318 DCHECK(value.IsConstant()) << value;
3319 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3320 __ jmp(slow_path->GetEntryLabel());
3321 }
3322 }
3323 break;
3324 }
3325 default:
3326 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003327 }
Calin Juravled0d48522014-11-04 16:40:20 +00003328}
3329
Calin Juravle9aec02f2014-11-18 23:06:35 +00003330void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3331 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3332
3333 LocationSummary* locations =
3334 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3335
3336 switch (op->GetResultType()) {
3337 case Primitive::kPrimInt:
3338 case Primitive::kPrimLong: {
3339 locations->SetInAt(0, Location::RequiresRegister());
3340 // The shift count needs to be in CL.
3341 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3342 locations->SetOut(Location::SameAsFirstInput());
3343 break;
3344 }
3345 default:
3346 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3347 }
3348}
3349
3350void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3351 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3352
3353 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003354 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003355 Location second = locations->InAt(1);
3356
3357 switch (op->GetResultType()) {
3358 case Primitive::kPrimInt: {
3359 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003360 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003361 if (op->IsShl()) {
3362 __ shll(first_reg, second_reg);
3363 } else if (op->IsShr()) {
3364 __ sarl(first_reg, second_reg);
3365 } else {
3366 __ shrl(first_reg, second_reg);
3367 }
3368 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003369 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003370 if (op->IsShl()) {
3371 __ shll(first_reg, imm);
3372 } else if (op->IsShr()) {
3373 __ sarl(first_reg, imm);
3374 } else {
3375 __ shrl(first_reg, imm);
3376 }
3377 }
3378 break;
3379 }
3380 case Primitive::kPrimLong: {
3381 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003382 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003383 if (op->IsShl()) {
3384 __ shlq(first_reg, second_reg);
3385 } else if (op->IsShr()) {
3386 __ sarq(first_reg, second_reg);
3387 } else {
3388 __ shrq(first_reg, second_reg);
3389 }
3390 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003391 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003392 if (op->IsShl()) {
3393 __ shlq(first_reg, imm);
3394 } else if (op->IsShr()) {
3395 __ sarq(first_reg, imm);
3396 } else {
3397 __ shrq(first_reg, imm);
3398 }
3399 }
3400 break;
3401 }
3402 default:
3403 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3404 }
3405}
3406
3407void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3408 HandleShift(shl);
3409}
3410
3411void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3412 HandleShift(shl);
3413}
3414
3415void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3416 HandleShift(shr);
3417}
3418
3419void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3420 HandleShift(shr);
3421}
3422
3423void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3424 HandleShift(ushr);
3425}
3426
3427void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3428 HandleShift(ushr);
3429}
3430
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003431void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003432 LocationSummary* locations =
3433 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003434 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003435 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003436 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003437 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003438}
3439
3440void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3441 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003442 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3443 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003444 // Note: if heap poisoning is enabled, the entry point takes cares
3445 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003446
Calin Juravle175dc732015-08-25 15:42:32 +01003447 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3448 instruction,
3449 instruction->GetDexPc(),
3450 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003451
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003452 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003453}
3454
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003455void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3456 LocationSummary* locations =
3457 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3458 InvokeRuntimeCallingConvention calling_convention;
3459 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003460 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003461 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003462 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003463}
3464
3465void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3466 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003467 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3468 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003469
Roland Levillain4d027112015-07-01 15:41:14 +01003470 // Note: if heap poisoning is enabled, the entry point takes cares
3471 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003472 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3473 instruction,
3474 instruction->GetDexPc(),
3475 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003476
3477 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003478}
3479
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003480void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003481 LocationSummary* locations =
3482 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003483 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3484 if (location.IsStackSlot()) {
3485 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3486 } else if (location.IsDoubleStackSlot()) {
3487 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3488 }
3489 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003490}
3491
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003492void InstructionCodeGeneratorX86_64::VisitParameterValue(
3493 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003494 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003495}
3496
3497void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3498 LocationSummary* locations =
3499 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3500 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3501}
3502
3503void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3504 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3505 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003506}
3507
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003508void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003509 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003510 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003511 locations->SetInAt(0, Location::RequiresRegister());
3512 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003513}
3514
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003515void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3516 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003517 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3518 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003519 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003520 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003521 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003522 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003523 break;
3524
3525 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003526 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003527 break;
3528
3529 default:
3530 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3531 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003532}
3533
David Brazdil66d126e2015-04-03 16:02:44 +01003534void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3535 LocationSummary* locations =
3536 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3537 locations->SetInAt(0, Location::RequiresRegister());
3538 locations->SetOut(Location::SameAsFirstInput());
3539}
3540
3541void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003542 LocationSummary* locations = bool_not->GetLocations();
3543 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3544 locations->Out().AsRegister<CpuRegister>().AsRegister());
3545 Location out = locations->Out();
3546 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3547}
3548
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003549void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003550 LocationSummary* locations =
3551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003552 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3553 locations->SetInAt(i, Location::Any());
3554 }
3555 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003556}
3557
3558void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003559 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003560 LOG(FATAL) << "Unimplemented";
3561}
3562
Calin Juravle52c48962014-12-16 17:02:57 +00003563void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3564 /*
3565 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3566 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3567 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3568 */
3569 switch (kind) {
3570 case MemBarrierKind::kAnyAny: {
3571 __ mfence();
3572 break;
3573 }
3574 case MemBarrierKind::kAnyStore:
3575 case MemBarrierKind::kLoadAny:
3576 case MemBarrierKind::kStoreStore: {
3577 // nop
3578 break;
3579 }
3580 default:
3581 LOG(FATAL) << "Unexpected memory barier " << kind;
3582 }
3583}
3584
3585void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3586 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3587
Nicolas Geoffray39468442014-09-02 15:17:15 +01003588 LocationSummary* locations =
3589 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003590 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003591 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3592 locations->SetOut(Location::RequiresFpuRegister());
3593 } else {
3594 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3595 }
Calin Juravle52c48962014-12-16 17:02:57 +00003596}
3597
3598void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3599 const FieldInfo& field_info) {
3600 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3601
3602 LocationSummary* locations = instruction->GetLocations();
3603 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3604 Location out = locations->Out();
3605 bool is_volatile = field_info.IsVolatile();
3606 Primitive::Type field_type = field_info.GetFieldType();
3607 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3608
3609 switch (field_type) {
3610 case Primitive::kPrimBoolean: {
3611 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3612 break;
3613 }
3614
3615 case Primitive::kPrimByte: {
3616 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3617 break;
3618 }
3619
3620 case Primitive::kPrimShort: {
3621 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3622 break;
3623 }
3624
3625 case Primitive::kPrimChar: {
3626 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3627 break;
3628 }
3629
3630 case Primitive::kPrimInt:
3631 case Primitive::kPrimNot: {
3632 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3633 break;
3634 }
3635
3636 case Primitive::kPrimLong: {
3637 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3638 break;
3639 }
3640
3641 case Primitive::kPrimFloat: {
3642 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3643 break;
3644 }
3645
3646 case Primitive::kPrimDouble: {
3647 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3648 break;
3649 }
3650
3651 case Primitive::kPrimVoid:
3652 LOG(FATAL) << "Unreachable type " << field_type;
3653 UNREACHABLE();
3654 }
3655
Calin Juravle77520bc2015-01-12 18:45:46 +00003656 codegen_->MaybeRecordImplicitNullCheck(instruction);
3657
Calin Juravle52c48962014-12-16 17:02:57 +00003658 if (is_volatile) {
3659 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3660 }
Roland Levillain4d027112015-07-01 15:41:14 +01003661
3662 if (field_type == Primitive::kPrimNot) {
3663 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3664 }
Calin Juravle52c48962014-12-16 17:02:57 +00003665}
3666
3667void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3668 const FieldInfo& field_info) {
3669 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3670
3671 LocationSummary* locations =
3672 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003673 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003674 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003675 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003676
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003677 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003678 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3679 locations->SetInAt(1, Location::RequiresFpuRegister());
3680 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003681 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003682 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003683 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003684 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003685 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003686 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003687 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3688 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003689 locations->AddTemp(Location::RequiresRegister());
3690 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003691}
3692
Calin Juravle52c48962014-12-16 17:02:57 +00003693void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003694 const FieldInfo& field_info,
3695 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003696 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3697
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003698 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003699 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3700 Location value = locations->InAt(1);
3701 bool is_volatile = field_info.IsVolatile();
3702 Primitive::Type field_type = field_info.GetFieldType();
3703 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3704
3705 if (is_volatile) {
3706 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3707 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003708
3709 switch (field_type) {
3710 case Primitive::kPrimBoolean:
3711 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003712 if (value.IsConstant()) {
3713 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3714 __ movb(Address(base, offset), Immediate(v));
3715 } else {
3716 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3717 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003718 break;
3719 }
3720
3721 case Primitive::kPrimShort:
3722 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003723 if (value.IsConstant()) {
3724 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3725 __ movw(Address(base, offset), Immediate(v));
3726 } else {
3727 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3728 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003729 break;
3730 }
3731
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003732 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003733 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003734 if (value.IsConstant()) {
3735 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003736 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3737 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3738 // Note: if heap poisoning is enabled, no need to poison
3739 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003740 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003741 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003742 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3743 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3744 __ movl(temp, value.AsRegister<CpuRegister>());
3745 __ PoisonHeapReference(temp);
3746 __ movl(Address(base, offset), temp);
3747 } else {
3748 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3749 }
Mark Mendell40741f32015-04-20 22:10:34 -04003750 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003751 break;
3752 }
3753
3754 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003755 if (value.IsConstant()) {
3756 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3757 DCHECK(IsInt<32>(v));
3758 int32_t v_32 = v;
3759 __ movq(Address(base, offset), Immediate(v_32));
3760 } else {
3761 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3762 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003763 break;
3764 }
3765
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003766 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003767 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003768 break;
3769 }
3770
3771 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003772 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003773 break;
3774 }
3775
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003776 case Primitive::kPrimVoid:
3777 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003778 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003779 }
Calin Juravle52c48962014-12-16 17:02:57 +00003780
Calin Juravle77520bc2015-01-12 18:45:46 +00003781 codegen_->MaybeRecordImplicitNullCheck(instruction);
3782
3783 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3784 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3785 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003786 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003787 }
3788
Calin Juravle52c48962014-12-16 17:02:57 +00003789 if (is_volatile) {
3790 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3791 }
3792}
3793
3794void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3795 HandleFieldSet(instruction, instruction->GetFieldInfo());
3796}
3797
3798void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003799 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003800}
3801
3802void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003803 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003804}
3805
3806void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003807 HandleFieldGet(instruction, instruction->GetFieldInfo());
3808}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003809
Calin Juravle52c48962014-12-16 17:02:57 +00003810void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3811 HandleFieldGet(instruction);
3812}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003813
Calin Juravle52c48962014-12-16 17:02:57 +00003814void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3815 HandleFieldGet(instruction, instruction->GetFieldInfo());
3816}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003817
Calin Juravle52c48962014-12-16 17:02:57 +00003818void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3819 HandleFieldSet(instruction, instruction->GetFieldInfo());
3820}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003821
Calin Juravle52c48962014-12-16 17:02:57 +00003822void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003823 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003824}
3825
Calin Juravle23a8e352015-09-08 19:56:31 +01003826void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
3827 HUnresolvedInstanceFieldGet* instruction) {
3828 FieldAccessCallingConvetionX86_64 calling_convention;
3829 codegen_->CreateUnresolvedFieldLocationSummary(
3830 instruction, instruction->GetFieldType(), calling_convention);
3831}
3832
3833void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
3834 HUnresolvedInstanceFieldGet* instruction) {
3835 codegen_->GenerateUnresolvedFieldAccess(instruction,
3836 instruction->GetFieldType(),
3837 instruction->GetFieldIndex(),
3838 instruction->GetDexPc());
3839}
3840
3841void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
3842 HUnresolvedInstanceFieldSet* instruction) {
3843 FieldAccessCallingConvetionX86_64 calling_convention;
3844 codegen_->CreateUnresolvedFieldLocationSummary(
3845 instruction, instruction->GetFieldType(), calling_convention);
3846}
3847
3848void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
3849 HUnresolvedInstanceFieldSet* instruction) {
3850 codegen_->GenerateUnresolvedFieldAccess(instruction,
3851 instruction->GetFieldType(),
3852 instruction->GetFieldIndex(),
3853 instruction->GetDexPc());
3854}
3855
3856void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
3857 HUnresolvedStaticFieldGet* instruction) {
3858 FieldAccessCallingConvetionX86_64 calling_convention;
3859 codegen_->CreateUnresolvedFieldLocationSummary(
3860 instruction, instruction->GetFieldType(), calling_convention);
3861}
3862
3863void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
3864 HUnresolvedStaticFieldGet* instruction) {
3865 codegen_->GenerateUnresolvedFieldAccess(instruction,
3866 instruction->GetFieldType(),
3867 instruction->GetFieldIndex(),
3868 instruction->GetDexPc());
3869}
3870
3871void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
3872 HUnresolvedStaticFieldSet* instruction) {
3873 FieldAccessCallingConvetionX86_64 calling_convention;
3874 codegen_->CreateUnresolvedFieldLocationSummary(
3875 instruction, instruction->GetFieldType(), calling_convention);
3876}
3877
3878void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
3879 HUnresolvedStaticFieldSet* instruction) {
3880 codegen_->GenerateUnresolvedFieldAccess(instruction,
3881 instruction->GetFieldType(),
3882 instruction->GetFieldIndex(),
3883 instruction->GetDexPc());
3884}
3885
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003886void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003887 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3888 ? LocationSummary::kCallOnSlowPath
3889 : LocationSummary::kNoCall;
3890 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3891 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003892 ? Location::RequiresRegister()
3893 : Location::Any();
3894 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003895 if (instruction->HasUses()) {
3896 locations->SetOut(Location::SameAsFirstInput());
3897 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003898}
3899
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003900void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003901 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3902 return;
3903 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003904 LocationSummary* locations = instruction->GetLocations();
3905 Location obj = locations->InAt(0);
3906
3907 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3908 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3909}
3910
3911void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003912 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003913 codegen_->AddSlowPath(slow_path);
3914
3915 LocationSummary* locations = instruction->GetLocations();
3916 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003917
3918 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003919 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003920 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003921 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003922 } else {
3923 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00003924 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003925 __ jmp(slow_path->GetEntryLabel());
3926 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003927 }
3928 __ j(kEqual, slow_path->GetEntryLabel());
3929}
3930
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003931void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003932 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003933 GenerateImplicitNullCheck(instruction);
3934 } else {
3935 GenerateExplicitNullCheck(instruction);
3936 }
3937}
3938
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003939void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003940 LocationSummary* locations =
3941 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003942 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003943 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003944 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3945 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3946 } else {
3947 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3948 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003949}
3950
3951void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3952 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003953 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003954 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003955 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003956
Roland Levillain4d027112015-07-01 15:41:14 +01003957 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003958 case Primitive::kPrimBoolean: {
3959 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003960 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003961 if (index.IsConstant()) {
3962 __ movzxb(out, Address(obj,
3963 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3964 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003965 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003966 }
3967 break;
3968 }
3969
3970 case Primitive::kPrimByte: {
3971 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003972 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003973 if (index.IsConstant()) {
3974 __ movsxb(out, Address(obj,
3975 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3976 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003977 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003978 }
3979 break;
3980 }
3981
3982 case Primitive::kPrimShort: {
3983 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003984 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003985 if (index.IsConstant()) {
3986 __ movsxw(out, Address(obj,
3987 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3988 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003989 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003990 }
3991 break;
3992 }
3993
3994 case Primitive::kPrimChar: {
3995 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003996 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003997 if (index.IsConstant()) {
3998 __ movzxw(out, Address(obj,
3999 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4000 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004001 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004002 }
4003 break;
4004 }
4005
4006 case Primitive::kPrimInt:
4007 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01004008 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4009 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004010 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004011 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004012 if (index.IsConstant()) {
4013 __ movl(out, Address(obj,
4014 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4015 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004016 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004017 }
4018 break;
4019 }
4020
4021 case Primitive::kPrimLong: {
4022 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004023 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004024 if (index.IsConstant()) {
4025 __ movq(out, Address(obj,
4026 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4027 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004028 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004029 }
4030 break;
4031 }
4032
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004033 case Primitive::kPrimFloat: {
4034 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004035 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004036 if (index.IsConstant()) {
4037 __ movss(out, Address(obj,
4038 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4039 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004040 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004041 }
4042 break;
4043 }
4044
4045 case Primitive::kPrimDouble: {
4046 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004047 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004048 if (index.IsConstant()) {
4049 __ movsd(out, Address(obj,
4050 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4051 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004052 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004053 }
4054 break;
4055 }
4056
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004057 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004058 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004059 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004060 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004061 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004062
4063 if (type == Primitive::kPrimNot) {
4064 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4065 __ MaybeUnpoisonHeapReference(out);
4066 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004067}
4068
4069void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004070 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004071
4072 bool needs_write_barrier =
4073 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4074 bool needs_runtime_call = instruction->NeedsTypeCheck();
4075
Nicolas Geoffray39468442014-09-02 15:17:15 +01004076 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004077 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
4078 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004079 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004080 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4081 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4082 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004083 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004084 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004085 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004086 1, Location::RegisterOrConstant(instruction->InputAt(1)));
4087 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004088 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04004089 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004090 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
4091 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004092 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004093 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004094 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004095
4096 if (needs_write_barrier) {
4097 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004098 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004099 locations->AddTemp(Location::RequiresRegister());
4100 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004101 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004102}
4103
4104void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4105 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004106 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004107 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004108 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004109 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004110 bool needs_runtime_call = locations->WillCall();
4111 bool needs_write_barrier =
4112 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004113
4114 switch (value_type) {
4115 case Primitive::kPrimBoolean:
4116 case Primitive::kPrimByte: {
4117 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004118 if (index.IsConstant()) {
4119 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004120 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004121 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004122 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00004123 __ movb(Address(obj, offset),
4124 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004125 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004126 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004127 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004128 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
4129 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004130 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004131 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004132 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4133 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004134 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004135 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004136 break;
4137 }
4138
4139 case Primitive::kPrimShort:
4140 case Primitive::kPrimChar: {
4141 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004142 if (index.IsConstant()) {
4143 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004144 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004145 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004146 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004147 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00004148 __ movw(Address(obj, offset),
4149 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004150 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004151 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004152 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004153 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004154 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
4155 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004156 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004157 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00004158 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004159 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4160 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004161 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004162 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004163 break;
4164 }
4165
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004166 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004167 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004168 if (!needs_runtime_call) {
4169 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4170 if (index.IsConstant()) {
4171 size_t offset =
4172 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4173 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004174 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4175 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4176 __ movl(temp, value.AsRegister<CpuRegister>());
4177 __ PoisonHeapReference(temp);
4178 __ movl(Address(obj, offset), temp);
4179 } else {
4180 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
4181 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004182 } else {
4183 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004184 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004185 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4186 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4187 // Note: if heap poisoning is enabled, no need to poison
4188 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04004189 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004190 }
4191 } else {
4192 DCHECK(index.IsRegister()) << index;
4193 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004194 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4195 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4196 __ movl(temp, value.AsRegister<CpuRegister>());
4197 __ PoisonHeapReference(temp);
4198 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4199 } else {
4200 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4201 value.AsRegister<CpuRegister>());
4202 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004203 } else {
4204 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004205 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004206 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4207 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4208 // Note: if heap poisoning is enabled, no need to poison
4209 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004210 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004211 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004212 }
4213 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004214 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004215 if (needs_write_barrier) {
4216 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004217 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4218 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004219 codegen_->MarkGCCard(
4220 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004221 }
4222 } else {
4223 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004224 // Note: if heap poisoning is enabled, pAputObject takes cares
4225 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004226 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4227 instruction,
4228 instruction->GetDexPc(),
4229 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004230 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004231 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232 break;
4233 }
4234
4235 case Primitive::kPrimLong: {
4236 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004237 if (index.IsConstant()) {
4238 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004239 if (value.IsRegister()) {
4240 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4241 } else {
4242 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4243 DCHECK(IsInt<32>(v));
4244 int32_t v_32 = v;
4245 __ movq(Address(obj, offset), Immediate(v_32));
4246 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004247 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004248 if (value.IsRegister()) {
4249 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4250 value.AsRegister<CpuRegister>());
4251 } else {
4252 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4253 DCHECK(IsInt<32>(v));
4254 int32_t v_32 = v;
4255 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4256 Immediate(v_32));
4257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004258 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004259 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004260 break;
4261 }
4262
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004263 case Primitive::kPrimFloat: {
4264 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4265 if (index.IsConstant()) {
4266 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4267 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004268 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004269 } else {
4270 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004271 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4272 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004273 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004274 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004275 break;
4276 }
4277
4278 case Primitive::kPrimDouble: {
4279 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4280 if (index.IsConstant()) {
4281 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4282 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004283 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004284 } else {
4285 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004286 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4287 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004288 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004289 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004290 break;
4291 }
4292
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004293 case Primitive::kPrimVoid:
4294 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004295 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004296 }
4297}
4298
4299void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004300 LocationSummary* locations =
4301 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004302 locations->SetInAt(0, Location::RequiresRegister());
4303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004304}
4305
4306void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4307 LocationSummary* locations = instruction->GetLocations();
4308 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004309 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4310 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004311 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004312 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004313}
4314
4315void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004316 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4317 ? LocationSummary::kCallOnSlowPath
4318 : LocationSummary::kNoCall;
4319 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004320 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004321 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004322 if (instruction->HasUses()) {
4323 locations->SetOut(Location::SameAsFirstInput());
4324 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004325}
4326
4327void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4328 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004329 Location index_loc = locations->InAt(0);
4330 Location length_loc = locations->InAt(1);
4331 SlowPathCodeX86_64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004332 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004333
Mark Mendell99dbd682015-04-22 16:18:52 -04004334 if (length_loc.IsConstant()) {
4335 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4336 if (index_loc.IsConstant()) {
4337 // BCE will remove the bounds check if we are guarenteed to pass.
4338 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4339 if (index < 0 || index >= length) {
4340 codegen_->AddSlowPath(slow_path);
4341 __ jmp(slow_path->GetEntryLabel());
4342 } else {
4343 // Some optimization after BCE may have generated this, and we should not
4344 // generate a bounds check if it is a valid range.
4345 }
4346 return;
4347 }
4348
4349 // We have to reverse the jump condition because the length is the constant.
4350 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4351 __ cmpl(index_reg, Immediate(length));
4352 codegen_->AddSlowPath(slow_path);
4353 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004354 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004355 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4356 if (index_loc.IsConstant()) {
4357 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4358 __ cmpl(length, Immediate(value));
4359 } else {
4360 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4361 }
4362 codegen_->AddSlowPath(slow_path);
4363 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004364 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004365}
4366
4367void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4368 CpuRegister card,
4369 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004370 CpuRegister value,
4371 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004372 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004373 if (value_can_be_null) {
4374 __ testl(value, value);
4375 __ j(kEqual, &is_null);
4376 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004377 __ gs()->movq(card, Address::Absolute(
4378 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4379 __ movq(temp, object);
4380 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004381 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004382 if (value_can_be_null) {
4383 __ Bind(&is_null);
4384 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004385}
4386
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004387void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4388 temp->SetLocations(nullptr);
4389}
4390
4391void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4392 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004393 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004394}
4395
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004396void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004397 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004398 LOG(FATAL) << "Unimplemented";
4399}
4400
4401void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004402 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4403}
4404
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004405void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4406 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4407}
4408
4409void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004410 HBasicBlock* block = instruction->GetBlock();
4411 if (block->GetLoopInformation() != nullptr) {
4412 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4413 // The back edge will generate the suspend check.
4414 return;
4415 }
4416 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4417 // The goto will generate the suspend check.
4418 return;
4419 }
4420 GenerateSuspendCheck(instruction, nullptr);
4421}
4422
4423void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4424 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004425 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004426 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4427 if (slow_path == nullptr) {
4428 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4429 instruction->SetSlowPath(slow_path);
4430 codegen_->AddSlowPath(slow_path);
4431 if (successor != nullptr) {
4432 DCHECK(successor->IsLoopHeader());
4433 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4434 }
4435 } else {
4436 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4437 }
4438
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004439 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004440 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004441 if (successor == nullptr) {
4442 __ j(kNotEqual, slow_path->GetEntryLabel());
4443 __ Bind(slow_path->GetReturnLabel());
4444 } else {
4445 __ j(kEqual, codegen_->GetLabelOf(successor));
4446 __ jmp(slow_path->GetEntryLabel());
4447 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004448}
4449
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004450X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4451 return codegen_->GetAssembler();
4452}
4453
4454void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4455 MoveOperands* move = moves_.Get(index);
4456 Location source = move->GetSource();
4457 Location destination = move->GetDestination();
4458
4459 if (source.IsRegister()) {
4460 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004461 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004462 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004463 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004464 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004465 } else {
4466 DCHECK(destination.IsDoubleStackSlot());
4467 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004468 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004469 }
4470 } else if (source.IsStackSlot()) {
4471 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004472 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004473 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004474 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004475 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004476 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004477 } else {
4478 DCHECK(destination.IsStackSlot());
4479 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4480 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4481 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004482 } else if (source.IsDoubleStackSlot()) {
4483 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004484 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004485 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004486 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004487 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4488 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004489 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004490 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004491 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4492 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4493 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004494 } else if (source.IsConstant()) {
4495 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004496 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4497 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004498 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004499 if (value == 0) {
4500 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4501 } else {
4502 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4503 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004504 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004505 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004506 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004507 }
4508 } else if (constant->IsLongConstant()) {
4509 int64_t value = constant->AsLongConstant()->GetValue();
4510 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004511 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004512 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004513 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004514 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004515 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004516 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004517 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004518 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004519 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004520 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4521 if (value == 0) {
4522 // easy FP 0.0.
4523 __ xorps(dest, dest);
4524 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004525 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004526 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004527 } else {
4528 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004529 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004530 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4531 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004532 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004533 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004534 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004535 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004536 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004537 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4538 if (value == 0) {
4539 __ xorpd(dest, dest);
4540 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004541 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004542 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004543 } else {
4544 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004545 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004546 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004547 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004548 } else if (source.IsFpuRegister()) {
4549 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004550 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004551 } else if (destination.IsStackSlot()) {
4552 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004553 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004554 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004555 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004556 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004557 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004558 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004559 }
4560}
4561
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004562void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004563 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004564 __ movl(Address(CpuRegister(RSP), mem), reg);
4565 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004566}
4567
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004568void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004569 ScratchRegisterScope ensure_scratch(
4570 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4571
4572 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4573 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4574 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4575 Address(CpuRegister(RSP), mem2 + stack_offset));
4576 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4577 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4578 CpuRegister(ensure_scratch.GetRegister()));
4579}
4580
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004581void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4582 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4583 __ movq(Address(CpuRegister(RSP), mem), reg);
4584 __ movq(reg, CpuRegister(TMP));
4585}
4586
4587void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4588 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004589 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004590
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004591 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4592 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4593 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4594 Address(CpuRegister(RSP), mem2 + stack_offset));
4595 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4596 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4597 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004598}
4599
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004600void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4601 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4602 __ movss(Address(CpuRegister(RSP), mem), reg);
4603 __ movd(reg, CpuRegister(TMP));
4604}
4605
4606void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4607 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4608 __ movsd(Address(CpuRegister(RSP), mem), reg);
4609 __ movd(reg, CpuRegister(TMP));
4610}
4611
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004612void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4613 MoveOperands* move = moves_.Get(index);
4614 Location source = move->GetSource();
4615 Location destination = move->GetDestination();
4616
4617 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004618 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004619 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004620 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004621 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004622 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004623 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004624 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4625 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004626 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004627 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004628 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004629 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4630 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004631 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004632 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4633 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4634 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004635 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004636 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004637 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004638 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004639 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004640 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004641 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004642 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004643 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004644 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004645 }
4646}
4647
4648
4649void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4650 __ pushq(CpuRegister(reg));
4651}
4652
4653
4654void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4655 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004656}
4657
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004658void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4659 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4660 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4661 Immediate(mirror::Class::kStatusInitialized));
4662 __ j(kLess, slow_path->GetEntryLabel());
4663 __ Bind(slow_path->GetExitLabel());
4664 // No need for memory fence, thanks to the X86_64 memory model.
4665}
4666
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004667void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004668 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4669 ? LocationSummary::kCallOnSlowPath
4670 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004671 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004672 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004673 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004674 locations->SetOut(Location::RequiresRegister());
4675}
4676
4677void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004678 LocationSummary* locations = cls->GetLocations();
4679 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4680 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004681 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004682 DCHECK(!cls->CanCallRuntime());
4683 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004684 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004685 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004686 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004687 __ movq(out, Address(
4688 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004689 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004690 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004691
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004692 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4693 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4694 codegen_->AddSlowPath(slow_path);
4695 __ testl(out, out);
4696 __ j(kEqual, slow_path->GetEntryLabel());
4697 if (cls->MustGenerateClinitCheck()) {
4698 GenerateClassInitializationCheck(slow_path, out);
4699 } else {
4700 __ Bind(slow_path->GetExitLabel());
4701 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004702 }
4703}
4704
4705void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4706 LocationSummary* locations =
4707 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4708 locations->SetInAt(0, Location::RequiresRegister());
4709 if (check->HasUses()) {
4710 locations->SetOut(Location::SameAsFirstInput());
4711 }
4712}
4713
4714void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004715 // We assume the class to not be null.
4716 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4717 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004718 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004719 GenerateClassInitializationCheck(slow_path,
4720 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004721}
4722
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004723void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4724 LocationSummary* locations =
4725 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004726 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004727 locations->SetOut(Location::RequiresRegister());
4728}
4729
4730void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4731 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4732 codegen_->AddSlowPath(slow_path);
4733
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004734 LocationSummary* locations = load->GetLocations();
4735 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4736 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004737 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004738 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004739 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004740 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004741 __ testl(out, out);
4742 __ j(kEqual, slow_path->GetEntryLabel());
4743 __ Bind(slow_path->GetExitLabel());
4744}
4745
David Brazdilcb1c0552015-08-04 16:22:25 +01004746static Address GetExceptionTlsAddress() {
4747 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4748}
4749
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004750void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4751 LocationSummary* locations =
4752 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4753 locations->SetOut(Location::RequiresRegister());
4754}
4755
4756void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004757 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4758}
4759
4760void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4761 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4762}
4763
4764void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4765 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004766}
4767
4768void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4769 LocationSummary* locations =
4770 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4771 InvokeRuntimeCallingConvention calling_convention;
4772 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4773}
4774
4775void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004776 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4777 instruction,
4778 instruction->GetDexPc(),
4779 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004780}
4781
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004782void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray75374372015-09-17 17:12:19 +00004783 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4784 ? LocationSummary::kNoCall
4785 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004786 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray75374372015-09-17 17:12:19 +00004787 locations->SetInAt(0, Location::RequiresRegister());
4788 locations->SetInAt(1, Location::Any());
4789 // Note that TypeCheckSlowPathX86_64 uses this register too.
4790 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004791}
4792
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004793void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004794 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004795 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004796 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004797 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004798 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004799 NearLabel done, zero;
Nicolas Geoffray75374372015-09-17 17:12:19 +00004800 SlowPathCodeX86_64* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004801
4802 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004803 // Avoid null check if we know obj is not null.
4804 if (instruction->MustDoNullCheck()) {
4805 __ testl(obj, obj);
4806 __ j(kEqual, &zero);
4807 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004808 // Compare the class of `obj` with `cls`.
4809 __ movl(out, Address(obj, class_offset));
4810 __ MaybeUnpoisonHeapReference(out);
4811 if (cls.IsRegister()) {
4812 __ cmpl(out, cls.AsRegister<CpuRegister>());
4813 } else {
4814 DCHECK(cls.IsStackSlot()) << cls;
4815 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4816 }
4817 if (instruction->IsClassFinal()) {
4818 // Classes must be equal for the instanceof to succeed.
4819 __ j(kNotEqual, &zero);
4820 __ movl(out, Immediate(1));
4821 __ jmp(&done);
4822 } else {
4823 // If the classes are not equal, we go into a slow path.
4824 DCHECK(locations->OnlyCallsOnSlowPath());
4825 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction);
4826 codegen_->AddSlowPath(slow_path);
4827 __ j(kNotEqual, slow_path->GetEntryLabel());
4828 __ movl(out, Immediate(1));
4829 __ jmp(&done);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004830 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004831
Nicolas Geoffray75374372015-09-17 17:12:19 +00004832 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004833 __ Bind(&zero);
Nicolas Geoffray75374372015-09-17 17:12:19 +00004834 __ movl(out, Immediate(0));
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004835 }
4836
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004837 if (slow_path != nullptr) {
4838 __ Bind(slow_path->GetExitLabel());
4839 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004840 __ Bind(&done);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004841}
4842
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004843void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4844 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray75374372015-09-17 17:12:19 +00004845 instruction, LocationSummary::kCallOnSlowPath);
4846 locations->SetInAt(0, Location::RequiresRegister());
4847 locations->SetInAt(1, Location::Any());
4848 // Note that TypeCheckSlowPathX86_64 uses this register too.
4849 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004850}
4851
4852void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4853 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004854 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004855 Location cls = locations->InAt(1);
Nicolas Geoffray75374372015-09-17 17:12:19 +00004856 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004857 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray75374372015-09-17 17:12:19 +00004858 SlowPathCodeX86_64* slow_path =
4859 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction);
4860 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004861
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004862 // Avoid null check if we know obj is not null.
4863 if (instruction->MustDoNullCheck()) {
4864 __ testl(obj, obj);
Nicolas Geoffray75374372015-09-17 17:12:19 +00004865 __ j(kEqual, slow_path->GetExitLabel());
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004866 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004867 // Compare the class of `obj` with `cls`.
4868 __ movl(temp, Address(obj, class_offset));
4869 __ MaybeUnpoisonHeapReference(temp);
4870 if (cls.IsRegister()) {
4871 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004872 } else {
Nicolas Geoffray75374372015-09-17 17:12:19 +00004873 DCHECK(cls.IsStackSlot()) << cls;
4874 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004875 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004876 // The checkcast succeeds if the classes are equal (fast path).
4877 // Otherwise, we need to go into the slow path to check the types.
4878 __ j(kNotEqual, slow_path->GetEntryLabel());
4879 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004880}
4881
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004882void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4883 LocationSummary* locations =
4884 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4885 InvokeRuntimeCallingConvention calling_convention;
4886 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4887}
4888
4889void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004890 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4891 : QUICK_ENTRY_POINT(pUnlockObject),
4892 instruction,
4893 instruction->GetDexPc(),
4894 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004895}
4896
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004897void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4898void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4899void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4900
4901void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4902 LocationSummary* locations =
4903 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4904 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4905 || instruction->GetResultType() == Primitive::kPrimLong);
4906 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004907 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004908 locations->SetOut(Location::SameAsFirstInput());
4909}
4910
4911void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4912 HandleBitwiseOperation(instruction);
4913}
4914
4915void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4916 HandleBitwiseOperation(instruction);
4917}
4918
4919void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4920 HandleBitwiseOperation(instruction);
4921}
4922
4923void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4924 LocationSummary* locations = instruction->GetLocations();
4925 Location first = locations->InAt(0);
4926 Location second = locations->InAt(1);
4927 DCHECK(first.Equals(locations->Out()));
4928
4929 if (instruction->GetResultType() == Primitive::kPrimInt) {
4930 if (second.IsRegister()) {
4931 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004932 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004933 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004934 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004935 } else {
4936 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004937 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004938 }
4939 } else if (second.IsConstant()) {
4940 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4941 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004942 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004943 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004944 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004945 } else {
4946 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004947 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004948 }
4949 } else {
4950 Address address(CpuRegister(RSP), second.GetStackIndex());
4951 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004952 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004953 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004954 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004955 } else {
4956 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004957 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004958 }
4959 }
4960 } else {
4961 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004962 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4963 bool second_is_constant = false;
4964 int64_t value = 0;
4965 if (second.IsConstant()) {
4966 second_is_constant = true;
4967 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004968 }
Mark Mendell40741f32015-04-20 22:10:34 -04004969 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004970
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004971 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004972 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004973 if (is_int32_value) {
4974 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4975 } else {
4976 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4977 }
4978 } else if (second.IsDoubleStackSlot()) {
4979 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004980 } else {
4981 __ andq(first_reg, second.AsRegister<CpuRegister>());
4982 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004983 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004984 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004985 if (is_int32_value) {
4986 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4987 } else {
4988 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4989 }
4990 } else if (second.IsDoubleStackSlot()) {
4991 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004992 } else {
4993 __ orq(first_reg, second.AsRegister<CpuRegister>());
4994 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004995 } else {
4996 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004997 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004998 if (is_int32_value) {
4999 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5000 } else {
5001 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5002 }
5003 } else if (second.IsDoubleStackSlot()) {
5004 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005005 } else {
5006 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5007 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005008 }
5009 }
5010}
5011
Calin Juravleb1498f62015-02-16 13:13:29 +00005012void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
5013 // Nothing to do, this should be removed during prepare for register allocator.
5014 UNUSED(instruction);
5015 LOG(FATAL) << "Unreachable";
5016}
5017
5018void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
5019 // Nothing to do, this should be removed during prepare for register allocator.
5020 UNUSED(instruction);
5021 LOG(FATAL) << "Unreachable";
5022}
5023
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005024void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
5025 DCHECK(codegen_->IsBaseline());
5026 LocationSummary* locations =
5027 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5028 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5029}
5030
5031void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5032 DCHECK(codegen_->IsBaseline());
5033 // Will be generated at use site.
5034}
5035
Mark Mendell92e83bf2015-05-07 11:25:03 -04005036void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
5037 if (value == 0) {
5038 __ xorl(dest, dest);
5039 } else if (value > 0 && IsInt<32>(value)) {
5040 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
5041 __ movl(dest, Immediate(static_cast<int32_t>(value)));
5042 } else {
5043 __ movq(dest, Immediate(value));
5044 }
5045}
5046
Mark Mendellcfa410b2015-05-25 16:02:44 -04005047void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
5048 DCHECK(dest.IsDoubleStackSlot());
5049 if (IsInt<32>(value)) {
5050 // Can move directly as an int32 constant.
5051 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
5052 Immediate(static_cast<int32_t>(value)));
5053 } else {
5054 Load64BitValue(CpuRegister(TMP), value);
5055 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
5056 }
5057}
5058
Mark Mendellf55c3e02015-03-26 21:07:46 -04005059void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
5060 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04005061 X86_64Assembler* assembler = GetAssembler();
5062 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04005063 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5064 // byte values. If used for vectors at a later time, this will need to be
5065 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04005066 assembler->Align(4, 0);
5067 constant_area_start_ = assembler->CodeSize();
5068 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04005069 }
5070
5071 // And finish up.
5072 CodeGenerator::Finalize(allocator);
5073}
5074
5075/**
5076 * Class to handle late fixup of offsets into constant area.
5077 */
5078class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5079 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04005080 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04005081 : codegen_(codegen), offset_into_constant_area_(offset) {}
5082
5083 private:
5084 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5085 // Patch the correct offset for the instruction. We use the address of the
5086 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
5087 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5088 int relative_position = constant_offset - pos;
5089
5090 // Patch in the right value.
5091 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5092 }
5093
Mark Mendell39dcf552015-04-09 20:42:42 -04005094 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04005095
5096 // Location in constant area that the fixup refers to.
5097 int offset_into_constant_area_;
5098};
5099
5100Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
5101 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5102 return Address::RIP(fixup);
5103}
5104
5105Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
5106 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5107 return Address::RIP(fixup);
5108}
5109
5110Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
5111 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5112 return Address::RIP(fixup);
5113}
5114
5115Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
5116 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5117 return Address::RIP(fixup);
5118}
5119
Roland Levillain4d027112015-07-01 15:41:14 +01005120#undef __
5121
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005122} // namespace x86_64
5123} // namespace art