blob: 42f3d82e2587990ddccce66a597f093886261e98 [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 Marko9b688a02015-05-06 14:12:42 +010021#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:
Roland Levillain5799fc02014-09-25 12:15:20 +0100173 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
174 Location index_location,
175 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100176 : instruction_(instruction),
177 index_location_(index_location),
178 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000180 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100181 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100182 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000183 // We're moving two locations to locations that could overlap, so we need a parallel
184 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000186 codegen->EmitParallelMoves(
187 index_location_,
188 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100189 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000190 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100191 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
192 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100193 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
194 instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100195 }
196
Alexandre Rames8158f282015-08-07 10:26:17 +0100197 bool IsFatal() const OVERRIDE { return true; }
198
Alexandre Rames9931f312015-06-19 14:47:01 +0100199 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
200
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100201 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100202 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100203 const Location index_location_;
204 const Location length_location_;
205
206 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
207};
208
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100210 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211 LoadClassSlowPathX86_64(HLoadClass* cls,
212 HInstruction* at,
213 uint32_t dex_pc,
214 bool do_clinit)
215 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
216 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
217 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100218
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100221 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
222 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100223
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000224 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100226 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100228 x64_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
229 : QUICK_ENTRY_POINT(pInitializeType),
230 at_, dex_pc_, this);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100231
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000232 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000234 if (out.IsValid()) {
235 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
236 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 }
238
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000239 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240 __ jmp(GetExitLabel());
241 }
242
Alexandre Rames9931f312015-06-19 14:47:01 +0100243 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
244
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100245 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000246 // The class this slow path will load.
247 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100248
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 // The instruction where this slow path is happening.
250 // (Might be the load class or an initialization check).
251 HInstruction* const at_;
252
253 // The dex PC of `at_`.
254 const uint32_t dex_pc_;
255
256 // Whether to initialize the class.
257 const bool do_clinit_;
258
259 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100260};
261
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000262class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
263 public:
264 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
265
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000266 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000267 LocationSummary* locations = instruction_->GetLocations();
268 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
269
270 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
271 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000272 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000273
274 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800275 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000276 Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100277 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
278 instruction_,
279 instruction_->GetDexPc(),
280 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000281 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000282 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000283 __ jmp(GetExitLabel());
284 }
285
Alexandre Rames9931f312015-06-19 14:47:01 +0100286 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
287
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000288 private:
289 HLoadString* const instruction_;
290
291 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
292};
293
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
295 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 TypeCheckSlowPathX86_64(HInstruction* instruction,
297 Location class_to_check,
298 Location object_class,
299 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000301 class_to_check_(class_to_check),
302 object_class_(object_class),
303 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000305 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 DCHECK(instruction_->IsCheckCast()
308 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309
310 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
311 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000312 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313
314 // We're moving two locations to locations that could overlap, so we need a parallel
315 // move resolver.
316 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000317 codegen->EmitParallelMoves(
318 class_to_check_,
319 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100320 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000321 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100322 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
323 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100326 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
327 instruction_,
328 dex_pc_,
329 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 } else {
331 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100332 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
333 instruction_,
334 dex_pc_,
335 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000337
338 if (instruction_->IsInstanceOf()) {
339 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
340 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000341
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000342 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000343 __ jmp(GetExitLabel());
344 }
345
Alexandre Rames9931f312015-06-19 14:47:01 +0100346 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
347
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000349 HInstruction* const instruction_;
350 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000351 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000352 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353
354 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
355};
356
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700357class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
358 public:
359 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
360 : instruction_(instruction) {}
361
362 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100363 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700364 __ Bind(GetEntryLabel());
365 SaveLiveRegisters(codegen, instruction_->GetLocations());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700366 DCHECK(instruction_->IsDeoptimize());
367 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
Alexandre Rames8158f282015-08-07 10:26:17 +0100368 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
369 deoptimize,
370 deoptimize->GetDexPc(),
371 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700372 }
373
Alexandre Rames9931f312015-06-19 14:47:01 +0100374 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
375
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700376 private:
377 HInstruction* const instruction_;
378 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
379};
380
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100381#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100382#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100383
Roland Levillain4fa13f62015-07-06 18:11:54 +0100384inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700385 switch (cond) {
386 case kCondEQ: return kEqual;
387 case kCondNE: return kNotEqual;
388 case kCondLT: return kLess;
389 case kCondLE: return kLessEqual;
390 case kCondGT: return kGreater;
391 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700392 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100393 LOG(FATAL) << "Unreachable";
394 UNREACHABLE();
395}
396
397inline Condition X86_64FPCondition(IfCondition cond) {
398 switch (cond) {
399 case kCondEQ: return kEqual;
400 case kCondNE: return kNotEqual;
401 case kCondLT: return kBelow;
402 case kCondLE: return kBelowEqual;
403 case kCondGT: return kAbove;
404 case kCondGE: return kAboveEqual;
405 };
406 LOG(FATAL) << "Unreachable";
407 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700408}
409
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800410void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100411 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800412 // All registers are assumed to be correctly set up.
413
Vladimir Marko9b688a02015-05-06 14:12:42 +0100414 switch (invoke->GetMethodLoadKind()) {
415 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
416 // temp = thread->string_init_entrypoint
417 __ gs()->movl(temp.AsRegister<CpuRegister>(),
418 Address::Absolute(invoke->GetStringInitOffset(), true));
419 break;
420 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
421 // Nothing to do.
422 break;
423 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
424 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
425 break;
426 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
427 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
428 method_patches_.emplace_back(invoke->GetTargetMethod());
429 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
430 break;
431 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
432 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
433 invoke->GetDexCacheArrayOffset());
434 __ movq(temp.AsRegister<CpuRegister>(),
435 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
436 // Bind the label at the end of the "movl" insn.
437 __ Bind(&pc_rel_dex_cache_patches_.back().label);
438 break;
439 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
440 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
441 Register method_reg;
442 CpuRegister reg = temp.AsRegister<CpuRegister>();
443 if (current_method.IsRegister()) {
444 method_reg = current_method.AsRegister<Register>();
445 } else {
446 DCHECK(invoke->GetLocations()->Intrinsified());
447 DCHECK(!current_method.IsValid());
448 method_reg = reg.AsRegister();
449 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
450 }
451 // temp = temp->dex_cache_resolved_methods_;
452 __ movl(reg, Address(CpuRegister(method_reg),
453 ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
454 // temp = temp[index_in_cache]
455 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
456 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
457 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +0100458 }
Vladimir Marko9b688a02015-05-06 14:12:42 +0100459 }
460
461 switch (invoke->GetCodePtrLocation()) {
462 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
463 __ call(&frame_entry_label_);
464 break;
465 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
466 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
467 Label* label = &relative_call_patches_.back().label;
468 __ call(label); // Bind to the patch label, override at link time.
469 __ Bind(label); // Bind the label at the end of the "call" insn.
470 break;
471 }
472 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
473 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
474 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
475 FALLTHROUGH_INTENDED;
476 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
477 // (temp + offset_of_quick_compiled_code)()
478 __ call(Address(temp.AsRegister<CpuRegister>(),
479 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
480 kX86_64WordSize).SizeValue()));
481 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000482 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800483
484 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800485}
486
Vladimir Marko9b688a02015-05-06 14:12:42 +0100487void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
488 DCHECK(linker_patches->empty());
489 size_t size =
490 method_patches_.size() + relative_call_patches_.size() + pc_rel_dex_cache_patches_.size();
491 linker_patches->reserve(size);
492 for (const MethodPatchInfo<Label>& info : method_patches_) {
493 // The label points to the end of the "movl" instruction but the literal offset for method
494 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
495 uint32_t literal_offset = info.label.Position() - 4;
496 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
497 info.target_method.dex_file,
498 info.target_method.dex_method_index));
499 }
500 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
501 // The label points to the end of the "call" instruction but the literal offset for method
502 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
503 uint32_t literal_offset = info.label.Position() - 4;
504 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
505 info.target_method.dex_file,
506 info.target_method.dex_method_index));
507 }
508 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
509 // The label points to the end of the "mov" instruction but the literal offset for method
510 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
511 uint32_t literal_offset = info.label.Position() - 4;
512 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
513 &info.target_dex_file,
514 info.label.Position(),
515 info.element_offset));
516 }
517}
518
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100519void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100520 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100521}
522
523void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100524 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100525}
526
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100527size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
528 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
529 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100530}
531
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100532size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
533 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
534 return kX86_64WordSize;
535}
536
537size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
538 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
539 return kX86_64WordSize;
540}
541
542size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
543 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
544 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100545}
546
Alexandre Rames8158f282015-08-07 10:26:17 +0100547void CodeGeneratorX86_64::InvokeRuntime(Address entry_point,
548 HInstruction* instruction,
549 uint32_t dex_pc,
550 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100551 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100552 __ gs()->call(entry_point);
553 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100554}
555
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000556static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000557// Use a fake return address register to mimic Quick.
558static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400559CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
560 const X86_64InstructionSetFeatures& isa_features,
561 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000562 : CodeGenerator(graph,
563 kNumberOfCpuRegisters,
564 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000565 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000566 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
567 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000568 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000569 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
570 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000571 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100572 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100573 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000574 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400575 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400576 isa_features_(isa_features),
Vladimir Marko9b688a02015-05-06 14:12:42 +0100577 constant_area_start_(0),
578 method_patches_(graph->GetArena()->Adapter()),
579 relative_call_patches_(graph->GetArena()->Adapter()),
580 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000581 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
582}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100583
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100584InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
585 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100586 : HGraphVisitor(graph),
587 assembler_(codegen->GetAssembler()),
588 codegen_(codegen) {}
589
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100590Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591 switch (type) {
592 case Primitive::kPrimLong:
593 case Primitive::kPrimByte:
594 case Primitive::kPrimBoolean:
595 case Primitive::kPrimChar:
596 case Primitive::kPrimShort:
597 case Primitive::kPrimInt:
598 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100599 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100600 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100601 }
602
603 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100604 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100605 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100606 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100607 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100608
609 case Primitive::kPrimVoid:
610 LOG(FATAL) << "Unreachable type " << type;
611 }
612
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100613 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100614}
615
Nicolas Geoffray98893962015-01-21 12:32:32 +0000616void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100617 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100618 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100619
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000620 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100621 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000622
Nicolas Geoffray98893962015-01-21 12:32:32 +0000623 if (is_baseline) {
624 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
625 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
626 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000627 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
628 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
629 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000630 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100631}
632
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100633static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100634 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100635}
David Srbecky9d8606d2015-04-12 09:35:32 +0100636
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100637static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100638 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100639}
640
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100641void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100642 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000643 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100644 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700645 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000646 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100647
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000648 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100649 __ testq(CpuRegister(RAX), Address(
650 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100651 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100652 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000653
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000654 if (HasEmptyFrame()) {
655 return;
656 }
657
Nicolas Geoffray98893962015-01-21 12:32:32 +0000658 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000659 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000660 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000661 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100662 __ cfi().AdjustCFAOffset(kX86_64WordSize);
663 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000664 }
665 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100666
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100667 int adjust = GetFrameSize() - GetCoreSpillSize();
668 __ subq(CpuRegister(RSP), Immediate(adjust));
669 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000670 uint32_t xmm_spill_location = GetFpuSpillStart();
671 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100672
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000673 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
674 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100675 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
676 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
677 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000678 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100679 }
680
Mathieu Chartiere401d142015-04-22 13:56:20 -0700681 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100682 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100683}
684
685void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100686 __ cfi().RememberState();
687 if (!HasEmptyFrame()) {
688 uint32_t xmm_spill_location = GetFpuSpillStart();
689 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
690 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
691 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
692 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
693 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
694 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
695 }
696 }
697
698 int adjust = GetFrameSize() - GetCoreSpillSize();
699 __ addq(CpuRegister(RSP), Immediate(adjust));
700 __ cfi().AdjustCFAOffset(-adjust);
701
702 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
703 Register reg = kCoreCalleeSaves[i];
704 if (allocated_registers_.ContainsCoreRegister(reg)) {
705 __ popq(CpuRegister(reg));
706 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
707 __ cfi().Restore(DWARFReg(reg));
708 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000709 }
710 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100711 __ ret();
712 __ cfi().RestoreState();
713 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100714}
715
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100716void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
717 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100718}
719
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100720Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
721 switch (load->GetType()) {
722 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100724 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725
726 case Primitive::kPrimInt:
727 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100728 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100729 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100730
731 case Primitive::kPrimBoolean:
732 case Primitive::kPrimByte:
733 case Primitive::kPrimChar:
734 case Primitive::kPrimShort:
735 case Primitive::kPrimVoid:
736 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700737 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100738 }
739
740 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700741 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742}
743
744void CodeGeneratorX86_64::Move(Location destination, Location source) {
745 if (source.Equals(destination)) {
746 return;
747 }
748 if (destination.IsRegister()) {
749 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000750 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100751 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000752 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100753 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000754 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100755 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100756 } else {
757 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000758 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100759 Address(CpuRegister(RSP), source.GetStackIndex()));
760 }
761 } else if (destination.IsFpuRegister()) {
762 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000763 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100764 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000765 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100766 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000767 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 Address(CpuRegister(RSP), source.GetStackIndex()));
769 } else {
770 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000771 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100772 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100773 }
774 } else if (destination.IsStackSlot()) {
775 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100776 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000777 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100778 } else if (source.IsFpuRegister()) {
779 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000780 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500781 } else if (source.IsConstant()) {
782 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000783 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500784 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100785 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500786 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000787 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
788 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100789 }
790 } else {
791 DCHECK(destination.IsDoubleStackSlot());
792 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100793 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000794 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100795 } else if (source.IsFpuRegister()) {
796 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000797 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500798 } else if (source.IsConstant()) {
799 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800800 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500801 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000802 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500803 } else {
804 DCHECK(constant->IsLongConstant());
805 value = constant->AsLongConstant()->GetValue();
806 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400807 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100808 } else {
809 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000810 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
811 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100812 }
813 }
814}
815
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100816void CodeGeneratorX86_64::Move(HInstruction* instruction,
817 Location location,
818 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000819 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100820 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700821 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100822 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000823 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100824 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000825 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000826 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
827 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000828 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000829 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000830 } else if (location.IsStackSlot()) {
831 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
832 } else {
833 DCHECK(location.IsConstant());
834 DCHECK_EQ(location.GetConstant(), const_to_move);
835 }
836 } else if (const_to_move->IsLongConstant()) {
837 int64_t value = const_to_move->AsLongConstant()->GetValue();
838 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400839 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000840 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400841 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000842 } else {
843 DCHECK(location.IsConstant());
844 DCHECK_EQ(location.GetConstant(), const_to_move);
845 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846 }
Roland Levillain476df552014-10-09 17:51:36 +0100847 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100848 switch (instruction->GetType()) {
849 case Primitive::kPrimBoolean:
850 case Primitive::kPrimByte:
851 case Primitive::kPrimChar:
852 case Primitive::kPrimShort:
853 case Primitive::kPrimInt:
854 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100855 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100856 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
857 break;
858
859 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100860 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000861 Move(location,
862 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100863 break;
864
865 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100866 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100867 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000868 } else if (instruction->IsTemporary()) {
869 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
870 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100871 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100872 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100873 switch (instruction->GetType()) {
874 case Primitive::kPrimBoolean:
875 case Primitive::kPrimByte:
876 case Primitive::kPrimChar:
877 case Primitive::kPrimShort:
878 case Primitive::kPrimInt:
879 case Primitive::kPrimNot:
880 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100881 case Primitive::kPrimFloat:
882 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000883 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100884 break;
885
886 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100888 }
889 }
890}
891
David Brazdilfc6a86a2015-06-26 10:33:45 +0000892void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100893 DCHECK(!successor->IsExitBlock());
894
895 HBasicBlock* block = got->GetBlock();
896 HInstruction* previous = got->GetPrevious();
897
898 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000899 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100900 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
901 return;
902 }
903
904 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
905 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
906 }
907 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100908 __ jmp(codegen_->GetLabelOf(successor));
909 }
910}
911
David Brazdilfc6a86a2015-06-26 10:33:45 +0000912void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
913 got->SetLocations(nullptr);
914}
915
916void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
917 HandleGoto(got, got->GetSuccessor());
918}
919
920void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
921 try_boundary->SetLocations(nullptr);
922}
923
924void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
925 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
926 if (!successor->IsExitBlock()) {
927 HandleGoto(try_boundary, successor);
928 }
929}
930
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100931void LocationsBuilderX86_64::VisitExit(HExit* exit) {
932 exit->SetLocations(nullptr);
933}
934
935void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700936 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100937}
938
Mark Mendellc4701932015-04-10 13:18:51 -0400939void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
940 Label* true_label,
941 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100942 if (cond->IsFPConditionTrueIfNaN()) {
943 __ j(kUnordered, true_label);
944 } else if (cond->IsFPConditionFalseIfNaN()) {
945 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400946 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100947 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400948}
949
950void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
951 HCondition* condition,
952 Label* true_target,
953 Label* false_target,
954 Label* always_true_target) {
955 LocationSummary* locations = condition->GetLocations();
956 Location left = locations->InAt(0);
957 Location right = locations->InAt(1);
958
959 // We don't want true_target as a nullptr.
960 if (true_target == nullptr) {
961 true_target = always_true_target;
962 }
963 bool falls_through = (false_target == nullptr);
964
965 // FP compares don't like null false_targets.
966 if (false_target == nullptr) {
967 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
968 }
969
970 Primitive::Type type = condition->InputAt(0)->GetType();
971 switch (type) {
972 case Primitive::kPrimLong: {
973 CpuRegister left_reg = left.AsRegister<CpuRegister>();
974 if (right.IsConstant()) {
975 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
976 if (IsInt<32>(value)) {
977 if (value == 0) {
978 __ testq(left_reg, left_reg);
979 } else {
980 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
981 }
982 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100983 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -0400984 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
985 }
986 } else if (right.IsDoubleStackSlot()) {
987 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
988 } else {
989 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
990 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100991 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -0400992 break;
993 }
994 case Primitive::kPrimFloat: {
995 if (right.IsFpuRegister()) {
996 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
997 } else if (right.IsConstant()) {
998 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
999 codegen_->LiteralFloatAddress(
1000 right.GetConstant()->AsFloatConstant()->GetValue()));
1001 } else {
1002 DCHECK(right.IsStackSlot());
1003 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1004 Address(CpuRegister(RSP), right.GetStackIndex()));
1005 }
1006 GenerateFPJumps(condition, true_target, false_target);
1007 break;
1008 }
1009 case Primitive::kPrimDouble: {
1010 if (right.IsFpuRegister()) {
1011 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1012 } else if (right.IsConstant()) {
1013 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1014 codegen_->LiteralDoubleAddress(
1015 right.GetConstant()->AsDoubleConstant()->GetValue()));
1016 } else {
1017 DCHECK(right.IsDoubleStackSlot());
1018 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1019 Address(CpuRegister(RSP), right.GetStackIndex()));
1020 }
1021 GenerateFPJumps(condition, true_target, false_target);
1022 break;
1023 }
1024 default:
1025 LOG(FATAL) << "Unexpected condition type " << type;
1026 }
1027
1028 if (!falls_through) {
1029 __ jmp(false_target);
1030 }
1031}
1032
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001033void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1034 Label* true_target,
1035 Label* false_target,
1036 Label* always_true_target) {
1037 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001038 if (cond->IsIntConstant()) {
1039 // Constant condition, statically compared against 1.
1040 int32_t cond_value = cond->AsIntConstant()->GetValue();
1041 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001042 if (always_true_target != nullptr) {
1043 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001044 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001045 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001046 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001047 DCHECK_EQ(cond_value, 0);
1048 }
1049 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001050 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001051 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1052 // Moves do not affect the eflags register, so if the condition is
1053 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001054 // again. We can't use the eflags on FP conditions if they are
1055 // materialized due to the complex branching.
1056 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001057 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001058 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1059 && !Primitive::IsFloatingPointType(type);
1060
Roland Levillain4fa13f62015-07-06 18:11:54 +01001061 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001062 if (!eflags_set) {
1063 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001064 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001065 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001066 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001067 } else {
1068 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1069 Immediate(0));
1070 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001071 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001072 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001073 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001074 }
1075 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001076 // Condition has not been materialized, use its inputs as the
1077 // comparison and its condition as the branch condition.
1078
Mark Mendellc4701932015-04-10 13:18:51 -04001079 // Is this a long or FP comparison that has been folded into the HCondition?
1080 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001081 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001082 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1083 true_target, false_target, always_true_target);
1084 return;
1085 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001086
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001087 Location lhs = cond->GetLocations()->InAt(0);
1088 Location rhs = cond->GetLocations()->InAt(1);
1089 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001090 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001091 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001092 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001093 if (constant == 0) {
1094 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1095 } else {
1096 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1097 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001098 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001099 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001100 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1101 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001102 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001103 }
Dave Allison20dfc792014-06-16 20:44:29 -07001104 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001105 if (false_target != nullptr) {
1106 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001107 }
1108}
1109
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001110void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1111 LocationSummary* locations =
1112 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1113 HInstruction* cond = if_instr->InputAt(0);
1114 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1115 locations->SetInAt(0, Location::Any());
1116 }
1117}
1118
1119void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1120 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1121 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1122 Label* always_true_target = true_target;
1123 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1124 if_instr->IfTrueSuccessor())) {
1125 always_true_target = nullptr;
1126 }
1127 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1128 if_instr->IfFalseSuccessor())) {
1129 false_target = nullptr;
1130 }
1131 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1132}
1133
1134void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1135 LocationSummary* locations = new (GetGraph()->GetArena())
1136 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1137 HInstruction* cond = deoptimize->InputAt(0);
1138 DCHECK(cond->IsCondition());
1139 if (cond->AsCondition()->NeedsMaterialization()) {
1140 locations->SetInAt(0, Location::Any());
1141 }
1142}
1143
1144void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1145 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
1146 DeoptimizationSlowPathX86_64(deoptimize);
1147 codegen_->AddSlowPath(slow_path);
1148 Label* slow_path_entry = slow_path->GetEntryLabel();
1149 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1150}
1151
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1153 local->SetLocations(nullptr);
1154}
1155
1156void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1157 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1158}
1159
1160void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1161 local->SetLocations(nullptr);
1162}
1163
1164void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1165 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001166 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001167}
1168
1169void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001170 LocationSummary* locations =
1171 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001172 switch (store->InputAt(1)->GetType()) {
1173 case Primitive::kPrimBoolean:
1174 case Primitive::kPrimByte:
1175 case Primitive::kPrimChar:
1176 case Primitive::kPrimShort:
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001179 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001180 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1181 break;
1182
1183 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001185 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1186 break;
1187
1188 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001190 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001191}
1192
1193void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001194 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001195}
1196
Roland Levillain0d37cd02015-05-27 16:39:19 +01001197void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001198 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001199 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001200 // Handle the long/FP comparisons made in instruction simplification.
1201 switch (cond->InputAt(0)->GetType()) {
1202 case Primitive::kPrimLong:
1203 locations->SetInAt(0, Location::RequiresRegister());
1204 locations->SetInAt(1, Location::Any());
1205 break;
1206 case Primitive::kPrimFloat:
1207 case Primitive::kPrimDouble:
1208 locations->SetInAt(0, Location::RequiresFpuRegister());
1209 locations->SetInAt(1, Location::Any());
1210 break;
1211 default:
1212 locations->SetInAt(0, Location::RequiresRegister());
1213 locations->SetInAt(1, Location::Any());
1214 break;
1215 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001216 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001217 locations->SetOut(Location::RequiresRegister());
1218 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001219}
1220
Roland Levillain0d37cd02015-05-27 16:39:19 +01001221void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001222 if (!cond->NeedsMaterialization()) {
1223 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001224 }
Mark Mendellc4701932015-04-10 13:18:51 -04001225
1226 LocationSummary* locations = cond->GetLocations();
1227 Location lhs = locations->InAt(0);
1228 Location rhs = locations->InAt(1);
1229 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1230 Label true_label, false_label;
1231
1232 switch (cond->InputAt(0)->GetType()) {
1233 default:
1234 // Integer case.
1235
1236 // Clear output register: setcc only sets the low byte.
1237 __ xorl(reg, reg);
1238
1239 if (rhs.IsRegister()) {
1240 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1241 } else if (rhs.IsConstant()) {
1242 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1243 if (constant == 0) {
1244 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1245 } else {
1246 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1247 }
1248 } else {
1249 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1250 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001251 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001252 return;
1253 case Primitive::kPrimLong:
1254 // Clear output register: setcc only sets the low byte.
1255 __ xorl(reg, reg);
1256
1257 if (rhs.IsRegister()) {
1258 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1259 } else if (rhs.IsConstant()) {
1260 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1261 if (IsInt<32>(value)) {
1262 if (value == 0) {
1263 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1264 } else {
1265 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1266 }
1267 } else {
1268 // Value won't fit in an int.
1269 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1270 }
1271 } else {
1272 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1273 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001274 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001275 return;
1276 case Primitive::kPrimFloat: {
1277 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1278 if (rhs.IsConstant()) {
1279 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1280 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1281 } else if (rhs.IsStackSlot()) {
1282 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1283 } else {
1284 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1285 }
1286 GenerateFPJumps(cond, &true_label, &false_label);
1287 break;
1288 }
1289 case Primitive::kPrimDouble: {
1290 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1291 if (rhs.IsConstant()) {
1292 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1293 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1294 } else if (rhs.IsDoubleStackSlot()) {
1295 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1296 } else {
1297 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1298 }
1299 GenerateFPJumps(cond, &true_label, &false_label);
1300 break;
1301 }
1302 }
1303
1304 // Convert the jumps into the result.
1305 Label done_label;
1306
Roland Levillain4fa13f62015-07-06 18:11:54 +01001307 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001308 __ Bind(&false_label);
1309 __ xorl(reg, reg);
1310 __ jmp(&done_label);
1311
Roland Levillain4fa13f62015-07-06 18:11:54 +01001312 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001313 __ Bind(&true_label);
1314 __ movl(reg, Immediate(1));
1315 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001316}
1317
1318void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1319 VisitCondition(comp);
1320}
1321
1322void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1323 VisitCondition(comp);
1324}
1325
1326void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1327 VisitCondition(comp);
1328}
1329
1330void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1331 VisitCondition(comp);
1332}
1333
1334void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1335 VisitCondition(comp);
1336}
1337
1338void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1339 VisitCondition(comp);
1340}
1341
1342void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1343 VisitCondition(comp);
1344}
1345
1346void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1347 VisitCondition(comp);
1348}
1349
1350void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1351 VisitCondition(comp);
1352}
1353
1354void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1355 VisitCondition(comp);
1356}
1357
1358void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1359 VisitCondition(comp);
1360}
1361
1362void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1363 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001364}
1365
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001366void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001367 LocationSummary* locations =
1368 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001369 switch (compare->InputAt(0)->GetType()) {
1370 case Primitive::kPrimLong: {
1371 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001372 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1374 break;
1375 }
1376 case Primitive::kPrimFloat:
1377 case Primitive::kPrimDouble: {
1378 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001379 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001380 locations->SetOut(Location::RequiresRegister());
1381 break;
1382 }
1383 default:
1384 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1385 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001386}
1387
1388void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001389 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001390 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001391 Location left = locations->InAt(0);
1392 Location right = locations->InAt(1);
1393
1394 Label less, greater, done;
1395 Primitive::Type type = compare->InputAt(0)->GetType();
1396 switch (type) {
1397 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001398 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1399 if (right.IsConstant()) {
1400 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001401 if (IsInt<32>(value)) {
1402 if (value == 0) {
1403 __ testq(left_reg, left_reg);
1404 } else {
1405 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1406 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001407 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001408 // Value won't fit in an int.
1409 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001410 }
Mark Mendell40741f32015-04-20 22:10:34 -04001411 } else if (right.IsDoubleStackSlot()) {
1412 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001413 } else {
1414 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1415 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001416 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001417 }
1418 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001419 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1420 if (right.IsConstant()) {
1421 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1422 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1423 } else if (right.IsStackSlot()) {
1424 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1425 } else {
1426 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1427 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001428 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1429 break;
1430 }
1431 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001432 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1433 if (right.IsConstant()) {
1434 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1435 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1436 } else if (right.IsDoubleStackSlot()) {
1437 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1438 } else {
1439 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1440 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001441 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1442 break;
1443 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001444 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001445 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001446 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001447 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001448 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001449 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001450
Calin Juravle91debbc2014-11-26 19:01:09 +00001451 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001452 __ movl(out, Immediate(1));
1453 __ jmp(&done);
1454
1455 __ Bind(&less);
1456 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001457
1458 __ Bind(&done);
1459}
1460
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001461void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001462 LocationSummary* locations =
1463 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001464 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001465}
1466
1467void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001468 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001469 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001470}
1471
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001472void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1473 LocationSummary* locations =
1474 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1475 locations->SetOut(Location::ConstantLocation(constant));
1476}
1477
1478void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1479 // Will be generated at use site.
1480 UNUSED(constant);
1481}
1482
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001483void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001484 LocationSummary* locations =
1485 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001486 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001487}
1488
1489void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001490 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001491 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001492}
1493
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001494void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1495 LocationSummary* locations =
1496 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1497 locations->SetOut(Location::ConstantLocation(constant));
1498}
1499
1500void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1501 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001502 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001503}
1504
1505void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1506 LocationSummary* locations =
1507 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1508 locations->SetOut(Location::ConstantLocation(constant));
1509}
1510
1511void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1512 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001513 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001514}
1515
Calin Juravle27df7582015-04-17 19:12:31 +01001516void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1517 memory_barrier->SetLocations(nullptr);
1518}
1519
1520void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1521 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1522}
1523
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001524void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1525 ret->SetLocations(nullptr);
1526}
1527
1528void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001529 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001530 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001531}
1532
1533void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001534 LocationSummary* locations =
1535 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001536 switch (ret->InputAt(0)->GetType()) {
1537 case Primitive::kPrimBoolean:
1538 case Primitive::kPrimByte:
1539 case Primitive::kPrimChar:
1540 case Primitive::kPrimShort:
1541 case Primitive::kPrimInt:
1542 case Primitive::kPrimNot:
1543 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001544 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001545 break;
1546
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001547 case Primitive::kPrimFloat:
1548 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001549 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001550 break;
1551
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001552 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001553 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001554 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001555}
1556
1557void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1558 if (kIsDebugBuild) {
1559 switch (ret->InputAt(0)->GetType()) {
1560 case Primitive::kPrimBoolean:
1561 case Primitive::kPrimByte:
1562 case Primitive::kPrimChar:
1563 case Primitive::kPrimShort:
1564 case Primitive::kPrimInt:
1565 case Primitive::kPrimNot:
1566 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001567 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001568 break;
1569
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001570 case Primitive::kPrimFloat:
1571 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001572 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001573 XMM0);
1574 break;
1575
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001576 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001577 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001578 }
1579 }
1580 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001581}
1582
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001583Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1584 switch (type) {
1585 case Primitive::kPrimBoolean:
1586 case Primitive::kPrimByte:
1587 case Primitive::kPrimChar:
1588 case Primitive::kPrimShort:
1589 case Primitive::kPrimInt:
1590 case Primitive::kPrimNot:
1591 case Primitive::kPrimLong:
1592 return Location::RegisterLocation(RAX);
1593
1594 case Primitive::kPrimVoid:
1595 return Location::NoLocation();
1596
1597 case Primitive::kPrimDouble:
1598 case Primitive::kPrimFloat:
1599 return Location::FpuRegisterLocation(XMM0);
1600 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001601
1602 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001603}
1604
1605Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1606 return Location::RegisterLocation(kMethodRegisterArgument);
1607}
1608
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001609Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001610 switch (type) {
1611 case Primitive::kPrimBoolean:
1612 case Primitive::kPrimByte:
1613 case Primitive::kPrimChar:
1614 case Primitive::kPrimShort:
1615 case Primitive::kPrimInt:
1616 case Primitive::kPrimNot: {
1617 uint32_t index = gp_index_++;
1618 stack_index_++;
1619 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001620 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001621 } else {
1622 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1623 }
1624 }
1625
1626 case Primitive::kPrimLong: {
1627 uint32_t index = gp_index_;
1628 stack_index_ += 2;
1629 if (index < calling_convention.GetNumberOfRegisters()) {
1630 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001631 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001632 } else {
1633 gp_index_ += 2;
1634 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1635 }
1636 }
1637
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001638 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001639 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001640 stack_index_++;
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::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1645 }
1646 }
1647
1648 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001649 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001650 stack_index_ += 2;
1651 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001652 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001653 } else {
1654 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1655 }
1656 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001657
1658 case Primitive::kPrimVoid:
1659 LOG(FATAL) << "Unexpected parameter type " << type;
1660 break;
1661 }
1662 return Location();
1663}
1664
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001665void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001666 // When we do not run baseline, explicit clinit checks triggered by static
1667 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1668 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001669
Mark Mendellfb8d2792015-03-31 22:16:59 -04001670 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001671 if (intrinsic.TryDispatch(invoke)) {
1672 return;
1673 }
1674
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001675 HandleInvoke(invoke);
1676}
1677
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001678static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1679 if (invoke->GetLocations()->Intrinsified()) {
1680 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1681 intrinsic.Dispatch(invoke);
1682 return true;
1683 }
1684 return false;
1685}
1686
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001687void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001688 // When we do not run baseline, explicit clinit checks triggered by static
1689 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1690 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001691
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001692 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1693 return;
1694 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001695
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001696 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001697 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001698 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001699 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001700}
1701
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001702void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001703 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001704 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001705}
1706
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001707void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001708 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001709 if (intrinsic.TryDispatch(invoke)) {
1710 return;
1711 }
1712
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001713 HandleInvoke(invoke);
1714}
1715
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001716void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001717 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1718 return;
1719 }
1720
Roland Levillain271ab9c2014-11-27 15:23:57 +00001721 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001722 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1723 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001724 LocationSummary* locations = invoke->GetLocations();
1725 Location receiver = locations->InAt(0);
1726 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1727 // temp = object->GetClass();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001728 DCHECK(receiver.IsRegister());
1729 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001730 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001731 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001732 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001733 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001734 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001735 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001736 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001737
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001738 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001739 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001740}
1741
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001742void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1743 HandleInvoke(invoke);
1744 // Add the hidden argument.
1745 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1746}
1747
1748void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1749 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001750 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001751 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1752 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001753 LocationSummary* locations = invoke->GetLocations();
1754 Location receiver = locations->InAt(0);
1755 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1756
1757 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001758 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1759 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001760
1761 // temp = object->GetClass();
1762 if (receiver.IsStackSlot()) {
1763 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1764 __ movl(temp, Address(temp, class_offset));
1765 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001766 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001767 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001768 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001769 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001770 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001771 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001772 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001773 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001774 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001775
1776 DCHECK(!codegen_->IsLeafMethod());
1777 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1778}
1779
Roland Levillain88cb1752014-10-20 16:36:47 +01001780void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1781 LocationSummary* locations =
1782 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1783 switch (neg->GetResultType()) {
1784 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001785 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001786 locations->SetInAt(0, Location::RequiresRegister());
1787 locations->SetOut(Location::SameAsFirstInput());
1788 break;
1789
Roland Levillain88cb1752014-10-20 16:36:47 +01001790 case Primitive::kPrimFloat:
1791 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001792 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001793 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001794 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001795 break;
1796
1797 default:
1798 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1799 }
1800}
1801
1802void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1803 LocationSummary* locations = neg->GetLocations();
1804 Location out = locations->Out();
1805 Location in = locations->InAt(0);
1806 switch (neg->GetResultType()) {
1807 case Primitive::kPrimInt:
1808 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001809 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001810 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001811 break;
1812
1813 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001814 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001815 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001816 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001817 break;
1818
Roland Levillain5368c212014-11-27 15:03:41 +00001819 case Primitive::kPrimFloat: {
1820 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001821 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001822 // Implement float negation with an exclusive or with value
1823 // 0x80000000 (mask for bit 31, representing the sign of a
1824 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001825 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001826 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001827 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001828 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001829
Roland Levillain5368c212014-11-27 15:03:41 +00001830 case Primitive::kPrimDouble: {
1831 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001832 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001833 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001834 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001835 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001836 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001837 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001838 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001839 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001840
1841 default:
1842 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1843 }
1844}
1845
Roland Levillaindff1f282014-11-05 14:15:05 +00001846void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1847 LocationSummary* locations =
1848 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1849 Primitive::Type result_type = conversion->GetResultType();
1850 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001851 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001852
David Brazdilb2bd1c52015-03-25 11:17:37 +00001853 // The Java language does not allow treating boolean as an integral type but
1854 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001855
Roland Levillaindff1f282014-11-05 14:15:05 +00001856 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001857 case Primitive::kPrimByte:
1858 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001859 case Primitive::kPrimBoolean:
1860 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001861 case Primitive::kPrimShort:
1862 case Primitive::kPrimInt:
1863 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001864 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001865 locations->SetInAt(0, Location::Any());
1866 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1867 break;
1868
1869 default:
1870 LOG(FATAL) << "Unexpected type conversion from " << input_type
1871 << " to " << result_type;
1872 }
1873 break;
1874
Roland Levillain01a8d712014-11-14 16:27:39 +00001875 case Primitive::kPrimShort:
1876 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001877 case Primitive::kPrimBoolean:
1878 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001879 case Primitive::kPrimByte:
1880 case Primitive::kPrimInt:
1881 case Primitive::kPrimChar:
1882 // Processing a Dex `int-to-short' instruction.
1883 locations->SetInAt(0, Location::Any());
1884 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1885 break;
1886
1887 default:
1888 LOG(FATAL) << "Unexpected type conversion from " << input_type
1889 << " to " << result_type;
1890 }
1891 break;
1892
Roland Levillain946e1432014-11-11 17:35:19 +00001893 case Primitive::kPrimInt:
1894 switch (input_type) {
1895 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001896 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001897 locations->SetInAt(0, Location::Any());
1898 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1899 break;
1900
1901 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001902 // Processing a Dex `float-to-int' instruction.
1903 locations->SetInAt(0, Location::RequiresFpuRegister());
1904 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001905 break;
1906
Roland Levillain946e1432014-11-11 17:35:19 +00001907 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001908 // Processing a Dex `double-to-int' instruction.
1909 locations->SetInAt(0, Location::RequiresFpuRegister());
1910 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001911 break;
1912
1913 default:
1914 LOG(FATAL) << "Unexpected type conversion from " << input_type
1915 << " to " << result_type;
1916 }
1917 break;
1918
Roland Levillaindff1f282014-11-05 14:15:05 +00001919 case Primitive::kPrimLong:
1920 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001921 case Primitive::kPrimBoolean:
1922 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001923 case Primitive::kPrimByte:
1924 case Primitive::kPrimShort:
1925 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001926 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001927 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001928 // TODO: We would benefit from a (to-be-implemented)
1929 // Location::RegisterOrStackSlot requirement for this input.
1930 locations->SetInAt(0, Location::RequiresRegister());
1931 locations->SetOut(Location::RequiresRegister());
1932 break;
1933
1934 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001935 // Processing a Dex `float-to-long' instruction.
1936 locations->SetInAt(0, Location::RequiresFpuRegister());
1937 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00001938 break;
1939
Roland Levillaindff1f282014-11-05 14:15:05 +00001940 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001941 // Processing a Dex `double-to-long' instruction.
1942 locations->SetInAt(0, Location::RequiresFpuRegister());
1943 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001944 break;
1945
1946 default:
1947 LOG(FATAL) << "Unexpected type conversion from " << input_type
1948 << " to " << result_type;
1949 }
1950 break;
1951
Roland Levillain981e4542014-11-14 11:47:14 +00001952 case Primitive::kPrimChar:
1953 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001954 case Primitive::kPrimBoolean:
1955 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001956 case Primitive::kPrimByte:
1957 case Primitive::kPrimShort:
1958 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001959 // Processing a Dex `int-to-char' instruction.
1960 locations->SetInAt(0, Location::Any());
1961 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1962 break;
1963
1964 default:
1965 LOG(FATAL) << "Unexpected type conversion from " << input_type
1966 << " to " << result_type;
1967 }
1968 break;
1969
Roland Levillaindff1f282014-11-05 14:15:05 +00001970 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001971 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001972 case Primitive::kPrimBoolean:
1973 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001974 case Primitive::kPrimByte:
1975 case Primitive::kPrimShort:
1976 case Primitive::kPrimInt:
1977 case Primitive::kPrimChar:
1978 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001979 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001980 locations->SetOut(Location::RequiresFpuRegister());
1981 break;
1982
1983 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001984 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001985 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001986 locations->SetOut(Location::RequiresFpuRegister());
1987 break;
1988
Roland Levillaincff13742014-11-17 14:32:17 +00001989 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001990 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001991 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001992 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001993 break;
1994
1995 default:
1996 LOG(FATAL) << "Unexpected type conversion from " << input_type
1997 << " to " << result_type;
1998 };
1999 break;
2000
Roland Levillaindff1f282014-11-05 14:15:05 +00002001 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002002 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002003 case Primitive::kPrimBoolean:
2004 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002005 case Primitive::kPrimByte:
2006 case Primitive::kPrimShort:
2007 case Primitive::kPrimInt:
2008 case Primitive::kPrimChar:
2009 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002010 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002011 locations->SetOut(Location::RequiresFpuRegister());
2012 break;
2013
2014 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002015 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002016 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002017 locations->SetOut(Location::RequiresFpuRegister());
2018 break;
2019
Roland Levillaincff13742014-11-17 14:32:17 +00002020 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002021 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002022 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002023 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002024 break;
2025
2026 default:
2027 LOG(FATAL) << "Unexpected type conversion from " << input_type
2028 << " to " << result_type;
2029 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002030 break;
2031
2032 default:
2033 LOG(FATAL) << "Unexpected type conversion from " << input_type
2034 << " to " << result_type;
2035 }
2036}
2037
2038void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2039 LocationSummary* locations = conversion->GetLocations();
2040 Location out = locations->Out();
2041 Location in = locations->InAt(0);
2042 Primitive::Type result_type = conversion->GetResultType();
2043 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002044 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002045 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002046 case Primitive::kPrimByte:
2047 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002048 case Primitive::kPrimBoolean:
2049 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002050 case Primitive::kPrimShort:
2051 case Primitive::kPrimInt:
2052 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002053 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002054 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002055 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002056 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002057 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002058 Address(CpuRegister(RSP), in.GetStackIndex()));
2059 } else {
2060 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002061 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002062 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2063 }
2064 break;
2065
2066 default:
2067 LOG(FATAL) << "Unexpected type conversion from " << input_type
2068 << " to " << result_type;
2069 }
2070 break;
2071
Roland Levillain01a8d712014-11-14 16:27:39 +00002072 case Primitive::kPrimShort:
2073 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002074 case Primitive::kPrimBoolean:
2075 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002076 case Primitive::kPrimByte:
2077 case Primitive::kPrimInt:
2078 case Primitive::kPrimChar:
2079 // Processing a Dex `int-to-short' instruction.
2080 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002082 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002083 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002084 Address(CpuRegister(RSP), in.GetStackIndex()));
2085 } else {
2086 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002087 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002088 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2089 }
2090 break;
2091
2092 default:
2093 LOG(FATAL) << "Unexpected type conversion from " << input_type
2094 << " to " << result_type;
2095 }
2096 break;
2097
Roland Levillain946e1432014-11-11 17:35:19 +00002098 case Primitive::kPrimInt:
2099 switch (input_type) {
2100 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002101 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002102 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002103 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002104 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002105 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002106 Address(CpuRegister(RSP), in.GetStackIndex()));
2107 } else {
2108 DCHECK(in.IsConstant());
2109 DCHECK(in.GetConstant()->IsLongConstant());
2110 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002111 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002112 }
2113 break;
2114
Roland Levillain3f8f9362014-12-02 17:45:01 +00002115 case Primitive::kPrimFloat: {
2116 // Processing a Dex `float-to-int' instruction.
2117 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2118 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain3f8f9362014-12-02 17:45:01 +00002119 Label done, nan;
2120
2121 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002122 // if input >= (float)INT_MAX goto done
2123 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002124 __ j(kAboveEqual, &done);
2125 // if input == NaN goto nan
2126 __ j(kUnordered, &nan);
2127 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002128 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002129 __ jmp(&done);
2130 __ Bind(&nan);
2131 // output = 0
2132 __ xorl(output, output);
2133 __ Bind(&done);
2134 break;
2135 }
2136
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002137 case Primitive::kPrimDouble: {
2138 // Processing a Dex `double-to-int' instruction.
2139 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2140 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002141 Label done, nan;
2142
2143 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002144 // if input >= (double)INT_MAX goto done
2145 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002146 __ j(kAboveEqual, &done);
2147 // if input == NaN goto nan
2148 __ j(kUnordered, &nan);
2149 // output = double-to-int-truncate(input)
2150 __ cvttsd2si(output, input);
2151 __ jmp(&done);
2152 __ Bind(&nan);
2153 // output = 0
2154 __ xorl(output, output);
2155 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002156 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002157 }
Roland Levillain946e1432014-11-11 17:35:19 +00002158
2159 default:
2160 LOG(FATAL) << "Unexpected type conversion from " << input_type
2161 << " to " << result_type;
2162 }
2163 break;
2164
Roland Levillaindff1f282014-11-05 14:15:05 +00002165 case Primitive::kPrimLong:
2166 switch (input_type) {
2167 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002168 case Primitive::kPrimBoolean:
2169 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002170 case Primitive::kPrimByte:
2171 case Primitive::kPrimShort:
2172 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002173 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002174 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002175 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002176 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002177 break;
2178
Roland Levillain624279f2014-12-04 11:54:28 +00002179 case Primitive::kPrimFloat: {
2180 // Processing a Dex `float-to-long' instruction.
2181 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2182 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain624279f2014-12-04 11:54:28 +00002183 Label done, nan;
2184
Mark Mendell92e83bf2015-05-07 11:25:03 -04002185 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002186 // if input >= (float)LONG_MAX goto done
2187 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002188 __ j(kAboveEqual, &done);
2189 // if input == NaN goto nan
2190 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002191 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002192 __ cvttss2si(output, input, true);
2193 __ jmp(&done);
2194 __ Bind(&nan);
2195 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002196 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002197 __ Bind(&done);
2198 break;
2199 }
2200
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002201 case Primitive::kPrimDouble: {
2202 // Processing a Dex `double-to-long' instruction.
2203 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2204 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002205 Label done, nan;
2206
Mark Mendell92e83bf2015-05-07 11:25:03 -04002207 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002208 // if input >= (double)LONG_MAX goto done
2209 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002210 __ j(kAboveEqual, &done);
2211 // if input == NaN goto nan
2212 __ j(kUnordered, &nan);
2213 // output = double-to-long-truncate(input)
2214 __ cvttsd2si(output, input, true);
2215 __ jmp(&done);
2216 __ Bind(&nan);
2217 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002218 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002219 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002220 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002221 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002222
2223 default:
2224 LOG(FATAL) << "Unexpected type conversion from " << input_type
2225 << " to " << result_type;
2226 }
2227 break;
2228
Roland Levillain981e4542014-11-14 11:47:14 +00002229 case Primitive::kPrimChar:
2230 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002231 case Primitive::kPrimBoolean:
2232 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002233 case Primitive::kPrimByte:
2234 case Primitive::kPrimShort:
2235 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002236 // Processing a Dex `int-to-char' instruction.
2237 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002238 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002239 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002240 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002241 Address(CpuRegister(RSP), in.GetStackIndex()));
2242 } else {
2243 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002244 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002245 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2246 }
2247 break;
2248
2249 default:
2250 LOG(FATAL) << "Unexpected type conversion from " << input_type
2251 << " to " << result_type;
2252 }
2253 break;
2254
Roland Levillaindff1f282014-11-05 14:15:05 +00002255 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002256 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002257 case Primitive::kPrimBoolean:
2258 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002259 case Primitive::kPrimByte:
2260 case Primitive::kPrimShort:
2261 case Primitive::kPrimInt:
2262 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002263 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002264 if (in.IsRegister()) {
2265 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2266 } else if (in.IsConstant()) {
2267 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2268 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2269 if (v == 0) {
2270 __ xorps(dest, dest);
2271 } else {
2272 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2273 }
2274 } else {
2275 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2276 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2277 }
Roland Levillaincff13742014-11-17 14:32:17 +00002278 break;
2279
2280 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002281 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002282 if (in.IsRegister()) {
2283 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2284 } else if (in.IsConstant()) {
2285 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2286 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2287 if (v == 0) {
2288 __ xorps(dest, dest);
2289 } else {
2290 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2291 }
2292 } else {
2293 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2294 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2295 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002296 break;
2297
Roland Levillaincff13742014-11-17 14:32:17 +00002298 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002299 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002300 if (in.IsFpuRegister()) {
2301 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2302 } else if (in.IsConstant()) {
2303 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2304 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2305 if (bit_cast<int64_t, double>(v) == 0) {
2306 __ xorps(dest, dest);
2307 } else {
2308 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2309 }
2310 } else {
2311 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2312 Address(CpuRegister(RSP), in.GetStackIndex()));
2313 }
Roland Levillaincff13742014-11-17 14:32:17 +00002314 break;
2315
2316 default:
2317 LOG(FATAL) << "Unexpected type conversion from " << input_type
2318 << " to " << result_type;
2319 };
2320 break;
2321
Roland Levillaindff1f282014-11-05 14:15:05 +00002322 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002323 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002324 case Primitive::kPrimBoolean:
2325 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002326 case Primitive::kPrimByte:
2327 case Primitive::kPrimShort:
2328 case Primitive::kPrimInt:
2329 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002330 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002331 if (in.IsRegister()) {
2332 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2333 } else if (in.IsConstant()) {
2334 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2335 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2336 if (v == 0) {
2337 __ xorpd(dest, dest);
2338 } else {
2339 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2340 }
2341 } else {
2342 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2343 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2344 }
Roland Levillaincff13742014-11-17 14:32:17 +00002345 break;
2346
2347 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002348 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002349 if (in.IsRegister()) {
2350 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2351 } else if (in.IsConstant()) {
2352 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2353 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2354 if (v == 0) {
2355 __ xorpd(dest, dest);
2356 } else {
2357 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2358 }
2359 } else {
2360 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2361 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2362 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002363 break;
2364
Roland Levillaincff13742014-11-17 14:32:17 +00002365 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002366 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002367 if (in.IsFpuRegister()) {
2368 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2369 } else if (in.IsConstant()) {
2370 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2371 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2372 if (bit_cast<int32_t, float>(v) == 0) {
2373 __ xorpd(dest, dest);
2374 } else {
2375 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2376 }
2377 } else {
2378 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2379 Address(CpuRegister(RSP), in.GetStackIndex()));
2380 }
Roland Levillaincff13742014-11-17 14:32:17 +00002381 break;
2382
2383 default:
2384 LOG(FATAL) << "Unexpected type conversion from " << input_type
2385 << " to " << result_type;
2386 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002387 break;
2388
2389 default:
2390 LOG(FATAL) << "Unexpected type conversion from " << input_type
2391 << " to " << result_type;
2392 }
2393}
2394
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002395void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002396 LocationSummary* locations =
2397 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002398 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002399 case Primitive::kPrimInt: {
2400 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002401 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2402 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002403 break;
2404 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002405
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002406 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002407 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002408 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002409 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002411 break;
2412 }
2413
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002414 case Primitive::kPrimDouble:
2415 case Primitive::kPrimFloat: {
2416 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002417 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002418 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002419 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002420 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002421
2422 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002423 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002424 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002425}
2426
2427void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2428 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002429 Location first = locations->InAt(0);
2430 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002431 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002432
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002433 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002434 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002435 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002436 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2437 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002438 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2439 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002440 } else {
2441 __ leal(out.AsRegister<CpuRegister>(), Address(
2442 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2443 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002444 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002445 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2446 __ addl(out.AsRegister<CpuRegister>(),
2447 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2448 } else {
2449 __ leal(out.AsRegister<CpuRegister>(), Address(
2450 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2451 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002452 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002453 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002454 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002455 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002456 break;
2457 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002458
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002459 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002460 if (second.IsRegister()) {
2461 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2462 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002463 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2464 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002465 } else {
2466 __ leaq(out.AsRegister<CpuRegister>(), Address(
2467 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2468 }
2469 } else {
2470 DCHECK(second.IsConstant());
2471 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2472 int32_t int32_value = Low32Bits(value);
2473 DCHECK_EQ(int32_value, value);
2474 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2475 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2476 } else {
2477 __ leaq(out.AsRegister<CpuRegister>(), Address(
2478 first.AsRegister<CpuRegister>(), int32_value));
2479 }
2480 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002481 break;
2482 }
2483
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002484 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002485 if (second.IsFpuRegister()) {
2486 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2487 } else if (second.IsConstant()) {
2488 __ addss(first.AsFpuRegister<XmmRegister>(),
2489 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2490 } else {
2491 DCHECK(second.IsStackSlot());
2492 __ addss(first.AsFpuRegister<XmmRegister>(),
2493 Address(CpuRegister(RSP), second.GetStackIndex()));
2494 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002495 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002496 }
2497
2498 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002499 if (second.IsFpuRegister()) {
2500 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2501 } else if (second.IsConstant()) {
2502 __ addsd(first.AsFpuRegister<XmmRegister>(),
2503 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2504 } else {
2505 DCHECK(second.IsDoubleStackSlot());
2506 __ addsd(first.AsFpuRegister<XmmRegister>(),
2507 Address(CpuRegister(RSP), second.GetStackIndex()));
2508 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002509 break;
2510 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002511
2512 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002513 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002514 }
2515}
2516
2517void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002518 LocationSummary* locations =
2519 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002520 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002521 case Primitive::kPrimInt: {
2522 locations->SetInAt(0, Location::RequiresRegister());
2523 locations->SetInAt(1, Location::Any());
2524 locations->SetOut(Location::SameAsFirstInput());
2525 break;
2526 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002527 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002528 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002529 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002530 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002531 break;
2532 }
Calin Juravle11351682014-10-23 15:38:15 +01002533 case Primitive::kPrimFloat:
2534 case Primitive::kPrimDouble: {
2535 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002536 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002537 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002538 break;
Calin Juravle11351682014-10-23 15:38:15 +01002539 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002540 default:
Calin Juravle11351682014-10-23 15:38:15 +01002541 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002542 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002543}
2544
2545void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2546 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002547 Location first = locations->InAt(0);
2548 Location second = locations->InAt(1);
2549 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002550 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002551 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002552 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002553 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002554 } else if (second.IsConstant()) {
2555 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002556 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002557 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002558 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002559 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002560 break;
2561 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002562 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002563 if (second.IsConstant()) {
2564 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2565 DCHECK(IsInt<32>(value));
2566 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2567 } else {
2568 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2569 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002570 break;
2571 }
2572
Calin Juravle11351682014-10-23 15:38:15 +01002573 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002574 if (second.IsFpuRegister()) {
2575 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2576 } else if (second.IsConstant()) {
2577 __ subss(first.AsFpuRegister<XmmRegister>(),
2578 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2579 } else {
2580 DCHECK(second.IsStackSlot());
2581 __ subss(first.AsFpuRegister<XmmRegister>(),
2582 Address(CpuRegister(RSP), second.GetStackIndex()));
2583 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002584 break;
Calin Juravle11351682014-10-23 15:38:15 +01002585 }
2586
2587 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002588 if (second.IsFpuRegister()) {
2589 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2590 } else if (second.IsConstant()) {
2591 __ subsd(first.AsFpuRegister<XmmRegister>(),
2592 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2593 } else {
2594 DCHECK(second.IsDoubleStackSlot());
2595 __ subsd(first.AsFpuRegister<XmmRegister>(),
2596 Address(CpuRegister(RSP), second.GetStackIndex()));
2597 }
Calin Juravle11351682014-10-23 15:38:15 +01002598 break;
2599 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002600
2601 default:
Calin Juravle11351682014-10-23 15:38:15 +01002602 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002603 }
2604}
2605
Calin Juravle34bacdf2014-10-07 20:23:36 +01002606void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2607 LocationSummary* locations =
2608 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2609 switch (mul->GetResultType()) {
2610 case Primitive::kPrimInt: {
2611 locations->SetInAt(0, Location::RequiresRegister());
2612 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002613 if (mul->InputAt(1)->IsIntConstant()) {
2614 // Can use 3 operand multiply.
2615 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2616 } else {
2617 locations->SetOut(Location::SameAsFirstInput());
2618 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002619 break;
2620 }
2621 case Primitive::kPrimLong: {
2622 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002623 locations->SetInAt(1, Location::Any());
2624 if (mul->InputAt(1)->IsLongConstant() &&
2625 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002626 // Can use 3 operand multiply.
2627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2628 } else {
2629 locations->SetOut(Location::SameAsFirstInput());
2630 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002631 break;
2632 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002633 case Primitive::kPrimFloat:
2634 case Primitive::kPrimDouble: {
2635 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002636 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002637 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002638 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002639 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002640
2641 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002642 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002643 }
2644}
2645
2646void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2647 LocationSummary* locations = mul->GetLocations();
2648 Location first = locations->InAt(0);
2649 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002650 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002651 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002652 case Primitive::kPrimInt:
2653 // The constant may have ended up in a register, so test explicitly to avoid
2654 // problems where the output may not be the same as the first operand.
2655 if (mul->InputAt(1)->IsIntConstant()) {
2656 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2657 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2658 } else if (second.IsRegister()) {
2659 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002660 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002661 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002662 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002663 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002664 __ imull(first.AsRegister<CpuRegister>(),
2665 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002666 }
2667 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002668 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002669 // The constant may have ended up in a register, so test explicitly to avoid
2670 // problems where the output may not be the same as the first operand.
2671 if (mul->InputAt(1)->IsLongConstant()) {
2672 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2673 if (IsInt<32>(value)) {
2674 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2675 Immediate(static_cast<int32_t>(value)));
2676 } else {
2677 // Have to use the constant area.
2678 DCHECK(first.Equals(out));
2679 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2680 }
2681 } else if (second.IsRegister()) {
2682 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002683 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002684 } else {
2685 DCHECK(second.IsDoubleStackSlot());
2686 DCHECK(first.Equals(out));
2687 __ imulq(first.AsRegister<CpuRegister>(),
2688 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002689 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002690 break;
2691 }
2692
Calin Juravleb5bfa962014-10-21 18:02:24 +01002693 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002694 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002695 if (second.IsFpuRegister()) {
2696 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2697 } else if (second.IsConstant()) {
2698 __ mulss(first.AsFpuRegister<XmmRegister>(),
2699 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2700 } else {
2701 DCHECK(second.IsStackSlot());
2702 __ mulss(first.AsFpuRegister<XmmRegister>(),
2703 Address(CpuRegister(RSP), second.GetStackIndex()));
2704 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002705 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002706 }
2707
2708 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002709 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002710 if (second.IsFpuRegister()) {
2711 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2712 } else if (second.IsConstant()) {
2713 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2714 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2715 } else {
2716 DCHECK(second.IsDoubleStackSlot());
2717 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2718 Address(CpuRegister(RSP), second.GetStackIndex()));
2719 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002720 break;
2721 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002722
2723 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002724 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002725 }
2726}
2727
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002728void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2729 uint32_t stack_adjustment, bool is_float) {
2730 if (source.IsStackSlot()) {
2731 DCHECK(is_float);
2732 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2733 } else if (source.IsDoubleStackSlot()) {
2734 DCHECK(!is_float);
2735 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2736 } else {
2737 // Write the value to the temporary location on the stack and load to FP stack.
2738 if (is_float) {
2739 Location stack_temp = Location::StackSlot(temp_offset);
2740 codegen_->Move(stack_temp, source);
2741 __ flds(Address(CpuRegister(RSP), temp_offset));
2742 } else {
2743 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2744 codegen_->Move(stack_temp, source);
2745 __ fldl(Address(CpuRegister(RSP), temp_offset));
2746 }
2747 }
2748}
2749
2750void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2751 Primitive::Type type = rem->GetResultType();
2752 bool is_float = type == Primitive::kPrimFloat;
2753 size_t elem_size = Primitive::ComponentSize(type);
2754 LocationSummary* locations = rem->GetLocations();
2755 Location first = locations->InAt(0);
2756 Location second = locations->InAt(1);
2757 Location out = locations->Out();
2758
2759 // Create stack space for 2 elements.
2760 // TODO: enhance register allocator to ask for stack temporaries.
2761 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2762
2763 // Load the values to the FP stack in reverse order, using temporaries if needed.
2764 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2765 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2766
2767 // Loop doing FPREM until we stabilize.
2768 Label retry;
2769 __ Bind(&retry);
2770 __ fprem();
2771
2772 // Move FP status to AX.
2773 __ fstsw();
2774
2775 // And see if the argument reduction is complete. This is signaled by the
2776 // C2 FPU flag bit set to 0.
2777 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2778 __ j(kNotEqual, &retry);
2779
2780 // We have settled on the final value. Retrieve it into an XMM register.
2781 // Store FP top of stack to real stack.
2782 if (is_float) {
2783 __ fsts(Address(CpuRegister(RSP), 0));
2784 } else {
2785 __ fstl(Address(CpuRegister(RSP), 0));
2786 }
2787
2788 // Pop the 2 items from the FP stack.
2789 __ fucompp();
2790
2791 // Load the value from the stack into an XMM register.
2792 DCHECK(out.IsFpuRegister()) << out;
2793 if (is_float) {
2794 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2795 } else {
2796 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2797 }
2798
2799 // And remove the temporary stack space we allocated.
2800 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2801}
2802
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002803void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2804 DCHECK(instruction->IsDiv() || instruction->IsRem());
2805
2806 LocationSummary* locations = instruction->GetLocations();
2807 Location second = locations->InAt(1);
2808 DCHECK(second.IsConstant());
2809
2810 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2811 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002812 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002813
2814 DCHECK(imm == 1 || imm == -1);
2815
2816 switch (instruction->GetResultType()) {
2817 case Primitive::kPrimInt: {
2818 if (instruction->IsRem()) {
2819 __ xorl(output_register, output_register);
2820 } else {
2821 __ movl(output_register, input_register);
2822 if (imm == -1) {
2823 __ negl(output_register);
2824 }
2825 }
2826 break;
2827 }
2828
2829 case Primitive::kPrimLong: {
2830 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002831 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002832 } else {
2833 __ movq(output_register, input_register);
2834 if (imm == -1) {
2835 __ negq(output_register);
2836 }
2837 }
2838 break;
2839 }
2840
2841 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002842 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002843 }
2844}
2845
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002846void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002847 LocationSummary* locations = instruction->GetLocations();
2848 Location second = locations->InAt(1);
2849
2850 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2851 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2852
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002853 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002854
2855 DCHECK(IsPowerOfTwo(std::abs(imm)));
2856
2857 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2858
2859 if (instruction->GetResultType() == Primitive::kPrimInt) {
2860 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2861 __ testl(numerator, numerator);
2862 __ cmov(kGreaterEqual, tmp, numerator);
2863 int shift = CTZ(imm);
2864 __ sarl(tmp, Immediate(shift));
2865
2866 if (imm < 0) {
2867 __ negl(tmp);
2868 }
2869
2870 __ movl(output_register, tmp);
2871 } else {
2872 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2873 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2874
Mark Mendell92e83bf2015-05-07 11:25:03 -04002875 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002876 __ addq(rdx, numerator);
2877 __ testq(numerator, numerator);
2878 __ cmov(kGreaterEqual, rdx, numerator);
2879 int shift = CTZ(imm);
2880 __ sarq(rdx, Immediate(shift));
2881
2882 if (imm < 0) {
2883 __ negq(rdx);
2884 }
2885
2886 __ movq(output_register, rdx);
2887 }
2888}
2889
2890void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2891 DCHECK(instruction->IsDiv() || instruction->IsRem());
2892
2893 LocationSummary* locations = instruction->GetLocations();
2894 Location second = locations->InAt(1);
2895
2896 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2897 : locations->GetTemp(0).AsRegister<CpuRegister>();
2898 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2899 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2900 : locations->Out().AsRegister<CpuRegister>();
2901 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2902
2903 DCHECK_EQ(RAX, eax.AsRegister());
2904 DCHECK_EQ(RDX, edx.AsRegister());
2905 if (instruction->IsDiv()) {
2906 DCHECK_EQ(RAX, out.AsRegister());
2907 } else {
2908 DCHECK_EQ(RDX, out.AsRegister());
2909 }
2910
2911 int64_t magic;
2912 int shift;
2913
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002914 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002915 if (instruction->GetResultType() == Primitive::kPrimInt) {
2916 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2917
2918 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2919
2920 __ movl(numerator, eax);
2921
2922 Label no_div;
2923 Label end;
2924 __ testl(eax, eax);
2925 __ j(kNotEqual, &no_div);
2926
2927 __ xorl(out, out);
2928 __ jmp(&end);
2929
2930 __ Bind(&no_div);
2931
2932 __ movl(eax, Immediate(magic));
2933 __ imull(numerator);
2934
2935 if (imm > 0 && magic < 0) {
2936 __ addl(edx, numerator);
2937 } else if (imm < 0 && magic > 0) {
2938 __ subl(edx, numerator);
2939 }
2940
2941 if (shift != 0) {
2942 __ sarl(edx, Immediate(shift));
2943 }
2944
2945 __ movl(eax, edx);
2946 __ shrl(edx, Immediate(31));
2947 __ addl(edx, eax);
2948
2949 if (instruction->IsRem()) {
2950 __ movl(eax, numerator);
2951 __ imull(edx, Immediate(imm));
2952 __ subl(eax, edx);
2953 __ movl(edx, eax);
2954 } else {
2955 __ movl(eax, edx);
2956 }
2957 __ Bind(&end);
2958 } else {
2959 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2960
2961 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2962
2963 CpuRegister rax = eax;
2964 CpuRegister rdx = edx;
2965
2966 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2967
2968 // Save the numerator.
2969 __ movq(numerator, rax);
2970
2971 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002972 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002973
2974 // RDX:RAX = magic * numerator
2975 __ imulq(numerator);
2976
2977 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002978 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002979 __ addq(rdx, numerator);
2980 } else if (imm < 0 && magic > 0) {
2981 // RDX -= numerator
2982 __ subq(rdx, numerator);
2983 }
2984
2985 // Shift if needed.
2986 if (shift != 0) {
2987 __ sarq(rdx, Immediate(shift));
2988 }
2989
2990 // RDX += 1 if RDX < 0
2991 __ movq(rax, rdx);
2992 __ shrq(rdx, Immediate(63));
2993 __ addq(rdx, rax);
2994
2995 if (instruction->IsRem()) {
2996 __ movq(rax, numerator);
2997
2998 if (IsInt<32>(imm)) {
2999 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3000 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003001 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003002 }
3003
3004 __ subq(rax, rdx);
3005 __ movq(rdx, rax);
3006 } else {
3007 __ movq(rax, rdx);
3008 }
3009 }
3010}
3011
Calin Juravlebacfec32014-11-14 15:54:36 +00003012void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3013 DCHECK(instruction->IsDiv() || instruction->IsRem());
3014 Primitive::Type type = instruction->GetResultType();
3015 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3016
3017 bool is_div = instruction->IsDiv();
3018 LocationSummary* locations = instruction->GetLocations();
3019
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003020 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3021 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003022
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003024 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003025
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003026 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003027 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003028
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003029 if (imm == 0) {
3030 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3031 } else if (imm == 1 || imm == -1) {
3032 DivRemOneOrMinusOne(instruction);
3033 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003034 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003035 } else {
3036 DCHECK(imm <= -2 || imm >= 2);
3037 GenerateDivRemWithAnyConstant(instruction);
3038 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003039 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003040 SlowPathCodeX86_64* slow_path =
3041 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3042 out.AsRegister(), type, is_div);
3043 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003044
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003045 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3046 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3047 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3048 // so it's safe to just use negl instead of more complex comparisons.
3049 if (type == Primitive::kPrimInt) {
3050 __ cmpl(second_reg, Immediate(-1));
3051 __ j(kEqual, slow_path->GetEntryLabel());
3052 // edx:eax <- sign-extended of eax
3053 __ cdq();
3054 // eax = quotient, edx = remainder
3055 __ idivl(second_reg);
3056 } else {
3057 __ cmpq(second_reg, Immediate(-1));
3058 __ j(kEqual, slow_path->GetEntryLabel());
3059 // rdx:rax <- sign-extended of rax
3060 __ cqo();
3061 // rax = quotient, rdx = remainder
3062 __ idivq(second_reg);
3063 }
3064 __ Bind(slow_path->GetExitLabel());
3065 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003066}
3067
Calin Juravle7c4954d2014-10-28 16:57:40 +00003068void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3069 LocationSummary* locations =
3070 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3071 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003072 case Primitive::kPrimInt:
3073 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003074 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003075 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003076 locations->SetOut(Location::SameAsFirstInput());
3077 // Intel uses edx:eax as the dividend.
3078 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003079 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3080 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3081 // output and request another temp.
3082 if (div->InputAt(1)->IsConstant()) {
3083 locations->AddTemp(Location::RequiresRegister());
3084 }
Calin Juravled0d48522014-11-04 16:40:20 +00003085 break;
3086 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003087
Calin Juravle7c4954d2014-10-28 16:57:40 +00003088 case Primitive::kPrimFloat:
3089 case Primitive::kPrimDouble: {
3090 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003091 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003092 locations->SetOut(Location::SameAsFirstInput());
3093 break;
3094 }
3095
3096 default:
3097 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3098 }
3099}
3100
3101void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3102 LocationSummary* locations = div->GetLocations();
3103 Location first = locations->InAt(0);
3104 Location second = locations->InAt(1);
3105 DCHECK(first.Equals(locations->Out()));
3106
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003107 Primitive::Type type = div->GetResultType();
3108 switch (type) {
3109 case Primitive::kPrimInt:
3110 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003111 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003112 break;
3113 }
3114
Calin Juravle7c4954d2014-10-28 16:57:40 +00003115 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003116 if (second.IsFpuRegister()) {
3117 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3118 } else if (second.IsConstant()) {
3119 __ divss(first.AsFpuRegister<XmmRegister>(),
3120 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3121 } else {
3122 DCHECK(second.IsStackSlot());
3123 __ divss(first.AsFpuRegister<XmmRegister>(),
3124 Address(CpuRegister(RSP), second.GetStackIndex()));
3125 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003126 break;
3127 }
3128
3129 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003130 if (second.IsFpuRegister()) {
3131 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3132 } else if (second.IsConstant()) {
3133 __ divsd(first.AsFpuRegister<XmmRegister>(),
3134 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3135 } else {
3136 DCHECK(second.IsDoubleStackSlot());
3137 __ divsd(first.AsFpuRegister<XmmRegister>(),
3138 Address(CpuRegister(RSP), second.GetStackIndex()));
3139 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003140 break;
3141 }
3142
3143 default:
3144 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3145 }
3146}
3147
Calin Juravlebacfec32014-11-14 15:54:36 +00003148void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003149 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003150 LocationSummary* locations =
3151 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003152
3153 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003154 case Primitive::kPrimInt:
3155 case Primitive::kPrimLong: {
3156 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003157 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003158 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3159 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003160 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3161 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3162 // output and request another temp.
3163 if (rem->InputAt(1)->IsConstant()) {
3164 locations->AddTemp(Location::RequiresRegister());
3165 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003166 break;
3167 }
3168
3169 case Primitive::kPrimFloat:
3170 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003171 locations->SetInAt(0, Location::Any());
3172 locations->SetInAt(1, Location::Any());
3173 locations->SetOut(Location::RequiresFpuRegister());
3174 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003175 break;
3176 }
3177
3178 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003179 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003180 }
3181}
3182
3183void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3184 Primitive::Type type = rem->GetResultType();
3185 switch (type) {
3186 case Primitive::kPrimInt:
3187 case Primitive::kPrimLong: {
3188 GenerateDivRemIntegral(rem);
3189 break;
3190 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003191 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003192 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003193 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003194 break;
3195 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003196 default:
3197 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3198 }
3199}
3200
Calin Juravled0d48522014-11-04 16:40:20 +00003201void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3202 LocationSummary* locations =
3203 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3204 locations->SetInAt(0, Location::Any());
3205 if (instruction->HasUses()) {
3206 locations->SetOut(Location::SameAsFirstInput());
3207 }
3208}
3209
3210void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3211 SlowPathCodeX86_64* slow_path =
3212 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3213 codegen_->AddSlowPath(slow_path);
3214
3215 LocationSummary* locations = instruction->GetLocations();
3216 Location value = locations->InAt(0);
3217
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003218 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003219 case Primitive::kPrimByte:
3220 case Primitive::kPrimChar:
3221 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003222 case Primitive::kPrimInt: {
3223 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003224 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003225 __ j(kEqual, slow_path->GetEntryLabel());
3226 } else if (value.IsStackSlot()) {
3227 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3228 __ j(kEqual, slow_path->GetEntryLabel());
3229 } else {
3230 DCHECK(value.IsConstant()) << value;
3231 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3232 __ jmp(slow_path->GetEntryLabel());
3233 }
3234 }
3235 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003236 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003237 case Primitive::kPrimLong: {
3238 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003239 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003240 __ j(kEqual, slow_path->GetEntryLabel());
3241 } else if (value.IsDoubleStackSlot()) {
3242 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3243 __ j(kEqual, slow_path->GetEntryLabel());
3244 } else {
3245 DCHECK(value.IsConstant()) << value;
3246 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3247 __ jmp(slow_path->GetEntryLabel());
3248 }
3249 }
3250 break;
3251 }
3252 default:
3253 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003254 }
Calin Juravled0d48522014-11-04 16:40:20 +00003255}
3256
Calin Juravle9aec02f2014-11-18 23:06:35 +00003257void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3258 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3259
3260 LocationSummary* locations =
3261 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3262
3263 switch (op->GetResultType()) {
3264 case Primitive::kPrimInt:
3265 case Primitive::kPrimLong: {
3266 locations->SetInAt(0, Location::RequiresRegister());
3267 // The shift count needs to be in CL.
3268 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3269 locations->SetOut(Location::SameAsFirstInput());
3270 break;
3271 }
3272 default:
3273 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3274 }
3275}
3276
3277void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3278 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3279
3280 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003281 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003282 Location second = locations->InAt(1);
3283
3284 switch (op->GetResultType()) {
3285 case Primitive::kPrimInt: {
3286 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003287 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003288 if (op->IsShl()) {
3289 __ shll(first_reg, second_reg);
3290 } else if (op->IsShr()) {
3291 __ sarl(first_reg, second_reg);
3292 } else {
3293 __ shrl(first_reg, second_reg);
3294 }
3295 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003296 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003297 if (op->IsShl()) {
3298 __ shll(first_reg, imm);
3299 } else if (op->IsShr()) {
3300 __ sarl(first_reg, imm);
3301 } else {
3302 __ shrl(first_reg, imm);
3303 }
3304 }
3305 break;
3306 }
3307 case Primitive::kPrimLong: {
3308 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003309 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003310 if (op->IsShl()) {
3311 __ shlq(first_reg, second_reg);
3312 } else if (op->IsShr()) {
3313 __ sarq(first_reg, second_reg);
3314 } else {
3315 __ shrq(first_reg, second_reg);
3316 }
3317 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003318 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003319 if (op->IsShl()) {
3320 __ shlq(first_reg, imm);
3321 } else if (op->IsShr()) {
3322 __ sarq(first_reg, imm);
3323 } else {
3324 __ shrq(first_reg, imm);
3325 }
3326 }
3327 break;
3328 }
3329 default:
3330 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3331 }
3332}
3333
3334void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3335 HandleShift(shl);
3336}
3337
3338void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3339 HandleShift(shl);
3340}
3341
3342void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3343 HandleShift(shr);
3344}
3345
3346void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3347 HandleShift(shr);
3348}
3349
3350void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3351 HandleShift(ushr);
3352}
3353
3354void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3355 HandleShift(ushr);
3356}
3357
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003358void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003359 LocationSummary* locations =
3360 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003361 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003362 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003363 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003364 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003365}
3366
3367void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3368 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003369 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3370 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003371 // Note: if heap poisoning is enabled, the entry point takes cares
3372 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003373
3374 codegen_->InvokeRuntime(
3375 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3376 instruction,
3377 instruction->GetDexPc(),
3378 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003379
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003380 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003381}
3382
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003383void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3384 LocationSummary* locations =
3385 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3386 InvokeRuntimeCallingConvention calling_convention;
3387 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003388 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003389 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003390 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003391}
3392
3393void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3394 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003395 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3396 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003397
Roland Levillain4d027112015-07-01 15:41:14 +01003398 // Note: if heap poisoning is enabled, the entry point takes cares
3399 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003400 codegen_->InvokeRuntime(
3401 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3402 instruction,
3403 instruction->GetDexPc(),
3404 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003405
3406 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003407}
3408
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003409void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003410 LocationSummary* locations =
3411 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003412 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3413 if (location.IsStackSlot()) {
3414 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3415 } else if (location.IsDoubleStackSlot()) {
3416 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3417 }
3418 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003419}
3420
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003421void InstructionCodeGeneratorX86_64::VisitParameterValue(
3422 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003423 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003424}
3425
3426void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3427 LocationSummary* locations =
3428 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3429 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3430}
3431
3432void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3433 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3434 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003435}
3436
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003437void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003438 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003439 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003440 locations->SetInAt(0, Location::RequiresRegister());
3441 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003442}
3443
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003444void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3445 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003446 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3447 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003448 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003449 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003450 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003451 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003452 break;
3453
3454 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003455 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003456 break;
3457
3458 default:
3459 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3460 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003461}
3462
David Brazdil66d126e2015-04-03 16:02:44 +01003463void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3464 LocationSummary* locations =
3465 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3466 locations->SetInAt(0, Location::RequiresRegister());
3467 locations->SetOut(Location::SameAsFirstInput());
3468}
3469
3470void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003471 LocationSummary* locations = bool_not->GetLocations();
3472 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3473 locations->Out().AsRegister<CpuRegister>().AsRegister());
3474 Location out = locations->Out();
3475 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3476}
3477
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003478void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003479 LocationSummary* locations =
3480 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003481 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3482 locations->SetInAt(i, Location::Any());
3483 }
3484 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003485}
3486
3487void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003488 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003489 LOG(FATAL) << "Unimplemented";
3490}
3491
Calin Juravle52c48962014-12-16 17:02:57 +00003492void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3493 /*
3494 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3495 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3496 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3497 */
3498 switch (kind) {
3499 case MemBarrierKind::kAnyAny: {
3500 __ mfence();
3501 break;
3502 }
3503 case MemBarrierKind::kAnyStore:
3504 case MemBarrierKind::kLoadAny:
3505 case MemBarrierKind::kStoreStore: {
3506 // nop
3507 break;
3508 }
3509 default:
3510 LOG(FATAL) << "Unexpected memory barier " << kind;
3511 }
3512}
3513
3514void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3515 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3516
Nicolas Geoffray39468442014-09-02 15:17:15 +01003517 LocationSummary* locations =
3518 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003519 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003520 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3521 locations->SetOut(Location::RequiresFpuRegister());
3522 } else {
3523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3524 }
Calin Juravle52c48962014-12-16 17:02:57 +00003525}
3526
3527void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3528 const FieldInfo& field_info) {
3529 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3530
3531 LocationSummary* locations = instruction->GetLocations();
3532 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3533 Location out = locations->Out();
3534 bool is_volatile = field_info.IsVolatile();
3535 Primitive::Type field_type = field_info.GetFieldType();
3536 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3537
3538 switch (field_type) {
3539 case Primitive::kPrimBoolean: {
3540 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3541 break;
3542 }
3543
3544 case Primitive::kPrimByte: {
3545 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3546 break;
3547 }
3548
3549 case Primitive::kPrimShort: {
3550 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3551 break;
3552 }
3553
3554 case Primitive::kPrimChar: {
3555 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3556 break;
3557 }
3558
3559 case Primitive::kPrimInt:
3560 case Primitive::kPrimNot: {
3561 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3562 break;
3563 }
3564
3565 case Primitive::kPrimLong: {
3566 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3567 break;
3568 }
3569
3570 case Primitive::kPrimFloat: {
3571 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3572 break;
3573 }
3574
3575 case Primitive::kPrimDouble: {
3576 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3577 break;
3578 }
3579
3580 case Primitive::kPrimVoid:
3581 LOG(FATAL) << "Unreachable type " << field_type;
3582 UNREACHABLE();
3583 }
3584
Calin Juravle77520bc2015-01-12 18:45:46 +00003585 codegen_->MaybeRecordImplicitNullCheck(instruction);
3586
Calin Juravle52c48962014-12-16 17:02:57 +00003587 if (is_volatile) {
3588 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3589 }
Roland Levillain4d027112015-07-01 15:41:14 +01003590
3591 if (field_type == Primitive::kPrimNot) {
3592 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3593 }
Calin Juravle52c48962014-12-16 17:02:57 +00003594}
3595
3596void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3597 const FieldInfo& field_info) {
3598 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3599
3600 LocationSummary* locations =
3601 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003602 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003603 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003604 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003605
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003606 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003607 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3608 locations->SetInAt(1, Location::RequiresFpuRegister());
3609 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003610 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003611 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003612 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003613 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003614 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003615 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003616 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3617 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003618 locations->AddTemp(Location::RequiresRegister());
3619 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003620}
3621
Calin Juravle52c48962014-12-16 17:02:57 +00003622void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003623 const FieldInfo& field_info,
3624 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003625 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3626
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003627 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003628 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3629 Location value = locations->InAt(1);
3630 bool is_volatile = field_info.IsVolatile();
3631 Primitive::Type field_type = field_info.GetFieldType();
3632 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3633
3634 if (is_volatile) {
3635 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3636 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003637
3638 switch (field_type) {
3639 case Primitive::kPrimBoolean:
3640 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003641 if (value.IsConstant()) {
3642 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3643 __ movb(Address(base, offset), Immediate(v));
3644 } else {
3645 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3646 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003647 break;
3648 }
3649
3650 case Primitive::kPrimShort:
3651 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003652 if (value.IsConstant()) {
3653 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3654 __ movw(Address(base, offset), Immediate(v));
3655 } else {
3656 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3657 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003658 break;
3659 }
3660
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003661 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003662 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003663 if (value.IsConstant()) {
3664 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003665 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3666 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3667 // Note: if heap poisoning is enabled, no need to poison
3668 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003669 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003670 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003671 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3672 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3673 __ movl(temp, value.AsRegister<CpuRegister>());
3674 __ PoisonHeapReference(temp);
3675 __ movl(Address(base, offset), temp);
3676 } else {
3677 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3678 }
Mark Mendell40741f32015-04-20 22:10:34 -04003679 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003680 break;
3681 }
3682
3683 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003684 if (value.IsConstant()) {
3685 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3686 DCHECK(IsInt<32>(v));
3687 int32_t v_32 = v;
3688 __ movq(Address(base, offset), Immediate(v_32));
3689 } else {
3690 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3691 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003692 break;
3693 }
3694
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003695 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003696 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003697 break;
3698 }
3699
3700 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003701 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003702 break;
3703 }
3704
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003705 case Primitive::kPrimVoid:
3706 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003707 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003708 }
Calin Juravle52c48962014-12-16 17:02:57 +00003709
Calin Juravle77520bc2015-01-12 18:45:46 +00003710 codegen_->MaybeRecordImplicitNullCheck(instruction);
3711
3712 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3713 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3714 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003715 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003716 }
3717
Calin Juravle52c48962014-12-16 17:02:57 +00003718 if (is_volatile) {
3719 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3720 }
3721}
3722
3723void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3724 HandleFieldSet(instruction, instruction->GetFieldInfo());
3725}
3726
3727void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003728 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003729}
3730
3731void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003732 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003733}
3734
3735void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003736 HandleFieldGet(instruction, instruction->GetFieldInfo());
3737}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003738
Calin Juravle52c48962014-12-16 17:02:57 +00003739void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3740 HandleFieldGet(instruction);
3741}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003742
Calin Juravle52c48962014-12-16 17:02:57 +00003743void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3744 HandleFieldGet(instruction, instruction->GetFieldInfo());
3745}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003746
Calin Juravle52c48962014-12-16 17:02:57 +00003747void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3748 HandleFieldSet(instruction, instruction->GetFieldInfo());
3749}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003750
Calin Juravle52c48962014-12-16 17:02:57 +00003751void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003752 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003753}
3754
3755void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003756 LocationSummary* locations =
3757 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003758 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3759 ? Location::RequiresRegister()
3760 : Location::Any();
3761 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003762 if (instruction->HasUses()) {
3763 locations->SetOut(Location::SameAsFirstInput());
3764 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003765}
3766
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003767void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003768 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3769 return;
3770 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003771 LocationSummary* locations = instruction->GetLocations();
3772 Location obj = locations->InAt(0);
3773
3774 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3775 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3776}
3777
3778void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003779 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003780 codegen_->AddSlowPath(slow_path);
3781
3782 LocationSummary* locations = instruction->GetLocations();
3783 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003784
3785 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003786 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003787 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003788 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003789 } else {
3790 DCHECK(obj.IsConstant()) << obj;
3791 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3792 __ jmp(slow_path->GetEntryLabel());
3793 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003794 }
3795 __ j(kEqual, slow_path->GetEntryLabel());
3796}
3797
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003798void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3799 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3800 GenerateImplicitNullCheck(instruction);
3801 } else {
3802 GenerateExplicitNullCheck(instruction);
3803 }
3804}
3805
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003807 LocationSummary* locations =
3808 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003809 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003810 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003811 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3812 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3813 } else {
3814 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3815 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003816}
3817
3818void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3819 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003820 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003821 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003822 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003823
Roland Levillain4d027112015-07-01 15:41:14 +01003824 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003825 case Primitive::kPrimBoolean: {
3826 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003827 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003828 if (index.IsConstant()) {
3829 __ movzxb(out, Address(obj,
3830 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3831 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003832 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003833 }
3834 break;
3835 }
3836
3837 case Primitive::kPrimByte: {
3838 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003839 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003840 if (index.IsConstant()) {
3841 __ movsxb(out, Address(obj,
3842 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3843 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003844 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003845 }
3846 break;
3847 }
3848
3849 case Primitive::kPrimShort: {
3850 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003851 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003852 if (index.IsConstant()) {
3853 __ movsxw(out, Address(obj,
3854 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3855 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003856 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003857 }
3858 break;
3859 }
3860
3861 case Primitive::kPrimChar: {
3862 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003863 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003864 if (index.IsConstant()) {
3865 __ movzxw(out, Address(obj,
3866 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3867 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003869 }
3870 break;
3871 }
3872
3873 case Primitive::kPrimInt:
3874 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003875 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3876 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003877 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003878 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879 if (index.IsConstant()) {
3880 __ movl(out, Address(obj,
3881 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3882 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003883 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003884 }
3885 break;
3886 }
3887
3888 case Primitive::kPrimLong: {
3889 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003890 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003891 if (index.IsConstant()) {
3892 __ movq(out, Address(obj,
3893 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3894 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003895 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003896 }
3897 break;
3898 }
3899
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003900 case Primitive::kPrimFloat: {
3901 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003902 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003903 if (index.IsConstant()) {
3904 __ movss(out, Address(obj,
3905 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3906 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003907 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003908 }
3909 break;
3910 }
3911
3912 case Primitive::kPrimDouble: {
3913 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003914 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003915 if (index.IsConstant()) {
3916 __ movsd(out, Address(obj,
3917 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3918 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003919 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003920 }
3921 break;
3922 }
3923
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003924 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003925 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003926 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003927 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003928 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003929
3930 if (type == Primitive::kPrimNot) {
3931 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3932 __ MaybeUnpoisonHeapReference(out);
3933 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003934}
3935
3936void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003937 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003938
3939 bool needs_write_barrier =
3940 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3941 bool needs_runtime_call = instruction->NeedsTypeCheck();
3942
Nicolas Geoffray39468442014-09-02 15:17:15 +01003943 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003944 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3945 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003946 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003947 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3948 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3949 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003950 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003951 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003952 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003953 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3954 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003955 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003956 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003957 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3958 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003959 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003960 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003961 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003962
3963 if (needs_write_barrier) {
3964 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003965 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003966 locations->AddTemp(Location::RequiresRegister());
3967 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003968 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003969}
3970
3971void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3972 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003973 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003974 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003975 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003976 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003977 bool needs_runtime_call = locations->WillCall();
3978 bool needs_write_barrier =
3979 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003980
3981 switch (value_type) {
3982 case Primitive::kPrimBoolean:
3983 case Primitive::kPrimByte: {
3984 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003985 if (index.IsConstant()) {
3986 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003987 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003988 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003989 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003990 __ movb(Address(obj, offset),
3991 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003992 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003993 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003994 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003995 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3996 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003997 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003998 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003999 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4000 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004001 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004002 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004003 break;
4004 }
4005
4006 case Primitive::kPrimShort:
4007 case Primitive::kPrimChar: {
4008 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004009 if (index.IsConstant()) {
4010 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004011 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004012 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004013 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004014 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00004015 __ movw(Address(obj, offset),
4016 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004017 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004018 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004019 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004020 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004021 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
4022 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004023 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004024 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00004025 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004026 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4027 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004028 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004029 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004030 break;
4031 }
4032
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004033 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004034 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004035 if (!needs_runtime_call) {
4036 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4037 if (index.IsConstant()) {
4038 size_t offset =
4039 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4040 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004041 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4042 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4043 __ movl(temp, value.AsRegister<CpuRegister>());
4044 __ PoisonHeapReference(temp);
4045 __ movl(Address(obj, offset), temp);
4046 } else {
4047 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
4048 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004049 } else {
4050 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004051 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004052 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4053 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4054 // Note: if heap poisoning is enabled, no need to poison
4055 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04004056 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004057 }
4058 } else {
4059 DCHECK(index.IsRegister()) << index;
4060 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004061 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4062 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4063 __ movl(temp, value.AsRegister<CpuRegister>());
4064 __ PoisonHeapReference(temp);
4065 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4066 } else {
4067 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4068 value.AsRegister<CpuRegister>());
4069 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004070 } else {
4071 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004072 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004073 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4074 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4075 // Note: if heap poisoning is enabled, no need to poison
4076 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004077 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004078 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004079 }
4080 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004081 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004082 if (needs_write_barrier) {
4083 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004084 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4085 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004086 codegen_->MarkGCCard(
4087 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004088 }
4089 } else {
4090 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004091 // Note: if heap poisoning is enabled, pAputObject takes cares
4092 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004093 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4094 instruction,
4095 instruction->GetDexPc(),
4096 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004097 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004098 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004099 break;
4100 }
4101
4102 case Primitive::kPrimLong: {
4103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004104 if (index.IsConstant()) {
4105 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004106 if (value.IsRegister()) {
4107 __ movq(Address(obj, offset), 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, offset), Immediate(v_32));
4113 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004114 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004115 if (value.IsRegister()) {
4116 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4117 value.AsRegister<CpuRegister>());
4118 } else {
4119 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4120 DCHECK(IsInt<32>(v));
4121 int32_t v_32 = v;
4122 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4123 Immediate(v_32));
4124 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004125 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004126 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004127 break;
4128 }
4129
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004130 case Primitive::kPrimFloat: {
4131 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4132 if (index.IsConstant()) {
4133 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4134 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004135 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004136 } else {
4137 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4139 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004140 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004141 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004142 break;
4143 }
4144
4145 case Primitive::kPrimDouble: {
4146 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4147 if (index.IsConstant()) {
4148 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4149 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004150 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004151 } else {
4152 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004153 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4154 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004155 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004156 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004157 break;
4158 }
4159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004160 case Primitive::kPrimVoid:
4161 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004162 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004163 }
4164}
4165
4166void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004167 LocationSummary* locations =
4168 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004169 locations->SetInAt(0, Location::RequiresRegister());
4170 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004171}
4172
4173void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4174 LocationSummary* locations = instruction->GetLocations();
4175 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004176 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4177 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004178 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004179 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004180}
4181
4182void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004183 LocationSummary* locations =
4184 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004185 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004186 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004187 if (instruction->HasUses()) {
4188 locations->SetOut(Location::SameAsFirstInput());
4189 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004190}
4191
4192void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4193 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004194 Location index_loc = locations->InAt(0);
4195 Location length_loc = locations->InAt(1);
4196 SlowPathCodeX86_64* slow_path =
4197 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004198
Mark Mendell99dbd682015-04-22 16:18:52 -04004199 if (length_loc.IsConstant()) {
4200 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4201 if (index_loc.IsConstant()) {
4202 // BCE will remove the bounds check if we are guarenteed to pass.
4203 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4204 if (index < 0 || index >= length) {
4205 codegen_->AddSlowPath(slow_path);
4206 __ jmp(slow_path->GetEntryLabel());
4207 } else {
4208 // Some optimization after BCE may have generated this, and we should not
4209 // generate a bounds check if it is a valid range.
4210 }
4211 return;
4212 }
4213
4214 // We have to reverse the jump condition because the length is the constant.
4215 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4216 __ cmpl(index_reg, Immediate(length));
4217 codegen_->AddSlowPath(slow_path);
4218 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004219 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004220 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4221 if (index_loc.IsConstant()) {
4222 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4223 __ cmpl(length, Immediate(value));
4224 } else {
4225 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4226 }
4227 codegen_->AddSlowPath(slow_path);
4228 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004229 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004230}
4231
4232void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4233 CpuRegister card,
4234 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004235 CpuRegister value,
4236 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004237 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004238 if (value_can_be_null) {
4239 __ testl(value, value);
4240 __ j(kEqual, &is_null);
4241 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004242 __ gs()->movq(card, Address::Absolute(
4243 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4244 __ movq(temp, object);
4245 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004246 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004247 if (value_can_be_null) {
4248 __ Bind(&is_null);
4249 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004250}
4251
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004252void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4253 temp->SetLocations(nullptr);
4254}
4255
4256void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4257 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004258 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004259}
4260
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004261void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004262 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004263 LOG(FATAL) << "Unimplemented";
4264}
4265
4266void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004267 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4268}
4269
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004270void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4271 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4272}
4273
4274void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004275 HBasicBlock* block = instruction->GetBlock();
4276 if (block->GetLoopInformation() != nullptr) {
4277 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4278 // The back edge will generate the suspend check.
4279 return;
4280 }
4281 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4282 // The goto will generate the suspend check.
4283 return;
4284 }
4285 GenerateSuspendCheck(instruction, nullptr);
4286}
4287
4288void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4289 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004290 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004291 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4292 if (slow_path == nullptr) {
4293 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4294 instruction->SetSlowPath(slow_path);
4295 codegen_->AddSlowPath(slow_path);
4296 if (successor != nullptr) {
4297 DCHECK(successor->IsLoopHeader());
4298 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4299 }
4300 } else {
4301 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4302 }
4303
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004304 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004305 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004306 if (successor == nullptr) {
4307 __ j(kNotEqual, slow_path->GetEntryLabel());
4308 __ Bind(slow_path->GetReturnLabel());
4309 } else {
4310 __ j(kEqual, codegen_->GetLabelOf(successor));
4311 __ jmp(slow_path->GetEntryLabel());
4312 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004313}
4314
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004315X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4316 return codegen_->GetAssembler();
4317}
4318
4319void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4320 MoveOperands* move = moves_.Get(index);
4321 Location source = move->GetSource();
4322 Location destination = move->GetDestination();
4323
4324 if (source.IsRegister()) {
4325 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004326 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004327 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004328 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004329 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004330 } else {
4331 DCHECK(destination.IsDoubleStackSlot());
4332 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004333 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004334 }
4335 } else if (source.IsStackSlot()) {
4336 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004337 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004338 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004339 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004340 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004341 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004342 } else {
4343 DCHECK(destination.IsStackSlot());
4344 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4345 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4346 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004347 } else if (source.IsDoubleStackSlot()) {
4348 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004349 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004350 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004351 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004352 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4353 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004354 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004355 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004356 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4357 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4358 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004359 } else if (source.IsConstant()) {
4360 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004361 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4362 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004363 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004364 if (value == 0) {
4365 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4366 } else {
4367 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4368 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004369 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004370 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004371 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004372 }
4373 } else if (constant->IsLongConstant()) {
4374 int64_t value = constant->AsLongConstant()->GetValue();
4375 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004376 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004377 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004378 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004379 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004380 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004381 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004382 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004383 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004384 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004385 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4386 if (value == 0) {
4387 // easy FP 0.0.
4388 __ xorps(dest, dest);
4389 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004390 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004391 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004392 } else {
4393 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004394 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004395 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4396 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004397 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004398 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004399 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004400 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004401 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004402 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4403 if (value == 0) {
4404 __ xorpd(dest, dest);
4405 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004406 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004407 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004408 } else {
4409 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004410 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004411 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004412 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004413 } else if (source.IsFpuRegister()) {
4414 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004415 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004416 } else if (destination.IsStackSlot()) {
4417 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004418 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004419 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004420 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004421 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004422 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004423 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004424 }
4425}
4426
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004427void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004428 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004429 __ movl(Address(CpuRegister(RSP), mem), reg);
4430 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004431}
4432
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004433void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004434 ScratchRegisterScope ensure_scratch(
4435 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4436
4437 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4438 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4439 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4440 Address(CpuRegister(RSP), mem2 + stack_offset));
4441 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4442 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4443 CpuRegister(ensure_scratch.GetRegister()));
4444}
4445
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004446void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4447 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4448 __ movq(Address(CpuRegister(RSP), mem), reg);
4449 __ movq(reg, CpuRegister(TMP));
4450}
4451
4452void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4453 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004454 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004455
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004456 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4457 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4458 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4459 Address(CpuRegister(RSP), mem2 + stack_offset));
4460 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4461 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4462 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004463}
4464
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004465void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4466 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4467 __ movss(Address(CpuRegister(RSP), mem), reg);
4468 __ movd(reg, CpuRegister(TMP));
4469}
4470
4471void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4472 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4473 __ movsd(Address(CpuRegister(RSP), mem), reg);
4474 __ movd(reg, CpuRegister(TMP));
4475}
4476
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004477void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4478 MoveOperands* move = moves_.Get(index);
4479 Location source = move->GetSource();
4480 Location destination = move->GetDestination();
4481
4482 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004483 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004484 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004485 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004486 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004487 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004488 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004489 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4490 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004491 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004492 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004493 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004494 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4495 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004496 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004497 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4498 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4499 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004500 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004501 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004502 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004503 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004504 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004505 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004506 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004507 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004508 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004509 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004510 }
4511}
4512
4513
4514void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4515 __ pushq(CpuRegister(reg));
4516}
4517
4518
4519void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4520 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004521}
4522
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004523void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4524 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4525 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4526 Immediate(mirror::Class::kStatusInitialized));
4527 __ j(kLess, slow_path->GetEntryLabel());
4528 __ Bind(slow_path->GetExitLabel());
4529 // No need for memory fence, thanks to the X86_64 memory model.
4530}
4531
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004532void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004533 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4534 ? LocationSummary::kCallOnSlowPath
4535 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004536 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004537 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004538 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004539 locations->SetOut(Location::RequiresRegister());
4540}
4541
4542void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004543 LocationSummary* locations = cls->GetLocations();
4544 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4545 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004546 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004547 DCHECK(!cls->CanCallRuntime());
4548 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004549 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004550 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004551 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004552 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004553 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004554 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004555 __ MaybeUnpoisonHeapReference(out);
4556
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004557 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4558 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4559 codegen_->AddSlowPath(slow_path);
4560 __ testl(out, out);
4561 __ j(kEqual, slow_path->GetEntryLabel());
4562 if (cls->MustGenerateClinitCheck()) {
4563 GenerateClassInitializationCheck(slow_path, out);
4564 } else {
4565 __ Bind(slow_path->GetExitLabel());
4566 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004567 }
4568}
4569
4570void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4571 LocationSummary* locations =
4572 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4573 locations->SetInAt(0, Location::RequiresRegister());
4574 if (check->HasUses()) {
4575 locations->SetOut(Location::SameAsFirstInput());
4576 }
4577}
4578
4579void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004580 // We assume the class to not be null.
4581 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4582 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004583 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004584 GenerateClassInitializationCheck(slow_path,
4585 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004586}
4587
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004588void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4589 LocationSummary* locations =
4590 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004591 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004592 locations->SetOut(Location::RequiresRegister());
4593}
4594
4595void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4596 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4597 codegen_->AddSlowPath(slow_path);
4598
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004599 LocationSummary* locations = load->GetLocations();
4600 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4601 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004602 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004603 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004604 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004605 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004606 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004607 __ testl(out, out);
4608 __ j(kEqual, slow_path->GetEntryLabel());
4609 __ Bind(slow_path->GetExitLabel());
4610}
4611
David Brazdilcb1c0552015-08-04 16:22:25 +01004612static Address GetExceptionTlsAddress() {
4613 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4614}
4615
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004616void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4617 LocationSummary* locations =
4618 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4619 locations->SetOut(Location::RequiresRegister());
4620}
4621
4622void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004623 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4624}
4625
4626void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4627 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4628}
4629
4630void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4631 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004632}
4633
4634void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4635 LocationSummary* locations =
4636 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4637 InvokeRuntimeCallingConvention calling_convention;
4638 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4639}
4640
4641void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004642 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4643 instruction,
4644 instruction->GetDexPc(),
4645 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004646}
4647
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004648void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004649 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4650 ? LocationSummary::kNoCall
4651 : LocationSummary::kCallOnSlowPath;
4652 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4653 locations->SetInAt(0, Location::RequiresRegister());
4654 locations->SetInAt(1, Location::Any());
4655 locations->SetOut(Location::RequiresRegister());
4656}
4657
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004658void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004659 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004660 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004661 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004662 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004663 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4664 Label done, zero;
4665 SlowPathCodeX86_64* slow_path = nullptr;
4666
4667 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004668 // Avoid null check if we know obj is not null.
4669 if (instruction->MustDoNullCheck()) {
4670 __ testl(obj, obj);
4671 __ j(kEqual, &zero);
4672 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004673 // Compare the class of `obj` with `cls`.
4674 __ movl(out, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004675 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004676 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004677 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004678 } else {
4679 DCHECK(cls.IsStackSlot()) << cls;
4680 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4681 }
4682 if (instruction->IsClassFinal()) {
4683 // Classes must be equal for the instanceof to succeed.
4684 __ j(kNotEqual, &zero);
4685 __ movl(out, Immediate(1));
4686 __ jmp(&done);
4687 } else {
4688 // If the classes are not equal, we go into a slow path.
4689 DCHECK(locations->OnlyCallsOnSlowPath());
4690 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004691 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004692 codegen_->AddSlowPath(slow_path);
4693 __ j(kNotEqual, slow_path->GetEntryLabel());
4694 __ movl(out, Immediate(1));
4695 __ jmp(&done);
4696 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004697
4698 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4699 __ Bind(&zero);
4700 __ movl(out, Immediate(0));
4701 }
4702
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004703 if (slow_path != nullptr) {
4704 __ Bind(slow_path->GetExitLabel());
4705 }
4706 __ Bind(&done);
4707}
4708
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004709void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4710 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4711 instruction, LocationSummary::kCallOnSlowPath);
4712 locations->SetInAt(0, Location::RequiresRegister());
4713 locations->SetInAt(1, Location::Any());
4714 locations->AddTemp(Location::RequiresRegister());
4715}
4716
4717void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4718 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004719 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004720 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004721 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004722 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4723 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4724 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4725 codegen_->AddSlowPath(slow_path);
4726
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004727 // Avoid null check if we know obj is not null.
4728 if (instruction->MustDoNullCheck()) {
4729 __ testl(obj, obj);
4730 __ j(kEqual, slow_path->GetExitLabel());
4731 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004732 // Compare the class of `obj` with `cls`.
4733 __ movl(temp, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004734 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004735 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004736 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004737 } else {
4738 DCHECK(cls.IsStackSlot()) << cls;
4739 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4740 }
Roland Levillain4d027112015-07-01 15:41:14 +01004741 // The checkcast succeeds if the classes are equal (fast path).
4742 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004743 __ j(kNotEqual, slow_path->GetEntryLabel());
4744 __ Bind(slow_path->GetExitLabel());
4745}
4746
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004747void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4748 LocationSummary* locations =
4749 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4750 InvokeRuntimeCallingConvention calling_convention;
4751 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4752}
4753
4754void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004755 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4756 : QUICK_ENTRY_POINT(pUnlockObject),
4757 instruction,
4758 instruction->GetDexPc(),
4759 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004760}
4761
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004762void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4763void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4764void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4765
4766void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4767 LocationSummary* locations =
4768 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4769 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4770 || instruction->GetResultType() == Primitive::kPrimLong);
4771 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004772 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004773 locations->SetOut(Location::SameAsFirstInput());
4774}
4775
4776void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4777 HandleBitwiseOperation(instruction);
4778}
4779
4780void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4781 HandleBitwiseOperation(instruction);
4782}
4783
4784void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4785 HandleBitwiseOperation(instruction);
4786}
4787
4788void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4789 LocationSummary* locations = instruction->GetLocations();
4790 Location first = locations->InAt(0);
4791 Location second = locations->InAt(1);
4792 DCHECK(first.Equals(locations->Out()));
4793
4794 if (instruction->GetResultType() == Primitive::kPrimInt) {
4795 if (second.IsRegister()) {
4796 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004797 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004798 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004799 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004800 } else {
4801 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004802 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004803 }
4804 } else if (second.IsConstant()) {
4805 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4806 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004807 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004808 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004809 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004810 } else {
4811 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004812 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004813 }
4814 } else {
4815 Address address(CpuRegister(RSP), second.GetStackIndex());
4816 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004817 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004818 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004819 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004820 } else {
4821 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004822 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004823 }
4824 }
4825 } else {
4826 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004827 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4828 bool second_is_constant = false;
4829 int64_t value = 0;
4830 if (second.IsConstant()) {
4831 second_is_constant = true;
4832 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004833 }
Mark Mendell40741f32015-04-20 22:10:34 -04004834 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004835
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004836 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004837 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004838 if (is_int32_value) {
4839 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4840 } else {
4841 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4842 }
4843 } else if (second.IsDoubleStackSlot()) {
4844 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004845 } else {
4846 __ andq(first_reg, second.AsRegister<CpuRegister>());
4847 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004848 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004849 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004850 if (is_int32_value) {
4851 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4852 } else {
4853 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4854 }
4855 } else if (second.IsDoubleStackSlot()) {
4856 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004857 } else {
4858 __ orq(first_reg, second.AsRegister<CpuRegister>());
4859 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004860 } else {
4861 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004862 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004863 if (is_int32_value) {
4864 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4865 } else {
4866 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4867 }
4868 } else if (second.IsDoubleStackSlot()) {
4869 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004870 } else {
4871 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4872 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004873 }
4874 }
4875}
4876
Calin Juravleb1498f62015-02-16 13:13:29 +00004877void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4878 // Nothing to do, this should be removed during prepare for register allocator.
4879 UNUSED(instruction);
4880 LOG(FATAL) << "Unreachable";
4881}
4882
4883void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4884 // Nothing to do, this should be removed during prepare for register allocator.
4885 UNUSED(instruction);
4886 LOG(FATAL) << "Unreachable";
4887}
4888
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004889void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
4890 DCHECK(codegen_->IsBaseline());
4891 LocationSummary* locations =
4892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4893 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4894}
4895
4896void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4897 DCHECK(codegen_->IsBaseline());
4898 // Will be generated at use site.
4899}
4900
Mark Mendell92e83bf2015-05-07 11:25:03 -04004901void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4902 if (value == 0) {
4903 __ xorl(dest, dest);
4904 } else if (value > 0 && IsInt<32>(value)) {
4905 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4906 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4907 } else {
4908 __ movq(dest, Immediate(value));
4909 }
4910}
4911
Mark Mendellcfa410b2015-05-25 16:02:44 -04004912void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
4913 DCHECK(dest.IsDoubleStackSlot());
4914 if (IsInt<32>(value)) {
4915 // Can move directly as an int32 constant.
4916 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
4917 Immediate(static_cast<int32_t>(value)));
4918 } else {
4919 Load64BitValue(CpuRegister(TMP), value);
4920 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
4921 }
4922}
4923
Mark Mendellf55c3e02015-03-26 21:07:46 -04004924void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4925 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004926 X86_64Assembler* assembler = GetAssembler();
4927 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004928 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4929 // byte values. If used for vectors at a later time, this will need to be
4930 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004931 assembler->Align(4, 0);
4932 constant_area_start_ = assembler->CodeSize();
4933 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004934 }
4935
4936 // And finish up.
4937 CodeGenerator::Finalize(allocator);
4938}
4939
4940/**
4941 * Class to handle late fixup of offsets into constant area.
4942 */
4943class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4944 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004945 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004946 : codegen_(codegen), offset_into_constant_area_(offset) {}
4947
4948 private:
4949 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4950 // Patch the correct offset for the instruction. We use the address of the
4951 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4952 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4953 int relative_position = constant_offset - pos;
4954
4955 // Patch in the right value.
4956 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4957 }
4958
Mark Mendell39dcf552015-04-09 20:42:42 -04004959 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004960
4961 // Location in constant area that the fixup refers to.
4962 int offset_into_constant_area_;
4963};
4964
4965Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4966 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4967 return Address::RIP(fixup);
4968}
4969
4970Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4971 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4972 return Address::RIP(fixup);
4973}
4974
4975Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4976 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4977 return Address::RIP(fixup);
4978}
4979
4980Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4981 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4982 return Address::RIP(fixup);
4983}
4984
Roland Levillain4d027112015-07-01 15:41:14 +01004985#undef __
4986
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004987} // namespace x86_64
4988} // namespace art