blob: 0f3eb74c64ec6a87d7582bc13f5b9cbdb12121cd [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())->
Alexandre Rames8158f282015-08-07 10:26:17 +010051#define QUICK_ENTRY_POINT(x) Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x), true)
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());
Alexandre Rames8158f282015-08-07 10:26:17 +010060 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
61 instruction_,
62 instruction_->GetDexPc(),
63 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 }
65
Alexandre Rames8158f282015-08-07 10:26:17 +010066 bool IsFatal() const OVERRIDE { return true; }
67
Alexandre Rames9931f312015-06-19 14:47:01 +010068 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
69
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010071 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
73};
74
Calin Juravled0d48522014-11-04 16:40:20 +000075class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
76 public:
77 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
78
Alexandre Rames2ed20af2015-03-06 13:55:35 +000079 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010080 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000081 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010082 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
83 instruction_,
84 instruction_->GetDexPc(),
85 this);
Calin Juravled0d48522014-11-04 16:40:20 +000086 }
87
Alexandre Rames8158f282015-08-07 10:26:17 +010088 bool IsFatal() const OVERRIDE { return true; }
89
Alexandre Rames9931f312015-06-19 14:47:01 +010090 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
91
Calin Juravled0d48522014-11-04 16:40:20 +000092 private:
93 HDivZeroCheck* const instruction_;
94 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
95};
96
Calin Juravlebacfec32014-11-14 15:54:36 +000097class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000098 public:
Roland Levillain3887c462015-08-12 18:15:42 +010099 DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
Calin Juravlebacfec32014-11-14 15:54:36 +0000100 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000101
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000102 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000103 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000104 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000105 if (is_div_) {
106 __ negl(cpu_reg_);
107 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400108 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 }
110
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000111 } else {
112 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negq(cpu_reg_);
115 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400116 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000117 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
Alexandre Rames9931f312015-06-19 14:47:01 +0100122 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
123
Calin Juravled0d48522014-11-04 16:40:20 +0000124 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 const bool is_div_;
128 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000129};
130
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100133 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000135
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000136 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100137 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000139 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100140 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
141 instruction_,
142 instruction_->GetDexPc(),
143 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000144 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 if (successor_ == nullptr) {
146 __ jmp(GetReturnLabel());
147 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 }
151
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 Label* GetReturnLabel() {
153 DCHECK(successor_ == nullptr);
154 return &return_label_;
155 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100157 HBasicBlock* GetSuccessor() const {
158 return successor_;
159 }
160
Alexandre Rames9931f312015-06-19 14:47:01 +0100161 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
162
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163 private:
164 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100165 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000166 Label return_label_;
167
168 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
169};
170
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100171class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100172 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100173 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
174 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100175
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000176 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100177 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames8158f282015-08-07 10:26:17 +0100178 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000180 // We're moving two locations to locations that could overlap, so we need a parallel
181 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100182 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000183 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100184 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000185 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100186 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100187 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100188 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
189 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100190 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
191 instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 }
193
Alexandre Rames8158f282015-08-07 10:26:17 +0100194 bool IsFatal() const OVERRIDE { return true; }
195
Alexandre Rames9931f312015-06-19 14:47:01 +0100196 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
197
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100199 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100200
201 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
202};
203
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206 LoadClassSlowPathX86_64(HLoadClass* cls,
207 HInstruction* at,
208 uint32_t dex_pc,
209 bool do_clinit)
210 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
211 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
212 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100213
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000214 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100216 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
217 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100218
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000219 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100221 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100223 x64_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
224 : QUICK_ENTRY_POINT(pInitializeType),
225 at_, dex_pc_, this);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100226
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000227 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000229 if (out.IsValid()) {
230 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
231 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232 }
233
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000234 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235 __ jmp(GetExitLabel());
236 }
237
Alexandre Rames9931f312015-06-19 14:47:01 +0100238 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
239
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 // The class this slow path will load.
242 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100243
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 // The instruction where this slow path is happening.
245 // (Might be the load class or an initialization check).
246 HInstruction* const at_;
247
248 // The dex PC of `at_`.
249 const uint32_t dex_pc_;
250
251 // Whether to initialize the class.
252 const bool do_clinit_;
253
254 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100255};
256
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000257class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
258 public:
259 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
260
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000261 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000262 LocationSummary* locations = instruction_->GetLocations();
263 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
264
265 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
266 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000267 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000268
269 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800270 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000271 Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100272 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
273 instruction_,
274 instruction_->GetDexPc(),
275 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000276 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000277 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000278 __ jmp(GetExitLabel());
279 }
280
Alexandre Rames9931f312015-06-19 14:47:01 +0100281 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
282
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000283 private:
284 HLoadString* const instruction_;
285
286 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
287};
288
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
290 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100291 explicit TypeCheckSlowPathX86_64(HInstruction* instruction)
292 : instruction_(instruction) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000294 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100296 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
297 : locations->Out();
298 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000299 DCHECK(instruction_->IsCheckCast()
300 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
303 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000304 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305
306 // We're moving two locations to locations that could overlap, so we need a parallel
307 // move resolver.
308 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000309 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100310 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000311 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100312 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100313 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100314 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
315 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000317 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100318 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
319 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100320 dex_pc,
Alexandre Rames8158f282015-08-07 10:26:17 +0100321 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 } else {
323 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100324 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
325 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100326 dex_pc,
Alexandre Rames8158f282015-08-07 10:26:17 +0100327 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000328 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329
330 if (instruction_->IsInstanceOf()) {
331 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
332 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000334 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 __ jmp(GetExitLabel());
336 }
337
Alexandre Rames9931f312015-06-19 14:47:01 +0100338 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
339
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000341 HInstruction* const instruction_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342
343 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
344};
345
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700346class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
347 public:
348 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
349 : instruction_(instruction) {}
350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100352 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700353 __ Bind(GetEntryLabel());
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700355 DCHECK(instruction_->IsDeoptimize());
356 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
Alexandre Rames8158f282015-08-07 10:26:17 +0100357 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
358 deoptimize,
359 deoptimize->GetDexPc(),
360 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
364
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 private:
366 HInstruction* const instruction_;
367 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
368};
369
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100370#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100371#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100372
Roland Levillain4fa13f62015-07-06 18:11:54 +0100373inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700374 switch (cond) {
375 case kCondEQ: return kEqual;
376 case kCondNE: return kNotEqual;
377 case kCondLT: return kLess;
378 case kCondLE: return kLessEqual;
379 case kCondGT: return kGreater;
380 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700381 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100382 LOG(FATAL) << "Unreachable";
383 UNREACHABLE();
384}
385
386inline Condition X86_64FPCondition(IfCondition cond) {
387 switch (cond) {
388 case kCondEQ: return kEqual;
389 case kCondNE: return kNotEqual;
390 case kCondLT: return kBelow;
391 case kCondLE: return kBelowEqual;
392 case kCondGT: return kAbove;
393 case kCondGE: return kAboveEqual;
394 };
395 LOG(FATAL) << "Unreachable";
396 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700397}
398
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800399void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100400 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800401 // All registers are assumed to be correctly set up.
402
Vladimir Marko58155012015-08-19 12:49:41 +0000403 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
404 switch (invoke->GetMethodLoadKind()) {
405 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
406 // temp = thread->string_init_entrypoint
407 __ gs()->movl(temp.AsRegister<CpuRegister>(),
408 Address::Absolute(invoke->GetStringInitOffset(), true));
409 break;
410 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
411 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
412 break;
413 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
414 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
415 break;
416 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
417 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
418 method_patches_.emplace_back(invoke->GetTargetMethod());
419 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
420 break;
421 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
422 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
423 invoke->GetDexCacheArrayOffset());
424 __ movq(temp.AsRegister<CpuRegister>(),
425 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
426 // Bind the label at the end of the "movl" insn.
427 __ Bind(&pc_rel_dex_cache_patches_.back().label);
428 break;
429 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
430 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
431 Register method_reg;
432 CpuRegister reg = temp.AsRegister<CpuRegister>();
433 if (current_method.IsRegister()) {
434 method_reg = current_method.AsRegister<Register>();
435 } else {
436 DCHECK(invoke->GetLocations()->Intrinsified());
437 DCHECK(!current_method.IsValid());
438 method_reg = reg.AsRegister();
439 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
440 }
441 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100442 __ movq(reg,
443 Address(CpuRegister(method_reg),
444 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000445 // temp = temp[index_in_cache]
446 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
447 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
448 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100449 }
Vladimir Marko58155012015-08-19 12:49:41 +0000450 }
451
452 switch (invoke->GetCodePtrLocation()) {
453 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
454 __ call(&frame_entry_label_);
455 break;
456 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
457 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
458 Label* label = &relative_call_patches_.back().label;
459 __ call(label); // Bind to the patch label, override at link time.
460 __ Bind(label); // Bind the label at the end of the "call" insn.
461 break;
462 }
463 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
464 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
465 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
466 FALLTHROUGH_INTENDED;
467 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
468 // (callee_method + offset_of_quick_compiled_code)()
469 __ call(Address(callee_method.AsRegister<CpuRegister>(),
470 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
471 kX86_64WordSize).SizeValue()));
472 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000473 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800474
475 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800476}
477
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000478void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
479 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
480 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
481 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
482 LocationSummary* locations = invoke->GetLocations();
483 Location receiver = locations->InAt(0);
484 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
485 // temp = object->GetClass();
486 DCHECK(receiver.IsRegister());
487 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
488 MaybeRecordImplicitNullCheck(invoke);
489 __ MaybeUnpoisonHeapReference(temp);
490 // temp = temp->GetMethodAt(method_offset);
491 __ movq(temp, Address(temp, method_offset));
492 // call temp->GetEntryPoint();
493 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
494 kX86_64WordSize).SizeValue()));
495}
496
Vladimir Marko58155012015-08-19 12:49:41 +0000497void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
498 DCHECK(linker_patches->empty());
499 size_t size =
500 method_patches_.size() + relative_call_patches_.size() + pc_rel_dex_cache_patches_.size();
501 linker_patches->reserve(size);
502 for (const MethodPatchInfo<Label>& info : method_patches_) {
503 // The label points to the end of the "movl" instruction but the literal offset for method
504 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
505 uint32_t literal_offset = info.label.Position() - 4;
506 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
507 info.target_method.dex_file,
508 info.target_method.dex_method_index));
509 }
510 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
511 // The label points to the end of the "call" instruction but the literal offset for method
512 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
513 uint32_t literal_offset = info.label.Position() - 4;
514 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
515 info.target_method.dex_file,
516 info.target_method.dex_method_index));
517 }
518 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
519 // The label points to the end of the "mov" instruction but the literal offset for method
520 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
521 uint32_t literal_offset = info.label.Position() - 4;
522 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
523 &info.target_dex_file,
524 info.label.Position(),
525 info.element_offset));
526 }
527}
528
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100529void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100530 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100531}
532
533void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100534 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100535}
536
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100537size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
538 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
539 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100540}
541
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100542size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
543 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
544 return kX86_64WordSize;
545}
546
547size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
548 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
549 return kX86_64WordSize;
550}
551
552size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
553 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
554 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100555}
556
Alexandre Rames8158f282015-08-07 10:26:17 +0100557void CodeGeneratorX86_64::InvokeRuntime(Address entry_point,
558 HInstruction* instruction,
559 uint32_t dex_pc,
560 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100561 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100562 __ gs()->call(entry_point);
563 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100564}
565
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000566static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000567// Use a fake return address register to mimic Quick.
568static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400569CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
570 const X86_64InstructionSetFeatures& isa_features,
571 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000572 : CodeGenerator(graph,
573 kNumberOfCpuRegisters,
574 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000575 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000576 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
577 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000578 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000579 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
580 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000581 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100582 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100583 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000584 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400585 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400586 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000587 constant_area_start_(0),
588 method_patches_(graph->GetArena()->Adapter()),
589 relative_call_patches_(graph->GetArena()->Adapter()),
590 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000591 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
592}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100594InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
595 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100596 : HGraphVisitor(graph),
597 assembler_(codegen->GetAssembler()),
598 codegen_(codegen) {}
599
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100600Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100601 switch (type) {
602 case Primitive::kPrimLong:
603 case Primitive::kPrimByte:
604 case Primitive::kPrimBoolean:
605 case Primitive::kPrimChar:
606 case Primitive::kPrimShort:
607 case Primitive::kPrimInt:
608 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100609 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100610 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100611 }
612
613 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100614 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100615 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100616 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100617 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100618
619 case Primitive::kPrimVoid:
620 LOG(FATAL) << "Unreachable type " << type;
621 }
622
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100623 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100624}
625
Nicolas Geoffray98893962015-01-21 12:32:32 +0000626void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100627 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100628 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100629
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000630 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100631 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000632
Nicolas Geoffray98893962015-01-21 12:32:32 +0000633 if (is_baseline) {
634 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
635 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
636 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000637 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
638 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
639 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000640 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100641}
642
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100643static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100644 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100645}
David Srbecky9d8606d2015-04-12 09:35:32 +0100646
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100647static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100648 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100649}
650
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100651void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100652 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000653 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100654 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700655 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000656 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100657
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000658 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100659 __ testq(CpuRegister(RAX), Address(
660 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100661 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100662 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000663
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000664 if (HasEmptyFrame()) {
665 return;
666 }
667
Nicolas Geoffray98893962015-01-21 12:32:32 +0000668 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000669 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000670 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000671 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100672 __ cfi().AdjustCFAOffset(kX86_64WordSize);
673 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000674 }
675 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100676
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100677 int adjust = GetFrameSize() - GetCoreSpillSize();
678 __ subq(CpuRegister(RSP), Immediate(adjust));
679 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000680 uint32_t xmm_spill_location = GetFpuSpillStart();
681 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100682
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000683 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
684 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100685 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
686 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
687 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000688 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100689 }
690
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100692 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100693}
694
695void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100696 __ cfi().RememberState();
697 if (!HasEmptyFrame()) {
698 uint32_t xmm_spill_location = GetFpuSpillStart();
699 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
700 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
701 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
702 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
703 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
704 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
705 }
706 }
707
708 int adjust = GetFrameSize() - GetCoreSpillSize();
709 __ addq(CpuRegister(RSP), Immediate(adjust));
710 __ cfi().AdjustCFAOffset(-adjust);
711
712 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
713 Register reg = kCoreCalleeSaves[i];
714 if (allocated_registers_.ContainsCoreRegister(reg)) {
715 __ popq(CpuRegister(reg));
716 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
717 __ cfi().Restore(DWARFReg(reg));
718 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000719 }
720 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100721 __ ret();
722 __ cfi().RestoreState();
723 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100724}
725
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100726void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
727 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100728}
729
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100730Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
731 switch (load->GetType()) {
732 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100733 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100734 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100735
736 case Primitive::kPrimInt:
737 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100738 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100739 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100740
741 case Primitive::kPrimBoolean:
742 case Primitive::kPrimByte:
743 case Primitive::kPrimChar:
744 case Primitive::kPrimShort:
745 case Primitive::kPrimVoid:
746 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700747 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100748 }
749
750 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700751 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100752}
753
754void CodeGeneratorX86_64::Move(Location destination, Location source) {
755 if (source.Equals(destination)) {
756 return;
757 }
758 if (destination.IsRegister()) {
759 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000760 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100761 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000762 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100763 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000764 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100765 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100766 } else {
767 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000768 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100769 Address(CpuRegister(RSP), source.GetStackIndex()));
770 }
771 } else if (destination.IsFpuRegister()) {
772 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000773 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100774 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000775 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100776 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000777 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100778 Address(CpuRegister(RSP), source.GetStackIndex()));
779 } else {
780 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000781 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100782 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100783 }
784 } else if (destination.IsStackSlot()) {
785 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100786 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000787 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 } else if (source.IsFpuRegister()) {
789 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000790 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500791 } else if (source.IsConstant()) {
792 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000793 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500794 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100795 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500796 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000797 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
798 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100799 }
800 } else {
801 DCHECK(destination.IsDoubleStackSlot());
802 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100803 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000804 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100805 } else if (source.IsFpuRegister()) {
806 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000807 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500808 } else if (source.IsConstant()) {
809 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800810 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500811 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000812 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500813 } else {
814 DCHECK(constant->IsLongConstant());
815 value = constant->AsLongConstant()->GetValue();
816 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400817 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100818 } else {
819 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000820 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
821 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100822 }
823 }
824}
825
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100826void CodeGeneratorX86_64::Move(HInstruction* instruction,
827 Location location,
828 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000829 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100830 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700831 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100832 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000833 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100834 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000835 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000836 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
837 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000838 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000839 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000840 } else if (location.IsStackSlot()) {
841 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
842 } else {
843 DCHECK(location.IsConstant());
844 DCHECK_EQ(location.GetConstant(), const_to_move);
845 }
846 } else if (const_to_move->IsLongConstant()) {
847 int64_t value = const_to_move->AsLongConstant()->GetValue();
848 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400849 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000850 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400851 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000852 } else {
853 DCHECK(location.IsConstant());
854 DCHECK_EQ(location.GetConstant(), const_to_move);
855 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100856 }
Roland Levillain476df552014-10-09 17:51:36 +0100857 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100858 switch (instruction->GetType()) {
859 case Primitive::kPrimBoolean:
860 case Primitive::kPrimByte:
861 case Primitive::kPrimChar:
862 case Primitive::kPrimShort:
863 case Primitive::kPrimInt:
864 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100865 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100866 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
867 break;
868
869 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100870 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000871 Move(location,
872 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100873 break;
874
875 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100876 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100877 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000878 } else if (instruction->IsTemporary()) {
879 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
880 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100881 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100882 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100883 switch (instruction->GetType()) {
884 case Primitive::kPrimBoolean:
885 case Primitive::kPrimByte:
886 case Primitive::kPrimChar:
887 case Primitive::kPrimShort:
888 case Primitive::kPrimInt:
889 case Primitive::kPrimNot:
890 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100891 case Primitive::kPrimFloat:
892 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000893 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100894 break;
895
896 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100897 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100898 }
899 }
900}
901
David Brazdilfc6a86a2015-06-26 10:33:45 +0000902void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100903 DCHECK(!successor->IsExitBlock());
904
905 HBasicBlock* block = got->GetBlock();
906 HInstruction* previous = got->GetPrevious();
907
908 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000909 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100910 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
911 return;
912 }
913
914 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
915 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
916 }
917 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100918 __ jmp(codegen_->GetLabelOf(successor));
919 }
920}
921
David Brazdilfc6a86a2015-06-26 10:33:45 +0000922void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
923 got->SetLocations(nullptr);
924}
925
926void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
927 HandleGoto(got, got->GetSuccessor());
928}
929
930void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
931 try_boundary->SetLocations(nullptr);
932}
933
934void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
935 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
936 if (!successor->IsExitBlock()) {
937 HandleGoto(try_boundary, successor);
938 }
939}
940
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100941void LocationsBuilderX86_64::VisitExit(HExit* exit) {
942 exit->SetLocations(nullptr);
943}
944
945void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700946 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100947}
948
Mark Mendellc4701932015-04-10 13:18:51 -0400949void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
950 Label* true_label,
951 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100952 if (cond->IsFPConditionTrueIfNaN()) {
953 __ j(kUnordered, true_label);
954 } else if (cond->IsFPConditionFalseIfNaN()) {
955 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400956 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100957 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400958}
959
960void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
961 HCondition* condition,
962 Label* true_target,
963 Label* false_target,
964 Label* always_true_target) {
965 LocationSummary* locations = condition->GetLocations();
966 Location left = locations->InAt(0);
967 Location right = locations->InAt(1);
968
969 // We don't want true_target as a nullptr.
970 if (true_target == nullptr) {
971 true_target = always_true_target;
972 }
973 bool falls_through = (false_target == nullptr);
974
975 // FP compares don't like null false_targets.
976 if (false_target == nullptr) {
977 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
978 }
979
980 Primitive::Type type = condition->InputAt(0)->GetType();
981 switch (type) {
982 case Primitive::kPrimLong: {
983 CpuRegister left_reg = left.AsRegister<CpuRegister>();
984 if (right.IsConstant()) {
985 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
986 if (IsInt<32>(value)) {
987 if (value == 0) {
988 __ testq(left_reg, left_reg);
989 } else {
990 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
991 }
992 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100993 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -0400994 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
995 }
996 } else if (right.IsDoubleStackSlot()) {
997 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
998 } else {
999 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1000 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001001 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001002 break;
1003 }
1004 case Primitive::kPrimFloat: {
1005 if (right.IsFpuRegister()) {
1006 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1007 } else if (right.IsConstant()) {
1008 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1009 codegen_->LiteralFloatAddress(
1010 right.GetConstant()->AsFloatConstant()->GetValue()));
1011 } else {
1012 DCHECK(right.IsStackSlot());
1013 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1014 Address(CpuRegister(RSP), right.GetStackIndex()));
1015 }
1016 GenerateFPJumps(condition, true_target, false_target);
1017 break;
1018 }
1019 case Primitive::kPrimDouble: {
1020 if (right.IsFpuRegister()) {
1021 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1022 } else if (right.IsConstant()) {
1023 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1024 codegen_->LiteralDoubleAddress(
1025 right.GetConstant()->AsDoubleConstant()->GetValue()));
1026 } else {
1027 DCHECK(right.IsDoubleStackSlot());
1028 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1029 Address(CpuRegister(RSP), right.GetStackIndex()));
1030 }
1031 GenerateFPJumps(condition, true_target, false_target);
1032 break;
1033 }
1034 default:
1035 LOG(FATAL) << "Unexpected condition type " << type;
1036 }
1037
1038 if (!falls_through) {
1039 __ jmp(false_target);
1040 }
1041}
1042
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001043void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1044 Label* true_target,
1045 Label* false_target,
1046 Label* always_true_target) {
1047 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001048 if (cond->IsIntConstant()) {
1049 // Constant condition, statically compared against 1.
1050 int32_t cond_value = cond->AsIntConstant()->GetValue();
1051 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001052 if (always_true_target != nullptr) {
1053 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001054 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001055 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001056 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001057 DCHECK_EQ(cond_value, 0);
1058 }
1059 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001060 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001061 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1062 // Moves do not affect the eflags register, so if the condition is
1063 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001064 // again. We can't use the eflags on FP conditions if they are
1065 // materialized due to the complex branching.
1066 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001067 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001068 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1069 && !Primitive::IsFloatingPointType(type);
1070
Roland Levillain4fa13f62015-07-06 18:11:54 +01001071 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001072 if (!eflags_set) {
1073 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001074 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001075 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001076 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001077 } else {
1078 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1079 Immediate(0));
1080 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001081 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001082 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001083 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001084 }
1085 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001086 // Condition has not been materialized, use its inputs as the
1087 // comparison and its condition as the branch condition.
1088
Mark Mendellc4701932015-04-10 13:18:51 -04001089 // Is this a long or FP comparison that has been folded into the HCondition?
1090 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001091 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001092 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1093 true_target, false_target, always_true_target);
1094 return;
1095 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001096
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001097 Location lhs = cond->GetLocations()->InAt(0);
1098 Location rhs = cond->GetLocations()->InAt(1);
1099 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001100 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001101 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001102 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001103 if (constant == 0) {
1104 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1105 } else {
1106 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1107 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001108 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001109 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001110 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1111 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001112 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001113 }
Dave Allison20dfc792014-06-16 20:44:29 -07001114 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001115 if (false_target != nullptr) {
1116 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001117 }
1118}
1119
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001120void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1121 LocationSummary* locations =
1122 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1123 HInstruction* cond = if_instr->InputAt(0);
1124 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1125 locations->SetInAt(0, Location::Any());
1126 }
1127}
1128
1129void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1130 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1131 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1132 Label* always_true_target = true_target;
1133 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1134 if_instr->IfTrueSuccessor())) {
1135 always_true_target = nullptr;
1136 }
1137 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1138 if_instr->IfFalseSuccessor())) {
1139 false_target = nullptr;
1140 }
1141 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1142}
1143
1144void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1145 LocationSummary* locations = new (GetGraph()->GetArena())
1146 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1147 HInstruction* cond = deoptimize->InputAt(0);
1148 DCHECK(cond->IsCondition());
1149 if (cond->AsCondition()->NeedsMaterialization()) {
1150 locations->SetInAt(0, Location::Any());
1151 }
1152}
1153
1154void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1155 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
1156 DeoptimizationSlowPathX86_64(deoptimize);
1157 codegen_->AddSlowPath(slow_path);
1158 Label* slow_path_entry = slow_path->GetEntryLabel();
1159 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1160}
1161
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001162void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1163 local->SetLocations(nullptr);
1164}
1165
1166void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1167 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1168}
1169
1170void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1171 local->SetLocations(nullptr);
1172}
1173
1174void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1175 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001176 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001177}
1178
1179void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001180 LocationSummary* locations =
1181 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001182 switch (store->InputAt(1)->GetType()) {
1183 case Primitive::kPrimBoolean:
1184 case Primitive::kPrimByte:
1185 case Primitive::kPrimChar:
1186 case Primitive::kPrimShort:
1187 case Primitive::kPrimInt:
1188 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001190 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1191 break;
1192
1193 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001194 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001195 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1196 break;
1197
1198 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001199 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001200 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001201}
1202
1203void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001204 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001205}
1206
Roland Levillain0d37cd02015-05-27 16:39:19 +01001207void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001208 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001209 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001210 // Handle the long/FP comparisons made in instruction simplification.
1211 switch (cond->InputAt(0)->GetType()) {
1212 case Primitive::kPrimLong:
1213 locations->SetInAt(0, Location::RequiresRegister());
1214 locations->SetInAt(1, Location::Any());
1215 break;
1216 case Primitive::kPrimFloat:
1217 case Primitive::kPrimDouble:
1218 locations->SetInAt(0, Location::RequiresFpuRegister());
1219 locations->SetInAt(1, Location::Any());
1220 break;
1221 default:
1222 locations->SetInAt(0, Location::RequiresRegister());
1223 locations->SetInAt(1, Location::Any());
1224 break;
1225 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001226 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001227 locations->SetOut(Location::RequiresRegister());
1228 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001229}
1230
Roland Levillain0d37cd02015-05-27 16:39:19 +01001231void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001232 if (!cond->NeedsMaterialization()) {
1233 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001234 }
Mark Mendellc4701932015-04-10 13:18:51 -04001235
1236 LocationSummary* locations = cond->GetLocations();
1237 Location lhs = locations->InAt(0);
1238 Location rhs = locations->InAt(1);
1239 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1240 Label true_label, false_label;
1241
1242 switch (cond->InputAt(0)->GetType()) {
1243 default:
1244 // Integer case.
1245
1246 // Clear output register: setcc only sets the low byte.
1247 __ xorl(reg, reg);
1248
1249 if (rhs.IsRegister()) {
1250 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1251 } else if (rhs.IsConstant()) {
1252 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1253 if (constant == 0) {
1254 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1255 } else {
1256 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1257 }
1258 } else {
1259 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1260 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001261 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001262 return;
1263 case Primitive::kPrimLong:
1264 // Clear output register: setcc only sets the low byte.
1265 __ xorl(reg, reg);
1266
1267 if (rhs.IsRegister()) {
1268 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1269 } else if (rhs.IsConstant()) {
1270 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1271 if (IsInt<32>(value)) {
1272 if (value == 0) {
1273 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1274 } else {
1275 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1276 }
1277 } else {
1278 // Value won't fit in an int.
1279 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1280 }
1281 } else {
1282 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1283 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001284 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001285 return;
1286 case Primitive::kPrimFloat: {
1287 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1288 if (rhs.IsConstant()) {
1289 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1290 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1291 } else if (rhs.IsStackSlot()) {
1292 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1293 } else {
1294 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1295 }
1296 GenerateFPJumps(cond, &true_label, &false_label);
1297 break;
1298 }
1299 case Primitive::kPrimDouble: {
1300 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1301 if (rhs.IsConstant()) {
1302 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1303 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1304 } else if (rhs.IsDoubleStackSlot()) {
1305 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1306 } else {
1307 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1308 }
1309 GenerateFPJumps(cond, &true_label, &false_label);
1310 break;
1311 }
1312 }
1313
1314 // Convert the jumps into the result.
1315 Label done_label;
1316
Roland Levillain4fa13f62015-07-06 18:11:54 +01001317 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001318 __ Bind(&false_label);
1319 __ xorl(reg, reg);
1320 __ jmp(&done_label);
1321
Roland Levillain4fa13f62015-07-06 18:11:54 +01001322 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001323 __ Bind(&true_label);
1324 __ movl(reg, Immediate(1));
1325 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001326}
1327
1328void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1329 VisitCondition(comp);
1330}
1331
1332void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1333 VisitCondition(comp);
1334}
1335
1336void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1337 VisitCondition(comp);
1338}
1339
1340void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1341 VisitCondition(comp);
1342}
1343
1344void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1345 VisitCondition(comp);
1346}
1347
1348void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1349 VisitCondition(comp);
1350}
1351
1352void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1353 VisitCondition(comp);
1354}
1355
1356void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1357 VisitCondition(comp);
1358}
1359
1360void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1361 VisitCondition(comp);
1362}
1363
1364void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1365 VisitCondition(comp);
1366}
1367
1368void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1369 VisitCondition(comp);
1370}
1371
1372void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1373 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001374}
1375
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001376void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001377 LocationSummary* locations =
1378 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001379 switch (compare->InputAt(0)->GetType()) {
1380 case Primitive::kPrimLong: {
1381 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001382 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1384 break;
1385 }
1386 case Primitive::kPrimFloat:
1387 case Primitive::kPrimDouble: {
1388 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001389 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001390 locations->SetOut(Location::RequiresRegister());
1391 break;
1392 }
1393 default:
1394 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1395 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001396}
1397
1398void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001399 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001400 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001401 Location left = locations->InAt(0);
1402 Location right = locations->InAt(1);
1403
1404 Label less, greater, done;
1405 Primitive::Type type = compare->InputAt(0)->GetType();
1406 switch (type) {
1407 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001408 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1409 if (right.IsConstant()) {
1410 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001411 if (IsInt<32>(value)) {
1412 if (value == 0) {
1413 __ testq(left_reg, left_reg);
1414 } else {
1415 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1416 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001417 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001418 // Value won't fit in an int.
1419 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001420 }
Mark Mendell40741f32015-04-20 22:10:34 -04001421 } else if (right.IsDoubleStackSlot()) {
1422 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001423 } else {
1424 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1425 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001426 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001427 }
1428 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001429 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1430 if (right.IsConstant()) {
1431 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1432 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1433 } else if (right.IsStackSlot()) {
1434 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1435 } else {
1436 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1437 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001438 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1439 break;
1440 }
1441 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001442 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1443 if (right.IsConstant()) {
1444 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1445 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1446 } else if (right.IsDoubleStackSlot()) {
1447 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1448 } else {
1449 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1450 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001451 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1452 break;
1453 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001454 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001455 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001456 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001457 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001458 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001459 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001460
Calin Juravle91debbc2014-11-26 19:01:09 +00001461 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001462 __ movl(out, Immediate(1));
1463 __ jmp(&done);
1464
1465 __ Bind(&less);
1466 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001467
1468 __ Bind(&done);
1469}
1470
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001471void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001472 LocationSummary* locations =
1473 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001474 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001475}
1476
1477void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001478 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001479 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001480}
1481
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001482void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1483 LocationSummary* locations =
1484 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1485 locations->SetOut(Location::ConstantLocation(constant));
1486}
1487
1488void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1489 // Will be generated at use site.
1490 UNUSED(constant);
1491}
1492
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001493void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001494 LocationSummary* locations =
1495 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001496 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001497}
1498
1499void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001500 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001501 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001502}
1503
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001504void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1505 LocationSummary* locations =
1506 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1507 locations->SetOut(Location::ConstantLocation(constant));
1508}
1509
1510void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1511 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001512 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001513}
1514
1515void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1516 LocationSummary* locations =
1517 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1518 locations->SetOut(Location::ConstantLocation(constant));
1519}
1520
1521void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1522 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001523 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001524}
1525
Calin Juravle27df7582015-04-17 19:12:31 +01001526void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1527 memory_barrier->SetLocations(nullptr);
1528}
1529
1530void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1531 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1532}
1533
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001534void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1535 ret->SetLocations(nullptr);
1536}
1537
1538void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001539 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001540 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001541}
1542
1543void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001544 LocationSummary* locations =
1545 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001546 switch (ret->InputAt(0)->GetType()) {
1547 case Primitive::kPrimBoolean:
1548 case Primitive::kPrimByte:
1549 case Primitive::kPrimChar:
1550 case Primitive::kPrimShort:
1551 case Primitive::kPrimInt:
1552 case Primitive::kPrimNot:
1553 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001554 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001555 break;
1556
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001557 case Primitive::kPrimFloat:
1558 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001559 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001560 break;
1561
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001562 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001563 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001564 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001565}
1566
1567void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1568 if (kIsDebugBuild) {
1569 switch (ret->InputAt(0)->GetType()) {
1570 case Primitive::kPrimBoolean:
1571 case Primitive::kPrimByte:
1572 case Primitive::kPrimChar:
1573 case Primitive::kPrimShort:
1574 case Primitive::kPrimInt:
1575 case Primitive::kPrimNot:
1576 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001577 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001578 break;
1579
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001580 case Primitive::kPrimFloat:
1581 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001582 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001583 XMM0);
1584 break;
1585
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001586 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001587 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001588 }
1589 }
1590 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001591}
1592
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001593Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1594 switch (type) {
1595 case Primitive::kPrimBoolean:
1596 case Primitive::kPrimByte:
1597 case Primitive::kPrimChar:
1598 case Primitive::kPrimShort:
1599 case Primitive::kPrimInt:
1600 case Primitive::kPrimNot:
1601 case Primitive::kPrimLong:
1602 return Location::RegisterLocation(RAX);
1603
1604 case Primitive::kPrimVoid:
1605 return Location::NoLocation();
1606
1607 case Primitive::kPrimDouble:
1608 case Primitive::kPrimFloat:
1609 return Location::FpuRegisterLocation(XMM0);
1610 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001611
1612 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001613}
1614
1615Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1616 return Location::RegisterLocation(kMethodRegisterArgument);
1617}
1618
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001619Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001620 switch (type) {
1621 case Primitive::kPrimBoolean:
1622 case Primitive::kPrimByte:
1623 case Primitive::kPrimChar:
1624 case Primitive::kPrimShort:
1625 case Primitive::kPrimInt:
1626 case Primitive::kPrimNot: {
1627 uint32_t index = gp_index_++;
1628 stack_index_++;
1629 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001630 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001631 } else {
1632 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1633 }
1634 }
1635
1636 case Primitive::kPrimLong: {
1637 uint32_t index = gp_index_;
1638 stack_index_ += 2;
1639 if (index < calling_convention.GetNumberOfRegisters()) {
1640 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001641 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001642 } else {
1643 gp_index_ += 2;
1644 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1645 }
1646 }
1647
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001648 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001649 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001650 stack_index_++;
1651 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001652 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001653 } else {
1654 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1655 }
1656 }
1657
1658 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001659 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001660 stack_index_ += 2;
1661 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001662 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001663 } else {
1664 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1665 }
1666 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001667
1668 case Primitive::kPrimVoid:
1669 LOG(FATAL) << "Unexpected parameter type " << type;
1670 break;
1671 }
1672 return Location();
1673}
1674
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001675void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001676 // When we do not run baseline, explicit clinit checks triggered by static
1677 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1678 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001679
Mark Mendellfb8d2792015-03-31 22:16:59 -04001680 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001681 if (intrinsic.TryDispatch(invoke)) {
1682 return;
1683 }
1684
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001685 HandleInvoke(invoke);
1686}
1687
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001688static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1689 if (invoke->GetLocations()->Intrinsified()) {
1690 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1691 intrinsic.Dispatch(invoke);
1692 return true;
1693 }
1694 return false;
1695}
1696
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001697void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001698 // When we do not run baseline, explicit clinit checks triggered by static
1699 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1700 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001701
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001702 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1703 return;
1704 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001705
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001706 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001707 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001708 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001709 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001710}
1711
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001712void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001713 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001714 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001715}
1716
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001717void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001718 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001719 if (intrinsic.TryDispatch(invoke)) {
1720 return;
1721 }
1722
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001723 HandleInvoke(invoke);
1724}
1725
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001726void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001727 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1728 return;
1729 }
1730
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001731 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001732
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001733 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001734 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001735}
1736
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001737void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1738 HandleInvoke(invoke);
1739 // Add the hidden argument.
1740 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1741}
1742
1743void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1744 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001745 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001746 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1747 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001748 LocationSummary* locations = invoke->GetLocations();
1749 Location receiver = locations->InAt(0);
1750 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1751
1752 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001753 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1754 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001755
1756 // temp = object->GetClass();
1757 if (receiver.IsStackSlot()) {
1758 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1759 __ movl(temp, Address(temp, class_offset));
1760 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001761 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001762 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001763 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001764 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001765 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001766 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001767 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001768 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001769 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001770
1771 DCHECK(!codegen_->IsLeafMethod());
1772 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1773}
1774
Roland Levillain88cb1752014-10-20 16:36:47 +01001775void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1776 LocationSummary* locations =
1777 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1778 switch (neg->GetResultType()) {
1779 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001780 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001781 locations->SetInAt(0, Location::RequiresRegister());
1782 locations->SetOut(Location::SameAsFirstInput());
1783 break;
1784
Roland Levillain88cb1752014-10-20 16:36:47 +01001785 case Primitive::kPrimFloat:
1786 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001787 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001788 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001789 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001790 break;
1791
1792 default:
1793 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1794 }
1795}
1796
1797void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1798 LocationSummary* locations = neg->GetLocations();
1799 Location out = locations->Out();
1800 Location in = locations->InAt(0);
1801 switch (neg->GetResultType()) {
1802 case Primitive::kPrimInt:
1803 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001804 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001805 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001806 break;
1807
1808 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001809 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001810 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001811 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001812 break;
1813
Roland Levillain5368c212014-11-27 15:03:41 +00001814 case Primitive::kPrimFloat: {
1815 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001816 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001817 // Implement float negation with an exclusive or with value
1818 // 0x80000000 (mask for bit 31, representing the sign of a
1819 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001820 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001821 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001822 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001823 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001824
Roland Levillain5368c212014-11-27 15:03:41 +00001825 case Primitive::kPrimDouble: {
1826 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001827 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001828 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001829 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001830 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001831 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001832 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001833 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001834 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001835
1836 default:
1837 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1838 }
1839}
1840
Roland Levillaindff1f282014-11-05 14:15:05 +00001841void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1842 LocationSummary* locations =
1843 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1844 Primitive::Type result_type = conversion->GetResultType();
1845 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001846 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001847
David Brazdilb2bd1c52015-03-25 11:17:37 +00001848 // The Java language does not allow treating boolean as an integral type but
1849 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001850
Roland Levillaindff1f282014-11-05 14:15:05 +00001851 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001852 case Primitive::kPrimByte:
1853 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001854 case Primitive::kPrimBoolean:
1855 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001856 case Primitive::kPrimShort:
1857 case Primitive::kPrimInt:
1858 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001859 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001860 locations->SetInAt(0, Location::Any());
1861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1862 break;
1863
1864 default:
1865 LOG(FATAL) << "Unexpected type conversion from " << input_type
1866 << " to " << result_type;
1867 }
1868 break;
1869
Roland Levillain01a8d712014-11-14 16:27:39 +00001870 case Primitive::kPrimShort:
1871 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001872 case Primitive::kPrimBoolean:
1873 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001874 case Primitive::kPrimByte:
1875 case Primitive::kPrimInt:
1876 case Primitive::kPrimChar:
1877 // Processing a Dex `int-to-short' instruction.
1878 locations->SetInAt(0, Location::Any());
1879 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1880 break;
1881
1882 default:
1883 LOG(FATAL) << "Unexpected type conversion from " << input_type
1884 << " to " << result_type;
1885 }
1886 break;
1887
Roland Levillain946e1432014-11-11 17:35:19 +00001888 case Primitive::kPrimInt:
1889 switch (input_type) {
1890 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001891 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001892 locations->SetInAt(0, Location::Any());
1893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1894 break;
1895
1896 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001897 // Processing a Dex `float-to-int' instruction.
1898 locations->SetInAt(0, Location::RequiresFpuRegister());
1899 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001900 break;
1901
Roland Levillain946e1432014-11-11 17:35:19 +00001902 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001903 // Processing a Dex `double-to-int' instruction.
1904 locations->SetInAt(0, Location::RequiresFpuRegister());
1905 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001906 break;
1907
1908 default:
1909 LOG(FATAL) << "Unexpected type conversion from " << input_type
1910 << " to " << result_type;
1911 }
1912 break;
1913
Roland Levillaindff1f282014-11-05 14:15:05 +00001914 case Primitive::kPrimLong:
1915 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001916 case Primitive::kPrimBoolean:
1917 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001918 case Primitive::kPrimByte:
1919 case Primitive::kPrimShort:
1920 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001921 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001922 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001923 // TODO: We would benefit from a (to-be-implemented)
1924 // Location::RegisterOrStackSlot requirement for this input.
1925 locations->SetInAt(0, Location::RequiresRegister());
1926 locations->SetOut(Location::RequiresRegister());
1927 break;
1928
1929 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001930 // Processing a Dex `float-to-long' instruction.
1931 locations->SetInAt(0, Location::RequiresFpuRegister());
1932 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00001933 break;
1934
Roland Levillaindff1f282014-11-05 14:15:05 +00001935 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001936 // Processing a Dex `double-to-long' instruction.
1937 locations->SetInAt(0, Location::RequiresFpuRegister());
1938 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001939 break;
1940
1941 default:
1942 LOG(FATAL) << "Unexpected type conversion from " << input_type
1943 << " to " << result_type;
1944 }
1945 break;
1946
Roland Levillain981e4542014-11-14 11:47:14 +00001947 case Primitive::kPrimChar:
1948 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001949 case Primitive::kPrimBoolean:
1950 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001951 case Primitive::kPrimByte:
1952 case Primitive::kPrimShort:
1953 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001954 // Processing a Dex `int-to-char' instruction.
1955 locations->SetInAt(0, Location::Any());
1956 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1957 break;
1958
1959 default:
1960 LOG(FATAL) << "Unexpected type conversion from " << input_type
1961 << " to " << result_type;
1962 }
1963 break;
1964
Roland Levillaindff1f282014-11-05 14:15:05 +00001965 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001966 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001967 case Primitive::kPrimBoolean:
1968 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001969 case Primitive::kPrimByte:
1970 case Primitive::kPrimShort:
1971 case Primitive::kPrimInt:
1972 case Primitive::kPrimChar:
1973 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001974 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001975 locations->SetOut(Location::RequiresFpuRegister());
1976 break;
1977
1978 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001979 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001980 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001981 locations->SetOut(Location::RequiresFpuRegister());
1982 break;
1983
Roland Levillaincff13742014-11-17 14:32:17 +00001984 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001985 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001986 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001987 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001988 break;
1989
1990 default:
1991 LOG(FATAL) << "Unexpected type conversion from " << input_type
1992 << " to " << result_type;
1993 };
1994 break;
1995
Roland Levillaindff1f282014-11-05 14:15:05 +00001996 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001997 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001998 case Primitive::kPrimBoolean:
1999 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002000 case Primitive::kPrimByte:
2001 case Primitive::kPrimShort:
2002 case Primitive::kPrimInt:
2003 case Primitive::kPrimChar:
2004 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002005 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002006 locations->SetOut(Location::RequiresFpuRegister());
2007 break;
2008
2009 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002010 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002011 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002012 locations->SetOut(Location::RequiresFpuRegister());
2013 break;
2014
Roland Levillaincff13742014-11-17 14:32:17 +00002015 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002016 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002017 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002018 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002019 break;
2020
2021 default:
2022 LOG(FATAL) << "Unexpected type conversion from " << input_type
2023 << " to " << result_type;
2024 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002025 break;
2026
2027 default:
2028 LOG(FATAL) << "Unexpected type conversion from " << input_type
2029 << " to " << result_type;
2030 }
2031}
2032
2033void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2034 LocationSummary* locations = conversion->GetLocations();
2035 Location out = locations->Out();
2036 Location in = locations->InAt(0);
2037 Primitive::Type result_type = conversion->GetResultType();
2038 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002039 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002040 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002041 case Primitive::kPrimByte:
2042 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002043 case Primitive::kPrimBoolean:
2044 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002045 case Primitive::kPrimShort:
2046 case Primitive::kPrimInt:
2047 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002048 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002049 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002051 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002052 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002053 Address(CpuRegister(RSP), in.GetStackIndex()));
2054 } else {
2055 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002057 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2058 }
2059 break;
2060
2061 default:
2062 LOG(FATAL) << "Unexpected type conversion from " << input_type
2063 << " to " << result_type;
2064 }
2065 break;
2066
Roland Levillain01a8d712014-11-14 16:27:39 +00002067 case Primitive::kPrimShort:
2068 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002069 case Primitive::kPrimBoolean:
2070 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002071 case Primitive::kPrimByte:
2072 case Primitive::kPrimInt:
2073 case Primitive::kPrimChar:
2074 // Processing a Dex `int-to-short' instruction.
2075 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002076 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002077 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002078 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002079 Address(CpuRegister(RSP), in.GetStackIndex()));
2080 } else {
2081 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002083 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2084 }
2085 break;
2086
2087 default:
2088 LOG(FATAL) << "Unexpected type conversion from " << input_type
2089 << " to " << result_type;
2090 }
2091 break;
2092
Roland Levillain946e1432014-11-11 17:35:19 +00002093 case Primitive::kPrimInt:
2094 switch (input_type) {
2095 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002096 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002097 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002098 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002099 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002100 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002101 Address(CpuRegister(RSP), in.GetStackIndex()));
2102 } else {
2103 DCHECK(in.IsConstant());
2104 DCHECK(in.GetConstant()->IsLongConstant());
2105 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002106 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002107 }
2108 break;
2109
Roland Levillain3f8f9362014-12-02 17:45:01 +00002110 case Primitive::kPrimFloat: {
2111 // Processing a Dex `float-to-int' instruction.
2112 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2113 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain3f8f9362014-12-02 17:45:01 +00002114 Label done, nan;
2115
2116 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002117 // if input >= (float)INT_MAX goto done
2118 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002119 __ j(kAboveEqual, &done);
2120 // if input == NaN goto nan
2121 __ j(kUnordered, &nan);
2122 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002123 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002124 __ jmp(&done);
2125 __ Bind(&nan);
2126 // output = 0
2127 __ xorl(output, output);
2128 __ Bind(&done);
2129 break;
2130 }
2131
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002132 case Primitive::kPrimDouble: {
2133 // Processing a Dex `double-to-int' instruction.
2134 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2135 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002136 Label done, nan;
2137
2138 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002139 // if input >= (double)INT_MAX goto done
2140 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002141 __ j(kAboveEqual, &done);
2142 // if input == NaN goto nan
2143 __ j(kUnordered, &nan);
2144 // output = double-to-int-truncate(input)
2145 __ cvttsd2si(output, input);
2146 __ jmp(&done);
2147 __ Bind(&nan);
2148 // output = 0
2149 __ xorl(output, output);
2150 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002151 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002152 }
Roland Levillain946e1432014-11-11 17:35:19 +00002153
2154 default:
2155 LOG(FATAL) << "Unexpected type conversion from " << input_type
2156 << " to " << result_type;
2157 }
2158 break;
2159
Roland Levillaindff1f282014-11-05 14:15:05 +00002160 case Primitive::kPrimLong:
2161 switch (input_type) {
2162 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002163 case Primitive::kPrimBoolean:
2164 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002165 case Primitive::kPrimByte:
2166 case Primitive::kPrimShort:
2167 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002168 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002169 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002170 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002171 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002172 break;
2173
Roland Levillain624279f2014-12-04 11:54:28 +00002174 case Primitive::kPrimFloat: {
2175 // Processing a Dex `float-to-long' instruction.
2176 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2177 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain624279f2014-12-04 11:54:28 +00002178 Label done, nan;
2179
Mark Mendell92e83bf2015-05-07 11:25:03 -04002180 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002181 // if input >= (float)LONG_MAX goto done
2182 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002183 __ j(kAboveEqual, &done);
2184 // if input == NaN goto nan
2185 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002186 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002187 __ cvttss2si(output, input, true);
2188 __ jmp(&done);
2189 __ Bind(&nan);
2190 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002191 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002192 __ Bind(&done);
2193 break;
2194 }
2195
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002196 case Primitive::kPrimDouble: {
2197 // Processing a Dex `double-to-long' instruction.
2198 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2199 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002200 Label done, nan;
2201
Mark Mendell92e83bf2015-05-07 11:25:03 -04002202 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002203 // if input >= (double)LONG_MAX goto done
2204 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002205 __ j(kAboveEqual, &done);
2206 // if input == NaN goto nan
2207 __ j(kUnordered, &nan);
2208 // output = double-to-long-truncate(input)
2209 __ cvttsd2si(output, input, true);
2210 __ jmp(&done);
2211 __ Bind(&nan);
2212 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002213 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002214 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002215 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002216 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002217
2218 default:
2219 LOG(FATAL) << "Unexpected type conversion from " << input_type
2220 << " to " << result_type;
2221 }
2222 break;
2223
Roland Levillain981e4542014-11-14 11:47:14 +00002224 case Primitive::kPrimChar:
2225 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002226 case Primitive::kPrimBoolean:
2227 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002228 case Primitive::kPrimByte:
2229 case Primitive::kPrimShort:
2230 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002231 // Processing a Dex `int-to-char' instruction.
2232 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002233 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002234 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002235 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002236 Address(CpuRegister(RSP), in.GetStackIndex()));
2237 } else {
2238 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002239 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002240 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2241 }
2242 break;
2243
2244 default:
2245 LOG(FATAL) << "Unexpected type conversion from " << input_type
2246 << " to " << result_type;
2247 }
2248 break;
2249
Roland Levillaindff1f282014-11-05 14:15:05 +00002250 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002251 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002252 case Primitive::kPrimBoolean:
2253 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002254 case Primitive::kPrimByte:
2255 case Primitive::kPrimShort:
2256 case Primitive::kPrimInt:
2257 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002258 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002259 if (in.IsRegister()) {
2260 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2261 } else if (in.IsConstant()) {
2262 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2263 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2264 if (v == 0) {
2265 __ xorps(dest, dest);
2266 } else {
2267 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2268 }
2269 } else {
2270 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2271 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2272 }
Roland Levillaincff13742014-11-17 14:32:17 +00002273 break;
2274
2275 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002276 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002277 if (in.IsRegister()) {
2278 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2279 } else if (in.IsConstant()) {
2280 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2281 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2282 if (v == 0) {
2283 __ xorps(dest, dest);
2284 } else {
2285 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2286 }
2287 } else {
2288 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2289 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2290 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002291 break;
2292
Roland Levillaincff13742014-11-17 14:32:17 +00002293 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002294 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002295 if (in.IsFpuRegister()) {
2296 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2297 } else if (in.IsConstant()) {
2298 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2299 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2300 if (bit_cast<int64_t, double>(v) == 0) {
2301 __ xorps(dest, dest);
2302 } else {
2303 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2304 }
2305 } else {
2306 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2307 Address(CpuRegister(RSP), in.GetStackIndex()));
2308 }
Roland Levillaincff13742014-11-17 14:32:17 +00002309 break;
2310
2311 default:
2312 LOG(FATAL) << "Unexpected type conversion from " << input_type
2313 << " to " << result_type;
2314 };
2315 break;
2316
Roland Levillaindff1f282014-11-05 14:15:05 +00002317 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002318 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002319 case Primitive::kPrimBoolean:
2320 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002321 case Primitive::kPrimByte:
2322 case Primitive::kPrimShort:
2323 case Primitive::kPrimInt:
2324 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002325 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002326 if (in.IsRegister()) {
2327 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2328 } else if (in.IsConstant()) {
2329 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2330 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2331 if (v == 0) {
2332 __ xorpd(dest, dest);
2333 } else {
2334 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2335 }
2336 } else {
2337 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2338 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2339 }
Roland Levillaincff13742014-11-17 14:32:17 +00002340 break;
2341
2342 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002343 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002344 if (in.IsRegister()) {
2345 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2346 } else if (in.IsConstant()) {
2347 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2348 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2349 if (v == 0) {
2350 __ xorpd(dest, dest);
2351 } else {
2352 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2353 }
2354 } else {
2355 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2356 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2357 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002358 break;
2359
Roland Levillaincff13742014-11-17 14:32:17 +00002360 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002361 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002362 if (in.IsFpuRegister()) {
2363 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2364 } else if (in.IsConstant()) {
2365 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2366 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2367 if (bit_cast<int32_t, float>(v) == 0) {
2368 __ xorpd(dest, dest);
2369 } else {
2370 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2371 }
2372 } else {
2373 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2374 Address(CpuRegister(RSP), in.GetStackIndex()));
2375 }
Roland Levillaincff13742014-11-17 14:32:17 +00002376 break;
2377
2378 default:
2379 LOG(FATAL) << "Unexpected type conversion from " << input_type
2380 << " to " << result_type;
2381 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002382 break;
2383
2384 default:
2385 LOG(FATAL) << "Unexpected type conversion from " << input_type
2386 << " to " << result_type;
2387 }
2388}
2389
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002390void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002391 LocationSummary* locations =
2392 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002393 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002394 case Primitive::kPrimInt: {
2395 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002396 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2397 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002398 break;
2399 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002400
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002401 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002402 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002403 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002404 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002406 break;
2407 }
2408
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002409 case Primitive::kPrimDouble:
2410 case Primitive::kPrimFloat: {
2411 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002412 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002413 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002414 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002415 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002416
2417 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002418 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002419 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002420}
2421
2422void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2423 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002424 Location first = locations->InAt(0);
2425 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002426 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002427
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002428 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002429 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002430 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002431 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2432 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002433 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2434 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002435 } else {
2436 __ leal(out.AsRegister<CpuRegister>(), Address(
2437 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2438 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002439 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002440 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2441 __ addl(out.AsRegister<CpuRegister>(),
2442 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2443 } else {
2444 __ leal(out.AsRegister<CpuRegister>(), Address(
2445 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2446 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002447 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002448 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002449 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002450 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002451 break;
2452 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002453
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002454 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002455 if (second.IsRegister()) {
2456 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2457 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002458 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2459 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002460 } else {
2461 __ leaq(out.AsRegister<CpuRegister>(), Address(
2462 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2463 }
2464 } else {
2465 DCHECK(second.IsConstant());
2466 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2467 int32_t int32_value = Low32Bits(value);
2468 DCHECK_EQ(int32_value, value);
2469 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2470 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2471 } else {
2472 __ leaq(out.AsRegister<CpuRegister>(), Address(
2473 first.AsRegister<CpuRegister>(), int32_value));
2474 }
2475 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002476 break;
2477 }
2478
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002479 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002480 if (second.IsFpuRegister()) {
2481 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2482 } else if (second.IsConstant()) {
2483 __ addss(first.AsFpuRegister<XmmRegister>(),
2484 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2485 } else {
2486 DCHECK(second.IsStackSlot());
2487 __ addss(first.AsFpuRegister<XmmRegister>(),
2488 Address(CpuRegister(RSP), second.GetStackIndex()));
2489 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002490 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002491 }
2492
2493 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002494 if (second.IsFpuRegister()) {
2495 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2496 } else if (second.IsConstant()) {
2497 __ addsd(first.AsFpuRegister<XmmRegister>(),
2498 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2499 } else {
2500 DCHECK(second.IsDoubleStackSlot());
2501 __ addsd(first.AsFpuRegister<XmmRegister>(),
2502 Address(CpuRegister(RSP), second.GetStackIndex()));
2503 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002504 break;
2505 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002506
2507 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002508 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002509 }
2510}
2511
2512void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002513 LocationSummary* locations =
2514 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002515 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002516 case Primitive::kPrimInt: {
2517 locations->SetInAt(0, Location::RequiresRegister());
2518 locations->SetInAt(1, Location::Any());
2519 locations->SetOut(Location::SameAsFirstInput());
2520 break;
2521 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002522 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002523 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002524 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002525 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002526 break;
2527 }
Calin Juravle11351682014-10-23 15:38:15 +01002528 case Primitive::kPrimFloat:
2529 case Primitive::kPrimDouble: {
2530 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002531 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002532 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002533 break;
Calin Juravle11351682014-10-23 15:38:15 +01002534 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002535 default:
Calin Juravle11351682014-10-23 15:38:15 +01002536 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002537 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002538}
2539
2540void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2541 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002542 Location first = locations->InAt(0);
2543 Location second = locations->InAt(1);
2544 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002545 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002546 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002547 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002548 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002549 } else if (second.IsConstant()) {
2550 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002551 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002552 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002553 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002554 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002555 break;
2556 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002557 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002558 if (second.IsConstant()) {
2559 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2560 DCHECK(IsInt<32>(value));
2561 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2562 } else {
2563 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2564 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002565 break;
2566 }
2567
Calin Juravle11351682014-10-23 15:38:15 +01002568 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002569 if (second.IsFpuRegister()) {
2570 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2571 } else if (second.IsConstant()) {
2572 __ subss(first.AsFpuRegister<XmmRegister>(),
2573 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2574 } else {
2575 DCHECK(second.IsStackSlot());
2576 __ subss(first.AsFpuRegister<XmmRegister>(),
2577 Address(CpuRegister(RSP), second.GetStackIndex()));
2578 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002579 break;
Calin Juravle11351682014-10-23 15:38:15 +01002580 }
2581
2582 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002583 if (second.IsFpuRegister()) {
2584 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2585 } else if (second.IsConstant()) {
2586 __ subsd(first.AsFpuRegister<XmmRegister>(),
2587 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2588 } else {
2589 DCHECK(second.IsDoubleStackSlot());
2590 __ subsd(first.AsFpuRegister<XmmRegister>(),
2591 Address(CpuRegister(RSP), second.GetStackIndex()));
2592 }
Calin Juravle11351682014-10-23 15:38:15 +01002593 break;
2594 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002595
2596 default:
Calin Juravle11351682014-10-23 15:38:15 +01002597 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002598 }
2599}
2600
Calin Juravle34bacdf2014-10-07 20:23:36 +01002601void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2602 LocationSummary* locations =
2603 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2604 switch (mul->GetResultType()) {
2605 case Primitive::kPrimInt: {
2606 locations->SetInAt(0, Location::RequiresRegister());
2607 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002608 if (mul->InputAt(1)->IsIntConstant()) {
2609 // Can use 3 operand multiply.
2610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2611 } else {
2612 locations->SetOut(Location::SameAsFirstInput());
2613 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002614 break;
2615 }
2616 case Primitive::kPrimLong: {
2617 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002618 locations->SetInAt(1, Location::Any());
2619 if (mul->InputAt(1)->IsLongConstant() &&
2620 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002621 // Can use 3 operand multiply.
2622 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2623 } else {
2624 locations->SetOut(Location::SameAsFirstInput());
2625 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002626 break;
2627 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002628 case Primitive::kPrimFloat:
2629 case Primitive::kPrimDouble: {
2630 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002631 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002632 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002633 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002634 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002635
2636 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002637 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002638 }
2639}
2640
2641void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2642 LocationSummary* locations = mul->GetLocations();
2643 Location first = locations->InAt(0);
2644 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002645 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002646 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002647 case Primitive::kPrimInt:
2648 // The constant may have ended up in a register, so test explicitly to avoid
2649 // problems where the output may not be the same as the first operand.
2650 if (mul->InputAt(1)->IsIntConstant()) {
2651 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2652 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2653 } else if (second.IsRegister()) {
2654 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002655 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002656 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002657 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002658 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002659 __ imull(first.AsRegister<CpuRegister>(),
2660 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002661 }
2662 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002663 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002664 // The constant may have ended up in a register, so test explicitly to avoid
2665 // problems where the output may not be the same as the first operand.
2666 if (mul->InputAt(1)->IsLongConstant()) {
2667 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2668 if (IsInt<32>(value)) {
2669 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2670 Immediate(static_cast<int32_t>(value)));
2671 } else {
2672 // Have to use the constant area.
2673 DCHECK(first.Equals(out));
2674 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2675 }
2676 } else if (second.IsRegister()) {
2677 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002678 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002679 } else {
2680 DCHECK(second.IsDoubleStackSlot());
2681 DCHECK(first.Equals(out));
2682 __ imulq(first.AsRegister<CpuRegister>(),
2683 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002684 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002685 break;
2686 }
2687
Calin Juravleb5bfa962014-10-21 18:02:24 +01002688 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002689 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002690 if (second.IsFpuRegister()) {
2691 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2692 } else if (second.IsConstant()) {
2693 __ mulss(first.AsFpuRegister<XmmRegister>(),
2694 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2695 } else {
2696 DCHECK(second.IsStackSlot());
2697 __ mulss(first.AsFpuRegister<XmmRegister>(),
2698 Address(CpuRegister(RSP), second.GetStackIndex()));
2699 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002700 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002701 }
2702
2703 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002704 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002705 if (second.IsFpuRegister()) {
2706 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2707 } else if (second.IsConstant()) {
2708 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2709 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2710 } else {
2711 DCHECK(second.IsDoubleStackSlot());
2712 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2713 Address(CpuRegister(RSP), second.GetStackIndex()));
2714 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002715 break;
2716 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002717
2718 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002719 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002720 }
2721}
2722
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002723void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2724 uint32_t stack_adjustment, bool is_float) {
2725 if (source.IsStackSlot()) {
2726 DCHECK(is_float);
2727 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2728 } else if (source.IsDoubleStackSlot()) {
2729 DCHECK(!is_float);
2730 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2731 } else {
2732 // Write the value to the temporary location on the stack and load to FP stack.
2733 if (is_float) {
2734 Location stack_temp = Location::StackSlot(temp_offset);
2735 codegen_->Move(stack_temp, source);
2736 __ flds(Address(CpuRegister(RSP), temp_offset));
2737 } else {
2738 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2739 codegen_->Move(stack_temp, source);
2740 __ fldl(Address(CpuRegister(RSP), temp_offset));
2741 }
2742 }
2743}
2744
2745void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2746 Primitive::Type type = rem->GetResultType();
2747 bool is_float = type == Primitive::kPrimFloat;
2748 size_t elem_size = Primitive::ComponentSize(type);
2749 LocationSummary* locations = rem->GetLocations();
2750 Location first = locations->InAt(0);
2751 Location second = locations->InAt(1);
2752 Location out = locations->Out();
2753
2754 // Create stack space for 2 elements.
2755 // TODO: enhance register allocator to ask for stack temporaries.
2756 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2757
2758 // Load the values to the FP stack in reverse order, using temporaries if needed.
2759 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2760 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2761
2762 // Loop doing FPREM until we stabilize.
2763 Label retry;
2764 __ Bind(&retry);
2765 __ fprem();
2766
2767 // Move FP status to AX.
2768 __ fstsw();
2769
2770 // And see if the argument reduction is complete. This is signaled by the
2771 // C2 FPU flag bit set to 0.
2772 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2773 __ j(kNotEqual, &retry);
2774
2775 // We have settled on the final value. Retrieve it into an XMM register.
2776 // Store FP top of stack to real stack.
2777 if (is_float) {
2778 __ fsts(Address(CpuRegister(RSP), 0));
2779 } else {
2780 __ fstl(Address(CpuRegister(RSP), 0));
2781 }
2782
2783 // Pop the 2 items from the FP stack.
2784 __ fucompp();
2785
2786 // Load the value from the stack into an XMM register.
2787 DCHECK(out.IsFpuRegister()) << out;
2788 if (is_float) {
2789 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2790 } else {
2791 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2792 }
2793
2794 // And remove the temporary stack space we allocated.
2795 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2796}
2797
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002798void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2799 DCHECK(instruction->IsDiv() || instruction->IsRem());
2800
2801 LocationSummary* locations = instruction->GetLocations();
2802 Location second = locations->InAt(1);
2803 DCHECK(second.IsConstant());
2804
2805 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2806 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002807 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002808
2809 DCHECK(imm == 1 || imm == -1);
2810
2811 switch (instruction->GetResultType()) {
2812 case Primitive::kPrimInt: {
2813 if (instruction->IsRem()) {
2814 __ xorl(output_register, output_register);
2815 } else {
2816 __ movl(output_register, input_register);
2817 if (imm == -1) {
2818 __ negl(output_register);
2819 }
2820 }
2821 break;
2822 }
2823
2824 case Primitive::kPrimLong: {
2825 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002826 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002827 } else {
2828 __ movq(output_register, input_register);
2829 if (imm == -1) {
2830 __ negq(output_register);
2831 }
2832 }
2833 break;
2834 }
2835
2836 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002837 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002838 }
2839}
2840
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002841void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002842 LocationSummary* locations = instruction->GetLocations();
2843 Location second = locations->InAt(1);
2844
2845 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2846 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2847
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002848 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002849
2850 DCHECK(IsPowerOfTwo(std::abs(imm)));
2851
2852 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2853
2854 if (instruction->GetResultType() == Primitive::kPrimInt) {
2855 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2856 __ testl(numerator, numerator);
2857 __ cmov(kGreaterEqual, tmp, numerator);
2858 int shift = CTZ(imm);
2859 __ sarl(tmp, Immediate(shift));
2860
2861 if (imm < 0) {
2862 __ negl(tmp);
2863 }
2864
2865 __ movl(output_register, tmp);
2866 } else {
2867 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2868 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2869
Mark Mendell92e83bf2015-05-07 11:25:03 -04002870 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002871 __ addq(rdx, numerator);
2872 __ testq(numerator, numerator);
2873 __ cmov(kGreaterEqual, rdx, numerator);
2874 int shift = CTZ(imm);
2875 __ sarq(rdx, Immediate(shift));
2876
2877 if (imm < 0) {
2878 __ negq(rdx);
2879 }
2880
2881 __ movq(output_register, rdx);
2882 }
2883}
2884
2885void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2886 DCHECK(instruction->IsDiv() || instruction->IsRem());
2887
2888 LocationSummary* locations = instruction->GetLocations();
2889 Location second = locations->InAt(1);
2890
2891 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2892 : locations->GetTemp(0).AsRegister<CpuRegister>();
2893 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2894 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2895 : locations->Out().AsRegister<CpuRegister>();
2896 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2897
2898 DCHECK_EQ(RAX, eax.AsRegister());
2899 DCHECK_EQ(RDX, edx.AsRegister());
2900 if (instruction->IsDiv()) {
2901 DCHECK_EQ(RAX, out.AsRegister());
2902 } else {
2903 DCHECK_EQ(RDX, out.AsRegister());
2904 }
2905
2906 int64_t magic;
2907 int shift;
2908
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002909 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002910 if (instruction->GetResultType() == Primitive::kPrimInt) {
2911 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2912
2913 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2914
2915 __ movl(numerator, eax);
2916
2917 Label no_div;
2918 Label end;
2919 __ testl(eax, eax);
2920 __ j(kNotEqual, &no_div);
2921
2922 __ xorl(out, out);
2923 __ jmp(&end);
2924
2925 __ Bind(&no_div);
2926
2927 __ movl(eax, Immediate(magic));
2928 __ imull(numerator);
2929
2930 if (imm > 0 && magic < 0) {
2931 __ addl(edx, numerator);
2932 } else if (imm < 0 && magic > 0) {
2933 __ subl(edx, numerator);
2934 }
2935
2936 if (shift != 0) {
2937 __ sarl(edx, Immediate(shift));
2938 }
2939
2940 __ movl(eax, edx);
2941 __ shrl(edx, Immediate(31));
2942 __ addl(edx, eax);
2943
2944 if (instruction->IsRem()) {
2945 __ movl(eax, numerator);
2946 __ imull(edx, Immediate(imm));
2947 __ subl(eax, edx);
2948 __ movl(edx, eax);
2949 } else {
2950 __ movl(eax, edx);
2951 }
2952 __ Bind(&end);
2953 } else {
2954 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2955
2956 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2957
2958 CpuRegister rax = eax;
2959 CpuRegister rdx = edx;
2960
2961 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2962
2963 // Save the numerator.
2964 __ movq(numerator, rax);
2965
2966 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002967 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002968
2969 // RDX:RAX = magic * numerator
2970 __ imulq(numerator);
2971
2972 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002973 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002974 __ addq(rdx, numerator);
2975 } else if (imm < 0 && magic > 0) {
2976 // RDX -= numerator
2977 __ subq(rdx, numerator);
2978 }
2979
2980 // Shift if needed.
2981 if (shift != 0) {
2982 __ sarq(rdx, Immediate(shift));
2983 }
2984
2985 // RDX += 1 if RDX < 0
2986 __ movq(rax, rdx);
2987 __ shrq(rdx, Immediate(63));
2988 __ addq(rdx, rax);
2989
2990 if (instruction->IsRem()) {
2991 __ movq(rax, numerator);
2992
2993 if (IsInt<32>(imm)) {
2994 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2995 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002996 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002997 }
2998
2999 __ subq(rax, rdx);
3000 __ movq(rdx, rax);
3001 } else {
3002 __ movq(rax, rdx);
3003 }
3004 }
3005}
3006
Calin Juravlebacfec32014-11-14 15:54:36 +00003007void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3008 DCHECK(instruction->IsDiv() || instruction->IsRem());
3009 Primitive::Type type = instruction->GetResultType();
3010 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3011
3012 bool is_div = instruction->IsDiv();
3013 LocationSummary* locations = instruction->GetLocations();
3014
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003015 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3016 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003017
Roland Levillain271ab9c2014-11-27 15:23:57 +00003018 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003019 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003020
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003021 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003022 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003023
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003024 if (imm == 0) {
3025 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3026 } else if (imm == 1 || imm == -1) {
3027 DivRemOneOrMinusOne(instruction);
3028 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003029 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003030 } else {
3031 DCHECK(imm <= -2 || imm >= 2);
3032 GenerateDivRemWithAnyConstant(instruction);
3033 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003034 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003035 SlowPathCodeX86_64* slow_path =
3036 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3037 out.AsRegister(), type, is_div);
3038 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003039
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003040 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3041 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3042 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3043 // so it's safe to just use negl instead of more complex comparisons.
3044 if (type == Primitive::kPrimInt) {
3045 __ cmpl(second_reg, Immediate(-1));
3046 __ j(kEqual, slow_path->GetEntryLabel());
3047 // edx:eax <- sign-extended of eax
3048 __ cdq();
3049 // eax = quotient, edx = remainder
3050 __ idivl(second_reg);
3051 } else {
3052 __ cmpq(second_reg, Immediate(-1));
3053 __ j(kEqual, slow_path->GetEntryLabel());
3054 // rdx:rax <- sign-extended of rax
3055 __ cqo();
3056 // rax = quotient, rdx = remainder
3057 __ idivq(second_reg);
3058 }
3059 __ Bind(slow_path->GetExitLabel());
3060 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003061}
3062
Calin Juravle7c4954d2014-10-28 16:57:40 +00003063void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3064 LocationSummary* locations =
3065 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3066 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003067 case Primitive::kPrimInt:
3068 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003069 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003070 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003071 locations->SetOut(Location::SameAsFirstInput());
3072 // Intel uses edx:eax as the dividend.
3073 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003074 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3075 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3076 // output and request another temp.
3077 if (div->InputAt(1)->IsConstant()) {
3078 locations->AddTemp(Location::RequiresRegister());
3079 }
Calin Juravled0d48522014-11-04 16:40:20 +00003080 break;
3081 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003082
Calin Juravle7c4954d2014-10-28 16:57:40 +00003083 case Primitive::kPrimFloat:
3084 case Primitive::kPrimDouble: {
3085 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003086 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003087 locations->SetOut(Location::SameAsFirstInput());
3088 break;
3089 }
3090
3091 default:
3092 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3093 }
3094}
3095
3096void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3097 LocationSummary* locations = div->GetLocations();
3098 Location first = locations->InAt(0);
3099 Location second = locations->InAt(1);
3100 DCHECK(first.Equals(locations->Out()));
3101
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003102 Primitive::Type type = div->GetResultType();
3103 switch (type) {
3104 case Primitive::kPrimInt:
3105 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003106 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003107 break;
3108 }
3109
Calin Juravle7c4954d2014-10-28 16:57:40 +00003110 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003111 if (second.IsFpuRegister()) {
3112 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3113 } else if (second.IsConstant()) {
3114 __ divss(first.AsFpuRegister<XmmRegister>(),
3115 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3116 } else {
3117 DCHECK(second.IsStackSlot());
3118 __ divss(first.AsFpuRegister<XmmRegister>(),
3119 Address(CpuRegister(RSP), second.GetStackIndex()));
3120 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003121 break;
3122 }
3123
3124 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003125 if (second.IsFpuRegister()) {
3126 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3127 } else if (second.IsConstant()) {
3128 __ divsd(first.AsFpuRegister<XmmRegister>(),
3129 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3130 } else {
3131 DCHECK(second.IsDoubleStackSlot());
3132 __ divsd(first.AsFpuRegister<XmmRegister>(),
3133 Address(CpuRegister(RSP), second.GetStackIndex()));
3134 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003135 break;
3136 }
3137
3138 default:
3139 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3140 }
3141}
3142
Calin Juravlebacfec32014-11-14 15:54:36 +00003143void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003144 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003145 LocationSummary* locations =
3146 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003147
3148 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003149 case Primitive::kPrimInt:
3150 case Primitive::kPrimLong: {
3151 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003152 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003153 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3154 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003155 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3156 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3157 // output and request another temp.
3158 if (rem->InputAt(1)->IsConstant()) {
3159 locations->AddTemp(Location::RequiresRegister());
3160 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003161 break;
3162 }
3163
3164 case Primitive::kPrimFloat:
3165 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003166 locations->SetInAt(0, Location::Any());
3167 locations->SetInAt(1, Location::Any());
3168 locations->SetOut(Location::RequiresFpuRegister());
3169 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003170 break;
3171 }
3172
3173 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003174 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003175 }
3176}
3177
3178void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3179 Primitive::Type type = rem->GetResultType();
3180 switch (type) {
3181 case Primitive::kPrimInt:
3182 case Primitive::kPrimLong: {
3183 GenerateDivRemIntegral(rem);
3184 break;
3185 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003186 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003187 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003188 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003189 break;
3190 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003191 default:
3192 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3193 }
3194}
3195
Calin Juravled0d48522014-11-04 16:40:20 +00003196void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3197 LocationSummary* locations =
3198 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3199 locations->SetInAt(0, Location::Any());
3200 if (instruction->HasUses()) {
3201 locations->SetOut(Location::SameAsFirstInput());
3202 }
3203}
3204
3205void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3206 SlowPathCodeX86_64* slow_path =
3207 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3208 codegen_->AddSlowPath(slow_path);
3209
3210 LocationSummary* locations = instruction->GetLocations();
3211 Location value = locations->InAt(0);
3212
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003213 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003214 case Primitive::kPrimByte:
3215 case Primitive::kPrimChar:
3216 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003217 case Primitive::kPrimInt: {
3218 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003219 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003220 __ j(kEqual, slow_path->GetEntryLabel());
3221 } else if (value.IsStackSlot()) {
3222 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3223 __ j(kEqual, slow_path->GetEntryLabel());
3224 } else {
3225 DCHECK(value.IsConstant()) << value;
3226 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3227 __ jmp(slow_path->GetEntryLabel());
3228 }
3229 }
3230 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003231 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003232 case Primitive::kPrimLong: {
3233 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003234 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003235 __ j(kEqual, slow_path->GetEntryLabel());
3236 } else if (value.IsDoubleStackSlot()) {
3237 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3238 __ j(kEqual, slow_path->GetEntryLabel());
3239 } else {
3240 DCHECK(value.IsConstant()) << value;
3241 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3242 __ jmp(slow_path->GetEntryLabel());
3243 }
3244 }
3245 break;
3246 }
3247 default:
3248 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003249 }
Calin Juravled0d48522014-11-04 16:40:20 +00003250}
3251
Calin Juravle9aec02f2014-11-18 23:06:35 +00003252void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3253 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3254
3255 LocationSummary* locations =
3256 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3257
3258 switch (op->GetResultType()) {
3259 case Primitive::kPrimInt:
3260 case Primitive::kPrimLong: {
3261 locations->SetInAt(0, Location::RequiresRegister());
3262 // The shift count needs to be in CL.
3263 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3264 locations->SetOut(Location::SameAsFirstInput());
3265 break;
3266 }
3267 default:
3268 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3269 }
3270}
3271
3272void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3273 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3274
3275 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003276 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003277 Location second = locations->InAt(1);
3278
3279 switch (op->GetResultType()) {
3280 case Primitive::kPrimInt: {
3281 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003282 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003283 if (op->IsShl()) {
3284 __ shll(first_reg, second_reg);
3285 } else if (op->IsShr()) {
3286 __ sarl(first_reg, second_reg);
3287 } else {
3288 __ shrl(first_reg, second_reg);
3289 }
3290 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003291 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003292 if (op->IsShl()) {
3293 __ shll(first_reg, imm);
3294 } else if (op->IsShr()) {
3295 __ sarl(first_reg, imm);
3296 } else {
3297 __ shrl(first_reg, imm);
3298 }
3299 }
3300 break;
3301 }
3302 case Primitive::kPrimLong: {
3303 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003304 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003305 if (op->IsShl()) {
3306 __ shlq(first_reg, second_reg);
3307 } else if (op->IsShr()) {
3308 __ sarq(first_reg, second_reg);
3309 } else {
3310 __ shrq(first_reg, second_reg);
3311 }
3312 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003313 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003314 if (op->IsShl()) {
3315 __ shlq(first_reg, imm);
3316 } else if (op->IsShr()) {
3317 __ sarq(first_reg, imm);
3318 } else {
3319 __ shrq(first_reg, imm);
3320 }
3321 }
3322 break;
3323 }
3324 default:
3325 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3326 }
3327}
3328
3329void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3330 HandleShift(shl);
3331}
3332
3333void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3334 HandleShift(shl);
3335}
3336
3337void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3338 HandleShift(shr);
3339}
3340
3341void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3342 HandleShift(shr);
3343}
3344
3345void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3346 HandleShift(ushr);
3347}
3348
3349void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3350 HandleShift(ushr);
3351}
3352
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003353void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003354 LocationSummary* locations =
3355 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003356 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003357 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003358 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003359 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003360}
3361
3362void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3363 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003364 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3365 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003366 // Note: if heap poisoning is enabled, the entry point takes cares
3367 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003368
3369 codegen_->InvokeRuntime(
3370 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3371 instruction,
3372 instruction->GetDexPc(),
3373 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003374
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003375 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003376}
3377
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003378void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3379 LocationSummary* locations =
3380 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3381 InvokeRuntimeCallingConvention calling_convention;
3382 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003383 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003384 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003385 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003386}
3387
3388void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3389 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003390 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3391 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003392
Roland Levillain4d027112015-07-01 15:41:14 +01003393 // Note: if heap poisoning is enabled, the entry point takes cares
3394 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003395 codegen_->InvokeRuntime(
3396 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3397 instruction,
3398 instruction->GetDexPc(),
3399 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003400
3401 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003402}
3403
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003404void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003405 LocationSummary* locations =
3406 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003407 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3408 if (location.IsStackSlot()) {
3409 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3410 } else if (location.IsDoubleStackSlot()) {
3411 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3412 }
3413 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003414}
3415
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003416void InstructionCodeGeneratorX86_64::VisitParameterValue(
3417 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003418 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003419}
3420
3421void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3422 LocationSummary* locations =
3423 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3424 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3425}
3426
3427void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3428 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3429 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003430}
3431
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003432void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003433 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003434 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003435 locations->SetInAt(0, Location::RequiresRegister());
3436 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003437}
3438
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003439void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3440 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003441 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3442 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003443 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003444 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003445 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003446 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003447 break;
3448
3449 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003450 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003451 break;
3452
3453 default:
3454 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3455 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003456}
3457
David Brazdil66d126e2015-04-03 16:02:44 +01003458void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3459 LocationSummary* locations =
3460 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3461 locations->SetInAt(0, Location::RequiresRegister());
3462 locations->SetOut(Location::SameAsFirstInput());
3463}
3464
3465void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003466 LocationSummary* locations = bool_not->GetLocations();
3467 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3468 locations->Out().AsRegister<CpuRegister>().AsRegister());
3469 Location out = locations->Out();
3470 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3471}
3472
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003473void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003474 LocationSummary* locations =
3475 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003476 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3477 locations->SetInAt(i, Location::Any());
3478 }
3479 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003480}
3481
3482void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003483 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003484 LOG(FATAL) << "Unimplemented";
3485}
3486
Calin Juravle52c48962014-12-16 17:02:57 +00003487void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3488 /*
3489 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3490 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3491 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3492 */
3493 switch (kind) {
3494 case MemBarrierKind::kAnyAny: {
3495 __ mfence();
3496 break;
3497 }
3498 case MemBarrierKind::kAnyStore:
3499 case MemBarrierKind::kLoadAny:
3500 case MemBarrierKind::kStoreStore: {
3501 // nop
3502 break;
3503 }
3504 default:
3505 LOG(FATAL) << "Unexpected memory barier " << kind;
3506 }
3507}
3508
3509void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3510 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3511
Nicolas Geoffray39468442014-09-02 15:17:15 +01003512 LocationSummary* locations =
3513 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003514 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003515 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3516 locations->SetOut(Location::RequiresFpuRegister());
3517 } else {
3518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3519 }
Calin Juravle52c48962014-12-16 17:02:57 +00003520}
3521
3522void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3523 const FieldInfo& field_info) {
3524 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3525
3526 LocationSummary* locations = instruction->GetLocations();
3527 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3528 Location out = locations->Out();
3529 bool is_volatile = field_info.IsVolatile();
3530 Primitive::Type field_type = field_info.GetFieldType();
3531 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3532
3533 switch (field_type) {
3534 case Primitive::kPrimBoolean: {
3535 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3536 break;
3537 }
3538
3539 case Primitive::kPrimByte: {
3540 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3541 break;
3542 }
3543
3544 case Primitive::kPrimShort: {
3545 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3546 break;
3547 }
3548
3549 case Primitive::kPrimChar: {
3550 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3551 break;
3552 }
3553
3554 case Primitive::kPrimInt:
3555 case Primitive::kPrimNot: {
3556 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3557 break;
3558 }
3559
3560 case Primitive::kPrimLong: {
3561 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3562 break;
3563 }
3564
3565 case Primitive::kPrimFloat: {
3566 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3567 break;
3568 }
3569
3570 case Primitive::kPrimDouble: {
3571 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3572 break;
3573 }
3574
3575 case Primitive::kPrimVoid:
3576 LOG(FATAL) << "Unreachable type " << field_type;
3577 UNREACHABLE();
3578 }
3579
Calin Juravle77520bc2015-01-12 18:45:46 +00003580 codegen_->MaybeRecordImplicitNullCheck(instruction);
3581
Calin Juravle52c48962014-12-16 17:02:57 +00003582 if (is_volatile) {
3583 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3584 }
Roland Levillain4d027112015-07-01 15:41:14 +01003585
3586 if (field_type == Primitive::kPrimNot) {
3587 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3588 }
Calin Juravle52c48962014-12-16 17:02:57 +00003589}
3590
3591void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3592 const FieldInfo& field_info) {
3593 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3594
3595 LocationSummary* locations =
3596 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003597 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003598 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003599 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003600
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003601 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003602 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3603 locations->SetInAt(1, Location::RequiresFpuRegister());
3604 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003605 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003606 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003607 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003608 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003609 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003610 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003611 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3612 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003613 locations->AddTemp(Location::RequiresRegister());
3614 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003615}
3616
Calin Juravle52c48962014-12-16 17:02:57 +00003617void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003618 const FieldInfo& field_info,
3619 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003620 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3621
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003622 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003623 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3624 Location value = locations->InAt(1);
3625 bool is_volatile = field_info.IsVolatile();
3626 Primitive::Type field_type = field_info.GetFieldType();
3627 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3628
3629 if (is_volatile) {
3630 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3631 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003632
3633 switch (field_type) {
3634 case Primitive::kPrimBoolean:
3635 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003636 if (value.IsConstant()) {
3637 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3638 __ movb(Address(base, offset), Immediate(v));
3639 } else {
3640 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3641 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003642 break;
3643 }
3644
3645 case Primitive::kPrimShort:
3646 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003647 if (value.IsConstant()) {
3648 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3649 __ movw(Address(base, offset), Immediate(v));
3650 } else {
3651 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3652 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003653 break;
3654 }
3655
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003656 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003657 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003658 if (value.IsConstant()) {
3659 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003660 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3661 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3662 // Note: if heap poisoning is enabled, no need to poison
3663 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003664 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003665 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003666 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3667 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3668 __ movl(temp, value.AsRegister<CpuRegister>());
3669 __ PoisonHeapReference(temp);
3670 __ movl(Address(base, offset), temp);
3671 } else {
3672 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3673 }
Mark Mendell40741f32015-04-20 22:10:34 -04003674 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003675 break;
3676 }
3677
3678 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003679 if (value.IsConstant()) {
3680 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3681 DCHECK(IsInt<32>(v));
3682 int32_t v_32 = v;
3683 __ movq(Address(base, offset), Immediate(v_32));
3684 } else {
3685 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3686 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003687 break;
3688 }
3689
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003690 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003691 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003692 break;
3693 }
3694
3695 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003696 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003697 break;
3698 }
3699
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003700 case Primitive::kPrimVoid:
3701 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003702 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003703 }
Calin Juravle52c48962014-12-16 17:02:57 +00003704
Calin Juravle77520bc2015-01-12 18:45:46 +00003705 codegen_->MaybeRecordImplicitNullCheck(instruction);
3706
3707 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3708 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3709 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003710 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003711 }
3712
Calin Juravle52c48962014-12-16 17:02:57 +00003713 if (is_volatile) {
3714 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3715 }
3716}
3717
3718void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3719 HandleFieldSet(instruction, instruction->GetFieldInfo());
3720}
3721
3722void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003723 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003724}
3725
3726void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003727 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003728}
3729
3730void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003731 HandleFieldGet(instruction, instruction->GetFieldInfo());
3732}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003733
Calin Juravle52c48962014-12-16 17:02:57 +00003734void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3735 HandleFieldGet(instruction);
3736}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003737
Calin Juravle52c48962014-12-16 17:02:57 +00003738void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3739 HandleFieldGet(instruction, instruction->GetFieldInfo());
3740}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003741
Calin Juravle52c48962014-12-16 17:02:57 +00003742void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3743 HandleFieldSet(instruction, instruction->GetFieldInfo());
3744}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003745
Calin Juravle52c48962014-12-16 17:02:57 +00003746void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003747 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003748}
3749
3750void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003751 LocationSummary* locations =
3752 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003753 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3754 ? Location::RequiresRegister()
3755 : Location::Any();
3756 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003757 if (instruction->HasUses()) {
3758 locations->SetOut(Location::SameAsFirstInput());
3759 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003760}
3761
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003762void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003763 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3764 return;
3765 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003766 LocationSummary* locations = instruction->GetLocations();
3767 Location obj = locations->InAt(0);
3768
3769 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3770 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3771}
3772
3773void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003774 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003775 codegen_->AddSlowPath(slow_path);
3776
3777 LocationSummary* locations = instruction->GetLocations();
3778 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003779
3780 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003781 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003782 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003783 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003784 } else {
3785 DCHECK(obj.IsConstant()) << obj;
3786 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3787 __ jmp(slow_path->GetEntryLabel());
3788 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003789 }
3790 __ j(kEqual, slow_path->GetEntryLabel());
3791}
3792
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003793void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3794 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3795 GenerateImplicitNullCheck(instruction);
3796 } else {
3797 GenerateExplicitNullCheck(instruction);
3798 }
3799}
3800
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003801void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003802 LocationSummary* locations =
3803 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003804 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003805 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003806 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3807 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3808 } else {
3809 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3810 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003811}
3812
3813void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3814 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003815 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003816 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003817 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003818
Roland Levillain4d027112015-07-01 15:41:14 +01003819 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003820 case Primitive::kPrimBoolean: {
3821 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003822 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003823 if (index.IsConstant()) {
3824 __ movzxb(out, Address(obj,
3825 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3826 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003827 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003828 }
3829 break;
3830 }
3831
3832 case Primitive::kPrimByte: {
3833 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003834 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003835 if (index.IsConstant()) {
3836 __ movsxb(out, Address(obj,
3837 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3838 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003839 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003840 }
3841 break;
3842 }
3843
3844 case Primitive::kPrimShort: {
3845 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003846 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003847 if (index.IsConstant()) {
3848 __ movsxw(out, Address(obj,
3849 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3850 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003851 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003852 }
3853 break;
3854 }
3855
3856 case Primitive::kPrimChar: {
3857 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003858 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003859 if (index.IsConstant()) {
3860 __ movzxw(out, Address(obj,
3861 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3862 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003863 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003864 }
3865 break;
3866 }
3867
3868 case Primitive::kPrimInt:
3869 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003870 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3871 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003872 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003873 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003874 if (index.IsConstant()) {
3875 __ movl(out, Address(obj,
3876 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3877 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003878 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879 }
3880 break;
3881 }
3882
3883 case Primitive::kPrimLong: {
3884 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003885 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003886 if (index.IsConstant()) {
3887 __ movq(out, Address(obj,
3888 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3889 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003890 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003891 }
3892 break;
3893 }
3894
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003895 case Primitive::kPrimFloat: {
3896 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003897 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003898 if (index.IsConstant()) {
3899 __ movss(out, Address(obj,
3900 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3901 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003902 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003903 }
3904 break;
3905 }
3906
3907 case Primitive::kPrimDouble: {
3908 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003909 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003910 if (index.IsConstant()) {
3911 __ movsd(out, Address(obj,
3912 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3913 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003914 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003915 }
3916 break;
3917 }
3918
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003919 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003920 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003921 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003923 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003924
3925 if (type == Primitive::kPrimNot) {
3926 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3927 __ MaybeUnpoisonHeapReference(out);
3928 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003929}
3930
3931void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003932 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003933
3934 bool needs_write_barrier =
3935 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3936 bool needs_runtime_call = instruction->NeedsTypeCheck();
3937
Nicolas Geoffray39468442014-09-02 15:17:15 +01003938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003939 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3940 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003941 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003942 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3943 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3944 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003945 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003946 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003947 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003948 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3949 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003950 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003951 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003952 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3953 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003954 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003955 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003956 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003957
3958 if (needs_write_barrier) {
3959 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003960 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003961 locations->AddTemp(Location::RequiresRegister());
3962 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003963 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003964}
3965
3966void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3967 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003968 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003969 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003970 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003971 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003972 bool needs_runtime_call = locations->WillCall();
3973 bool needs_write_barrier =
3974 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003975
3976 switch (value_type) {
3977 case Primitive::kPrimBoolean:
3978 case Primitive::kPrimByte: {
3979 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003980 if (index.IsConstant()) {
3981 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003982 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003983 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003984 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003985 __ movb(Address(obj, offset),
3986 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003987 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003988 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003989 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003990 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3991 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003992 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003993 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003994 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3995 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003996 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003997 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003998 break;
3999 }
4000
4001 case Primitive::kPrimShort:
4002 case Primitive::kPrimChar: {
4003 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004004 if (index.IsConstant()) {
4005 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004006 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004007 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004008 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004009 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00004010 __ movw(Address(obj, offset),
4011 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004012 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004013 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004014 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004015 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004016 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
4017 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004018 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004019 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00004020 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004021 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4022 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004023 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004024 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004025 break;
4026 }
4027
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004028 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004029 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004030 if (!needs_runtime_call) {
4031 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4032 if (index.IsConstant()) {
4033 size_t offset =
4034 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4035 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004036 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4037 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4038 __ movl(temp, value.AsRegister<CpuRegister>());
4039 __ PoisonHeapReference(temp);
4040 __ movl(Address(obj, offset), temp);
4041 } else {
4042 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
4043 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004044 } else {
4045 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004046 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004047 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4048 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4049 // Note: if heap poisoning is enabled, no need to poison
4050 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04004051 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004052 }
4053 } else {
4054 DCHECK(index.IsRegister()) << index;
4055 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004056 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4057 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4058 __ movl(temp, value.AsRegister<CpuRegister>());
4059 __ PoisonHeapReference(temp);
4060 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4061 } else {
4062 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4063 value.AsRegister<CpuRegister>());
4064 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004065 } else {
4066 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004067 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004068 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4069 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4070 // Note: if heap poisoning is enabled, no need to poison
4071 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004072 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004073 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004074 }
4075 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004076 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004077 if (needs_write_barrier) {
4078 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004079 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4080 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004081 codegen_->MarkGCCard(
4082 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004083 }
4084 } else {
4085 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004086 // Note: if heap poisoning is enabled, pAputObject takes cares
4087 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004088 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4089 instruction,
4090 instruction->GetDexPc(),
4091 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004092 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004093 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004094 break;
4095 }
4096
4097 case Primitive::kPrimLong: {
4098 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004099 if (index.IsConstant()) {
4100 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004101 if (value.IsRegister()) {
4102 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4103 } else {
4104 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4105 DCHECK(IsInt<32>(v));
4106 int32_t v_32 = v;
4107 __ movq(Address(obj, offset), Immediate(v_32));
4108 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004109 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004110 if (value.IsRegister()) {
4111 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4112 value.AsRegister<CpuRegister>());
4113 } else {
4114 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4115 DCHECK(IsInt<32>(v));
4116 int32_t v_32 = v;
4117 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4118 Immediate(v_32));
4119 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004120 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004121 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004122 break;
4123 }
4124
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004125 case Primitive::kPrimFloat: {
4126 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4127 if (index.IsConstant()) {
4128 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4129 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004130 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004131 } else {
4132 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004133 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4134 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004135 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004136 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004137 break;
4138 }
4139
4140 case Primitive::kPrimDouble: {
4141 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4142 if (index.IsConstant()) {
4143 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4144 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004145 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004146 } else {
4147 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004148 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4149 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004150 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004151 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004152 break;
4153 }
4154
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004155 case Primitive::kPrimVoid:
4156 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004157 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004158 }
4159}
4160
4161void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004162 LocationSummary* locations =
4163 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004164 locations->SetInAt(0, Location::RequiresRegister());
4165 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004166}
4167
4168void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4169 LocationSummary* locations = instruction->GetLocations();
4170 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004171 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4172 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004173 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004174 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004175}
4176
4177void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004178 LocationSummary* locations =
4179 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004180 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004181 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004182 if (instruction->HasUses()) {
4183 locations->SetOut(Location::SameAsFirstInput());
4184 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004185}
4186
4187void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4188 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004189 Location index_loc = locations->InAt(0);
4190 Location length_loc = locations->InAt(1);
4191 SlowPathCodeX86_64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004192 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004193
Mark Mendell99dbd682015-04-22 16:18:52 -04004194 if (length_loc.IsConstant()) {
4195 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4196 if (index_loc.IsConstant()) {
4197 // BCE will remove the bounds check if we are guarenteed to pass.
4198 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4199 if (index < 0 || index >= length) {
4200 codegen_->AddSlowPath(slow_path);
4201 __ jmp(slow_path->GetEntryLabel());
4202 } else {
4203 // Some optimization after BCE may have generated this, and we should not
4204 // generate a bounds check if it is a valid range.
4205 }
4206 return;
4207 }
4208
4209 // We have to reverse the jump condition because the length is the constant.
4210 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4211 __ cmpl(index_reg, Immediate(length));
4212 codegen_->AddSlowPath(slow_path);
4213 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004214 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004215 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4216 if (index_loc.IsConstant()) {
4217 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4218 __ cmpl(length, Immediate(value));
4219 } else {
4220 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4221 }
4222 codegen_->AddSlowPath(slow_path);
4223 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004224 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004225}
4226
4227void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4228 CpuRegister card,
4229 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004230 CpuRegister value,
4231 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004233 if (value_can_be_null) {
4234 __ testl(value, value);
4235 __ j(kEqual, &is_null);
4236 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004237 __ gs()->movq(card, Address::Absolute(
4238 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4239 __ movq(temp, object);
4240 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004241 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004242 if (value_can_be_null) {
4243 __ Bind(&is_null);
4244 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004245}
4246
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004247void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4248 temp->SetLocations(nullptr);
4249}
4250
4251void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4252 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004253 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004254}
4255
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004256void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004257 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004258 LOG(FATAL) << "Unimplemented";
4259}
4260
4261void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004262 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4263}
4264
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004265void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4266 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4267}
4268
4269void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004270 HBasicBlock* block = instruction->GetBlock();
4271 if (block->GetLoopInformation() != nullptr) {
4272 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4273 // The back edge will generate the suspend check.
4274 return;
4275 }
4276 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4277 // The goto will generate the suspend check.
4278 return;
4279 }
4280 GenerateSuspendCheck(instruction, nullptr);
4281}
4282
4283void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4284 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004285 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004286 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4287 if (slow_path == nullptr) {
4288 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4289 instruction->SetSlowPath(slow_path);
4290 codegen_->AddSlowPath(slow_path);
4291 if (successor != nullptr) {
4292 DCHECK(successor->IsLoopHeader());
4293 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4294 }
4295 } else {
4296 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4297 }
4298
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004299 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004300 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004301 if (successor == nullptr) {
4302 __ j(kNotEqual, slow_path->GetEntryLabel());
4303 __ Bind(slow_path->GetReturnLabel());
4304 } else {
4305 __ j(kEqual, codegen_->GetLabelOf(successor));
4306 __ jmp(slow_path->GetEntryLabel());
4307 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004308}
4309
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004310X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4311 return codegen_->GetAssembler();
4312}
4313
4314void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4315 MoveOperands* move = moves_.Get(index);
4316 Location source = move->GetSource();
4317 Location destination = move->GetDestination();
4318
4319 if (source.IsRegister()) {
4320 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004321 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004322 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004323 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004324 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004325 } else {
4326 DCHECK(destination.IsDoubleStackSlot());
4327 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004328 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004329 }
4330 } else if (source.IsStackSlot()) {
4331 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004332 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004333 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004334 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004335 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004336 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004337 } else {
4338 DCHECK(destination.IsStackSlot());
4339 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4340 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4341 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004342 } else if (source.IsDoubleStackSlot()) {
4343 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004344 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004345 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004346 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004347 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4348 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004349 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004350 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004351 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4352 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4353 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004354 } else if (source.IsConstant()) {
4355 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004356 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4357 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004358 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004359 if (value == 0) {
4360 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4361 } else {
4362 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4363 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004364 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004365 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004366 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004367 }
4368 } else if (constant->IsLongConstant()) {
4369 int64_t value = constant->AsLongConstant()->GetValue();
4370 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004371 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004372 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004373 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004374 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004375 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004376 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004377 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004378 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004379 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004380 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4381 if (value == 0) {
4382 // easy FP 0.0.
4383 __ xorps(dest, dest);
4384 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004385 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004386 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004387 } else {
4388 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004389 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004390 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4391 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004392 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004393 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004394 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004395 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004396 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004397 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4398 if (value == 0) {
4399 __ xorpd(dest, dest);
4400 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004401 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004402 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004403 } else {
4404 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004405 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004406 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004407 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004408 } else if (source.IsFpuRegister()) {
4409 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004410 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004411 } else if (destination.IsStackSlot()) {
4412 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004413 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004414 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004415 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004416 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004417 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004418 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004419 }
4420}
4421
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004422void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004423 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004424 __ movl(Address(CpuRegister(RSP), mem), reg);
4425 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004426}
4427
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004428void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004429 ScratchRegisterScope ensure_scratch(
4430 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4431
4432 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4433 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4434 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4435 Address(CpuRegister(RSP), mem2 + stack_offset));
4436 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4437 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4438 CpuRegister(ensure_scratch.GetRegister()));
4439}
4440
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004441void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4442 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4443 __ movq(Address(CpuRegister(RSP), mem), reg);
4444 __ movq(reg, CpuRegister(TMP));
4445}
4446
4447void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4448 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004449 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004450
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004451 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4452 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4453 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4454 Address(CpuRegister(RSP), mem2 + stack_offset));
4455 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4456 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4457 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004458}
4459
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004460void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4461 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4462 __ movss(Address(CpuRegister(RSP), mem), reg);
4463 __ movd(reg, CpuRegister(TMP));
4464}
4465
4466void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4467 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4468 __ movsd(Address(CpuRegister(RSP), mem), reg);
4469 __ movd(reg, CpuRegister(TMP));
4470}
4471
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004472void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4473 MoveOperands* move = moves_.Get(index);
4474 Location source = move->GetSource();
4475 Location destination = move->GetDestination();
4476
4477 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004478 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004479 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004480 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004481 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004482 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004483 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004484 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4485 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004486 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004487 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004488 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004489 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4490 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004491 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004492 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4493 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4494 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004495 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004496 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004497 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004498 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004499 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004500 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004501 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004502 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004503 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004504 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004505 }
4506}
4507
4508
4509void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4510 __ pushq(CpuRegister(reg));
4511}
4512
4513
4514void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4515 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004516}
4517
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004518void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4519 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4520 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4521 Immediate(mirror::Class::kStatusInitialized));
4522 __ j(kLess, slow_path->GetEntryLabel());
4523 __ Bind(slow_path->GetExitLabel());
4524 // No need for memory fence, thanks to the X86_64 memory model.
4525}
4526
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004527void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004528 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4529 ? LocationSummary::kCallOnSlowPath
4530 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004531 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004532 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004533 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004534 locations->SetOut(Location::RequiresRegister());
4535}
4536
4537void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004538 LocationSummary* locations = cls->GetLocations();
4539 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4540 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004541 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004542 DCHECK(!cls->CanCallRuntime());
4543 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004544 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004545 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004546 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004547 __ movq(out, Address(
4548 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004549 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004550 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004551
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004552 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4553 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4554 codegen_->AddSlowPath(slow_path);
4555 __ testl(out, out);
4556 __ j(kEqual, slow_path->GetEntryLabel());
4557 if (cls->MustGenerateClinitCheck()) {
4558 GenerateClassInitializationCheck(slow_path, out);
4559 } else {
4560 __ Bind(slow_path->GetExitLabel());
4561 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004562 }
4563}
4564
4565void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4566 LocationSummary* locations =
4567 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4568 locations->SetInAt(0, Location::RequiresRegister());
4569 if (check->HasUses()) {
4570 locations->SetOut(Location::SameAsFirstInput());
4571 }
4572}
4573
4574void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004575 // We assume the class to not be null.
4576 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4577 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004578 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004579 GenerateClassInitializationCheck(slow_path,
4580 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004581}
4582
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004583void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4584 LocationSummary* locations =
4585 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004586 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004587 locations->SetOut(Location::RequiresRegister());
4588}
4589
4590void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4591 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4592 codegen_->AddSlowPath(slow_path);
4593
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004594 LocationSummary* locations = load->GetLocations();
4595 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4596 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004597 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004598 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004599 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004600 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004601 __ testl(out, out);
4602 __ j(kEqual, slow_path->GetEntryLabel());
4603 __ Bind(slow_path->GetExitLabel());
4604}
4605
David Brazdilcb1c0552015-08-04 16:22:25 +01004606static Address GetExceptionTlsAddress() {
4607 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4608}
4609
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004610void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4611 LocationSummary* locations =
4612 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4613 locations->SetOut(Location::RequiresRegister());
4614}
4615
4616void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004617 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4618}
4619
4620void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4621 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4622}
4623
4624void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4625 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004626}
4627
4628void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4629 LocationSummary* locations =
4630 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4631 InvokeRuntimeCallingConvention calling_convention;
4632 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4633}
4634
4635void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004636 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4637 instruction,
4638 instruction->GetDexPc(),
4639 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004640}
4641
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004642void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004643 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4644 ? LocationSummary::kNoCall
4645 : LocationSummary::kCallOnSlowPath;
4646 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4647 locations->SetInAt(0, Location::RequiresRegister());
4648 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004649 // Note that TypeCheckSlowPathX86_64 uses this register too.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004650 locations->SetOut(Location::RequiresRegister());
4651}
4652
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004653void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004654 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004655 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004656 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004657 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004658 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4659 Label done, zero;
4660 SlowPathCodeX86_64* slow_path = nullptr;
4661
4662 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004663 // Avoid null check if we know obj is not null.
4664 if (instruction->MustDoNullCheck()) {
4665 __ testl(obj, obj);
4666 __ j(kEqual, &zero);
4667 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004668 // Compare the class of `obj` with `cls`.
4669 __ movl(out, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004670 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004671 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004672 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004673 } else {
4674 DCHECK(cls.IsStackSlot()) << cls;
4675 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4676 }
4677 if (instruction->IsClassFinal()) {
4678 // Classes must be equal for the instanceof to succeed.
4679 __ j(kNotEqual, &zero);
4680 __ movl(out, Immediate(1));
4681 __ jmp(&done);
4682 } else {
4683 // If the classes are not equal, we go into a slow path.
4684 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004685 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004686 codegen_->AddSlowPath(slow_path);
4687 __ j(kNotEqual, slow_path->GetEntryLabel());
4688 __ movl(out, Immediate(1));
4689 __ jmp(&done);
4690 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004691
4692 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4693 __ Bind(&zero);
4694 __ movl(out, Immediate(0));
4695 }
4696
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004697 if (slow_path != nullptr) {
4698 __ Bind(slow_path->GetExitLabel());
4699 }
4700 __ Bind(&done);
4701}
4702
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004703void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4705 instruction, LocationSummary::kCallOnSlowPath);
4706 locations->SetInAt(0, Location::RequiresRegister());
4707 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004708 // Note that TypeCheckSlowPathX86_64 uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004709 locations->AddTemp(Location::RequiresRegister());
4710}
4711
4712void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4713 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004714 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004715 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004716 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004717 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004718 SlowPathCodeX86_64* slow_path =
4719 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004720 codegen_->AddSlowPath(slow_path);
4721
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004722 // Avoid null check if we know obj is not null.
4723 if (instruction->MustDoNullCheck()) {
4724 __ testl(obj, obj);
4725 __ j(kEqual, slow_path->GetExitLabel());
4726 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004727 // Compare the class of `obj` with `cls`.
4728 __ movl(temp, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004729 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004730 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004731 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004732 } else {
4733 DCHECK(cls.IsStackSlot()) << cls;
4734 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4735 }
Roland Levillain4d027112015-07-01 15:41:14 +01004736 // The checkcast succeeds if the classes are equal (fast path).
4737 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004738 __ j(kNotEqual, slow_path->GetEntryLabel());
4739 __ Bind(slow_path->GetExitLabel());
4740}
4741
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004742void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4743 LocationSummary* locations =
4744 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4745 InvokeRuntimeCallingConvention calling_convention;
4746 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4747}
4748
4749void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004750 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4751 : QUICK_ENTRY_POINT(pUnlockObject),
4752 instruction,
4753 instruction->GetDexPc(),
4754 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004755}
4756
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004757void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4758void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4759void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4760
4761void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4762 LocationSummary* locations =
4763 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4764 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4765 || instruction->GetResultType() == Primitive::kPrimLong);
4766 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004767 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004768 locations->SetOut(Location::SameAsFirstInput());
4769}
4770
4771void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4772 HandleBitwiseOperation(instruction);
4773}
4774
4775void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4776 HandleBitwiseOperation(instruction);
4777}
4778
4779void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4780 HandleBitwiseOperation(instruction);
4781}
4782
4783void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4784 LocationSummary* locations = instruction->GetLocations();
4785 Location first = locations->InAt(0);
4786 Location second = locations->InAt(1);
4787 DCHECK(first.Equals(locations->Out()));
4788
4789 if (instruction->GetResultType() == Primitive::kPrimInt) {
4790 if (second.IsRegister()) {
4791 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004792 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004793 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004794 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004795 } else {
4796 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004797 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004798 }
4799 } else if (second.IsConstant()) {
4800 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4801 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004802 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004803 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004804 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004805 } else {
4806 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004807 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004808 }
4809 } else {
4810 Address address(CpuRegister(RSP), second.GetStackIndex());
4811 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004812 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004813 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004814 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004815 } else {
4816 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004817 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004818 }
4819 }
4820 } else {
4821 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004822 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4823 bool second_is_constant = false;
4824 int64_t value = 0;
4825 if (second.IsConstant()) {
4826 second_is_constant = true;
4827 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004828 }
Mark Mendell40741f32015-04-20 22:10:34 -04004829 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004830
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004831 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004832 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004833 if (is_int32_value) {
4834 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4835 } else {
4836 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4837 }
4838 } else if (second.IsDoubleStackSlot()) {
4839 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004840 } else {
4841 __ andq(first_reg, second.AsRegister<CpuRegister>());
4842 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004843 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004844 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004845 if (is_int32_value) {
4846 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4847 } else {
4848 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4849 }
4850 } else if (second.IsDoubleStackSlot()) {
4851 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004852 } else {
4853 __ orq(first_reg, second.AsRegister<CpuRegister>());
4854 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004855 } else {
4856 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004857 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004858 if (is_int32_value) {
4859 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4860 } else {
4861 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4862 }
4863 } else if (second.IsDoubleStackSlot()) {
4864 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004865 } else {
4866 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4867 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004868 }
4869 }
4870}
4871
Calin Juravleb1498f62015-02-16 13:13:29 +00004872void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4873 // Nothing to do, this should be removed during prepare for register allocator.
4874 UNUSED(instruction);
4875 LOG(FATAL) << "Unreachable";
4876}
4877
4878void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4879 // Nothing to do, this should be removed during prepare for register allocator.
4880 UNUSED(instruction);
4881 LOG(FATAL) << "Unreachable";
4882}
4883
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004884void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
4885 DCHECK(codegen_->IsBaseline());
4886 LocationSummary* locations =
4887 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4888 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4889}
4890
4891void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4892 DCHECK(codegen_->IsBaseline());
4893 // Will be generated at use site.
4894}
4895
Mark Mendell92e83bf2015-05-07 11:25:03 -04004896void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4897 if (value == 0) {
4898 __ xorl(dest, dest);
4899 } else if (value > 0 && IsInt<32>(value)) {
4900 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4901 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4902 } else {
4903 __ movq(dest, Immediate(value));
4904 }
4905}
4906
Mark Mendellcfa410b2015-05-25 16:02:44 -04004907void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
4908 DCHECK(dest.IsDoubleStackSlot());
4909 if (IsInt<32>(value)) {
4910 // Can move directly as an int32 constant.
4911 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
4912 Immediate(static_cast<int32_t>(value)));
4913 } else {
4914 Load64BitValue(CpuRegister(TMP), value);
4915 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
4916 }
4917}
4918
Mark Mendellf55c3e02015-03-26 21:07:46 -04004919void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4920 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004921 X86_64Assembler* assembler = GetAssembler();
4922 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004923 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4924 // byte values. If used for vectors at a later time, this will need to be
4925 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004926 assembler->Align(4, 0);
4927 constant_area_start_ = assembler->CodeSize();
4928 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004929 }
4930
4931 // And finish up.
4932 CodeGenerator::Finalize(allocator);
4933}
4934
4935/**
4936 * Class to handle late fixup of offsets into constant area.
4937 */
4938class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4939 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004940 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004941 : codegen_(codegen), offset_into_constant_area_(offset) {}
4942
4943 private:
4944 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4945 // Patch the correct offset for the instruction. We use the address of the
4946 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4947 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4948 int relative_position = constant_offset - pos;
4949
4950 // Patch in the right value.
4951 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4952 }
4953
Mark Mendell39dcf552015-04-09 20:42:42 -04004954 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004955
4956 // Location in constant area that the fixup refers to.
4957 int offset_into_constant_area_;
4958};
4959
4960Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4961 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4962 return Address::RIP(fixup);
4963}
4964
4965Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4966 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4967 return Address::RIP(fixup);
4968}
4969
4970Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4971 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4972 return Address::RIP(fixup);
4973}
4974
4975Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4976 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4977 return Address::RIP(fixup);
4978}
4979
Roland Levillain4d027112015-07-01 15:41:14 +01004980#undef __
4981
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004982} // namespace x86_64
4983} // namespace art