blob: 4aae0375cb78ee6d521a64a51b9ddd340fdb7e62 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "intrinsics.h"
25#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "mirror/object_reference.h"
29#include "thread.h"
30#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "utils/x86_64/assembler_x86_64.h"
33#include "utils/x86_64/managed_register_x86_64.h"
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace art {
36
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010037namespace x86_64 {
38
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010039// Some x86_64 instructions require a register to be available as temp.
40static constexpr Register TMP = R11;
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010044
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000045static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000046static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010047
Mark Mendell24f2dfa2015-01-14 19:51:45 -050048static constexpr int kC2ConditionMask = 0x400;
49
Roland Levillain62a46b22015-06-01 18:24:13 +010050#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())->
Alexandre Rames8158f282015-08-07 10:26:17 +010051#define QUICK_ENTRY_POINT(x) Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x), true)
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010053class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Alexandre Rames2ed20af2015-03-06 13:55:35 +000057 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010058 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010060 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
61 instruction_,
62 instruction_->GetDexPc(),
63 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 }
65
Alexandre Rames8158f282015-08-07 10:26:17 +010066 bool IsFatal() const OVERRIDE { return true; }
67
Alexandre Rames9931f312015-06-19 14:47:01 +010068 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
69
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010071 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
73};
74
Calin Juravled0d48522014-11-04 16:40:20 +000075class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
76 public:
77 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
78
Alexandre Rames2ed20af2015-03-06 13:55:35 +000079 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010080 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000081 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010082 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
83 instruction_,
84 instruction_->GetDexPc(),
85 this);
Calin Juravled0d48522014-11-04 16:40:20 +000086 }
87
Alexandre Rames8158f282015-08-07 10:26:17 +010088 bool IsFatal() const OVERRIDE { return true; }
89
Alexandre Rames9931f312015-06-19 14:47:01 +010090 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
91
Calin Juravled0d48522014-11-04 16:40:20 +000092 private:
93 HDivZeroCheck* const instruction_;
94 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
95};
96
Calin Juravlebacfec32014-11-14 15:54:36 +000097class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +000098 public:
Roland Levillain3887c462015-08-12 18:15:42 +010099 DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
Calin Juravlebacfec32014-11-14 15:54:36 +0000100 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000101
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000102 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000103 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000104 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000105 if (is_div_) {
106 __ negl(cpu_reg_);
107 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400108 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 }
110
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000111 } else {
112 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negq(cpu_reg_);
115 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400116 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000117 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
Alexandre Rames9931f312015-06-19 14:47:01 +0100122 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
123
Calin Juravled0d48522014-11-04 16:40:20 +0000124 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 const bool is_div_;
128 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000129};
130
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100133 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000135
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000136 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100137 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000139 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100140 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
141 instruction_,
142 instruction_->GetDexPc(),
143 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000144 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 if (successor_ == nullptr) {
146 __ jmp(GetReturnLabel());
147 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 }
151
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 Label* GetReturnLabel() {
153 DCHECK(successor_ == nullptr);
154 return &return_label_;
155 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100157 HBasicBlock* GetSuccessor() const {
158 return successor_;
159 }
160
Alexandre Rames9931f312015-06-19 14:47:01 +0100161 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
162
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163 private:
164 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100165 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000166 Label return_label_;
167
168 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
169};
170
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100171class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100172 public:
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 Marko58155012015-08-19 12:49:41 +0000414 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
415 switch (invoke->GetMethodLoadKind()) {
416 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
417 // temp = thread->string_init_entrypoint
418 __ gs()->movl(temp.AsRegister<CpuRegister>(),
419 Address::Absolute(invoke->GetStringInitOffset(), true));
420 break;
421 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
422 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
423 break;
424 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
425 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
426 break;
427 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
428 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
429 method_patches_.emplace_back(invoke->GetTargetMethod());
430 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
431 break;
432 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
433 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
434 invoke->GetDexCacheArrayOffset());
435 __ movq(temp.AsRegister<CpuRegister>(),
436 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
437 // Bind the label at the end of the "movl" insn.
438 __ Bind(&pc_rel_dex_cache_patches_.back().label);
439 break;
440 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
441 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
442 Register method_reg;
443 CpuRegister reg = temp.AsRegister<CpuRegister>();
444 if (current_method.IsRegister()) {
445 method_reg = current_method.AsRegister<Register>();
446 } else {
447 DCHECK(invoke->GetLocations()->Intrinsified());
448 DCHECK(!current_method.IsValid());
449 method_reg = reg.AsRegister();
450 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
451 }
452 // temp = temp->dex_cache_resolved_methods_;
453 __ movl(reg, Address(CpuRegister(method_reg),
454 ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
455 // temp = temp[index_in_cache]
456 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
457 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
458 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100459 }
Vladimir Marko58155012015-08-19 12:49:41 +0000460 }
461
462 switch (invoke->GetCodePtrLocation()) {
463 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
464 __ call(&frame_entry_label_);
465 break;
466 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
467 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
468 Label* label = &relative_call_patches_.back().label;
469 __ call(label); // Bind to the patch label, override at link time.
470 __ Bind(label); // Bind the label at the end of the "call" insn.
471 break;
472 }
473 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
474 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
475 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
476 FALLTHROUGH_INTENDED;
477 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
478 // (callee_method + offset_of_quick_compiled_code)()
479 __ call(Address(callee_method.AsRegister<CpuRegister>(),
480 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
481 kX86_64WordSize).SizeValue()));
482 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000483 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800484
485 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800486}
487
Vladimir Marko58155012015-08-19 12:49:41 +0000488void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
489 DCHECK(linker_patches->empty());
490 size_t size =
491 method_patches_.size() + relative_call_patches_.size() + pc_rel_dex_cache_patches_.size();
492 linker_patches->reserve(size);
493 for (const MethodPatchInfo<Label>& info : method_patches_) {
494 // The label points to the end of the "movl" instruction but the literal offset for method
495 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
496 uint32_t literal_offset = info.label.Position() - 4;
497 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
498 info.target_method.dex_file,
499 info.target_method.dex_method_index));
500 }
501 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
502 // The label points to the end of the "call" instruction but the literal offset for method
503 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
504 uint32_t literal_offset = info.label.Position() - 4;
505 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
506 info.target_method.dex_file,
507 info.target_method.dex_method_index));
508 }
509 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
510 // The label points to the end of the "mov" instruction but the literal offset for method
511 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
512 uint32_t literal_offset = info.label.Position() - 4;
513 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
514 &info.target_dex_file,
515 info.label.Position(),
516 info.element_offset));
517 }
518}
519
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100520void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100521 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100522}
523
524void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100525 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100526}
527
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100528size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
529 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
530 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100531}
532
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100533size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
534 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
535 return kX86_64WordSize;
536}
537
538size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
539 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
540 return kX86_64WordSize;
541}
542
543size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
544 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
545 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100546}
547
Alexandre Rames8158f282015-08-07 10:26:17 +0100548void CodeGeneratorX86_64::InvokeRuntime(Address entry_point,
549 HInstruction* instruction,
550 uint32_t dex_pc,
551 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100552 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100553 __ gs()->call(entry_point);
554 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100555}
556
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000557static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000558// Use a fake return address register to mimic Quick.
559static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400560CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
561 const X86_64InstructionSetFeatures& isa_features,
562 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000563 : CodeGenerator(graph,
564 kNumberOfCpuRegisters,
565 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000566 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000567 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
568 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000569 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000570 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
571 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000572 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100573 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100574 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000575 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400576 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400577 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000578 constant_area_start_(0),
579 method_patches_(graph->GetArena()->Adapter()),
580 relative_call_patches_(graph->GetArena()->Adapter()),
581 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000582 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
583}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100584
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100585InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
586 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100587 : HGraphVisitor(graph),
588 assembler_(codegen->GetAssembler()),
589 codegen_(codegen) {}
590
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100591Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100592 switch (type) {
593 case Primitive::kPrimLong:
594 case Primitive::kPrimByte:
595 case Primitive::kPrimBoolean:
596 case Primitive::kPrimChar:
597 case Primitive::kPrimShort:
598 case Primitive::kPrimInt:
599 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100600 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100601 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100602 }
603
604 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100605 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100606 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100607 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100609
610 case Primitive::kPrimVoid:
611 LOG(FATAL) << "Unreachable type " << type;
612 }
613
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100614 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615}
616
Nicolas Geoffray98893962015-01-21 12:32:32 +0000617void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100618 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100619 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100620
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000621 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100622 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000623
Nicolas Geoffray98893962015-01-21 12:32:32 +0000624 if (is_baseline) {
625 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
626 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
627 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000628 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
629 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
630 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000631 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100632}
633
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100634static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100635 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100636}
David Srbecky9d8606d2015-04-12 09:35:32 +0100637
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100638static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100639 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100640}
641
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100642void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100643 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000644 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100645 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700646 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000647 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100648
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000649 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100650 __ testq(CpuRegister(RAX), Address(
651 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100652 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100653 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000654
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000655 if (HasEmptyFrame()) {
656 return;
657 }
658
Nicolas Geoffray98893962015-01-21 12:32:32 +0000659 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000660 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000661 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000662 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100663 __ cfi().AdjustCFAOffset(kX86_64WordSize);
664 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000665 }
666 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100667
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100668 int adjust = GetFrameSize() - GetCoreSpillSize();
669 __ subq(CpuRegister(RSP), Immediate(adjust));
670 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000671 uint32_t xmm_spill_location = GetFpuSpillStart();
672 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100673
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000674 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
675 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100676 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
677 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
678 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000679 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100680 }
681
Mathieu Chartiere401d142015-04-22 13:56:20 -0700682 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100683 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100684}
685
686void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100687 __ cfi().RememberState();
688 if (!HasEmptyFrame()) {
689 uint32_t xmm_spill_location = GetFpuSpillStart();
690 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
691 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
692 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
693 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
694 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
695 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
696 }
697 }
698
699 int adjust = GetFrameSize() - GetCoreSpillSize();
700 __ addq(CpuRegister(RSP), Immediate(adjust));
701 __ cfi().AdjustCFAOffset(-adjust);
702
703 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
704 Register reg = kCoreCalleeSaves[i];
705 if (allocated_registers_.ContainsCoreRegister(reg)) {
706 __ popq(CpuRegister(reg));
707 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
708 __ cfi().Restore(DWARFReg(reg));
709 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000710 }
711 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100712 __ ret();
713 __ cfi().RestoreState();
714 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100715}
716
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100717void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
718 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100719}
720
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100721Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
722 switch (load->GetType()) {
723 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100726
727 case Primitive::kPrimInt:
728 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100729 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100731
732 case Primitive::kPrimBoolean:
733 case Primitive::kPrimByte:
734 case Primitive::kPrimChar:
735 case Primitive::kPrimShort:
736 case Primitive::kPrimVoid:
737 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700738 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100739 }
740
741 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700742 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100743}
744
745void CodeGeneratorX86_64::Move(Location destination, Location source) {
746 if (source.Equals(destination)) {
747 return;
748 }
749 if (destination.IsRegister()) {
750 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000751 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100752 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000753 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100754 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000755 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100756 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100757 } else {
758 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000759 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100760 Address(CpuRegister(RSP), source.GetStackIndex()));
761 }
762 } else if (destination.IsFpuRegister()) {
763 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000764 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100765 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000766 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000768 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100769 Address(CpuRegister(RSP), source.GetStackIndex()));
770 } else {
771 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000772 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100773 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100774 }
775 } else if (destination.IsStackSlot()) {
776 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100777 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000778 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100779 } else if (source.IsFpuRegister()) {
780 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000781 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500782 } else if (source.IsConstant()) {
783 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000784 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500785 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100786 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500787 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000788 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
789 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100790 }
791 } else {
792 DCHECK(destination.IsDoubleStackSlot());
793 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100794 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000795 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 } else if (source.IsFpuRegister()) {
797 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000798 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500799 } else if (source.IsConstant()) {
800 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800801 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500802 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000803 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500804 } else {
805 DCHECK(constant->IsLongConstant());
806 value = constant->AsLongConstant()->GetValue();
807 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400808 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100809 } else {
810 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000811 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
812 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100813 }
814 }
815}
816
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100817void CodeGeneratorX86_64::Move(HInstruction* instruction,
818 Location location,
819 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000820 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100821 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700822 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100823 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000824 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100825 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000826 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000827 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
828 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000829 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000830 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000831 } else if (location.IsStackSlot()) {
832 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
833 } else {
834 DCHECK(location.IsConstant());
835 DCHECK_EQ(location.GetConstant(), const_to_move);
836 }
837 } else if (const_to_move->IsLongConstant()) {
838 int64_t value = const_to_move->AsLongConstant()->GetValue();
839 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400840 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000841 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400842 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000843 } else {
844 DCHECK(location.IsConstant());
845 DCHECK_EQ(location.GetConstant(), const_to_move);
846 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100847 }
Roland Levillain476df552014-10-09 17:51:36 +0100848 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100849 switch (instruction->GetType()) {
850 case Primitive::kPrimBoolean:
851 case Primitive::kPrimByte:
852 case Primitive::kPrimChar:
853 case Primitive::kPrimShort:
854 case Primitive::kPrimInt:
855 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100856 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100857 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
858 break;
859
860 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100861 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000862 Move(location,
863 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100864 break;
865
866 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100867 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100868 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000869 } else if (instruction->IsTemporary()) {
870 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
871 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100872 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100873 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100874 switch (instruction->GetType()) {
875 case Primitive::kPrimBoolean:
876 case Primitive::kPrimByte:
877 case Primitive::kPrimChar:
878 case Primitive::kPrimShort:
879 case Primitive::kPrimInt:
880 case Primitive::kPrimNot:
881 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100882 case Primitive::kPrimFloat:
883 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000884 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100885 break;
886
887 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100888 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100889 }
890 }
891}
892
David Brazdilfc6a86a2015-06-26 10:33:45 +0000893void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100894 DCHECK(!successor->IsExitBlock());
895
896 HBasicBlock* block = got->GetBlock();
897 HInstruction* previous = got->GetPrevious();
898
899 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000900 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100901 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
902 return;
903 }
904
905 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
906 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
907 }
908 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100909 __ jmp(codegen_->GetLabelOf(successor));
910 }
911}
912
David Brazdilfc6a86a2015-06-26 10:33:45 +0000913void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
914 got->SetLocations(nullptr);
915}
916
917void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
918 HandleGoto(got, got->GetSuccessor());
919}
920
921void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
922 try_boundary->SetLocations(nullptr);
923}
924
925void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
926 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
927 if (!successor->IsExitBlock()) {
928 HandleGoto(try_boundary, successor);
929 }
930}
931
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100932void LocationsBuilderX86_64::VisitExit(HExit* exit) {
933 exit->SetLocations(nullptr);
934}
935
936void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700937 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100938}
939
Mark Mendellc4701932015-04-10 13:18:51 -0400940void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
941 Label* true_label,
942 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100943 if (cond->IsFPConditionTrueIfNaN()) {
944 __ j(kUnordered, true_label);
945 } else if (cond->IsFPConditionFalseIfNaN()) {
946 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400947 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100948 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400949}
950
951void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
952 HCondition* condition,
953 Label* true_target,
954 Label* false_target,
955 Label* always_true_target) {
956 LocationSummary* locations = condition->GetLocations();
957 Location left = locations->InAt(0);
958 Location right = locations->InAt(1);
959
960 // We don't want true_target as a nullptr.
961 if (true_target == nullptr) {
962 true_target = always_true_target;
963 }
964 bool falls_through = (false_target == nullptr);
965
966 // FP compares don't like null false_targets.
967 if (false_target == nullptr) {
968 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
969 }
970
971 Primitive::Type type = condition->InputAt(0)->GetType();
972 switch (type) {
973 case Primitive::kPrimLong: {
974 CpuRegister left_reg = left.AsRegister<CpuRegister>();
975 if (right.IsConstant()) {
976 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
977 if (IsInt<32>(value)) {
978 if (value == 0) {
979 __ testq(left_reg, left_reg);
980 } else {
981 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
982 }
983 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100984 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -0400985 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
986 }
987 } else if (right.IsDoubleStackSlot()) {
988 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
989 } else {
990 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
991 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100992 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -0400993 break;
994 }
995 case Primitive::kPrimFloat: {
996 if (right.IsFpuRegister()) {
997 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
998 } else if (right.IsConstant()) {
999 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1000 codegen_->LiteralFloatAddress(
1001 right.GetConstant()->AsFloatConstant()->GetValue()));
1002 } else {
1003 DCHECK(right.IsStackSlot());
1004 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1005 Address(CpuRegister(RSP), right.GetStackIndex()));
1006 }
1007 GenerateFPJumps(condition, true_target, false_target);
1008 break;
1009 }
1010 case Primitive::kPrimDouble: {
1011 if (right.IsFpuRegister()) {
1012 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1013 } else if (right.IsConstant()) {
1014 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1015 codegen_->LiteralDoubleAddress(
1016 right.GetConstant()->AsDoubleConstant()->GetValue()));
1017 } else {
1018 DCHECK(right.IsDoubleStackSlot());
1019 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1020 Address(CpuRegister(RSP), right.GetStackIndex()));
1021 }
1022 GenerateFPJumps(condition, true_target, false_target);
1023 break;
1024 }
1025 default:
1026 LOG(FATAL) << "Unexpected condition type " << type;
1027 }
1028
1029 if (!falls_through) {
1030 __ jmp(false_target);
1031 }
1032}
1033
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001034void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1035 Label* true_target,
1036 Label* false_target,
1037 Label* always_true_target) {
1038 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001039 if (cond->IsIntConstant()) {
1040 // Constant condition, statically compared against 1.
1041 int32_t cond_value = cond->AsIntConstant()->GetValue();
1042 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001043 if (always_true_target != nullptr) {
1044 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001045 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001046 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001047 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001048 DCHECK_EQ(cond_value, 0);
1049 }
1050 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001051 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001052 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1053 // Moves do not affect the eflags register, so if the condition is
1054 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001055 // again. We can't use the eflags on FP conditions if they are
1056 // materialized due to the complex branching.
1057 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001058 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001059 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1060 && !Primitive::IsFloatingPointType(type);
1061
Roland Levillain4fa13f62015-07-06 18:11:54 +01001062 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001063 if (!eflags_set) {
1064 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001065 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001066 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001067 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001068 } else {
1069 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1070 Immediate(0));
1071 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001072 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001073 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001074 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001075 }
1076 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001077 // Condition has not been materialized, use its inputs as the
1078 // comparison and its condition as the branch condition.
1079
Mark Mendellc4701932015-04-10 13:18:51 -04001080 // Is this a long or FP comparison that has been folded into the HCondition?
1081 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001082 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001083 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1084 true_target, false_target, always_true_target);
1085 return;
1086 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001087
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001088 Location lhs = cond->GetLocations()->InAt(0);
1089 Location rhs = cond->GetLocations()->InAt(1);
1090 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001091 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001092 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001093 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001094 if (constant == 0) {
1095 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1096 } else {
1097 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1098 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001099 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001100 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001101 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1102 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001103 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001104 }
Dave Allison20dfc792014-06-16 20:44:29 -07001105 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001106 if (false_target != nullptr) {
1107 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108 }
1109}
1110
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001111void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1112 LocationSummary* locations =
1113 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1114 HInstruction* cond = if_instr->InputAt(0);
1115 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1116 locations->SetInAt(0, Location::Any());
1117 }
1118}
1119
1120void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1121 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1122 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1123 Label* always_true_target = true_target;
1124 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1125 if_instr->IfTrueSuccessor())) {
1126 always_true_target = nullptr;
1127 }
1128 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1129 if_instr->IfFalseSuccessor())) {
1130 false_target = nullptr;
1131 }
1132 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1133}
1134
1135void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1136 LocationSummary* locations = new (GetGraph()->GetArena())
1137 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1138 HInstruction* cond = deoptimize->InputAt(0);
1139 DCHECK(cond->IsCondition());
1140 if (cond->AsCondition()->NeedsMaterialization()) {
1141 locations->SetInAt(0, Location::Any());
1142 }
1143}
1144
1145void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1146 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
1147 DeoptimizationSlowPathX86_64(deoptimize);
1148 codegen_->AddSlowPath(slow_path);
1149 Label* slow_path_entry = slow_path->GetEntryLabel();
1150 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1151}
1152
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001153void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1154 local->SetLocations(nullptr);
1155}
1156
1157void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1158 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1159}
1160
1161void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1162 local->SetLocations(nullptr);
1163}
1164
1165void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1166 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001167 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001168}
1169
1170void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001171 LocationSummary* locations =
1172 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001173 switch (store->InputAt(1)->GetType()) {
1174 case Primitive::kPrimBoolean:
1175 case Primitive::kPrimByte:
1176 case Primitive::kPrimChar:
1177 case Primitive::kPrimShort:
1178 case Primitive::kPrimInt:
1179 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001180 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001181 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1182 break;
1183
1184 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001185 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001186 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1187 break;
1188
1189 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001190 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001191 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001192}
1193
1194void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001195 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196}
1197
Roland Levillain0d37cd02015-05-27 16:39:19 +01001198void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001199 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001200 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001201 // Handle the long/FP comparisons made in instruction simplification.
1202 switch (cond->InputAt(0)->GetType()) {
1203 case Primitive::kPrimLong:
1204 locations->SetInAt(0, Location::RequiresRegister());
1205 locations->SetInAt(1, Location::Any());
1206 break;
1207 case Primitive::kPrimFloat:
1208 case Primitive::kPrimDouble:
1209 locations->SetInAt(0, Location::RequiresFpuRegister());
1210 locations->SetInAt(1, Location::Any());
1211 break;
1212 default:
1213 locations->SetInAt(0, Location::RequiresRegister());
1214 locations->SetInAt(1, Location::Any());
1215 break;
1216 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001217 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001218 locations->SetOut(Location::RequiresRegister());
1219 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001220}
1221
Roland Levillain0d37cd02015-05-27 16:39:19 +01001222void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001223 if (!cond->NeedsMaterialization()) {
1224 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001225 }
Mark Mendellc4701932015-04-10 13:18:51 -04001226
1227 LocationSummary* locations = cond->GetLocations();
1228 Location lhs = locations->InAt(0);
1229 Location rhs = locations->InAt(1);
1230 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1231 Label true_label, false_label;
1232
1233 switch (cond->InputAt(0)->GetType()) {
1234 default:
1235 // Integer case.
1236
1237 // Clear output register: setcc only sets the low byte.
1238 __ xorl(reg, reg);
1239
1240 if (rhs.IsRegister()) {
1241 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1242 } else if (rhs.IsConstant()) {
1243 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1244 if (constant == 0) {
1245 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1246 } else {
1247 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1248 }
1249 } else {
1250 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1251 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001252 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001253 return;
1254 case Primitive::kPrimLong:
1255 // Clear output register: setcc only sets the low byte.
1256 __ xorl(reg, reg);
1257
1258 if (rhs.IsRegister()) {
1259 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1260 } else if (rhs.IsConstant()) {
1261 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1262 if (IsInt<32>(value)) {
1263 if (value == 0) {
1264 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1265 } else {
1266 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1267 }
1268 } else {
1269 // Value won't fit in an int.
1270 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1271 }
1272 } else {
1273 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1274 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001275 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001276 return;
1277 case Primitive::kPrimFloat: {
1278 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1279 if (rhs.IsConstant()) {
1280 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1281 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1282 } else if (rhs.IsStackSlot()) {
1283 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1284 } else {
1285 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1286 }
1287 GenerateFPJumps(cond, &true_label, &false_label);
1288 break;
1289 }
1290 case Primitive::kPrimDouble: {
1291 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1292 if (rhs.IsConstant()) {
1293 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1294 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1295 } else if (rhs.IsDoubleStackSlot()) {
1296 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1297 } else {
1298 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1299 }
1300 GenerateFPJumps(cond, &true_label, &false_label);
1301 break;
1302 }
1303 }
1304
1305 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001306 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001307
Roland Levillain4fa13f62015-07-06 18:11:54 +01001308 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001309 __ Bind(&false_label);
1310 __ xorl(reg, reg);
1311 __ jmp(&done_label);
1312
Roland Levillain4fa13f62015-07-06 18:11:54 +01001313 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001314 __ Bind(&true_label);
1315 __ movl(reg, Immediate(1));
1316 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001317}
1318
1319void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1320 VisitCondition(comp);
1321}
1322
1323void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1324 VisitCondition(comp);
1325}
1326
1327void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1328 VisitCondition(comp);
1329}
1330
1331void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1332 VisitCondition(comp);
1333}
1334
1335void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1336 VisitCondition(comp);
1337}
1338
1339void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1340 VisitCondition(comp);
1341}
1342
1343void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1344 VisitCondition(comp);
1345}
1346
1347void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1348 VisitCondition(comp);
1349}
1350
1351void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1352 VisitCondition(comp);
1353}
1354
1355void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1356 VisitCondition(comp);
1357}
1358
1359void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1360 VisitCondition(comp);
1361}
1362
1363void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1364 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001365}
1366
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001367void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001368 LocationSummary* locations =
1369 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001370 switch (compare->InputAt(0)->GetType()) {
1371 case Primitive::kPrimLong: {
1372 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001373 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1375 break;
1376 }
1377 case Primitive::kPrimFloat:
1378 case Primitive::kPrimDouble: {
1379 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001380 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001381 locations->SetOut(Location::RequiresRegister());
1382 break;
1383 }
1384 default:
1385 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1386 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001387}
1388
1389void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001390 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001391 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001392 Location left = locations->InAt(0);
1393 Location right = locations->InAt(1);
1394
Mark Mendell0c9497d2015-08-21 09:30:05 -04001395 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001396 Primitive::Type type = compare->InputAt(0)->GetType();
1397 switch (type) {
1398 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001399 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1400 if (right.IsConstant()) {
1401 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001402 if (IsInt<32>(value)) {
1403 if (value == 0) {
1404 __ testq(left_reg, left_reg);
1405 } else {
1406 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1407 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001408 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001409 // Value won't fit in an int.
1410 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001411 }
Mark Mendell40741f32015-04-20 22:10:34 -04001412 } else if (right.IsDoubleStackSlot()) {
1413 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001414 } else {
1415 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1416 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001417 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001418 }
1419 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001420 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1421 if (right.IsConstant()) {
1422 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1423 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1424 } else if (right.IsStackSlot()) {
1425 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1426 } else {
1427 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1428 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001429 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1430 break;
1431 }
1432 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001433 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1434 if (right.IsConstant()) {
1435 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1436 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1437 } else if (right.IsDoubleStackSlot()) {
1438 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1439 } else {
1440 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1441 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001442 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1443 break;
1444 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001445 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001446 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001447 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001448 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001449 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001450 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001451
Calin Juravle91debbc2014-11-26 19:01:09 +00001452 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001453 __ movl(out, Immediate(1));
1454 __ jmp(&done);
1455
1456 __ Bind(&less);
1457 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001458
1459 __ Bind(&done);
1460}
1461
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001462void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001463 LocationSummary* locations =
1464 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001465 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001466}
1467
1468void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001469 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001470 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001471}
1472
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001473void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1474 LocationSummary* locations =
1475 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1476 locations->SetOut(Location::ConstantLocation(constant));
1477}
1478
1479void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1480 // Will be generated at use site.
1481 UNUSED(constant);
1482}
1483
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001484void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001485 LocationSummary* locations =
1486 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001487 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001488}
1489
1490void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001491 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001492 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001493}
1494
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001495void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1496 LocationSummary* locations =
1497 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1498 locations->SetOut(Location::ConstantLocation(constant));
1499}
1500
1501void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1502 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001503 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001504}
1505
1506void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1507 LocationSummary* locations =
1508 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1509 locations->SetOut(Location::ConstantLocation(constant));
1510}
1511
1512void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1513 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001514 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001515}
1516
Calin Juravle27df7582015-04-17 19:12:31 +01001517void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1518 memory_barrier->SetLocations(nullptr);
1519}
1520
1521void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1522 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1523}
1524
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001525void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1526 ret->SetLocations(nullptr);
1527}
1528
1529void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001530 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001531 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001532}
1533
1534void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001535 LocationSummary* locations =
1536 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001537 switch (ret->InputAt(0)->GetType()) {
1538 case Primitive::kPrimBoolean:
1539 case Primitive::kPrimByte:
1540 case Primitive::kPrimChar:
1541 case Primitive::kPrimShort:
1542 case Primitive::kPrimInt:
1543 case Primitive::kPrimNot:
1544 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001545 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001546 break;
1547
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001548 case Primitive::kPrimFloat:
1549 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001550 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001551 break;
1552
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001553 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001554 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001555 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001556}
1557
1558void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1559 if (kIsDebugBuild) {
1560 switch (ret->InputAt(0)->GetType()) {
1561 case Primitive::kPrimBoolean:
1562 case Primitive::kPrimByte:
1563 case Primitive::kPrimChar:
1564 case Primitive::kPrimShort:
1565 case Primitive::kPrimInt:
1566 case Primitive::kPrimNot:
1567 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001568 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001569 break;
1570
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001571 case Primitive::kPrimFloat:
1572 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001573 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001574 XMM0);
1575 break;
1576
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001577 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001578 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001579 }
1580 }
1581 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001582}
1583
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001584Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1585 switch (type) {
1586 case Primitive::kPrimBoolean:
1587 case Primitive::kPrimByte:
1588 case Primitive::kPrimChar:
1589 case Primitive::kPrimShort:
1590 case Primitive::kPrimInt:
1591 case Primitive::kPrimNot:
1592 case Primitive::kPrimLong:
1593 return Location::RegisterLocation(RAX);
1594
1595 case Primitive::kPrimVoid:
1596 return Location::NoLocation();
1597
1598 case Primitive::kPrimDouble:
1599 case Primitive::kPrimFloat:
1600 return Location::FpuRegisterLocation(XMM0);
1601 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001602
1603 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001604}
1605
1606Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1607 return Location::RegisterLocation(kMethodRegisterArgument);
1608}
1609
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001610Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001611 switch (type) {
1612 case Primitive::kPrimBoolean:
1613 case Primitive::kPrimByte:
1614 case Primitive::kPrimChar:
1615 case Primitive::kPrimShort:
1616 case Primitive::kPrimInt:
1617 case Primitive::kPrimNot: {
1618 uint32_t index = gp_index_++;
1619 stack_index_++;
1620 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001621 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001622 } else {
1623 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1624 }
1625 }
1626
1627 case Primitive::kPrimLong: {
1628 uint32_t index = gp_index_;
1629 stack_index_ += 2;
1630 if (index < calling_convention.GetNumberOfRegisters()) {
1631 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001632 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001633 } else {
1634 gp_index_ += 2;
1635 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1636 }
1637 }
1638
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001639 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001640 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001641 stack_index_++;
1642 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001643 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001644 } else {
1645 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1646 }
1647 }
1648
1649 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001650 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001651 stack_index_ += 2;
1652 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001653 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001654 } else {
1655 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1656 }
1657 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001658
1659 case Primitive::kPrimVoid:
1660 LOG(FATAL) << "Unexpected parameter type " << type;
1661 break;
1662 }
1663 return Location();
1664}
1665
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001666void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001667 // When we do not run baseline, explicit clinit checks triggered by static
1668 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1669 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001670
Mark Mendellfb8d2792015-03-31 22:16:59 -04001671 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001672 if (intrinsic.TryDispatch(invoke)) {
1673 return;
1674 }
1675
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001676 HandleInvoke(invoke);
1677}
1678
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001679static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1680 if (invoke->GetLocations()->Intrinsified()) {
1681 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1682 intrinsic.Dispatch(invoke);
1683 return true;
1684 }
1685 return false;
1686}
1687
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001688void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001689 // When we do not run baseline, explicit clinit checks triggered by static
1690 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1691 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001692
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001693 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1694 return;
1695 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001696
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001697 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001698 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001699 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001700 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001701}
1702
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001703void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001704 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001705 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001706}
1707
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001708void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001709 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001710 if (intrinsic.TryDispatch(invoke)) {
1711 return;
1712 }
1713
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001714 HandleInvoke(invoke);
1715}
1716
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001717void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001718 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1719 return;
1720 }
1721
Roland Levillain271ab9c2014-11-27 15:23:57 +00001722 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001723 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1724 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001725 LocationSummary* locations = invoke->GetLocations();
1726 Location receiver = locations->InAt(0);
1727 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1728 // temp = object->GetClass();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001729 DCHECK(receiver.IsRegister());
1730 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001731 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001732 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001733 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001734 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001735 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001736 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001737 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001738
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001739 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001740 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001741}
1742
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001743void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1744 HandleInvoke(invoke);
1745 // Add the hidden argument.
1746 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1747}
1748
1749void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1750 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001751 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001752 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1753 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001754 LocationSummary* locations = invoke->GetLocations();
1755 Location receiver = locations->InAt(0);
1756 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1757
1758 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001759 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1760 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001761
1762 // temp = object->GetClass();
1763 if (receiver.IsStackSlot()) {
1764 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1765 __ movl(temp, Address(temp, class_offset));
1766 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001767 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001768 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001769 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001770 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001771 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001772 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001773 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001774 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001775 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001776
1777 DCHECK(!codegen_->IsLeafMethod());
1778 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1779}
1780
Roland Levillain88cb1752014-10-20 16:36:47 +01001781void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1782 LocationSummary* locations =
1783 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1784 switch (neg->GetResultType()) {
1785 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001786 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001787 locations->SetInAt(0, Location::RequiresRegister());
1788 locations->SetOut(Location::SameAsFirstInput());
1789 break;
1790
Roland Levillain88cb1752014-10-20 16:36:47 +01001791 case Primitive::kPrimFloat:
1792 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001793 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001794 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001795 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001796 break;
1797
1798 default:
1799 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1800 }
1801}
1802
1803void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1804 LocationSummary* locations = neg->GetLocations();
1805 Location out = locations->Out();
1806 Location in = locations->InAt(0);
1807 switch (neg->GetResultType()) {
1808 case Primitive::kPrimInt:
1809 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001810 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001811 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001812 break;
1813
1814 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001815 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001816 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001817 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001818 break;
1819
Roland Levillain5368c212014-11-27 15:03:41 +00001820 case Primitive::kPrimFloat: {
1821 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001822 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001823 // Implement float negation with an exclusive or with value
1824 // 0x80000000 (mask for bit 31, representing the sign of a
1825 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001826 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001827 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001828 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001829 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001830
Roland Levillain5368c212014-11-27 15:03:41 +00001831 case Primitive::kPrimDouble: {
1832 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001833 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001834 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001835 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001836 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001837 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001838 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001839 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001840 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001841
1842 default:
1843 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1844 }
1845}
1846
Roland Levillaindff1f282014-11-05 14:15:05 +00001847void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1848 LocationSummary* locations =
1849 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1850 Primitive::Type result_type = conversion->GetResultType();
1851 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001852 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001853
David Brazdilb2bd1c52015-03-25 11:17:37 +00001854 // The Java language does not allow treating boolean as an integral type but
1855 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001856
Roland Levillaindff1f282014-11-05 14:15:05 +00001857 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001858 case Primitive::kPrimByte:
1859 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001860 case Primitive::kPrimBoolean:
1861 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001862 case Primitive::kPrimShort:
1863 case Primitive::kPrimInt:
1864 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001865 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001866 locations->SetInAt(0, Location::Any());
1867 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1868 break;
1869
1870 default:
1871 LOG(FATAL) << "Unexpected type conversion from " << input_type
1872 << " to " << result_type;
1873 }
1874 break;
1875
Roland Levillain01a8d712014-11-14 16:27:39 +00001876 case Primitive::kPrimShort:
1877 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001878 case Primitive::kPrimBoolean:
1879 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001880 case Primitive::kPrimByte:
1881 case Primitive::kPrimInt:
1882 case Primitive::kPrimChar:
1883 // Processing a Dex `int-to-short' instruction.
1884 locations->SetInAt(0, Location::Any());
1885 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1886 break;
1887
1888 default:
1889 LOG(FATAL) << "Unexpected type conversion from " << input_type
1890 << " to " << result_type;
1891 }
1892 break;
1893
Roland Levillain946e1432014-11-11 17:35:19 +00001894 case Primitive::kPrimInt:
1895 switch (input_type) {
1896 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001897 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001898 locations->SetInAt(0, Location::Any());
1899 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1900 break;
1901
1902 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001903 // Processing a Dex `float-to-int' instruction.
1904 locations->SetInAt(0, Location::RequiresFpuRegister());
1905 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001906 break;
1907
Roland Levillain946e1432014-11-11 17:35:19 +00001908 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001909 // Processing a Dex `double-to-int' instruction.
1910 locations->SetInAt(0, Location::RequiresFpuRegister());
1911 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001912 break;
1913
1914 default:
1915 LOG(FATAL) << "Unexpected type conversion from " << input_type
1916 << " to " << result_type;
1917 }
1918 break;
1919
Roland Levillaindff1f282014-11-05 14:15:05 +00001920 case Primitive::kPrimLong:
1921 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001922 case Primitive::kPrimBoolean:
1923 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001924 case Primitive::kPrimByte:
1925 case Primitive::kPrimShort:
1926 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001927 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001928 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001929 // TODO: We would benefit from a (to-be-implemented)
1930 // Location::RegisterOrStackSlot requirement for this input.
1931 locations->SetInAt(0, Location::RequiresRegister());
1932 locations->SetOut(Location::RequiresRegister());
1933 break;
1934
1935 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001936 // Processing a Dex `float-to-long' instruction.
1937 locations->SetInAt(0, Location::RequiresFpuRegister());
1938 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00001939 break;
1940
Roland Levillaindff1f282014-11-05 14:15:05 +00001941 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001942 // Processing a Dex `double-to-long' instruction.
1943 locations->SetInAt(0, Location::RequiresFpuRegister());
1944 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001945 break;
1946
1947 default:
1948 LOG(FATAL) << "Unexpected type conversion from " << input_type
1949 << " to " << result_type;
1950 }
1951 break;
1952
Roland Levillain981e4542014-11-14 11:47:14 +00001953 case Primitive::kPrimChar:
1954 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001955 case Primitive::kPrimBoolean:
1956 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001957 case Primitive::kPrimByte:
1958 case Primitive::kPrimShort:
1959 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001960 // Processing a Dex `int-to-char' instruction.
1961 locations->SetInAt(0, Location::Any());
1962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1963 break;
1964
1965 default:
1966 LOG(FATAL) << "Unexpected type conversion from " << input_type
1967 << " to " << result_type;
1968 }
1969 break;
1970
Roland Levillaindff1f282014-11-05 14:15:05 +00001971 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001972 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001973 case Primitive::kPrimBoolean:
1974 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001975 case Primitive::kPrimByte:
1976 case Primitive::kPrimShort:
1977 case Primitive::kPrimInt:
1978 case Primitive::kPrimChar:
1979 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001980 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001981 locations->SetOut(Location::RequiresFpuRegister());
1982 break;
1983
1984 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001985 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001986 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001987 locations->SetOut(Location::RequiresFpuRegister());
1988 break;
1989
Roland Levillaincff13742014-11-17 14:32:17 +00001990 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001991 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001992 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001993 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001994 break;
1995
1996 default:
1997 LOG(FATAL) << "Unexpected type conversion from " << input_type
1998 << " to " << result_type;
1999 };
2000 break;
2001
Roland Levillaindff1f282014-11-05 14:15:05 +00002002 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002003 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002004 case Primitive::kPrimBoolean:
2005 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002006 case Primitive::kPrimByte:
2007 case Primitive::kPrimShort:
2008 case Primitive::kPrimInt:
2009 case Primitive::kPrimChar:
2010 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002011 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002012 locations->SetOut(Location::RequiresFpuRegister());
2013 break;
2014
2015 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002016 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002017 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002018 locations->SetOut(Location::RequiresFpuRegister());
2019 break;
2020
Roland Levillaincff13742014-11-17 14:32:17 +00002021 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002022 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002023 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002024 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002025 break;
2026
2027 default:
2028 LOG(FATAL) << "Unexpected type conversion from " << input_type
2029 << " to " << result_type;
2030 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002031 break;
2032
2033 default:
2034 LOG(FATAL) << "Unexpected type conversion from " << input_type
2035 << " to " << result_type;
2036 }
2037}
2038
2039void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2040 LocationSummary* locations = conversion->GetLocations();
2041 Location out = locations->Out();
2042 Location in = locations->InAt(0);
2043 Primitive::Type result_type = conversion->GetResultType();
2044 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002045 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002046 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002047 case Primitive::kPrimByte:
2048 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002049 case Primitive::kPrimBoolean:
2050 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002051 case Primitive::kPrimShort:
2052 case Primitive::kPrimInt:
2053 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002054 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002055 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002057 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002058 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002059 Address(CpuRegister(RSP), in.GetStackIndex()));
2060 } else {
2061 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002062 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002063 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2064 }
2065 break;
2066
2067 default:
2068 LOG(FATAL) << "Unexpected type conversion from " << input_type
2069 << " to " << result_type;
2070 }
2071 break;
2072
Roland Levillain01a8d712014-11-14 16:27:39 +00002073 case Primitive::kPrimShort:
2074 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002075 case Primitive::kPrimBoolean:
2076 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002077 case Primitive::kPrimByte:
2078 case Primitive::kPrimInt:
2079 case Primitive::kPrimChar:
2080 // Processing a Dex `int-to-short' instruction.
2081 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002083 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002084 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002085 Address(CpuRegister(RSP), in.GetStackIndex()));
2086 } else {
2087 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002089 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2090 }
2091 break;
2092
2093 default:
2094 LOG(FATAL) << "Unexpected type conversion from " << input_type
2095 << " to " << result_type;
2096 }
2097 break;
2098
Roland Levillain946e1432014-11-11 17:35:19 +00002099 case Primitive::kPrimInt:
2100 switch (input_type) {
2101 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002102 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002103 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002104 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002105 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002106 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002107 Address(CpuRegister(RSP), in.GetStackIndex()));
2108 } else {
2109 DCHECK(in.IsConstant());
2110 DCHECK(in.GetConstant()->IsLongConstant());
2111 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002112 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002113 }
2114 break;
2115
Roland Levillain3f8f9362014-12-02 17:45:01 +00002116 case Primitive::kPrimFloat: {
2117 // Processing a Dex `float-to-int' instruction.
2118 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2119 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002120 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002121
2122 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002123 // if input >= (float)INT_MAX goto done
2124 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002125 __ j(kAboveEqual, &done);
2126 // if input == NaN goto nan
2127 __ j(kUnordered, &nan);
2128 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002129 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002130 __ jmp(&done);
2131 __ Bind(&nan);
2132 // output = 0
2133 __ xorl(output, output);
2134 __ Bind(&done);
2135 break;
2136 }
2137
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002138 case Primitive::kPrimDouble: {
2139 // Processing a Dex `double-to-int' instruction.
2140 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2141 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002142 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002143
2144 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002145 // if input >= (double)INT_MAX goto done
2146 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002147 __ j(kAboveEqual, &done);
2148 // if input == NaN goto nan
2149 __ j(kUnordered, &nan);
2150 // output = double-to-int-truncate(input)
2151 __ cvttsd2si(output, input);
2152 __ jmp(&done);
2153 __ Bind(&nan);
2154 // output = 0
2155 __ xorl(output, output);
2156 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002157 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002158 }
Roland Levillain946e1432014-11-11 17:35:19 +00002159
2160 default:
2161 LOG(FATAL) << "Unexpected type conversion from " << input_type
2162 << " to " << result_type;
2163 }
2164 break;
2165
Roland Levillaindff1f282014-11-05 14:15:05 +00002166 case Primitive::kPrimLong:
2167 switch (input_type) {
2168 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002169 case Primitive::kPrimBoolean:
2170 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002171 case Primitive::kPrimByte:
2172 case Primitive::kPrimShort:
2173 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002174 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002175 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002176 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002177 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002178 break;
2179
Roland Levillain624279f2014-12-04 11:54:28 +00002180 case Primitive::kPrimFloat: {
2181 // Processing a Dex `float-to-long' instruction.
2182 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2183 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002184 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002185
Mark Mendell92e83bf2015-05-07 11:25:03 -04002186 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002187 // if input >= (float)LONG_MAX goto done
2188 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002189 __ j(kAboveEqual, &done);
2190 // if input == NaN goto nan
2191 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002192 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002193 __ cvttss2si(output, input, true);
2194 __ jmp(&done);
2195 __ Bind(&nan);
2196 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002197 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002198 __ Bind(&done);
2199 break;
2200 }
2201
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002202 case Primitive::kPrimDouble: {
2203 // Processing a Dex `double-to-long' instruction.
2204 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2205 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002206 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002207
Mark Mendell92e83bf2015-05-07 11:25:03 -04002208 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002209 // if input >= (double)LONG_MAX goto done
2210 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002211 __ j(kAboveEqual, &done);
2212 // if input == NaN goto nan
2213 __ j(kUnordered, &nan);
2214 // output = double-to-long-truncate(input)
2215 __ cvttsd2si(output, input, true);
2216 __ jmp(&done);
2217 __ Bind(&nan);
2218 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002219 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002220 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002221 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002222 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002223
2224 default:
2225 LOG(FATAL) << "Unexpected type conversion from " << input_type
2226 << " to " << result_type;
2227 }
2228 break;
2229
Roland Levillain981e4542014-11-14 11:47:14 +00002230 case Primitive::kPrimChar:
2231 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002232 case Primitive::kPrimBoolean:
2233 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002234 case Primitive::kPrimByte:
2235 case Primitive::kPrimShort:
2236 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002237 // Processing a Dex `int-to-char' instruction.
2238 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002239 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002240 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002242 Address(CpuRegister(RSP), in.GetStackIndex()));
2243 } else {
2244 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002245 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002246 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2247 }
2248 break;
2249
2250 default:
2251 LOG(FATAL) << "Unexpected type conversion from " << input_type
2252 << " to " << result_type;
2253 }
2254 break;
2255
Roland Levillaindff1f282014-11-05 14:15:05 +00002256 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002257 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002258 case Primitive::kPrimBoolean:
2259 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002260 case Primitive::kPrimByte:
2261 case Primitive::kPrimShort:
2262 case Primitive::kPrimInt:
2263 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002264 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002265 if (in.IsRegister()) {
2266 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2267 } else if (in.IsConstant()) {
2268 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2269 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2270 if (v == 0) {
2271 __ xorps(dest, dest);
2272 } else {
2273 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2274 }
2275 } else {
2276 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2277 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2278 }
Roland Levillaincff13742014-11-17 14:32:17 +00002279 break;
2280
2281 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002282 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002283 if (in.IsRegister()) {
2284 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2285 } else if (in.IsConstant()) {
2286 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2287 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2288 if (v == 0) {
2289 __ xorps(dest, dest);
2290 } else {
2291 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2292 }
2293 } else {
2294 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2295 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2296 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002297 break;
2298
Roland Levillaincff13742014-11-17 14:32:17 +00002299 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002300 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002301 if (in.IsFpuRegister()) {
2302 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2303 } else if (in.IsConstant()) {
2304 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2305 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2306 if (bit_cast<int64_t, double>(v) == 0) {
2307 __ xorps(dest, dest);
2308 } else {
2309 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2310 }
2311 } else {
2312 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2313 Address(CpuRegister(RSP), in.GetStackIndex()));
2314 }
Roland Levillaincff13742014-11-17 14:32:17 +00002315 break;
2316
2317 default:
2318 LOG(FATAL) << "Unexpected type conversion from " << input_type
2319 << " to " << result_type;
2320 };
2321 break;
2322
Roland Levillaindff1f282014-11-05 14:15:05 +00002323 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002324 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002325 case Primitive::kPrimBoolean:
2326 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002327 case Primitive::kPrimByte:
2328 case Primitive::kPrimShort:
2329 case Primitive::kPrimInt:
2330 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002331 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002332 if (in.IsRegister()) {
2333 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2334 } else if (in.IsConstant()) {
2335 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2336 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2337 if (v == 0) {
2338 __ xorpd(dest, dest);
2339 } else {
2340 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2341 }
2342 } else {
2343 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2344 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2345 }
Roland Levillaincff13742014-11-17 14:32:17 +00002346 break;
2347
2348 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002349 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002350 if (in.IsRegister()) {
2351 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2352 } else if (in.IsConstant()) {
2353 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2354 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2355 if (v == 0) {
2356 __ xorpd(dest, dest);
2357 } else {
2358 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2359 }
2360 } else {
2361 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2362 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2363 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002364 break;
2365
Roland Levillaincff13742014-11-17 14:32:17 +00002366 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002367 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002368 if (in.IsFpuRegister()) {
2369 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2370 } else if (in.IsConstant()) {
2371 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2372 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2373 if (bit_cast<int32_t, float>(v) == 0) {
2374 __ xorpd(dest, dest);
2375 } else {
2376 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2377 }
2378 } else {
2379 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2380 Address(CpuRegister(RSP), in.GetStackIndex()));
2381 }
Roland Levillaincff13742014-11-17 14:32:17 +00002382 break;
2383
2384 default:
2385 LOG(FATAL) << "Unexpected type conversion from " << input_type
2386 << " to " << result_type;
2387 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002388 break;
2389
2390 default:
2391 LOG(FATAL) << "Unexpected type conversion from " << input_type
2392 << " to " << result_type;
2393 }
2394}
2395
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002396void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002397 LocationSummary* locations =
2398 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002399 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002400 case Primitive::kPrimInt: {
2401 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002402 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2403 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002404 break;
2405 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002406
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002407 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002408 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002409 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002410 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002411 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002412 break;
2413 }
2414
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002415 case Primitive::kPrimDouble:
2416 case Primitive::kPrimFloat: {
2417 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002418 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002419 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002420 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002421 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002422
2423 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002424 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002425 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002426}
2427
2428void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2429 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002430 Location first = locations->InAt(0);
2431 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002432 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002433
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002434 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002435 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002436 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002437 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2438 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002439 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2440 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002441 } else {
2442 __ leal(out.AsRegister<CpuRegister>(), Address(
2443 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2444 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002445 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002446 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2447 __ addl(out.AsRegister<CpuRegister>(),
2448 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2449 } else {
2450 __ leal(out.AsRegister<CpuRegister>(), Address(
2451 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2452 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002453 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002454 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002455 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002456 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002457 break;
2458 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002459
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002460 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002461 if (second.IsRegister()) {
2462 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2463 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002464 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2465 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002466 } else {
2467 __ leaq(out.AsRegister<CpuRegister>(), Address(
2468 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2469 }
2470 } else {
2471 DCHECK(second.IsConstant());
2472 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2473 int32_t int32_value = Low32Bits(value);
2474 DCHECK_EQ(int32_value, value);
2475 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2476 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2477 } else {
2478 __ leaq(out.AsRegister<CpuRegister>(), Address(
2479 first.AsRegister<CpuRegister>(), int32_value));
2480 }
2481 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002482 break;
2483 }
2484
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002485 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002486 if (second.IsFpuRegister()) {
2487 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2488 } else if (second.IsConstant()) {
2489 __ addss(first.AsFpuRegister<XmmRegister>(),
2490 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2491 } else {
2492 DCHECK(second.IsStackSlot());
2493 __ addss(first.AsFpuRegister<XmmRegister>(),
2494 Address(CpuRegister(RSP), second.GetStackIndex()));
2495 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002496 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002497 }
2498
2499 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002500 if (second.IsFpuRegister()) {
2501 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2502 } else if (second.IsConstant()) {
2503 __ addsd(first.AsFpuRegister<XmmRegister>(),
2504 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2505 } else {
2506 DCHECK(second.IsDoubleStackSlot());
2507 __ addsd(first.AsFpuRegister<XmmRegister>(),
2508 Address(CpuRegister(RSP), second.GetStackIndex()));
2509 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002510 break;
2511 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002512
2513 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002514 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002515 }
2516}
2517
2518void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002519 LocationSummary* locations =
2520 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002521 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002522 case Primitive::kPrimInt: {
2523 locations->SetInAt(0, Location::RequiresRegister());
2524 locations->SetInAt(1, Location::Any());
2525 locations->SetOut(Location::SameAsFirstInput());
2526 break;
2527 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002528 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002529 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002530 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002531 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002532 break;
2533 }
Calin Juravle11351682014-10-23 15:38:15 +01002534 case Primitive::kPrimFloat:
2535 case Primitive::kPrimDouble: {
2536 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002537 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002538 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002539 break;
Calin Juravle11351682014-10-23 15:38:15 +01002540 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002541 default:
Calin Juravle11351682014-10-23 15:38:15 +01002542 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002543 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002544}
2545
2546void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2547 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002548 Location first = locations->InAt(0);
2549 Location second = locations->InAt(1);
2550 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002551 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002552 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002553 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002554 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002555 } else if (second.IsConstant()) {
2556 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002557 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002558 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002559 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002560 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002561 break;
2562 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002563 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002564 if (second.IsConstant()) {
2565 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2566 DCHECK(IsInt<32>(value));
2567 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2568 } else {
2569 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2570 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002571 break;
2572 }
2573
Calin Juravle11351682014-10-23 15:38:15 +01002574 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002575 if (second.IsFpuRegister()) {
2576 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2577 } else if (second.IsConstant()) {
2578 __ subss(first.AsFpuRegister<XmmRegister>(),
2579 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2580 } else {
2581 DCHECK(second.IsStackSlot());
2582 __ subss(first.AsFpuRegister<XmmRegister>(),
2583 Address(CpuRegister(RSP), second.GetStackIndex()));
2584 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002585 break;
Calin Juravle11351682014-10-23 15:38:15 +01002586 }
2587
2588 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002589 if (second.IsFpuRegister()) {
2590 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2591 } else if (second.IsConstant()) {
2592 __ subsd(first.AsFpuRegister<XmmRegister>(),
2593 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2594 } else {
2595 DCHECK(second.IsDoubleStackSlot());
2596 __ subsd(first.AsFpuRegister<XmmRegister>(),
2597 Address(CpuRegister(RSP), second.GetStackIndex()));
2598 }
Calin Juravle11351682014-10-23 15:38:15 +01002599 break;
2600 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002601
2602 default:
Calin Juravle11351682014-10-23 15:38:15 +01002603 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002604 }
2605}
2606
Calin Juravle34bacdf2014-10-07 20:23:36 +01002607void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2608 LocationSummary* locations =
2609 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2610 switch (mul->GetResultType()) {
2611 case Primitive::kPrimInt: {
2612 locations->SetInAt(0, Location::RequiresRegister());
2613 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002614 if (mul->InputAt(1)->IsIntConstant()) {
2615 // Can use 3 operand multiply.
2616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2617 } else {
2618 locations->SetOut(Location::SameAsFirstInput());
2619 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002620 break;
2621 }
2622 case Primitive::kPrimLong: {
2623 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002624 locations->SetInAt(1, Location::Any());
2625 if (mul->InputAt(1)->IsLongConstant() &&
2626 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002627 // Can use 3 operand multiply.
2628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2629 } else {
2630 locations->SetOut(Location::SameAsFirstInput());
2631 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002632 break;
2633 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002634 case Primitive::kPrimFloat:
2635 case Primitive::kPrimDouble: {
2636 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002637 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002638 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002639 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002640 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002641
2642 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002643 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002644 }
2645}
2646
2647void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2648 LocationSummary* locations = mul->GetLocations();
2649 Location first = locations->InAt(0);
2650 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002651 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002652 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002653 case Primitive::kPrimInt:
2654 // The constant may have ended up in a register, so test explicitly to avoid
2655 // problems where the output may not be the same as the first operand.
2656 if (mul->InputAt(1)->IsIntConstant()) {
2657 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2658 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2659 } else if (second.IsRegister()) {
2660 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002661 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002662 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002663 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002664 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002665 __ imull(first.AsRegister<CpuRegister>(),
2666 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002667 }
2668 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002669 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002670 // The constant may have ended up in a register, so test explicitly to avoid
2671 // problems where the output may not be the same as the first operand.
2672 if (mul->InputAt(1)->IsLongConstant()) {
2673 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2674 if (IsInt<32>(value)) {
2675 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2676 Immediate(static_cast<int32_t>(value)));
2677 } else {
2678 // Have to use the constant area.
2679 DCHECK(first.Equals(out));
2680 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2681 }
2682 } else if (second.IsRegister()) {
2683 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002684 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002685 } else {
2686 DCHECK(second.IsDoubleStackSlot());
2687 DCHECK(first.Equals(out));
2688 __ imulq(first.AsRegister<CpuRegister>(),
2689 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002690 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002691 break;
2692 }
2693
Calin Juravleb5bfa962014-10-21 18:02:24 +01002694 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002695 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002696 if (second.IsFpuRegister()) {
2697 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2698 } else if (second.IsConstant()) {
2699 __ mulss(first.AsFpuRegister<XmmRegister>(),
2700 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2701 } else {
2702 DCHECK(second.IsStackSlot());
2703 __ mulss(first.AsFpuRegister<XmmRegister>(),
2704 Address(CpuRegister(RSP), second.GetStackIndex()));
2705 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002706 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002707 }
2708
2709 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002710 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002711 if (second.IsFpuRegister()) {
2712 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2713 } else if (second.IsConstant()) {
2714 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2715 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2716 } else {
2717 DCHECK(second.IsDoubleStackSlot());
2718 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2719 Address(CpuRegister(RSP), second.GetStackIndex()));
2720 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002721 break;
2722 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002723
2724 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002725 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002726 }
2727}
2728
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002729void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2730 uint32_t stack_adjustment, bool is_float) {
2731 if (source.IsStackSlot()) {
2732 DCHECK(is_float);
2733 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2734 } else if (source.IsDoubleStackSlot()) {
2735 DCHECK(!is_float);
2736 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2737 } else {
2738 // Write the value to the temporary location on the stack and load to FP stack.
2739 if (is_float) {
2740 Location stack_temp = Location::StackSlot(temp_offset);
2741 codegen_->Move(stack_temp, source);
2742 __ flds(Address(CpuRegister(RSP), temp_offset));
2743 } else {
2744 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2745 codegen_->Move(stack_temp, source);
2746 __ fldl(Address(CpuRegister(RSP), temp_offset));
2747 }
2748 }
2749}
2750
2751void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2752 Primitive::Type type = rem->GetResultType();
2753 bool is_float = type == Primitive::kPrimFloat;
2754 size_t elem_size = Primitive::ComponentSize(type);
2755 LocationSummary* locations = rem->GetLocations();
2756 Location first = locations->InAt(0);
2757 Location second = locations->InAt(1);
2758 Location out = locations->Out();
2759
2760 // Create stack space for 2 elements.
2761 // TODO: enhance register allocator to ask for stack temporaries.
2762 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2763
2764 // Load the values to the FP stack in reverse order, using temporaries if needed.
2765 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2766 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2767
2768 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002769 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002770 __ Bind(&retry);
2771 __ fprem();
2772
2773 // Move FP status to AX.
2774 __ fstsw();
2775
2776 // And see if the argument reduction is complete. This is signaled by the
2777 // C2 FPU flag bit set to 0.
2778 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2779 __ j(kNotEqual, &retry);
2780
2781 // We have settled on the final value. Retrieve it into an XMM register.
2782 // Store FP top of stack to real stack.
2783 if (is_float) {
2784 __ fsts(Address(CpuRegister(RSP), 0));
2785 } else {
2786 __ fstl(Address(CpuRegister(RSP), 0));
2787 }
2788
2789 // Pop the 2 items from the FP stack.
2790 __ fucompp();
2791
2792 // Load the value from the stack into an XMM register.
2793 DCHECK(out.IsFpuRegister()) << out;
2794 if (is_float) {
2795 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2796 } else {
2797 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2798 }
2799
2800 // And remove the temporary stack space we allocated.
2801 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2802}
2803
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002804void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2805 DCHECK(instruction->IsDiv() || instruction->IsRem());
2806
2807 LocationSummary* locations = instruction->GetLocations();
2808 Location second = locations->InAt(1);
2809 DCHECK(second.IsConstant());
2810
2811 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2812 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002813 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002814
2815 DCHECK(imm == 1 || imm == -1);
2816
2817 switch (instruction->GetResultType()) {
2818 case Primitive::kPrimInt: {
2819 if (instruction->IsRem()) {
2820 __ xorl(output_register, output_register);
2821 } else {
2822 __ movl(output_register, input_register);
2823 if (imm == -1) {
2824 __ negl(output_register);
2825 }
2826 }
2827 break;
2828 }
2829
2830 case Primitive::kPrimLong: {
2831 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002832 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002833 } else {
2834 __ movq(output_register, input_register);
2835 if (imm == -1) {
2836 __ negq(output_register);
2837 }
2838 }
2839 break;
2840 }
2841
2842 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002843 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002844 }
2845}
2846
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002847void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002848 LocationSummary* locations = instruction->GetLocations();
2849 Location second = locations->InAt(1);
2850
2851 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2852 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2853
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002854 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002855
2856 DCHECK(IsPowerOfTwo(std::abs(imm)));
2857
2858 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2859
2860 if (instruction->GetResultType() == Primitive::kPrimInt) {
2861 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2862 __ testl(numerator, numerator);
2863 __ cmov(kGreaterEqual, tmp, numerator);
2864 int shift = CTZ(imm);
2865 __ sarl(tmp, Immediate(shift));
2866
2867 if (imm < 0) {
2868 __ negl(tmp);
2869 }
2870
2871 __ movl(output_register, tmp);
2872 } else {
2873 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2874 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2875
Mark Mendell92e83bf2015-05-07 11:25:03 -04002876 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002877 __ addq(rdx, numerator);
2878 __ testq(numerator, numerator);
2879 __ cmov(kGreaterEqual, rdx, numerator);
2880 int shift = CTZ(imm);
2881 __ sarq(rdx, Immediate(shift));
2882
2883 if (imm < 0) {
2884 __ negq(rdx);
2885 }
2886
2887 __ movq(output_register, rdx);
2888 }
2889}
2890
2891void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2892 DCHECK(instruction->IsDiv() || instruction->IsRem());
2893
2894 LocationSummary* locations = instruction->GetLocations();
2895 Location second = locations->InAt(1);
2896
2897 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2898 : locations->GetTemp(0).AsRegister<CpuRegister>();
2899 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2900 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2901 : locations->Out().AsRegister<CpuRegister>();
2902 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2903
2904 DCHECK_EQ(RAX, eax.AsRegister());
2905 DCHECK_EQ(RDX, edx.AsRegister());
2906 if (instruction->IsDiv()) {
2907 DCHECK_EQ(RAX, out.AsRegister());
2908 } else {
2909 DCHECK_EQ(RDX, out.AsRegister());
2910 }
2911
2912 int64_t magic;
2913 int shift;
2914
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002915 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002916 if (instruction->GetResultType() == Primitive::kPrimInt) {
2917 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2918
2919 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2920
2921 __ movl(numerator, eax);
2922
Mark Mendell0c9497d2015-08-21 09:30:05 -04002923 NearLabel no_div;
2924 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002925 __ testl(eax, eax);
2926 __ j(kNotEqual, &no_div);
2927
2928 __ xorl(out, out);
2929 __ jmp(&end);
2930
2931 __ Bind(&no_div);
2932
2933 __ movl(eax, Immediate(magic));
2934 __ imull(numerator);
2935
2936 if (imm > 0 && magic < 0) {
2937 __ addl(edx, numerator);
2938 } else if (imm < 0 && magic > 0) {
2939 __ subl(edx, numerator);
2940 }
2941
2942 if (shift != 0) {
2943 __ sarl(edx, Immediate(shift));
2944 }
2945
2946 __ movl(eax, edx);
2947 __ shrl(edx, Immediate(31));
2948 __ addl(edx, eax);
2949
2950 if (instruction->IsRem()) {
2951 __ movl(eax, numerator);
2952 __ imull(edx, Immediate(imm));
2953 __ subl(eax, edx);
2954 __ movl(edx, eax);
2955 } else {
2956 __ movl(eax, edx);
2957 }
2958 __ Bind(&end);
2959 } else {
2960 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2961
2962 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2963
2964 CpuRegister rax = eax;
2965 CpuRegister rdx = edx;
2966
2967 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2968
2969 // Save the numerator.
2970 __ movq(numerator, rax);
2971
2972 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002973 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002974
2975 // RDX:RAX = magic * numerator
2976 __ imulq(numerator);
2977
2978 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002979 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002980 __ addq(rdx, numerator);
2981 } else if (imm < 0 && magic > 0) {
2982 // RDX -= numerator
2983 __ subq(rdx, numerator);
2984 }
2985
2986 // Shift if needed.
2987 if (shift != 0) {
2988 __ sarq(rdx, Immediate(shift));
2989 }
2990
2991 // RDX += 1 if RDX < 0
2992 __ movq(rax, rdx);
2993 __ shrq(rdx, Immediate(63));
2994 __ addq(rdx, rax);
2995
2996 if (instruction->IsRem()) {
2997 __ movq(rax, numerator);
2998
2999 if (IsInt<32>(imm)) {
3000 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3001 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003002 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003003 }
3004
3005 __ subq(rax, rdx);
3006 __ movq(rdx, rax);
3007 } else {
3008 __ movq(rax, rdx);
3009 }
3010 }
3011}
3012
Calin Juravlebacfec32014-11-14 15:54:36 +00003013void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3014 DCHECK(instruction->IsDiv() || instruction->IsRem());
3015 Primitive::Type type = instruction->GetResultType();
3016 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3017
3018 bool is_div = instruction->IsDiv();
3019 LocationSummary* locations = instruction->GetLocations();
3020
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003021 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3022 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003023
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003025 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003026
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003027 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003028 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003029
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003030 if (imm == 0) {
3031 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3032 } else if (imm == 1 || imm == -1) {
3033 DivRemOneOrMinusOne(instruction);
3034 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003035 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003036 } else {
3037 DCHECK(imm <= -2 || imm >= 2);
3038 GenerateDivRemWithAnyConstant(instruction);
3039 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003040 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003041 SlowPathCodeX86_64* slow_path =
3042 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3043 out.AsRegister(), type, is_div);
3044 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003045
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003046 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3047 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3048 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3049 // so it's safe to just use negl instead of more complex comparisons.
3050 if (type == Primitive::kPrimInt) {
3051 __ cmpl(second_reg, Immediate(-1));
3052 __ j(kEqual, slow_path->GetEntryLabel());
3053 // edx:eax <- sign-extended of eax
3054 __ cdq();
3055 // eax = quotient, edx = remainder
3056 __ idivl(second_reg);
3057 } else {
3058 __ cmpq(second_reg, Immediate(-1));
3059 __ j(kEqual, slow_path->GetEntryLabel());
3060 // rdx:rax <- sign-extended of rax
3061 __ cqo();
3062 // rax = quotient, rdx = remainder
3063 __ idivq(second_reg);
3064 }
3065 __ Bind(slow_path->GetExitLabel());
3066 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003067}
3068
Calin Juravle7c4954d2014-10-28 16:57:40 +00003069void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3070 LocationSummary* locations =
3071 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3072 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003073 case Primitive::kPrimInt:
3074 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003075 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003076 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003077 locations->SetOut(Location::SameAsFirstInput());
3078 // Intel uses edx:eax as the dividend.
3079 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003080 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3081 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3082 // output and request another temp.
3083 if (div->InputAt(1)->IsConstant()) {
3084 locations->AddTemp(Location::RequiresRegister());
3085 }
Calin Juravled0d48522014-11-04 16:40:20 +00003086 break;
3087 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003088
Calin Juravle7c4954d2014-10-28 16:57:40 +00003089 case Primitive::kPrimFloat:
3090 case Primitive::kPrimDouble: {
3091 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003092 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003093 locations->SetOut(Location::SameAsFirstInput());
3094 break;
3095 }
3096
3097 default:
3098 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3099 }
3100}
3101
3102void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3103 LocationSummary* locations = div->GetLocations();
3104 Location first = locations->InAt(0);
3105 Location second = locations->InAt(1);
3106 DCHECK(first.Equals(locations->Out()));
3107
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003108 Primitive::Type type = div->GetResultType();
3109 switch (type) {
3110 case Primitive::kPrimInt:
3111 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003112 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003113 break;
3114 }
3115
Calin Juravle7c4954d2014-10-28 16:57:40 +00003116 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003117 if (second.IsFpuRegister()) {
3118 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3119 } else if (second.IsConstant()) {
3120 __ divss(first.AsFpuRegister<XmmRegister>(),
3121 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3122 } else {
3123 DCHECK(second.IsStackSlot());
3124 __ divss(first.AsFpuRegister<XmmRegister>(),
3125 Address(CpuRegister(RSP), second.GetStackIndex()));
3126 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003127 break;
3128 }
3129
3130 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003131 if (second.IsFpuRegister()) {
3132 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3133 } else if (second.IsConstant()) {
3134 __ divsd(first.AsFpuRegister<XmmRegister>(),
3135 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3136 } else {
3137 DCHECK(second.IsDoubleStackSlot());
3138 __ divsd(first.AsFpuRegister<XmmRegister>(),
3139 Address(CpuRegister(RSP), second.GetStackIndex()));
3140 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003141 break;
3142 }
3143
3144 default:
3145 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3146 }
3147}
3148
Calin Juravlebacfec32014-11-14 15:54:36 +00003149void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003150 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003151 LocationSummary* locations =
3152 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003153
3154 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003155 case Primitive::kPrimInt:
3156 case Primitive::kPrimLong: {
3157 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003158 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003159 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3160 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003161 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3162 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3163 // output and request another temp.
3164 if (rem->InputAt(1)->IsConstant()) {
3165 locations->AddTemp(Location::RequiresRegister());
3166 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003167 break;
3168 }
3169
3170 case Primitive::kPrimFloat:
3171 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003172 locations->SetInAt(0, Location::Any());
3173 locations->SetInAt(1, Location::Any());
3174 locations->SetOut(Location::RequiresFpuRegister());
3175 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003176 break;
3177 }
3178
3179 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003180 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003181 }
3182}
3183
3184void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3185 Primitive::Type type = rem->GetResultType();
3186 switch (type) {
3187 case Primitive::kPrimInt:
3188 case Primitive::kPrimLong: {
3189 GenerateDivRemIntegral(rem);
3190 break;
3191 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003192 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003193 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003194 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003195 break;
3196 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003197 default:
3198 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3199 }
3200}
3201
Calin Juravled0d48522014-11-04 16:40:20 +00003202void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3203 LocationSummary* locations =
3204 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3205 locations->SetInAt(0, Location::Any());
3206 if (instruction->HasUses()) {
3207 locations->SetOut(Location::SameAsFirstInput());
3208 }
3209}
3210
3211void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3212 SlowPathCodeX86_64* slow_path =
3213 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3214 codegen_->AddSlowPath(slow_path);
3215
3216 LocationSummary* locations = instruction->GetLocations();
3217 Location value = locations->InAt(0);
3218
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003219 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003220 case Primitive::kPrimByte:
3221 case Primitive::kPrimChar:
3222 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003223 case Primitive::kPrimInt: {
3224 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003225 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003226 __ j(kEqual, slow_path->GetEntryLabel());
3227 } else if (value.IsStackSlot()) {
3228 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3229 __ j(kEqual, slow_path->GetEntryLabel());
3230 } else {
3231 DCHECK(value.IsConstant()) << value;
3232 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3233 __ jmp(slow_path->GetEntryLabel());
3234 }
3235 }
3236 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003237 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003238 case Primitive::kPrimLong: {
3239 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003240 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003241 __ j(kEqual, slow_path->GetEntryLabel());
3242 } else if (value.IsDoubleStackSlot()) {
3243 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3244 __ j(kEqual, slow_path->GetEntryLabel());
3245 } else {
3246 DCHECK(value.IsConstant()) << value;
3247 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3248 __ jmp(slow_path->GetEntryLabel());
3249 }
3250 }
3251 break;
3252 }
3253 default:
3254 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003255 }
Calin Juravled0d48522014-11-04 16:40:20 +00003256}
3257
Calin Juravle9aec02f2014-11-18 23:06:35 +00003258void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3259 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3260
3261 LocationSummary* locations =
3262 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3263
3264 switch (op->GetResultType()) {
3265 case Primitive::kPrimInt:
3266 case Primitive::kPrimLong: {
3267 locations->SetInAt(0, Location::RequiresRegister());
3268 // The shift count needs to be in CL.
3269 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3270 locations->SetOut(Location::SameAsFirstInput());
3271 break;
3272 }
3273 default:
3274 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3275 }
3276}
3277
3278void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3279 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3280
3281 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003282 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003283 Location second = locations->InAt(1);
3284
3285 switch (op->GetResultType()) {
3286 case Primitive::kPrimInt: {
3287 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003288 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003289 if (op->IsShl()) {
3290 __ shll(first_reg, second_reg);
3291 } else if (op->IsShr()) {
3292 __ sarl(first_reg, second_reg);
3293 } else {
3294 __ shrl(first_reg, second_reg);
3295 }
3296 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003297 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003298 if (op->IsShl()) {
3299 __ shll(first_reg, imm);
3300 } else if (op->IsShr()) {
3301 __ sarl(first_reg, imm);
3302 } else {
3303 __ shrl(first_reg, imm);
3304 }
3305 }
3306 break;
3307 }
3308 case Primitive::kPrimLong: {
3309 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003310 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003311 if (op->IsShl()) {
3312 __ shlq(first_reg, second_reg);
3313 } else if (op->IsShr()) {
3314 __ sarq(first_reg, second_reg);
3315 } else {
3316 __ shrq(first_reg, second_reg);
3317 }
3318 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003319 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003320 if (op->IsShl()) {
3321 __ shlq(first_reg, imm);
3322 } else if (op->IsShr()) {
3323 __ sarq(first_reg, imm);
3324 } else {
3325 __ shrq(first_reg, imm);
3326 }
3327 }
3328 break;
3329 }
3330 default:
3331 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3332 }
3333}
3334
3335void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3336 HandleShift(shl);
3337}
3338
3339void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3340 HandleShift(shl);
3341}
3342
3343void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3344 HandleShift(shr);
3345}
3346
3347void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3348 HandleShift(shr);
3349}
3350
3351void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3352 HandleShift(ushr);
3353}
3354
3355void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3356 HandleShift(ushr);
3357}
3358
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003359void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003360 LocationSummary* locations =
3361 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003362 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003363 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003364 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003365 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003366}
3367
3368void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3369 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003370 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3371 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003372 // Note: if heap poisoning is enabled, the entry point takes cares
3373 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003374
3375 codegen_->InvokeRuntime(
3376 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3377 instruction,
3378 instruction->GetDexPc(),
3379 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003380
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003381 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003382}
3383
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003384void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3385 LocationSummary* locations =
3386 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3387 InvokeRuntimeCallingConvention calling_convention;
3388 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003389 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003390 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003391 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003392}
3393
3394void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3395 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003396 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3397 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003398
Roland Levillain4d027112015-07-01 15:41:14 +01003399 // Note: if heap poisoning is enabled, the entry point takes cares
3400 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003401 codegen_->InvokeRuntime(
3402 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3403 instruction,
3404 instruction->GetDexPc(),
3405 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003406
3407 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003408}
3409
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003410void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003411 LocationSummary* locations =
3412 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003413 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3414 if (location.IsStackSlot()) {
3415 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3416 } else if (location.IsDoubleStackSlot()) {
3417 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3418 }
3419 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003420}
3421
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003422void InstructionCodeGeneratorX86_64::VisitParameterValue(
3423 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003424 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003425}
3426
3427void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3428 LocationSummary* locations =
3429 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3430 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3431}
3432
3433void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3434 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3435 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003436}
3437
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003438void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003439 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003440 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003441 locations->SetInAt(0, Location::RequiresRegister());
3442 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003443}
3444
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003445void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3446 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003447 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3448 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003449 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003450 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003451 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003452 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003453 break;
3454
3455 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003456 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003457 break;
3458
3459 default:
3460 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3461 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003462}
3463
David Brazdil66d126e2015-04-03 16:02:44 +01003464void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3465 LocationSummary* locations =
3466 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3467 locations->SetInAt(0, Location::RequiresRegister());
3468 locations->SetOut(Location::SameAsFirstInput());
3469}
3470
3471void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003472 LocationSummary* locations = bool_not->GetLocations();
3473 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3474 locations->Out().AsRegister<CpuRegister>().AsRegister());
3475 Location out = locations->Out();
3476 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3477}
3478
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003479void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003480 LocationSummary* locations =
3481 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003482 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3483 locations->SetInAt(i, Location::Any());
3484 }
3485 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003486}
3487
3488void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003489 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003490 LOG(FATAL) << "Unimplemented";
3491}
3492
Calin Juravle52c48962014-12-16 17:02:57 +00003493void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3494 /*
3495 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3496 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3497 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3498 */
3499 switch (kind) {
3500 case MemBarrierKind::kAnyAny: {
3501 __ mfence();
3502 break;
3503 }
3504 case MemBarrierKind::kAnyStore:
3505 case MemBarrierKind::kLoadAny:
3506 case MemBarrierKind::kStoreStore: {
3507 // nop
3508 break;
3509 }
3510 default:
3511 LOG(FATAL) << "Unexpected memory barier " << kind;
3512 }
3513}
3514
3515void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3516 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3517
Nicolas Geoffray39468442014-09-02 15:17:15 +01003518 LocationSummary* locations =
3519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003520 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003521 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3522 locations->SetOut(Location::RequiresFpuRegister());
3523 } else {
3524 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3525 }
Calin Juravle52c48962014-12-16 17:02:57 +00003526}
3527
3528void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3529 const FieldInfo& field_info) {
3530 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3531
3532 LocationSummary* locations = instruction->GetLocations();
3533 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3534 Location out = locations->Out();
3535 bool is_volatile = field_info.IsVolatile();
3536 Primitive::Type field_type = field_info.GetFieldType();
3537 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3538
3539 switch (field_type) {
3540 case Primitive::kPrimBoolean: {
3541 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3542 break;
3543 }
3544
3545 case Primitive::kPrimByte: {
3546 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3547 break;
3548 }
3549
3550 case Primitive::kPrimShort: {
3551 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3552 break;
3553 }
3554
3555 case Primitive::kPrimChar: {
3556 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3557 break;
3558 }
3559
3560 case Primitive::kPrimInt:
3561 case Primitive::kPrimNot: {
3562 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3563 break;
3564 }
3565
3566 case Primitive::kPrimLong: {
3567 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3568 break;
3569 }
3570
3571 case Primitive::kPrimFloat: {
3572 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3573 break;
3574 }
3575
3576 case Primitive::kPrimDouble: {
3577 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3578 break;
3579 }
3580
3581 case Primitive::kPrimVoid:
3582 LOG(FATAL) << "Unreachable type " << field_type;
3583 UNREACHABLE();
3584 }
3585
Calin Juravle77520bc2015-01-12 18:45:46 +00003586 codegen_->MaybeRecordImplicitNullCheck(instruction);
3587
Calin Juravle52c48962014-12-16 17:02:57 +00003588 if (is_volatile) {
3589 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3590 }
Roland Levillain4d027112015-07-01 15:41:14 +01003591
3592 if (field_type == Primitive::kPrimNot) {
3593 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3594 }
Calin Juravle52c48962014-12-16 17:02:57 +00003595}
3596
3597void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3598 const FieldInfo& field_info) {
3599 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3600
3601 LocationSummary* locations =
3602 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003603 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003604 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003605 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003606
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003607 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003608 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3609 locations->SetInAt(1, Location::RequiresFpuRegister());
3610 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003611 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003612 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003613 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003614 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003615 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003616 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003617 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3618 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003619 locations->AddTemp(Location::RequiresRegister());
3620 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003621}
3622
Calin Juravle52c48962014-12-16 17:02:57 +00003623void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003624 const FieldInfo& field_info,
3625 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003626 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3627
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003628 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003629 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3630 Location value = locations->InAt(1);
3631 bool is_volatile = field_info.IsVolatile();
3632 Primitive::Type field_type = field_info.GetFieldType();
3633 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3634
3635 if (is_volatile) {
3636 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3637 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003638
3639 switch (field_type) {
3640 case Primitive::kPrimBoolean:
3641 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003642 if (value.IsConstant()) {
3643 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3644 __ movb(Address(base, offset), Immediate(v));
3645 } else {
3646 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3647 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003648 break;
3649 }
3650
3651 case Primitive::kPrimShort:
3652 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003653 if (value.IsConstant()) {
3654 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3655 __ movw(Address(base, offset), Immediate(v));
3656 } else {
3657 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3658 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003659 break;
3660 }
3661
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003662 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003663 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003664 if (value.IsConstant()) {
3665 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003666 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3667 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3668 // Note: if heap poisoning is enabled, no need to poison
3669 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003670 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003671 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003672 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3673 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3674 __ movl(temp, value.AsRegister<CpuRegister>());
3675 __ PoisonHeapReference(temp);
3676 __ movl(Address(base, offset), temp);
3677 } else {
3678 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3679 }
Mark Mendell40741f32015-04-20 22:10:34 -04003680 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003681 break;
3682 }
3683
3684 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003685 if (value.IsConstant()) {
3686 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3687 DCHECK(IsInt<32>(v));
3688 int32_t v_32 = v;
3689 __ movq(Address(base, offset), Immediate(v_32));
3690 } else {
3691 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3692 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003693 break;
3694 }
3695
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003696 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003697 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003698 break;
3699 }
3700
3701 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003702 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003703 break;
3704 }
3705
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003706 case Primitive::kPrimVoid:
3707 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003708 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003709 }
Calin Juravle52c48962014-12-16 17:02:57 +00003710
Calin Juravle77520bc2015-01-12 18:45:46 +00003711 codegen_->MaybeRecordImplicitNullCheck(instruction);
3712
3713 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3714 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3715 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003716 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003717 }
3718
Calin Juravle52c48962014-12-16 17:02:57 +00003719 if (is_volatile) {
3720 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3721 }
3722}
3723
3724void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3725 HandleFieldSet(instruction, instruction->GetFieldInfo());
3726}
3727
3728void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003729 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003730}
3731
3732void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003733 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003734}
3735
3736void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003737 HandleFieldGet(instruction, instruction->GetFieldInfo());
3738}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003739
Calin Juravle52c48962014-12-16 17:02:57 +00003740void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3741 HandleFieldGet(instruction);
3742}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003743
Calin Juravle52c48962014-12-16 17:02:57 +00003744void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3745 HandleFieldGet(instruction, instruction->GetFieldInfo());
3746}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003747
Calin Juravle52c48962014-12-16 17:02:57 +00003748void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3749 HandleFieldSet(instruction, instruction->GetFieldInfo());
3750}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003751
Calin Juravle52c48962014-12-16 17:02:57 +00003752void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003753 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003754}
3755
3756void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003757 LocationSummary* locations =
3758 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003759 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3760 ? Location::RequiresRegister()
3761 : Location::Any();
3762 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003763 if (instruction->HasUses()) {
3764 locations->SetOut(Location::SameAsFirstInput());
3765 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003766}
3767
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003768void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003769 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3770 return;
3771 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003772 LocationSummary* locations = instruction->GetLocations();
3773 Location obj = locations->InAt(0);
3774
3775 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3776 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3777}
3778
3779void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003780 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003781 codegen_->AddSlowPath(slow_path);
3782
3783 LocationSummary* locations = instruction->GetLocations();
3784 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003785
3786 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003787 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003788 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003789 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003790 } else {
3791 DCHECK(obj.IsConstant()) << obj;
3792 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3793 __ jmp(slow_path->GetEntryLabel());
3794 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003795 }
3796 __ j(kEqual, slow_path->GetEntryLabel());
3797}
3798
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003799void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
3800 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3801 GenerateImplicitNullCheck(instruction);
3802 } else {
3803 GenerateExplicitNullCheck(instruction);
3804 }
3805}
3806
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003807void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003808 LocationSummary* locations =
3809 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003810 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003811 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003812 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3813 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3814 } else {
3815 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3816 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003817}
3818
3819void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3820 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003821 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003822 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003823 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003824
Roland Levillain4d027112015-07-01 15:41:14 +01003825 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003826 case Primitive::kPrimBoolean: {
3827 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003829 if (index.IsConstant()) {
3830 __ movzxb(out, Address(obj,
3831 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3832 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003833 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003834 }
3835 break;
3836 }
3837
3838 case Primitive::kPrimByte: {
3839 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003840 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003841 if (index.IsConstant()) {
3842 __ movsxb(out, Address(obj,
3843 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3844 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003845 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003846 }
3847 break;
3848 }
3849
3850 case Primitive::kPrimShort: {
3851 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003853 if (index.IsConstant()) {
3854 __ movsxw(out, Address(obj,
3855 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3856 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003857 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003858 }
3859 break;
3860 }
3861
3862 case Primitive::kPrimChar: {
3863 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003864 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003865 if (index.IsConstant()) {
3866 __ movzxw(out, Address(obj,
3867 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3868 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003869 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003870 }
3871 break;
3872 }
3873
3874 case Primitive::kPrimInt:
3875 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003876 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3877 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003878 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003879 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003880 if (index.IsConstant()) {
3881 __ movl(out, Address(obj,
3882 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3883 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003884 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003885 }
3886 break;
3887 }
3888
3889 case Primitive::kPrimLong: {
3890 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003891 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003892 if (index.IsConstant()) {
3893 __ movq(out, Address(obj,
3894 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3895 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003896 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003897 }
3898 break;
3899 }
3900
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003901 case Primitive::kPrimFloat: {
3902 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003903 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003904 if (index.IsConstant()) {
3905 __ movss(out, Address(obj,
3906 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3907 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003908 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003909 }
3910 break;
3911 }
3912
3913 case Primitive::kPrimDouble: {
3914 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003915 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003916 if (index.IsConstant()) {
3917 __ movsd(out, Address(obj,
3918 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3919 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003920 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003921 }
3922 break;
3923 }
3924
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003925 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003926 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003927 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003928 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003929 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003930
3931 if (type == Primitive::kPrimNot) {
3932 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3933 __ MaybeUnpoisonHeapReference(out);
3934 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003935}
3936
3937void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003938 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003939
3940 bool needs_write_barrier =
3941 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3942 bool needs_runtime_call = instruction->NeedsTypeCheck();
3943
Nicolas Geoffray39468442014-09-02 15:17:15 +01003944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003945 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3946 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003947 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003948 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3949 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3950 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003951 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003952 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003953 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003954 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3955 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003956 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003957 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003958 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3959 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003960 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003961 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003962 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003963
3964 if (needs_write_barrier) {
3965 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003966 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003967 locations->AddTemp(Location::RequiresRegister());
3968 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003969 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003970}
3971
3972void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3973 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003974 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003975 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003976 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003977 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003978 bool needs_runtime_call = locations->WillCall();
3979 bool needs_write_barrier =
3980 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003981
3982 switch (value_type) {
3983 case Primitive::kPrimBoolean:
3984 case Primitive::kPrimByte: {
3985 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003986 if (index.IsConstant()) {
3987 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003988 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003989 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003990 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00003991 __ movb(Address(obj, offset),
3992 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003993 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003994 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003995 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003996 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
3997 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003998 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003999 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004000 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4001 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004002 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004003 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004004 break;
4005 }
4006
4007 case Primitive::kPrimShort:
4008 case Primitive::kPrimChar: {
4009 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004010 if (index.IsConstant()) {
4011 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004012 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004013 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004014 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004015 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00004016 __ movw(Address(obj, offset),
4017 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004018 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004019 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004020 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004021 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004022 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
4023 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004024 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004025 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00004026 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004027 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4028 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004029 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004030 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004031 break;
4032 }
4033
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004034 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004035 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004036 if (!needs_runtime_call) {
4037 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4038 if (index.IsConstant()) {
4039 size_t offset =
4040 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4041 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004042 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4043 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4044 __ movl(temp, value.AsRegister<CpuRegister>());
4045 __ PoisonHeapReference(temp);
4046 __ movl(Address(obj, offset), temp);
4047 } else {
4048 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
4049 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004050 } else {
4051 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004052 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004053 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4054 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4055 // Note: if heap poisoning is enabled, no need to poison
4056 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04004057 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004058 }
4059 } else {
4060 DCHECK(index.IsRegister()) << index;
4061 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004062 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4063 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4064 __ movl(temp, value.AsRegister<CpuRegister>());
4065 __ PoisonHeapReference(temp);
4066 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4067 } else {
4068 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4069 value.AsRegister<CpuRegister>());
4070 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004071 } else {
4072 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004073 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004074 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4075 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4076 // Note: if heap poisoning is enabled, no need to poison
4077 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004078 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004079 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004080 }
4081 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004082 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004083 if (needs_write_barrier) {
4084 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004085 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4086 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004087 codegen_->MarkGCCard(
4088 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004089 }
4090 } else {
4091 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004092 // Note: if heap poisoning is enabled, pAputObject takes cares
4093 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004094 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4095 instruction,
4096 instruction->GetDexPc(),
4097 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004098 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004099 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004100 break;
4101 }
4102
4103 case Primitive::kPrimLong: {
4104 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004105 if (index.IsConstant()) {
4106 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004107 if (value.IsRegister()) {
4108 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4109 } else {
4110 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4111 DCHECK(IsInt<32>(v));
4112 int32_t v_32 = v;
4113 __ movq(Address(obj, offset), Immediate(v_32));
4114 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004115 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004116 if (value.IsRegister()) {
4117 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4118 value.AsRegister<CpuRegister>());
4119 } else {
4120 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4121 DCHECK(IsInt<32>(v));
4122 int32_t v_32 = v;
4123 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4124 Immediate(v_32));
4125 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004126 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004127 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004128 break;
4129 }
4130
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004131 case Primitive::kPrimFloat: {
4132 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4133 if (index.IsConstant()) {
4134 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4135 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004136 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004137 } else {
4138 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004139 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4140 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004141 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004142 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004143 break;
4144 }
4145
4146 case Primitive::kPrimDouble: {
4147 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4148 if (index.IsConstant()) {
4149 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4150 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004151 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004152 } else {
4153 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004154 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4155 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004156 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004157 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004158 break;
4159 }
4160
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004161 case Primitive::kPrimVoid:
4162 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004163 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004164 }
4165}
4166
4167void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004168 LocationSummary* locations =
4169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004170 locations->SetInAt(0, Location::RequiresRegister());
4171 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004172}
4173
4174void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4175 LocationSummary* locations = instruction->GetLocations();
4176 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004177 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4178 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004180 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004181}
4182
4183void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004184 LocationSummary* locations =
4185 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004186 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004187 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004188 if (instruction->HasUses()) {
4189 locations->SetOut(Location::SameAsFirstInput());
4190 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004191}
4192
4193void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4194 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004195 Location index_loc = locations->InAt(0);
4196 Location length_loc = locations->InAt(1);
4197 SlowPathCodeX86_64* slow_path =
4198 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004199
Mark Mendell99dbd682015-04-22 16:18:52 -04004200 if (length_loc.IsConstant()) {
4201 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4202 if (index_loc.IsConstant()) {
4203 // BCE will remove the bounds check if we are guarenteed to pass.
4204 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4205 if (index < 0 || index >= length) {
4206 codegen_->AddSlowPath(slow_path);
4207 __ jmp(slow_path->GetEntryLabel());
4208 } else {
4209 // Some optimization after BCE may have generated this, and we should not
4210 // generate a bounds check if it is a valid range.
4211 }
4212 return;
4213 }
4214
4215 // We have to reverse the jump condition because the length is the constant.
4216 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4217 __ cmpl(index_reg, Immediate(length));
4218 codegen_->AddSlowPath(slow_path);
4219 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004220 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004221 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4222 if (index_loc.IsConstant()) {
4223 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4224 __ cmpl(length, Immediate(value));
4225 } else {
4226 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4227 }
4228 codegen_->AddSlowPath(slow_path);
4229 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004230 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004231}
4232
4233void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4234 CpuRegister card,
4235 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004236 CpuRegister value,
4237 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004238 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004239 if (value_can_be_null) {
4240 __ testl(value, value);
4241 __ j(kEqual, &is_null);
4242 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004243 __ gs()->movq(card, Address::Absolute(
4244 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4245 __ movq(temp, object);
4246 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004247 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004248 if (value_can_be_null) {
4249 __ Bind(&is_null);
4250 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004251}
4252
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4254 temp->SetLocations(nullptr);
4255}
4256
4257void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4258 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004259 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004260}
4261
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004262void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004263 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004264 LOG(FATAL) << "Unimplemented";
4265}
4266
4267void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004268 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4269}
4270
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004271void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4272 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4273}
4274
4275void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004276 HBasicBlock* block = instruction->GetBlock();
4277 if (block->GetLoopInformation() != nullptr) {
4278 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4279 // The back edge will generate the suspend check.
4280 return;
4281 }
4282 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4283 // The goto will generate the suspend check.
4284 return;
4285 }
4286 GenerateSuspendCheck(instruction, nullptr);
4287}
4288
4289void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4290 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004291 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004292 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4293 if (slow_path == nullptr) {
4294 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4295 instruction->SetSlowPath(slow_path);
4296 codegen_->AddSlowPath(slow_path);
4297 if (successor != nullptr) {
4298 DCHECK(successor->IsLoopHeader());
4299 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4300 }
4301 } else {
4302 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4303 }
4304
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004305 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004306 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004307 if (successor == nullptr) {
4308 __ j(kNotEqual, slow_path->GetEntryLabel());
4309 __ Bind(slow_path->GetReturnLabel());
4310 } else {
4311 __ j(kEqual, codegen_->GetLabelOf(successor));
4312 __ jmp(slow_path->GetEntryLabel());
4313 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004314}
4315
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004316X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4317 return codegen_->GetAssembler();
4318}
4319
4320void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4321 MoveOperands* move = moves_.Get(index);
4322 Location source = move->GetSource();
4323 Location destination = move->GetDestination();
4324
4325 if (source.IsRegister()) {
4326 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004327 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004328 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004329 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004330 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004331 } else {
4332 DCHECK(destination.IsDoubleStackSlot());
4333 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004334 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004335 }
4336 } else if (source.IsStackSlot()) {
4337 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004338 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004339 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004340 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004341 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004342 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004343 } else {
4344 DCHECK(destination.IsStackSlot());
4345 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4346 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4347 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004348 } else if (source.IsDoubleStackSlot()) {
4349 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004350 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004351 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004352 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004353 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4354 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004355 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004356 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004357 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4358 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4359 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004360 } else if (source.IsConstant()) {
4361 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004362 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4363 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004364 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004365 if (value == 0) {
4366 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4367 } else {
4368 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4369 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004370 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004371 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004372 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004373 }
4374 } else if (constant->IsLongConstant()) {
4375 int64_t value = constant->AsLongConstant()->GetValue();
4376 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004377 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004378 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004379 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004380 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004381 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004382 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004383 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004384 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004385 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004386 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4387 if (value == 0) {
4388 // easy FP 0.0.
4389 __ xorps(dest, dest);
4390 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004391 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004392 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004393 } else {
4394 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004395 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004396 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4397 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004398 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004399 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004400 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004401 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004402 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004403 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4404 if (value == 0) {
4405 __ xorpd(dest, dest);
4406 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004407 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004408 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004409 } else {
4410 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004411 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004412 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004413 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004414 } else if (source.IsFpuRegister()) {
4415 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004416 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004417 } else if (destination.IsStackSlot()) {
4418 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004419 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004420 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004421 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004422 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004423 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004424 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004425 }
4426}
4427
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004428void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004429 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004430 __ movl(Address(CpuRegister(RSP), mem), reg);
4431 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004432}
4433
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004434void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004435 ScratchRegisterScope ensure_scratch(
4436 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4437
4438 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4439 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4440 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4441 Address(CpuRegister(RSP), mem2 + stack_offset));
4442 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4443 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4444 CpuRegister(ensure_scratch.GetRegister()));
4445}
4446
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004447void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4448 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4449 __ movq(Address(CpuRegister(RSP), mem), reg);
4450 __ movq(reg, CpuRegister(TMP));
4451}
4452
4453void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4454 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004455 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004456
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004457 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4458 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4459 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4460 Address(CpuRegister(RSP), mem2 + stack_offset));
4461 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4462 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4463 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004464}
4465
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004466void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4467 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4468 __ movss(Address(CpuRegister(RSP), mem), reg);
4469 __ movd(reg, CpuRegister(TMP));
4470}
4471
4472void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4473 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4474 __ movsd(Address(CpuRegister(RSP), mem), reg);
4475 __ movd(reg, CpuRegister(TMP));
4476}
4477
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004478void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4479 MoveOperands* move = moves_.Get(index);
4480 Location source = move->GetSource();
4481 Location destination = move->GetDestination();
4482
4483 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004484 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004485 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004486 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004487 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004488 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004489 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004490 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4491 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004492 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004493 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004494 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004495 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4496 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004497 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004498 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4499 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4500 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004501 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004502 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004503 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004504 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004505 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004506 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004507 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004508 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004509 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004510 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004511 }
4512}
4513
4514
4515void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4516 __ pushq(CpuRegister(reg));
4517}
4518
4519
4520void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4521 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004522}
4523
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004524void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4525 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4526 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4527 Immediate(mirror::Class::kStatusInitialized));
4528 __ j(kLess, slow_path->GetEntryLabel());
4529 __ Bind(slow_path->GetExitLabel());
4530 // No need for memory fence, thanks to the X86_64 memory model.
4531}
4532
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004533void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004534 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4535 ? LocationSummary::kCallOnSlowPath
4536 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004537 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004538 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004539 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004540 locations->SetOut(Location::RequiresRegister());
4541}
4542
4543void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004544 LocationSummary* locations = cls->GetLocations();
4545 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4546 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004547 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004548 DCHECK(!cls->CanCallRuntime());
4549 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004550 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004551 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004552 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004553 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004554 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004555 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004556 __ MaybeUnpoisonHeapReference(out);
4557
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004558 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4559 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4560 codegen_->AddSlowPath(slow_path);
4561 __ testl(out, out);
4562 __ j(kEqual, slow_path->GetEntryLabel());
4563 if (cls->MustGenerateClinitCheck()) {
4564 GenerateClassInitializationCheck(slow_path, out);
4565 } else {
4566 __ Bind(slow_path->GetExitLabel());
4567 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004568 }
4569}
4570
4571void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4572 LocationSummary* locations =
4573 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4574 locations->SetInAt(0, Location::RequiresRegister());
4575 if (check->HasUses()) {
4576 locations->SetOut(Location::SameAsFirstInput());
4577 }
4578}
4579
4580void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004581 // We assume the class to not be null.
4582 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4583 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004584 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004585 GenerateClassInitializationCheck(slow_path,
4586 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004587}
4588
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004589void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4590 LocationSummary* locations =
4591 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004592 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004593 locations->SetOut(Location::RequiresRegister());
4594}
4595
4596void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4597 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4598 codegen_->AddSlowPath(slow_path);
4599
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004600 LocationSummary* locations = load->GetLocations();
4601 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4602 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004603 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004604 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004605 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004606 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004607 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004608 __ testl(out, out);
4609 __ j(kEqual, slow_path->GetEntryLabel());
4610 __ Bind(slow_path->GetExitLabel());
4611}
4612
David Brazdilcb1c0552015-08-04 16:22:25 +01004613static Address GetExceptionTlsAddress() {
4614 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4615}
4616
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004617void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4618 LocationSummary* locations =
4619 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4620 locations->SetOut(Location::RequiresRegister());
4621}
4622
4623void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004624 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4625}
4626
4627void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4628 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4629}
4630
4631void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4632 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004633}
4634
4635void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4636 LocationSummary* locations =
4637 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4638 InvokeRuntimeCallingConvention calling_convention;
4639 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4640}
4641
4642void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004643 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4644 instruction,
4645 instruction->GetDexPc(),
4646 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004647}
4648
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004649void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004650 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4651 ? LocationSummary::kNoCall
4652 : LocationSummary::kCallOnSlowPath;
4653 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4654 locations->SetInAt(0, Location::RequiresRegister());
4655 locations->SetInAt(1, Location::Any());
4656 locations->SetOut(Location::RequiresRegister());
4657}
4658
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004659void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004660 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004661 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004662 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004663 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004664 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Mark Mendell0c9497d2015-08-21 09:30:05 -04004665 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004666 SlowPathCodeX86_64* slow_path = nullptr;
4667
4668 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004669 // Avoid null check if we know obj is not null.
4670 if (instruction->MustDoNullCheck()) {
4671 __ testl(obj, obj);
4672 __ j(kEqual, &zero);
4673 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004674 // Compare the class of `obj` with `cls`.
4675 __ movl(out, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004676 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004677 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004678 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004679 } else {
4680 DCHECK(cls.IsStackSlot()) << cls;
4681 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4682 }
4683 if (instruction->IsClassFinal()) {
4684 // Classes must be equal for the instanceof to succeed.
4685 __ j(kNotEqual, &zero);
4686 __ movl(out, Immediate(1));
4687 __ jmp(&done);
4688 } else {
4689 // If the classes are not equal, we go into a slow path.
4690 DCHECK(locations->OnlyCallsOnSlowPath());
4691 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004692 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004693 codegen_->AddSlowPath(slow_path);
4694 __ j(kNotEqual, slow_path->GetEntryLabel());
4695 __ movl(out, Immediate(1));
4696 __ jmp(&done);
4697 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004698
4699 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4700 __ Bind(&zero);
4701 __ movl(out, Immediate(0));
4702 }
4703
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004704 if (slow_path != nullptr) {
4705 __ Bind(slow_path->GetExitLabel());
4706 }
4707 __ Bind(&done);
4708}
4709
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004710void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4711 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4712 instruction, LocationSummary::kCallOnSlowPath);
4713 locations->SetInAt(0, Location::RequiresRegister());
4714 locations->SetInAt(1, Location::Any());
4715 locations->AddTemp(Location::RequiresRegister());
4716}
4717
4718void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4719 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004720 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004721 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004722 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004723 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4724 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4725 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4726 codegen_->AddSlowPath(slow_path);
4727
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004728 // Avoid null check if we know obj is not null.
4729 if (instruction->MustDoNullCheck()) {
4730 __ testl(obj, obj);
4731 __ j(kEqual, slow_path->GetExitLabel());
4732 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004733 // Compare the class of `obj` with `cls`.
4734 __ movl(temp, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004735 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004736 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004737 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004738 } else {
4739 DCHECK(cls.IsStackSlot()) << cls;
4740 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4741 }
Roland Levillain4d027112015-07-01 15:41:14 +01004742 // The checkcast succeeds if the classes are equal (fast path).
4743 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004744 __ j(kNotEqual, slow_path->GetEntryLabel());
4745 __ Bind(slow_path->GetExitLabel());
4746}
4747
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004748void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4749 LocationSummary* locations =
4750 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4751 InvokeRuntimeCallingConvention calling_convention;
4752 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4753}
4754
4755void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004756 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4757 : QUICK_ENTRY_POINT(pUnlockObject),
4758 instruction,
4759 instruction->GetDexPc(),
4760 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004761}
4762
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004763void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4764void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4765void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4766
4767void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4768 LocationSummary* locations =
4769 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4770 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4771 || instruction->GetResultType() == Primitive::kPrimLong);
4772 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004773 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004774 locations->SetOut(Location::SameAsFirstInput());
4775}
4776
4777void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4778 HandleBitwiseOperation(instruction);
4779}
4780
4781void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4782 HandleBitwiseOperation(instruction);
4783}
4784
4785void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4786 HandleBitwiseOperation(instruction);
4787}
4788
4789void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4790 LocationSummary* locations = instruction->GetLocations();
4791 Location first = locations->InAt(0);
4792 Location second = locations->InAt(1);
4793 DCHECK(first.Equals(locations->Out()));
4794
4795 if (instruction->GetResultType() == Primitive::kPrimInt) {
4796 if (second.IsRegister()) {
4797 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004798 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004799 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004800 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004801 } else {
4802 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004803 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004804 }
4805 } else if (second.IsConstant()) {
4806 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4807 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004808 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004809 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004810 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004811 } else {
4812 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004813 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004814 }
4815 } else {
4816 Address address(CpuRegister(RSP), second.GetStackIndex());
4817 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004818 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004819 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004820 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004821 } else {
4822 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004823 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004824 }
4825 }
4826 } else {
4827 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004828 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4829 bool second_is_constant = false;
4830 int64_t value = 0;
4831 if (second.IsConstant()) {
4832 second_is_constant = true;
4833 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004834 }
Mark Mendell40741f32015-04-20 22:10:34 -04004835 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004836
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004837 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004838 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004839 if (is_int32_value) {
4840 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4841 } else {
4842 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4843 }
4844 } else if (second.IsDoubleStackSlot()) {
4845 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004846 } else {
4847 __ andq(first_reg, second.AsRegister<CpuRegister>());
4848 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004849 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004850 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004851 if (is_int32_value) {
4852 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4853 } else {
4854 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4855 }
4856 } else if (second.IsDoubleStackSlot()) {
4857 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004858 } else {
4859 __ orq(first_reg, second.AsRegister<CpuRegister>());
4860 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004861 } else {
4862 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004863 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004864 if (is_int32_value) {
4865 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4866 } else {
4867 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4868 }
4869 } else if (second.IsDoubleStackSlot()) {
4870 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004871 } else {
4872 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4873 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004874 }
4875 }
4876}
4877
Calin Juravleb1498f62015-02-16 13:13:29 +00004878void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4879 // Nothing to do, this should be removed during prepare for register allocator.
4880 UNUSED(instruction);
4881 LOG(FATAL) << "Unreachable";
4882}
4883
4884void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4885 // Nothing to do, this should be removed during prepare for register allocator.
4886 UNUSED(instruction);
4887 LOG(FATAL) << "Unreachable";
4888}
4889
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004890void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
4891 DCHECK(codegen_->IsBaseline());
4892 LocationSummary* locations =
4893 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4894 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4895}
4896
4897void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4898 DCHECK(codegen_->IsBaseline());
4899 // Will be generated at use site.
4900}
4901
Mark Mendell92e83bf2015-05-07 11:25:03 -04004902void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4903 if (value == 0) {
4904 __ xorl(dest, dest);
4905 } else if (value > 0 && IsInt<32>(value)) {
4906 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4907 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4908 } else {
4909 __ movq(dest, Immediate(value));
4910 }
4911}
4912
Mark Mendellcfa410b2015-05-25 16:02:44 -04004913void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
4914 DCHECK(dest.IsDoubleStackSlot());
4915 if (IsInt<32>(value)) {
4916 // Can move directly as an int32 constant.
4917 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
4918 Immediate(static_cast<int32_t>(value)));
4919 } else {
4920 Load64BitValue(CpuRegister(TMP), value);
4921 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
4922 }
4923}
4924
Mark Mendellf55c3e02015-03-26 21:07:46 -04004925void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4926 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004927 X86_64Assembler* assembler = GetAssembler();
4928 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004929 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4930 // byte values. If used for vectors at a later time, this will need to be
4931 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004932 assembler->Align(4, 0);
4933 constant_area_start_ = assembler->CodeSize();
4934 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004935 }
4936
4937 // And finish up.
4938 CodeGenerator::Finalize(allocator);
4939}
4940
4941/**
4942 * Class to handle late fixup of offsets into constant area.
4943 */
4944class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4945 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004946 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004947 : codegen_(codegen), offset_into_constant_area_(offset) {}
4948
4949 private:
4950 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4951 // Patch the correct offset for the instruction. We use the address of the
4952 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4953 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4954 int relative_position = constant_offset - pos;
4955
4956 // Patch in the right value.
4957 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4958 }
4959
Mark Mendell39dcf552015-04-09 20:42:42 -04004960 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004961
4962 // Location in constant area that the fixup refers to.
4963 int offset_into_constant_area_;
4964};
4965
4966Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4967 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4968 return Address::RIP(fixup);
4969}
4970
4971Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4972 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4973 return Address::RIP(fixup);
4974}
4975
4976Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4977 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4978 return Address::RIP(fixup);
4979}
4980
4981Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4982 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4983 return Address::RIP(fixup);
4984}
4985
Roland Levillain4d027112015-07-01 15:41:14 +01004986#undef __
4987
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004988} // namespace x86_64
4989} // namespace art