blob: ad6588c359dc1d73224183378f914bc4b744766f [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())->
Calin Juravle175dc732015-08-25 15:42:32 +010051#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Andreas Gampe85b62f22015-09-09 13:15:38 -070053class NullCheckSlowPathX86_64 : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -070079class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000080 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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700105class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700139class SuspendCheckSlowPathX86_64 : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700179class BoundsCheckSlowPathX86_64 : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700216class LoadClassSlowPathX86_64 : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700269class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000270 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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700301class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000303 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
304 : instruction_(instruction), is_fatal_(is_fatal) {}
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 Geoffray85c7bab2015-09-18 13:40:46 +0000316
317 if (instruction_->IsCheckCast()) {
318 // The codegen for the instruction overwrites `temp`, so put it back in place.
319 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
320 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
321 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
322 __ movl(temp, Address(obj, class_offset));
323 __ MaybeUnpoisonHeapReference(temp);
324 }
325
326 if (!is_fatal_) {
327 SaveLiveRegisters(codegen, locations);
328 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
330 // We're moving two locations to locations that could overlap, so we need a parallel
331 // move resolver.
332 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000333 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100334 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000335 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100336 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100337 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100338 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
339 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000341 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100342 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
343 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100344 dex_pc,
Alexandre Rames8158f282015-08-07 10:26:17 +0100345 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000346 } else {
347 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100348 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
349 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100350 dex_pc,
Alexandre Rames8158f282015-08-07 10:26:17 +0100351 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000352 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000354 if (!is_fatal_) {
355 if (instruction_->IsInstanceOf()) {
356 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
357 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000358
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000359 RestoreLiveRegisters(codegen, locations);
360 __ jmp(GetExitLabel());
361 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000362 }
363
Alexandre Rames9931f312015-06-19 14:47:01 +0100364 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
365
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000366 bool IsFatal() const OVERRIDE { return is_fatal_; }
367
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000368 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000369 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000371
372 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
373};
374
Andreas Gampe85b62f22015-09-09 13:15:38 -0700375class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700376 public:
377 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
378 : instruction_(instruction) {}
379
380 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100381 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 __ Bind(GetEntryLabel());
383 SaveLiveRegisters(codegen, instruction_->GetLocations());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384 DCHECK(instruction_->IsDeoptimize());
385 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
Alexandre Rames8158f282015-08-07 10:26:17 +0100386 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
387 deoptimize,
388 deoptimize->GetDexPc(),
389 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 }
391
Alexandre Rames9931f312015-06-19 14:47:01 +0100392 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
393
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 private:
395 HInstruction* const instruction_;
396 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
397};
398
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100399class ArraySetSlowPathX86_64 : public SlowPathCode {
400 public:
401 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : instruction_(instruction) {}
402
403 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
404 LocationSummary* locations = instruction_->GetLocations();
405 __ Bind(GetEntryLabel());
406 SaveLiveRegisters(codegen, locations);
407
408 InvokeRuntimeCallingConvention calling_convention;
409 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
410 parallel_move.AddMove(
411 locations->InAt(0),
412 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
413 Primitive::kPrimNot,
414 nullptr);
415 parallel_move.AddMove(
416 locations->InAt(1),
417 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
418 Primitive::kPrimInt,
419 nullptr);
420 parallel_move.AddMove(
421 locations->InAt(2),
422 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
423 Primitive::kPrimNot,
424 nullptr);
425 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
426
427 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
428 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
429 instruction_,
430 instruction_->GetDexPc(),
431 this);
432 RestoreLiveRegisters(codegen, locations);
433 __ jmp(GetExitLabel());
434 }
435
436 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
437
438 private:
439 HInstruction* const instruction_;
440
441 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
442};
443
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100444#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100445#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100446
Roland Levillain4fa13f62015-07-06 18:11:54 +0100447inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700448 switch (cond) {
449 case kCondEQ: return kEqual;
450 case kCondNE: return kNotEqual;
451 case kCondLT: return kLess;
452 case kCondLE: return kLessEqual;
453 case kCondGT: return kGreater;
454 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700455 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100456 LOG(FATAL) << "Unreachable";
457 UNREACHABLE();
458}
459
460inline Condition X86_64FPCondition(IfCondition cond) {
461 switch (cond) {
462 case kCondEQ: return kEqual;
463 case kCondNE: return kNotEqual;
464 case kCondLT: return kBelow;
465 case kCondLE: return kBelowEqual;
466 case kCondGT: return kAbove;
467 case kCondGE: return kAboveEqual;
468 };
469 LOG(FATAL) << "Unreachable";
470 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700471}
472
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800473void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100474 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800475 // All registers are assumed to be correctly set up.
476
Vladimir Marko58155012015-08-19 12:49:41 +0000477 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
478 switch (invoke->GetMethodLoadKind()) {
479 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
480 // temp = thread->string_init_entrypoint
481 __ gs()->movl(temp.AsRegister<CpuRegister>(),
482 Address::Absolute(invoke->GetStringInitOffset(), true));
483 break;
484 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
485 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
486 break;
487 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
488 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
489 break;
490 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
491 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
492 method_patches_.emplace_back(invoke->GetTargetMethod());
493 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
494 break;
495 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
496 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
497 invoke->GetDexCacheArrayOffset());
498 __ movq(temp.AsRegister<CpuRegister>(),
499 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
500 // Bind the label at the end of the "movl" insn.
501 __ Bind(&pc_rel_dex_cache_patches_.back().label);
502 break;
503 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
504 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
505 Register method_reg;
506 CpuRegister reg = temp.AsRegister<CpuRegister>();
507 if (current_method.IsRegister()) {
508 method_reg = current_method.AsRegister<Register>();
509 } else {
510 DCHECK(invoke->GetLocations()->Intrinsified());
511 DCHECK(!current_method.IsValid());
512 method_reg = reg.AsRegister();
513 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
514 }
515 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100516 __ movq(reg,
517 Address(CpuRegister(method_reg),
518 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000519 // temp = temp[index_in_cache]
520 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
521 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
522 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100523 }
Vladimir Marko58155012015-08-19 12:49:41 +0000524 }
525
526 switch (invoke->GetCodePtrLocation()) {
527 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
528 __ call(&frame_entry_label_);
529 break;
530 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
531 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
532 Label* label = &relative_call_patches_.back().label;
533 __ call(label); // Bind to the patch label, override at link time.
534 __ Bind(label); // Bind the label at the end of the "call" insn.
535 break;
536 }
537 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
538 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
539 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
540 FALLTHROUGH_INTENDED;
541 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
542 // (callee_method + offset_of_quick_compiled_code)()
543 __ call(Address(callee_method.AsRegister<CpuRegister>(),
544 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
545 kX86_64WordSize).SizeValue()));
546 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000547 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800548
549 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800550}
551
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000552void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
553 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
554 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
555 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
556 LocationSummary* locations = invoke->GetLocations();
557 Location receiver = locations->InAt(0);
558 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
559 // temp = object->GetClass();
560 DCHECK(receiver.IsRegister());
561 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
562 MaybeRecordImplicitNullCheck(invoke);
563 __ MaybeUnpoisonHeapReference(temp);
564 // temp = temp->GetMethodAt(method_offset);
565 __ movq(temp, Address(temp, method_offset));
566 // call temp->GetEntryPoint();
567 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
568 kX86_64WordSize).SizeValue()));
569}
570
Vladimir Marko58155012015-08-19 12:49:41 +0000571void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
572 DCHECK(linker_patches->empty());
573 size_t size =
574 method_patches_.size() + relative_call_patches_.size() + pc_rel_dex_cache_patches_.size();
575 linker_patches->reserve(size);
576 for (const MethodPatchInfo<Label>& info : method_patches_) {
577 // The label points to the end of the "movl" instruction but the literal offset for method
578 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
579 uint32_t literal_offset = info.label.Position() - 4;
580 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
581 info.target_method.dex_file,
582 info.target_method.dex_method_index));
583 }
584 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
585 // The label points to the end of the "call" instruction but the literal offset for method
586 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
587 uint32_t literal_offset = info.label.Position() - 4;
588 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
589 info.target_method.dex_file,
590 info.target_method.dex_method_index));
591 }
592 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
593 // The label points to the end of the "mov" instruction but the literal offset for method
594 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
595 uint32_t literal_offset = info.label.Position() - 4;
596 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
597 &info.target_dex_file,
598 info.label.Position(),
599 info.element_offset));
600 }
601}
602
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100603void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100604 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605}
606
607void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100608 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100609}
610
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100611size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
612 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
613 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100614}
615
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100616size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
617 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
618 return kX86_64WordSize;
619}
620
621size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
622 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
623 return kX86_64WordSize;
624}
625
626size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
627 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
628 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100629}
630
Calin Juravle175dc732015-08-25 15:42:32 +0100631void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
632 HInstruction* instruction,
633 uint32_t dex_pc,
634 SlowPathCode* slow_path) {
635 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
636 instruction,
637 dex_pc,
638 slow_path);
639}
640
641void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100642 HInstruction* instruction,
643 uint32_t dex_pc,
644 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100645 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100646 __ gs()->call(Address::Absolute(entry_point_offset, true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100647 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100648}
649
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000650static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000651// Use a fake return address register to mimic Quick.
652static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400653CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
654 const X86_64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100655 const CompilerOptions& compiler_options,
656 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000657 : CodeGenerator(graph,
658 kNumberOfCpuRegisters,
659 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000660 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000661 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
662 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000663 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000664 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
665 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100666 compiler_options,
667 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100668 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100669 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000670 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400671 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400672 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000673 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100674 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
675 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
676 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000677 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
678}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100679
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100680InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
681 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100682 : HGraphVisitor(graph),
683 assembler_(codegen->GetAssembler()),
684 codegen_(codegen) {}
685
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100686Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100687 switch (type) {
688 case Primitive::kPrimLong:
689 case Primitive::kPrimByte:
690 case Primitive::kPrimBoolean:
691 case Primitive::kPrimChar:
692 case Primitive::kPrimShort:
693 case Primitive::kPrimInt:
694 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100695 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100696 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100697 }
698
699 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100700 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100701 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100702 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100703 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704
705 case Primitive::kPrimVoid:
706 LOG(FATAL) << "Unreachable type " << type;
707 }
708
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100709 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100710}
711
Nicolas Geoffray98893962015-01-21 12:32:32 +0000712void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100713 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100714 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100715
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000716 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100717 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000718
Nicolas Geoffray98893962015-01-21 12:32:32 +0000719 if (is_baseline) {
720 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
721 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
722 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000723 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
724 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
725 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000726 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100727}
728
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100729static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100730 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100731}
David Srbecky9d8606d2015-04-12 09:35:32 +0100732
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100733static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100734 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100735}
736
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100737void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100738 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000739 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100740 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700741 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000742 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100743
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000744 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100745 __ testq(CpuRegister(RAX), Address(
746 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100747 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100748 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000749
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000750 if (HasEmptyFrame()) {
751 return;
752 }
753
Nicolas Geoffray98893962015-01-21 12:32:32 +0000754 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000755 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000756 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000757 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100758 __ cfi().AdjustCFAOffset(kX86_64WordSize);
759 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000760 }
761 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100762
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100763 int adjust = GetFrameSize() - GetCoreSpillSize();
764 __ subq(CpuRegister(RSP), Immediate(adjust));
765 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000766 uint32_t xmm_spill_location = GetFpuSpillStart();
767 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100768
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000769 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
770 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100771 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
772 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
773 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000774 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100775 }
776
Mathieu Chartiere401d142015-04-22 13:56:20 -0700777 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100778 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100779}
780
781void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100782 __ cfi().RememberState();
783 if (!HasEmptyFrame()) {
784 uint32_t xmm_spill_location = GetFpuSpillStart();
785 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
786 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
787 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
788 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
789 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
790 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
791 }
792 }
793
794 int adjust = GetFrameSize() - GetCoreSpillSize();
795 __ addq(CpuRegister(RSP), Immediate(adjust));
796 __ cfi().AdjustCFAOffset(-adjust);
797
798 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
799 Register reg = kCoreCalleeSaves[i];
800 if (allocated_registers_.ContainsCoreRegister(reg)) {
801 __ popq(CpuRegister(reg));
802 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
803 __ cfi().Restore(DWARFReg(reg));
804 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000805 }
806 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100807 __ ret();
808 __ cfi().RestoreState();
809 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100810}
811
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100812void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
813 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100814}
815
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100816Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
817 switch (load->GetType()) {
818 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100819 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100820 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100821
822 case Primitive::kPrimInt:
823 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100824 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100825 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100826
827 case Primitive::kPrimBoolean:
828 case Primitive::kPrimByte:
829 case Primitive::kPrimChar:
830 case Primitive::kPrimShort:
831 case Primitive::kPrimVoid:
832 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700833 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100834 }
835
836 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700837 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100838}
839
840void CodeGeneratorX86_64::Move(Location destination, Location source) {
841 if (source.Equals(destination)) {
842 return;
843 }
844 if (destination.IsRegister()) {
845 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000846 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100847 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000848 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100849 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000850 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100851 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100852 } else {
853 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000854 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100855 Address(CpuRegister(RSP), source.GetStackIndex()));
856 }
857 } else if (destination.IsFpuRegister()) {
858 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000859 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100860 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000861 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100862 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000863 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100864 Address(CpuRegister(RSP), source.GetStackIndex()));
865 } else {
866 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000867 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100868 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100869 }
870 } else if (destination.IsStackSlot()) {
871 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100872 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000873 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100874 } else if (source.IsFpuRegister()) {
875 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000876 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500877 } else if (source.IsConstant()) {
878 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000879 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500880 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100881 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500882 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000883 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
884 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100885 }
886 } else {
887 DCHECK(destination.IsDoubleStackSlot());
888 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100889 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000890 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100891 } else if (source.IsFpuRegister()) {
892 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000893 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500894 } else if (source.IsConstant()) {
895 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800896 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500897 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000898 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500899 } else {
900 DCHECK(constant->IsLongConstant());
901 value = constant->AsLongConstant()->GetValue();
902 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400903 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100904 } else {
905 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000906 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
907 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100908 }
909 }
910}
911
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100912void CodeGeneratorX86_64::Move(HInstruction* instruction,
913 Location location,
914 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000915 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100916 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700917 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100918 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000919 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100920 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000921 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000922 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
923 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000924 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000925 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000926 } else if (location.IsStackSlot()) {
927 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
928 } else {
929 DCHECK(location.IsConstant());
930 DCHECK_EQ(location.GetConstant(), const_to_move);
931 }
932 } else if (const_to_move->IsLongConstant()) {
933 int64_t value = const_to_move->AsLongConstant()->GetValue();
934 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400935 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000936 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400937 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000938 } else {
939 DCHECK(location.IsConstant());
940 DCHECK_EQ(location.GetConstant(), const_to_move);
941 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100942 }
Roland Levillain476df552014-10-09 17:51:36 +0100943 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100944 switch (instruction->GetType()) {
945 case Primitive::kPrimBoolean:
946 case Primitive::kPrimByte:
947 case Primitive::kPrimChar:
948 case Primitive::kPrimShort:
949 case Primitive::kPrimInt:
950 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100951 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100952 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
953 break;
954
955 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100956 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000957 Move(location,
958 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100959 break;
960
961 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100962 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100963 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000964 } else if (instruction->IsTemporary()) {
965 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
966 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100967 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100968 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100969 switch (instruction->GetType()) {
970 case Primitive::kPrimBoolean:
971 case Primitive::kPrimByte:
972 case Primitive::kPrimChar:
973 case Primitive::kPrimShort:
974 case Primitive::kPrimInt:
975 case Primitive::kPrimNot:
976 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100977 case Primitive::kPrimFloat:
978 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000979 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100980 break;
981
982 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100983 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100984 }
985 }
986}
987
Calin Juravle175dc732015-08-25 15:42:32 +0100988void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
989 DCHECK(location.IsRegister());
990 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
991}
992
Calin Juravlee460d1d2015-09-29 04:52:17 +0100993void CodeGeneratorX86_64::MoveLocation(
994 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
995 Move(dst, src);
996}
997
998void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
999 if (location.IsRegister()) {
1000 locations->AddTemp(location);
1001 } else {
1002 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1003 }
1004}
1005
David Brazdilfc6a86a2015-06-26 10:33:45 +00001006void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001007 DCHECK(!successor->IsExitBlock());
1008
1009 HBasicBlock* block = got->GetBlock();
1010 HInstruction* previous = got->GetPrevious();
1011
1012 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001013 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001014 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1015 return;
1016 }
1017
1018 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1019 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1020 }
1021 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001022 __ jmp(codegen_->GetLabelOf(successor));
1023 }
1024}
1025
David Brazdilfc6a86a2015-06-26 10:33:45 +00001026void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1027 got->SetLocations(nullptr);
1028}
1029
1030void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1031 HandleGoto(got, got->GetSuccessor());
1032}
1033
1034void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1035 try_boundary->SetLocations(nullptr);
1036}
1037
1038void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1039 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1040 if (!successor->IsExitBlock()) {
1041 HandleGoto(try_boundary, successor);
1042 }
1043}
1044
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001045void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1046 exit->SetLocations(nullptr);
1047}
1048
1049void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001050 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001051}
1052
Mark Mendellc4701932015-04-10 13:18:51 -04001053void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
1054 Label* true_label,
1055 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001056 if (cond->IsFPConditionTrueIfNaN()) {
1057 __ j(kUnordered, true_label);
1058 } else if (cond->IsFPConditionFalseIfNaN()) {
1059 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001060 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001061 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001062}
1063
1064void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
1065 HCondition* condition,
1066 Label* true_target,
1067 Label* false_target,
1068 Label* always_true_target) {
1069 LocationSummary* locations = condition->GetLocations();
1070 Location left = locations->InAt(0);
1071 Location right = locations->InAt(1);
1072
1073 // We don't want true_target as a nullptr.
1074 if (true_target == nullptr) {
1075 true_target = always_true_target;
1076 }
1077 bool falls_through = (false_target == nullptr);
1078
1079 // FP compares don't like null false_targets.
1080 if (false_target == nullptr) {
1081 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1082 }
1083
1084 Primitive::Type type = condition->InputAt(0)->GetType();
1085 switch (type) {
1086 case Primitive::kPrimLong: {
1087 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1088 if (right.IsConstant()) {
1089 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1090 if (IsInt<32>(value)) {
1091 if (value == 0) {
1092 __ testq(left_reg, left_reg);
1093 } else {
1094 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1095 }
1096 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001097 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001098 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1099 }
1100 } else if (right.IsDoubleStackSlot()) {
1101 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1102 } else {
1103 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1104 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001105 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001106 break;
1107 }
1108 case Primitive::kPrimFloat: {
1109 if (right.IsFpuRegister()) {
1110 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1111 } else if (right.IsConstant()) {
1112 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1113 codegen_->LiteralFloatAddress(
1114 right.GetConstant()->AsFloatConstant()->GetValue()));
1115 } else {
1116 DCHECK(right.IsStackSlot());
1117 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1118 Address(CpuRegister(RSP), right.GetStackIndex()));
1119 }
1120 GenerateFPJumps(condition, true_target, false_target);
1121 break;
1122 }
1123 case Primitive::kPrimDouble: {
1124 if (right.IsFpuRegister()) {
1125 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1126 } else if (right.IsConstant()) {
1127 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1128 codegen_->LiteralDoubleAddress(
1129 right.GetConstant()->AsDoubleConstant()->GetValue()));
1130 } else {
1131 DCHECK(right.IsDoubleStackSlot());
1132 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1133 Address(CpuRegister(RSP), right.GetStackIndex()));
1134 }
1135 GenerateFPJumps(condition, true_target, false_target);
1136 break;
1137 }
1138 default:
1139 LOG(FATAL) << "Unexpected condition type " << type;
1140 }
1141
1142 if (!falls_through) {
1143 __ jmp(false_target);
1144 }
1145}
1146
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001147void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1148 Label* true_target,
1149 Label* false_target,
1150 Label* always_true_target) {
1151 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001152 if (cond->IsIntConstant()) {
1153 // Constant condition, statically compared against 1.
1154 int32_t cond_value = cond->AsIntConstant()->GetValue();
1155 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001156 if (always_true_target != nullptr) {
1157 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001158 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001159 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001160 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001161 DCHECK_EQ(cond_value, 0);
1162 }
1163 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001164 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001165 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1166 // Moves do not affect the eflags register, so if the condition is
1167 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001168 // again. We can't use the eflags on FP conditions if they are
1169 // materialized due to the complex branching.
1170 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001171 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001172 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1173 && !Primitive::IsFloatingPointType(type);
1174
Roland Levillain4fa13f62015-07-06 18:11:54 +01001175 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001176 if (!eflags_set) {
1177 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001178 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001179 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001180 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001181 } else {
1182 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1183 Immediate(0));
1184 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001185 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001186 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001187 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001188 }
1189 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001190 // Condition has not been materialized, use its inputs as the
1191 // comparison and its condition as the branch condition.
1192
Mark Mendellc4701932015-04-10 13:18:51 -04001193 // Is this a long or FP comparison that has been folded into the HCondition?
1194 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001195 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001196 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1197 true_target, false_target, always_true_target);
1198 return;
1199 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001200
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001201 Location lhs = cond->GetLocations()->InAt(0);
1202 Location rhs = cond->GetLocations()->InAt(1);
1203 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001204 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001205 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001206 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001207 if (constant == 0) {
1208 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1209 } else {
1210 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1211 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001212 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001213 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001214 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1215 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001216 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001217 }
Dave Allison20dfc792014-06-16 20:44:29 -07001218 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001219 if (false_target != nullptr) {
1220 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001221 }
1222}
1223
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001224void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1225 LocationSummary* locations =
1226 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1227 HInstruction* cond = if_instr->InputAt(0);
1228 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1229 locations->SetInAt(0, Location::Any());
1230 }
1231}
1232
1233void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1234 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1235 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1236 Label* always_true_target = true_target;
1237 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1238 if_instr->IfTrueSuccessor())) {
1239 always_true_target = nullptr;
1240 }
1241 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1242 if_instr->IfFalseSuccessor())) {
1243 false_target = nullptr;
1244 }
1245 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1246}
1247
1248void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1249 LocationSummary* locations = new (GetGraph()->GetArena())
1250 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1251 HInstruction* cond = deoptimize->InputAt(0);
1252 DCHECK(cond->IsCondition());
1253 if (cond->AsCondition()->NeedsMaterialization()) {
1254 locations->SetInAt(0, Location::Any());
1255 }
1256}
1257
1258void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001259 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001260 DeoptimizationSlowPathX86_64(deoptimize);
1261 codegen_->AddSlowPath(slow_path);
1262 Label* slow_path_entry = slow_path->GetEntryLabel();
1263 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1264}
1265
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001266void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1267 local->SetLocations(nullptr);
1268}
1269
1270void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1271 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1272}
1273
1274void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1275 local->SetLocations(nullptr);
1276}
1277
1278void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1279 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001280 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001281}
1282
1283void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001284 LocationSummary* locations =
1285 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001286 switch (store->InputAt(1)->GetType()) {
1287 case Primitive::kPrimBoolean:
1288 case Primitive::kPrimByte:
1289 case Primitive::kPrimChar:
1290 case Primitive::kPrimShort:
1291 case Primitive::kPrimInt:
1292 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001293 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001294 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1295 break;
1296
1297 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001298 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001299 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1300 break;
1301
1302 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001303 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001304 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001305}
1306
1307void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001308 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001309}
1310
Roland Levillain0d37cd02015-05-27 16:39:19 +01001311void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001312 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001313 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001314 // Handle the long/FP comparisons made in instruction simplification.
1315 switch (cond->InputAt(0)->GetType()) {
1316 case Primitive::kPrimLong:
1317 locations->SetInAt(0, Location::RequiresRegister());
1318 locations->SetInAt(1, Location::Any());
1319 break;
1320 case Primitive::kPrimFloat:
1321 case Primitive::kPrimDouble:
1322 locations->SetInAt(0, Location::RequiresFpuRegister());
1323 locations->SetInAt(1, Location::Any());
1324 break;
1325 default:
1326 locations->SetInAt(0, Location::RequiresRegister());
1327 locations->SetInAt(1, Location::Any());
1328 break;
1329 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001330 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001331 locations->SetOut(Location::RequiresRegister());
1332 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001333}
1334
Roland Levillain0d37cd02015-05-27 16:39:19 +01001335void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001336 if (!cond->NeedsMaterialization()) {
1337 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001338 }
Mark Mendellc4701932015-04-10 13:18:51 -04001339
1340 LocationSummary* locations = cond->GetLocations();
1341 Location lhs = locations->InAt(0);
1342 Location rhs = locations->InAt(1);
1343 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1344 Label true_label, false_label;
1345
1346 switch (cond->InputAt(0)->GetType()) {
1347 default:
1348 // Integer case.
1349
1350 // Clear output register: setcc only sets the low byte.
1351 __ xorl(reg, reg);
1352
1353 if (rhs.IsRegister()) {
1354 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1355 } else if (rhs.IsConstant()) {
1356 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1357 if (constant == 0) {
1358 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1359 } else {
1360 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1361 }
1362 } else {
1363 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1364 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001365 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001366 return;
1367 case Primitive::kPrimLong:
1368 // Clear output register: setcc only sets the low byte.
1369 __ xorl(reg, reg);
1370
1371 if (rhs.IsRegister()) {
1372 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1373 } else if (rhs.IsConstant()) {
1374 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1375 if (IsInt<32>(value)) {
1376 if (value == 0) {
1377 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1378 } else {
1379 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1380 }
1381 } else {
1382 // Value won't fit in an int.
1383 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1384 }
1385 } else {
1386 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1387 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001388 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001389 return;
1390 case Primitive::kPrimFloat: {
1391 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1392 if (rhs.IsConstant()) {
1393 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1394 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1395 } else if (rhs.IsStackSlot()) {
1396 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1397 } else {
1398 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1399 }
1400 GenerateFPJumps(cond, &true_label, &false_label);
1401 break;
1402 }
1403 case Primitive::kPrimDouble: {
1404 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1405 if (rhs.IsConstant()) {
1406 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1407 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1408 } else if (rhs.IsDoubleStackSlot()) {
1409 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1410 } else {
1411 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1412 }
1413 GenerateFPJumps(cond, &true_label, &false_label);
1414 break;
1415 }
1416 }
1417
1418 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001419 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001420
Roland Levillain4fa13f62015-07-06 18:11:54 +01001421 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001422 __ Bind(&false_label);
1423 __ xorl(reg, reg);
1424 __ jmp(&done_label);
1425
Roland Levillain4fa13f62015-07-06 18:11:54 +01001426 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001427 __ Bind(&true_label);
1428 __ movl(reg, Immediate(1));
1429 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001430}
1431
1432void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1433 VisitCondition(comp);
1434}
1435
1436void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1437 VisitCondition(comp);
1438}
1439
1440void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1441 VisitCondition(comp);
1442}
1443
1444void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1445 VisitCondition(comp);
1446}
1447
1448void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1449 VisitCondition(comp);
1450}
1451
1452void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1453 VisitCondition(comp);
1454}
1455
1456void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1457 VisitCondition(comp);
1458}
1459
1460void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1461 VisitCondition(comp);
1462}
1463
1464void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1465 VisitCondition(comp);
1466}
1467
1468void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1469 VisitCondition(comp);
1470}
1471
1472void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1473 VisitCondition(comp);
1474}
1475
1476void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1477 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001478}
1479
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001480void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001481 LocationSummary* locations =
1482 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001483 switch (compare->InputAt(0)->GetType()) {
1484 case Primitive::kPrimLong: {
1485 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001486 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1488 break;
1489 }
1490 case Primitive::kPrimFloat:
1491 case Primitive::kPrimDouble: {
1492 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001493 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001494 locations->SetOut(Location::RequiresRegister());
1495 break;
1496 }
1497 default:
1498 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1499 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001500}
1501
1502void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001503 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001504 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001505 Location left = locations->InAt(0);
1506 Location right = locations->InAt(1);
1507
Mark Mendell0c9497d2015-08-21 09:30:05 -04001508 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001509 Primitive::Type type = compare->InputAt(0)->GetType();
1510 switch (type) {
1511 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001512 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1513 if (right.IsConstant()) {
1514 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001515 if (IsInt<32>(value)) {
1516 if (value == 0) {
1517 __ testq(left_reg, left_reg);
1518 } else {
1519 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1520 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001521 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001522 // Value won't fit in an int.
1523 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001524 }
Mark Mendell40741f32015-04-20 22:10:34 -04001525 } else if (right.IsDoubleStackSlot()) {
1526 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001527 } else {
1528 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1529 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001530 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001531 }
1532 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001533 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1534 if (right.IsConstant()) {
1535 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1536 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1537 } else if (right.IsStackSlot()) {
1538 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1539 } else {
1540 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1541 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001542 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1543 break;
1544 }
1545 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001546 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1547 if (right.IsConstant()) {
1548 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1549 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1550 } else if (right.IsDoubleStackSlot()) {
1551 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1552 } else {
1553 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1554 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001555 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1556 break;
1557 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001558 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001559 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001560 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001561 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001562 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001563 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001564
Calin Juravle91debbc2014-11-26 19:01:09 +00001565 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001566 __ movl(out, Immediate(1));
1567 __ jmp(&done);
1568
1569 __ Bind(&less);
1570 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001571
1572 __ Bind(&done);
1573}
1574
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001575void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001576 LocationSummary* locations =
1577 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001578 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001579}
1580
1581void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001582 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001583 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001584}
1585
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001586void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1587 LocationSummary* locations =
1588 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1589 locations->SetOut(Location::ConstantLocation(constant));
1590}
1591
1592void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1593 // Will be generated at use site.
1594 UNUSED(constant);
1595}
1596
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001597void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001598 LocationSummary* locations =
1599 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001600 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001601}
1602
1603void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001604 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001605 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001606}
1607
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001608void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1609 LocationSummary* locations =
1610 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1611 locations->SetOut(Location::ConstantLocation(constant));
1612}
1613
1614void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1615 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001616 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001617}
1618
1619void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1620 LocationSummary* locations =
1621 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1622 locations->SetOut(Location::ConstantLocation(constant));
1623}
1624
1625void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1626 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001627 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001628}
1629
Calin Juravle27df7582015-04-17 19:12:31 +01001630void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1631 memory_barrier->SetLocations(nullptr);
1632}
1633
1634void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1635 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1636}
1637
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001638void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1639 ret->SetLocations(nullptr);
1640}
1641
1642void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001643 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001644 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001645}
1646
1647void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001648 LocationSummary* locations =
1649 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001650 switch (ret->InputAt(0)->GetType()) {
1651 case Primitive::kPrimBoolean:
1652 case Primitive::kPrimByte:
1653 case Primitive::kPrimChar:
1654 case Primitive::kPrimShort:
1655 case Primitive::kPrimInt:
1656 case Primitive::kPrimNot:
1657 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001658 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001659 break;
1660
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001661 case Primitive::kPrimFloat:
1662 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001663 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001664 break;
1665
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001666 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001667 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001668 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001669}
1670
1671void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1672 if (kIsDebugBuild) {
1673 switch (ret->InputAt(0)->GetType()) {
1674 case Primitive::kPrimBoolean:
1675 case Primitive::kPrimByte:
1676 case Primitive::kPrimChar:
1677 case Primitive::kPrimShort:
1678 case Primitive::kPrimInt:
1679 case Primitive::kPrimNot:
1680 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001681 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001682 break;
1683
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001684 case Primitive::kPrimFloat:
1685 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001686 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001687 XMM0);
1688 break;
1689
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001690 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001691 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001692 }
1693 }
1694 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001695}
1696
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001697Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1698 switch (type) {
1699 case Primitive::kPrimBoolean:
1700 case Primitive::kPrimByte:
1701 case Primitive::kPrimChar:
1702 case Primitive::kPrimShort:
1703 case Primitive::kPrimInt:
1704 case Primitive::kPrimNot:
1705 case Primitive::kPrimLong:
1706 return Location::RegisterLocation(RAX);
1707
1708 case Primitive::kPrimVoid:
1709 return Location::NoLocation();
1710
1711 case Primitive::kPrimDouble:
1712 case Primitive::kPrimFloat:
1713 return Location::FpuRegisterLocation(XMM0);
1714 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001715
1716 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001717}
1718
1719Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1720 return Location::RegisterLocation(kMethodRegisterArgument);
1721}
1722
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001723Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001724 switch (type) {
1725 case Primitive::kPrimBoolean:
1726 case Primitive::kPrimByte:
1727 case Primitive::kPrimChar:
1728 case Primitive::kPrimShort:
1729 case Primitive::kPrimInt:
1730 case Primitive::kPrimNot: {
1731 uint32_t index = gp_index_++;
1732 stack_index_++;
1733 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001734 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001735 } else {
1736 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1737 }
1738 }
1739
1740 case Primitive::kPrimLong: {
1741 uint32_t index = gp_index_;
1742 stack_index_ += 2;
1743 if (index < calling_convention.GetNumberOfRegisters()) {
1744 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001745 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001746 } else {
1747 gp_index_ += 2;
1748 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1749 }
1750 }
1751
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001752 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001753 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001754 stack_index_++;
1755 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001756 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001757 } else {
1758 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1759 }
1760 }
1761
1762 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001763 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001764 stack_index_ += 2;
1765 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001766 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001767 } else {
1768 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1769 }
1770 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001771
1772 case Primitive::kPrimVoid:
1773 LOG(FATAL) << "Unexpected parameter type " << type;
1774 break;
1775 }
1776 return Location();
1777}
1778
Calin Juravle175dc732015-08-25 15:42:32 +01001779void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1780 // The trampoline uses the same calling convention as dex calling conventions,
1781 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1782 // the method_idx.
1783 HandleInvoke(invoke);
1784}
1785
1786void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1787 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1788}
1789
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001790void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001791 // When we do not run baseline, explicit clinit checks triggered by static
1792 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1793 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001794
Mark Mendellfb8d2792015-03-31 22:16:59 -04001795 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001796 if (intrinsic.TryDispatch(invoke)) {
1797 return;
1798 }
1799
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001800 HandleInvoke(invoke);
1801}
1802
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001803static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1804 if (invoke->GetLocations()->Intrinsified()) {
1805 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1806 intrinsic.Dispatch(invoke);
1807 return true;
1808 }
1809 return false;
1810}
1811
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001812void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001813 // When we do not run baseline, explicit clinit checks triggered by static
1814 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1815 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001816
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001817 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1818 return;
1819 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001820
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001821 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001822 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001823 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001824 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001825}
1826
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001827void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001828 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001829 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001830}
1831
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001832void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001833 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001834 if (intrinsic.TryDispatch(invoke)) {
1835 return;
1836 }
1837
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001838 HandleInvoke(invoke);
1839}
1840
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001841void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001842 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1843 return;
1844 }
1845
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001846 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001847
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001848 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001849 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001850}
1851
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001852void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1853 HandleInvoke(invoke);
1854 // Add the hidden argument.
1855 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1856}
1857
1858void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1859 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001860 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001861 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1862 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001863 LocationSummary* locations = invoke->GetLocations();
1864 Location receiver = locations->InAt(0);
1865 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1866
1867 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001868 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1869 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001870
1871 // temp = object->GetClass();
1872 if (receiver.IsStackSlot()) {
1873 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1874 __ movl(temp, Address(temp, class_offset));
1875 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001876 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001877 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001878 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001879 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001880 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001881 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001882 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001883 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001884 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001885
1886 DCHECK(!codegen_->IsLeafMethod());
1887 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1888}
1889
Roland Levillain88cb1752014-10-20 16:36:47 +01001890void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1891 LocationSummary* locations =
1892 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1893 switch (neg->GetResultType()) {
1894 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001895 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001896 locations->SetInAt(0, Location::RequiresRegister());
1897 locations->SetOut(Location::SameAsFirstInput());
1898 break;
1899
Roland Levillain88cb1752014-10-20 16:36:47 +01001900 case Primitive::kPrimFloat:
1901 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001902 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001903 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001904 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001905 break;
1906
1907 default:
1908 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1909 }
1910}
1911
1912void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1913 LocationSummary* locations = neg->GetLocations();
1914 Location out = locations->Out();
1915 Location in = locations->InAt(0);
1916 switch (neg->GetResultType()) {
1917 case Primitive::kPrimInt:
1918 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001919 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001920 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001921 break;
1922
1923 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001924 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001925 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001926 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001927 break;
1928
Roland Levillain5368c212014-11-27 15:03:41 +00001929 case Primitive::kPrimFloat: {
1930 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001931 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001932 // Implement float negation with an exclusive or with value
1933 // 0x80000000 (mask for bit 31, representing the sign of a
1934 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001935 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001936 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001937 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001938 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001939
Roland Levillain5368c212014-11-27 15:03:41 +00001940 case Primitive::kPrimDouble: {
1941 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001942 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001943 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001944 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001945 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001946 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001947 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001948 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001949 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001950
1951 default:
1952 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1953 }
1954}
1955
Roland Levillaindff1f282014-11-05 14:15:05 +00001956void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1957 LocationSummary* locations =
1958 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1959 Primitive::Type result_type = conversion->GetResultType();
1960 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001961 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001962
David Brazdilb2bd1c52015-03-25 11:17:37 +00001963 // The Java language does not allow treating boolean as an integral type but
1964 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001965
Roland Levillaindff1f282014-11-05 14:15:05 +00001966 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001967 case Primitive::kPrimByte:
1968 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001969 case Primitive::kPrimBoolean:
1970 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001971 case Primitive::kPrimShort:
1972 case Primitive::kPrimInt:
1973 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001974 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001975 locations->SetInAt(0, Location::Any());
1976 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1977 break;
1978
1979 default:
1980 LOG(FATAL) << "Unexpected type conversion from " << input_type
1981 << " to " << result_type;
1982 }
1983 break;
1984
Roland Levillain01a8d712014-11-14 16:27:39 +00001985 case Primitive::kPrimShort:
1986 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001987 case Primitive::kPrimBoolean:
1988 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001989 case Primitive::kPrimByte:
1990 case Primitive::kPrimInt:
1991 case Primitive::kPrimChar:
1992 // Processing a Dex `int-to-short' instruction.
1993 locations->SetInAt(0, Location::Any());
1994 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1995 break;
1996
1997 default:
1998 LOG(FATAL) << "Unexpected type conversion from " << input_type
1999 << " to " << result_type;
2000 }
2001 break;
2002
Roland Levillain946e1432014-11-11 17:35:19 +00002003 case Primitive::kPrimInt:
2004 switch (input_type) {
2005 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002006 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002007 locations->SetInAt(0, Location::Any());
2008 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2009 break;
2010
2011 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002012 // Processing a Dex `float-to-int' instruction.
2013 locations->SetInAt(0, Location::RequiresFpuRegister());
2014 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002015 break;
2016
Roland Levillain946e1432014-11-11 17:35:19 +00002017 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002018 // Processing a Dex `double-to-int' instruction.
2019 locations->SetInAt(0, Location::RequiresFpuRegister());
2020 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002021 break;
2022
2023 default:
2024 LOG(FATAL) << "Unexpected type conversion from " << input_type
2025 << " to " << result_type;
2026 }
2027 break;
2028
Roland Levillaindff1f282014-11-05 14:15:05 +00002029 case Primitive::kPrimLong:
2030 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002031 case Primitive::kPrimBoolean:
2032 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002033 case Primitive::kPrimByte:
2034 case Primitive::kPrimShort:
2035 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002036 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002037 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002038 // TODO: We would benefit from a (to-be-implemented)
2039 // Location::RegisterOrStackSlot requirement for this input.
2040 locations->SetInAt(0, Location::RequiresRegister());
2041 locations->SetOut(Location::RequiresRegister());
2042 break;
2043
2044 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002045 // Processing a Dex `float-to-long' instruction.
2046 locations->SetInAt(0, Location::RequiresFpuRegister());
2047 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002048 break;
2049
Roland Levillaindff1f282014-11-05 14:15:05 +00002050 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002051 // Processing a Dex `double-to-long' instruction.
2052 locations->SetInAt(0, Location::RequiresFpuRegister());
2053 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002054 break;
2055
2056 default:
2057 LOG(FATAL) << "Unexpected type conversion from " << input_type
2058 << " to " << result_type;
2059 }
2060 break;
2061
Roland Levillain981e4542014-11-14 11:47:14 +00002062 case Primitive::kPrimChar:
2063 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002064 case Primitive::kPrimBoolean:
2065 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002066 case Primitive::kPrimByte:
2067 case Primitive::kPrimShort:
2068 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002069 // Processing a Dex `int-to-char' instruction.
2070 locations->SetInAt(0, Location::Any());
2071 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2072 break;
2073
2074 default:
2075 LOG(FATAL) << "Unexpected type conversion from " << input_type
2076 << " to " << result_type;
2077 }
2078 break;
2079
Roland Levillaindff1f282014-11-05 14:15:05 +00002080 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002081 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002082 case Primitive::kPrimBoolean:
2083 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002084 case Primitive::kPrimByte:
2085 case Primitive::kPrimShort:
2086 case Primitive::kPrimInt:
2087 case Primitive::kPrimChar:
2088 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002089 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002090 locations->SetOut(Location::RequiresFpuRegister());
2091 break;
2092
2093 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002094 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002095 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002096 locations->SetOut(Location::RequiresFpuRegister());
2097 break;
2098
Roland Levillaincff13742014-11-17 14:32:17 +00002099 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002100 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002101 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002102 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002103 break;
2104
2105 default:
2106 LOG(FATAL) << "Unexpected type conversion from " << input_type
2107 << " to " << result_type;
2108 };
2109 break;
2110
Roland Levillaindff1f282014-11-05 14:15:05 +00002111 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002112 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002113 case Primitive::kPrimBoolean:
2114 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002115 case Primitive::kPrimByte:
2116 case Primitive::kPrimShort:
2117 case Primitive::kPrimInt:
2118 case Primitive::kPrimChar:
2119 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002120 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002121 locations->SetOut(Location::RequiresFpuRegister());
2122 break;
2123
2124 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002125 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002126 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002127 locations->SetOut(Location::RequiresFpuRegister());
2128 break;
2129
Roland Levillaincff13742014-11-17 14:32:17 +00002130 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002131 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002132 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002133 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002134 break;
2135
2136 default:
2137 LOG(FATAL) << "Unexpected type conversion from " << input_type
2138 << " to " << result_type;
2139 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002140 break;
2141
2142 default:
2143 LOG(FATAL) << "Unexpected type conversion from " << input_type
2144 << " to " << result_type;
2145 }
2146}
2147
2148void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2149 LocationSummary* locations = conversion->GetLocations();
2150 Location out = locations->Out();
2151 Location in = locations->InAt(0);
2152 Primitive::Type result_type = conversion->GetResultType();
2153 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002154 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002155 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002156 case Primitive::kPrimByte:
2157 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002158 case Primitive::kPrimBoolean:
2159 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002160 case Primitive::kPrimShort:
2161 case Primitive::kPrimInt:
2162 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002163 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002164 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002165 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002166 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002167 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002168 Address(CpuRegister(RSP), in.GetStackIndex()));
2169 } else {
2170 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002171 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002172 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2173 }
2174 break;
2175
2176 default:
2177 LOG(FATAL) << "Unexpected type conversion from " << input_type
2178 << " to " << result_type;
2179 }
2180 break;
2181
Roland Levillain01a8d712014-11-14 16:27:39 +00002182 case Primitive::kPrimShort:
2183 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002184 case Primitive::kPrimBoolean:
2185 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002186 case Primitive::kPrimByte:
2187 case Primitive::kPrimInt:
2188 case Primitive::kPrimChar:
2189 // Processing a Dex `int-to-short' instruction.
2190 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002191 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002192 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002193 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002194 Address(CpuRegister(RSP), in.GetStackIndex()));
2195 } else {
2196 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002197 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002198 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2199 }
2200 break;
2201
2202 default:
2203 LOG(FATAL) << "Unexpected type conversion from " << input_type
2204 << " to " << result_type;
2205 }
2206 break;
2207
Roland Levillain946e1432014-11-11 17:35:19 +00002208 case Primitive::kPrimInt:
2209 switch (input_type) {
2210 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002211 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002212 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002213 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002214 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002215 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002216 Address(CpuRegister(RSP), in.GetStackIndex()));
2217 } else {
2218 DCHECK(in.IsConstant());
2219 DCHECK(in.GetConstant()->IsLongConstant());
2220 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002221 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002222 }
2223 break;
2224
Roland Levillain3f8f9362014-12-02 17:45:01 +00002225 case Primitive::kPrimFloat: {
2226 // Processing a Dex `float-to-int' instruction.
2227 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2228 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002229 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002230
2231 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002232 // if input >= (float)INT_MAX goto done
2233 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002234 __ j(kAboveEqual, &done);
2235 // if input == NaN goto nan
2236 __ j(kUnordered, &nan);
2237 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002238 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002239 __ jmp(&done);
2240 __ Bind(&nan);
2241 // output = 0
2242 __ xorl(output, output);
2243 __ Bind(&done);
2244 break;
2245 }
2246
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002247 case Primitive::kPrimDouble: {
2248 // Processing a Dex `double-to-int' instruction.
2249 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2250 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002251 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002252
2253 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002254 // if input >= (double)INT_MAX goto done
2255 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002256 __ j(kAboveEqual, &done);
2257 // if input == NaN goto nan
2258 __ j(kUnordered, &nan);
2259 // output = double-to-int-truncate(input)
2260 __ cvttsd2si(output, input);
2261 __ jmp(&done);
2262 __ Bind(&nan);
2263 // output = 0
2264 __ xorl(output, output);
2265 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002266 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002267 }
Roland Levillain946e1432014-11-11 17:35:19 +00002268
2269 default:
2270 LOG(FATAL) << "Unexpected type conversion from " << input_type
2271 << " to " << result_type;
2272 }
2273 break;
2274
Roland Levillaindff1f282014-11-05 14:15:05 +00002275 case Primitive::kPrimLong:
2276 switch (input_type) {
2277 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002278 case Primitive::kPrimBoolean:
2279 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002280 case Primitive::kPrimByte:
2281 case Primitive::kPrimShort:
2282 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002283 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002284 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002285 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002286 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002287 break;
2288
Roland Levillain624279f2014-12-04 11:54:28 +00002289 case Primitive::kPrimFloat: {
2290 // Processing a Dex `float-to-long' instruction.
2291 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2292 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002293 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002294
Mark Mendell92e83bf2015-05-07 11:25:03 -04002295 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002296 // if input >= (float)LONG_MAX goto done
2297 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002298 __ j(kAboveEqual, &done);
2299 // if input == NaN goto nan
2300 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002301 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002302 __ cvttss2si(output, input, true);
2303 __ jmp(&done);
2304 __ Bind(&nan);
2305 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002306 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002307 __ Bind(&done);
2308 break;
2309 }
2310
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002311 case Primitive::kPrimDouble: {
2312 // Processing a Dex `double-to-long' instruction.
2313 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2314 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002315 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002316
Mark Mendell92e83bf2015-05-07 11:25:03 -04002317 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002318 // if input >= (double)LONG_MAX goto done
2319 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002320 __ j(kAboveEqual, &done);
2321 // if input == NaN goto nan
2322 __ j(kUnordered, &nan);
2323 // output = double-to-long-truncate(input)
2324 __ cvttsd2si(output, input, true);
2325 __ jmp(&done);
2326 __ Bind(&nan);
2327 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002328 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002329 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002330 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002331 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002332
2333 default:
2334 LOG(FATAL) << "Unexpected type conversion from " << input_type
2335 << " to " << result_type;
2336 }
2337 break;
2338
Roland Levillain981e4542014-11-14 11:47:14 +00002339 case Primitive::kPrimChar:
2340 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002341 case Primitive::kPrimBoolean:
2342 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002343 case Primitive::kPrimByte:
2344 case Primitive::kPrimShort:
2345 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002346 // Processing a Dex `int-to-char' instruction.
2347 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002348 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002349 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002351 Address(CpuRegister(RSP), in.GetStackIndex()));
2352 } else {
2353 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002354 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002355 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2356 }
2357 break;
2358
2359 default:
2360 LOG(FATAL) << "Unexpected type conversion from " << input_type
2361 << " to " << result_type;
2362 }
2363 break;
2364
Roland Levillaindff1f282014-11-05 14:15:05 +00002365 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002366 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002367 case Primitive::kPrimBoolean:
2368 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002369 case Primitive::kPrimByte:
2370 case Primitive::kPrimShort:
2371 case Primitive::kPrimInt:
2372 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002373 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002374 if (in.IsRegister()) {
2375 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2376 } else if (in.IsConstant()) {
2377 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2378 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2379 if (v == 0) {
2380 __ xorps(dest, dest);
2381 } else {
2382 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2383 }
2384 } else {
2385 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2386 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2387 }
Roland Levillaincff13742014-11-17 14:32:17 +00002388 break;
2389
2390 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002391 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002392 if (in.IsRegister()) {
2393 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2394 } else if (in.IsConstant()) {
2395 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2396 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2397 if (v == 0) {
2398 __ xorps(dest, dest);
2399 } else {
2400 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2401 }
2402 } else {
2403 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2404 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2405 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002406 break;
2407
Roland Levillaincff13742014-11-17 14:32:17 +00002408 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002409 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002410 if (in.IsFpuRegister()) {
2411 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2412 } else if (in.IsConstant()) {
2413 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2414 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2415 if (bit_cast<int64_t, double>(v) == 0) {
2416 __ xorps(dest, dest);
2417 } else {
2418 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2419 }
2420 } else {
2421 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2422 Address(CpuRegister(RSP), in.GetStackIndex()));
2423 }
Roland Levillaincff13742014-11-17 14:32:17 +00002424 break;
2425
2426 default:
2427 LOG(FATAL) << "Unexpected type conversion from " << input_type
2428 << " to " << result_type;
2429 };
2430 break;
2431
Roland Levillaindff1f282014-11-05 14:15:05 +00002432 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002433 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002434 case Primitive::kPrimBoolean:
2435 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002436 case Primitive::kPrimByte:
2437 case Primitive::kPrimShort:
2438 case Primitive::kPrimInt:
2439 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002440 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002441 if (in.IsRegister()) {
2442 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2443 } else if (in.IsConstant()) {
2444 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2445 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2446 if (v == 0) {
2447 __ xorpd(dest, dest);
2448 } else {
2449 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2450 }
2451 } else {
2452 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2453 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2454 }
Roland Levillaincff13742014-11-17 14:32:17 +00002455 break;
2456
2457 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002458 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002459 if (in.IsRegister()) {
2460 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2461 } else if (in.IsConstant()) {
2462 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2463 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2464 if (v == 0) {
2465 __ xorpd(dest, dest);
2466 } else {
2467 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2468 }
2469 } else {
2470 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2471 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2472 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002473 break;
2474
Roland Levillaincff13742014-11-17 14:32:17 +00002475 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002476 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002477 if (in.IsFpuRegister()) {
2478 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2479 } else if (in.IsConstant()) {
2480 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2481 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2482 if (bit_cast<int32_t, float>(v) == 0) {
2483 __ xorpd(dest, dest);
2484 } else {
2485 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2486 }
2487 } else {
2488 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2489 Address(CpuRegister(RSP), in.GetStackIndex()));
2490 }
Roland Levillaincff13742014-11-17 14:32:17 +00002491 break;
2492
2493 default:
2494 LOG(FATAL) << "Unexpected type conversion from " << input_type
2495 << " to " << result_type;
2496 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected type conversion from " << input_type
2501 << " to " << result_type;
2502 }
2503}
2504
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002505void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002506 LocationSummary* locations =
2507 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002508 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002509 case Primitive::kPrimInt: {
2510 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002511 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2512 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002513 break;
2514 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002515
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002516 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002517 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002518 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002519 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002520 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002521 break;
2522 }
2523
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002524 case Primitive::kPrimDouble:
2525 case Primitive::kPrimFloat: {
2526 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002527 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002528 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002529 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002530 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002531
2532 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002533 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002534 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002535}
2536
2537void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2538 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002539 Location first = locations->InAt(0);
2540 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002541 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002542
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002543 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002544 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002545 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002546 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2547 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002548 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2549 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002550 } else {
2551 __ leal(out.AsRegister<CpuRegister>(), Address(
2552 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2553 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002554 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002555 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2556 __ addl(out.AsRegister<CpuRegister>(),
2557 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2558 } else {
2559 __ leal(out.AsRegister<CpuRegister>(), Address(
2560 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2561 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002562 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002563 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002564 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002565 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002566 break;
2567 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002568
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002569 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002570 if (second.IsRegister()) {
2571 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2572 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002573 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2574 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002575 } else {
2576 __ leaq(out.AsRegister<CpuRegister>(), Address(
2577 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2578 }
2579 } else {
2580 DCHECK(second.IsConstant());
2581 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2582 int32_t int32_value = Low32Bits(value);
2583 DCHECK_EQ(int32_value, value);
2584 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2585 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2586 } else {
2587 __ leaq(out.AsRegister<CpuRegister>(), Address(
2588 first.AsRegister<CpuRegister>(), int32_value));
2589 }
2590 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002591 break;
2592 }
2593
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002594 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002595 if (second.IsFpuRegister()) {
2596 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2597 } else if (second.IsConstant()) {
2598 __ addss(first.AsFpuRegister<XmmRegister>(),
2599 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2600 } else {
2601 DCHECK(second.IsStackSlot());
2602 __ addss(first.AsFpuRegister<XmmRegister>(),
2603 Address(CpuRegister(RSP), second.GetStackIndex()));
2604 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002605 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002606 }
2607
2608 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002609 if (second.IsFpuRegister()) {
2610 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2611 } else if (second.IsConstant()) {
2612 __ addsd(first.AsFpuRegister<XmmRegister>(),
2613 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2614 } else {
2615 DCHECK(second.IsDoubleStackSlot());
2616 __ addsd(first.AsFpuRegister<XmmRegister>(),
2617 Address(CpuRegister(RSP), second.GetStackIndex()));
2618 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002619 break;
2620 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002621
2622 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002623 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002624 }
2625}
2626
2627void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002628 LocationSummary* locations =
2629 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002630 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002631 case Primitive::kPrimInt: {
2632 locations->SetInAt(0, Location::RequiresRegister());
2633 locations->SetInAt(1, Location::Any());
2634 locations->SetOut(Location::SameAsFirstInput());
2635 break;
2636 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002637 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002638 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002639 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002640 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002641 break;
2642 }
Calin Juravle11351682014-10-23 15:38:15 +01002643 case Primitive::kPrimFloat:
2644 case Primitive::kPrimDouble: {
2645 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002646 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002647 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002648 break;
Calin Juravle11351682014-10-23 15:38:15 +01002649 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002650 default:
Calin Juravle11351682014-10-23 15:38:15 +01002651 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002652 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002653}
2654
2655void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2656 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002657 Location first = locations->InAt(0);
2658 Location second = locations->InAt(1);
2659 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002660 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002661 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002662 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002663 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002664 } else if (second.IsConstant()) {
2665 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002666 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002667 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002668 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002669 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002670 break;
2671 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002672 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002673 if (second.IsConstant()) {
2674 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2675 DCHECK(IsInt<32>(value));
2676 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2677 } else {
2678 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2679 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002680 break;
2681 }
2682
Calin Juravle11351682014-10-23 15:38:15 +01002683 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002684 if (second.IsFpuRegister()) {
2685 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2686 } else if (second.IsConstant()) {
2687 __ subss(first.AsFpuRegister<XmmRegister>(),
2688 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2689 } else {
2690 DCHECK(second.IsStackSlot());
2691 __ subss(first.AsFpuRegister<XmmRegister>(),
2692 Address(CpuRegister(RSP), second.GetStackIndex()));
2693 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002694 break;
Calin Juravle11351682014-10-23 15:38:15 +01002695 }
2696
2697 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002698 if (second.IsFpuRegister()) {
2699 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2700 } else if (second.IsConstant()) {
2701 __ subsd(first.AsFpuRegister<XmmRegister>(),
2702 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2703 } else {
2704 DCHECK(second.IsDoubleStackSlot());
2705 __ subsd(first.AsFpuRegister<XmmRegister>(),
2706 Address(CpuRegister(RSP), second.GetStackIndex()));
2707 }
Calin Juravle11351682014-10-23 15:38:15 +01002708 break;
2709 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002710
2711 default:
Calin Juravle11351682014-10-23 15:38:15 +01002712 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002713 }
2714}
2715
Calin Juravle34bacdf2014-10-07 20:23:36 +01002716void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2717 LocationSummary* locations =
2718 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2719 switch (mul->GetResultType()) {
2720 case Primitive::kPrimInt: {
2721 locations->SetInAt(0, Location::RequiresRegister());
2722 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002723 if (mul->InputAt(1)->IsIntConstant()) {
2724 // Can use 3 operand multiply.
2725 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2726 } else {
2727 locations->SetOut(Location::SameAsFirstInput());
2728 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002729 break;
2730 }
2731 case Primitive::kPrimLong: {
2732 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002733 locations->SetInAt(1, Location::Any());
2734 if (mul->InputAt(1)->IsLongConstant() &&
2735 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002736 // Can use 3 operand multiply.
2737 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2738 } else {
2739 locations->SetOut(Location::SameAsFirstInput());
2740 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002741 break;
2742 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002743 case Primitive::kPrimFloat:
2744 case Primitive::kPrimDouble: {
2745 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002746 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002747 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002748 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002749 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002750
2751 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002752 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002753 }
2754}
2755
2756void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2757 LocationSummary* locations = mul->GetLocations();
2758 Location first = locations->InAt(0);
2759 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002760 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002761 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002762 case Primitive::kPrimInt:
2763 // The constant may have ended up in a register, so test explicitly to avoid
2764 // problems where the output may not be the same as the first operand.
2765 if (mul->InputAt(1)->IsIntConstant()) {
2766 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2767 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2768 } else if (second.IsRegister()) {
2769 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002771 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002772 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002773 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002774 __ imull(first.AsRegister<CpuRegister>(),
2775 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002776 }
2777 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002778 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002779 // The constant may have ended up in a register, so test explicitly to avoid
2780 // problems where the output may not be the same as the first operand.
2781 if (mul->InputAt(1)->IsLongConstant()) {
2782 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2783 if (IsInt<32>(value)) {
2784 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2785 Immediate(static_cast<int32_t>(value)));
2786 } else {
2787 // Have to use the constant area.
2788 DCHECK(first.Equals(out));
2789 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2790 }
2791 } else if (second.IsRegister()) {
2792 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002793 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002794 } else {
2795 DCHECK(second.IsDoubleStackSlot());
2796 DCHECK(first.Equals(out));
2797 __ imulq(first.AsRegister<CpuRegister>(),
2798 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002799 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002800 break;
2801 }
2802
Calin Juravleb5bfa962014-10-21 18:02:24 +01002803 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002804 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002805 if (second.IsFpuRegister()) {
2806 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2807 } else if (second.IsConstant()) {
2808 __ mulss(first.AsFpuRegister<XmmRegister>(),
2809 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2810 } else {
2811 DCHECK(second.IsStackSlot());
2812 __ mulss(first.AsFpuRegister<XmmRegister>(),
2813 Address(CpuRegister(RSP), second.GetStackIndex()));
2814 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002815 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002816 }
2817
2818 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002819 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002820 if (second.IsFpuRegister()) {
2821 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2822 } else if (second.IsConstant()) {
2823 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2824 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2825 } else {
2826 DCHECK(second.IsDoubleStackSlot());
2827 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2828 Address(CpuRegister(RSP), second.GetStackIndex()));
2829 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002830 break;
2831 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002832
2833 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002834 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002835 }
2836}
2837
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002838void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2839 uint32_t stack_adjustment, bool is_float) {
2840 if (source.IsStackSlot()) {
2841 DCHECK(is_float);
2842 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2843 } else if (source.IsDoubleStackSlot()) {
2844 DCHECK(!is_float);
2845 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2846 } else {
2847 // Write the value to the temporary location on the stack and load to FP stack.
2848 if (is_float) {
2849 Location stack_temp = Location::StackSlot(temp_offset);
2850 codegen_->Move(stack_temp, source);
2851 __ flds(Address(CpuRegister(RSP), temp_offset));
2852 } else {
2853 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2854 codegen_->Move(stack_temp, source);
2855 __ fldl(Address(CpuRegister(RSP), temp_offset));
2856 }
2857 }
2858}
2859
2860void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2861 Primitive::Type type = rem->GetResultType();
2862 bool is_float = type == Primitive::kPrimFloat;
2863 size_t elem_size = Primitive::ComponentSize(type);
2864 LocationSummary* locations = rem->GetLocations();
2865 Location first = locations->InAt(0);
2866 Location second = locations->InAt(1);
2867 Location out = locations->Out();
2868
2869 // Create stack space for 2 elements.
2870 // TODO: enhance register allocator to ask for stack temporaries.
2871 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2872
2873 // Load the values to the FP stack in reverse order, using temporaries if needed.
2874 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2875 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2876
2877 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002878 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002879 __ Bind(&retry);
2880 __ fprem();
2881
2882 // Move FP status to AX.
2883 __ fstsw();
2884
2885 // And see if the argument reduction is complete. This is signaled by the
2886 // C2 FPU flag bit set to 0.
2887 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2888 __ j(kNotEqual, &retry);
2889
2890 // We have settled on the final value. Retrieve it into an XMM register.
2891 // Store FP top of stack to real stack.
2892 if (is_float) {
2893 __ fsts(Address(CpuRegister(RSP), 0));
2894 } else {
2895 __ fstl(Address(CpuRegister(RSP), 0));
2896 }
2897
2898 // Pop the 2 items from the FP stack.
2899 __ fucompp();
2900
2901 // Load the value from the stack into an XMM register.
2902 DCHECK(out.IsFpuRegister()) << out;
2903 if (is_float) {
2904 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2905 } else {
2906 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2907 }
2908
2909 // And remove the temporary stack space we allocated.
2910 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2911}
2912
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002913void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2914 DCHECK(instruction->IsDiv() || instruction->IsRem());
2915
2916 LocationSummary* locations = instruction->GetLocations();
2917 Location second = locations->InAt(1);
2918 DCHECK(second.IsConstant());
2919
2920 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2921 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002922 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002923
2924 DCHECK(imm == 1 || imm == -1);
2925
2926 switch (instruction->GetResultType()) {
2927 case Primitive::kPrimInt: {
2928 if (instruction->IsRem()) {
2929 __ xorl(output_register, output_register);
2930 } else {
2931 __ movl(output_register, input_register);
2932 if (imm == -1) {
2933 __ negl(output_register);
2934 }
2935 }
2936 break;
2937 }
2938
2939 case Primitive::kPrimLong: {
2940 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002941 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002942 } else {
2943 __ movq(output_register, input_register);
2944 if (imm == -1) {
2945 __ negq(output_register);
2946 }
2947 }
2948 break;
2949 }
2950
2951 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002952 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002953 }
2954}
2955
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002956void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002957 LocationSummary* locations = instruction->GetLocations();
2958 Location second = locations->InAt(1);
2959
2960 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2961 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2962
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002963 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002964
2965 DCHECK(IsPowerOfTwo(std::abs(imm)));
2966
2967 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2968
2969 if (instruction->GetResultType() == Primitive::kPrimInt) {
2970 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2971 __ testl(numerator, numerator);
2972 __ cmov(kGreaterEqual, tmp, numerator);
2973 int shift = CTZ(imm);
2974 __ sarl(tmp, Immediate(shift));
2975
2976 if (imm < 0) {
2977 __ negl(tmp);
2978 }
2979
2980 __ movl(output_register, tmp);
2981 } else {
2982 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2983 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2984
Mark Mendell92e83bf2015-05-07 11:25:03 -04002985 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002986 __ addq(rdx, numerator);
2987 __ testq(numerator, numerator);
2988 __ cmov(kGreaterEqual, rdx, numerator);
2989 int shift = CTZ(imm);
2990 __ sarq(rdx, Immediate(shift));
2991
2992 if (imm < 0) {
2993 __ negq(rdx);
2994 }
2995
2996 __ movq(output_register, rdx);
2997 }
2998}
2999
3000void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3001 DCHECK(instruction->IsDiv() || instruction->IsRem());
3002
3003 LocationSummary* locations = instruction->GetLocations();
3004 Location second = locations->InAt(1);
3005
3006 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3007 : locations->GetTemp(0).AsRegister<CpuRegister>();
3008 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3009 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3010 : locations->Out().AsRegister<CpuRegister>();
3011 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3012
3013 DCHECK_EQ(RAX, eax.AsRegister());
3014 DCHECK_EQ(RDX, edx.AsRegister());
3015 if (instruction->IsDiv()) {
3016 DCHECK_EQ(RAX, out.AsRegister());
3017 } else {
3018 DCHECK_EQ(RDX, out.AsRegister());
3019 }
3020
3021 int64_t magic;
3022 int shift;
3023
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003024 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003025 if (instruction->GetResultType() == Primitive::kPrimInt) {
3026 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3027
3028 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3029
3030 __ movl(numerator, eax);
3031
Mark Mendell0c9497d2015-08-21 09:30:05 -04003032 NearLabel no_div;
3033 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003034 __ testl(eax, eax);
3035 __ j(kNotEqual, &no_div);
3036
3037 __ xorl(out, out);
3038 __ jmp(&end);
3039
3040 __ Bind(&no_div);
3041
3042 __ movl(eax, Immediate(magic));
3043 __ imull(numerator);
3044
3045 if (imm > 0 && magic < 0) {
3046 __ addl(edx, numerator);
3047 } else if (imm < 0 && magic > 0) {
3048 __ subl(edx, numerator);
3049 }
3050
3051 if (shift != 0) {
3052 __ sarl(edx, Immediate(shift));
3053 }
3054
3055 __ movl(eax, edx);
3056 __ shrl(edx, Immediate(31));
3057 __ addl(edx, eax);
3058
3059 if (instruction->IsRem()) {
3060 __ movl(eax, numerator);
3061 __ imull(edx, Immediate(imm));
3062 __ subl(eax, edx);
3063 __ movl(edx, eax);
3064 } else {
3065 __ movl(eax, edx);
3066 }
3067 __ Bind(&end);
3068 } else {
3069 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3070
3071 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3072
3073 CpuRegister rax = eax;
3074 CpuRegister rdx = edx;
3075
3076 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3077
3078 // Save the numerator.
3079 __ movq(numerator, rax);
3080
3081 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003082 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003083
3084 // RDX:RAX = magic * numerator
3085 __ imulq(numerator);
3086
3087 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003088 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003089 __ addq(rdx, numerator);
3090 } else if (imm < 0 && magic > 0) {
3091 // RDX -= numerator
3092 __ subq(rdx, numerator);
3093 }
3094
3095 // Shift if needed.
3096 if (shift != 0) {
3097 __ sarq(rdx, Immediate(shift));
3098 }
3099
3100 // RDX += 1 if RDX < 0
3101 __ movq(rax, rdx);
3102 __ shrq(rdx, Immediate(63));
3103 __ addq(rdx, rax);
3104
3105 if (instruction->IsRem()) {
3106 __ movq(rax, numerator);
3107
3108 if (IsInt<32>(imm)) {
3109 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3110 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003111 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003112 }
3113
3114 __ subq(rax, rdx);
3115 __ movq(rdx, rax);
3116 } else {
3117 __ movq(rax, rdx);
3118 }
3119 }
3120}
3121
Calin Juravlebacfec32014-11-14 15:54:36 +00003122void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3123 DCHECK(instruction->IsDiv() || instruction->IsRem());
3124 Primitive::Type type = instruction->GetResultType();
3125 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3126
3127 bool is_div = instruction->IsDiv();
3128 LocationSummary* locations = instruction->GetLocations();
3129
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003130 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3131 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003132
Roland Levillain271ab9c2014-11-27 15:23:57 +00003133 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003134 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003135
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003136 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003137 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003138
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003139 if (imm == 0) {
3140 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3141 } else if (imm == 1 || imm == -1) {
3142 DivRemOneOrMinusOne(instruction);
3143 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003144 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003145 } else {
3146 DCHECK(imm <= -2 || imm >= 2);
3147 GenerateDivRemWithAnyConstant(instruction);
3148 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003149 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003150 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003151 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3152 out.AsRegister(), type, is_div);
3153 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003154
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003155 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3156 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3157 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3158 // so it's safe to just use negl instead of more complex comparisons.
3159 if (type == Primitive::kPrimInt) {
3160 __ cmpl(second_reg, Immediate(-1));
3161 __ j(kEqual, slow_path->GetEntryLabel());
3162 // edx:eax <- sign-extended of eax
3163 __ cdq();
3164 // eax = quotient, edx = remainder
3165 __ idivl(second_reg);
3166 } else {
3167 __ cmpq(second_reg, Immediate(-1));
3168 __ j(kEqual, slow_path->GetEntryLabel());
3169 // rdx:rax <- sign-extended of rax
3170 __ cqo();
3171 // rax = quotient, rdx = remainder
3172 __ idivq(second_reg);
3173 }
3174 __ Bind(slow_path->GetExitLabel());
3175 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003176}
3177
Calin Juravle7c4954d2014-10-28 16:57:40 +00003178void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3179 LocationSummary* locations =
3180 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3181 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003182 case Primitive::kPrimInt:
3183 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003184 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003185 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003186 locations->SetOut(Location::SameAsFirstInput());
3187 // Intel uses edx:eax as the dividend.
3188 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003189 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3190 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3191 // output and request another temp.
3192 if (div->InputAt(1)->IsConstant()) {
3193 locations->AddTemp(Location::RequiresRegister());
3194 }
Calin Juravled0d48522014-11-04 16:40:20 +00003195 break;
3196 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003197
Calin Juravle7c4954d2014-10-28 16:57:40 +00003198 case Primitive::kPrimFloat:
3199 case Primitive::kPrimDouble: {
3200 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003201 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003202 locations->SetOut(Location::SameAsFirstInput());
3203 break;
3204 }
3205
3206 default:
3207 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3208 }
3209}
3210
3211void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3212 LocationSummary* locations = div->GetLocations();
3213 Location first = locations->InAt(0);
3214 Location second = locations->InAt(1);
3215 DCHECK(first.Equals(locations->Out()));
3216
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003217 Primitive::Type type = div->GetResultType();
3218 switch (type) {
3219 case Primitive::kPrimInt:
3220 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003221 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003222 break;
3223 }
3224
Calin Juravle7c4954d2014-10-28 16:57:40 +00003225 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003226 if (second.IsFpuRegister()) {
3227 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3228 } else if (second.IsConstant()) {
3229 __ divss(first.AsFpuRegister<XmmRegister>(),
3230 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3231 } else {
3232 DCHECK(second.IsStackSlot());
3233 __ divss(first.AsFpuRegister<XmmRegister>(),
3234 Address(CpuRegister(RSP), second.GetStackIndex()));
3235 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003236 break;
3237 }
3238
3239 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003240 if (second.IsFpuRegister()) {
3241 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3242 } else if (second.IsConstant()) {
3243 __ divsd(first.AsFpuRegister<XmmRegister>(),
3244 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3245 } else {
3246 DCHECK(second.IsDoubleStackSlot());
3247 __ divsd(first.AsFpuRegister<XmmRegister>(),
3248 Address(CpuRegister(RSP), second.GetStackIndex()));
3249 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003250 break;
3251 }
3252
3253 default:
3254 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3255 }
3256}
3257
Calin Juravlebacfec32014-11-14 15:54:36 +00003258void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003259 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003260 LocationSummary* locations =
3261 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003262
3263 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003264 case Primitive::kPrimInt:
3265 case Primitive::kPrimLong: {
3266 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003267 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003268 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3269 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003270 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3271 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3272 // output and request another temp.
3273 if (rem->InputAt(1)->IsConstant()) {
3274 locations->AddTemp(Location::RequiresRegister());
3275 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003276 break;
3277 }
3278
3279 case Primitive::kPrimFloat:
3280 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003281 locations->SetInAt(0, Location::Any());
3282 locations->SetInAt(1, Location::Any());
3283 locations->SetOut(Location::RequiresFpuRegister());
3284 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003285 break;
3286 }
3287
3288 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003289 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003290 }
3291}
3292
3293void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3294 Primitive::Type type = rem->GetResultType();
3295 switch (type) {
3296 case Primitive::kPrimInt:
3297 case Primitive::kPrimLong: {
3298 GenerateDivRemIntegral(rem);
3299 break;
3300 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003301 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003302 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003303 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003304 break;
3305 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003306 default:
3307 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3308 }
3309}
3310
Calin Juravled0d48522014-11-04 16:40:20 +00003311void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003312 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3313 ? LocationSummary::kCallOnSlowPath
3314 : LocationSummary::kNoCall;
3315 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003316 locations->SetInAt(0, Location::Any());
3317 if (instruction->HasUses()) {
3318 locations->SetOut(Location::SameAsFirstInput());
3319 }
3320}
3321
3322void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003323 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003324 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3325 codegen_->AddSlowPath(slow_path);
3326
3327 LocationSummary* locations = instruction->GetLocations();
3328 Location value = locations->InAt(0);
3329
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003330 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003331 case Primitive::kPrimByte:
3332 case Primitive::kPrimChar:
3333 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003334 case Primitive::kPrimInt: {
3335 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003336 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003337 __ j(kEqual, slow_path->GetEntryLabel());
3338 } else if (value.IsStackSlot()) {
3339 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3340 __ j(kEqual, slow_path->GetEntryLabel());
3341 } else {
3342 DCHECK(value.IsConstant()) << value;
3343 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3344 __ jmp(slow_path->GetEntryLabel());
3345 }
3346 }
3347 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003348 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003349 case Primitive::kPrimLong: {
3350 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003351 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003352 __ j(kEqual, slow_path->GetEntryLabel());
3353 } else if (value.IsDoubleStackSlot()) {
3354 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3355 __ j(kEqual, slow_path->GetEntryLabel());
3356 } else {
3357 DCHECK(value.IsConstant()) << value;
3358 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3359 __ jmp(slow_path->GetEntryLabel());
3360 }
3361 }
3362 break;
3363 }
3364 default:
3365 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003366 }
Calin Juravled0d48522014-11-04 16:40:20 +00003367}
3368
Calin Juravle9aec02f2014-11-18 23:06:35 +00003369void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3370 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3371
3372 LocationSummary* locations =
3373 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3374
3375 switch (op->GetResultType()) {
3376 case Primitive::kPrimInt:
3377 case Primitive::kPrimLong: {
3378 locations->SetInAt(0, Location::RequiresRegister());
3379 // The shift count needs to be in CL.
3380 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3381 locations->SetOut(Location::SameAsFirstInput());
3382 break;
3383 }
3384 default:
3385 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3386 }
3387}
3388
3389void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3390 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3391
3392 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003393 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003394 Location second = locations->InAt(1);
3395
3396 switch (op->GetResultType()) {
3397 case Primitive::kPrimInt: {
3398 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003399 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003400 if (op->IsShl()) {
3401 __ shll(first_reg, second_reg);
3402 } else if (op->IsShr()) {
3403 __ sarl(first_reg, second_reg);
3404 } else {
3405 __ shrl(first_reg, second_reg);
3406 }
3407 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003408 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003409 if (op->IsShl()) {
3410 __ shll(first_reg, imm);
3411 } else if (op->IsShr()) {
3412 __ sarl(first_reg, imm);
3413 } else {
3414 __ shrl(first_reg, imm);
3415 }
3416 }
3417 break;
3418 }
3419 case Primitive::kPrimLong: {
3420 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003421 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003422 if (op->IsShl()) {
3423 __ shlq(first_reg, second_reg);
3424 } else if (op->IsShr()) {
3425 __ sarq(first_reg, second_reg);
3426 } else {
3427 __ shrq(first_reg, second_reg);
3428 }
3429 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003430 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003431 if (op->IsShl()) {
3432 __ shlq(first_reg, imm);
3433 } else if (op->IsShr()) {
3434 __ sarq(first_reg, imm);
3435 } else {
3436 __ shrq(first_reg, imm);
3437 }
3438 }
3439 break;
3440 }
3441 default:
3442 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3443 }
3444}
3445
3446void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3447 HandleShift(shl);
3448}
3449
3450void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3451 HandleShift(shl);
3452}
3453
3454void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3455 HandleShift(shr);
3456}
3457
3458void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3459 HandleShift(shr);
3460}
3461
3462void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3463 HandleShift(ushr);
3464}
3465
3466void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3467 HandleShift(ushr);
3468}
3469
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003470void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003471 LocationSummary* locations =
3472 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003473 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003474 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003475 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003476 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003477}
3478
3479void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3480 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003481 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3482 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003483 // Note: if heap poisoning is enabled, the entry point takes cares
3484 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003485
Calin Juravle175dc732015-08-25 15:42:32 +01003486 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3487 instruction,
3488 instruction->GetDexPc(),
3489 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003490
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003491 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003492}
3493
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003494void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3495 LocationSummary* locations =
3496 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3497 InvokeRuntimeCallingConvention calling_convention;
3498 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003499 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003500 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003501 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003502}
3503
3504void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3505 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003506 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3507 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003508
Roland Levillain4d027112015-07-01 15:41:14 +01003509 // Note: if heap poisoning is enabled, the entry point takes cares
3510 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003511 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3512 instruction,
3513 instruction->GetDexPc(),
3514 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003515
3516 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003517}
3518
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003519void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003520 LocationSummary* locations =
3521 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003522 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3523 if (location.IsStackSlot()) {
3524 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3525 } else if (location.IsDoubleStackSlot()) {
3526 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3527 }
3528 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003529}
3530
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003531void InstructionCodeGeneratorX86_64::VisitParameterValue(
3532 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003533 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003534}
3535
3536void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3537 LocationSummary* locations =
3538 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3539 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3540}
3541
3542void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3543 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3544 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003545}
3546
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003547void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003548 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003549 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003550 locations->SetInAt(0, Location::RequiresRegister());
3551 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003552}
3553
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003554void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3555 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003556 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3557 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003558 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003559 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003560 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003561 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003562 break;
3563
3564 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003565 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003566 break;
3567
3568 default:
3569 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3570 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003571}
3572
David Brazdil66d126e2015-04-03 16:02:44 +01003573void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3574 LocationSummary* locations =
3575 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3576 locations->SetInAt(0, Location::RequiresRegister());
3577 locations->SetOut(Location::SameAsFirstInput());
3578}
3579
3580void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003581 LocationSummary* locations = bool_not->GetLocations();
3582 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3583 locations->Out().AsRegister<CpuRegister>().AsRegister());
3584 Location out = locations->Out();
3585 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3586}
3587
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003588void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003589 LocationSummary* locations =
3590 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003591 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3592 locations->SetInAt(i, Location::Any());
3593 }
3594 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003595}
3596
3597void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003598 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003599 LOG(FATAL) << "Unimplemented";
3600}
3601
Calin Juravle52c48962014-12-16 17:02:57 +00003602void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3603 /*
3604 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3605 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3606 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3607 */
3608 switch (kind) {
3609 case MemBarrierKind::kAnyAny: {
3610 __ mfence();
3611 break;
3612 }
3613 case MemBarrierKind::kAnyStore:
3614 case MemBarrierKind::kLoadAny:
3615 case MemBarrierKind::kStoreStore: {
3616 // nop
3617 break;
3618 }
3619 default:
3620 LOG(FATAL) << "Unexpected memory barier " << kind;
3621 }
3622}
3623
3624void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3625 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3626
Nicolas Geoffray39468442014-09-02 15:17:15 +01003627 LocationSummary* locations =
3628 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003629 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003630 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3631 locations->SetOut(Location::RequiresFpuRegister());
3632 } else {
3633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3634 }
Calin Juravle52c48962014-12-16 17:02:57 +00003635}
3636
3637void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3638 const FieldInfo& field_info) {
3639 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3640
3641 LocationSummary* locations = instruction->GetLocations();
3642 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3643 Location out = locations->Out();
3644 bool is_volatile = field_info.IsVolatile();
3645 Primitive::Type field_type = field_info.GetFieldType();
3646 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3647
3648 switch (field_type) {
3649 case Primitive::kPrimBoolean: {
3650 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3651 break;
3652 }
3653
3654 case Primitive::kPrimByte: {
3655 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3656 break;
3657 }
3658
3659 case Primitive::kPrimShort: {
3660 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3661 break;
3662 }
3663
3664 case Primitive::kPrimChar: {
3665 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3666 break;
3667 }
3668
3669 case Primitive::kPrimInt:
3670 case Primitive::kPrimNot: {
3671 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3672 break;
3673 }
3674
3675 case Primitive::kPrimLong: {
3676 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3677 break;
3678 }
3679
3680 case Primitive::kPrimFloat: {
3681 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3682 break;
3683 }
3684
3685 case Primitive::kPrimDouble: {
3686 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3687 break;
3688 }
3689
3690 case Primitive::kPrimVoid:
3691 LOG(FATAL) << "Unreachable type " << field_type;
3692 UNREACHABLE();
3693 }
3694
Calin Juravle77520bc2015-01-12 18:45:46 +00003695 codegen_->MaybeRecordImplicitNullCheck(instruction);
3696
Calin Juravle52c48962014-12-16 17:02:57 +00003697 if (is_volatile) {
3698 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3699 }
Roland Levillain4d027112015-07-01 15:41:14 +01003700
3701 if (field_type == Primitive::kPrimNot) {
3702 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3703 }
Calin Juravle52c48962014-12-16 17:02:57 +00003704}
3705
3706void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3707 const FieldInfo& field_info) {
3708 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3709
3710 LocationSummary* locations =
3711 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003712 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003713 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003714 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003715
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003716 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003717 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3718 locations->SetInAt(1, Location::RequiresFpuRegister());
3719 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003720 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003721 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003722 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003723 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003724 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003725 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003726 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3727 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003728 locations->AddTemp(Location::RequiresRegister());
3729 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003730}
3731
Calin Juravle52c48962014-12-16 17:02:57 +00003732void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003733 const FieldInfo& field_info,
3734 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003735 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3736
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003737 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003738 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3739 Location value = locations->InAt(1);
3740 bool is_volatile = field_info.IsVolatile();
3741 Primitive::Type field_type = field_info.GetFieldType();
3742 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3743
3744 if (is_volatile) {
3745 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3746 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003747
3748 switch (field_type) {
3749 case Primitive::kPrimBoolean:
3750 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003751 if (value.IsConstant()) {
3752 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3753 __ movb(Address(base, offset), Immediate(v));
3754 } else {
3755 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3756 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003757 break;
3758 }
3759
3760 case Primitive::kPrimShort:
3761 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003762 if (value.IsConstant()) {
3763 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3764 __ movw(Address(base, offset), Immediate(v));
3765 } else {
3766 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3767 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003768 break;
3769 }
3770
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003772 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003773 if (value.IsConstant()) {
3774 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003775 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3776 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3777 // Note: if heap poisoning is enabled, no need to poison
3778 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003779 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003780 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003781 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3782 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3783 __ movl(temp, value.AsRegister<CpuRegister>());
3784 __ PoisonHeapReference(temp);
3785 __ movl(Address(base, offset), temp);
3786 } else {
3787 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3788 }
Mark Mendell40741f32015-04-20 22:10:34 -04003789 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003790 break;
3791 }
3792
3793 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003794 if (value.IsConstant()) {
3795 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3796 DCHECK(IsInt<32>(v));
3797 int32_t v_32 = v;
3798 __ movq(Address(base, offset), Immediate(v_32));
3799 } else {
3800 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3801 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003802 break;
3803 }
3804
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003805 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003806 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003807 break;
3808 }
3809
3810 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003811 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003812 break;
3813 }
3814
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003815 case Primitive::kPrimVoid:
3816 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003817 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003818 }
Calin Juravle52c48962014-12-16 17:02:57 +00003819
Calin Juravle77520bc2015-01-12 18:45:46 +00003820 codegen_->MaybeRecordImplicitNullCheck(instruction);
3821
3822 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3823 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3824 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003825 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003826 }
3827
Calin Juravle52c48962014-12-16 17:02:57 +00003828 if (is_volatile) {
3829 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3830 }
3831}
3832
3833void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3834 HandleFieldSet(instruction, instruction->GetFieldInfo());
3835}
3836
3837void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003838 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003839}
3840
3841void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003842 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003843}
3844
3845void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003846 HandleFieldGet(instruction, instruction->GetFieldInfo());
3847}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003848
Calin Juravle52c48962014-12-16 17:02:57 +00003849void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3850 HandleFieldGet(instruction);
3851}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003852
Calin Juravle52c48962014-12-16 17:02:57 +00003853void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3854 HandleFieldGet(instruction, instruction->GetFieldInfo());
3855}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003856
Calin Juravle52c48962014-12-16 17:02:57 +00003857void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3858 HandleFieldSet(instruction, instruction->GetFieldInfo());
3859}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003860
Calin Juravle52c48962014-12-16 17:02:57 +00003861void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003862 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003863}
3864
Calin Juravlee460d1d2015-09-29 04:52:17 +01003865void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
3866 HUnresolvedInstanceFieldGet* instruction) {
3867 FieldAccessCallingConventionX86_64 calling_convention;
3868 codegen_->CreateUnresolvedFieldLocationSummary(
3869 instruction, instruction->GetFieldType(), calling_convention);
3870}
3871
3872void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
3873 HUnresolvedInstanceFieldGet* instruction) {
3874 FieldAccessCallingConventionX86_64 calling_convention;
3875 codegen_->GenerateUnresolvedFieldAccess(instruction,
3876 instruction->GetFieldType(),
3877 instruction->GetFieldIndex(),
3878 instruction->GetDexPc(),
3879 calling_convention);
3880}
3881
3882void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
3883 HUnresolvedInstanceFieldSet* instruction) {
3884 FieldAccessCallingConventionX86_64 calling_convention;
3885 codegen_->CreateUnresolvedFieldLocationSummary(
3886 instruction, instruction->GetFieldType(), calling_convention);
3887}
3888
3889void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
3890 HUnresolvedInstanceFieldSet* instruction) {
3891 FieldAccessCallingConventionX86_64 calling_convention;
3892 codegen_->GenerateUnresolvedFieldAccess(instruction,
3893 instruction->GetFieldType(),
3894 instruction->GetFieldIndex(),
3895 instruction->GetDexPc(),
3896 calling_convention);
3897}
3898
3899void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
3900 HUnresolvedStaticFieldGet* instruction) {
3901 FieldAccessCallingConventionX86_64 calling_convention;
3902 codegen_->CreateUnresolvedFieldLocationSummary(
3903 instruction, instruction->GetFieldType(), calling_convention);
3904}
3905
3906void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
3907 HUnresolvedStaticFieldGet* instruction) {
3908 FieldAccessCallingConventionX86_64 calling_convention;
3909 codegen_->GenerateUnresolvedFieldAccess(instruction,
3910 instruction->GetFieldType(),
3911 instruction->GetFieldIndex(),
3912 instruction->GetDexPc(),
3913 calling_convention);
3914}
3915
3916void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
3917 HUnresolvedStaticFieldSet* instruction) {
3918 FieldAccessCallingConventionX86_64 calling_convention;
3919 codegen_->CreateUnresolvedFieldLocationSummary(
3920 instruction, instruction->GetFieldType(), calling_convention);
3921}
3922
3923void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
3924 HUnresolvedStaticFieldSet* instruction) {
3925 FieldAccessCallingConventionX86_64 calling_convention;
3926 codegen_->GenerateUnresolvedFieldAccess(instruction,
3927 instruction->GetFieldType(),
3928 instruction->GetFieldIndex(),
3929 instruction->GetDexPc(),
3930 calling_convention);
3931}
3932
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003933void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003934 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3935 ? LocationSummary::kCallOnSlowPath
3936 : LocationSummary::kNoCall;
3937 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3938 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003939 ? Location::RequiresRegister()
3940 : Location::Any();
3941 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003942 if (instruction->HasUses()) {
3943 locations->SetOut(Location::SameAsFirstInput());
3944 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003945}
3946
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003947void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003948 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3949 return;
3950 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003951 LocationSummary* locations = instruction->GetLocations();
3952 Location obj = locations->InAt(0);
3953
3954 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3955 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3956}
3957
3958void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003959 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003960 codegen_->AddSlowPath(slow_path);
3961
3962 LocationSummary* locations = instruction->GetLocations();
3963 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003964
3965 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003966 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003967 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003968 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003969 } else {
3970 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00003971 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003972 __ jmp(slow_path->GetEntryLabel());
3973 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003974 }
3975 __ j(kEqual, slow_path->GetEntryLabel());
3976}
3977
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003978void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003979 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003980 GenerateImplicitNullCheck(instruction);
3981 } else {
3982 GenerateExplicitNullCheck(instruction);
3983 }
3984}
3985
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003986void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003987 LocationSummary* locations =
3988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003989 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003990 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003991 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3992 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3993 } else {
3994 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3995 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003996}
3997
3998void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3999 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004000 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004001 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004002 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004003
Roland Levillain4d027112015-07-01 15:41:14 +01004004 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004005 case Primitive::kPrimBoolean: {
4006 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004007 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004008 if (index.IsConstant()) {
4009 __ movzxb(out, Address(obj,
4010 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4011 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004012 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004013 }
4014 break;
4015 }
4016
4017 case Primitive::kPrimByte: {
4018 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004019 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004020 if (index.IsConstant()) {
4021 __ movsxb(out, Address(obj,
4022 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4023 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004024 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004025 }
4026 break;
4027 }
4028
4029 case Primitive::kPrimShort: {
4030 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004031 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004032 if (index.IsConstant()) {
4033 __ movsxw(out, Address(obj,
4034 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4035 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004036 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004037 }
4038 break;
4039 }
4040
4041 case Primitive::kPrimChar: {
4042 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004044 if (index.IsConstant()) {
4045 __ movzxw(out, Address(obj,
4046 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4047 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004048 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004049 }
4050 break;
4051 }
4052
4053 case Primitive::kPrimInt:
4054 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01004055 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4056 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004057 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004058 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004059 if (index.IsConstant()) {
4060 __ movl(out, Address(obj,
4061 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4062 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004063 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004064 }
4065 break;
4066 }
4067
4068 case Primitive::kPrimLong: {
4069 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004070 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004071 if (index.IsConstant()) {
4072 __ movq(out, Address(obj,
4073 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4074 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004076 }
4077 break;
4078 }
4079
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004080 case Primitive::kPrimFloat: {
4081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004082 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004083 if (index.IsConstant()) {
4084 __ movss(out, Address(obj,
4085 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4086 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004087 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004088 }
4089 break;
4090 }
4091
4092 case Primitive::kPrimDouble: {
4093 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004094 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004095 if (index.IsConstant()) {
4096 __ movsd(out, Address(obj,
4097 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4098 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004099 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004100 }
4101 break;
4102 }
4103
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004104 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004105 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004106 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004107 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004108 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004109
4110 if (type == Primitive::kPrimNot) {
4111 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4112 __ MaybeUnpoisonHeapReference(out);
4113 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004114}
4115
4116void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004117 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004118
4119 bool needs_write_barrier =
4120 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004121 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004122
Nicolas Geoffray39468442014-09-02 15:17:15 +01004123 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004124 instruction,
4125 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004126
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004127 locations->SetInAt(0, Location::RequiresRegister());
4128 locations->SetInAt(
4129 1, Location::RegisterOrConstant(instruction->InputAt(1)));
4130 locations->SetInAt(2, Location::RequiresRegister());
4131 if (value_type == Primitive::kPrimLong) {
4132 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
4133 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
4134 locations->SetInAt(2, Location::RequiresFpuRegister());
4135 } else {
4136 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4137 }
4138
4139 if (needs_write_barrier) {
4140 // Temporary registers for the write barrier.
4141 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4142 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004143 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004144}
4145
4146void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4147 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004148 CpuRegister array = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004149 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004150 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004151 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004152 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004153 bool needs_write_barrier =
4154 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004155 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4156 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4157 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004158
4159 switch (value_type) {
4160 case Primitive::kPrimBoolean:
4161 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004162 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4163 Address address = index.IsConstant()
4164 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4165 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4166 if (value.IsRegister()) {
4167 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004168 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004169 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004170 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004171 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004172 break;
4173 }
4174
4175 case Primitive::kPrimShort:
4176 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004177 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4178 Address address = index.IsConstant()
4179 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4180 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4181 if (value.IsRegister()) {
4182 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004183 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004184 DCHECK(value.IsConstant()) << value;
4185 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004186 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004187 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004188 break;
4189 }
4190
4191 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004192 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4193 Address address = index.IsConstant()
4194 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4195 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4196 if (!value.IsRegister()) {
4197 // Just setting null.
4198 DCHECK(instruction->InputAt(2)->IsNullConstant());
4199 DCHECK(value.IsConstant()) << value;
4200 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004201 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004202 DCHECK(!needs_write_barrier);
4203 DCHECK(!may_need_runtime_call);
4204 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004205 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004206
4207 DCHECK(needs_write_barrier);
4208 CpuRegister register_value = value.AsRegister<CpuRegister>();
4209 NearLabel done, not_null, do_put;
4210 SlowPathCode* slow_path = nullptr;
4211 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4212 if (may_need_runtime_call) {
4213 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4214 codegen_->AddSlowPath(slow_path);
4215 if (instruction->GetValueCanBeNull()) {
4216 __ testl(register_value, register_value);
4217 __ j(kNotEqual, &not_null);
4218 __ movl(address, Immediate(0));
4219 codegen_->MaybeRecordImplicitNullCheck(instruction);
4220 __ jmp(&done);
4221 __ Bind(&not_null);
4222 }
4223
4224 __ movl(temp, Address(array, class_offset));
4225 codegen_->MaybeRecordImplicitNullCheck(instruction);
4226 __ MaybeUnpoisonHeapReference(temp);
4227 __ movl(temp, Address(temp, component_offset));
4228 // No need to poison/unpoison, we're comparing two poisoned references.
4229 __ cmpl(temp, Address(register_value, class_offset));
4230 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4231 __ j(kEqual, &do_put);
4232 __ MaybeUnpoisonHeapReference(temp);
4233 __ movl(temp, Address(temp, super_offset));
4234 // No need to unpoison the result, we're comparing against null.
4235 __ testl(temp, temp);
4236 __ j(kNotEqual, slow_path->GetEntryLabel());
4237 __ Bind(&do_put);
4238 } else {
4239 __ j(kNotEqual, slow_path->GetEntryLabel());
4240 }
4241 }
4242
4243 if (kPoisonHeapReferences) {
4244 __ movl(temp, register_value);
4245 __ PoisonHeapReference(temp);
4246 __ movl(address, temp);
4247 } else {
4248 __ movl(address, register_value);
4249 }
4250 if (!may_need_runtime_call) {
4251 codegen_->MaybeRecordImplicitNullCheck(instruction);
4252 }
4253
4254 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4255 codegen_->MarkGCCard(
4256 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4257 __ Bind(&done);
4258
4259 if (slow_path != nullptr) {
4260 __ Bind(slow_path->GetExitLabel());
4261 }
4262
4263 break;
4264 }
4265 case Primitive::kPrimInt: {
4266 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4267 Address address = index.IsConstant()
4268 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4269 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4270 if (value.IsRegister()) {
4271 __ movl(address, value.AsRegister<CpuRegister>());
4272 } else {
4273 DCHECK(value.IsConstant()) << value;
4274 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4275 __ movl(address, Immediate(v));
4276 }
4277 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004278 break;
4279 }
4280
4281 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004282 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4283 Address address = index.IsConstant()
4284 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4285 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4286 if (value.IsRegister()) {
4287 __ movq(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004288 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004289 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4290 DCHECK(IsInt<32>(v));
4291 int32_t v_32 = v;
4292 __ movq(address, Immediate(v_32));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004293 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004294 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004295 break;
4296 }
4297
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004298 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004299 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4300 Address address = index.IsConstant()
4301 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4302 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4303 DCHECK(value.IsFpuRegister());
4304 __ movss(address, value.AsFpuRegister<XmmRegister>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004305 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004306 break;
4307 }
4308
4309 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004310 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4311 Address address = index.IsConstant()
4312 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4313 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4314 DCHECK(value.IsFpuRegister());
4315 __ movsd(address, value.AsFpuRegister<XmmRegister>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004316 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004317 break;
4318 }
4319
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004320 case Primitive::kPrimVoid:
4321 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004322 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004323 }
4324}
4325
4326void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004327 LocationSummary* locations =
4328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004329 locations->SetInAt(0, Location::RequiresRegister());
4330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004331}
4332
4333void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4334 LocationSummary* locations = instruction->GetLocations();
4335 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004336 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4337 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004338 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004339 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004340}
4341
4342void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004343 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4344 ? LocationSummary::kCallOnSlowPath
4345 : LocationSummary::kNoCall;
4346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004347 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004348 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004349 if (instruction->HasUses()) {
4350 locations->SetOut(Location::SameAsFirstInput());
4351 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004352}
4353
4354void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4355 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004356 Location index_loc = locations->InAt(0);
4357 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004358 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004359 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004360
Mark Mendell99dbd682015-04-22 16:18:52 -04004361 if (length_loc.IsConstant()) {
4362 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4363 if (index_loc.IsConstant()) {
4364 // BCE will remove the bounds check if we are guarenteed to pass.
4365 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4366 if (index < 0 || index >= length) {
4367 codegen_->AddSlowPath(slow_path);
4368 __ jmp(slow_path->GetEntryLabel());
4369 } else {
4370 // Some optimization after BCE may have generated this, and we should not
4371 // generate a bounds check if it is a valid range.
4372 }
4373 return;
4374 }
4375
4376 // We have to reverse the jump condition because the length is the constant.
4377 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4378 __ cmpl(index_reg, Immediate(length));
4379 codegen_->AddSlowPath(slow_path);
4380 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004381 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004382 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4383 if (index_loc.IsConstant()) {
4384 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4385 __ cmpl(length, Immediate(value));
4386 } else {
4387 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4388 }
4389 codegen_->AddSlowPath(slow_path);
4390 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004391 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004392}
4393
4394void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4395 CpuRegister card,
4396 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004397 CpuRegister value,
4398 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004399 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004400 if (value_can_be_null) {
4401 __ testl(value, value);
4402 __ j(kEqual, &is_null);
4403 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004404 __ gs()->movq(card, Address::Absolute(
4405 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4406 __ movq(temp, object);
4407 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004408 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004409 if (value_can_be_null) {
4410 __ Bind(&is_null);
4411 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004412}
4413
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004414void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4415 temp->SetLocations(nullptr);
4416}
4417
4418void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4419 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004420 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004421}
4422
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004423void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004424 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004425 LOG(FATAL) << "Unimplemented";
4426}
4427
4428void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004429 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4430}
4431
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004432void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4433 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4434}
4435
4436void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004437 HBasicBlock* block = instruction->GetBlock();
4438 if (block->GetLoopInformation() != nullptr) {
4439 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4440 // The back edge will generate the suspend check.
4441 return;
4442 }
4443 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4444 // The goto will generate the suspend check.
4445 return;
4446 }
4447 GenerateSuspendCheck(instruction, nullptr);
4448}
4449
4450void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4451 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004452 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004453 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4454 if (slow_path == nullptr) {
4455 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4456 instruction->SetSlowPath(slow_path);
4457 codegen_->AddSlowPath(slow_path);
4458 if (successor != nullptr) {
4459 DCHECK(successor->IsLoopHeader());
4460 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4461 }
4462 } else {
4463 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4464 }
4465
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004466 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004467 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004468 if (successor == nullptr) {
4469 __ j(kNotEqual, slow_path->GetEntryLabel());
4470 __ Bind(slow_path->GetReturnLabel());
4471 } else {
4472 __ j(kEqual, codegen_->GetLabelOf(successor));
4473 __ jmp(slow_path->GetEntryLabel());
4474 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004475}
4476
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004477X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4478 return codegen_->GetAssembler();
4479}
4480
4481void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004482 DCHECK_LT(index, moves_.size());
4483 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004484 Location source = move->GetSource();
4485 Location destination = move->GetDestination();
4486
4487 if (source.IsRegister()) {
4488 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004489 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004490 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004491 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004492 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004493 } else {
4494 DCHECK(destination.IsDoubleStackSlot());
4495 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004496 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004497 }
4498 } else if (source.IsStackSlot()) {
4499 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004500 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004501 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004502 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004503 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004504 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004505 } else {
4506 DCHECK(destination.IsStackSlot());
4507 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4508 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4509 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004510 } else if (source.IsDoubleStackSlot()) {
4511 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004512 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004513 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004514 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004515 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4516 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004517 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004518 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004519 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4520 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4521 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004522 } else if (source.IsConstant()) {
4523 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004524 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4525 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004526 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004527 if (value == 0) {
4528 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4529 } else {
4530 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4531 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004532 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004533 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004534 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004535 }
4536 } else if (constant->IsLongConstant()) {
4537 int64_t value = constant->AsLongConstant()->GetValue();
4538 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004539 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004540 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004541 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004542 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004543 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004544 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004545 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004546 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004547 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004548 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4549 if (value == 0) {
4550 // easy FP 0.0.
4551 __ xorps(dest, dest);
4552 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004553 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004554 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004555 } else {
4556 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004557 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004558 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4559 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004560 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004561 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004562 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004563 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004564 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004565 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4566 if (value == 0) {
4567 __ xorpd(dest, dest);
4568 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004569 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004570 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004571 } else {
4572 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004573 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004574 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004575 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004576 } else if (source.IsFpuRegister()) {
4577 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004578 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004579 } else if (destination.IsStackSlot()) {
4580 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004581 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004582 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004583 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004584 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004585 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004586 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004587 }
4588}
4589
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004590void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004591 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004592 __ movl(Address(CpuRegister(RSP), mem), reg);
4593 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004594}
4595
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004596void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004597 ScratchRegisterScope ensure_scratch(
4598 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4599
4600 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4601 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4602 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4603 Address(CpuRegister(RSP), mem2 + stack_offset));
4604 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4605 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4606 CpuRegister(ensure_scratch.GetRegister()));
4607}
4608
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004609void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4610 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4611 __ movq(Address(CpuRegister(RSP), mem), reg);
4612 __ movq(reg, CpuRegister(TMP));
4613}
4614
4615void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4616 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004617 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004618
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004619 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4620 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4621 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4622 Address(CpuRegister(RSP), mem2 + stack_offset));
4623 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4624 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4625 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004626}
4627
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004628void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4629 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4630 __ movss(Address(CpuRegister(RSP), mem), reg);
4631 __ movd(reg, CpuRegister(TMP));
4632}
4633
4634void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4635 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4636 __ movsd(Address(CpuRegister(RSP), mem), reg);
4637 __ movd(reg, CpuRegister(TMP));
4638}
4639
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004640void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004641 DCHECK_LT(index, moves_.size());
4642 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004643 Location source = move->GetSource();
4644 Location destination = move->GetDestination();
4645
4646 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004647 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004648 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004649 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004650 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004651 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004652 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004653 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4654 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004655 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004656 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004657 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004658 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4659 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004660 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004661 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4662 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4663 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004664 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004665 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004666 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004667 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004668 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004669 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004670 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004671 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004672 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004673 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004674 }
4675}
4676
4677
4678void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4679 __ pushq(CpuRegister(reg));
4680}
4681
4682
4683void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4684 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004685}
4686
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004687void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004688 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004689 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4690 Immediate(mirror::Class::kStatusInitialized));
4691 __ j(kLess, slow_path->GetEntryLabel());
4692 __ Bind(slow_path->GetExitLabel());
4693 // No need for memory fence, thanks to the X86_64 memory model.
4694}
4695
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004696void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004697 InvokeRuntimeCallingConvention calling_convention;
4698 CodeGenerator::CreateLoadClassLocationSummary(
4699 cls,
4700 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4701 Location::RegisterLocation(RAX));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004702}
4703
4704void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004705 LocationSummary* locations = cls->GetLocations();
4706 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4707 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle98893e12015-10-02 21:05:03 +01004708 if (cls->NeedsAccessCheck()) {
4709 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4710 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4711 cls,
4712 cls->GetDexPc(),
4713 nullptr);
4714 } else if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004715 DCHECK(!cls->CanCallRuntime());
4716 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004717 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004718 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004719 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004720 __ movq(out, Address(
4721 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004722 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004723 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004724
Andreas Gampe85b62f22015-09-09 13:15:38 -07004725 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004726 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4727 codegen_->AddSlowPath(slow_path);
4728 __ testl(out, out);
4729 __ j(kEqual, slow_path->GetEntryLabel());
4730 if (cls->MustGenerateClinitCheck()) {
4731 GenerateClassInitializationCheck(slow_path, out);
4732 } else {
4733 __ Bind(slow_path->GetExitLabel());
4734 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004735 }
4736}
4737
4738void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4739 LocationSummary* locations =
4740 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4741 locations->SetInAt(0, Location::RequiresRegister());
4742 if (check->HasUses()) {
4743 locations->SetOut(Location::SameAsFirstInput());
4744 }
4745}
4746
4747void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004748 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004749 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004750 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004751 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004752 GenerateClassInitializationCheck(slow_path,
4753 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004754}
4755
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004756void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4757 LocationSummary* locations =
4758 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004759 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004760 locations->SetOut(Location::RequiresRegister());
4761}
4762
4763void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004764 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004765 codegen_->AddSlowPath(slow_path);
4766
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004767 LocationSummary* locations = load->GetLocations();
4768 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4769 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004770 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004771 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004772 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004773 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004774 __ testl(out, out);
4775 __ j(kEqual, slow_path->GetEntryLabel());
4776 __ Bind(slow_path->GetExitLabel());
4777}
4778
David Brazdilcb1c0552015-08-04 16:22:25 +01004779static Address GetExceptionTlsAddress() {
4780 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4781}
4782
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004783void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4784 LocationSummary* locations =
4785 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4786 locations->SetOut(Location::RequiresRegister());
4787}
4788
4789void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004790 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4791}
4792
4793void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4794 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4795}
4796
4797void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4798 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004799}
4800
4801void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4802 LocationSummary* locations =
4803 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4804 InvokeRuntimeCallingConvention calling_convention;
4805 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4806}
4807
4808void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004809 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4810 instruction,
4811 instruction->GetDexPc(),
4812 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004813}
4814
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004815void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004816 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4817 switch (instruction->GetTypeCheckKind()) {
4818 case TypeCheckKind::kExactCheck:
4819 case TypeCheckKind::kAbstractClassCheck:
4820 case TypeCheckKind::kClassHierarchyCheck:
4821 case TypeCheckKind::kArrayObjectCheck:
4822 call_kind = LocationSummary::kNoCall;
4823 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004824 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004825 case TypeCheckKind::kInterfaceCheck:
4826 call_kind = LocationSummary::kCall;
4827 break;
4828 case TypeCheckKind::kArrayCheck:
4829 call_kind = LocationSummary::kCallOnSlowPath;
4830 break;
4831 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004832 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004833 if (call_kind != LocationSummary::kCall) {
4834 locations->SetInAt(0, Location::RequiresRegister());
4835 locations->SetInAt(1, Location::Any());
4836 // Note that TypeCheckSlowPathX86_64 uses this register too.
4837 locations->SetOut(Location::RequiresRegister());
4838 } else {
4839 InvokeRuntimeCallingConvention calling_convention;
4840 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4841 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4842 locations->SetOut(Location::RegisterLocation(RAX));
4843 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004844}
4845
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004846void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004847 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004848 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004849 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004850 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004851 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004852 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4853 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4854 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004855 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004856 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004857
4858 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004859 // Avoid null check if we know obj is not null.
4860 if (instruction->MustDoNullCheck()) {
4861 __ testl(obj, obj);
4862 __ j(kEqual, &zero);
4863 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004864
Calin Juravle98893e12015-10-02 21:05:03 +01004865 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004866 // This is safe, as the register is caller-save, and the object must be in another
4867 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004868 CpuRegister target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4869 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004870 ? obj
4871 : out;
4872 __ movl(target, Address(obj, class_offset));
4873 __ MaybeUnpoisonHeapReference(target);
4874
4875 switch (instruction->GetTypeCheckKind()) {
4876 case TypeCheckKind::kExactCheck: {
4877 if (cls.IsRegister()) {
4878 __ cmpl(out, cls.AsRegister<CpuRegister>());
4879 } else {
4880 DCHECK(cls.IsStackSlot()) << cls;
4881 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4882 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004883 if (zero.IsLinked()) {
4884 // Classes must be equal for the instanceof to succeed.
4885 __ j(kNotEqual, &zero);
4886 __ movl(out, Immediate(1));
4887 __ jmp(&done);
4888 } else {
4889 __ setcc(kEqual, out);
4890 // setcc only sets the low byte.
4891 __ andl(out, Immediate(1));
4892 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004893 break;
4894 }
4895 case TypeCheckKind::kAbstractClassCheck: {
4896 // If the class is abstract, we eagerly fetch the super class of the
4897 // object to avoid doing a comparison we know will fail.
4898 NearLabel loop, success;
4899 __ Bind(&loop);
4900 __ movl(out, Address(out, super_offset));
4901 __ MaybeUnpoisonHeapReference(out);
4902 __ testl(out, out);
4903 // If `out` is null, we use it for the result, and jump to `done`.
4904 __ j(kEqual, &done);
4905 if (cls.IsRegister()) {
4906 __ cmpl(out, cls.AsRegister<CpuRegister>());
4907 } else {
4908 DCHECK(cls.IsStackSlot()) << cls;
4909 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4910 }
4911 __ j(kNotEqual, &loop);
4912 __ movl(out, Immediate(1));
4913 if (zero.IsLinked()) {
4914 __ jmp(&done);
4915 }
4916 break;
4917 }
4918 case TypeCheckKind::kClassHierarchyCheck: {
4919 // Walk over the class hierarchy to find a match.
4920 NearLabel loop, success;
4921 __ Bind(&loop);
4922 if (cls.IsRegister()) {
4923 __ cmpl(out, cls.AsRegister<CpuRegister>());
4924 } else {
4925 DCHECK(cls.IsStackSlot()) << cls;
4926 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4927 }
4928 __ j(kEqual, &success);
4929 __ movl(out, Address(out, super_offset));
4930 __ MaybeUnpoisonHeapReference(out);
4931 __ testl(out, out);
4932 __ j(kNotEqual, &loop);
4933 // If `out` is null, we use it for the result, and jump to `done`.
4934 __ jmp(&done);
4935 __ Bind(&success);
4936 __ movl(out, Immediate(1));
4937 if (zero.IsLinked()) {
4938 __ jmp(&done);
4939 }
4940 break;
4941 }
4942 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004943 // Do an exact check.
4944 NearLabel exact_check;
4945 if (cls.IsRegister()) {
4946 __ cmpl(out, cls.AsRegister<CpuRegister>());
4947 } else {
4948 DCHECK(cls.IsStackSlot()) << cls;
4949 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4950 }
4951 __ j(kEqual, &exact_check);
4952 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004953 __ movl(out, Address(out, component_offset));
4954 __ MaybeUnpoisonHeapReference(out);
4955 __ testl(out, out);
4956 // If `out` is null, we use it for the result, and jump to `done`.
4957 __ j(kEqual, &done);
4958 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
4959 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004960 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004961 __ movl(out, Immediate(1));
4962 __ jmp(&done);
4963 break;
4964 }
4965 case TypeCheckKind::kArrayCheck: {
4966 if (cls.IsRegister()) {
4967 __ cmpl(out, cls.AsRegister<CpuRegister>());
4968 } else {
4969 DCHECK(cls.IsStackSlot()) << cls;
4970 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4971 }
4972 DCHECK(locations->OnlyCallsOnSlowPath());
4973 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4974 instruction, /* is_fatal */ false);
4975 codegen_->AddSlowPath(slow_path);
4976 __ j(kNotEqual, slow_path->GetEntryLabel());
4977 __ movl(out, Immediate(1));
4978 if (zero.IsLinked()) {
4979 __ jmp(&done);
4980 }
4981 break;
4982 }
Calin Juravle98893e12015-10-02 21:05:03 +01004983 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004984 case TypeCheckKind::kInterfaceCheck:
4985 default: {
4986 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4987 instruction,
4988 instruction->GetDexPc(),
4989 nullptr);
4990 if (zero.IsLinked()) {
4991 __ jmp(&done);
4992 }
4993 break;
4994 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004995 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004996
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004997 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004998 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004999 __ xorl(out, out);
5000 }
5001
5002 if (done.IsLinked()) {
5003 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005004 }
5005
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005006 if (slow_path != nullptr) {
5007 __ Bind(slow_path->GetExitLabel());
5008 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005009}
5010
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005011void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005012 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5013 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5014
5015 switch (instruction->GetTypeCheckKind()) {
5016 case TypeCheckKind::kExactCheck:
5017 case TypeCheckKind::kAbstractClassCheck:
5018 case TypeCheckKind::kClassHierarchyCheck:
5019 case TypeCheckKind::kArrayObjectCheck:
5020 call_kind = throws_into_catch
5021 ? LocationSummary::kCallOnSlowPath
5022 : LocationSummary::kNoCall;
5023 break;
Calin Juravle98893e12015-10-02 21:05:03 +01005024 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005025 case TypeCheckKind::kInterfaceCheck:
5026 call_kind = LocationSummary::kCall;
5027 break;
5028 case TypeCheckKind::kArrayCheck:
5029 call_kind = LocationSummary::kCallOnSlowPath;
5030 break;
5031 }
5032
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005033 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005034 instruction, call_kind);
5035 if (call_kind != LocationSummary::kCall) {
5036 locations->SetInAt(0, Location::RequiresRegister());
5037 locations->SetInAt(1, Location::Any());
5038 // Note that TypeCheckSlowPathX86_64 uses this register too.
5039 locations->AddTemp(Location::RequiresRegister());
5040 } else {
5041 InvokeRuntimeCallingConvention calling_convention;
5042 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5043 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5044 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005045}
5046
5047void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
5048 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005049 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005050 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005051 CpuRegister temp = locations->WillCall()
5052 ? CpuRegister(kNoRegister)
5053 : locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005054
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005055 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5056 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5057 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5058 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5059 SlowPathCode* slow_path = nullptr;
5060
5061 if (!locations->WillCall()) {
5062 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
5063 instruction, !locations->CanCall());
5064 codegen_->AddSlowPath(slow_path);
5065 }
5066
5067 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005068 // Avoid null check if we know obj is not null.
5069 if (instruction->MustDoNullCheck()) {
5070 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005071 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005072 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005073
5074 if (locations->WillCall()) {
5075 __ movl(obj, Address(obj, class_offset));
5076 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005077 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005078 __ movl(temp, Address(obj, class_offset));
5079 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005080 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005081
5082 switch (instruction->GetTypeCheckKind()) {
5083 case TypeCheckKind::kExactCheck:
5084 case TypeCheckKind::kArrayCheck: {
5085 if (cls.IsRegister()) {
5086 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5087 } else {
5088 DCHECK(cls.IsStackSlot()) << cls;
5089 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5090 }
5091 // Jump to slow path for throwing the exception or doing a
5092 // more involved array check.
5093 __ j(kNotEqual, slow_path->GetEntryLabel());
5094 break;
5095 }
5096 case TypeCheckKind::kAbstractClassCheck: {
5097 // If the class is abstract, we eagerly fetch the super class of the
5098 // object to avoid doing a comparison we know will fail.
5099 NearLabel loop;
5100 __ Bind(&loop);
5101 __ movl(temp, Address(temp, super_offset));
5102 __ MaybeUnpoisonHeapReference(temp);
5103 __ testl(temp, temp);
5104 // Jump to the slow path to throw the exception.
5105 __ j(kEqual, slow_path->GetEntryLabel());
5106 if (cls.IsRegister()) {
5107 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5108 } else {
5109 DCHECK(cls.IsStackSlot()) << cls;
5110 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5111 }
5112 __ j(kNotEqual, &loop);
5113 break;
5114 }
5115 case TypeCheckKind::kClassHierarchyCheck: {
5116 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005117 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005118 __ Bind(&loop);
5119 if (cls.IsRegister()) {
5120 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5121 } else {
5122 DCHECK(cls.IsStackSlot()) << cls;
5123 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5124 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005125 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005126 __ movl(temp, Address(temp, super_offset));
5127 __ MaybeUnpoisonHeapReference(temp);
5128 __ testl(temp, temp);
5129 __ j(kNotEqual, &loop);
5130 // Jump to the slow path to throw the exception.
5131 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005132 break;
5133 }
5134 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005135 // Do an exact check.
5136 if (cls.IsRegister()) {
5137 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5138 } else {
5139 DCHECK(cls.IsStackSlot()) << cls;
5140 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5141 }
5142 __ j(kEqual, &done);
5143 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005144 __ movl(temp, Address(temp, component_offset));
5145 __ MaybeUnpoisonHeapReference(temp);
5146 __ testl(temp, temp);
5147 __ j(kEqual, slow_path->GetEntryLabel());
5148 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5149 __ j(kNotEqual, slow_path->GetEntryLabel());
5150 break;
5151 }
Calin Juravle98893e12015-10-02 21:05:03 +01005152 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005153 case TypeCheckKind::kInterfaceCheck:
5154 default:
5155 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5156 instruction,
5157 instruction->GetDexPc(),
5158 nullptr);
5159 break;
5160 }
5161 __ Bind(&done);
5162
5163 if (slow_path != nullptr) {
5164 __ Bind(slow_path->GetExitLabel());
5165 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005166}
5167
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005168void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5169 LocationSummary* locations =
5170 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5171 InvokeRuntimeCallingConvention calling_convention;
5172 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5173}
5174
5175void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005176 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5177 : QUICK_ENTRY_POINT(pUnlockObject),
5178 instruction,
5179 instruction->GetDexPc(),
5180 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005181}
5182
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005183void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5184void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5185void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5186
5187void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5188 LocationSummary* locations =
5189 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5190 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5191 || instruction->GetResultType() == Primitive::kPrimLong);
5192 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005193 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005194 locations->SetOut(Location::SameAsFirstInput());
5195}
5196
5197void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5198 HandleBitwiseOperation(instruction);
5199}
5200
5201void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5202 HandleBitwiseOperation(instruction);
5203}
5204
5205void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5206 HandleBitwiseOperation(instruction);
5207}
5208
5209void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5210 LocationSummary* locations = instruction->GetLocations();
5211 Location first = locations->InAt(0);
5212 Location second = locations->InAt(1);
5213 DCHECK(first.Equals(locations->Out()));
5214
5215 if (instruction->GetResultType() == Primitive::kPrimInt) {
5216 if (second.IsRegister()) {
5217 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005218 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005219 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005220 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005221 } else {
5222 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005223 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005224 }
5225 } else if (second.IsConstant()) {
5226 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5227 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005228 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005229 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005230 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005231 } else {
5232 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005233 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005234 }
5235 } else {
5236 Address address(CpuRegister(RSP), second.GetStackIndex());
5237 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005238 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005239 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005240 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005241 } else {
5242 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005243 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005244 }
5245 }
5246 } else {
5247 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005248 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5249 bool second_is_constant = false;
5250 int64_t value = 0;
5251 if (second.IsConstant()) {
5252 second_is_constant = true;
5253 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005254 }
Mark Mendell40741f32015-04-20 22:10:34 -04005255 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005256
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005257 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005258 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005259 if (is_int32_value) {
5260 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5261 } else {
5262 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5263 }
5264 } else if (second.IsDoubleStackSlot()) {
5265 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005266 } else {
5267 __ andq(first_reg, second.AsRegister<CpuRegister>());
5268 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005269 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005270 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005271 if (is_int32_value) {
5272 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5273 } else {
5274 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5275 }
5276 } else if (second.IsDoubleStackSlot()) {
5277 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005278 } else {
5279 __ orq(first_reg, second.AsRegister<CpuRegister>());
5280 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005281 } else {
5282 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005283 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005284 if (is_int32_value) {
5285 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5286 } else {
5287 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5288 }
5289 } else if (second.IsDoubleStackSlot()) {
5290 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005291 } else {
5292 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5293 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005294 }
5295 }
5296}
5297
Calin Juravleb1498f62015-02-16 13:13:29 +00005298void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
5299 // Nothing to do, this should be removed during prepare for register allocator.
5300 UNUSED(instruction);
5301 LOG(FATAL) << "Unreachable";
5302}
5303
5304void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
5305 // Nothing to do, this should be removed during prepare for register allocator.
5306 UNUSED(instruction);
5307 LOG(FATAL) << "Unreachable";
5308}
5309
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005310void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
5311 DCHECK(codegen_->IsBaseline());
5312 LocationSummary* locations =
5313 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5314 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5315}
5316
5317void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5318 DCHECK(codegen_->IsBaseline());
5319 // Will be generated at use site.
5320}
5321
Mark Mendellfe57faa2015-09-18 09:26:15 -04005322// Simple implementation of packed switch - generate cascaded compare/jumps.
5323void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5324 LocationSummary* locations =
5325 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5326 locations->SetInAt(0, Location::RequiresRegister());
5327}
5328
5329void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5330 int32_t lower_bound = switch_instr->GetStartValue();
5331 int32_t num_entries = switch_instr->GetNumEntries();
5332 LocationSummary* locations = switch_instr->GetLocations();
5333 CpuRegister value_reg = locations->InAt(0).AsRegister<CpuRegister>();
5334 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5335
5336 // Create a series of compare/jumps.
5337 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5338 for (int i = 0; i < num_entries; i++) {
5339 int32_t case_value = lower_bound + i;
5340 if (case_value == 0) {
5341 __ testl(value_reg, value_reg);
5342 } else {
5343 __ cmpl(value_reg, Immediate(case_value));
5344 }
5345 __ j(kEqual, codegen_->GetLabelOf(successors.at(i)));
5346 }
5347
5348 // And the default for any other value.
5349 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5350 __ jmp(codegen_->GetLabelOf(default_block));
5351 }
5352}
5353
Mark Mendell92e83bf2015-05-07 11:25:03 -04005354void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
5355 if (value == 0) {
5356 __ xorl(dest, dest);
5357 } else if (value > 0 && IsInt<32>(value)) {
5358 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
5359 __ movl(dest, Immediate(static_cast<int32_t>(value)));
5360 } else {
5361 __ movq(dest, Immediate(value));
5362 }
5363}
5364
Mark Mendellcfa410b2015-05-25 16:02:44 -04005365void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
5366 DCHECK(dest.IsDoubleStackSlot());
5367 if (IsInt<32>(value)) {
5368 // Can move directly as an int32 constant.
5369 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
5370 Immediate(static_cast<int32_t>(value)));
5371 } else {
5372 Load64BitValue(CpuRegister(TMP), value);
5373 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
5374 }
5375}
5376
Mark Mendellf55c3e02015-03-26 21:07:46 -04005377void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
5378 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04005379 X86_64Assembler* assembler = GetAssembler();
5380 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04005381 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5382 // byte values. If used for vectors at a later time, this will need to be
5383 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04005384 assembler->Align(4, 0);
5385 constant_area_start_ = assembler->CodeSize();
5386 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04005387 }
5388
5389 // And finish up.
5390 CodeGenerator::Finalize(allocator);
5391}
5392
5393/**
5394 * Class to handle late fixup of offsets into constant area.
5395 */
Vladimir Marko5233f932015-09-29 19:01:15 +01005396class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendellf55c3e02015-03-26 21:07:46 -04005397 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04005398 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04005399 : codegen_(codegen), offset_into_constant_area_(offset) {}
5400
5401 private:
5402 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5403 // Patch the correct offset for the instruction. We use the address of the
5404 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
5405 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5406 int relative_position = constant_offset - pos;
5407
5408 // Patch in the right value.
5409 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5410 }
5411
Mark Mendell39dcf552015-04-09 20:42:42 -04005412 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04005413
5414 // Location in constant area that the fixup refers to.
5415 int offset_into_constant_area_;
5416};
5417
5418Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
5419 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5420 return Address::RIP(fixup);
5421}
5422
5423Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
5424 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5425 return Address::RIP(fixup);
5426}
5427
5428Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
5429 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5430 return Address::RIP(fixup);
5431}
5432
5433Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
5434 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5435 return Address::RIP(fixup);
5436}
5437
Andreas Gampe85b62f22015-09-09 13:15:38 -07005438// TODO: trg as memory.
5439void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5440 if (!trg.IsValid()) {
5441 DCHECK(type == Primitive::kPrimVoid);
5442 return;
5443 }
5444
5445 DCHECK_NE(type, Primitive::kPrimVoid);
5446
5447 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
5448 if (trg.Equals(return_loc)) {
5449 return;
5450 }
5451
5452 // Let the parallel move resolver take care of all of this.
5453 HParallelMove parallel_move(GetGraph()->GetArena());
5454 parallel_move.AddMove(return_loc, trg, type, nullptr);
5455 GetMoveResolver()->EmitNativeCode(&parallel_move);
5456}
5457
Roland Levillain4d027112015-07-01 15:41:14 +01005458#undef __
5459
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005460} // namespace x86_64
5461} // namespace art