blob: 0cf1089cf8f27ee567146404e528580c4ea030c1 [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());
David Brazdil77a48ae2015-09-15 12:34:04 +000060 if (instruction_->CanThrowIntoCatchBlock()) {
61 // Live registers will be restored in the catch block if caught.
62 SaveLiveRegisters(codegen, instruction_->GetLocations());
63 }
Alexandre Rames8158f282015-08-07 10:26:17 +010064 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
65 instruction_,
66 instruction_->GetDexPc(),
67 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 }
69
Alexandre Rames8158f282015-08-07 10:26:17 +010070 bool IsFatal() const OVERRIDE { return true; }
71
Alexandre Rames9931f312015-06-19 14:47:01 +010072 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
73
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
77};
78
Calin Juravled0d48522014-11-04 16:40:20 +000079class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
80 public:
81 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
82
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010084 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000085 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000086 if (instruction_->CanThrowIntoCatchBlock()) {
87 // Live registers will be restored in the catch block if caught.
88 SaveLiveRegisters(codegen, instruction_->GetLocations());
89 }
Alexandre Rames8158f282015-08-07 10:26:17 +010090 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
91 instruction_,
92 instruction_->GetDexPc(),
93 this);
Calin Juravled0d48522014-11-04 16:40:20 +000094 }
95
Alexandre Rames8158f282015-08-07 10:26:17 +010096 bool IsFatal() const OVERRIDE { return true; }
97
Alexandre Rames9931f312015-06-19 14:47:01 +010098 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
99
Calin Juravled0d48522014-11-04 16:40:20 +0000100 private:
101 HDivZeroCheck* const instruction_;
102 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
103};
104
Calin Juravlebacfec32014-11-14 15:54:36 +0000105class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +0000106 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100107 DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000110 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000112 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(cpu_reg_);
115 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400116 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000117 }
118
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000119 } else {
120 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000121 if (is_div_) {
122 __ negq(cpu_reg_);
123 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400124 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 }
Calin Juravled0d48522014-11-04 16:40:20 +0000127 __ jmp(GetExitLabel());
128 }
129
Alexandre Rames9931f312015-06-19 14:47:01 +0100130 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
131
Calin Juravled0d48522014-11-04 16:40:20 +0000132 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000133 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000134 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000135 const bool is_div_;
136 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000137};
138
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100139class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100141 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000144 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100145 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000147 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100148 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
149 instruction_,
150 instruction_->GetDexPc(),
151 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000152 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100153 if (successor_ == nullptr) {
154 __ jmp(GetReturnLabel());
155 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100156 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 }
159
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 Label* GetReturnLabel() {
161 DCHECK(successor_ == nullptr);
162 return &return_label_;
163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100165 HBasicBlock* GetSuccessor() const {
166 return successor_;
167 }
168
Alexandre Rames9931f312015-06-19 14:47:01 +0100169 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
170
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 private:
172 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
177};
178
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100179class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100181 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
182 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000184 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100185 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames8158f282015-08-07 10:26:17 +0100186 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000188 if (instruction_->CanThrowIntoCatchBlock()) {
189 // Live registers will be restored in the catch block if caught.
190 SaveLiveRegisters(codegen, instruction_->GetLocations());
191 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000192 // We're moving two locations to locations that could overlap, so we need a parallel
193 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000195 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100196 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000197 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100198 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100199 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100200 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
201 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100202 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
203 instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100204 }
205
Alexandre Rames8158f282015-08-07 10:26:17 +0100206 bool IsFatal() const OVERRIDE { return true; }
207
Alexandre Rames9931f312015-06-19 14:47:01 +0100208 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
209
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100210 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100211 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100212
213 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
214};
215
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100217 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000218 LoadClassSlowPathX86_64(HLoadClass* cls,
219 HInstruction* at,
220 uint32_t dex_pc,
221 bool do_clinit)
222 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
223 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
224 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
229 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000231 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100233 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100235 x64_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
236 : QUICK_ENTRY_POINT(pInitializeType),
237 at_, dex_pc_, this);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100238
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000239 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000241 if (out.IsValid()) {
242 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
243 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 }
245
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000246 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100247 __ jmp(GetExitLabel());
248 }
249
Alexandre Rames9931f312015-06-19 14:47:01 +0100250 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
251
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 // The class this slow path will load.
254 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100255
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 // The instruction where this slow path is happening.
257 // (Might be the load class or an initialization check).
258 HInstruction* const at_;
259
260 // The dex PC of `at_`.
261 const uint32_t dex_pc_;
262
263 // Whether to initialize the class.
264 const bool do_clinit_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100267};
268
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000269class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
270 public:
271 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
272
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274 LocationSummary* locations = instruction_->GetLocations();
275 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
276
277 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
278 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000279 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000280
281 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800282 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000283 Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100284 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
285 instruction_,
286 instruction_->GetDexPc(),
287 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000288 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000289 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000290 __ jmp(GetExitLabel());
291 }
292
Alexandre Rames9931f312015-06-19 14:47:01 +0100293 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
294
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000295 private:
296 HLoadString* const instruction_;
297
298 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
299};
300
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
302 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100303 explicit TypeCheckSlowPathX86_64(HInstruction* instruction)
304 : instruction_(instruction) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000306 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000307 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100308 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
309 : locations->Out();
310 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 DCHECK(instruction_->IsCheckCast()
312 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313
314 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
315 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000316 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
318 // We're moving two locations to locations that could overlap, so we need a parallel
319 // move resolver.
320 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000321 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100322 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000323 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100324 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100326 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
327 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100330 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
331 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100332 dex_pc,
Alexandre Rames8158f282015-08-07 10:26:17 +0100333 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000334 } else {
335 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100336 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
337 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100338 dex_pc,
Alexandre Rames8158f282015-08-07 10:26:17 +0100339 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000340 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000341
342 if (instruction_->IsInstanceOf()) {
343 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
344 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000346 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347 __ jmp(GetExitLabel());
348 }
349
Alexandre Rames9931f312015-06-19 14:47:01 +0100350 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
351
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000352 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353 HInstruction* const instruction_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000354
355 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
356};
357
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
359 public:
360 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
361 : instruction_(instruction) {}
362
363 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100364 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 __ Bind(GetEntryLabel());
366 SaveLiveRegisters(codegen, instruction_->GetLocations());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 DCHECK(instruction_->IsDeoptimize());
368 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
Alexandre Rames8158f282015-08-07 10:26:17 +0100369 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
370 deoptimize,
371 deoptimize->GetDexPc(),
372 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700373 }
374
Alexandre Rames9931f312015-06-19 14:47:01 +0100375 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
376
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700377 private:
378 HInstruction* const instruction_;
379 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
380};
381
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100382#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100383#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100384
Roland Levillain4fa13f62015-07-06 18:11:54 +0100385inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700386 switch (cond) {
387 case kCondEQ: return kEqual;
388 case kCondNE: return kNotEqual;
389 case kCondLT: return kLess;
390 case kCondLE: return kLessEqual;
391 case kCondGT: return kGreater;
392 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700393 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100394 LOG(FATAL) << "Unreachable";
395 UNREACHABLE();
396}
397
398inline Condition X86_64FPCondition(IfCondition cond) {
399 switch (cond) {
400 case kCondEQ: return kEqual;
401 case kCondNE: return kNotEqual;
402 case kCondLT: return kBelow;
403 case kCondLE: return kBelowEqual;
404 case kCondGT: return kAbove;
405 case kCondGE: return kAboveEqual;
406 };
407 LOG(FATAL) << "Unreachable";
408 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700409}
410
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800411void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100412 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800413 // All registers are assumed to be correctly set up.
414
Vladimir Marko58155012015-08-19 12:49:41 +0000415 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
416 switch (invoke->GetMethodLoadKind()) {
417 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
418 // temp = thread->string_init_entrypoint
419 __ gs()->movl(temp.AsRegister<CpuRegister>(),
420 Address::Absolute(invoke->GetStringInitOffset(), true));
421 break;
422 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
423 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
424 break;
425 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
426 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
427 break;
428 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
429 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
430 method_patches_.emplace_back(invoke->GetTargetMethod());
431 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
432 break;
433 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
434 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
435 invoke->GetDexCacheArrayOffset());
436 __ movq(temp.AsRegister<CpuRegister>(),
437 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
438 // Bind the label at the end of the "movl" insn.
439 __ Bind(&pc_rel_dex_cache_patches_.back().label);
440 break;
441 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
442 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
443 Register method_reg;
444 CpuRegister reg = temp.AsRegister<CpuRegister>();
445 if (current_method.IsRegister()) {
446 method_reg = current_method.AsRegister<Register>();
447 } else {
448 DCHECK(invoke->GetLocations()->Intrinsified());
449 DCHECK(!current_method.IsValid());
450 method_reg = reg.AsRegister();
451 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
452 }
453 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100454 __ movq(reg,
455 Address(CpuRegister(method_reg),
456 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000457 // temp = temp[index_in_cache]
458 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
459 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
460 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100461 }
Vladimir Marko58155012015-08-19 12:49:41 +0000462 }
463
464 switch (invoke->GetCodePtrLocation()) {
465 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
466 __ call(&frame_entry_label_);
467 break;
468 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
469 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
470 Label* label = &relative_call_patches_.back().label;
471 __ call(label); // Bind to the patch label, override at link time.
472 __ Bind(label); // Bind the label at the end of the "call" insn.
473 break;
474 }
475 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
476 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
477 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
478 FALLTHROUGH_INTENDED;
479 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
480 // (callee_method + offset_of_quick_compiled_code)()
481 __ call(Address(callee_method.AsRegister<CpuRegister>(),
482 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
483 kX86_64WordSize).SizeValue()));
484 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000485 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800486
487 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800488}
489
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000490void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
491 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
492 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
493 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
494 LocationSummary* locations = invoke->GetLocations();
495 Location receiver = locations->InAt(0);
496 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
497 // temp = object->GetClass();
498 DCHECK(receiver.IsRegister());
499 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
500 MaybeRecordImplicitNullCheck(invoke);
501 __ MaybeUnpoisonHeapReference(temp);
502 // temp = temp->GetMethodAt(method_offset);
503 __ movq(temp, Address(temp, method_offset));
504 // call temp->GetEntryPoint();
505 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
506 kX86_64WordSize).SizeValue()));
507}
508
Vladimir Marko58155012015-08-19 12:49:41 +0000509void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
510 DCHECK(linker_patches->empty());
511 size_t size =
512 method_patches_.size() + relative_call_patches_.size() + pc_rel_dex_cache_patches_.size();
513 linker_patches->reserve(size);
514 for (const MethodPatchInfo<Label>& info : method_patches_) {
515 // The label points to the end of the "movl" instruction but the literal offset for method
516 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
517 uint32_t literal_offset = info.label.Position() - 4;
518 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
519 info.target_method.dex_file,
520 info.target_method.dex_method_index));
521 }
522 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
523 // The label points to the end of the "call" instruction but the literal offset for method
524 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
525 uint32_t literal_offset = info.label.Position() - 4;
526 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
527 info.target_method.dex_file,
528 info.target_method.dex_method_index));
529 }
530 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
531 // The label points to the end of the "mov" instruction but the literal offset for method
532 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
533 uint32_t literal_offset = info.label.Position() - 4;
534 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
535 &info.target_dex_file,
536 info.label.Position(),
537 info.element_offset));
538 }
539}
540
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100541void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100542 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100543}
544
545void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100546 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100547}
548
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100549size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
550 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
551 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100552}
553
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100554size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
555 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
556 return kX86_64WordSize;
557}
558
559size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
560 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
561 return kX86_64WordSize;
562}
563
564size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
565 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
566 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100567}
568
Alexandre Rames8158f282015-08-07 10:26:17 +0100569void CodeGeneratorX86_64::InvokeRuntime(Address entry_point,
570 HInstruction* instruction,
571 uint32_t dex_pc,
572 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100573 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100574 __ gs()->call(entry_point);
575 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100576}
577
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000578static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000579// Use a fake return address register to mimic Quick.
580static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400581CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
582 const X86_64InstructionSetFeatures& isa_features,
583 const CompilerOptions& compiler_options)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000584 : CodeGenerator(graph,
585 kNumberOfCpuRegisters,
586 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000587 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000588 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
589 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000590 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000591 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
592 arraysize(kFpuCalleeSaves)),
Nicolas Geoffray98893962015-01-21 12:32:32 +0000593 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100594 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100595 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000596 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400597 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400598 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000599 constant_area_start_(0),
600 method_patches_(graph->GetArena()->Adapter()),
601 relative_call_patches_(graph->GetArena()->Adapter()),
602 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000603 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
604}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100606InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
607 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100608 : HGraphVisitor(graph),
609 assembler_(codegen->GetAssembler()),
610 codegen_(codegen) {}
611
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100612Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100613 switch (type) {
614 case Primitive::kPrimLong:
615 case Primitive::kPrimByte:
616 case Primitive::kPrimBoolean:
617 case Primitive::kPrimChar:
618 case Primitive::kPrimShort:
619 case Primitive::kPrimInt:
620 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100621 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100622 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100623 }
624
625 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100627 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100628 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100630
631 case Primitive::kPrimVoid:
632 LOG(FATAL) << "Unreachable type " << type;
633 }
634
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100635 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100636}
637
Nicolas Geoffray98893962015-01-21 12:32:32 +0000638void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100639 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100640 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100641
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000642 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100643 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000644
Nicolas Geoffray98893962015-01-21 12:32:32 +0000645 if (is_baseline) {
646 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
647 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
648 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000649 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
650 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
651 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000652 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100653}
654
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100655static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100656 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100657}
David Srbecky9d8606d2015-04-12 09:35:32 +0100658
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100659static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100660 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100661}
662
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100663void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100664 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000665 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100666 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700667 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000668 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100669
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000670 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100671 __ testq(CpuRegister(RAX), Address(
672 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100673 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100674 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000675
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000676 if (HasEmptyFrame()) {
677 return;
678 }
679
Nicolas Geoffray98893962015-01-21 12:32:32 +0000680 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000681 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000682 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000683 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100684 __ cfi().AdjustCFAOffset(kX86_64WordSize);
685 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000686 }
687 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100688
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100689 int adjust = GetFrameSize() - GetCoreSpillSize();
690 __ subq(CpuRegister(RSP), Immediate(adjust));
691 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000692 uint32_t xmm_spill_location = GetFpuSpillStart();
693 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100694
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000695 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
696 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100697 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
698 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
699 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000700 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100701 }
702
Mathieu Chartiere401d142015-04-22 13:56:20 -0700703 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100704 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100705}
706
707void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100708 __ cfi().RememberState();
709 if (!HasEmptyFrame()) {
710 uint32_t xmm_spill_location = GetFpuSpillStart();
711 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
712 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
713 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
714 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
715 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
716 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
717 }
718 }
719
720 int adjust = GetFrameSize() - GetCoreSpillSize();
721 __ addq(CpuRegister(RSP), Immediate(adjust));
722 __ cfi().AdjustCFAOffset(-adjust);
723
724 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
725 Register reg = kCoreCalleeSaves[i];
726 if (allocated_registers_.ContainsCoreRegister(reg)) {
727 __ popq(CpuRegister(reg));
728 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
729 __ cfi().Restore(DWARFReg(reg));
730 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000731 }
732 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100733 __ ret();
734 __ cfi().RestoreState();
735 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100736}
737
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100738void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
739 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100740}
741
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
743 switch (load->GetType()) {
744 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100745 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100746 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100747
748 case Primitive::kPrimInt:
749 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100750 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100751 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100752
753 case Primitive::kPrimBoolean:
754 case Primitive::kPrimByte:
755 case Primitive::kPrimChar:
756 case Primitive::kPrimShort:
757 case Primitive::kPrimVoid:
758 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700759 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100760 }
761
762 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700763 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100764}
765
766void CodeGeneratorX86_64::Move(Location destination, Location source) {
767 if (source.Equals(destination)) {
768 return;
769 }
770 if (destination.IsRegister()) {
771 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000772 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100773 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000774 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100775 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000776 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100777 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100778 } else {
779 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000780 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100781 Address(CpuRegister(RSP), source.GetStackIndex()));
782 }
783 } else if (destination.IsFpuRegister()) {
784 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000785 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100786 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000787 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000789 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100790 Address(CpuRegister(RSP), source.GetStackIndex()));
791 } else {
792 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000793 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100794 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100795 }
796 } else if (destination.IsStackSlot()) {
797 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000799 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100800 } else if (source.IsFpuRegister()) {
801 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000802 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500803 } else if (source.IsConstant()) {
804 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000805 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500806 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100807 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500808 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000809 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
810 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100811 }
812 } else {
813 DCHECK(destination.IsDoubleStackSlot());
814 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100815 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000816 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100817 } else if (source.IsFpuRegister()) {
818 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000819 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500820 } else if (source.IsConstant()) {
821 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800822 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500823 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000824 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500825 } else {
826 DCHECK(constant->IsLongConstant());
827 value = constant->AsLongConstant()->GetValue();
828 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400829 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100830 } else {
831 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000832 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
833 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100834 }
835 }
836}
837
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100838void CodeGeneratorX86_64::Move(HInstruction* instruction,
839 Location location,
840 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000841 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100842 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700843 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100844 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000845 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100846 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000847 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000848 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
849 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000850 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000851 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000852 } else if (location.IsStackSlot()) {
853 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
854 } else {
855 DCHECK(location.IsConstant());
856 DCHECK_EQ(location.GetConstant(), const_to_move);
857 }
858 } else if (const_to_move->IsLongConstant()) {
859 int64_t value = const_to_move->AsLongConstant()->GetValue();
860 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400861 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000862 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400863 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000864 } else {
865 DCHECK(location.IsConstant());
866 DCHECK_EQ(location.GetConstant(), const_to_move);
867 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100868 }
Roland Levillain476df552014-10-09 17:51:36 +0100869 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100870 switch (instruction->GetType()) {
871 case Primitive::kPrimBoolean:
872 case Primitive::kPrimByte:
873 case Primitive::kPrimChar:
874 case Primitive::kPrimShort:
875 case Primitive::kPrimInt:
876 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100877 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100878 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
879 break;
880
881 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100882 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000883 Move(location,
884 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100885 break;
886
887 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100888 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100889 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000890 } else if (instruction->IsTemporary()) {
891 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
892 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100893 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100894 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100895 switch (instruction->GetType()) {
896 case Primitive::kPrimBoolean:
897 case Primitive::kPrimByte:
898 case Primitive::kPrimChar:
899 case Primitive::kPrimShort:
900 case Primitive::kPrimInt:
901 case Primitive::kPrimNot:
902 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100903 case Primitive::kPrimFloat:
904 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000905 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100906 break;
907
908 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100909 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100910 }
911 }
912}
913
David Brazdilfc6a86a2015-06-26 10:33:45 +0000914void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100915 DCHECK(!successor->IsExitBlock());
916
917 HBasicBlock* block = got->GetBlock();
918 HInstruction* previous = got->GetPrevious();
919
920 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000921 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100922 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
923 return;
924 }
925
926 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
927 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
928 }
929 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100930 __ jmp(codegen_->GetLabelOf(successor));
931 }
932}
933
David Brazdilfc6a86a2015-06-26 10:33:45 +0000934void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
935 got->SetLocations(nullptr);
936}
937
938void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
939 HandleGoto(got, got->GetSuccessor());
940}
941
942void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
943 try_boundary->SetLocations(nullptr);
944}
945
946void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
947 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
948 if (!successor->IsExitBlock()) {
949 HandleGoto(try_boundary, successor);
950 }
951}
952
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100953void LocationsBuilderX86_64::VisitExit(HExit* exit) {
954 exit->SetLocations(nullptr);
955}
956
957void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700958 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100959}
960
Mark Mendellc4701932015-04-10 13:18:51 -0400961void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
962 Label* true_label,
963 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100964 if (cond->IsFPConditionTrueIfNaN()) {
965 __ j(kUnordered, true_label);
966 } else if (cond->IsFPConditionFalseIfNaN()) {
967 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400968 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100969 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400970}
971
972void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
973 HCondition* condition,
974 Label* true_target,
975 Label* false_target,
976 Label* always_true_target) {
977 LocationSummary* locations = condition->GetLocations();
978 Location left = locations->InAt(0);
979 Location right = locations->InAt(1);
980
981 // We don't want true_target as a nullptr.
982 if (true_target == nullptr) {
983 true_target = always_true_target;
984 }
985 bool falls_through = (false_target == nullptr);
986
987 // FP compares don't like null false_targets.
988 if (false_target == nullptr) {
989 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
990 }
991
992 Primitive::Type type = condition->InputAt(0)->GetType();
993 switch (type) {
994 case Primitive::kPrimLong: {
995 CpuRegister left_reg = left.AsRegister<CpuRegister>();
996 if (right.IsConstant()) {
997 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
998 if (IsInt<32>(value)) {
999 if (value == 0) {
1000 __ testq(left_reg, left_reg);
1001 } else {
1002 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1003 }
1004 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001005 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001006 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1007 }
1008 } else if (right.IsDoubleStackSlot()) {
1009 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1010 } else {
1011 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1012 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001013 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001014 break;
1015 }
1016 case Primitive::kPrimFloat: {
1017 if (right.IsFpuRegister()) {
1018 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1019 } else if (right.IsConstant()) {
1020 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1021 codegen_->LiteralFloatAddress(
1022 right.GetConstant()->AsFloatConstant()->GetValue()));
1023 } else {
1024 DCHECK(right.IsStackSlot());
1025 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1026 Address(CpuRegister(RSP), right.GetStackIndex()));
1027 }
1028 GenerateFPJumps(condition, true_target, false_target);
1029 break;
1030 }
1031 case Primitive::kPrimDouble: {
1032 if (right.IsFpuRegister()) {
1033 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1034 } else if (right.IsConstant()) {
1035 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1036 codegen_->LiteralDoubleAddress(
1037 right.GetConstant()->AsDoubleConstant()->GetValue()));
1038 } else {
1039 DCHECK(right.IsDoubleStackSlot());
1040 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1041 Address(CpuRegister(RSP), right.GetStackIndex()));
1042 }
1043 GenerateFPJumps(condition, true_target, false_target);
1044 break;
1045 }
1046 default:
1047 LOG(FATAL) << "Unexpected condition type " << type;
1048 }
1049
1050 if (!falls_through) {
1051 __ jmp(false_target);
1052 }
1053}
1054
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001055void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1056 Label* true_target,
1057 Label* false_target,
1058 Label* always_true_target) {
1059 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001060 if (cond->IsIntConstant()) {
1061 // Constant condition, statically compared against 1.
1062 int32_t cond_value = cond->AsIntConstant()->GetValue();
1063 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001064 if (always_true_target != nullptr) {
1065 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001066 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001067 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001068 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001069 DCHECK_EQ(cond_value, 0);
1070 }
1071 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001072 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001073 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1074 // Moves do not affect the eflags register, so if the condition is
1075 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001076 // again. We can't use the eflags on FP conditions if they are
1077 // materialized due to the complex branching.
1078 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001079 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001080 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1081 && !Primitive::IsFloatingPointType(type);
1082
Roland Levillain4fa13f62015-07-06 18:11:54 +01001083 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001084 if (!eflags_set) {
1085 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001086 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001087 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001088 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001089 } else {
1090 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1091 Immediate(0));
1092 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001093 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001094 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001095 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001096 }
1097 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001098 // Condition has not been materialized, use its inputs as the
1099 // comparison and its condition as the branch condition.
1100
Mark Mendellc4701932015-04-10 13:18:51 -04001101 // Is this a long or FP comparison that has been folded into the HCondition?
1102 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001103 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001104 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1105 true_target, false_target, always_true_target);
1106 return;
1107 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001108
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001109 Location lhs = cond->GetLocations()->InAt(0);
1110 Location rhs = cond->GetLocations()->InAt(1);
1111 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001112 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001113 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001114 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001115 if (constant == 0) {
1116 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1117 } else {
1118 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1119 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001120 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001121 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001122 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1123 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001124 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001125 }
Dave Allison20dfc792014-06-16 20:44:29 -07001126 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001127 if (false_target != nullptr) {
1128 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129 }
1130}
1131
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001132void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1133 LocationSummary* locations =
1134 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1135 HInstruction* cond = if_instr->InputAt(0);
1136 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1137 locations->SetInAt(0, Location::Any());
1138 }
1139}
1140
1141void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1142 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1143 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1144 Label* always_true_target = true_target;
1145 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1146 if_instr->IfTrueSuccessor())) {
1147 always_true_target = nullptr;
1148 }
1149 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1150 if_instr->IfFalseSuccessor())) {
1151 false_target = nullptr;
1152 }
1153 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1154}
1155
1156void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1157 LocationSummary* locations = new (GetGraph()->GetArena())
1158 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1159 HInstruction* cond = deoptimize->InputAt(0);
1160 DCHECK(cond->IsCondition());
1161 if (cond->AsCondition()->NeedsMaterialization()) {
1162 locations->SetInAt(0, Location::Any());
1163 }
1164}
1165
1166void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1167 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
1168 DeoptimizationSlowPathX86_64(deoptimize);
1169 codegen_->AddSlowPath(slow_path);
1170 Label* slow_path_entry = slow_path->GetEntryLabel();
1171 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1172}
1173
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001174void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1175 local->SetLocations(nullptr);
1176}
1177
1178void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1179 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1180}
1181
1182void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1183 local->SetLocations(nullptr);
1184}
1185
1186void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1187 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001188 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001189}
1190
1191void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001192 LocationSummary* locations =
1193 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001194 switch (store->InputAt(1)->GetType()) {
1195 case Primitive::kPrimBoolean:
1196 case Primitive::kPrimByte:
1197 case Primitive::kPrimChar:
1198 case Primitive::kPrimShort:
1199 case Primitive::kPrimInt:
1200 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001201 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001202 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1203 break;
1204
1205 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001206 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001207 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1208 break;
1209
1210 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001211 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001212 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001213}
1214
1215void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001216 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001217}
1218
Roland Levillain0d37cd02015-05-27 16:39:19 +01001219void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001220 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001221 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001222 // Handle the long/FP comparisons made in instruction simplification.
1223 switch (cond->InputAt(0)->GetType()) {
1224 case Primitive::kPrimLong:
1225 locations->SetInAt(0, Location::RequiresRegister());
1226 locations->SetInAt(1, Location::Any());
1227 break;
1228 case Primitive::kPrimFloat:
1229 case Primitive::kPrimDouble:
1230 locations->SetInAt(0, Location::RequiresFpuRegister());
1231 locations->SetInAt(1, Location::Any());
1232 break;
1233 default:
1234 locations->SetInAt(0, Location::RequiresRegister());
1235 locations->SetInAt(1, Location::Any());
1236 break;
1237 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001238 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001239 locations->SetOut(Location::RequiresRegister());
1240 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241}
1242
Roland Levillain0d37cd02015-05-27 16:39:19 +01001243void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001244 if (!cond->NeedsMaterialization()) {
1245 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001246 }
Mark Mendellc4701932015-04-10 13:18:51 -04001247
1248 LocationSummary* locations = cond->GetLocations();
1249 Location lhs = locations->InAt(0);
1250 Location rhs = locations->InAt(1);
1251 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1252 Label true_label, false_label;
1253
1254 switch (cond->InputAt(0)->GetType()) {
1255 default:
1256 // Integer case.
1257
1258 // Clear output register: setcc only sets the low byte.
1259 __ xorl(reg, reg);
1260
1261 if (rhs.IsRegister()) {
1262 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1263 } else if (rhs.IsConstant()) {
1264 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1265 if (constant == 0) {
1266 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1267 } else {
1268 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1269 }
1270 } else {
1271 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1272 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001273 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001274 return;
1275 case Primitive::kPrimLong:
1276 // Clear output register: setcc only sets the low byte.
1277 __ xorl(reg, reg);
1278
1279 if (rhs.IsRegister()) {
1280 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1281 } else if (rhs.IsConstant()) {
1282 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1283 if (IsInt<32>(value)) {
1284 if (value == 0) {
1285 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1286 } else {
1287 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1288 }
1289 } else {
1290 // Value won't fit in an int.
1291 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1292 }
1293 } else {
1294 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1295 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001296 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001297 return;
1298 case Primitive::kPrimFloat: {
1299 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1300 if (rhs.IsConstant()) {
1301 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1302 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1303 } else if (rhs.IsStackSlot()) {
1304 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1305 } else {
1306 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1307 }
1308 GenerateFPJumps(cond, &true_label, &false_label);
1309 break;
1310 }
1311 case Primitive::kPrimDouble: {
1312 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1313 if (rhs.IsConstant()) {
1314 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1315 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1316 } else if (rhs.IsDoubleStackSlot()) {
1317 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1318 } else {
1319 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1320 }
1321 GenerateFPJumps(cond, &true_label, &false_label);
1322 break;
1323 }
1324 }
1325
1326 // Convert the jumps into the result.
1327 Label done_label;
1328
Roland Levillain4fa13f62015-07-06 18:11:54 +01001329 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001330 __ Bind(&false_label);
1331 __ xorl(reg, reg);
1332 __ jmp(&done_label);
1333
Roland Levillain4fa13f62015-07-06 18:11:54 +01001334 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001335 __ Bind(&true_label);
1336 __ movl(reg, Immediate(1));
1337 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001338}
1339
1340void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1341 VisitCondition(comp);
1342}
1343
1344void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1345 VisitCondition(comp);
1346}
1347
1348void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1349 VisitCondition(comp);
1350}
1351
1352void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1353 VisitCondition(comp);
1354}
1355
1356void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1357 VisitCondition(comp);
1358}
1359
1360void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1361 VisitCondition(comp);
1362}
1363
1364void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1365 VisitCondition(comp);
1366}
1367
1368void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1369 VisitCondition(comp);
1370}
1371
1372void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1373 VisitCondition(comp);
1374}
1375
1376void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1377 VisitCondition(comp);
1378}
1379
1380void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1381 VisitCondition(comp);
1382}
1383
1384void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1385 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001386}
1387
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001388void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001389 LocationSummary* locations =
1390 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001391 switch (compare->InputAt(0)->GetType()) {
1392 case Primitive::kPrimLong: {
1393 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001394 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001395 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1396 break;
1397 }
1398 case Primitive::kPrimFloat:
1399 case Primitive::kPrimDouble: {
1400 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001401 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001402 locations->SetOut(Location::RequiresRegister());
1403 break;
1404 }
1405 default:
1406 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1407 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001408}
1409
1410void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001411 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001412 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001413 Location left = locations->InAt(0);
1414 Location right = locations->InAt(1);
1415
1416 Label less, greater, done;
1417 Primitive::Type type = compare->InputAt(0)->GetType();
1418 switch (type) {
1419 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001420 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1421 if (right.IsConstant()) {
1422 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001423 if (IsInt<32>(value)) {
1424 if (value == 0) {
1425 __ testq(left_reg, left_reg);
1426 } else {
1427 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1428 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001429 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001430 // Value won't fit in an int.
1431 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001432 }
Mark Mendell40741f32015-04-20 22:10:34 -04001433 } else if (right.IsDoubleStackSlot()) {
1434 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001435 } else {
1436 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1437 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001438 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001439 }
1440 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001441 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1442 if (right.IsConstant()) {
1443 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1444 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1445 } else if (right.IsStackSlot()) {
1446 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1447 } else {
1448 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1449 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001450 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1451 break;
1452 }
1453 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001454 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1455 if (right.IsConstant()) {
1456 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1457 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1458 } else if (right.IsDoubleStackSlot()) {
1459 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1460 } else {
1461 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1462 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001463 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1464 break;
1465 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001466 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001467 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001468 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001469 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001470 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001471 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001472
Calin Juravle91debbc2014-11-26 19:01:09 +00001473 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001474 __ movl(out, Immediate(1));
1475 __ jmp(&done);
1476
1477 __ Bind(&less);
1478 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001479
1480 __ Bind(&done);
1481}
1482
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001483void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001484 LocationSummary* locations =
1485 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001486 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001487}
1488
1489void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001490 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001491 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001492}
1493
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001494void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1495 LocationSummary* locations =
1496 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1497 locations->SetOut(Location::ConstantLocation(constant));
1498}
1499
1500void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1501 // Will be generated at use site.
1502 UNUSED(constant);
1503}
1504
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001505void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001506 LocationSummary* locations =
1507 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001508 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001509}
1510
1511void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001512 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001513 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001514}
1515
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001516void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1517 LocationSummary* locations =
1518 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1519 locations->SetOut(Location::ConstantLocation(constant));
1520}
1521
1522void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1523 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001524 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001525}
1526
1527void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1528 LocationSummary* locations =
1529 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1530 locations->SetOut(Location::ConstantLocation(constant));
1531}
1532
1533void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1534 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001535 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001536}
1537
Calin Juravle27df7582015-04-17 19:12:31 +01001538void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1539 memory_barrier->SetLocations(nullptr);
1540}
1541
1542void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1543 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1544}
1545
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001546void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1547 ret->SetLocations(nullptr);
1548}
1549
1550void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001551 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001552 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001553}
1554
1555void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001556 LocationSummary* locations =
1557 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001558 switch (ret->InputAt(0)->GetType()) {
1559 case Primitive::kPrimBoolean:
1560 case Primitive::kPrimByte:
1561 case Primitive::kPrimChar:
1562 case Primitive::kPrimShort:
1563 case Primitive::kPrimInt:
1564 case Primitive::kPrimNot:
1565 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001566 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001567 break;
1568
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001569 case Primitive::kPrimFloat:
1570 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001571 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001572 break;
1573
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001574 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001575 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001576 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001577}
1578
1579void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1580 if (kIsDebugBuild) {
1581 switch (ret->InputAt(0)->GetType()) {
1582 case Primitive::kPrimBoolean:
1583 case Primitive::kPrimByte:
1584 case Primitive::kPrimChar:
1585 case Primitive::kPrimShort:
1586 case Primitive::kPrimInt:
1587 case Primitive::kPrimNot:
1588 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001589 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001590 break;
1591
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001592 case Primitive::kPrimFloat:
1593 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001594 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001595 XMM0);
1596 break;
1597
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001598 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001599 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001600 }
1601 }
1602 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001603}
1604
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001605Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1606 switch (type) {
1607 case Primitive::kPrimBoolean:
1608 case Primitive::kPrimByte:
1609 case Primitive::kPrimChar:
1610 case Primitive::kPrimShort:
1611 case Primitive::kPrimInt:
1612 case Primitive::kPrimNot:
1613 case Primitive::kPrimLong:
1614 return Location::RegisterLocation(RAX);
1615
1616 case Primitive::kPrimVoid:
1617 return Location::NoLocation();
1618
1619 case Primitive::kPrimDouble:
1620 case Primitive::kPrimFloat:
1621 return Location::FpuRegisterLocation(XMM0);
1622 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001623
1624 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001625}
1626
1627Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1628 return Location::RegisterLocation(kMethodRegisterArgument);
1629}
1630
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001631Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001632 switch (type) {
1633 case Primitive::kPrimBoolean:
1634 case Primitive::kPrimByte:
1635 case Primitive::kPrimChar:
1636 case Primitive::kPrimShort:
1637 case Primitive::kPrimInt:
1638 case Primitive::kPrimNot: {
1639 uint32_t index = gp_index_++;
1640 stack_index_++;
1641 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001642 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001643 } else {
1644 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1645 }
1646 }
1647
1648 case Primitive::kPrimLong: {
1649 uint32_t index = gp_index_;
1650 stack_index_ += 2;
1651 if (index < calling_convention.GetNumberOfRegisters()) {
1652 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001653 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001654 } else {
1655 gp_index_ += 2;
1656 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1657 }
1658 }
1659
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001660 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001661 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001662 stack_index_++;
1663 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001664 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001665 } else {
1666 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1667 }
1668 }
1669
1670 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001671 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001672 stack_index_ += 2;
1673 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001674 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001675 } else {
1676 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1677 }
1678 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001679
1680 case Primitive::kPrimVoid:
1681 LOG(FATAL) << "Unexpected parameter type " << type;
1682 break;
1683 }
1684 return Location();
1685}
1686
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001687void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001688 // When we do not run baseline, explicit clinit checks triggered by static
1689 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1690 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001691
Mark Mendellfb8d2792015-03-31 22:16:59 -04001692 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001693 if (intrinsic.TryDispatch(invoke)) {
1694 return;
1695 }
1696
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001697 HandleInvoke(invoke);
1698}
1699
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001700static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1701 if (invoke->GetLocations()->Intrinsified()) {
1702 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1703 intrinsic.Dispatch(invoke);
1704 return true;
1705 }
1706 return false;
1707}
1708
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001709void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001710 // When we do not run baseline, explicit clinit checks triggered by static
1711 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1712 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001713
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001714 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1715 return;
1716 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001717
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001718 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001719 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001720 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001721 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001722}
1723
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001724void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001725 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001726 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001727}
1728
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001729void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001730 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001731 if (intrinsic.TryDispatch(invoke)) {
1732 return;
1733 }
1734
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001735 HandleInvoke(invoke);
1736}
1737
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001738void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001739 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1740 return;
1741 }
1742
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001743 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001744
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001745 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001746 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001747}
1748
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001749void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1750 HandleInvoke(invoke);
1751 // Add the hidden argument.
1752 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1753}
1754
1755void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1756 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001757 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001758 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1759 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001760 LocationSummary* locations = invoke->GetLocations();
1761 Location receiver = locations->InAt(0);
1762 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1763
1764 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001765 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1766 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001767
1768 // temp = object->GetClass();
1769 if (receiver.IsStackSlot()) {
1770 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1771 __ movl(temp, Address(temp, class_offset));
1772 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001773 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001774 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001775 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001776 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001777 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001778 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001779 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001780 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001781 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001782
1783 DCHECK(!codegen_->IsLeafMethod());
1784 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1785}
1786
Roland Levillain88cb1752014-10-20 16:36:47 +01001787void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1788 LocationSummary* locations =
1789 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1790 switch (neg->GetResultType()) {
1791 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001792 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001793 locations->SetInAt(0, Location::RequiresRegister());
1794 locations->SetOut(Location::SameAsFirstInput());
1795 break;
1796
Roland Levillain88cb1752014-10-20 16:36:47 +01001797 case Primitive::kPrimFloat:
1798 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001799 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001800 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001801 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001802 break;
1803
1804 default:
1805 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1806 }
1807}
1808
1809void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1810 LocationSummary* locations = neg->GetLocations();
1811 Location out = locations->Out();
1812 Location in = locations->InAt(0);
1813 switch (neg->GetResultType()) {
1814 case Primitive::kPrimInt:
1815 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001816 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001817 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001818 break;
1819
1820 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001821 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001822 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001823 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001824 break;
1825
Roland Levillain5368c212014-11-27 15:03:41 +00001826 case Primitive::kPrimFloat: {
1827 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001828 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001829 // Implement float negation with an exclusive or with value
1830 // 0x80000000 (mask for bit 31, representing the sign of a
1831 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001832 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001833 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001834 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001835 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001836
Roland Levillain5368c212014-11-27 15:03:41 +00001837 case Primitive::kPrimDouble: {
1838 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001839 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001840 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001841 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001842 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001843 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001844 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001845 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001846 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001847
1848 default:
1849 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1850 }
1851}
1852
Roland Levillaindff1f282014-11-05 14:15:05 +00001853void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1854 LocationSummary* locations =
1855 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1856 Primitive::Type result_type = conversion->GetResultType();
1857 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001858 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001859
David Brazdilb2bd1c52015-03-25 11:17:37 +00001860 // The Java language does not allow treating boolean as an integral type but
1861 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001862
Roland Levillaindff1f282014-11-05 14:15:05 +00001863 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001864 case Primitive::kPrimByte:
1865 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001866 case Primitive::kPrimBoolean:
1867 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001868 case Primitive::kPrimShort:
1869 case Primitive::kPrimInt:
1870 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001871 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001872 locations->SetInAt(0, Location::Any());
1873 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1874 break;
1875
1876 default:
1877 LOG(FATAL) << "Unexpected type conversion from " << input_type
1878 << " to " << result_type;
1879 }
1880 break;
1881
Roland Levillain01a8d712014-11-14 16:27:39 +00001882 case Primitive::kPrimShort:
1883 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001884 case Primitive::kPrimBoolean:
1885 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001886 case Primitive::kPrimByte:
1887 case Primitive::kPrimInt:
1888 case Primitive::kPrimChar:
1889 // Processing a Dex `int-to-short' instruction.
1890 locations->SetInAt(0, Location::Any());
1891 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1892 break;
1893
1894 default:
1895 LOG(FATAL) << "Unexpected type conversion from " << input_type
1896 << " to " << result_type;
1897 }
1898 break;
1899
Roland Levillain946e1432014-11-11 17:35:19 +00001900 case Primitive::kPrimInt:
1901 switch (input_type) {
1902 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001903 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001904 locations->SetInAt(0, Location::Any());
1905 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1906 break;
1907
1908 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001909 // Processing a Dex `float-to-int' instruction.
1910 locations->SetInAt(0, Location::RequiresFpuRegister());
1911 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001912 break;
1913
Roland Levillain946e1432014-11-11 17:35:19 +00001914 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001915 // Processing a Dex `double-to-int' instruction.
1916 locations->SetInAt(0, Location::RequiresFpuRegister());
1917 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001918 break;
1919
1920 default:
1921 LOG(FATAL) << "Unexpected type conversion from " << input_type
1922 << " to " << result_type;
1923 }
1924 break;
1925
Roland Levillaindff1f282014-11-05 14:15:05 +00001926 case Primitive::kPrimLong:
1927 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001928 case Primitive::kPrimBoolean:
1929 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001930 case Primitive::kPrimByte:
1931 case Primitive::kPrimShort:
1932 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001933 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001934 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001935 // TODO: We would benefit from a (to-be-implemented)
1936 // Location::RegisterOrStackSlot requirement for this input.
1937 locations->SetInAt(0, Location::RequiresRegister());
1938 locations->SetOut(Location::RequiresRegister());
1939 break;
1940
1941 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001942 // Processing a Dex `float-to-long' instruction.
1943 locations->SetInAt(0, Location::RequiresFpuRegister());
1944 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00001945 break;
1946
Roland Levillaindff1f282014-11-05 14:15:05 +00001947 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001948 // Processing a Dex `double-to-long' instruction.
1949 locations->SetInAt(0, Location::RequiresFpuRegister());
1950 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001951 break;
1952
1953 default:
1954 LOG(FATAL) << "Unexpected type conversion from " << input_type
1955 << " to " << result_type;
1956 }
1957 break;
1958
Roland Levillain981e4542014-11-14 11:47:14 +00001959 case Primitive::kPrimChar:
1960 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001961 case Primitive::kPrimBoolean:
1962 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001963 case Primitive::kPrimByte:
1964 case Primitive::kPrimShort:
1965 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001966 // Processing a Dex `int-to-char' instruction.
1967 locations->SetInAt(0, Location::Any());
1968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1969 break;
1970
1971 default:
1972 LOG(FATAL) << "Unexpected type conversion from " << input_type
1973 << " to " << result_type;
1974 }
1975 break;
1976
Roland Levillaindff1f282014-11-05 14:15:05 +00001977 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001978 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001979 case Primitive::kPrimBoolean:
1980 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001981 case Primitive::kPrimByte:
1982 case Primitive::kPrimShort:
1983 case Primitive::kPrimInt:
1984 case Primitive::kPrimChar:
1985 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001986 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00001987 locations->SetOut(Location::RequiresFpuRegister());
1988 break;
1989
1990 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001991 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001992 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001993 locations->SetOut(Location::RequiresFpuRegister());
1994 break;
1995
Roland Levillaincff13742014-11-17 14:32:17 +00001996 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001997 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04001998 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00001999 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002000 break;
2001
2002 default:
2003 LOG(FATAL) << "Unexpected type conversion from " << input_type
2004 << " to " << result_type;
2005 };
2006 break;
2007
Roland Levillaindff1f282014-11-05 14:15:05 +00002008 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002009 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002010 case Primitive::kPrimBoolean:
2011 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002012 case Primitive::kPrimByte:
2013 case Primitive::kPrimShort:
2014 case Primitive::kPrimInt:
2015 case Primitive::kPrimChar:
2016 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002017 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002018 locations->SetOut(Location::RequiresFpuRegister());
2019 break;
2020
2021 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002022 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002023 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002024 locations->SetOut(Location::RequiresFpuRegister());
2025 break;
2026
Roland Levillaincff13742014-11-17 14:32:17 +00002027 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002028 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002029 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002030 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002031 break;
2032
2033 default:
2034 LOG(FATAL) << "Unexpected type conversion from " << input_type
2035 << " to " << result_type;
2036 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002037 break;
2038
2039 default:
2040 LOG(FATAL) << "Unexpected type conversion from " << input_type
2041 << " to " << result_type;
2042 }
2043}
2044
2045void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2046 LocationSummary* locations = conversion->GetLocations();
2047 Location out = locations->Out();
2048 Location in = locations->InAt(0);
2049 Primitive::Type result_type = conversion->GetResultType();
2050 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002051 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002052 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002053 case Primitive::kPrimByte:
2054 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002055 case Primitive::kPrimBoolean:
2056 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002057 case Primitive::kPrimShort:
2058 case Primitive::kPrimInt:
2059 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002060 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002061 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002062 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002063 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002064 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002065 Address(CpuRegister(RSP), in.GetStackIndex()));
2066 } else {
2067 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002068 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002069 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2070 }
2071 break;
2072
2073 default:
2074 LOG(FATAL) << "Unexpected type conversion from " << input_type
2075 << " to " << result_type;
2076 }
2077 break;
2078
Roland Levillain01a8d712014-11-14 16:27:39 +00002079 case Primitive::kPrimShort:
2080 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002081 case Primitive::kPrimBoolean:
2082 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002083 case Primitive::kPrimByte:
2084 case Primitive::kPrimInt:
2085 case Primitive::kPrimChar:
2086 // Processing a Dex `int-to-short' instruction.
2087 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002089 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002090 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002091 Address(CpuRegister(RSP), in.GetStackIndex()));
2092 } else {
2093 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002095 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2096 }
2097 break;
2098
2099 default:
2100 LOG(FATAL) << "Unexpected type conversion from " << input_type
2101 << " to " << result_type;
2102 }
2103 break;
2104
Roland Levillain946e1432014-11-11 17:35:19 +00002105 case Primitive::kPrimInt:
2106 switch (input_type) {
2107 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002108 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002109 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002110 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002111 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002112 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002113 Address(CpuRegister(RSP), in.GetStackIndex()));
2114 } else {
2115 DCHECK(in.IsConstant());
2116 DCHECK(in.GetConstant()->IsLongConstant());
2117 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002119 }
2120 break;
2121
Roland Levillain3f8f9362014-12-02 17:45:01 +00002122 case Primitive::kPrimFloat: {
2123 // Processing a Dex `float-to-int' instruction.
2124 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2125 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain3f8f9362014-12-02 17:45:01 +00002126 Label done, nan;
2127
2128 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002129 // if input >= (float)INT_MAX goto done
2130 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002131 __ j(kAboveEqual, &done);
2132 // if input == NaN goto nan
2133 __ j(kUnordered, &nan);
2134 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002135 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002136 __ jmp(&done);
2137 __ Bind(&nan);
2138 // output = 0
2139 __ xorl(output, output);
2140 __ Bind(&done);
2141 break;
2142 }
2143
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002144 case Primitive::kPrimDouble: {
2145 // Processing a Dex `double-to-int' instruction.
2146 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2147 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002148 Label done, nan;
2149
2150 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002151 // if input >= (double)INT_MAX goto done
2152 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002153 __ j(kAboveEqual, &done);
2154 // if input == NaN goto nan
2155 __ j(kUnordered, &nan);
2156 // output = double-to-int-truncate(input)
2157 __ cvttsd2si(output, input);
2158 __ jmp(&done);
2159 __ Bind(&nan);
2160 // output = 0
2161 __ xorl(output, output);
2162 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002163 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002164 }
Roland Levillain946e1432014-11-11 17:35:19 +00002165
2166 default:
2167 LOG(FATAL) << "Unexpected type conversion from " << input_type
2168 << " to " << result_type;
2169 }
2170 break;
2171
Roland Levillaindff1f282014-11-05 14:15:05 +00002172 case Primitive::kPrimLong:
2173 switch (input_type) {
2174 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002175 case Primitive::kPrimBoolean:
2176 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002177 case Primitive::kPrimByte:
2178 case Primitive::kPrimShort:
2179 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002180 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002181 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002182 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002183 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002184 break;
2185
Roland Levillain624279f2014-12-04 11:54:28 +00002186 case Primitive::kPrimFloat: {
2187 // Processing a Dex `float-to-long' instruction.
2188 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2189 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain624279f2014-12-04 11:54:28 +00002190 Label done, nan;
2191
Mark Mendell92e83bf2015-05-07 11:25:03 -04002192 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002193 // if input >= (float)LONG_MAX goto done
2194 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002195 __ j(kAboveEqual, &done);
2196 // if input == NaN goto nan
2197 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002198 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002199 __ cvttss2si(output, input, true);
2200 __ jmp(&done);
2201 __ Bind(&nan);
2202 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002203 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002204 __ Bind(&done);
2205 break;
2206 }
2207
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002208 case Primitive::kPrimDouble: {
2209 // Processing a Dex `double-to-long' instruction.
2210 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2211 CpuRegister output = out.AsRegister<CpuRegister>();
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002212 Label done, nan;
2213
Mark Mendell92e83bf2015-05-07 11:25:03 -04002214 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002215 // if input >= (double)LONG_MAX goto done
2216 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002217 __ j(kAboveEqual, &done);
2218 // if input == NaN goto nan
2219 __ j(kUnordered, &nan);
2220 // output = double-to-long-truncate(input)
2221 __ cvttsd2si(output, input, true);
2222 __ jmp(&done);
2223 __ Bind(&nan);
2224 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002225 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002226 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002227 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002228 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002229
2230 default:
2231 LOG(FATAL) << "Unexpected type conversion from " << input_type
2232 << " to " << result_type;
2233 }
2234 break;
2235
Roland Levillain981e4542014-11-14 11:47:14 +00002236 case Primitive::kPrimChar:
2237 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002238 case Primitive::kPrimBoolean:
2239 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002240 case Primitive::kPrimByte:
2241 case Primitive::kPrimShort:
2242 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002243 // Processing a Dex `int-to-char' instruction.
2244 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002245 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002246 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002247 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002248 Address(CpuRegister(RSP), in.GetStackIndex()));
2249 } else {
2250 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002251 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002252 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2253 }
2254 break;
2255
2256 default:
2257 LOG(FATAL) << "Unexpected type conversion from " << input_type
2258 << " to " << result_type;
2259 }
2260 break;
2261
Roland Levillaindff1f282014-11-05 14:15:05 +00002262 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002263 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002264 case Primitive::kPrimBoolean:
2265 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002266 case Primitive::kPrimByte:
2267 case Primitive::kPrimShort:
2268 case Primitive::kPrimInt:
2269 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002270 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002271 if (in.IsRegister()) {
2272 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2273 } else if (in.IsConstant()) {
2274 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2275 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2276 if (v == 0) {
2277 __ xorps(dest, dest);
2278 } else {
2279 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2280 }
2281 } else {
2282 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2283 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2284 }
Roland Levillaincff13742014-11-17 14:32:17 +00002285 break;
2286
2287 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002288 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002289 if (in.IsRegister()) {
2290 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2291 } else if (in.IsConstant()) {
2292 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2293 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2294 if (v == 0) {
2295 __ xorps(dest, dest);
2296 } else {
2297 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2298 }
2299 } else {
2300 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2301 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2302 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002303 break;
2304
Roland Levillaincff13742014-11-17 14:32:17 +00002305 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002306 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002307 if (in.IsFpuRegister()) {
2308 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2309 } else if (in.IsConstant()) {
2310 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2311 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2312 if (bit_cast<int64_t, double>(v) == 0) {
2313 __ xorps(dest, dest);
2314 } else {
2315 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2316 }
2317 } else {
2318 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2319 Address(CpuRegister(RSP), in.GetStackIndex()));
2320 }
Roland Levillaincff13742014-11-17 14:32:17 +00002321 break;
2322
2323 default:
2324 LOG(FATAL) << "Unexpected type conversion from " << input_type
2325 << " to " << result_type;
2326 };
2327 break;
2328
Roland Levillaindff1f282014-11-05 14:15:05 +00002329 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002330 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002331 case Primitive::kPrimBoolean:
2332 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002333 case Primitive::kPrimByte:
2334 case Primitive::kPrimShort:
2335 case Primitive::kPrimInt:
2336 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002337 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002338 if (in.IsRegister()) {
2339 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2340 } else if (in.IsConstant()) {
2341 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2342 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2343 if (v == 0) {
2344 __ xorpd(dest, dest);
2345 } else {
2346 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2347 }
2348 } else {
2349 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2350 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2351 }
Roland Levillaincff13742014-11-17 14:32:17 +00002352 break;
2353
2354 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002355 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002356 if (in.IsRegister()) {
2357 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2358 } else if (in.IsConstant()) {
2359 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2360 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2361 if (v == 0) {
2362 __ xorpd(dest, dest);
2363 } else {
2364 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2365 }
2366 } else {
2367 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2368 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2369 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002370 break;
2371
Roland Levillaincff13742014-11-17 14:32:17 +00002372 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002373 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002374 if (in.IsFpuRegister()) {
2375 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2376 } else if (in.IsConstant()) {
2377 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2378 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2379 if (bit_cast<int32_t, float>(v) == 0) {
2380 __ xorpd(dest, dest);
2381 } else {
2382 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2383 }
2384 } else {
2385 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2386 Address(CpuRegister(RSP), in.GetStackIndex()));
2387 }
Roland Levillaincff13742014-11-17 14:32:17 +00002388 break;
2389
2390 default:
2391 LOG(FATAL) << "Unexpected type conversion from " << input_type
2392 << " to " << result_type;
2393 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002394 break;
2395
2396 default:
2397 LOG(FATAL) << "Unexpected type conversion from " << input_type
2398 << " to " << result_type;
2399 }
2400}
2401
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002402void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002403 LocationSummary* locations =
2404 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002405 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002406 case Primitive::kPrimInt: {
2407 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002408 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002410 break;
2411 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002412
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002413 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002414 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002415 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002416 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002417 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002418 break;
2419 }
2420
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002421 case Primitive::kPrimDouble:
2422 case Primitive::kPrimFloat: {
2423 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002424 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002425 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002426 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002427 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002428
2429 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002430 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002431 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002432}
2433
2434void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2435 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002436 Location first = locations->InAt(0);
2437 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002438 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002439
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002440 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002441 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002442 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002443 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2444 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002445 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2446 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002447 } else {
2448 __ leal(out.AsRegister<CpuRegister>(), Address(
2449 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2450 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002451 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002452 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2453 __ addl(out.AsRegister<CpuRegister>(),
2454 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2455 } else {
2456 __ leal(out.AsRegister<CpuRegister>(), Address(
2457 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2458 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002459 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002460 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002461 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002462 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002463 break;
2464 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002465
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002466 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002467 if (second.IsRegister()) {
2468 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2469 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002470 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2471 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002472 } else {
2473 __ leaq(out.AsRegister<CpuRegister>(), Address(
2474 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2475 }
2476 } else {
2477 DCHECK(second.IsConstant());
2478 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2479 int32_t int32_value = Low32Bits(value);
2480 DCHECK_EQ(int32_value, value);
2481 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2482 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2483 } else {
2484 __ leaq(out.AsRegister<CpuRegister>(), Address(
2485 first.AsRegister<CpuRegister>(), int32_value));
2486 }
2487 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002488 break;
2489 }
2490
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002491 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002492 if (second.IsFpuRegister()) {
2493 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2494 } else if (second.IsConstant()) {
2495 __ addss(first.AsFpuRegister<XmmRegister>(),
2496 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2497 } else {
2498 DCHECK(second.IsStackSlot());
2499 __ addss(first.AsFpuRegister<XmmRegister>(),
2500 Address(CpuRegister(RSP), second.GetStackIndex()));
2501 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002502 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002503 }
2504
2505 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002506 if (second.IsFpuRegister()) {
2507 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2508 } else if (second.IsConstant()) {
2509 __ addsd(first.AsFpuRegister<XmmRegister>(),
2510 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2511 } else {
2512 DCHECK(second.IsDoubleStackSlot());
2513 __ addsd(first.AsFpuRegister<XmmRegister>(),
2514 Address(CpuRegister(RSP), second.GetStackIndex()));
2515 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002516 break;
2517 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002518
2519 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002520 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002521 }
2522}
2523
2524void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002525 LocationSummary* locations =
2526 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002527 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002528 case Primitive::kPrimInt: {
2529 locations->SetInAt(0, Location::RequiresRegister());
2530 locations->SetInAt(1, Location::Any());
2531 locations->SetOut(Location::SameAsFirstInput());
2532 break;
2533 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002534 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002535 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002536 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002537 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002538 break;
2539 }
Calin Juravle11351682014-10-23 15:38:15 +01002540 case Primitive::kPrimFloat:
2541 case Primitive::kPrimDouble: {
2542 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002543 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002544 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002545 break;
Calin Juravle11351682014-10-23 15:38:15 +01002546 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002547 default:
Calin Juravle11351682014-10-23 15:38:15 +01002548 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002549 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002550}
2551
2552void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2553 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002554 Location first = locations->InAt(0);
2555 Location second = locations->InAt(1);
2556 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002557 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002558 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002559 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002560 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002561 } else if (second.IsConstant()) {
2562 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002563 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002564 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002565 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002566 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002567 break;
2568 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002569 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002570 if (second.IsConstant()) {
2571 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2572 DCHECK(IsInt<32>(value));
2573 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2574 } else {
2575 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2576 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002577 break;
2578 }
2579
Calin Juravle11351682014-10-23 15:38:15 +01002580 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002581 if (second.IsFpuRegister()) {
2582 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2583 } else if (second.IsConstant()) {
2584 __ subss(first.AsFpuRegister<XmmRegister>(),
2585 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2586 } else {
2587 DCHECK(second.IsStackSlot());
2588 __ subss(first.AsFpuRegister<XmmRegister>(),
2589 Address(CpuRegister(RSP), second.GetStackIndex()));
2590 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002591 break;
Calin Juravle11351682014-10-23 15:38:15 +01002592 }
2593
2594 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002595 if (second.IsFpuRegister()) {
2596 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2597 } else if (second.IsConstant()) {
2598 __ subsd(first.AsFpuRegister<XmmRegister>(),
2599 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2600 } else {
2601 DCHECK(second.IsDoubleStackSlot());
2602 __ subsd(first.AsFpuRegister<XmmRegister>(),
2603 Address(CpuRegister(RSP), second.GetStackIndex()));
2604 }
Calin Juravle11351682014-10-23 15:38:15 +01002605 break;
2606 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002607
2608 default:
Calin Juravle11351682014-10-23 15:38:15 +01002609 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002610 }
2611}
2612
Calin Juravle34bacdf2014-10-07 20:23:36 +01002613void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2614 LocationSummary* locations =
2615 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2616 switch (mul->GetResultType()) {
2617 case Primitive::kPrimInt: {
2618 locations->SetInAt(0, Location::RequiresRegister());
2619 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002620 if (mul->InputAt(1)->IsIntConstant()) {
2621 // Can use 3 operand multiply.
2622 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2623 } else {
2624 locations->SetOut(Location::SameAsFirstInput());
2625 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002626 break;
2627 }
2628 case Primitive::kPrimLong: {
2629 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002630 locations->SetInAt(1, Location::Any());
2631 if (mul->InputAt(1)->IsLongConstant() &&
2632 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002633 // Can use 3 operand multiply.
2634 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2635 } else {
2636 locations->SetOut(Location::SameAsFirstInput());
2637 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002638 break;
2639 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002640 case Primitive::kPrimFloat:
2641 case Primitive::kPrimDouble: {
2642 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002643 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002644 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002645 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002646 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002647
2648 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002649 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002650 }
2651}
2652
2653void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2654 LocationSummary* locations = mul->GetLocations();
2655 Location first = locations->InAt(0);
2656 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002657 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002658 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002659 case Primitive::kPrimInt:
2660 // The constant may have ended up in a register, so test explicitly to avoid
2661 // problems where the output may not be the same as the first operand.
2662 if (mul->InputAt(1)->IsIntConstant()) {
2663 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2664 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2665 } else if (second.IsRegister()) {
2666 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002667 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002668 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002669 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002670 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002671 __ imull(first.AsRegister<CpuRegister>(),
2672 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002673 }
2674 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002675 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002676 // The constant may have ended up in a register, so test explicitly to avoid
2677 // problems where the output may not be the same as the first operand.
2678 if (mul->InputAt(1)->IsLongConstant()) {
2679 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2680 if (IsInt<32>(value)) {
2681 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2682 Immediate(static_cast<int32_t>(value)));
2683 } else {
2684 // Have to use the constant area.
2685 DCHECK(first.Equals(out));
2686 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2687 }
2688 } else if (second.IsRegister()) {
2689 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002690 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002691 } else {
2692 DCHECK(second.IsDoubleStackSlot());
2693 DCHECK(first.Equals(out));
2694 __ imulq(first.AsRegister<CpuRegister>(),
2695 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002696 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002697 break;
2698 }
2699
Calin Juravleb5bfa962014-10-21 18:02:24 +01002700 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002701 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002702 if (second.IsFpuRegister()) {
2703 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2704 } else if (second.IsConstant()) {
2705 __ mulss(first.AsFpuRegister<XmmRegister>(),
2706 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2707 } else {
2708 DCHECK(second.IsStackSlot());
2709 __ mulss(first.AsFpuRegister<XmmRegister>(),
2710 Address(CpuRegister(RSP), second.GetStackIndex()));
2711 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002712 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002713 }
2714
2715 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002716 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002717 if (second.IsFpuRegister()) {
2718 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2719 } else if (second.IsConstant()) {
2720 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2721 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2722 } else {
2723 DCHECK(second.IsDoubleStackSlot());
2724 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2725 Address(CpuRegister(RSP), second.GetStackIndex()));
2726 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002727 break;
2728 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002729
2730 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002731 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002732 }
2733}
2734
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002735void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2736 uint32_t stack_adjustment, bool is_float) {
2737 if (source.IsStackSlot()) {
2738 DCHECK(is_float);
2739 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2740 } else if (source.IsDoubleStackSlot()) {
2741 DCHECK(!is_float);
2742 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2743 } else {
2744 // Write the value to the temporary location on the stack and load to FP stack.
2745 if (is_float) {
2746 Location stack_temp = Location::StackSlot(temp_offset);
2747 codegen_->Move(stack_temp, source);
2748 __ flds(Address(CpuRegister(RSP), temp_offset));
2749 } else {
2750 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2751 codegen_->Move(stack_temp, source);
2752 __ fldl(Address(CpuRegister(RSP), temp_offset));
2753 }
2754 }
2755}
2756
2757void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2758 Primitive::Type type = rem->GetResultType();
2759 bool is_float = type == Primitive::kPrimFloat;
2760 size_t elem_size = Primitive::ComponentSize(type);
2761 LocationSummary* locations = rem->GetLocations();
2762 Location first = locations->InAt(0);
2763 Location second = locations->InAt(1);
2764 Location out = locations->Out();
2765
2766 // Create stack space for 2 elements.
2767 // TODO: enhance register allocator to ask for stack temporaries.
2768 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2769
2770 // Load the values to the FP stack in reverse order, using temporaries if needed.
2771 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2772 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2773
2774 // Loop doing FPREM until we stabilize.
2775 Label retry;
2776 __ Bind(&retry);
2777 __ fprem();
2778
2779 // Move FP status to AX.
2780 __ fstsw();
2781
2782 // And see if the argument reduction is complete. This is signaled by the
2783 // C2 FPU flag bit set to 0.
2784 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2785 __ j(kNotEqual, &retry);
2786
2787 // We have settled on the final value. Retrieve it into an XMM register.
2788 // Store FP top of stack to real stack.
2789 if (is_float) {
2790 __ fsts(Address(CpuRegister(RSP), 0));
2791 } else {
2792 __ fstl(Address(CpuRegister(RSP), 0));
2793 }
2794
2795 // Pop the 2 items from the FP stack.
2796 __ fucompp();
2797
2798 // Load the value from the stack into an XMM register.
2799 DCHECK(out.IsFpuRegister()) << out;
2800 if (is_float) {
2801 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2802 } else {
2803 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2804 }
2805
2806 // And remove the temporary stack space we allocated.
2807 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2808}
2809
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002810void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2811 DCHECK(instruction->IsDiv() || instruction->IsRem());
2812
2813 LocationSummary* locations = instruction->GetLocations();
2814 Location second = locations->InAt(1);
2815 DCHECK(second.IsConstant());
2816
2817 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2818 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002819 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002820
2821 DCHECK(imm == 1 || imm == -1);
2822
2823 switch (instruction->GetResultType()) {
2824 case Primitive::kPrimInt: {
2825 if (instruction->IsRem()) {
2826 __ xorl(output_register, output_register);
2827 } else {
2828 __ movl(output_register, input_register);
2829 if (imm == -1) {
2830 __ negl(output_register);
2831 }
2832 }
2833 break;
2834 }
2835
2836 case Primitive::kPrimLong: {
2837 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002838 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002839 } else {
2840 __ movq(output_register, input_register);
2841 if (imm == -1) {
2842 __ negq(output_register);
2843 }
2844 }
2845 break;
2846 }
2847
2848 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002849 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002850 }
2851}
2852
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002853void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002854 LocationSummary* locations = instruction->GetLocations();
2855 Location second = locations->InAt(1);
2856
2857 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2858 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2859
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002860 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002861
2862 DCHECK(IsPowerOfTwo(std::abs(imm)));
2863
2864 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2865
2866 if (instruction->GetResultType() == Primitive::kPrimInt) {
2867 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2868 __ testl(numerator, numerator);
2869 __ cmov(kGreaterEqual, tmp, numerator);
2870 int shift = CTZ(imm);
2871 __ sarl(tmp, Immediate(shift));
2872
2873 if (imm < 0) {
2874 __ negl(tmp);
2875 }
2876
2877 __ movl(output_register, tmp);
2878 } else {
2879 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2880 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2881
Mark Mendell92e83bf2015-05-07 11:25:03 -04002882 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002883 __ addq(rdx, numerator);
2884 __ testq(numerator, numerator);
2885 __ cmov(kGreaterEqual, rdx, numerator);
2886 int shift = CTZ(imm);
2887 __ sarq(rdx, Immediate(shift));
2888
2889 if (imm < 0) {
2890 __ negq(rdx);
2891 }
2892
2893 __ movq(output_register, rdx);
2894 }
2895}
2896
2897void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2898 DCHECK(instruction->IsDiv() || instruction->IsRem());
2899
2900 LocationSummary* locations = instruction->GetLocations();
2901 Location second = locations->InAt(1);
2902
2903 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2904 : locations->GetTemp(0).AsRegister<CpuRegister>();
2905 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2906 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2907 : locations->Out().AsRegister<CpuRegister>();
2908 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2909
2910 DCHECK_EQ(RAX, eax.AsRegister());
2911 DCHECK_EQ(RDX, edx.AsRegister());
2912 if (instruction->IsDiv()) {
2913 DCHECK_EQ(RAX, out.AsRegister());
2914 } else {
2915 DCHECK_EQ(RDX, out.AsRegister());
2916 }
2917
2918 int64_t magic;
2919 int shift;
2920
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002921 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002922 if (instruction->GetResultType() == Primitive::kPrimInt) {
2923 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2924
2925 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2926
2927 __ movl(numerator, eax);
2928
2929 Label no_div;
2930 Label end;
2931 __ testl(eax, eax);
2932 __ j(kNotEqual, &no_div);
2933
2934 __ xorl(out, out);
2935 __ jmp(&end);
2936
2937 __ Bind(&no_div);
2938
2939 __ movl(eax, Immediate(magic));
2940 __ imull(numerator);
2941
2942 if (imm > 0 && magic < 0) {
2943 __ addl(edx, numerator);
2944 } else if (imm < 0 && magic > 0) {
2945 __ subl(edx, numerator);
2946 }
2947
2948 if (shift != 0) {
2949 __ sarl(edx, Immediate(shift));
2950 }
2951
2952 __ movl(eax, edx);
2953 __ shrl(edx, Immediate(31));
2954 __ addl(edx, eax);
2955
2956 if (instruction->IsRem()) {
2957 __ movl(eax, numerator);
2958 __ imull(edx, Immediate(imm));
2959 __ subl(eax, edx);
2960 __ movl(edx, eax);
2961 } else {
2962 __ movl(eax, edx);
2963 }
2964 __ Bind(&end);
2965 } else {
2966 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
2967
2968 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2969
2970 CpuRegister rax = eax;
2971 CpuRegister rdx = edx;
2972
2973 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
2974
2975 // Save the numerator.
2976 __ movq(numerator, rax);
2977
2978 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04002979 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002980
2981 // RDX:RAX = magic * numerator
2982 __ imulq(numerator);
2983
2984 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002985 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002986 __ addq(rdx, numerator);
2987 } else if (imm < 0 && magic > 0) {
2988 // RDX -= numerator
2989 __ subq(rdx, numerator);
2990 }
2991
2992 // Shift if needed.
2993 if (shift != 0) {
2994 __ sarq(rdx, Immediate(shift));
2995 }
2996
2997 // RDX += 1 if RDX < 0
2998 __ movq(rax, rdx);
2999 __ shrq(rdx, Immediate(63));
3000 __ addq(rdx, rax);
3001
3002 if (instruction->IsRem()) {
3003 __ movq(rax, numerator);
3004
3005 if (IsInt<32>(imm)) {
3006 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3007 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003008 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003009 }
3010
3011 __ subq(rax, rdx);
3012 __ movq(rdx, rax);
3013 } else {
3014 __ movq(rax, rdx);
3015 }
3016 }
3017}
3018
Calin Juravlebacfec32014-11-14 15:54:36 +00003019void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3020 DCHECK(instruction->IsDiv() || instruction->IsRem());
3021 Primitive::Type type = instruction->GetResultType();
3022 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3023
3024 bool is_div = instruction->IsDiv();
3025 LocationSummary* locations = instruction->GetLocations();
3026
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003027 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3028 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003029
Roland Levillain271ab9c2014-11-27 15:23:57 +00003030 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003031 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003032
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003033 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003034 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003035
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003036 if (imm == 0) {
3037 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3038 } else if (imm == 1 || imm == -1) {
3039 DivRemOneOrMinusOne(instruction);
3040 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003041 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003042 } else {
3043 DCHECK(imm <= -2 || imm >= 2);
3044 GenerateDivRemWithAnyConstant(instruction);
3045 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003046 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003047 SlowPathCodeX86_64* slow_path =
3048 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3049 out.AsRegister(), type, is_div);
3050 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003051
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003052 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3053 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3054 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3055 // so it's safe to just use negl instead of more complex comparisons.
3056 if (type == Primitive::kPrimInt) {
3057 __ cmpl(second_reg, Immediate(-1));
3058 __ j(kEqual, slow_path->GetEntryLabel());
3059 // edx:eax <- sign-extended of eax
3060 __ cdq();
3061 // eax = quotient, edx = remainder
3062 __ idivl(second_reg);
3063 } else {
3064 __ cmpq(second_reg, Immediate(-1));
3065 __ j(kEqual, slow_path->GetEntryLabel());
3066 // rdx:rax <- sign-extended of rax
3067 __ cqo();
3068 // rax = quotient, rdx = remainder
3069 __ idivq(second_reg);
3070 }
3071 __ Bind(slow_path->GetExitLabel());
3072 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003073}
3074
Calin Juravle7c4954d2014-10-28 16:57:40 +00003075void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3076 LocationSummary* locations =
3077 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3078 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003079 case Primitive::kPrimInt:
3080 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003081 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003082 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003083 locations->SetOut(Location::SameAsFirstInput());
3084 // Intel uses edx:eax as the dividend.
3085 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003086 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3087 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3088 // output and request another temp.
3089 if (div->InputAt(1)->IsConstant()) {
3090 locations->AddTemp(Location::RequiresRegister());
3091 }
Calin Juravled0d48522014-11-04 16:40:20 +00003092 break;
3093 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003094
Calin Juravle7c4954d2014-10-28 16:57:40 +00003095 case Primitive::kPrimFloat:
3096 case Primitive::kPrimDouble: {
3097 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003098 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003099 locations->SetOut(Location::SameAsFirstInput());
3100 break;
3101 }
3102
3103 default:
3104 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3105 }
3106}
3107
3108void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3109 LocationSummary* locations = div->GetLocations();
3110 Location first = locations->InAt(0);
3111 Location second = locations->InAt(1);
3112 DCHECK(first.Equals(locations->Out()));
3113
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003114 Primitive::Type type = div->GetResultType();
3115 switch (type) {
3116 case Primitive::kPrimInt:
3117 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003118 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003119 break;
3120 }
3121
Calin Juravle7c4954d2014-10-28 16:57:40 +00003122 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003123 if (second.IsFpuRegister()) {
3124 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3125 } else if (second.IsConstant()) {
3126 __ divss(first.AsFpuRegister<XmmRegister>(),
3127 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3128 } else {
3129 DCHECK(second.IsStackSlot());
3130 __ divss(first.AsFpuRegister<XmmRegister>(),
3131 Address(CpuRegister(RSP), second.GetStackIndex()));
3132 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003133 break;
3134 }
3135
3136 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003137 if (second.IsFpuRegister()) {
3138 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3139 } else if (second.IsConstant()) {
3140 __ divsd(first.AsFpuRegister<XmmRegister>(),
3141 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3142 } else {
3143 DCHECK(second.IsDoubleStackSlot());
3144 __ divsd(first.AsFpuRegister<XmmRegister>(),
3145 Address(CpuRegister(RSP), second.GetStackIndex()));
3146 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003147 break;
3148 }
3149
3150 default:
3151 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3152 }
3153}
3154
Calin Juravlebacfec32014-11-14 15:54:36 +00003155void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003156 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003157 LocationSummary* locations =
3158 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003159
3160 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003161 case Primitive::kPrimInt:
3162 case Primitive::kPrimLong: {
3163 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003164 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003165 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3166 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003167 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3168 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3169 // output and request another temp.
3170 if (rem->InputAt(1)->IsConstant()) {
3171 locations->AddTemp(Location::RequiresRegister());
3172 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003173 break;
3174 }
3175
3176 case Primitive::kPrimFloat:
3177 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003178 locations->SetInAt(0, Location::Any());
3179 locations->SetInAt(1, Location::Any());
3180 locations->SetOut(Location::RequiresFpuRegister());
3181 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003182 break;
3183 }
3184
3185 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003186 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003187 }
3188}
3189
3190void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3191 Primitive::Type type = rem->GetResultType();
3192 switch (type) {
3193 case Primitive::kPrimInt:
3194 case Primitive::kPrimLong: {
3195 GenerateDivRemIntegral(rem);
3196 break;
3197 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003198 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003199 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003200 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003201 break;
3202 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003203 default:
3204 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3205 }
3206}
3207
Calin Juravled0d48522014-11-04 16:40:20 +00003208void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003209 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3210 ? LocationSummary::kCallOnSlowPath
3211 : LocationSummary::kNoCall;
3212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003213 locations->SetInAt(0, Location::Any());
3214 if (instruction->HasUses()) {
3215 locations->SetOut(Location::SameAsFirstInput());
3216 }
3217}
3218
3219void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3220 SlowPathCodeX86_64* slow_path =
3221 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3222 codegen_->AddSlowPath(slow_path);
3223
3224 LocationSummary* locations = instruction->GetLocations();
3225 Location value = locations->InAt(0);
3226
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003227 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003228 case Primitive::kPrimByte:
3229 case Primitive::kPrimChar:
3230 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003231 case Primitive::kPrimInt: {
3232 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003233 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003234 __ j(kEqual, slow_path->GetEntryLabel());
3235 } else if (value.IsStackSlot()) {
3236 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3237 __ j(kEqual, slow_path->GetEntryLabel());
3238 } else {
3239 DCHECK(value.IsConstant()) << value;
3240 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3241 __ jmp(slow_path->GetEntryLabel());
3242 }
3243 }
3244 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003245 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003246 case Primitive::kPrimLong: {
3247 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003248 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003249 __ j(kEqual, slow_path->GetEntryLabel());
3250 } else if (value.IsDoubleStackSlot()) {
3251 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3252 __ j(kEqual, slow_path->GetEntryLabel());
3253 } else {
3254 DCHECK(value.IsConstant()) << value;
3255 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3256 __ jmp(slow_path->GetEntryLabel());
3257 }
3258 }
3259 break;
3260 }
3261 default:
3262 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003263 }
Calin Juravled0d48522014-11-04 16:40:20 +00003264}
3265
Calin Juravle9aec02f2014-11-18 23:06:35 +00003266void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3267 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3268
3269 LocationSummary* locations =
3270 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3271
3272 switch (op->GetResultType()) {
3273 case Primitive::kPrimInt:
3274 case Primitive::kPrimLong: {
3275 locations->SetInAt(0, Location::RequiresRegister());
3276 // The shift count needs to be in CL.
3277 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3278 locations->SetOut(Location::SameAsFirstInput());
3279 break;
3280 }
3281 default:
3282 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3283 }
3284}
3285
3286void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3287 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3288
3289 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003290 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003291 Location second = locations->InAt(1);
3292
3293 switch (op->GetResultType()) {
3294 case Primitive::kPrimInt: {
3295 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003296 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003297 if (op->IsShl()) {
3298 __ shll(first_reg, second_reg);
3299 } else if (op->IsShr()) {
3300 __ sarl(first_reg, second_reg);
3301 } else {
3302 __ shrl(first_reg, second_reg);
3303 }
3304 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003305 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003306 if (op->IsShl()) {
3307 __ shll(first_reg, imm);
3308 } else if (op->IsShr()) {
3309 __ sarl(first_reg, imm);
3310 } else {
3311 __ shrl(first_reg, imm);
3312 }
3313 }
3314 break;
3315 }
3316 case Primitive::kPrimLong: {
3317 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003318 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003319 if (op->IsShl()) {
3320 __ shlq(first_reg, second_reg);
3321 } else if (op->IsShr()) {
3322 __ sarq(first_reg, second_reg);
3323 } else {
3324 __ shrq(first_reg, second_reg);
3325 }
3326 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003327 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003328 if (op->IsShl()) {
3329 __ shlq(first_reg, imm);
3330 } else if (op->IsShr()) {
3331 __ sarq(first_reg, imm);
3332 } else {
3333 __ shrq(first_reg, imm);
3334 }
3335 }
3336 break;
3337 }
3338 default:
3339 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3340 }
3341}
3342
3343void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3344 HandleShift(shl);
3345}
3346
3347void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3348 HandleShift(shl);
3349}
3350
3351void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3352 HandleShift(shr);
3353}
3354
3355void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3356 HandleShift(shr);
3357}
3358
3359void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3360 HandleShift(ushr);
3361}
3362
3363void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3364 HandleShift(ushr);
3365}
3366
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003367void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003368 LocationSummary* locations =
3369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003370 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003371 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003372 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003373 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003374}
3375
3376void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3377 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003378 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3379 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003380 // Note: if heap poisoning is enabled, the entry point takes cares
3381 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003382
3383 codegen_->InvokeRuntime(
3384 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3385 instruction,
3386 instruction->GetDexPc(),
3387 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003388
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003389 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003390}
3391
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003392void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3393 LocationSummary* locations =
3394 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3395 InvokeRuntimeCallingConvention calling_convention;
3396 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003397 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003398 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003399 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003400}
3401
3402void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3403 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003404 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3405 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003406
Roland Levillain4d027112015-07-01 15:41:14 +01003407 // Note: if heap poisoning is enabled, the entry point takes cares
3408 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003409 codegen_->InvokeRuntime(
3410 Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true),
3411 instruction,
3412 instruction->GetDexPc(),
3413 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003414
3415 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003416}
3417
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003418void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003419 LocationSummary* locations =
3420 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003421 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3422 if (location.IsStackSlot()) {
3423 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3424 } else if (location.IsDoubleStackSlot()) {
3425 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3426 }
3427 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003428}
3429
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003430void InstructionCodeGeneratorX86_64::VisitParameterValue(
3431 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003432 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003433}
3434
3435void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3436 LocationSummary* locations =
3437 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3438 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3439}
3440
3441void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3442 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3443 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003444}
3445
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003446void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003447 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003448 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003449 locations->SetInAt(0, Location::RequiresRegister());
3450 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003451}
3452
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003453void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3454 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003455 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3456 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003457 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003458 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003459 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003460 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003461 break;
3462
3463 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003464 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003465 break;
3466
3467 default:
3468 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3469 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003470}
3471
David Brazdil66d126e2015-04-03 16:02:44 +01003472void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3473 LocationSummary* locations =
3474 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3475 locations->SetInAt(0, Location::RequiresRegister());
3476 locations->SetOut(Location::SameAsFirstInput());
3477}
3478
3479void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003480 LocationSummary* locations = bool_not->GetLocations();
3481 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3482 locations->Out().AsRegister<CpuRegister>().AsRegister());
3483 Location out = locations->Out();
3484 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3485}
3486
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003487void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003488 LocationSummary* locations =
3489 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003490 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3491 locations->SetInAt(i, Location::Any());
3492 }
3493 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003494}
3495
3496void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003497 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003498 LOG(FATAL) << "Unimplemented";
3499}
3500
Calin Juravle52c48962014-12-16 17:02:57 +00003501void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3502 /*
3503 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3504 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3505 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3506 */
3507 switch (kind) {
3508 case MemBarrierKind::kAnyAny: {
3509 __ mfence();
3510 break;
3511 }
3512 case MemBarrierKind::kAnyStore:
3513 case MemBarrierKind::kLoadAny:
3514 case MemBarrierKind::kStoreStore: {
3515 // nop
3516 break;
3517 }
3518 default:
3519 LOG(FATAL) << "Unexpected memory barier " << kind;
3520 }
3521}
3522
3523void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3524 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3525
Nicolas Geoffray39468442014-09-02 15:17:15 +01003526 LocationSummary* locations =
3527 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003528 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003529 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3530 locations->SetOut(Location::RequiresFpuRegister());
3531 } else {
3532 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3533 }
Calin Juravle52c48962014-12-16 17:02:57 +00003534}
3535
3536void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3537 const FieldInfo& field_info) {
3538 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3539
3540 LocationSummary* locations = instruction->GetLocations();
3541 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3542 Location out = locations->Out();
3543 bool is_volatile = field_info.IsVolatile();
3544 Primitive::Type field_type = field_info.GetFieldType();
3545 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3546
3547 switch (field_type) {
3548 case Primitive::kPrimBoolean: {
3549 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3550 break;
3551 }
3552
3553 case Primitive::kPrimByte: {
3554 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3555 break;
3556 }
3557
3558 case Primitive::kPrimShort: {
3559 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3560 break;
3561 }
3562
3563 case Primitive::kPrimChar: {
3564 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3565 break;
3566 }
3567
3568 case Primitive::kPrimInt:
3569 case Primitive::kPrimNot: {
3570 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3571 break;
3572 }
3573
3574 case Primitive::kPrimLong: {
3575 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3576 break;
3577 }
3578
3579 case Primitive::kPrimFloat: {
3580 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3581 break;
3582 }
3583
3584 case Primitive::kPrimDouble: {
3585 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3586 break;
3587 }
3588
3589 case Primitive::kPrimVoid:
3590 LOG(FATAL) << "Unreachable type " << field_type;
3591 UNREACHABLE();
3592 }
3593
Calin Juravle77520bc2015-01-12 18:45:46 +00003594 codegen_->MaybeRecordImplicitNullCheck(instruction);
3595
Calin Juravle52c48962014-12-16 17:02:57 +00003596 if (is_volatile) {
3597 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3598 }
Roland Levillain4d027112015-07-01 15:41:14 +01003599
3600 if (field_type == Primitive::kPrimNot) {
3601 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3602 }
Calin Juravle52c48962014-12-16 17:02:57 +00003603}
3604
3605void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3606 const FieldInfo& field_info) {
3607 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3608
3609 LocationSummary* locations =
3610 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003611 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003612 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003613 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003614
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003615 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003616 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3617 locations->SetInAt(1, Location::RequiresFpuRegister());
3618 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003619 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003620 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003621 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003622 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003623 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003624 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003625 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3626 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003627 locations->AddTemp(Location::RequiresRegister());
3628 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003629}
3630
Calin Juravle52c48962014-12-16 17:02:57 +00003631void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003632 const FieldInfo& field_info,
3633 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003634 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3635
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003636 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003637 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3638 Location value = locations->InAt(1);
3639 bool is_volatile = field_info.IsVolatile();
3640 Primitive::Type field_type = field_info.GetFieldType();
3641 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3642
3643 if (is_volatile) {
3644 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3645 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003646
3647 switch (field_type) {
3648 case Primitive::kPrimBoolean:
3649 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003650 if (value.IsConstant()) {
3651 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3652 __ movb(Address(base, offset), Immediate(v));
3653 } else {
3654 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3655 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003656 break;
3657 }
3658
3659 case Primitive::kPrimShort:
3660 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003661 if (value.IsConstant()) {
3662 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3663 __ movw(Address(base, offset), Immediate(v));
3664 } else {
3665 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3666 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003667 break;
3668 }
3669
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003670 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003671 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003672 if (value.IsConstant()) {
3673 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003674 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3675 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3676 // Note: if heap poisoning is enabled, no need to poison
3677 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003678 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003679 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003680 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3681 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3682 __ movl(temp, value.AsRegister<CpuRegister>());
3683 __ PoisonHeapReference(temp);
3684 __ movl(Address(base, offset), temp);
3685 } else {
3686 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3687 }
Mark Mendell40741f32015-04-20 22:10:34 -04003688 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003689 break;
3690 }
3691
3692 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003693 if (value.IsConstant()) {
3694 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3695 DCHECK(IsInt<32>(v));
3696 int32_t v_32 = v;
3697 __ movq(Address(base, offset), Immediate(v_32));
3698 } else {
3699 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3700 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003701 break;
3702 }
3703
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003704 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003705 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003706 break;
3707 }
3708
3709 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003710 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003711 break;
3712 }
3713
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003714 case Primitive::kPrimVoid:
3715 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003716 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003717 }
Calin Juravle52c48962014-12-16 17:02:57 +00003718
Calin Juravle77520bc2015-01-12 18:45:46 +00003719 codegen_->MaybeRecordImplicitNullCheck(instruction);
3720
3721 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3722 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3723 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003724 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003725 }
3726
Calin Juravle52c48962014-12-16 17:02:57 +00003727 if (is_volatile) {
3728 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3729 }
3730}
3731
3732void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3733 HandleFieldSet(instruction, instruction->GetFieldInfo());
3734}
3735
3736void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003737 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003738}
3739
3740void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003741 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003742}
3743
3744void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003745 HandleFieldGet(instruction, instruction->GetFieldInfo());
3746}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003747
Calin Juravle52c48962014-12-16 17:02:57 +00003748void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3749 HandleFieldGet(instruction);
3750}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003751
Calin Juravle52c48962014-12-16 17:02:57 +00003752void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3753 HandleFieldGet(instruction, instruction->GetFieldInfo());
3754}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003755
Calin Juravle52c48962014-12-16 17:02:57 +00003756void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3757 HandleFieldSet(instruction, instruction->GetFieldInfo());
3758}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003759
Calin Juravle52c48962014-12-16 17:02:57 +00003760void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003761 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003762}
3763
3764void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003765 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3766 ? LocationSummary::kCallOnSlowPath
3767 : LocationSummary::kNoCall;
3768 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3769 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003770 ? Location::RequiresRegister()
3771 : Location::Any();
3772 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003773 if (instruction->HasUses()) {
3774 locations->SetOut(Location::SameAsFirstInput());
3775 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003776}
3777
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003778void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003779 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3780 return;
3781 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003782 LocationSummary* locations = instruction->GetLocations();
3783 Location obj = locations->InAt(0);
3784
3785 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3786 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3787}
3788
3789void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003790 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003791 codegen_->AddSlowPath(slow_path);
3792
3793 LocationSummary* locations = instruction->GetLocations();
3794 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003795
3796 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003797 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003798 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003799 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003800 } else {
3801 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00003802 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003803 __ jmp(slow_path->GetEntryLabel());
3804 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003805 }
3806 __ j(kEqual, slow_path->GetEntryLabel());
3807}
3808
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003809void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003810 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003811 GenerateImplicitNullCheck(instruction);
3812 } else {
3813 GenerateExplicitNullCheck(instruction);
3814 }
3815}
3816
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003817void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003818 LocationSummary* locations =
3819 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003820 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003821 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003822 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3823 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3824 } else {
3825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3826 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003827}
3828
3829void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3830 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003831 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003832 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003833 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003834
Roland Levillain4d027112015-07-01 15:41:14 +01003835 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003836 case Primitive::kPrimBoolean: {
3837 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003838 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003839 if (index.IsConstant()) {
3840 __ movzxb(out, Address(obj,
3841 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3842 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003843 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003844 }
3845 break;
3846 }
3847
3848 case Primitive::kPrimByte: {
3849 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003851 if (index.IsConstant()) {
3852 __ movsxb(out, Address(obj,
3853 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3854 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003855 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003856 }
3857 break;
3858 }
3859
3860 case Primitive::kPrimShort: {
3861 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003862 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003863 if (index.IsConstant()) {
3864 __ movsxw(out, Address(obj,
3865 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3866 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003867 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003868 }
3869 break;
3870 }
3871
3872 case Primitive::kPrimChar: {
3873 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003874 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003875 if (index.IsConstant()) {
3876 __ movzxw(out, Address(obj,
3877 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3878 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003879 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003880 }
3881 break;
3882 }
3883
3884 case Primitive::kPrimInt:
3885 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003886 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3887 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003888 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003889 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003890 if (index.IsConstant()) {
3891 __ movl(out, Address(obj,
3892 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3893 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003894 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003895 }
3896 break;
3897 }
3898
3899 case Primitive::kPrimLong: {
3900 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003901 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003902 if (index.IsConstant()) {
3903 __ movq(out, Address(obj,
3904 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3905 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003906 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003907 }
3908 break;
3909 }
3910
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003911 case Primitive::kPrimFloat: {
3912 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003913 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003914 if (index.IsConstant()) {
3915 __ movss(out, Address(obj,
3916 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3917 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003918 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003919 }
3920 break;
3921 }
3922
3923 case Primitive::kPrimDouble: {
3924 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003925 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003926 if (index.IsConstant()) {
3927 __ movsd(out, Address(obj,
3928 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3929 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003930 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003931 }
3932 break;
3933 }
3934
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003935 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003936 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003937 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003938 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003939 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003940
3941 if (type == Primitive::kPrimNot) {
3942 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3943 __ MaybeUnpoisonHeapReference(out);
3944 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003945}
3946
3947void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003948 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003949
3950 bool needs_write_barrier =
3951 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3952 bool needs_runtime_call = instruction->NeedsTypeCheck();
3953
Nicolas Geoffray39468442014-09-02 15:17:15 +01003954 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003955 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3956 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003957 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003958 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3959 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3960 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003961 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003962 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003963 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003964 1, Location::RegisterOrConstant(instruction->InputAt(1)));
3965 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003966 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04003967 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003968 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
3969 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003970 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003971 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003972 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003973
3974 if (needs_write_barrier) {
3975 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003976 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003977 locations->AddTemp(Location::RequiresRegister());
3978 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003979 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003980}
3981
3982void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
3983 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003984 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003985 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003986 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003987 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003988 bool needs_runtime_call = locations->WillCall();
3989 bool needs_write_barrier =
3990 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003991
3992 switch (value_type) {
3993 case Primitive::kPrimBoolean:
3994 case Primitive::kPrimByte: {
3995 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003996 if (index.IsConstant()) {
3997 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003998 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003999 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004000 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00004001 __ movb(Address(obj, offset),
4002 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004003 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004004 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004005 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004006 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
4007 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004008 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004009 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004010 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4011 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004012 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004013 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004014 break;
4015 }
4016
4017 case Primitive::kPrimShort:
4018 case Primitive::kPrimChar: {
4019 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004020 if (index.IsConstant()) {
4021 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004022 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004023 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004024 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004025 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00004026 __ movw(Address(obj, offset),
4027 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004028 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004029 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004030 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004031 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004032 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
4033 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004034 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004035 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00004036 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004037 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4038 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004039 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004040 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004041 break;
4042 }
4043
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004044 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004045 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004046 if (!needs_runtime_call) {
4047 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4048 if (index.IsConstant()) {
4049 size_t offset =
4050 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4051 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004052 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4053 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4054 __ movl(temp, value.AsRegister<CpuRegister>());
4055 __ PoisonHeapReference(temp);
4056 __ movl(Address(obj, offset), temp);
4057 } else {
4058 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
4059 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004060 } else {
4061 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004062 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004063 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4064 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4065 // Note: if heap poisoning is enabled, no need to poison
4066 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04004067 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004068 }
4069 } else {
4070 DCHECK(index.IsRegister()) << index;
4071 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004072 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4073 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4074 __ movl(temp, value.AsRegister<CpuRegister>());
4075 __ PoisonHeapReference(temp);
4076 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4077 } else {
4078 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4079 value.AsRegister<CpuRegister>());
4080 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004081 } else {
4082 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004083 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004084 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4085 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4086 // Note: if heap poisoning is enabled, no need to poison
4087 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004088 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004089 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004090 }
4091 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004092 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004093 if (needs_write_barrier) {
4094 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4096 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004097 codegen_->MarkGCCard(
4098 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004099 }
4100 } else {
4101 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004102 // Note: if heap poisoning is enabled, pAputObject takes cares
4103 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004104 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4105 instruction,
4106 instruction->GetDexPc(),
4107 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004108 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004109 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004110 break;
4111 }
4112
4113 case Primitive::kPrimLong: {
4114 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004115 if (index.IsConstant()) {
4116 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004117 if (value.IsRegister()) {
4118 __ movq(Address(obj, offset), 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, offset), Immediate(v_32));
4124 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004125 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004126 if (value.IsRegister()) {
4127 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4128 value.AsRegister<CpuRegister>());
4129 } else {
4130 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4131 DCHECK(IsInt<32>(v));
4132 int32_t v_32 = v;
4133 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4134 Immediate(v_32));
4135 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004136 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004137 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004138 break;
4139 }
4140
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004141 case Primitive::kPrimFloat: {
4142 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4143 if (index.IsConstant()) {
4144 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4145 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004146 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004147 } else {
4148 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004149 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4150 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004151 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004152 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004153 break;
4154 }
4155
4156 case Primitive::kPrimDouble: {
4157 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4158 if (index.IsConstant()) {
4159 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4160 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004161 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004162 } else {
4163 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004164 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4165 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004166 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004167 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004168 break;
4169 }
4170
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004171 case Primitive::kPrimVoid:
4172 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004173 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004174 }
4175}
4176
4177void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004178 LocationSummary* locations =
4179 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004180 locations->SetInAt(0, Location::RequiresRegister());
4181 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004182}
4183
4184void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4185 LocationSummary* locations = instruction->GetLocations();
4186 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004187 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4188 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004189 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004190 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004191}
4192
4193void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004194 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4195 ? LocationSummary::kCallOnSlowPath
4196 : LocationSummary::kNoCall;
4197 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004198 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004199 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004200 if (instruction->HasUses()) {
4201 locations->SetOut(Location::SameAsFirstInput());
4202 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004203}
4204
4205void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4206 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004207 Location index_loc = locations->InAt(0);
4208 Location length_loc = locations->InAt(1);
4209 SlowPathCodeX86_64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004210 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004211
Mark Mendell99dbd682015-04-22 16:18:52 -04004212 if (length_loc.IsConstant()) {
4213 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4214 if (index_loc.IsConstant()) {
4215 // BCE will remove the bounds check if we are guarenteed to pass.
4216 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4217 if (index < 0 || index >= length) {
4218 codegen_->AddSlowPath(slow_path);
4219 __ jmp(slow_path->GetEntryLabel());
4220 } else {
4221 // Some optimization after BCE may have generated this, and we should not
4222 // generate a bounds check if it is a valid range.
4223 }
4224 return;
4225 }
4226
4227 // We have to reverse the jump condition because the length is the constant.
4228 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4229 __ cmpl(index_reg, Immediate(length));
4230 codegen_->AddSlowPath(slow_path);
4231 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004232 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004233 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4234 if (index_loc.IsConstant()) {
4235 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4236 __ cmpl(length, Immediate(value));
4237 } else {
4238 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4239 }
4240 codegen_->AddSlowPath(slow_path);
4241 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004242 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004243}
4244
4245void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4246 CpuRegister card,
4247 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004248 CpuRegister value,
4249 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004250 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004251 if (value_can_be_null) {
4252 __ testl(value, value);
4253 __ j(kEqual, &is_null);
4254 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004255 __ gs()->movq(card, Address::Absolute(
4256 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4257 __ movq(temp, object);
4258 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004259 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004260 if (value_can_be_null) {
4261 __ Bind(&is_null);
4262 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004263}
4264
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004265void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4266 temp->SetLocations(nullptr);
4267}
4268
4269void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4270 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004271 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004272}
4273
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004274void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004275 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004276 LOG(FATAL) << "Unimplemented";
4277}
4278
4279void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004280 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4281}
4282
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004283void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4284 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4285}
4286
4287void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004288 HBasicBlock* block = instruction->GetBlock();
4289 if (block->GetLoopInformation() != nullptr) {
4290 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4291 // The back edge will generate the suspend check.
4292 return;
4293 }
4294 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4295 // The goto will generate the suspend check.
4296 return;
4297 }
4298 GenerateSuspendCheck(instruction, nullptr);
4299}
4300
4301void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4302 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004303 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004304 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4305 if (slow_path == nullptr) {
4306 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4307 instruction->SetSlowPath(slow_path);
4308 codegen_->AddSlowPath(slow_path);
4309 if (successor != nullptr) {
4310 DCHECK(successor->IsLoopHeader());
4311 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4312 }
4313 } else {
4314 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4315 }
4316
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004317 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004318 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004319 if (successor == nullptr) {
4320 __ j(kNotEqual, slow_path->GetEntryLabel());
4321 __ Bind(slow_path->GetReturnLabel());
4322 } else {
4323 __ j(kEqual, codegen_->GetLabelOf(successor));
4324 __ jmp(slow_path->GetEntryLabel());
4325 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004326}
4327
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004328X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4329 return codegen_->GetAssembler();
4330}
4331
4332void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4333 MoveOperands* move = moves_.Get(index);
4334 Location source = move->GetSource();
4335 Location destination = move->GetDestination();
4336
4337 if (source.IsRegister()) {
4338 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004339 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004340 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004341 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004342 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004343 } else {
4344 DCHECK(destination.IsDoubleStackSlot());
4345 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004346 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004347 }
4348 } else if (source.IsStackSlot()) {
4349 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004350 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004351 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004352 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004353 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004354 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004355 } else {
4356 DCHECK(destination.IsStackSlot());
4357 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4358 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4359 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004360 } else if (source.IsDoubleStackSlot()) {
4361 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004362 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004363 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004364 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004365 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4366 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004367 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004368 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004369 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4370 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4371 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004372 } else if (source.IsConstant()) {
4373 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004374 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4375 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004376 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004377 if (value == 0) {
4378 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4379 } else {
4380 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4381 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004382 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004383 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004384 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004385 }
4386 } else if (constant->IsLongConstant()) {
4387 int64_t value = constant->AsLongConstant()->GetValue();
4388 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004389 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004390 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004391 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004392 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004393 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004394 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004395 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004396 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004397 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004398 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4399 if (value == 0) {
4400 // easy FP 0.0.
4401 __ xorps(dest, dest);
4402 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004403 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004404 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004405 } else {
4406 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004407 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004408 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4409 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004410 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004411 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004412 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004413 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004414 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004415 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4416 if (value == 0) {
4417 __ xorpd(dest, dest);
4418 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004419 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004420 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004421 } else {
4422 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004423 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004424 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004425 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004426 } else if (source.IsFpuRegister()) {
4427 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004428 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004429 } else if (destination.IsStackSlot()) {
4430 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004431 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004432 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004433 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004434 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004435 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004436 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004437 }
4438}
4439
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004440void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004441 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004442 __ movl(Address(CpuRegister(RSP), mem), reg);
4443 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004444}
4445
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004446void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004447 ScratchRegisterScope ensure_scratch(
4448 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4449
4450 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4451 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4452 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4453 Address(CpuRegister(RSP), mem2 + stack_offset));
4454 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4455 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4456 CpuRegister(ensure_scratch.GetRegister()));
4457}
4458
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004459void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4460 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4461 __ movq(Address(CpuRegister(RSP), mem), reg);
4462 __ movq(reg, CpuRegister(TMP));
4463}
4464
4465void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4466 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004467 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004468
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004469 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4470 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4471 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4472 Address(CpuRegister(RSP), mem2 + stack_offset));
4473 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4474 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4475 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004476}
4477
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004478void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4479 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4480 __ movss(Address(CpuRegister(RSP), mem), reg);
4481 __ movd(reg, CpuRegister(TMP));
4482}
4483
4484void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4485 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4486 __ movsd(Address(CpuRegister(RSP), mem), reg);
4487 __ movd(reg, CpuRegister(TMP));
4488}
4489
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004490void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4491 MoveOperands* move = moves_.Get(index);
4492 Location source = move->GetSource();
4493 Location destination = move->GetDestination();
4494
4495 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004496 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004497 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004498 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004499 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004500 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004501 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004502 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4503 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004504 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004505 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004506 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004507 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4508 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004509 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004510 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4511 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4512 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004513 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004514 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004515 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004516 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004517 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004518 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004519 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004520 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004521 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004522 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004523 }
4524}
4525
4526
4527void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4528 __ pushq(CpuRegister(reg));
4529}
4530
4531
4532void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4533 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004534}
4535
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004536void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4537 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4538 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4539 Immediate(mirror::Class::kStatusInitialized));
4540 __ j(kLess, slow_path->GetEntryLabel());
4541 __ Bind(slow_path->GetExitLabel());
4542 // No need for memory fence, thanks to the X86_64 memory model.
4543}
4544
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004545void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004546 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4547 ? LocationSummary::kCallOnSlowPath
4548 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004549 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004550 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004551 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004552 locations->SetOut(Location::RequiresRegister());
4553}
4554
4555void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004556 LocationSummary* locations = cls->GetLocations();
4557 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4558 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004559 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004560 DCHECK(!cls->CanCallRuntime());
4561 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004562 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004563 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004564 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004565 __ movq(out, Address(
4566 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004567 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004568 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004569
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004570 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4571 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4572 codegen_->AddSlowPath(slow_path);
4573 __ testl(out, out);
4574 __ j(kEqual, slow_path->GetEntryLabel());
4575 if (cls->MustGenerateClinitCheck()) {
4576 GenerateClassInitializationCheck(slow_path, out);
4577 } else {
4578 __ Bind(slow_path->GetExitLabel());
4579 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004580 }
4581}
4582
4583void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4584 LocationSummary* locations =
4585 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4586 locations->SetInAt(0, Location::RequiresRegister());
4587 if (check->HasUses()) {
4588 locations->SetOut(Location::SameAsFirstInput());
4589 }
4590}
4591
4592void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004593 // We assume the class to not be null.
4594 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4595 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004596 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004597 GenerateClassInitializationCheck(slow_path,
4598 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004599}
4600
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004601void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4602 LocationSummary* locations =
4603 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004604 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004605 locations->SetOut(Location::RequiresRegister());
4606}
4607
4608void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4609 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4610 codegen_->AddSlowPath(slow_path);
4611
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004612 LocationSummary* locations = load->GetLocations();
4613 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4614 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004615 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004616 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004617 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004618 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004619 __ testl(out, out);
4620 __ j(kEqual, slow_path->GetEntryLabel());
4621 __ Bind(slow_path->GetExitLabel());
4622}
4623
David Brazdilcb1c0552015-08-04 16:22:25 +01004624static Address GetExceptionTlsAddress() {
4625 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4626}
4627
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004628void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4629 LocationSummary* locations =
4630 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4631 locations->SetOut(Location::RequiresRegister());
4632}
4633
4634void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004635 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4636}
4637
4638void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4639 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4640}
4641
4642void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4643 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004644}
4645
4646void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4647 LocationSummary* locations =
4648 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4649 InvokeRuntimeCallingConvention calling_convention;
4650 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4651}
4652
4653void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004654 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4655 instruction,
4656 instruction->GetDexPc(),
4657 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004658}
4659
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004660void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004661 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4662 ? LocationSummary::kNoCall
4663 : LocationSummary::kCallOnSlowPath;
4664 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4665 locations->SetInAt(0, Location::RequiresRegister());
4666 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004667 // Note that TypeCheckSlowPathX86_64 uses this register too.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004668 locations->SetOut(Location::RequiresRegister());
4669}
4670
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004671void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004672 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004673 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004674 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004675 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004676 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4677 Label done, zero;
4678 SlowPathCodeX86_64* slow_path = nullptr;
4679
4680 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004681 // Avoid null check if we know obj is not null.
4682 if (instruction->MustDoNullCheck()) {
4683 __ testl(obj, obj);
4684 __ j(kEqual, &zero);
4685 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004686 // Compare the class of `obj` with `cls`.
4687 __ movl(out, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004688 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004689 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004690 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004691 } else {
4692 DCHECK(cls.IsStackSlot()) << cls;
4693 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4694 }
4695 if (instruction->IsClassFinal()) {
4696 // Classes must be equal for the instanceof to succeed.
4697 __ j(kNotEqual, &zero);
4698 __ movl(out, Immediate(1));
4699 __ jmp(&done);
4700 } else {
4701 // If the classes are not equal, we go into a slow path.
4702 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004703 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004704 codegen_->AddSlowPath(slow_path);
4705 __ j(kNotEqual, slow_path->GetEntryLabel());
4706 __ movl(out, Immediate(1));
4707 __ jmp(&done);
4708 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004709
4710 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4711 __ Bind(&zero);
4712 __ movl(out, Immediate(0));
4713 }
4714
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004715 if (slow_path != nullptr) {
4716 __ Bind(slow_path->GetExitLabel());
4717 }
4718 __ Bind(&done);
4719}
4720
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004721void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
4722 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4723 instruction, LocationSummary::kCallOnSlowPath);
4724 locations->SetInAt(0, Location::RequiresRegister());
4725 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004726 // Note that TypeCheckSlowPathX86_64 uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004727 locations->AddTemp(Location::RequiresRegister());
4728}
4729
4730void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4731 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004732 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004733 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004734 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004735 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004736 SlowPathCodeX86_64* slow_path =
4737 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004738 codegen_->AddSlowPath(slow_path);
4739
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004740 // Avoid null check if we know obj is not null.
4741 if (instruction->MustDoNullCheck()) {
4742 __ testl(obj, obj);
4743 __ j(kEqual, slow_path->GetExitLabel());
4744 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004745 // Compare the class of `obj` with `cls`.
4746 __ movl(temp, Address(obj, class_offset));
Roland Levillain4d027112015-07-01 15:41:14 +01004747 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004748 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004749 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004750 } else {
4751 DCHECK(cls.IsStackSlot()) << cls;
4752 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4753 }
Roland Levillain4d027112015-07-01 15:41:14 +01004754 // The checkcast succeeds if the classes are equal (fast path).
4755 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004756 __ j(kNotEqual, slow_path->GetEntryLabel());
4757 __ Bind(slow_path->GetExitLabel());
4758}
4759
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004760void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
4761 LocationSummary* locations =
4762 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4763 InvokeRuntimeCallingConvention calling_convention;
4764 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4765}
4766
4767void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004768 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4769 : QUICK_ENTRY_POINT(pUnlockObject),
4770 instruction,
4771 instruction->GetDexPc(),
4772 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004773}
4774
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004775void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4776void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4777void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4778
4779void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4780 LocationSummary* locations =
4781 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4782 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4783 || instruction->GetResultType() == Primitive::kPrimLong);
4784 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004785 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004786 locations->SetOut(Location::SameAsFirstInput());
4787}
4788
4789void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
4790 HandleBitwiseOperation(instruction);
4791}
4792
4793void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
4794 HandleBitwiseOperation(instruction);
4795}
4796
4797void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
4798 HandleBitwiseOperation(instruction);
4799}
4800
4801void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
4802 LocationSummary* locations = instruction->GetLocations();
4803 Location first = locations->InAt(0);
4804 Location second = locations->InAt(1);
4805 DCHECK(first.Equals(locations->Out()));
4806
4807 if (instruction->GetResultType() == Primitive::kPrimInt) {
4808 if (second.IsRegister()) {
4809 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004810 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004811 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004812 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004813 } else {
4814 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004815 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004816 }
4817 } else if (second.IsConstant()) {
4818 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
4819 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004820 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004821 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004822 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004823 } else {
4824 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004825 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004826 }
4827 } else {
4828 Address address(CpuRegister(RSP), second.GetStackIndex());
4829 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004830 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004831 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004832 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004833 } else {
4834 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004835 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004836 }
4837 }
4838 } else {
4839 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004840 CpuRegister first_reg = first.AsRegister<CpuRegister>();
4841 bool second_is_constant = false;
4842 int64_t value = 0;
4843 if (second.IsConstant()) {
4844 second_is_constant = true;
4845 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004846 }
Mark Mendell40741f32015-04-20 22:10:34 -04004847 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004848
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004849 if (instruction->IsAnd()) {
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 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
4853 } else {
4854 __ andq(first_reg, codegen_->LiteralInt64Address(value));
4855 }
4856 } else if (second.IsDoubleStackSlot()) {
4857 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004858 } else {
4859 __ andq(first_reg, second.AsRegister<CpuRegister>());
4860 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004861 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004862 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004863 if (is_int32_value) {
4864 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
4865 } else {
4866 __ orq(first_reg, codegen_->LiteralInt64Address(value));
4867 }
4868 } else if (second.IsDoubleStackSlot()) {
4869 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004870 } else {
4871 __ orq(first_reg, second.AsRegister<CpuRegister>());
4872 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004873 } else {
4874 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004875 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04004876 if (is_int32_value) {
4877 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
4878 } else {
4879 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
4880 }
4881 } else if (second.IsDoubleStackSlot()) {
4882 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004883 } else {
4884 __ xorq(first_reg, second.AsRegister<CpuRegister>());
4885 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004886 }
4887 }
4888}
4889
Calin Juravleb1498f62015-02-16 13:13:29 +00004890void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
4891 // Nothing to do, this should be removed during prepare for register allocator.
4892 UNUSED(instruction);
4893 LOG(FATAL) << "Unreachable";
4894}
4895
4896void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
4897 // Nothing to do, this should be removed during prepare for register allocator.
4898 UNUSED(instruction);
4899 LOG(FATAL) << "Unreachable";
4900}
4901
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004902void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
4903 DCHECK(codegen_->IsBaseline());
4904 LocationSummary* locations =
4905 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4906 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4907}
4908
4909void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4910 DCHECK(codegen_->IsBaseline());
4911 // Will be generated at use site.
4912}
4913
Mark Mendell92e83bf2015-05-07 11:25:03 -04004914void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
4915 if (value == 0) {
4916 __ xorl(dest, dest);
4917 } else if (value > 0 && IsInt<32>(value)) {
4918 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
4919 __ movl(dest, Immediate(static_cast<int32_t>(value)));
4920 } else {
4921 __ movq(dest, Immediate(value));
4922 }
4923}
4924
Mark Mendellcfa410b2015-05-25 16:02:44 -04004925void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
4926 DCHECK(dest.IsDoubleStackSlot());
4927 if (IsInt<32>(value)) {
4928 // Can move directly as an int32 constant.
4929 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
4930 Immediate(static_cast<int32_t>(value)));
4931 } else {
4932 Load64BitValue(CpuRegister(TMP), value);
4933 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
4934 }
4935}
4936
Mark Mendellf55c3e02015-03-26 21:07:46 -04004937void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
4938 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04004939 X86_64Assembler* assembler = GetAssembler();
4940 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04004941 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
4942 // byte values. If used for vectors at a later time, this will need to be
4943 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04004944 assembler->Align(4, 0);
4945 constant_area_start_ = assembler->CodeSize();
4946 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04004947 }
4948
4949 // And finish up.
4950 CodeGenerator::Finalize(allocator);
4951}
4952
4953/**
4954 * Class to handle late fixup of offsets into constant area.
4955 */
4956class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
4957 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04004958 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04004959 : codegen_(codegen), offset_into_constant_area_(offset) {}
4960
4961 private:
4962 void Process(const MemoryRegion& region, int pos) OVERRIDE {
4963 // Patch the correct offset for the instruction. We use the address of the
4964 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
4965 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
4966 int relative_position = constant_offset - pos;
4967
4968 // Patch in the right value.
4969 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
4970 }
4971
Mark Mendell39dcf552015-04-09 20:42:42 -04004972 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04004973
4974 // Location in constant area that the fixup refers to.
4975 int offset_into_constant_area_;
4976};
4977
4978Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
4979 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
4980 return Address::RIP(fixup);
4981}
4982
4983Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
4984 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
4985 return Address::RIP(fixup);
4986}
4987
4988Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
4989 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
4990 return Address::RIP(fixup);
4991}
4992
4993Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
4994 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
4995 return Address::RIP(fixup);
4996}
4997
Roland Levillain4d027112015-07-01 15:41:14 +01004998#undef __
4999
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005000} // namespace x86_64
5001} // namespace art