blob: ff52f4f9250494cd4b34acc3ac80fc396124055f [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_;
442 __ movl(reg, Address(CpuRegister(method_reg),
443 ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
444 // temp = temp[index_in_cache]
445 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
446 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
447 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100448 }
Vladimir Marko58155012015-08-19 12:49:41 +0000449 }
450
451 switch (invoke->GetCodePtrLocation()) {
452 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
453 __ call(&frame_entry_label_);
454 break;
455 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
456 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
457 Label* label = &relative_call_patches_.back().label;
458 __ call(label); // Bind to the patch label, override at link time.
459 __ Bind(label); // Bind the label at the end of the "call" insn.
460 break;
461 }
462 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
463 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
464 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
465 FALLTHROUGH_INTENDED;
466 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
467 // (callee_method + offset_of_quick_compiled_code)()
468 __ call(Address(callee_method.AsRegister<CpuRegister>(),
469 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
470 kX86_64WordSize).SizeValue()));
471 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000472 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800473
474 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800475}
476
Vladimir Marko58155012015-08-19 12:49:41 +0000477void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
478 DCHECK(linker_patches->empty());
479 size_t size =
480 method_patches_.size() + relative_call_patches_.size() + pc_rel_dex_cache_patches_.size();
481 linker_patches->reserve(size);
482 for (const MethodPatchInfo<Label>& info : method_patches_) {
483 // The label points to the end of the "movl" instruction but the literal offset for method
484 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
485 uint32_t literal_offset = info.label.Position() - 4;
486 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
487 info.target_method.dex_file,
488 info.target_method.dex_method_index));
489 }
490 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
491 // The label points to the end of the "call" instruction but the literal offset for method
492 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
493 uint32_t literal_offset = info.label.Position() - 4;
494 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
495 info.target_method.dex_file,
496 info.target_method.dex_method_index));
497 }
498 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
499 // The label points to the end of the "mov" instruction but the literal offset for method
500 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
501 uint32_t literal_offset = info.label.Position() - 4;
502 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
503 &info.target_dex_file,
504 info.label.Position(),
505 info.element_offset));
506 }
507}
508
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100509void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100510 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100511}
512
513void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100514 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100515}
516
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100517size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
518 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
519 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100520}
521
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100522size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
523 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
524 return kX86_64WordSize;
525}
526
527size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
528 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
529 return kX86_64WordSize;
530}
531
532size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
533 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
534 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100535}
536
Alexandre Rames8158f282015-08-07 10:26:17 +0100537void CodeGeneratorX86_64::InvokeRuntime(Address entry_point,
538 HInstruction* instruction,
539 uint32_t dex_pc,
540 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100541 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100542 __ gs()->call(entry_point);
543 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100544}
545
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000546static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000547// Use a fake return address register to mimic Quick.
548static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400549CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
550 const X86_64InstructionSetFeatures& isa_features,
551 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000552 : CodeGenerator(graph,
553 kNumberOfCpuRegisters,
554 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000555 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000556 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
557 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000558 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000559 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
560 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000561 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100562 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100563 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000564 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400565 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400566 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000567 constant_area_start_(0),
568 method_patches_(graph->GetArena()->Adapter()),
569 relative_call_patches_(graph->GetArena()->Adapter()),
570 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000571 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
572}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100573
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100574InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
575 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100576 : HGraphVisitor(graph),
577 assembler_(codegen->GetAssembler()),
578 codegen_(codegen) {}
579
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100580Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100581 switch (type) {
582 case Primitive::kPrimLong:
583 case Primitive::kPrimByte:
584 case Primitive::kPrimBoolean:
585 case Primitive::kPrimChar:
586 case Primitive::kPrimShort:
587 case Primitive::kPrimInt:
588 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100589 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100590 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591 }
592
593 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100594 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100595 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100596 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100597 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100598
599 case Primitive::kPrimVoid:
600 LOG(FATAL) << "Unreachable type " << type;
601 }
602
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100603 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100604}
605
Nicolas Geoffray98893962015-01-21 12:32:32 +0000606void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100607 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100608 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100609
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000610 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100611 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000612
Nicolas Geoffray98893962015-01-21 12:32:32 +0000613 if (is_baseline) {
614 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
615 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
616 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000617 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
618 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
619 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000620 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100621}
622
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100623static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100624 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100625}
David Srbecky9d8606d2015-04-12 09:35:32 +0100626
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100627static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100628 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100629}
630
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100631void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100632 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000633 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100634 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700635 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000636 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100637
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000638 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100639 __ testq(CpuRegister(RAX), Address(
640 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100641 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100642 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000643
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000644 if (HasEmptyFrame()) {
645 return;
646 }
647
Nicolas Geoffray98893962015-01-21 12:32:32 +0000648 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000649 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000650 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000651 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100652 __ cfi().AdjustCFAOffset(kX86_64WordSize);
653 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000654 }
655 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100656
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100657 int adjust = GetFrameSize() - GetCoreSpillSize();
658 __ subq(CpuRegister(RSP), Immediate(adjust));
659 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000660 uint32_t xmm_spill_location = GetFpuSpillStart();
661 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100662
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000663 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
664 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100665 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
666 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
667 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000668 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100669 }
670
Mathieu Chartiere401d142015-04-22 13:56:20 -0700671 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100672 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100673}
674
675void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100676 __ cfi().RememberState();
677 if (!HasEmptyFrame()) {
678 uint32_t xmm_spill_location = GetFpuSpillStart();
679 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
680 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
681 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
682 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
683 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
684 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
685 }
686 }
687
688 int adjust = GetFrameSize() - GetCoreSpillSize();
689 __ addq(CpuRegister(RSP), Immediate(adjust));
690 __ cfi().AdjustCFAOffset(-adjust);
691
692 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
693 Register reg = kCoreCalleeSaves[i];
694 if (allocated_registers_.ContainsCoreRegister(reg)) {
695 __ popq(CpuRegister(reg));
696 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
697 __ cfi().Restore(DWARFReg(reg));
698 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000699 }
700 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100701 __ ret();
702 __ cfi().RestoreState();
703 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704}
705
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100706void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
707 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100708}
709
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100710Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
711 switch (load->GetType()) {
712 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100713 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100714 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100715
716 case Primitive::kPrimInt:
717 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100718 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100719 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100720
721 case Primitive::kPrimBoolean:
722 case Primitive::kPrimByte:
723 case Primitive::kPrimChar:
724 case Primitive::kPrimShort:
725 case Primitive::kPrimVoid:
726 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700727 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100728 }
729
730 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700731 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100732}
733
734void CodeGeneratorX86_64::Move(Location destination, Location source) {
735 if (source.Equals(destination)) {
736 return;
737 }
738 if (destination.IsRegister()) {
739 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000740 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100741 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000742 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100743 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000744 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100745 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100746 } else {
747 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000748 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100749 Address(CpuRegister(RSP), source.GetStackIndex()));
750 }
751 } else if (destination.IsFpuRegister()) {
752 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000753 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100754 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000755 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100756 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000757 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100758 Address(CpuRegister(RSP), source.GetStackIndex()));
759 } else {
760 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000761 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100762 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100763 }
764 } else if (destination.IsStackSlot()) {
765 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100766 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000767 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 } else if (source.IsFpuRegister()) {
769 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000770 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500771 } else if (source.IsConstant()) {
772 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000773 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500774 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100775 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500776 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000777 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
778 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100779 }
780 } else {
781 DCHECK(destination.IsDoubleStackSlot());
782 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100783 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000784 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100785 } else if (source.IsFpuRegister()) {
786 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000787 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500788 } else if (source.IsConstant()) {
789 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800790 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500791 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000792 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500793 } else {
794 DCHECK(constant->IsLongConstant());
795 value = constant->AsLongConstant()->GetValue();
796 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400797 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100798 } else {
799 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000800 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
801 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100802 }
803 }
804}
805
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100806void CodeGeneratorX86_64::Move(HInstruction* instruction,
807 Location location,
808 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000809 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100810 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700811 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100812 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000813 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100814 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000815 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000816 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
817 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000819 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000820 } else if (location.IsStackSlot()) {
821 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
822 } else {
823 DCHECK(location.IsConstant());
824 DCHECK_EQ(location.GetConstant(), const_to_move);
825 }
826 } else if (const_to_move->IsLongConstant()) {
827 int64_t value = const_to_move->AsLongConstant()->GetValue();
828 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400829 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000830 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400831 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000832 } else {
833 DCHECK(location.IsConstant());
834 DCHECK_EQ(location.GetConstant(), const_to_move);
835 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100836 }
Roland Levillain476df552014-10-09 17:51:36 +0100837 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100838 switch (instruction->GetType()) {
839 case Primitive::kPrimBoolean:
840 case Primitive::kPrimByte:
841 case Primitive::kPrimChar:
842 case Primitive::kPrimShort:
843 case Primitive::kPrimInt:
844 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100845 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
847 break;
848
849 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100850 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000851 Move(location,
852 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100853 break;
854
855 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100856 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100857 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000858 } else if (instruction->IsTemporary()) {
859 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
860 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100861 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100862 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100863 switch (instruction->GetType()) {
864 case Primitive::kPrimBoolean:
865 case Primitive::kPrimByte:
866 case Primitive::kPrimChar:
867 case Primitive::kPrimShort:
868 case Primitive::kPrimInt:
869 case Primitive::kPrimNot:
870 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100871 case Primitive::kPrimFloat:
872 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000873 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100874 break;
875
876 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100877 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100878 }
879 }
880}
881
David Brazdilfc6a86a2015-06-26 10:33:45 +0000882void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100883 DCHECK(!successor->IsExitBlock());
884
885 HBasicBlock* block = got->GetBlock();
886 HInstruction* previous = got->GetPrevious();
887
888 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000889 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100890 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
891 return;
892 }
893
894 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
895 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
896 }
897 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100898 __ jmp(codegen_->GetLabelOf(successor));
899 }
900}
901
David Brazdilfc6a86a2015-06-26 10:33:45 +0000902void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
903 got->SetLocations(nullptr);
904}
905
906void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
907 HandleGoto(got, got->GetSuccessor());
908}
909
910void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
911 try_boundary->SetLocations(nullptr);
912}
913
914void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
915 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
916 if (!successor->IsExitBlock()) {
917 HandleGoto(try_boundary, successor);
918 }
919}
920
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100921void LocationsBuilderX86_64::VisitExit(HExit* exit) {
922 exit->SetLocations(nullptr);
923}
924
925void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700926 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927}
928
Mark Mendellc4701932015-04-10 13:18:51 -0400929void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
930 Label* true_label,
931 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100932 if (cond->IsFPConditionTrueIfNaN()) {
933 __ j(kUnordered, true_label);
934 } else if (cond->IsFPConditionFalseIfNaN()) {
935 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400936 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100937 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400938}
939
940void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
941 HCondition* condition,
942 Label* true_target,
943 Label* false_target,
944 Label* always_true_target) {
945 LocationSummary* locations = condition->GetLocations();
946 Location left = locations->InAt(0);
947 Location right = locations->InAt(1);
948
949 // We don't want true_target as a nullptr.
950 if (true_target == nullptr) {
951 true_target = always_true_target;
952 }
953 bool falls_through = (false_target == nullptr);
954
955 // FP compares don't like null false_targets.
956 if (false_target == nullptr) {
957 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
958 }
959
960 Primitive::Type type = condition->InputAt(0)->GetType();
961 switch (type) {
962 case Primitive::kPrimLong: {
963 CpuRegister left_reg = left.AsRegister<CpuRegister>();
964 if (right.IsConstant()) {
965 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
966 if (IsInt<32>(value)) {
967 if (value == 0) {
968 __ testq(left_reg, left_reg);
969 } else {
970 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
971 }
972 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100973 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -0400974 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
975 }
976 } else if (right.IsDoubleStackSlot()) {
977 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
978 } else {
979 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
980 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100981 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -0400982 break;
983 }
984 case Primitive::kPrimFloat: {
985 if (right.IsFpuRegister()) {
986 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
987 } else if (right.IsConstant()) {
988 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
989 codegen_->LiteralFloatAddress(
990 right.GetConstant()->AsFloatConstant()->GetValue()));
991 } else {
992 DCHECK(right.IsStackSlot());
993 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
994 Address(CpuRegister(RSP), right.GetStackIndex()));
995 }
996 GenerateFPJumps(condition, true_target, false_target);
997 break;
998 }
999 case Primitive::kPrimDouble: {
1000 if (right.IsFpuRegister()) {
1001 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1002 } else if (right.IsConstant()) {
1003 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1004 codegen_->LiteralDoubleAddress(
1005 right.GetConstant()->AsDoubleConstant()->GetValue()));
1006 } else {
1007 DCHECK(right.IsDoubleStackSlot());
1008 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1009 Address(CpuRegister(RSP), right.GetStackIndex()));
1010 }
1011 GenerateFPJumps(condition, true_target, false_target);
1012 break;
1013 }
1014 default:
1015 LOG(FATAL) << "Unexpected condition type " << type;
1016 }
1017
1018 if (!falls_through) {
1019 __ jmp(false_target);
1020 }
1021}
1022
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001023void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1024 Label* true_target,
1025 Label* false_target,
1026 Label* always_true_target) {
1027 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001028 if (cond->IsIntConstant()) {
1029 // Constant condition, statically compared against 1.
1030 int32_t cond_value = cond->AsIntConstant()->GetValue();
1031 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001032 if (always_true_target != nullptr) {
1033 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001034 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001035 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001036 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001037 DCHECK_EQ(cond_value, 0);
1038 }
1039 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001040 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001041 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1042 // Moves do not affect the eflags register, so if the condition is
1043 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001044 // again. We can't use the eflags on FP conditions if they are
1045 // materialized due to the complex branching.
1046 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001047 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001048 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1049 && !Primitive::IsFloatingPointType(type);
1050
Roland Levillain4fa13f62015-07-06 18:11:54 +01001051 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001052 if (!eflags_set) {
1053 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001054 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001055 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001056 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001057 } else {
1058 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1059 Immediate(0));
1060 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001061 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001062 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001063 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001064 }
1065 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001066 // Condition has not been materialized, use its inputs as the
1067 // comparison and its condition as the branch condition.
1068
Mark Mendellc4701932015-04-10 13:18:51 -04001069 // Is this a long or FP comparison that has been folded into the HCondition?
1070 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001071 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001072 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1073 true_target, false_target, always_true_target);
1074 return;
1075 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001076
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001077 Location lhs = cond->GetLocations()->InAt(0);
1078 Location rhs = cond->GetLocations()->InAt(1);
1079 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001080 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001081 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001082 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001083 if (constant == 0) {
1084 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1085 } else {
1086 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1087 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001088 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001089 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001090 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1091 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001092 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001093 }
Dave Allison20dfc792014-06-16 20:44:29 -07001094 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001095 if (false_target != nullptr) {
1096 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001097 }
1098}
1099
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001100void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1101 LocationSummary* locations =
1102 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1103 HInstruction* cond = if_instr->InputAt(0);
1104 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1105 locations->SetInAt(0, Location::Any());
1106 }
1107}
1108
1109void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1110 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1111 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1112 Label* always_true_target = true_target;
1113 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1114 if_instr->IfTrueSuccessor())) {
1115 always_true_target = nullptr;
1116 }
1117 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1118 if_instr->IfFalseSuccessor())) {
1119 false_target = nullptr;
1120 }
1121 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1122}
1123
1124void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1125 LocationSummary* locations = new (GetGraph()->GetArena())
1126 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1127 HInstruction* cond = deoptimize->InputAt(0);
1128 DCHECK(cond->IsCondition());
1129 if (cond->AsCondition()->NeedsMaterialization()) {
1130 locations->SetInAt(0, Location::Any());
1131 }
1132}
1133
1134void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1135 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
1136 DeoptimizationSlowPathX86_64(deoptimize);
1137 codegen_->AddSlowPath(slow_path);
1138 Label* slow_path_entry = slow_path->GetEntryLabel();
1139 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1140}
1141
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001142void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1143 local->SetLocations(nullptr);
1144}
1145
1146void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1147 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1148}
1149
1150void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1151 local->SetLocations(nullptr);
1152}
1153
1154void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1155 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001156 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001157}
1158
1159void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001160 LocationSummary* locations =
1161 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001162 switch (store->InputAt(1)->GetType()) {
1163 case Primitive::kPrimBoolean:
1164 case Primitive::kPrimByte:
1165 case Primitive::kPrimChar:
1166 case Primitive::kPrimShort:
1167 case Primitive::kPrimInt:
1168 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001169 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001170 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1171 break;
1172
1173 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001174 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001175 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1176 break;
1177
1178 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001179 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001180 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001181}
1182
1183void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001184 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001185}
1186
Roland Levillain0d37cd02015-05-27 16:39:19 +01001187void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001188 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001189 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001190 // Handle the long/FP comparisons made in instruction simplification.
1191 switch (cond->InputAt(0)->GetType()) {
1192 case Primitive::kPrimLong:
1193 locations->SetInAt(0, Location::RequiresRegister());
1194 locations->SetInAt(1, Location::Any());
1195 break;
1196 case Primitive::kPrimFloat:
1197 case Primitive::kPrimDouble:
1198 locations->SetInAt(0, Location::RequiresFpuRegister());
1199 locations->SetInAt(1, Location::Any());
1200 break;
1201 default:
1202 locations->SetInAt(0, Location::RequiresRegister());
1203 locations->SetInAt(1, Location::Any());
1204 break;
1205 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001206 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001207 locations->SetOut(Location::RequiresRegister());
1208 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001209}
1210
Roland Levillain0d37cd02015-05-27 16:39:19 +01001211void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001212 if (!cond->NeedsMaterialization()) {
1213 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001214 }
Mark Mendellc4701932015-04-10 13:18:51 -04001215
1216 LocationSummary* locations = cond->GetLocations();
1217 Location lhs = locations->InAt(0);
1218 Location rhs = locations->InAt(1);
1219 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1220 Label true_label, false_label;
1221
1222 switch (cond->InputAt(0)->GetType()) {
1223 default:
1224 // Integer case.
1225
1226 // Clear output register: setcc only sets the low byte.
1227 __ xorl(reg, reg);
1228
1229 if (rhs.IsRegister()) {
1230 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1231 } else if (rhs.IsConstant()) {
1232 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1233 if (constant == 0) {
1234 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1235 } else {
1236 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1237 }
1238 } else {
1239 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1240 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001241 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001242 return;
1243 case Primitive::kPrimLong:
1244 // Clear output register: setcc only sets the low byte.
1245 __ xorl(reg, reg);
1246
1247 if (rhs.IsRegister()) {
1248 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1249 } else if (rhs.IsConstant()) {
1250 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1251 if (IsInt<32>(value)) {
1252 if (value == 0) {
1253 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1254 } else {
1255 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1256 }
1257 } else {
1258 // Value won't fit in an int.
1259 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1260 }
1261 } else {
1262 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1263 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001264 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001265 return;
1266 case Primitive::kPrimFloat: {
1267 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1268 if (rhs.IsConstant()) {
1269 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1270 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1271 } else if (rhs.IsStackSlot()) {
1272 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1273 } else {
1274 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1275 }
1276 GenerateFPJumps(cond, &true_label, &false_label);
1277 break;
1278 }
1279 case Primitive::kPrimDouble: {
1280 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1281 if (rhs.IsConstant()) {
1282 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1283 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1284 } else if (rhs.IsDoubleStackSlot()) {
1285 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1286 } else {
1287 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1288 }
1289 GenerateFPJumps(cond, &true_label, &false_label);
1290 break;
1291 }
1292 }
1293
1294 // Convert the jumps into the result.
1295 Label done_label;
1296
Roland Levillain4fa13f62015-07-06 18:11:54 +01001297 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001298 __ Bind(&false_label);
1299 __ xorl(reg, reg);
1300 __ jmp(&done_label);
1301
Roland Levillain4fa13f62015-07-06 18:11:54 +01001302 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001303 __ Bind(&true_label);
1304 __ movl(reg, Immediate(1));
1305 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001306}
1307
1308void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1309 VisitCondition(comp);
1310}
1311
1312void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1313 VisitCondition(comp);
1314}
1315
1316void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1317 VisitCondition(comp);
1318}
1319
1320void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1321 VisitCondition(comp);
1322}
1323
1324void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1325 VisitCondition(comp);
1326}
1327
1328void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1329 VisitCondition(comp);
1330}
1331
1332void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1333 VisitCondition(comp);
1334}
1335
1336void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1337 VisitCondition(comp);
1338}
1339
1340void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1341 VisitCondition(comp);
1342}
1343
1344void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1345 VisitCondition(comp);
1346}
1347
1348void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1349 VisitCondition(comp);
1350}
1351
1352void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1353 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001354}
1355
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001356void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001357 LocationSummary* locations =
1358 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001359 switch (compare->InputAt(0)->GetType()) {
1360 case Primitive::kPrimLong: {
1361 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001362 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1364 break;
1365 }
1366 case Primitive::kPrimFloat:
1367 case Primitive::kPrimDouble: {
1368 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001369 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001370 locations->SetOut(Location::RequiresRegister());
1371 break;
1372 }
1373 default:
1374 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1375 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001376}
1377
1378void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001379 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001380 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001381 Location left = locations->InAt(0);
1382 Location right = locations->InAt(1);
1383
1384 Label less, greater, done;
1385 Primitive::Type type = compare->InputAt(0)->GetType();
1386 switch (type) {
1387 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001388 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1389 if (right.IsConstant()) {
1390 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001391 if (IsInt<32>(value)) {
1392 if (value == 0) {
1393 __ testq(left_reg, left_reg);
1394 } else {
1395 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1396 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001397 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001398 // Value won't fit in an int.
1399 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001400 }
Mark Mendell40741f32015-04-20 22:10:34 -04001401 } else if (right.IsDoubleStackSlot()) {
1402 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001403 } else {
1404 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1405 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001406 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001407 }
1408 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001409 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1410 if (right.IsConstant()) {
1411 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1412 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1413 } else if (right.IsStackSlot()) {
1414 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1415 } else {
1416 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1417 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001418 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1419 break;
1420 }
1421 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001422 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1423 if (right.IsConstant()) {
1424 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1425 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1426 } else if (right.IsDoubleStackSlot()) {
1427 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1428 } else {
1429 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1430 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001431 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1432 break;
1433 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001434 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001435 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001436 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001437 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001438 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001439 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001440
Calin Juravle91debbc2014-11-26 19:01:09 +00001441 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001442 __ movl(out, Immediate(1));
1443 __ jmp(&done);
1444
1445 __ Bind(&less);
1446 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001447
1448 __ Bind(&done);
1449}
1450
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001451void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001452 LocationSummary* locations =
1453 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001454 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001455}
1456
1457void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001458 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001459 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001460}
1461
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001462void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1463 LocationSummary* locations =
1464 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1465 locations->SetOut(Location::ConstantLocation(constant));
1466}
1467
1468void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1469 // Will be generated at use site.
1470 UNUSED(constant);
1471}
1472
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001473void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001474 LocationSummary* locations =
1475 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001476 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001477}
1478
1479void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001480 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001481 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001482}
1483
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001484void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1485 LocationSummary* locations =
1486 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1487 locations->SetOut(Location::ConstantLocation(constant));
1488}
1489
1490void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1491 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001492 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001493}
1494
1495void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1496 LocationSummary* locations =
1497 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1498 locations->SetOut(Location::ConstantLocation(constant));
1499}
1500
1501void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1502 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001503 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001504}
1505
Calin Juravle27df7582015-04-17 19:12:31 +01001506void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1507 memory_barrier->SetLocations(nullptr);
1508}
1509
1510void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1511 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1512}
1513
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001514void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1515 ret->SetLocations(nullptr);
1516}
1517
1518void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001519 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001520 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001521}
1522
1523void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001524 LocationSummary* locations =
1525 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001526 switch (ret->InputAt(0)->GetType()) {
1527 case Primitive::kPrimBoolean:
1528 case Primitive::kPrimByte:
1529 case Primitive::kPrimChar:
1530 case Primitive::kPrimShort:
1531 case Primitive::kPrimInt:
1532 case Primitive::kPrimNot:
1533 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001534 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001535 break;
1536
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001537 case Primitive::kPrimFloat:
1538 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001539 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001540 break;
1541
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001542 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001543 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001544 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001545}
1546
1547void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1548 if (kIsDebugBuild) {
1549 switch (ret->InputAt(0)->GetType()) {
1550 case Primitive::kPrimBoolean:
1551 case Primitive::kPrimByte:
1552 case Primitive::kPrimChar:
1553 case Primitive::kPrimShort:
1554 case Primitive::kPrimInt:
1555 case Primitive::kPrimNot:
1556 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001557 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001558 break;
1559
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001560 case Primitive::kPrimFloat:
1561 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001562 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001563 XMM0);
1564 break;
1565
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001566 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001567 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001568 }
1569 }
1570 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001571}
1572
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001573Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1574 switch (type) {
1575 case Primitive::kPrimBoolean:
1576 case Primitive::kPrimByte:
1577 case Primitive::kPrimChar:
1578 case Primitive::kPrimShort:
1579 case Primitive::kPrimInt:
1580 case Primitive::kPrimNot:
1581 case Primitive::kPrimLong:
1582 return Location::RegisterLocation(RAX);
1583
1584 case Primitive::kPrimVoid:
1585 return Location::NoLocation();
1586
1587 case Primitive::kPrimDouble:
1588 case Primitive::kPrimFloat:
1589 return Location::FpuRegisterLocation(XMM0);
1590 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001591
1592 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001593}
1594
1595Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1596 return Location::RegisterLocation(kMethodRegisterArgument);
1597}
1598
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001599Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001600 switch (type) {
1601 case Primitive::kPrimBoolean:
1602 case Primitive::kPrimByte:
1603 case Primitive::kPrimChar:
1604 case Primitive::kPrimShort:
1605 case Primitive::kPrimInt:
1606 case Primitive::kPrimNot: {
1607 uint32_t index = gp_index_++;
1608 stack_index_++;
1609 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001610 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001611 } else {
1612 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1613 }
1614 }
1615
1616 case Primitive::kPrimLong: {
1617 uint32_t index = gp_index_;
1618 stack_index_ += 2;
1619 if (index < calling_convention.GetNumberOfRegisters()) {
1620 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001621 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001622 } else {
1623 gp_index_ += 2;
1624 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1625 }
1626 }
1627
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001628 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001629 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001630 stack_index_++;
1631 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001632 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001633 } else {
1634 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1635 }
1636 }
1637
1638 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001639 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001640 stack_index_ += 2;
1641 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001642 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001643 } else {
1644 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1645 }
1646 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001647
1648 case Primitive::kPrimVoid:
1649 LOG(FATAL) << "Unexpected parameter type " << type;
1650 break;
1651 }
1652 return Location();
1653}
1654
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001655void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001656 // When we do not run baseline, explicit clinit checks triggered by static
1657 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1658 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001659
Mark Mendellfb8d2792015-03-31 22:16:59 -04001660 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001661 if (intrinsic.TryDispatch(invoke)) {
1662 return;
1663 }
1664
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001665 HandleInvoke(invoke);
1666}
1667
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001668static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1669 if (invoke->GetLocations()->Intrinsified()) {
1670 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1671 intrinsic.Dispatch(invoke);
1672 return true;
1673 }
1674 return false;
1675}
1676
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001677void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001678 // When we do not run baseline, explicit clinit checks triggered by static
1679 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1680 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001681
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001682 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1683 return;
1684 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001685
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001686 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001687 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001688 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001689 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001690}
1691
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001692void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001693 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001694 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001695}
1696
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001697void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001698 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001699 if (intrinsic.TryDispatch(invoke)) {
1700 return;
1701 }
1702
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001703 HandleInvoke(invoke);
1704}
1705
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001706void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001707 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1708 return;
1709 }
1710
Roland Levillain271ab9c2014-11-27 15:23:57 +00001711 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001712 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1713 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001714 LocationSummary* locations = invoke->GetLocations();
1715 Location receiver = locations->InAt(0);
1716 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1717 // temp = object->GetClass();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001718 DCHECK(receiver.IsRegister());
1719 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001720 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001721 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001722 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001723 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001724 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001725 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001726 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001727
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001728 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001729 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001730}
1731
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001732void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1733 HandleInvoke(invoke);
1734 // Add the hidden argument.
1735 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1736}
1737
1738void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1739 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001740 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001741 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1742 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001743 LocationSummary* locations = invoke->GetLocations();
1744 Location receiver = locations->InAt(0);
1745 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1746
1747 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001748 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1749 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001750
1751 // temp = object->GetClass();
1752 if (receiver.IsStackSlot()) {
1753 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1754 __ movl(temp, Address(temp, class_offset));
1755 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001756 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001757 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001758 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001759 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001760 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001761 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001762 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001763 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001764 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001765
1766 DCHECK(!codegen_->IsLeafMethod());
1767 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1768}
1769
Roland Levillain88cb1752014-10-20 16:36:47 +01001770void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1771 LocationSummary* locations =
1772 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1773 switch (neg->GetResultType()) {
1774 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001775 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001776 locations->SetInAt(0, Location::RequiresRegister());
1777 locations->SetOut(Location::SameAsFirstInput());
1778 break;
1779
Roland Levillain88cb1752014-10-20 16:36:47 +01001780 case Primitive::kPrimFloat:
1781 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001782 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001783 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001784 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001785 break;
1786
1787 default:
1788 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1789 }
1790}
1791
1792void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1793 LocationSummary* locations = neg->GetLocations();
1794 Location out = locations->Out();
1795 Location in = locations->InAt(0);
1796 switch (neg->GetResultType()) {
1797 case Primitive::kPrimInt:
1798 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001799 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001800 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001801 break;
1802
1803 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001804 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001805 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001806 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001807 break;
1808
Roland Levillain5368c212014-11-27 15:03:41 +00001809 case Primitive::kPrimFloat: {
1810 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001811 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001812 // Implement float negation with an exclusive or with value
1813 // 0x80000000 (mask for bit 31, representing the sign of a
1814 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001815 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001816 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001817 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001818 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001819
Roland Levillain5368c212014-11-27 15:03:41 +00001820 case Primitive::kPrimDouble: {
1821 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001822 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001823 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001824 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001825 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001826 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001827 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001828 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001829 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001830
1831 default:
1832 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1833 }
1834}
1835
Roland Levillaindff1f282014-11-05 14:15:05 +00001836void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1837 LocationSummary* locations =
1838 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1839 Primitive::Type result_type = conversion->GetResultType();
1840 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001841 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001842
David Brazdilb2bd1c52015-03-25 11:17:37 +00001843 // The Java language does not allow treating boolean as an integral type but
1844 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001845
Roland Levillaindff1f282014-11-05 14:15:05 +00001846 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001847 case Primitive::kPrimByte:
1848 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001849 case Primitive::kPrimBoolean:
1850 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001851 case Primitive::kPrimShort:
1852 case Primitive::kPrimInt:
1853 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001854 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001855 locations->SetInAt(0, Location::Any());
1856 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1857 break;
1858
1859 default:
1860 LOG(FATAL) << "Unexpected type conversion from " << input_type
1861 << " to " << result_type;
1862 }
1863 break;
1864
Roland Levillain01a8d712014-11-14 16:27:39 +00001865 case Primitive::kPrimShort:
1866 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001867 case Primitive::kPrimBoolean:
1868 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001869 case Primitive::kPrimByte:
1870 case Primitive::kPrimInt:
1871 case Primitive::kPrimChar:
1872 // Processing a Dex `int-to-short' instruction.
1873 locations->SetInAt(0, Location::Any());
1874 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1875 break;
1876
1877 default:
1878 LOG(FATAL) << "Unexpected type conversion from " << input_type
1879 << " to " << result_type;
1880 }
1881 break;
1882
Roland Levillain946e1432014-11-11 17:35:19 +00001883 case Primitive::kPrimInt:
1884 switch (input_type) {
1885 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001886 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001887 locations->SetInAt(0, Location::Any());
1888 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1889 break;
1890
1891 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001892 // Processing a Dex `float-to-int' instruction.
1893 locations->SetInAt(0, Location::RequiresFpuRegister());
1894 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001895 break;
1896
Roland Levillain946e1432014-11-11 17:35:19 +00001897 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001898 // Processing a Dex `double-to-int' instruction.
1899 locations->SetInAt(0, Location::RequiresFpuRegister());
1900 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001901 break;
1902
1903 default:
1904 LOG(FATAL) << "Unexpected type conversion from " << input_type
1905 << " to " << result_type;
1906 }
1907 break;
1908
Roland Levillaindff1f282014-11-05 14:15:05 +00001909 case Primitive::kPrimLong:
1910 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001911 case Primitive::kPrimBoolean:
1912 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001913 case Primitive::kPrimByte:
1914 case Primitive::kPrimShort:
1915 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001916 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001917 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001918 // TODO: We would benefit from a (to-be-implemented)
1919 // Location::RegisterOrStackSlot requirement for this input.
1920 locations->SetInAt(0, Location::RequiresRegister());
1921 locations->SetOut(Location::RequiresRegister());
1922 break;
1923
1924 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001925 // Processing a Dex `float-to-long' instruction.
1926 locations->SetInAt(0, Location::RequiresFpuRegister());
1927 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00001928 break;
1929
Roland Levillaindff1f282014-11-05 14:15:05 +00001930 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001931 // Processing a Dex `double-to-long' instruction.
1932 locations->SetInAt(0, Location::RequiresFpuRegister());
1933 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001934 break;
1935
1936 default:
1937 LOG(FATAL) << "Unexpected type conversion from " << input_type
1938 << " to " << result_type;
1939 }
1940 break;
1941
Roland Levillain981e4542014-11-14 11:47:14 +00001942 case Primitive::kPrimChar:
1943 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001944 case Primitive::kPrimBoolean:
1945 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001946 case Primitive::kPrimByte:
1947 case Primitive::kPrimShort:
1948 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001949 // Processing a Dex `int-to-char' instruction.
1950 locations->SetInAt(0, Location::Any());
1951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1952 break;
1953
1954 default:
1955 LOG(FATAL) << "Unexpected type conversion from " << input_type
1956 << " to " << result_type;
1957 }
1958 break;
1959
Roland Levillaindff1f282014-11-05 14:15:05 +00001960 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001961 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001962 case Primitive::kPrimBoolean:
1963 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001964 case Primitive::kPrimByte:
1965 case Primitive::kPrimShort:
1966 case Primitive::kPrimInt:
1967 case Primitive::kPrimChar:
1968 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001969 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001970 locations->SetOut(Location::RequiresFpuRegister());
1971 break;
1972
1973 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001974 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001975 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001976 locations->SetOut(Location::RequiresFpuRegister());
1977 break;
1978
Roland Levillaincff13742014-11-17 14:32:17 +00001979 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001980 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001981 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001982 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001983 break;
1984
1985 default:
1986 LOG(FATAL) << "Unexpected type conversion from " << input_type
1987 << " to " << result_type;
1988 };
1989 break;
1990
Roland Levillaindff1f282014-11-05 14:15:05 +00001991 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001992 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001993 case Primitive::kPrimBoolean:
1994 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001995 case Primitive::kPrimByte:
1996 case Primitive::kPrimShort:
1997 case Primitive::kPrimInt:
1998 case Primitive::kPrimChar:
1999 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002000 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002001 locations->SetOut(Location::RequiresFpuRegister());
2002 break;
2003
2004 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002005 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002006 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002007 locations->SetOut(Location::RequiresFpuRegister());
2008 break;
2009
Roland Levillaincff13742014-11-17 14:32:17 +00002010 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002011 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002012 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002013 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002014 break;
2015
2016 default:
2017 LOG(FATAL) << "Unexpected type conversion from " << input_type
2018 << " to " << result_type;
2019 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002020 break;
2021
2022 default:
2023 LOG(FATAL) << "Unexpected type conversion from " << input_type
2024 << " to " << result_type;
2025 }
2026}
2027
2028void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2029 LocationSummary* locations = conversion->GetLocations();
2030 Location out = locations->Out();
2031 Location in = locations->InAt(0);
2032 Primitive::Type result_type = conversion->GetResultType();
2033 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002034 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002035 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002036 case Primitive::kPrimByte:
2037 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002038 case Primitive::kPrimBoolean:
2039 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002040 case Primitive::kPrimShort:
2041 case Primitive::kPrimInt:
2042 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002043 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002044 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002046 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002047 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002048 Address(CpuRegister(RSP), in.GetStackIndex()));
2049 } else {
2050 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002051 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002052 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2053 }
2054 break;
2055
2056 default:
2057 LOG(FATAL) << "Unexpected type conversion from " << input_type
2058 << " to " << result_type;
2059 }
2060 break;
2061
Roland Levillain01a8d712014-11-14 16:27:39 +00002062 case Primitive::kPrimShort:
2063 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002064 case Primitive::kPrimBoolean:
2065 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002066 case Primitive::kPrimByte:
2067 case Primitive::kPrimInt:
2068 case Primitive::kPrimChar:
2069 // Processing a Dex `int-to-short' instruction.
2070 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002071 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002072 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002073 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002074 Address(CpuRegister(RSP), in.GetStackIndex()));
2075 } else {
2076 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002077 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002078 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2079 }
2080 break;
2081
2082 default:
2083 LOG(FATAL) << "Unexpected type conversion from " << input_type
2084 << " to " << result_type;
2085 }
2086 break;
2087
Roland Levillain946e1432014-11-11 17:35:19 +00002088 case Primitive::kPrimInt:
2089 switch (input_type) {
2090 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002091 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002092 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002093 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002094 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002095 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002096 Address(CpuRegister(RSP), in.GetStackIndex()));
2097 } else {
2098 DCHECK(in.IsConstant());
2099 DCHECK(in.GetConstant()->IsLongConstant());
2100 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002101 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002102 }
2103 break;
2104
Roland Levillain3f8f9362014-12-02 17:45:01 +00002105 case Primitive::kPrimFloat: {
2106 // Processing a Dex `float-to-int' instruction.
2107 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2108 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain3f8f9362014-12-02 17:45:01 +00002109 Label done, nan;
2110
2111 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002112 // if input >= (float)INT_MAX goto done
2113 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002114 __ j(kAboveEqual, &done);
2115 // if input == NaN goto nan
2116 __ j(kUnordered, &nan);
2117 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002118 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002119 __ jmp(&done);
2120 __ Bind(&nan);
2121 // output = 0
2122 __ xorl(output, output);
2123 __ Bind(&done);
2124 break;
2125 }
2126
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002127 case Primitive::kPrimDouble: {
2128 // Processing a Dex `double-to-int' instruction.
2129 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2130 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002131 Label done, nan;
2132
2133 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002134 // if input >= (double)INT_MAX goto done
2135 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002136 __ j(kAboveEqual, &done);
2137 // if input == NaN goto nan
2138 __ j(kUnordered, &nan);
2139 // output = double-to-int-truncate(input)
2140 __ cvttsd2si(output, input);
2141 __ jmp(&done);
2142 __ Bind(&nan);
2143 // output = 0
2144 __ xorl(output, output);
2145 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002146 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002147 }
Roland Levillain946e1432014-11-11 17:35:19 +00002148
2149 default:
2150 LOG(FATAL) << "Unexpected type conversion from " << input_type
2151 << " to " << result_type;
2152 }
2153 break;
2154
Roland Levillaindff1f282014-11-05 14:15:05 +00002155 case Primitive::kPrimLong:
2156 switch (input_type) {
2157 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002158 case Primitive::kPrimBoolean:
2159 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002160 case Primitive::kPrimByte:
2161 case Primitive::kPrimShort:
2162 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002163 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002164 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002165 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002166 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002167 break;
2168
Roland Levillain624279f2014-12-04 11:54:28 +00002169 case Primitive::kPrimFloat: {
2170 // Processing a Dex `float-to-long' instruction.
2171 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2172 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain624279f2014-12-04 11:54:28 +00002173 Label done, nan;
2174
Mark Mendell92e83bf2015-05-07 11:25:03 -04002175 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002176 // if input >= (float)LONG_MAX goto done
2177 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002178 __ j(kAboveEqual, &done);
2179 // if input == NaN goto nan
2180 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002181 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002182 __ cvttss2si(output, input, true);
2183 __ jmp(&done);
2184 __ Bind(&nan);
2185 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002186 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002187 __ Bind(&done);
2188 break;
2189 }
2190
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002191 case Primitive::kPrimDouble: {
2192 // Processing a Dex `double-to-long' instruction.
2193 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2194 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002195 Label done, nan;
2196
Mark Mendell92e83bf2015-05-07 11:25:03 -04002197 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002198 // if input >= (double)LONG_MAX goto done
2199 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002200 __ j(kAboveEqual, &done);
2201 // if input == NaN goto nan
2202 __ j(kUnordered, &nan);
2203 // output = double-to-long-truncate(input)
2204 __ cvttsd2si(output, input, true);
2205 __ jmp(&done);
2206 __ Bind(&nan);
2207 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002208 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002209 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002210 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002211 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002212
2213 default:
2214 LOG(FATAL) << "Unexpected type conversion from " << input_type
2215 << " to " << result_type;
2216 }
2217 break;
2218
Roland Levillain981e4542014-11-14 11:47:14 +00002219 case Primitive::kPrimChar:
2220 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002221 case Primitive::kPrimBoolean:
2222 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002223 case Primitive::kPrimByte:
2224 case Primitive::kPrimShort:
2225 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002226 // Processing a Dex `int-to-char' instruction.
2227 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002228 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002229 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002230 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002231 Address(CpuRegister(RSP), in.GetStackIndex()));
2232 } else {
2233 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002235 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2236 }
2237 break;
2238
2239 default:
2240 LOG(FATAL) << "Unexpected type conversion from " << input_type
2241 << " to " << result_type;
2242 }
2243 break;
2244
Roland Levillaindff1f282014-11-05 14:15:05 +00002245 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002246 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002247 case Primitive::kPrimBoolean:
2248 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002249 case Primitive::kPrimByte:
2250 case Primitive::kPrimShort:
2251 case Primitive::kPrimInt:
2252 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002253 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002254 if (in.IsRegister()) {
2255 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2256 } else if (in.IsConstant()) {
2257 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2258 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2259 if (v == 0) {
2260 __ xorps(dest, dest);
2261 } else {
2262 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2263 }
2264 } else {
2265 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2266 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2267 }
Roland Levillaincff13742014-11-17 14:32:17 +00002268 break;
2269
2270 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002271 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002272 if (in.IsRegister()) {
2273 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2274 } else if (in.IsConstant()) {
2275 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2276 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2277 if (v == 0) {
2278 __ xorps(dest, dest);
2279 } else {
2280 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2281 }
2282 } else {
2283 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2284 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2285 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002286 break;
2287
Roland Levillaincff13742014-11-17 14:32:17 +00002288 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002289 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002290 if (in.IsFpuRegister()) {
2291 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2292 } else if (in.IsConstant()) {
2293 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2294 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2295 if (bit_cast<int64_t, double>(v) == 0) {
2296 __ xorps(dest, dest);
2297 } else {
2298 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2299 }
2300 } else {
2301 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2302 Address(CpuRegister(RSP), in.GetStackIndex()));
2303 }
Roland Levillaincff13742014-11-17 14:32:17 +00002304 break;
2305
2306 default:
2307 LOG(FATAL) << "Unexpected type conversion from " << input_type
2308 << " to " << result_type;
2309 };
2310 break;
2311
Roland Levillaindff1f282014-11-05 14:15:05 +00002312 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002313 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002314 case Primitive::kPrimBoolean:
2315 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002316 case Primitive::kPrimByte:
2317 case Primitive::kPrimShort:
2318 case Primitive::kPrimInt:
2319 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002320 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002321 if (in.IsRegister()) {
2322 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2323 } else if (in.IsConstant()) {
2324 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2325 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2326 if (v == 0) {
2327 __ xorpd(dest, dest);
2328 } else {
2329 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2330 }
2331 } else {
2332 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2333 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2334 }
Roland Levillaincff13742014-11-17 14:32:17 +00002335 break;
2336
2337 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002338 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002339 if (in.IsRegister()) {
2340 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2341 } else if (in.IsConstant()) {
2342 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2343 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2344 if (v == 0) {
2345 __ xorpd(dest, dest);
2346 } else {
2347 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2348 }
2349 } else {
2350 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2351 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2352 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002353 break;
2354
Roland Levillaincff13742014-11-17 14:32:17 +00002355 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002356 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002357 if (in.IsFpuRegister()) {
2358 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2359 } else if (in.IsConstant()) {
2360 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2361 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2362 if (bit_cast<int32_t, float>(v) == 0) {
2363 __ xorpd(dest, dest);
2364 } else {
2365 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2366 }
2367 } else {
2368 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2369 Address(CpuRegister(RSP), in.GetStackIndex()));
2370 }
Roland Levillaincff13742014-11-17 14:32:17 +00002371 break;
2372
2373 default:
2374 LOG(FATAL) << "Unexpected type conversion from " << input_type
2375 << " to " << result_type;
2376 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002377 break;
2378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 }
2383}
2384
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002385void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002386 LocationSummary* locations =
2387 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002388 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002389 case Primitive::kPrimInt: {
2390 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002391 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2392 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002393 break;
2394 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002395
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002396 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002397 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002398 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002399 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002401 break;
2402 }
2403
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002404 case Primitive::kPrimDouble:
2405 case Primitive::kPrimFloat: {
2406 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002407 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002408 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002409 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002410 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002411
2412 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002413 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002414 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002415}
2416
2417void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2418 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002419 Location first = locations->InAt(0);
2420 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002421 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002422
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002423 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002424 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002425 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002426 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2427 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002428 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2429 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002430 } else {
2431 __ leal(out.AsRegister<CpuRegister>(), Address(
2432 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2433 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002434 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002435 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2436 __ addl(out.AsRegister<CpuRegister>(),
2437 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2438 } else {
2439 __ leal(out.AsRegister<CpuRegister>(), Address(
2440 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2441 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002442 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002443 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002444 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002445 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002446 break;
2447 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002448
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002449 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002450 if (second.IsRegister()) {
2451 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2452 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002453 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2454 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002455 } else {
2456 __ leaq(out.AsRegister<CpuRegister>(), Address(
2457 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2458 }
2459 } else {
2460 DCHECK(second.IsConstant());
2461 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2462 int32_t int32_value = Low32Bits(value);
2463 DCHECK_EQ(int32_value, value);
2464 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2465 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2466 } else {
2467 __ leaq(out.AsRegister<CpuRegister>(), Address(
2468 first.AsRegister<CpuRegister>(), int32_value));
2469 }
2470 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002471 break;
2472 }
2473
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002474 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002475 if (second.IsFpuRegister()) {
2476 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2477 } else if (second.IsConstant()) {
2478 __ addss(first.AsFpuRegister<XmmRegister>(),
2479 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2480 } else {
2481 DCHECK(second.IsStackSlot());
2482 __ addss(first.AsFpuRegister<XmmRegister>(),
2483 Address(CpuRegister(RSP), second.GetStackIndex()));
2484 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002485 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002486 }
2487
2488 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002489 if (second.IsFpuRegister()) {
2490 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2491 } else if (second.IsConstant()) {
2492 __ addsd(first.AsFpuRegister<XmmRegister>(),
2493 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2494 } else {
2495 DCHECK(second.IsDoubleStackSlot());
2496 __ addsd(first.AsFpuRegister<XmmRegister>(),
2497 Address(CpuRegister(RSP), second.GetStackIndex()));
2498 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002499 break;
2500 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002501
2502 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002503 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002504 }
2505}
2506
2507void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002508 LocationSummary* locations =
2509 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002510 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002511 case Primitive::kPrimInt: {
2512 locations->SetInAt(0, Location::RequiresRegister());
2513 locations->SetInAt(1, Location::Any());
2514 locations->SetOut(Location::SameAsFirstInput());
2515 break;
2516 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002517 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002518 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002519 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002520 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002521 break;
2522 }
Calin Juravle11351682014-10-23 15:38:15 +01002523 case Primitive::kPrimFloat:
2524 case Primitive::kPrimDouble: {
2525 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002526 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002527 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002528 break;
Calin Juravle11351682014-10-23 15:38:15 +01002529 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002530 default:
Calin Juravle11351682014-10-23 15:38:15 +01002531 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002532 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002533}
2534
2535void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2536 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002537 Location first = locations->InAt(0);
2538 Location second = locations->InAt(1);
2539 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002540 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002541 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002542 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002543 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002544 } else if (second.IsConstant()) {
2545 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002546 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002547 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002548 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002549 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002550 break;
2551 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002552 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002553 if (second.IsConstant()) {
2554 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2555 DCHECK(IsInt<32>(value));
2556 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2557 } else {
2558 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2559 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002560 break;
2561 }
2562
Calin Juravle11351682014-10-23 15:38:15 +01002563 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002564 if (second.IsFpuRegister()) {
2565 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2566 } else if (second.IsConstant()) {
2567 __ subss(first.AsFpuRegister<XmmRegister>(),
2568 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2569 } else {
2570 DCHECK(second.IsStackSlot());
2571 __ subss(first.AsFpuRegister<XmmRegister>(),
2572 Address(CpuRegister(RSP), second.GetStackIndex()));
2573 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002574 break;
Calin Juravle11351682014-10-23 15:38:15 +01002575 }
2576
2577 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002578 if (second.IsFpuRegister()) {
2579 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2580 } else if (second.IsConstant()) {
2581 __ subsd(first.AsFpuRegister<XmmRegister>(),
2582 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2583 } else {
2584 DCHECK(second.IsDoubleStackSlot());
2585 __ subsd(first.AsFpuRegister<XmmRegister>(),
2586 Address(CpuRegister(RSP), second.GetStackIndex()));
2587 }
Calin Juravle11351682014-10-23 15:38:15 +01002588 break;
2589 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002590
2591 default:
Calin Juravle11351682014-10-23 15:38:15 +01002592 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002593 }
2594}
2595
Calin Juravle34bacdf2014-10-07 20:23:36 +01002596void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2597 LocationSummary* locations =
2598 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2599 switch (mul->GetResultType()) {
2600 case Primitive::kPrimInt: {
2601 locations->SetInAt(0, Location::RequiresRegister());
2602 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002603 if (mul->InputAt(1)->IsIntConstant()) {
2604 // Can use 3 operand multiply.
2605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2606 } else {
2607 locations->SetOut(Location::SameAsFirstInput());
2608 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002609 break;
2610 }
2611 case Primitive::kPrimLong: {
2612 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002613 locations->SetInAt(1, Location::Any());
2614 if (mul->InputAt(1)->IsLongConstant() &&
2615 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002616 // Can use 3 operand multiply.
2617 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2618 } else {
2619 locations->SetOut(Location::SameAsFirstInput());
2620 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002621 break;
2622 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002623 case Primitive::kPrimFloat:
2624 case Primitive::kPrimDouble: {
2625 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002626 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002627 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002628 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002629 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002630
2631 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002632 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002633 }
2634}
2635
2636void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2637 LocationSummary* locations = mul->GetLocations();
2638 Location first = locations->InAt(0);
2639 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002640 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002641 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002642 case Primitive::kPrimInt:
2643 // The constant may have ended up in a register, so test explicitly to avoid
2644 // problems where the output may not be the same as the first operand.
2645 if (mul->InputAt(1)->IsIntConstant()) {
2646 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2647 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2648 } else if (second.IsRegister()) {
2649 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002650 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002651 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002652 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002653 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002654 __ imull(first.AsRegister<CpuRegister>(),
2655 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002656 }
2657 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002658 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002659 // The constant may have ended up in a register, so test explicitly to avoid
2660 // problems where the output may not be the same as the first operand.
2661 if (mul->InputAt(1)->IsLongConstant()) {
2662 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2663 if (IsInt<32>(value)) {
2664 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2665 Immediate(static_cast<int32_t>(value)));
2666 } else {
2667 // Have to use the constant area.
2668 DCHECK(first.Equals(out));
2669 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2670 }
2671 } else if (second.IsRegister()) {
2672 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002673 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002674 } else {
2675 DCHECK(second.IsDoubleStackSlot());
2676 DCHECK(first.Equals(out));
2677 __ imulq(first.AsRegister<CpuRegister>(),
2678 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002679 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002680 break;
2681 }
2682
Calin Juravleb5bfa962014-10-21 18:02:24 +01002683 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002684 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002685 if (second.IsFpuRegister()) {
2686 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2687 } else if (second.IsConstant()) {
2688 __ mulss(first.AsFpuRegister<XmmRegister>(),
2689 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2690 } else {
2691 DCHECK(second.IsStackSlot());
2692 __ mulss(first.AsFpuRegister<XmmRegister>(),
2693 Address(CpuRegister(RSP), second.GetStackIndex()));
2694 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002695 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002696 }
2697
2698 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002699 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002700 if (second.IsFpuRegister()) {
2701 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2702 } else if (second.IsConstant()) {
2703 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2704 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2705 } else {
2706 DCHECK(second.IsDoubleStackSlot());
2707 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2708 Address(CpuRegister(RSP), second.GetStackIndex()));
2709 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002710 break;
2711 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002712
2713 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002714 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002715 }
2716}
2717
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002718void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2719 uint32_t stack_adjustment, bool is_float) {
2720 if (source.IsStackSlot()) {
2721 DCHECK(is_float);
2722 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2723 } else if (source.IsDoubleStackSlot()) {
2724 DCHECK(!is_float);
2725 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2726 } else {
2727 // Write the value to the temporary location on the stack and load to FP stack.
2728 if (is_float) {
2729 Location stack_temp = Location::StackSlot(temp_offset);
2730 codegen_->Move(stack_temp, source);
2731 __ flds(Address(CpuRegister(RSP), temp_offset));
2732 } else {
2733 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2734 codegen_->Move(stack_temp, source);
2735 __ fldl(Address(CpuRegister(RSP), temp_offset));
2736 }
2737 }
2738}
2739
2740void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2741 Primitive::Type type = rem->GetResultType();
2742 bool is_float = type == Primitive::kPrimFloat;
2743 size_t elem_size = Primitive::ComponentSize(type);
2744 LocationSummary* locations = rem->GetLocations();
2745 Location first = locations->InAt(0);
2746 Location second = locations->InAt(1);
2747 Location out = locations->Out();
2748
2749 // Create stack space for 2 elements.
2750 // TODO: enhance register allocator to ask for stack temporaries.
2751 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2752
2753 // Load the values to the FP stack in reverse order, using temporaries if needed.
2754 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2755 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2756
2757 // Loop doing FPREM until we stabilize.
2758 Label retry;
2759 __ Bind(&retry);
2760 __ fprem();
2761
2762 // Move FP status to AX.
2763 __ fstsw();
2764
2765 // And see if the argument reduction is complete. This is signaled by the
2766 // C2 FPU flag bit set to 0.
2767 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2768 __ j(kNotEqual, &retry);
2769
2770 // We have settled on the final value. Retrieve it into an XMM register.
2771 // Store FP top of stack to real stack.
2772 if (is_float) {
2773 __ fsts(Address(CpuRegister(RSP), 0));
2774 } else {
2775 __ fstl(Address(CpuRegister(RSP), 0));
2776 }
2777
2778 // Pop the 2 items from the FP stack.
2779 __ fucompp();
2780
2781 // Load the value from the stack into an XMM register.
2782 DCHECK(out.IsFpuRegister()) << out;
2783 if (is_float) {
2784 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2785 } else {
2786 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2787 }
2788
2789 // And remove the temporary stack space we allocated.
2790 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2791}
2792
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002793void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2794 DCHECK(instruction->IsDiv() || instruction->IsRem());
2795
2796 LocationSummary* locations = instruction->GetLocations();
2797 Location second = locations->InAt(1);
2798 DCHECK(second.IsConstant());
2799
2800 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2801 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002802 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002803
2804 DCHECK(imm == 1 || imm == -1);
2805
2806 switch (instruction->GetResultType()) {
2807 case Primitive::kPrimInt: {
2808 if (instruction->IsRem()) {
2809 __ xorl(output_register, output_register);
2810 } else {
2811 __ movl(output_register, input_register);
2812 if (imm == -1) {
2813 __ negl(output_register);
2814 }
2815 }
2816 break;
2817 }
2818
2819 case Primitive::kPrimLong: {
2820 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002821 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002822 } else {
2823 __ movq(output_register, input_register);
2824 if (imm == -1) {
2825 __ negq(output_register);
2826 }
2827 }
2828 break;
2829 }
2830
2831 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002832 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002833 }
2834}
2835
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002836void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002837 LocationSummary* locations = instruction->GetLocations();
2838 Location second = locations->InAt(1);
2839
2840 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2841 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2842
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002843 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002844
2845 DCHECK(IsPowerOfTwo(std::abs(imm)));
2846
2847 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2848
2849 if (instruction->GetResultType() == Primitive::kPrimInt) {
2850 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2851 __ testl(numerator, numerator);
2852 __ cmov(kGreaterEqual, tmp, numerator);
2853 int shift = CTZ(imm);
2854 __ sarl(tmp, Immediate(shift));
2855
2856 if (imm < 0) {
2857 __ negl(tmp);
2858 }
2859
2860 __ movl(output_register, tmp);
2861 } else {
2862 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2863 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2864
Mark Mendell92e83bf2015-05-07 11:25:03 -04002865 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002866 __ addq(rdx, numerator);
2867 __ testq(numerator, numerator);
2868 __ cmov(kGreaterEqual, rdx, numerator);
2869 int shift = CTZ(imm);
2870 __ sarq(rdx, Immediate(shift));
2871
2872 if (imm < 0) {
2873 __ negq(rdx);
2874 }
2875
2876 __ movq(output_register, rdx);
2877 }
2878}
2879
2880void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2881 DCHECK(instruction->IsDiv() || instruction->IsRem());
2882
2883 LocationSummary* locations = instruction->GetLocations();
2884 Location second = locations->InAt(1);
2885
2886 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2887 : locations->GetTemp(0).AsRegister<CpuRegister>();
2888 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2889 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2890 : locations->Out().AsRegister<CpuRegister>();
2891 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2892
2893 DCHECK_EQ(RAX, eax.AsRegister());
2894 DCHECK_EQ(RDX, edx.AsRegister());
2895 if (instruction->IsDiv()) {
2896 DCHECK_EQ(RAX, out.AsRegister());
2897 } else {
2898 DCHECK_EQ(RDX, out.AsRegister());
2899 }
2900
2901 int64_t magic;
2902 int shift;
2903
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002904 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002905 if (instruction->GetResultType() == Primitive::kPrimInt) {
2906 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2907
2908 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2909
2910 __ movl(numerator, eax);
2911
2912 Label no_div;
2913 Label end;
2914 __ testl(eax, eax);
2915 __ j(kNotEqual, &no_div);
2916
2917 __ xorl(out, out);
2918 __ jmp(&end);
2919
2920 __ Bind(&no_div);
2921
2922 __ movl(eax, Immediate(magic));
2923 __ imull(numerator);
2924
2925 if (imm > 0 && magic < 0) {
2926 __ addl(edx, numerator);
2927 } else if (imm < 0 && magic > 0) {
2928 __ subl(edx, numerator);
2929 }
2930
2931 if (shift != 0) {
2932 __ sarl(edx, Immediate(shift));
2933 }
2934
2935 __ movl(eax, edx);
2936 __ shrl(edx, Immediate(31));
2937 __ addl(edx, eax);
2938
2939 if (instruction->IsRem()) {
2940 __ movl(eax, numerator);
2941 __ imull(edx, Immediate(imm));
2942 __ subl(eax, edx);
2943 __ movl(edx, eax);
2944 } else {
2945 __ movl(eax, edx);
2946 }
2947 __ Bind(&end);
2948 } else {
2949 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2950
2951 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2952
2953 CpuRegister rax = eax;
2954 CpuRegister rdx = edx;
2955
2956 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2957
2958 // Save the numerator.
2959 __ movq(numerator, rax);
2960
2961 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002962 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002963
2964 // RDX:RAX = magic * numerator
2965 __ imulq(numerator);
2966
2967 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002968 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002969 __ addq(rdx, numerator);
2970 } else if (imm < 0 && magic > 0) {
2971 // RDX -= numerator
2972 __ subq(rdx, numerator);
2973 }
2974
2975 // Shift if needed.
2976 if (shift != 0) {
2977 __ sarq(rdx, Immediate(shift));
2978 }
2979
2980 // RDX += 1 if RDX < 0
2981 __ movq(rax, rdx);
2982 __ shrq(rdx, Immediate(63));
2983 __ addq(rdx, rax);
2984
2985 if (instruction->IsRem()) {
2986 __ movq(rax, numerator);
2987
2988 if (IsInt<32>(imm)) {
2989 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
2990 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002991 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002992 }
2993
2994 __ subq(rax, rdx);
2995 __ movq(rdx, rax);
2996 } else {
2997 __ movq(rax, rdx);
2998 }
2999 }
3000}
3001
Calin Juravlebacfec32014-11-14 15:54:36 +00003002void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3003 DCHECK(instruction->IsDiv() || instruction->IsRem());
3004 Primitive::Type type = instruction->GetResultType();
3005 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3006
3007 bool is_div = instruction->IsDiv();
3008 LocationSummary* locations = instruction->GetLocations();
3009
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003010 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3011 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003012
Roland Levillain271ab9c2014-11-27 15:23:57 +00003013 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003014 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003015
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003016 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003017 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003018
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003019 if (imm == 0) {
3020 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3021 } else if (imm == 1 || imm == -1) {
3022 DivRemOneOrMinusOne(instruction);
3023 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003024 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003025 } else {
3026 DCHECK(imm <= -2 || imm >= 2);
3027 GenerateDivRemWithAnyConstant(instruction);
3028 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003029 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003030 SlowPathCodeX86_64* slow_path =
3031 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3032 out.AsRegister(), type, is_div);
3033 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003034
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003035 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3036 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3037 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3038 // so it's safe to just use negl instead of more complex comparisons.
3039 if (type == Primitive::kPrimInt) {
3040 __ cmpl(second_reg, Immediate(-1));
3041 __ j(kEqual, slow_path->GetEntryLabel());
3042 // edx:eax <- sign-extended of eax
3043 __ cdq();
3044 // eax = quotient, edx = remainder
3045 __ idivl(second_reg);
3046 } else {
3047 __ cmpq(second_reg, Immediate(-1));
3048 __ j(kEqual, slow_path->GetEntryLabel());
3049 // rdx:rax <- sign-extended of rax
3050 __ cqo();
3051 // rax = quotient, rdx = remainder
3052 __ idivq(second_reg);
3053 }
3054 __ Bind(slow_path->GetExitLabel());
3055 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003056}
3057
Calin Juravle7c4954d2014-10-28 16:57:40 +00003058void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3059 LocationSummary* locations =
3060 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3061 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003062 case Primitive::kPrimInt:
3063 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003064 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003065 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003066 locations->SetOut(Location::SameAsFirstInput());
3067 // Intel uses edx:eax as the dividend.
3068 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003069 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3070 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3071 // output and request another temp.
3072 if (div->InputAt(1)->IsConstant()) {
3073 locations->AddTemp(Location::RequiresRegister());
3074 }
Calin Juravled0d48522014-11-04 16:40:20 +00003075 break;
3076 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003077
Calin Juravle7c4954d2014-10-28 16:57:40 +00003078 case Primitive::kPrimFloat:
3079 case Primitive::kPrimDouble: {
3080 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003081 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003082 locations->SetOut(Location::SameAsFirstInput());
3083 break;
3084 }
3085
3086 default:
3087 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3088 }
3089}
3090
3091void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3092 LocationSummary* locations = div->GetLocations();
3093 Location first = locations->InAt(0);
3094 Location second = locations->InAt(1);
3095 DCHECK(first.Equals(locations->Out()));
3096
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003097 Primitive::Type type = div->GetResultType();
3098 switch (type) {
3099 case Primitive::kPrimInt:
3100 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003101 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003102 break;
3103 }
3104
Calin Juravle7c4954d2014-10-28 16:57:40 +00003105 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003106 if (second.IsFpuRegister()) {
3107 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3108 } else if (second.IsConstant()) {
3109 __ divss(first.AsFpuRegister<XmmRegister>(),
3110 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3111 } else {
3112 DCHECK(second.IsStackSlot());
3113 __ divss(first.AsFpuRegister<XmmRegister>(),
3114 Address(CpuRegister(RSP), second.GetStackIndex()));
3115 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003116 break;
3117 }
3118
3119 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003120 if (second.IsFpuRegister()) {
3121 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3122 } else if (second.IsConstant()) {
3123 __ divsd(first.AsFpuRegister<XmmRegister>(),
3124 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3125 } else {
3126 DCHECK(second.IsDoubleStackSlot());
3127 __ divsd(first.AsFpuRegister<XmmRegister>(),
3128 Address(CpuRegister(RSP), second.GetStackIndex()));
3129 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003130 break;
3131 }
3132
3133 default:
3134 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3135 }
3136}
3137
Calin Juravlebacfec32014-11-14 15:54:36 +00003138void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003139 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003140 LocationSummary* locations =
3141 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003142
3143 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003144 case Primitive::kPrimInt:
3145 case Primitive::kPrimLong: {
3146 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003147 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003148 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3149 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003150 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3151 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3152 // output and request another temp.
3153 if (rem->InputAt(1)->IsConstant()) {
3154 locations->AddTemp(Location::RequiresRegister());
3155 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003156 break;
3157 }
3158
3159 case Primitive::kPrimFloat:
3160 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003161 locations->SetInAt(0, Location::Any());
3162 locations->SetInAt(1, Location::Any());
3163 locations->SetOut(Location::RequiresFpuRegister());
3164 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003165 break;
3166 }
3167
3168 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003169 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003170 }
3171}
3172
3173void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3174 Primitive::Type type = rem->GetResultType();
3175 switch (type) {
3176 case Primitive::kPrimInt:
3177 case Primitive::kPrimLong: {
3178 GenerateDivRemIntegral(rem);
3179 break;
3180 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003181 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003182 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003183 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003184 break;
3185 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003186 default:
3187 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3188 }
3189}
3190
Calin Juravled0d48522014-11-04 16:40:20 +00003191void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3192 LocationSummary* locations =
3193 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3194 locations->SetInAt(0, Location::Any());
3195 if (instruction->HasUses()) {
3196 locations->SetOut(Location::SameAsFirstInput());
3197 }
3198}
3199
3200void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3201 SlowPathCodeX86_64* slow_path =
3202 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3203 codegen_->AddSlowPath(slow_path);
3204
3205 LocationSummary* locations = instruction->GetLocations();
3206 Location value = locations->InAt(0);
3207
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003208 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003209 case Primitive::kPrimByte:
3210 case Primitive::kPrimChar:
3211 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003212 case Primitive::kPrimInt: {
3213 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003215 __ j(kEqual, slow_path->GetEntryLabel());
3216 } else if (value.IsStackSlot()) {
3217 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3218 __ j(kEqual, slow_path->GetEntryLabel());
3219 } else {
3220 DCHECK(value.IsConstant()) << value;
3221 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3222 __ jmp(slow_path->GetEntryLabel());
3223 }
3224 }
3225 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003226 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003227 case Primitive::kPrimLong: {
3228 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003230 __ j(kEqual, slow_path->GetEntryLabel());
3231 } else if (value.IsDoubleStackSlot()) {
3232 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3233 __ j(kEqual, slow_path->GetEntryLabel());
3234 } else {
3235 DCHECK(value.IsConstant()) << value;
3236 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3237 __ jmp(slow_path->GetEntryLabel());
3238 }
3239 }
3240 break;
3241 }
3242 default:
3243 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003244 }
Calin Juravled0d48522014-11-04 16:40:20 +00003245}
3246
Calin Juravle9aec02f2014-11-18 23:06:35 +00003247void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3248 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3249
3250 LocationSummary* locations =
3251 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3252
3253 switch (op->GetResultType()) {
3254 case Primitive::kPrimInt:
3255 case Primitive::kPrimLong: {
3256 locations->SetInAt(0, Location::RequiresRegister());
3257 // The shift count needs to be in CL.
3258 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3259 locations->SetOut(Location::SameAsFirstInput());
3260 break;
3261 }
3262 default:
3263 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3264 }
3265}
3266
3267void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3268 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3269
3270 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003271 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003272 Location second = locations->InAt(1);
3273
3274 switch (op->GetResultType()) {
3275 case Primitive::kPrimInt: {
3276 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003277 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003278 if (op->IsShl()) {
3279 __ shll(first_reg, second_reg);
3280 } else if (op->IsShr()) {
3281 __ sarl(first_reg, second_reg);
3282 } else {
3283 __ shrl(first_reg, second_reg);
3284 }
3285 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003286 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003287 if (op->IsShl()) {
3288 __ shll(first_reg, imm);
3289 } else if (op->IsShr()) {
3290 __ sarl(first_reg, imm);
3291 } else {
3292 __ shrl(first_reg, imm);
3293 }
3294 }
3295 break;
3296 }
3297 case Primitive::kPrimLong: {
3298 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003299 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003300 if (op->IsShl()) {
3301 __ shlq(first_reg, second_reg);
3302 } else if (op->IsShr()) {
3303 __ sarq(first_reg, second_reg);
3304 } else {
3305 __ shrq(first_reg, second_reg);
3306 }
3307 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003308 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003309 if (op->IsShl()) {
3310 __ shlq(first_reg, imm);
3311 } else if (op->IsShr()) {
3312 __ sarq(first_reg, imm);
3313 } else {
3314 __ shrq(first_reg, imm);
3315 }
3316 }
3317 break;
3318 }
3319 default:
3320 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3321 }
3322}
3323
3324void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3325 HandleShift(shl);
3326}
3327
3328void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3329 HandleShift(shl);
3330}
3331
3332void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3333 HandleShift(shr);
3334}
3335
3336void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3337 HandleShift(shr);
3338}
3339
3340void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3341 HandleShift(ushr);
3342}
3343
3344void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3345 HandleShift(ushr);
3346}
3347
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003348void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003349 LocationSummary* locations =
3350 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003351 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003352 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003353 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003354 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003355}
3356
3357void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3358 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003359 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3360 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003361 // Note: if heap poisoning is enabled, the entry point takes cares
3362 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003363
3364 codegen_->InvokeRuntime(
3365 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3366 instruction,
3367 instruction->GetDexPc(),
3368 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003369
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003370 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003371}
3372
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003373void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3374 LocationSummary* locations =
3375 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3376 InvokeRuntimeCallingConvention calling_convention;
3377 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003378 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003379 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003380 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003381}
3382
3383void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3384 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003385 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3386 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003387
Roland Levillain4d027112015-07-01 15:41:14 +01003388 // Note: if heap poisoning is enabled, the entry point takes cares
3389 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003390 codegen_->InvokeRuntime(
3391 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3392 instruction,
3393 instruction->GetDexPc(),
3394 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003395
3396 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003397}
3398
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003399void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003400 LocationSummary* locations =
3401 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003402 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3403 if (location.IsStackSlot()) {
3404 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3405 } else if (location.IsDoubleStackSlot()) {
3406 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3407 }
3408 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003409}
3410
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003411void InstructionCodeGeneratorX86_64::VisitParameterValue(
3412 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003413 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003414}
3415
3416void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3417 LocationSummary* locations =
3418 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3419 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3420}
3421
3422void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3423 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3424 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003425}
3426
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003427void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003428 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003429 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003430 locations->SetInAt(0, Location::RequiresRegister());
3431 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003432}
3433
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003434void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3435 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003436 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3437 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003438 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003439 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003440 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003441 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003442 break;
3443
3444 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003445 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003446 break;
3447
3448 default:
3449 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3450 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003451}
3452
David Brazdil66d126e2015-04-03 16:02:44 +01003453void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3454 LocationSummary* locations =
3455 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3456 locations->SetInAt(0, Location::RequiresRegister());
3457 locations->SetOut(Location::SameAsFirstInput());
3458}
3459
3460void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003461 LocationSummary* locations = bool_not->GetLocations();
3462 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3463 locations->Out().AsRegister<CpuRegister>().AsRegister());
3464 Location out = locations->Out();
3465 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3466}
3467
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003468void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003469 LocationSummary* locations =
3470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003471 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3472 locations->SetInAt(i, Location::Any());
3473 }
3474 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003475}
3476
3477void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003478 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003479 LOG(FATAL) << "Unimplemented";
3480}
3481
Calin Juravle52c48962014-12-16 17:02:57 +00003482void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3483 /*
3484 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3485 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3486 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3487 */
3488 switch (kind) {
3489 case MemBarrierKind::kAnyAny: {
3490 __ mfence();
3491 break;
3492 }
3493 case MemBarrierKind::kAnyStore:
3494 case MemBarrierKind::kLoadAny:
3495 case MemBarrierKind::kStoreStore: {
3496 // nop
3497 break;
3498 }
3499 default:
3500 LOG(FATAL) << "Unexpected memory barier " << kind;
3501 }
3502}
3503
3504void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3505 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3506
Nicolas Geoffray39468442014-09-02 15:17:15 +01003507 LocationSummary* locations =
3508 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003509 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003510 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3511 locations->SetOut(Location::RequiresFpuRegister());
3512 } else {
3513 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3514 }
Calin Juravle52c48962014-12-16 17:02:57 +00003515}
3516
3517void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3518 const FieldInfo& field_info) {
3519 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3520
3521 LocationSummary* locations = instruction->GetLocations();
3522 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3523 Location out = locations->Out();
3524 bool is_volatile = field_info.IsVolatile();
3525 Primitive::Type field_type = field_info.GetFieldType();
3526 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3527
3528 switch (field_type) {
3529 case Primitive::kPrimBoolean: {
3530 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3531 break;
3532 }
3533
3534 case Primitive::kPrimByte: {
3535 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3536 break;
3537 }
3538
3539 case Primitive::kPrimShort: {
3540 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3541 break;
3542 }
3543
3544 case Primitive::kPrimChar: {
3545 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3546 break;
3547 }
3548
3549 case Primitive::kPrimInt:
3550 case Primitive::kPrimNot: {
3551 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3552 break;
3553 }
3554
3555 case Primitive::kPrimLong: {
3556 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3557 break;
3558 }
3559
3560 case Primitive::kPrimFloat: {
3561 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3562 break;
3563 }
3564
3565 case Primitive::kPrimDouble: {
3566 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3567 break;
3568 }
3569
3570 case Primitive::kPrimVoid:
3571 LOG(FATAL) << "Unreachable type " << field_type;
3572 UNREACHABLE();
3573 }
3574
Calin Juravle77520bc2015-01-12 18:45:46 +00003575 codegen_->MaybeRecordImplicitNullCheck(instruction);
3576
Calin Juravle52c48962014-12-16 17:02:57 +00003577 if (is_volatile) {
3578 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3579 }
Roland Levillain4d027112015-07-01 15:41:14 +01003580
3581 if (field_type == Primitive::kPrimNot) {
3582 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3583 }
Calin Juravle52c48962014-12-16 17:02:57 +00003584}
3585
3586void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3587 const FieldInfo& field_info) {
3588 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3589
3590 LocationSummary* locations =
3591 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003592 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003593 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003594 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003595
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003596 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003597 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3598 locations->SetInAt(1, Location::RequiresFpuRegister());
3599 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003600 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003601 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003602 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003603 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003604 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003605 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003606 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3607 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003608 locations->AddTemp(Location::RequiresRegister());
3609 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003610}
3611
Calin Juravle52c48962014-12-16 17:02:57 +00003612void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003613 const FieldInfo& field_info,
3614 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003615 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3616
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003617 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003618 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3619 Location value = locations->InAt(1);
3620 bool is_volatile = field_info.IsVolatile();
3621 Primitive::Type field_type = field_info.GetFieldType();
3622 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3623
3624 if (is_volatile) {
3625 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3626 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003627
3628 switch (field_type) {
3629 case Primitive::kPrimBoolean:
3630 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003631 if (value.IsConstant()) {
3632 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3633 __ movb(Address(base, offset), Immediate(v));
3634 } else {
3635 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3636 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003637 break;
3638 }
3639
3640 case Primitive::kPrimShort:
3641 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003642 if (value.IsConstant()) {
3643 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3644 __ movw(Address(base, offset), Immediate(v));
3645 } else {
3646 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3647 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003648 break;
3649 }
3650
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003651 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003652 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003653 if (value.IsConstant()) {
3654 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003655 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3656 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3657 // Note: if heap poisoning is enabled, no need to poison
3658 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003659 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003660 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003661 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3662 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3663 __ movl(temp, value.AsRegister<CpuRegister>());
3664 __ PoisonHeapReference(temp);
3665 __ movl(Address(base, offset), temp);
3666 } else {
3667 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3668 }
Mark Mendell40741f32015-04-20 22:10:34 -04003669 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003670 break;
3671 }
3672
3673 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003674 if (value.IsConstant()) {
3675 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3676 DCHECK(IsInt<32>(v));
3677 int32_t v_32 = v;
3678 __ movq(Address(base, offset), Immediate(v_32));
3679 } else {
3680 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3681 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003682 break;
3683 }
3684
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003685 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003686 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003687 break;
3688 }
3689
3690 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003691 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003692 break;
3693 }
3694
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003695 case Primitive::kPrimVoid:
3696 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003697 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003698 }
Calin Juravle52c48962014-12-16 17:02:57 +00003699
Calin Juravle77520bc2015-01-12 18:45:46 +00003700 codegen_->MaybeRecordImplicitNullCheck(instruction);
3701
3702 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3703 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3704 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003705 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003706 }
3707
Calin Juravle52c48962014-12-16 17:02:57 +00003708 if (is_volatile) {
3709 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3710 }
3711}
3712
3713void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3714 HandleFieldSet(instruction, instruction->GetFieldInfo());
3715}
3716
3717void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003718 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003719}
3720
3721void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003722 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003723}
3724
3725void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003726 HandleFieldGet(instruction, instruction->GetFieldInfo());
3727}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003728
Calin Juravle52c48962014-12-16 17:02:57 +00003729void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3730 HandleFieldGet(instruction);
3731}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003732
Calin Juravle52c48962014-12-16 17:02:57 +00003733void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3734 HandleFieldGet(instruction, instruction->GetFieldInfo());
3735}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003736
Calin Juravle52c48962014-12-16 17:02:57 +00003737void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3738 HandleFieldSet(instruction, instruction->GetFieldInfo());
3739}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003740
Calin Juravle52c48962014-12-16 17:02:57 +00003741void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003742 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003743}
3744
3745void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003746 LocationSummary* locations =
3747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003748 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3749 ? Location::RequiresRegister()
3750 : Location::Any();
3751 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003752 if (instruction->HasUses()) {
3753 locations->SetOut(Location::SameAsFirstInput());
3754 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003755}
3756
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003757void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003758 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3759 return;
3760 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003761 LocationSummary* locations = instruction->GetLocations();
3762 Location obj = locations->InAt(0);
3763
3764 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3765 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3766}
3767
3768void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003769 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003770 codegen_->AddSlowPath(slow_path);
3771
3772 LocationSummary* locations = instruction->GetLocations();
3773 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003774
3775 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003776 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003777 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003778 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003779 } else {
3780 DCHECK(obj.IsConstant()) << obj;
3781 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3782 __ jmp(slow_path->GetEntryLabel());
3783 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003784 }
3785 __ j(kEqual, slow_path->GetEntryLabel());
3786}
3787
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003788void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3789 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3790 GenerateImplicitNullCheck(instruction);
3791 } else {
3792 GenerateExplicitNullCheck(instruction);
3793 }
3794}
3795
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003797 LocationSummary* locations =
3798 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003799 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003800 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003801 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3802 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3803 } else {
3804 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3805 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806}
3807
3808void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3809 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003810 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003811 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003812 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003813
Roland Levillain4d027112015-07-01 15:41:14 +01003814 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003815 case Primitive::kPrimBoolean: {
3816 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003817 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003818 if (index.IsConstant()) {
3819 __ movzxb(out, Address(obj,
3820 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3821 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003822 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003823 }
3824 break;
3825 }
3826
3827 case Primitive::kPrimByte: {
3828 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003829 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003830 if (index.IsConstant()) {
3831 __ movsxb(out, Address(obj,
3832 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3833 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003834 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003835 }
3836 break;
3837 }
3838
3839 case Primitive::kPrimShort: {
3840 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003841 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003842 if (index.IsConstant()) {
3843 __ movsxw(out, Address(obj,
3844 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3845 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003846 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003847 }
3848 break;
3849 }
3850
3851 case Primitive::kPrimChar: {
3852 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003853 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003854 if (index.IsConstant()) {
3855 __ movzxw(out, Address(obj,
3856 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3857 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003858 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003859 }
3860 break;
3861 }
3862
3863 case Primitive::kPrimInt:
3864 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003865 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3866 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003867 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003869 if (index.IsConstant()) {
3870 __ movl(out, Address(obj,
3871 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3872 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003873 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003874 }
3875 break;
3876 }
3877
3878 case Primitive::kPrimLong: {
3879 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003880 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003881 if (index.IsConstant()) {
3882 __ movq(out, Address(obj,
3883 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3884 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003885 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003886 }
3887 break;
3888 }
3889
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003890 case Primitive::kPrimFloat: {
3891 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003892 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003893 if (index.IsConstant()) {
3894 __ movss(out, Address(obj,
3895 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3896 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003897 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003898 }
3899 break;
3900 }
3901
3902 case Primitive::kPrimDouble: {
3903 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003904 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003905 if (index.IsConstant()) {
3906 __ movsd(out, Address(obj,
3907 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3908 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003909 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003910 }
3911 break;
3912 }
3913
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003914 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003915 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003916 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003917 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003918 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003919
3920 if (type == Primitive::kPrimNot) {
3921 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3922 __ MaybeUnpoisonHeapReference(out);
3923 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003924}
3925
3926void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003927 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003928
3929 bool needs_write_barrier =
3930 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3931 bool needs_runtime_call = instruction->NeedsTypeCheck();
3932
Nicolas Geoffray39468442014-09-02 15:17:15 +01003933 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003934 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3935 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003936 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003937 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3938 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3939 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003940 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003941 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003942 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003943 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3944 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003945 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003946 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003947 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3948 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003949 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003950 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003951 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003952
3953 if (needs_write_barrier) {
3954 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003955 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003956 locations->AddTemp(Location::RequiresRegister());
3957 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003958 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003959}
3960
3961void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3962 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003963 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003964 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003965 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003966 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003967 bool needs_runtime_call = locations->WillCall();
3968 bool needs_write_barrier =
3969 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003970
3971 switch (value_type) {
3972 case Primitive::kPrimBoolean:
3973 case Primitive::kPrimByte: {
3974 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003975 if (index.IsConstant()) {
3976 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003977 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003978 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003979 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003980 __ movb(Address(obj, offset),
3981 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003982 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003983 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003984 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003985 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3986 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003987 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003988 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003989 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3990 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003991 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003992 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003993 break;
3994 }
3995
3996 case Primitive::kPrimShort:
3997 case Primitive::kPrimChar: {
3998 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003999 if (index.IsConstant()) {
4000 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004001 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004002 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004003 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004004 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00004005 __ movw(Address(obj, offset),
4006 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004007 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004008 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004009 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004010 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004011 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
4012 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004013 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004014 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00004015 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004016 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4017 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004018 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004019 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004020 break;
4021 }
4022
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004023 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004024 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004025 if (!needs_runtime_call) {
4026 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4027 if (index.IsConstant()) {
4028 size_t offset =
4029 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4030 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004031 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4032 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4033 __ movl(temp, value.AsRegister<CpuRegister>());
4034 __ PoisonHeapReference(temp);
4035 __ movl(Address(obj, offset), temp);
4036 } else {
4037 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
4038 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004039 } else {
4040 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004041 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004042 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4043 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4044 // Note: if heap poisoning is enabled, no need to poison
4045 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04004046 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004047 }
4048 } else {
4049 DCHECK(index.IsRegister()) << index;
4050 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004051 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4052 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4053 __ movl(temp, value.AsRegister<CpuRegister>());
4054 __ PoisonHeapReference(temp);
4055 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4056 } else {
4057 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4058 value.AsRegister<CpuRegister>());
4059 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004060 } else {
4061 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004062 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004063 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4064 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4065 // Note: if heap poisoning is enabled, no need to poison
4066 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004067 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004068 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004069 }
4070 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004071 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004072 if (needs_write_barrier) {
4073 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004074 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4075 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004076 codegen_->MarkGCCard(
4077 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004078 }
4079 } else {
4080 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004081 // Note: if heap poisoning is enabled, pAputObject takes cares
4082 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004083 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4084 instruction,
4085 instruction->GetDexPc(),
4086 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004087 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004088 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004089 break;
4090 }
4091
4092 case Primitive::kPrimLong: {
4093 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004094 if (index.IsConstant()) {
4095 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004096 if (value.IsRegister()) {
4097 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4098 } else {
4099 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4100 DCHECK(IsInt<32>(v));
4101 int32_t v_32 = v;
4102 __ movq(Address(obj, offset), Immediate(v_32));
4103 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004104 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004105 if (value.IsRegister()) {
4106 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4107 value.AsRegister<CpuRegister>());
4108 } else {
4109 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4110 DCHECK(IsInt<32>(v));
4111 int32_t v_32 = v;
4112 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4113 Immediate(v_32));
4114 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004115 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004116 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004117 break;
4118 }
4119
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004120 case Primitive::kPrimFloat: {
4121 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4122 if (index.IsConstant()) {
4123 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4124 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004125 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004126 } else {
4127 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004128 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4129 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004130 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004131 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004132 break;
4133 }
4134
4135 case Primitive::kPrimDouble: {
4136 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4137 if (index.IsConstant()) {
4138 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4139 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004140 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004141 } else {
4142 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004143 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4144 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004145 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004146 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004147 break;
4148 }
4149
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004150 case Primitive::kPrimVoid:
4151 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004152 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004153 }
4154}
4155
4156void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004157 LocationSummary* locations =
4158 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004159 locations->SetInAt(0, Location::RequiresRegister());
4160 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004161}
4162
4163void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4164 LocationSummary* locations = instruction->GetLocations();
4165 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004166 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4167 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004168 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004169 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004170}
4171
4172void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004173 LocationSummary* locations =
4174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004175 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004176 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004177 if (instruction->HasUses()) {
4178 locations->SetOut(Location::SameAsFirstInput());
4179 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004180}
4181
4182void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4183 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004184 Location index_loc = locations->InAt(0);
4185 Location length_loc = locations->InAt(1);
4186 SlowPathCodeX86_64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004187 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004188
Mark Mendell99dbd682015-04-22 16:18:52 -04004189 if (length_loc.IsConstant()) {
4190 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4191 if (index_loc.IsConstant()) {
4192 // BCE will remove the bounds check if we are guarenteed to pass.
4193 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4194 if (index < 0 || index >= length) {
4195 codegen_->AddSlowPath(slow_path);
4196 __ jmp(slow_path->GetEntryLabel());
4197 } else {
4198 // Some optimization after BCE may have generated this, and we should not
4199 // generate a bounds check if it is a valid range.
4200 }
4201 return;
4202 }
4203
4204 // We have to reverse the jump condition because the length is the constant.
4205 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4206 __ cmpl(index_reg, Immediate(length));
4207 codegen_->AddSlowPath(slow_path);
4208 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004209 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004210 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4211 if (index_loc.IsConstant()) {
4212 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4213 __ cmpl(length, Immediate(value));
4214 } else {
4215 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4216 }
4217 codegen_->AddSlowPath(slow_path);
4218 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004219 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004220}
4221
4222void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4223 CpuRegister card,
4224 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004225 CpuRegister value,
4226 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004227 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004228 if (value_can_be_null) {
4229 __ testl(value, value);
4230 __ j(kEqual, &is_null);
4231 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232 __ gs()->movq(card, Address::Absolute(
4233 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4234 __ movq(temp, object);
4235 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004236 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004237 if (value_can_be_null) {
4238 __ Bind(&is_null);
4239 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240}
4241
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004242void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4243 temp->SetLocations(nullptr);
4244}
4245
4246void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4247 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004248 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004249}
4250
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004251void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004252 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004253 LOG(FATAL) << "Unimplemented";
4254}
4255
4256void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004257 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4258}
4259
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004260void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4261 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4262}
4263
4264void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004265 HBasicBlock* block = instruction->GetBlock();
4266 if (block->GetLoopInformation() != nullptr) {
4267 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4268 // The back edge will generate the suspend check.
4269 return;
4270 }
4271 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4272 // The goto will generate the suspend check.
4273 return;
4274 }
4275 GenerateSuspendCheck(instruction, nullptr);
4276}
4277
4278void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4279 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004280 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004281 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4282 if (slow_path == nullptr) {
4283 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4284 instruction->SetSlowPath(slow_path);
4285 codegen_->AddSlowPath(slow_path);
4286 if (successor != nullptr) {
4287 DCHECK(successor->IsLoopHeader());
4288 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4289 }
4290 } else {
4291 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4292 }
4293
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004294 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004295 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004296 if (successor == nullptr) {
4297 __ j(kNotEqual, slow_path->GetEntryLabel());
4298 __ Bind(slow_path->GetReturnLabel());
4299 } else {
4300 __ j(kEqual, codegen_->GetLabelOf(successor));
4301 __ jmp(slow_path->GetEntryLabel());
4302 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004303}
4304
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004305X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4306 return codegen_->GetAssembler();
4307}
4308
4309void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4310 MoveOperands* move = moves_.Get(index);
4311 Location source = move->GetSource();
4312 Location destination = move->GetDestination();
4313
4314 if (source.IsRegister()) {
4315 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004316 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004317 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004318 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004319 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004320 } else {
4321 DCHECK(destination.IsDoubleStackSlot());
4322 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004323 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004324 }
4325 } else if (source.IsStackSlot()) {
4326 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004327 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004328 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004329 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004330 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004331 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004332 } else {
4333 DCHECK(destination.IsStackSlot());
4334 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4335 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4336 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004337 } else if (source.IsDoubleStackSlot()) {
4338 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004339 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004340 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004341 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004342 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4343 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004344 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004345 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004346 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4347 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4348 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004349 } else if (source.IsConstant()) {
4350 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004351 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4352 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004353 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004354 if (value == 0) {
4355 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4356 } else {
4357 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4358 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004359 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004360 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004361 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004362 }
4363 } else if (constant->IsLongConstant()) {
4364 int64_t value = constant->AsLongConstant()->GetValue();
4365 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004366 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004367 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004368 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004369 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004370 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004371 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004372 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004373 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004374 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004375 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4376 if (value == 0) {
4377 // easy FP 0.0.
4378 __ xorps(dest, dest);
4379 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004380 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004381 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004382 } else {
4383 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004384 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004385 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4386 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004387 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004388 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004389 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004390 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004391 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004392 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4393 if (value == 0) {
4394 __ xorpd(dest, dest);
4395 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004396 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004397 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004398 } else {
4399 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004400 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004401 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004402 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004403 } else if (source.IsFpuRegister()) {
4404 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004405 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004406 } else if (destination.IsStackSlot()) {
4407 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004408 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004409 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004410 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004411 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004412 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004413 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004414 }
4415}
4416
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004417void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004418 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004419 __ movl(Address(CpuRegister(RSP), mem), reg);
4420 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004421}
4422
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004423void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004424 ScratchRegisterScope ensure_scratch(
4425 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4426
4427 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4428 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4429 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4430 Address(CpuRegister(RSP), mem2 + stack_offset));
4431 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4432 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4433 CpuRegister(ensure_scratch.GetRegister()));
4434}
4435
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004436void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4437 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4438 __ movq(Address(CpuRegister(RSP), mem), reg);
4439 __ movq(reg, CpuRegister(TMP));
4440}
4441
4442void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4443 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004444 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004445
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004446 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4447 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4448 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4449 Address(CpuRegister(RSP), mem2 + stack_offset));
4450 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4451 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4452 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004453}
4454
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004455void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4456 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4457 __ movss(Address(CpuRegister(RSP), mem), reg);
4458 __ movd(reg, CpuRegister(TMP));
4459}
4460
4461void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4462 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4463 __ movsd(Address(CpuRegister(RSP), mem), reg);
4464 __ movd(reg, CpuRegister(TMP));
4465}
4466
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004467void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4468 MoveOperands* move = moves_.Get(index);
4469 Location source = move->GetSource();
4470 Location destination = move->GetDestination();
4471
4472 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004473 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004474 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004475 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004476 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004477 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004478 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004479 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4480 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004481 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004482 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004483 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004484 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4485 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004486 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004487 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4488 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4489 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004490 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004491 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004492 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004493 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004494 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004495 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004496 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004497 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004498 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004499 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004500 }
4501}
4502
4503
4504void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4505 __ pushq(CpuRegister(reg));
4506}
4507
4508
4509void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4510 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004511}
4512
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004513void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4514 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4515 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4516 Immediate(mirror::Class::kStatusInitialized));
4517 __ j(kLess, slow_path->GetEntryLabel());
4518 __ Bind(slow_path->GetExitLabel());
4519 // No need for memory fence, thanks to the X86_64 memory model.
4520}
4521
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004522void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004523 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4524 ? LocationSummary::kCallOnSlowPath
4525 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004526 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004527 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004528 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004529 locations->SetOut(Location::RequiresRegister());
4530}
4531
4532void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004533 LocationSummary* locations = cls->GetLocations();
4534 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4535 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004536 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004537 DCHECK(!cls->CanCallRuntime());
4538 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004539 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004540 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004541 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004542 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004543 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004544 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004545 __ MaybeUnpoisonHeapReference(out);
4546
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004547 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4548 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4549 codegen_->AddSlowPath(slow_path);
4550 __ testl(out, out);
4551 __ j(kEqual, slow_path->GetEntryLabel());
4552 if (cls->MustGenerateClinitCheck()) {
4553 GenerateClassInitializationCheck(slow_path, out);
4554 } else {
4555 __ Bind(slow_path->GetExitLabel());
4556 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004557 }
4558}
4559
4560void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4561 LocationSummary* locations =
4562 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4563 locations->SetInAt(0, Location::RequiresRegister());
4564 if (check->HasUses()) {
4565 locations->SetOut(Location::SameAsFirstInput());
4566 }
4567}
4568
4569void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004570 // We assume the class to not be null.
4571 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4572 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004573 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004574 GenerateClassInitializationCheck(slow_path,
4575 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004576}
4577
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004578void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4579 LocationSummary* locations =
4580 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004581 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004582 locations->SetOut(Location::RequiresRegister());
4583}
4584
4585void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4586 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4587 codegen_->AddSlowPath(slow_path);
4588
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004589 LocationSummary* locations = load->GetLocations();
4590 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4591 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004592 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004593 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004594 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004595 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004596 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004597 __ testl(out, out);
4598 __ j(kEqual, slow_path->GetEntryLabel());
4599 __ Bind(slow_path->GetExitLabel());
4600}
4601
David Brazdilcb1c0552015-08-04 16:22:25 +01004602static Address GetExceptionTlsAddress() {
4603 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4604}
4605
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004606void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4607 LocationSummary* locations =
4608 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4609 locations->SetOut(Location::RequiresRegister());
4610}
4611
4612void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004613 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4614}
4615
4616void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4617 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4618}
4619
4620void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4621 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004622}
4623
4624void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4625 LocationSummary* locations =
4626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4627 InvokeRuntimeCallingConvention calling_convention;
4628 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4629}
4630
4631void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004632 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4633 instruction,
4634 instruction->GetDexPc(),
4635 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004636}
4637
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004638void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004639 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4640 ? LocationSummary::kNoCall
4641 : LocationSummary::kCallOnSlowPath;
4642 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4643 locations->SetInAt(0, Location::RequiresRegister());
4644 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004645 // Note that TypeCheckSlowPathX86_64 uses this register too.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004646 locations->SetOut(Location::RequiresRegister());
4647}
4648
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004649void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004650 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004651 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004652 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004653 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004654 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4655 Label done, zero;
4656 SlowPathCodeX86_64* slow_path = nullptr;
4657
4658 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004659 // Avoid null check if we know obj is not null.
4660 if (instruction->MustDoNullCheck()) {
4661 __ testl(obj, obj);
4662 __ j(kEqual, &zero);
4663 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004664 // Compare the class of `obj` with `cls`.
4665 __ movl(out, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004666 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004667 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004668 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004669 } else {
4670 DCHECK(cls.IsStackSlot()) << cls;
4671 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4672 }
4673 if (instruction->IsClassFinal()) {
4674 // Classes must be equal for the instanceof to succeed.
4675 __ j(kNotEqual, &zero);
4676 __ movl(out, Immediate(1));
4677 __ jmp(&done);
4678 } else {
4679 // If the classes are not equal, we go into a slow path.
4680 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004681 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004682 codegen_->AddSlowPath(slow_path);
4683 __ j(kNotEqual, slow_path->GetEntryLabel());
4684 __ movl(out, Immediate(1));
4685 __ jmp(&done);
4686 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004687
4688 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4689 __ Bind(&zero);
4690 __ movl(out, Immediate(0));
4691 }
4692
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004693 if (slow_path != nullptr) {
4694 __ Bind(slow_path->GetExitLabel());
4695 }
4696 __ Bind(&done);
4697}
4698
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004699void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4700 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4701 instruction, LocationSummary::kCallOnSlowPath);
4702 locations->SetInAt(0, Location::RequiresRegister());
4703 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004704 // Note that TypeCheckSlowPathX86_64 uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004705 locations->AddTemp(Location::RequiresRegister());
4706}
4707
4708void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4709 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004710 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004711 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004712 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004713 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004714 SlowPathCodeX86_64* slow_path =
4715 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004716 codegen_->AddSlowPath(slow_path);
4717
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004718 // Avoid null check if we know obj is not null.
4719 if (instruction->MustDoNullCheck()) {
4720 __ testl(obj, obj);
4721 __ j(kEqual, slow_path->GetExitLabel());
4722 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004723 // Compare the class of `obj` with `cls`.
4724 __ movl(temp, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004725 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004726 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004727 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004728 } else {
4729 DCHECK(cls.IsStackSlot()) << cls;
4730 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4731 }
Roland Levillain4d027112015-07-01 15:41:14 +01004732 // The checkcast succeeds if the classes are equal (fast path).
4733 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004734 __ j(kNotEqual, slow_path->GetEntryLabel());
4735 __ Bind(slow_path->GetExitLabel());
4736}
4737
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004738void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4739 LocationSummary* locations =
4740 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4741 InvokeRuntimeCallingConvention calling_convention;
4742 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4743}
4744
4745void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004746 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4747 : QUICK_ENTRY_POINT(pUnlockObject),
4748 instruction,
4749 instruction->GetDexPc(),
4750 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004751}
4752
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004753void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4754void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4755void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4756
4757void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4758 LocationSummary* locations =
4759 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4760 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4761 || instruction->GetResultType() == Primitive::kPrimLong);
4762 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004763 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004764 locations->SetOut(Location::SameAsFirstInput());
4765}
4766
4767void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4768 HandleBitwiseOperation(instruction);
4769}
4770
4771void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4772 HandleBitwiseOperation(instruction);
4773}
4774
4775void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4776 HandleBitwiseOperation(instruction);
4777}
4778
4779void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4780 LocationSummary* locations = instruction->GetLocations();
4781 Location first = locations->InAt(0);
4782 Location second = locations->InAt(1);
4783 DCHECK(first.Equals(locations->Out()));
4784
4785 if (instruction->GetResultType() == Primitive::kPrimInt) {
4786 if (second.IsRegister()) {
4787 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004788 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004789 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004790 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004791 } else {
4792 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004793 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004794 }
4795 } else if (second.IsConstant()) {
4796 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4797 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004798 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004799 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004800 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004801 } else {
4802 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004803 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004804 }
4805 } else {
4806 Address address(CpuRegister(RSP), second.GetStackIndex());
4807 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004808 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004809 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004810 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004811 } else {
4812 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004813 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004814 }
4815 }
4816 } else {
4817 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004818 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4819 bool second_is_constant = false;
4820 int64_t value = 0;
4821 if (second.IsConstant()) {
4822 second_is_constant = true;
4823 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004824 }
Mark Mendell40741f32015-04-20 22:10:34 -04004825 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004826
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004827 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004828 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004829 if (is_int32_value) {
4830 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4831 } else {
4832 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4833 }
4834 } else if (second.IsDoubleStackSlot()) {
4835 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004836 } else {
4837 __ andq(first_reg, second.AsRegister<CpuRegister>());
4838 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004839 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004840 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004841 if (is_int32_value) {
4842 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4843 } else {
4844 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4845 }
4846 } else if (second.IsDoubleStackSlot()) {
4847 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004848 } else {
4849 __ orq(first_reg, second.AsRegister<CpuRegister>());
4850 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004851 } else {
4852 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004853 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004854 if (is_int32_value) {
4855 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4856 } else {
4857 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4858 }
4859 } else if (second.IsDoubleStackSlot()) {
4860 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004861 } else {
4862 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4863 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004864 }
4865 }
4866}
4867
Calin Juravleb1498f62015-02-16 13:13:29 +00004868void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4869 // Nothing to do, this should be removed during prepare for register allocator.
4870 UNUSED(instruction);
4871 LOG(FATAL) << "Unreachable";
4872}
4873
4874void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4875 // Nothing to do, this should be removed during prepare for register allocator.
4876 UNUSED(instruction);
4877 LOG(FATAL) << "Unreachable";
4878}
4879
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004880void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
4881 DCHECK(codegen_->IsBaseline());
4882 LocationSummary* locations =
4883 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4884 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4885}
4886
4887void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4888 DCHECK(codegen_->IsBaseline());
4889 // Will be generated at use site.
4890}
4891
Mark Mendell92e83bf2015-05-07 11:25:03 -04004892void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4893 if (value == 0) {
4894 __ xorl(dest, dest);
4895 } else if (value > 0 && IsInt<32>(value)) {
4896 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4897 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4898 } else {
4899 __ movq(dest, Immediate(value));
4900 }
4901}
4902
Mark Mendellcfa410b2015-05-25 16:02:44 -04004903void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
4904 DCHECK(dest.IsDoubleStackSlot());
4905 if (IsInt<32>(value)) {
4906 // Can move directly as an int32 constant.
4907 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
4908 Immediate(static_cast<int32_t>(value)));
4909 } else {
4910 Load64BitValue(CpuRegister(TMP), value);
4911 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
4912 }
4913}
4914
Mark Mendellf55c3e02015-03-26 21:07:46 -04004915void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4916 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004917 X86_64Assembler* assembler = GetAssembler();
4918 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004919 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4920 // byte values. If used for vectors at a later time, this will need to be
4921 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004922 assembler->Align(4, 0);
4923 constant_area_start_ = assembler->CodeSize();
4924 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004925 }
4926
4927 // And finish up.
4928 CodeGenerator::Finalize(allocator);
4929}
4930
4931/**
4932 * Class to handle late fixup of offsets into constant area.
4933 */
4934class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4935 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004936 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004937 : codegen_(codegen), offset_into_constant_area_(offset) {}
4938
4939 private:
4940 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4941 // Patch the correct offset for the instruction. We use the address of the
4942 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4943 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4944 int relative_position = constant_offset - pos;
4945
4946 // Patch in the right value.
4947 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4948 }
4949
Mark Mendell39dcf552015-04-09 20:42:42 -04004950 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004951
4952 // Location in constant area that the fixup refers to.
4953 int offset_into_constant_area_;
4954};
4955
4956Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4957 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4958 return Address::RIP(fixup);
4959}
4960
4961Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4962 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4963 return Address::RIP(fixup);
4964}
4965
4966Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4967 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4968 return Address::RIP(fixup);
4969}
4970
4971Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4972 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4973 return Address::RIP(fixup);
4974}
4975
Roland Levillain4d027112015-07-01 15:41:14 +01004976#undef __
4977
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004978} // namespace x86_64
4979} // namespace art