blob: 3299db4ca7a2dc64d5defc2d80fe9bdfd79d3bc5 [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),
674 method_patches_(graph->GetArena()->Adapter()),
675 relative_call_patches_(graph->GetArena()->Adapter()),
676 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
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
David Brazdilfc6a86a2015-06-26 10:33:45 +0000993void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100994 DCHECK(!successor->IsExitBlock());
995
996 HBasicBlock* block = got->GetBlock();
997 HInstruction* previous = got->GetPrevious();
998
999 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001000 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001001 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1002 return;
1003 }
1004
1005 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1006 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1007 }
1008 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001009 __ jmp(codegen_->GetLabelOf(successor));
1010 }
1011}
1012
David Brazdilfc6a86a2015-06-26 10:33:45 +00001013void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1014 got->SetLocations(nullptr);
1015}
1016
1017void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1018 HandleGoto(got, got->GetSuccessor());
1019}
1020
1021void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1022 try_boundary->SetLocations(nullptr);
1023}
1024
1025void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1026 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1027 if (!successor->IsExitBlock()) {
1028 HandleGoto(try_boundary, successor);
1029 }
1030}
1031
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001032void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1033 exit->SetLocations(nullptr);
1034}
1035
1036void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001037 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001038}
1039
Mark Mendellc4701932015-04-10 13:18:51 -04001040void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
1041 Label* true_label,
1042 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001043 if (cond->IsFPConditionTrueIfNaN()) {
1044 __ j(kUnordered, true_label);
1045 } else if (cond->IsFPConditionFalseIfNaN()) {
1046 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001047 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001048 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001049}
1050
1051void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
1052 HCondition* condition,
1053 Label* true_target,
1054 Label* false_target,
1055 Label* always_true_target) {
1056 LocationSummary* locations = condition->GetLocations();
1057 Location left = locations->InAt(0);
1058 Location right = locations->InAt(1);
1059
1060 // We don't want true_target as a nullptr.
1061 if (true_target == nullptr) {
1062 true_target = always_true_target;
1063 }
1064 bool falls_through = (false_target == nullptr);
1065
1066 // FP compares don't like null false_targets.
1067 if (false_target == nullptr) {
1068 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1069 }
1070
1071 Primitive::Type type = condition->InputAt(0)->GetType();
1072 switch (type) {
1073 case Primitive::kPrimLong: {
1074 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1075 if (right.IsConstant()) {
1076 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1077 if (IsInt<32>(value)) {
1078 if (value == 0) {
1079 __ testq(left_reg, left_reg);
1080 } else {
1081 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1082 }
1083 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001084 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001085 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1086 }
1087 } else if (right.IsDoubleStackSlot()) {
1088 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1089 } else {
1090 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1091 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001092 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001093 break;
1094 }
1095 case Primitive::kPrimFloat: {
1096 if (right.IsFpuRegister()) {
1097 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1098 } else if (right.IsConstant()) {
1099 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1100 codegen_->LiteralFloatAddress(
1101 right.GetConstant()->AsFloatConstant()->GetValue()));
1102 } else {
1103 DCHECK(right.IsStackSlot());
1104 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1105 Address(CpuRegister(RSP), right.GetStackIndex()));
1106 }
1107 GenerateFPJumps(condition, true_target, false_target);
1108 break;
1109 }
1110 case Primitive::kPrimDouble: {
1111 if (right.IsFpuRegister()) {
1112 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1113 } else if (right.IsConstant()) {
1114 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1115 codegen_->LiteralDoubleAddress(
1116 right.GetConstant()->AsDoubleConstant()->GetValue()));
1117 } else {
1118 DCHECK(right.IsDoubleStackSlot());
1119 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1120 Address(CpuRegister(RSP), right.GetStackIndex()));
1121 }
1122 GenerateFPJumps(condition, true_target, false_target);
1123 break;
1124 }
1125 default:
1126 LOG(FATAL) << "Unexpected condition type " << type;
1127 }
1128
1129 if (!falls_through) {
1130 __ jmp(false_target);
1131 }
1132}
1133
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001134void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1135 Label* true_target,
1136 Label* false_target,
1137 Label* always_true_target) {
1138 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001139 if (cond->IsIntConstant()) {
1140 // Constant condition, statically compared against 1.
1141 int32_t cond_value = cond->AsIntConstant()->GetValue();
1142 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001143 if (always_true_target != nullptr) {
1144 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001145 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001146 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001147 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001148 DCHECK_EQ(cond_value, 0);
1149 }
1150 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001151 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001152 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1153 // Moves do not affect the eflags register, so if the condition is
1154 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001155 // again. We can't use the eflags on FP conditions if they are
1156 // materialized due to the complex branching.
1157 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001158 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001159 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1160 && !Primitive::IsFloatingPointType(type);
1161
Roland Levillain4fa13f62015-07-06 18:11:54 +01001162 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001163 if (!eflags_set) {
1164 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001165 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001166 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001167 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001168 } else {
1169 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1170 Immediate(0));
1171 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001172 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001173 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001174 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001175 }
1176 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001177 // Condition has not been materialized, use its inputs as the
1178 // comparison and its condition as the branch condition.
1179
Mark Mendellc4701932015-04-10 13:18:51 -04001180 // Is this a long or FP comparison that has been folded into the HCondition?
1181 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001182 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001183 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1184 true_target, false_target, always_true_target);
1185 return;
1186 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001187
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001188 Location lhs = cond->GetLocations()->InAt(0);
1189 Location rhs = cond->GetLocations()->InAt(1);
1190 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001191 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001192 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001193 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001194 if (constant == 0) {
1195 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1196 } else {
1197 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1198 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001199 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001200 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001201 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1202 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001203 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001204 }
Dave Allison20dfc792014-06-16 20:44:29 -07001205 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001206 if (false_target != nullptr) {
1207 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001208 }
1209}
1210
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001211void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1212 LocationSummary* locations =
1213 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1214 HInstruction* cond = if_instr->InputAt(0);
1215 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1216 locations->SetInAt(0, Location::Any());
1217 }
1218}
1219
1220void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1221 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1222 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1223 Label* always_true_target = true_target;
1224 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1225 if_instr->IfTrueSuccessor())) {
1226 always_true_target = nullptr;
1227 }
1228 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1229 if_instr->IfFalseSuccessor())) {
1230 false_target = nullptr;
1231 }
1232 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1233}
1234
1235void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1236 LocationSummary* locations = new (GetGraph()->GetArena())
1237 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1238 HInstruction* cond = deoptimize->InputAt(0);
1239 DCHECK(cond->IsCondition());
1240 if (cond->AsCondition()->NeedsMaterialization()) {
1241 locations->SetInAt(0, Location::Any());
1242 }
1243}
1244
1245void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001246 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001247 DeoptimizationSlowPathX86_64(deoptimize);
1248 codegen_->AddSlowPath(slow_path);
1249 Label* slow_path_entry = slow_path->GetEntryLabel();
1250 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1251}
1252
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001253void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1254 local->SetLocations(nullptr);
1255}
1256
1257void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1258 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1259}
1260
1261void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1262 local->SetLocations(nullptr);
1263}
1264
1265void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1266 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001267 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001268}
1269
1270void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001271 LocationSummary* locations =
1272 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001273 switch (store->InputAt(1)->GetType()) {
1274 case Primitive::kPrimBoolean:
1275 case Primitive::kPrimByte:
1276 case Primitive::kPrimChar:
1277 case Primitive::kPrimShort:
1278 case Primitive::kPrimInt:
1279 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001280 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001281 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1282 break;
1283
1284 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001285 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001286 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1287 break;
1288
1289 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001290 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001291 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001292}
1293
1294void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001295 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001296}
1297
Roland Levillain0d37cd02015-05-27 16:39:19 +01001298void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001299 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001300 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001301 // Handle the long/FP comparisons made in instruction simplification.
1302 switch (cond->InputAt(0)->GetType()) {
1303 case Primitive::kPrimLong:
1304 locations->SetInAt(0, Location::RequiresRegister());
1305 locations->SetInAt(1, Location::Any());
1306 break;
1307 case Primitive::kPrimFloat:
1308 case Primitive::kPrimDouble:
1309 locations->SetInAt(0, Location::RequiresFpuRegister());
1310 locations->SetInAt(1, Location::Any());
1311 break;
1312 default:
1313 locations->SetInAt(0, Location::RequiresRegister());
1314 locations->SetInAt(1, Location::Any());
1315 break;
1316 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001317 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001318 locations->SetOut(Location::RequiresRegister());
1319 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001320}
1321
Roland Levillain0d37cd02015-05-27 16:39:19 +01001322void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001323 if (!cond->NeedsMaterialization()) {
1324 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001325 }
Mark Mendellc4701932015-04-10 13:18:51 -04001326
1327 LocationSummary* locations = cond->GetLocations();
1328 Location lhs = locations->InAt(0);
1329 Location rhs = locations->InAt(1);
1330 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1331 Label true_label, false_label;
1332
1333 switch (cond->InputAt(0)->GetType()) {
1334 default:
1335 // Integer case.
1336
1337 // Clear output register: setcc only sets the low byte.
1338 __ xorl(reg, reg);
1339
1340 if (rhs.IsRegister()) {
1341 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1342 } else if (rhs.IsConstant()) {
1343 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1344 if (constant == 0) {
1345 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1346 } else {
1347 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1348 }
1349 } else {
1350 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1351 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001352 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001353 return;
1354 case Primitive::kPrimLong:
1355 // Clear output register: setcc only sets the low byte.
1356 __ xorl(reg, reg);
1357
1358 if (rhs.IsRegister()) {
1359 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1360 } else if (rhs.IsConstant()) {
1361 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1362 if (IsInt<32>(value)) {
1363 if (value == 0) {
1364 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1365 } else {
1366 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1367 }
1368 } else {
1369 // Value won't fit in an int.
1370 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1371 }
1372 } else {
1373 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1374 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001375 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001376 return;
1377 case Primitive::kPrimFloat: {
1378 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1379 if (rhs.IsConstant()) {
1380 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1381 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1382 } else if (rhs.IsStackSlot()) {
1383 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1384 } else {
1385 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1386 }
1387 GenerateFPJumps(cond, &true_label, &false_label);
1388 break;
1389 }
1390 case Primitive::kPrimDouble: {
1391 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1392 if (rhs.IsConstant()) {
1393 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1394 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1395 } else if (rhs.IsDoubleStackSlot()) {
1396 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1397 } else {
1398 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1399 }
1400 GenerateFPJumps(cond, &true_label, &false_label);
1401 break;
1402 }
1403 }
1404
1405 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001406 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001407
Roland Levillain4fa13f62015-07-06 18:11:54 +01001408 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001409 __ Bind(&false_label);
1410 __ xorl(reg, reg);
1411 __ jmp(&done_label);
1412
Roland Levillain4fa13f62015-07-06 18:11:54 +01001413 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001414 __ Bind(&true_label);
1415 __ movl(reg, Immediate(1));
1416 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001417}
1418
1419void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1420 VisitCondition(comp);
1421}
1422
1423void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1424 VisitCondition(comp);
1425}
1426
1427void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1428 VisitCondition(comp);
1429}
1430
1431void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1432 VisitCondition(comp);
1433}
1434
1435void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1436 VisitCondition(comp);
1437}
1438
1439void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1440 VisitCondition(comp);
1441}
1442
1443void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1444 VisitCondition(comp);
1445}
1446
1447void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1448 VisitCondition(comp);
1449}
1450
1451void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1452 VisitCondition(comp);
1453}
1454
1455void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1456 VisitCondition(comp);
1457}
1458
1459void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1460 VisitCondition(comp);
1461}
1462
1463void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1464 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001465}
1466
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001467void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001468 LocationSummary* locations =
1469 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001470 switch (compare->InputAt(0)->GetType()) {
1471 case Primitive::kPrimLong: {
1472 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001473 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1475 break;
1476 }
1477 case Primitive::kPrimFloat:
1478 case Primitive::kPrimDouble: {
1479 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001480 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001481 locations->SetOut(Location::RequiresRegister());
1482 break;
1483 }
1484 default:
1485 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1486 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001487}
1488
1489void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001490 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001491 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001492 Location left = locations->InAt(0);
1493 Location right = locations->InAt(1);
1494
Mark Mendell0c9497d2015-08-21 09:30:05 -04001495 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001496 Primitive::Type type = compare->InputAt(0)->GetType();
1497 switch (type) {
1498 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001499 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1500 if (right.IsConstant()) {
1501 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001502 if (IsInt<32>(value)) {
1503 if (value == 0) {
1504 __ testq(left_reg, left_reg);
1505 } else {
1506 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1507 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001508 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001509 // Value won't fit in an int.
1510 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001511 }
Mark Mendell40741f32015-04-20 22:10:34 -04001512 } else if (right.IsDoubleStackSlot()) {
1513 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001514 } else {
1515 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1516 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001517 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001518 }
1519 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001520 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1521 if (right.IsConstant()) {
1522 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1523 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1524 } else if (right.IsStackSlot()) {
1525 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1526 } else {
1527 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1528 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001529 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1530 break;
1531 }
1532 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001533 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1534 if (right.IsConstant()) {
1535 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1536 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1537 } else if (right.IsDoubleStackSlot()) {
1538 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1539 } else {
1540 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1541 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001542 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1543 break;
1544 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001545 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001546 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001547 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001548 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001549 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001550 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001551
Calin Juravle91debbc2014-11-26 19:01:09 +00001552 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001553 __ movl(out, Immediate(1));
1554 __ jmp(&done);
1555
1556 __ Bind(&less);
1557 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001558
1559 __ Bind(&done);
1560}
1561
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001562void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001563 LocationSummary* locations =
1564 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001565 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001566}
1567
1568void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001569 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001570 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001571}
1572
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001573void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1574 LocationSummary* locations =
1575 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1576 locations->SetOut(Location::ConstantLocation(constant));
1577}
1578
1579void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1580 // Will be generated at use site.
1581 UNUSED(constant);
1582}
1583
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001584void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001585 LocationSummary* locations =
1586 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001587 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001588}
1589
1590void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001591 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001592 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001593}
1594
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001595void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1596 LocationSummary* locations =
1597 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1598 locations->SetOut(Location::ConstantLocation(constant));
1599}
1600
1601void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1602 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001603 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001604}
1605
1606void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1607 LocationSummary* locations =
1608 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1609 locations->SetOut(Location::ConstantLocation(constant));
1610}
1611
1612void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1613 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001614 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001615}
1616
Calin Juravle27df7582015-04-17 19:12:31 +01001617void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1618 memory_barrier->SetLocations(nullptr);
1619}
1620
1621void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1622 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1623}
1624
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001625void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1626 ret->SetLocations(nullptr);
1627}
1628
1629void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001630 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001631 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001632}
1633
1634void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001635 LocationSummary* locations =
1636 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001637 switch (ret->InputAt(0)->GetType()) {
1638 case Primitive::kPrimBoolean:
1639 case Primitive::kPrimByte:
1640 case Primitive::kPrimChar:
1641 case Primitive::kPrimShort:
1642 case Primitive::kPrimInt:
1643 case Primitive::kPrimNot:
1644 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001645 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001646 break;
1647
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001648 case Primitive::kPrimFloat:
1649 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001650 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001651 break;
1652
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001653 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001654 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001655 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001656}
1657
1658void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1659 if (kIsDebugBuild) {
1660 switch (ret->InputAt(0)->GetType()) {
1661 case Primitive::kPrimBoolean:
1662 case Primitive::kPrimByte:
1663 case Primitive::kPrimChar:
1664 case Primitive::kPrimShort:
1665 case Primitive::kPrimInt:
1666 case Primitive::kPrimNot:
1667 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001668 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001669 break;
1670
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001671 case Primitive::kPrimFloat:
1672 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001673 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001674 XMM0);
1675 break;
1676
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001677 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001678 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001679 }
1680 }
1681 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001682}
1683
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001684Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1685 switch (type) {
1686 case Primitive::kPrimBoolean:
1687 case Primitive::kPrimByte:
1688 case Primitive::kPrimChar:
1689 case Primitive::kPrimShort:
1690 case Primitive::kPrimInt:
1691 case Primitive::kPrimNot:
1692 case Primitive::kPrimLong:
1693 return Location::RegisterLocation(RAX);
1694
1695 case Primitive::kPrimVoid:
1696 return Location::NoLocation();
1697
1698 case Primitive::kPrimDouble:
1699 case Primitive::kPrimFloat:
1700 return Location::FpuRegisterLocation(XMM0);
1701 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001702
1703 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001704}
1705
1706Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1707 return Location::RegisterLocation(kMethodRegisterArgument);
1708}
1709
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001710Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001711 switch (type) {
1712 case Primitive::kPrimBoolean:
1713 case Primitive::kPrimByte:
1714 case Primitive::kPrimChar:
1715 case Primitive::kPrimShort:
1716 case Primitive::kPrimInt:
1717 case Primitive::kPrimNot: {
1718 uint32_t index = gp_index_++;
1719 stack_index_++;
1720 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001721 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001722 } else {
1723 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1724 }
1725 }
1726
1727 case Primitive::kPrimLong: {
1728 uint32_t index = gp_index_;
1729 stack_index_ += 2;
1730 if (index < calling_convention.GetNumberOfRegisters()) {
1731 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001732 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001733 } else {
1734 gp_index_ += 2;
1735 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1736 }
1737 }
1738
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001739 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001740 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001741 stack_index_++;
1742 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001743 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001744 } else {
1745 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1746 }
1747 }
1748
1749 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001750 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001751 stack_index_ += 2;
1752 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001753 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001754 } else {
1755 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1756 }
1757 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001758
1759 case Primitive::kPrimVoid:
1760 LOG(FATAL) << "Unexpected parameter type " << type;
1761 break;
1762 }
1763 return Location();
1764}
1765
Calin Juravle175dc732015-08-25 15:42:32 +01001766void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1767 // The trampoline uses the same calling convention as dex calling conventions,
1768 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1769 // the method_idx.
1770 HandleInvoke(invoke);
1771}
1772
1773void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1774 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1775}
1776
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001777void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001778 // When we do not run baseline, explicit clinit checks triggered by static
1779 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1780 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001781
Mark Mendellfb8d2792015-03-31 22:16:59 -04001782 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001783 if (intrinsic.TryDispatch(invoke)) {
1784 return;
1785 }
1786
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001787 HandleInvoke(invoke);
1788}
1789
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001790static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1791 if (invoke->GetLocations()->Intrinsified()) {
1792 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1793 intrinsic.Dispatch(invoke);
1794 return true;
1795 }
1796 return false;
1797}
1798
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001799void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001800 // When we do not run baseline, explicit clinit checks triggered by static
1801 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1802 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001803
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001804 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1805 return;
1806 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001807
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001808 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001809 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001810 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001811 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001812}
1813
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001814void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001815 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001816 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001817}
1818
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001819void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001820 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001821 if (intrinsic.TryDispatch(invoke)) {
1822 return;
1823 }
1824
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001825 HandleInvoke(invoke);
1826}
1827
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001828void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001829 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1830 return;
1831 }
1832
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001833 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001834
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001835 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001836 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001837}
1838
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001839void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1840 HandleInvoke(invoke);
1841 // Add the hidden argument.
1842 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1843}
1844
1845void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1846 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001847 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001848 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1849 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001850 LocationSummary* locations = invoke->GetLocations();
1851 Location receiver = locations->InAt(0);
1852 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1853
1854 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001855 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1856 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001857
1858 // temp = object->GetClass();
1859 if (receiver.IsStackSlot()) {
1860 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1861 __ movl(temp, Address(temp, class_offset));
1862 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001863 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001864 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001865 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001866 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001867 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001868 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001869 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001870 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001871 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001872
1873 DCHECK(!codegen_->IsLeafMethod());
1874 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1875}
1876
Roland Levillain88cb1752014-10-20 16:36:47 +01001877void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1878 LocationSummary* locations =
1879 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1880 switch (neg->GetResultType()) {
1881 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001882 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001883 locations->SetInAt(0, Location::RequiresRegister());
1884 locations->SetOut(Location::SameAsFirstInput());
1885 break;
1886
Roland Levillain88cb1752014-10-20 16:36:47 +01001887 case Primitive::kPrimFloat:
1888 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001889 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001890 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001891 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001892 break;
1893
1894 default:
1895 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1896 }
1897}
1898
1899void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1900 LocationSummary* locations = neg->GetLocations();
1901 Location out = locations->Out();
1902 Location in = locations->InAt(0);
1903 switch (neg->GetResultType()) {
1904 case Primitive::kPrimInt:
1905 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001906 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001907 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001908 break;
1909
1910 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001911 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001912 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001913 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001914 break;
1915
Roland Levillain5368c212014-11-27 15:03:41 +00001916 case Primitive::kPrimFloat: {
1917 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001918 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001919 // Implement float negation with an exclusive or with value
1920 // 0x80000000 (mask for bit 31, representing the sign of a
1921 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001922 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001923 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001924 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001925 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001926
Roland Levillain5368c212014-11-27 15:03:41 +00001927 case Primitive::kPrimDouble: {
1928 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001929 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001930 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001931 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001932 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001933 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001935 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001936 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001937
1938 default:
1939 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1940 }
1941}
1942
Roland Levillaindff1f282014-11-05 14:15:05 +00001943void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1944 LocationSummary* locations =
1945 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1946 Primitive::Type result_type = conversion->GetResultType();
1947 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001948 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001949
David Brazdilb2bd1c52015-03-25 11:17:37 +00001950 // The Java language does not allow treating boolean as an integral type but
1951 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001952
Roland Levillaindff1f282014-11-05 14:15:05 +00001953 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001954 case Primitive::kPrimByte:
1955 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001956 case Primitive::kPrimBoolean:
1957 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001958 case Primitive::kPrimShort:
1959 case Primitive::kPrimInt:
1960 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001961 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001962 locations->SetInAt(0, Location::Any());
1963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1964 break;
1965
1966 default:
1967 LOG(FATAL) << "Unexpected type conversion from " << input_type
1968 << " to " << result_type;
1969 }
1970 break;
1971
Roland Levillain01a8d712014-11-14 16:27:39 +00001972 case Primitive::kPrimShort:
1973 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001974 case Primitive::kPrimBoolean:
1975 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001976 case Primitive::kPrimByte:
1977 case Primitive::kPrimInt:
1978 case Primitive::kPrimChar:
1979 // Processing a Dex `int-to-short' instruction.
1980 locations->SetInAt(0, Location::Any());
1981 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1982 break;
1983
1984 default:
1985 LOG(FATAL) << "Unexpected type conversion from " << input_type
1986 << " to " << result_type;
1987 }
1988 break;
1989
Roland Levillain946e1432014-11-11 17:35:19 +00001990 case Primitive::kPrimInt:
1991 switch (input_type) {
1992 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001993 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001994 locations->SetInAt(0, Location::Any());
1995 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1996 break;
1997
1998 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001999 // Processing a Dex `float-to-int' instruction.
2000 locations->SetInAt(0, Location::RequiresFpuRegister());
2001 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002002 break;
2003
Roland Levillain946e1432014-11-11 17:35:19 +00002004 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002005 // Processing a Dex `double-to-int' instruction.
2006 locations->SetInAt(0, Location::RequiresFpuRegister());
2007 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002008 break;
2009
2010 default:
2011 LOG(FATAL) << "Unexpected type conversion from " << input_type
2012 << " to " << result_type;
2013 }
2014 break;
2015
Roland Levillaindff1f282014-11-05 14:15:05 +00002016 case Primitive::kPrimLong:
2017 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002018 case Primitive::kPrimBoolean:
2019 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002020 case Primitive::kPrimByte:
2021 case Primitive::kPrimShort:
2022 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002023 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002024 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002025 // TODO: We would benefit from a (to-be-implemented)
2026 // Location::RegisterOrStackSlot requirement for this input.
2027 locations->SetInAt(0, Location::RequiresRegister());
2028 locations->SetOut(Location::RequiresRegister());
2029 break;
2030
2031 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002032 // Processing a Dex `float-to-long' instruction.
2033 locations->SetInAt(0, Location::RequiresFpuRegister());
2034 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002035 break;
2036
Roland Levillaindff1f282014-11-05 14:15:05 +00002037 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002038 // Processing a Dex `double-to-long' instruction.
2039 locations->SetInAt(0, Location::RequiresFpuRegister());
2040 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002041 break;
2042
2043 default:
2044 LOG(FATAL) << "Unexpected type conversion from " << input_type
2045 << " to " << result_type;
2046 }
2047 break;
2048
Roland Levillain981e4542014-11-14 11:47:14 +00002049 case Primitive::kPrimChar:
2050 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002051 case Primitive::kPrimBoolean:
2052 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002053 case Primitive::kPrimByte:
2054 case Primitive::kPrimShort:
2055 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002056 // Processing a Dex `int-to-char' instruction.
2057 locations->SetInAt(0, Location::Any());
2058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2059 break;
2060
2061 default:
2062 LOG(FATAL) << "Unexpected type conversion from " << input_type
2063 << " to " << result_type;
2064 }
2065 break;
2066
Roland Levillaindff1f282014-11-05 14:15:05 +00002067 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002068 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002069 case Primitive::kPrimBoolean:
2070 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002071 case Primitive::kPrimByte:
2072 case Primitive::kPrimShort:
2073 case Primitive::kPrimInt:
2074 case Primitive::kPrimChar:
2075 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002076 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002077 locations->SetOut(Location::RequiresFpuRegister());
2078 break;
2079
2080 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002081 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002082 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002083 locations->SetOut(Location::RequiresFpuRegister());
2084 break;
2085
Roland Levillaincff13742014-11-17 14:32:17 +00002086 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002087 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002088 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002089 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002090 break;
2091
2092 default:
2093 LOG(FATAL) << "Unexpected type conversion from " << input_type
2094 << " to " << result_type;
2095 };
2096 break;
2097
Roland Levillaindff1f282014-11-05 14:15:05 +00002098 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002099 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002100 case Primitive::kPrimBoolean:
2101 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002102 case Primitive::kPrimByte:
2103 case Primitive::kPrimShort:
2104 case Primitive::kPrimInt:
2105 case Primitive::kPrimChar:
2106 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002107 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002108 locations->SetOut(Location::RequiresFpuRegister());
2109 break;
2110
2111 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002112 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002113 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002114 locations->SetOut(Location::RequiresFpuRegister());
2115 break;
2116
Roland Levillaincff13742014-11-17 14:32:17 +00002117 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002118 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002119 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002120 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002121 break;
2122
2123 default:
2124 LOG(FATAL) << "Unexpected type conversion from " << input_type
2125 << " to " << result_type;
2126 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002127 break;
2128
2129 default:
2130 LOG(FATAL) << "Unexpected type conversion from " << input_type
2131 << " to " << result_type;
2132 }
2133}
2134
2135void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2136 LocationSummary* locations = conversion->GetLocations();
2137 Location out = locations->Out();
2138 Location in = locations->InAt(0);
2139 Primitive::Type result_type = conversion->GetResultType();
2140 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002141 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002142 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002143 case Primitive::kPrimByte:
2144 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002145 case Primitive::kPrimBoolean:
2146 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002147 case Primitive::kPrimShort:
2148 case Primitive::kPrimInt:
2149 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002150 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002151 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002152 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002153 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002154 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002155 Address(CpuRegister(RSP), in.GetStackIndex()));
2156 } else {
2157 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002158 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002159 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2160 }
2161 break;
2162
2163 default:
2164 LOG(FATAL) << "Unexpected type conversion from " << input_type
2165 << " to " << result_type;
2166 }
2167 break;
2168
Roland Levillain01a8d712014-11-14 16:27:39 +00002169 case Primitive::kPrimShort:
2170 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002171 case Primitive::kPrimBoolean:
2172 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002173 case Primitive::kPrimByte:
2174 case Primitive::kPrimInt:
2175 case Primitive::kPrimChar:
2176 // Processing a Dex `int-to-short' instruction.
2177 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002178 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002179 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002180 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002181 Address(CpuRegister(RSP), in.GetStackIndex()));
2182 } else {
2183 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002184 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002185 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2186 }
2187 break;
2188
2189 default:
2190 LOG(FATAL) << "Unexpected type conversion from " << input_type
2191 << " to " << result_type;
2192 }
2193 break;
2194
Roland Levillain946e1432014-11-11 17:35:19 +00002195 case Primitive::kPrimInt:
2196 switch (input_type) {
2197 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002198 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002199 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002200 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002201 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002202 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002203 Address(CpuRegister(RSP), in.GetStackIndex()));
2204 } else {
2205 DCHECK(in.IsConstant());
2206 DCHECK(in.GetConstant()->IsLongConstant());
2207 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002208 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002209 }
2210 break;
2211
Roland Levillain3f8f9362014-12-02 17:45:01 +00002212 case Primitive::kPrimFloat: {
2213 // Processing a Dex `float-to-int' instruction.
2214 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2215 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002216 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002217
2218 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002219 // if input >= (float)INT_MAX goto done
2220 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002221 __ j(kAboveEqual, &done);
2222 // if input == NaN goto nan
2223 __ j(kUnordered, &nan);
2224 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002225 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002226 __ jmp(&done);
2227 __ Bind(&nan);
2228 // output = 0
2229 __ xorl(output, output);
2230 __ Bind(&done);
2231 break;
2232 }
2233
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002234 case Primitive::kPrimDouble: {
2235 // Processing a Dex `double-to-int' instruction.
2236 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2237 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002238 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002239
2240 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002241 // if input >= (double)INT_MAX goto done
2242 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002243 __ j(kAboveEqual, &done);
2244 // if input == NaN goto nan
2245 __ j(kUnordered, &nan);
2246 // output = double-to-int-truncate(input)
2247 __ cvttsd2si(output, input);
2248 __ jmp(&done);
2249 __ Bind(&nan);
2250 // output = 0
2251 __ xorl(output, output);
2252 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002253 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002254 }
Roland Levillain946e1432014-11-11 17:35:19 +00002255
2256 default:
2257 LOG(FATAL) << "Unexpected type conversion from " << input_type
2258 << " to " << result_type;
2259 }
2260 break;
2261
Roland Levillaindff1f282014-11-05 14:15:05 +00002262 case Primitive::kPrimLong:
2263 switch (input_type) {
2264 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002265 case Primitive::kPrimBoolean:
2266 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002267 case Primitive::kPrimByte:
2268 case Primitive::kPrimShort:
2269 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002270 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002271 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002272 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002273 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002274 break;
2275
Roland Levillain624279f2014-12-04 11:54:28 +00002276 case Primitive::kPrimFloat: {
2277 // Processing a Dex `float-to-long' instruction.
2278 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2279 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002280 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002281
Mark Mendell92e83bf2015-05-07 11:25:03 -04002282 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002283 // if input >= (float)LONG_MAX goto done
2284 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002285 __ j(kAboveEqual, &done);
2286 // if input == NaN goto nan
2287 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002288 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002289 __ cvttss2si(output, input, true);
2290 __ jmp(&done);
2291 __ Bind(&nan);
2292 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002293 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002294 __ Bind(&done);
2295 break;
2296 }
2297
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002298 case Primitive::kPrimDouble: {
2299 // Processing a Dex `double-to-long' instruction.
2300 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2301 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002302 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002303
Mark Mendell92e83bf2015-05-07 11:25:03 -04002304 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002305 // if input >= (double)LONG_MAX goto done
2306 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002307 __ j(kAboveEqual, &done);
2308 // if input == NaN goto nan
2309 __ j(kUnordered, &nan);
2310 // output = double-to-long-truncate(input)
2311 __ cvttsd2si(output, input, true);
2312 __ jmp(&done);
2313 __ Bind(&nan);
2314 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002315 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002316 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002317 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002318 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002319
2320 default:
2321 LOG(FATAL) << "Unexpected type conversion from " << input_type
2322 << " to " << result_type;
2323 }
2324 break;
2325
Roland Levillain981e4542014-11-14 11:47:14 +00002326 case Primitive::kPrimChar:
2327 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002328 case Primitive::kPrimBoolean:
2329 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002330 case Primitive::kPrimByte:
2331 case Primitive::kPrimShort:
2332 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002333 // Processing a Dex `int-to-char' instruction.
2334 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002335 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002336 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002338 Address(CpuRegister(RSP), in.GetStackIndex()));
2339 } else {
2340 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002341 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002342 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2343 }
2344 break;
2345
2346 default:
2347 LOG(FATAL) << "Unexpected type conversion from " << input_type
2348 << " to " << result_type;
2349 }
2350 break;
2351
Roland Levillaindff1f282014-11-05 14:15:05 +00002352 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002353 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002354 case Primitive::kPrimBoolean:
2355 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002356 case Primitive::kPrimByte:
2357 case Primitive::kPrimShort:
2358 case Primitive::kPrimInt:
2359 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002360 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002361 if (in.IsRegister()) {
2362 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2363 } else if (in.IsConstant()) {
2364 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2365 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2366 if (v == 0) {
2367 __ xorps(dest, dest);
2368 } else {
2369 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2370 }
2371 } else {
2372 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2373 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2374 }
Roland Levillaincff13742014-11-17 14:32:17 +00002375 break;
2376
2377 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002378 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002379 if (in.IsRegister()) {
2380 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2381 } else if (in.IsConstant()) {
2382 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2383 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2384 if (v == 0) {
2385 __ xorps(dest, dest);
2386 } else {
2387 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2388 }
2389 } else {
2390 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2391 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2392 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002393 break;
2394
Roland Levillaincff13742014-11-17 14:32:17 +00002395 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002396 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002397 if (in.IsFpuRegister()) {
2398 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2399 } else if (in.IsConstant()) {
2400 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2401 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2402 if (bit_cast<int64_t, double>(v) == 0) {
2403 __ xorps(dest, dest);
2404 } else {
2405 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2406 }
2407 } else {
2408 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2409 Address(CpuRegister(RSP), in.GetStackIndex()));
2410 }
Roland Levillaincff13742014-11-17 14:32:17 +00002411 break;
2412
2413 default:
2414 LOG(FATAL) << "Unexpected type conversion from " << input_type
2415 << " to " << result_type;
2416 };
2417 break;
2418
Roland Levillaindff1f282014-11-05 14:15:05 +00002419 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002420 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002421 case Primitive::kPrimBoolean:
2422 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002423 case Primitive::kPrimByte:
2424 case Primitive::kPrimShort:
2425 case Primitive::kPrimInt:
2426 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002427 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002428 if (in.IsRegister()) {
2429 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2430 } else if (in.IsConstant()) {
2431 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2432 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2433 if (v == 0) {
2434 __ xorpd(dest, dest);
2435 } else {
2436 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2437 }
2438 } else {
2439 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2440 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2441 }
Roland Levillaincff13742014-11-17 14:32:17 +00002442 break;
2443
2444 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002445 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002446 if (in.IsRegister()) {
2447 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2448 } else if (in.IsConstant()) {
2449 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2450 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2451 if (v == 0) {
2452 __ xorpd(dest, dest);
2453 } else {
2454 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2455 }
2456 } else {
2457 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2458 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2459 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002460 break;
2461
Roland Levillaincff13742014-11-17 14:32:17 +00002462 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002463 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002464 if (in.IsFpuRegister()) {
2465 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2466 } else if (in.IsConstant()) {
2467 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2468 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2469 if (bit_cast<int32_t, float>(v) == 0) {
2470 __ xorpd(dest, dest);
2471 } else {
2472 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2473 }
2474 } else {
2475 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2476 Address(CpuRegister(RSP), in.GetStackIndex()));
2477 }
Roland Levillaincff13742014-11-17 14:32:17 +00002478 break;
2479
2480 default:
2481 LOG(FATAL) << "Unexpected type conversion from " << input_type
2482 << " to " << result_type;
2483 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002484 break;
2485
2486 default:
2487 LOG(FATAL) << "Unexpected type conversion from " << input_type
2488 << " to " << result_type;
2489 }
2490}
2491
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002492void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002493 LocationSummary* locations =
2494 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002495 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002496 case Primitive::kPrimInt: {
2497 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002498 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002500 break;
2501 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002502
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002503 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002504 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002505 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002506 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002507 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002508 break;
2509 }
2510
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002511 case Primitive::kPrimDouble:
2512 case Primitive::kPrimFloat: {
2513 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002514 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002515 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002516 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002517 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002518
2519 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002520 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002521 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002522}
2523
2524void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2525 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002526 Location first = locations->InAt(0);
2527 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002528 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002529
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002530 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002531 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002532 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002533 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2534 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002535 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2536 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002537 } else {
2538 __ leal(out.AsRegister<CpuRegister>(), Address(
2539 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2540 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002541 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002542 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2543 __ addl(out.AsRegister<CpuRegister>(),
2544 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2545 } else {
2546 __ leal(out.AsRegister<CpuRegister>(), Address(
2547 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2548 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002549 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002550 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002551 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002552 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002553 break;
2554 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002555
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002556 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002557 if (second.IsRegister()) {
2558 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2559 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002560 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2561 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002562 } else {
2563 __ leaq(out.AsRegister<CpuRegister>(), Address(
2564 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2565 }
2566 } else {
2567 DCHECK(second.IsConstant());
2568 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2569 int32_t int32_value = Low32Bits(value);
2570 DCHECK_EQ(int32_value, value);
2571 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2572 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2573 } else {
2574 __ leaq(out.AsRegister<CpuRegister>(), Address(
2575 first.AsRegister<CpuRegister>(), int32_value));
2576 }
2577 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002578 break;
2579 }
2580
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002581 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002582 if (second.IsFpuRegister()) {
2583 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2584 } else if (second.IsConstant()) {
2585 __ addss(first.AsFpuRegister<XmmRegister>(),
2586 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2587 } else {
2588 DCHECK(second.IsStackSlot());
2589 __ addss(first.AsFpuRegister<XmmRegister>(),
2590 Address(CpuRegister(RSP), second.GetStackIndex()));
2591 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002592 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002593 }
2594
2595 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002596 if (second.IsFpuRegister()) {
2597 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2598 } else if (second.IsConstant()) {
2599 __ addsd(first.AsFpuRegister<XmmRegister>(),
2600 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2601 } else {
2602 DCHECK(second.IsDoubleStackSlot());
2603 __ addsd(first.AsFpuRegister<XmmRegister>(),
2604 Address(CpuRegister(RSP), second.GetStackIndex()));
2605 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002606 break;
2607 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002608
2609 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002610 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002611 }
2612}
2613
2614void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002615 LocationSummary* locations =
2616 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002617 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002618 case Primitive::kPrimInt: {
2619 locations->SetInAt(0, Location::RequiresRegister());
2620 locations->SetInAt(1, Location::Any());
2621 locations->SetOut(Location::SameAsFirstInput());
2622 break;
2623 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002624 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002625 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002626 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002627 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002628 break;
2629 }
Calin Juravle11351682014-10-23 15:38:15 +01002630 case Primitive::kPrimFloat:
2631 case Primitive::kPrimDouble: {
2632 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002633 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002634 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002635 break;
Calin Juravle11351682014-10-23 15:38:15 +01002636 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002637 default:
Calin Juravle11351682014-10-23 15:38:15 +01002638 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002639 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002640}
2641
2642void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2643 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002644 Location first = locations->InAt(0);
2645 Location second = locations->InAt(1);
2646 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002647 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002648 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002649 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002650 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002651 } else if (second.IsConstant()) {
2652 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002653 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002654 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002655 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002656 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002657 break;
2658 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002659 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002660 if (second.IsConstant()) {
2661 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2662 DCHECK(IsInt<32>(value));
2663 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2664 } else {
2665 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2666 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002667 break;
2668 }
2669
Calin Juravle11351682014-10-23 15:38:15 +01002670 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002671 if (second.IsFpuRegister()) {
2672 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2673 } else if (second.IsConstant()) {
2674 __ subss(first.AsFpuRegister<XmmRegister>(),
2675 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2676 } else {
2677 DCHECK(second.IsStackSlot());
2678 __ subss(first.AsFpuRegister<XmmRegister>(),
2679 Address(CpuRegister(RSP), second.GetStackIndex()));
2680 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002681 break;
Calin Juravle11351682014-10-23 15:38:15 +01002682 }
2683
2684 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002685 if (second.IsFpuRegister()) {
2686 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2687 } else if (second.IsConstant()) {
2688 __ subsd(first.AsFpuRegister<XmmRegister>(),
2689 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2690 } else {
2691 DCHECK(second.IsDoubleStackSlot());
2692 __ subsd(first.AsFpuRegister<XmmRegister>(),
2693 Address(CpuRegister(RSP), second.GetStackIndex()));
2694 }
Calin Juravle11351682014-10-23 15:38:15 +01002695 break;
2696 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002697
2698 default:
Calin Juravle11351682014-10-23 15:38:15 +01002699 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002700 }
2701}
2702
Calin Juravle34bacdf2014-10-07 20:23:36 +01002703void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2704 LocationSummary* locations =
2705 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2706 switch (mul->GetResultType()) {
2707 case Primitive::kPrimInt: {
2708 locations->SetInAt(0, Location::RequiresRegister());
2709 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002710 if (mul->InputAt(1)->IsIntConstant()) {
2711 // Can use 3 operand multiply.
2712 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2713 } else {
2714 locations->SetOut(Location::SameAsFirstInput());
2715 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002716 break;
2717 }
2718 case Primitive::kPrimLong: {
2719 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002720 locations->SetInAt(1, Location::Any());
2721 if (mul->InputAt(1)->IsLongConstant() &&
2722 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002723 // Can use 3 operand multiply.
2724 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2725 } else {
2726 locations->SetOut(Location::SameAsFirstInput());
2727 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002728 break;
2729 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002730 case Primitive::kPrimFloat:
2731 case Primitive::kPrimDouble: {
2732 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002733 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002734 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002735 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002736 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002737
2738 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002739 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002740 }
2741}
2742
2743void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2744 LocationSummary* locations = mul->GetLocations();
2745 Location first = locations->InAt(0);
2746 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002747 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002748 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002749 case Primitive::kPrimInt:
2750 // The constant may have ended up in a register, so test explicitly to avoid
2751 // problems where the output may not be the same as the first operand.
2752 if (mul->InputAt(1)->IsIntConstant()) {
2753 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2754 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2755 } else if (second.IsRegister()) {
2756 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002757 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002758 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002759 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002760 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002761 __ imull(first.AsRegister<CpuRegister>(),
2762 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002763 }
2764 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002765 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002766 // The constant may have ended up in a register, so test explicitly to avoid
2767 // problems where the output may not be the same as the first operand.
2768 if (mul->InputAt(1)->IsLongConstant()) {
2769 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2770 if (IsInt<32>(value)) {
2771 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2772 Immediate(static_cast<int32_t>(value)));
2773 } else {
2774 // Have to use the constant area.
2775 DCHECK(first.Equals(out));
2776 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2777 }
2778 } else if (second.IsRegister()) {
2779 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002780 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002781 } else {
2782 DCHECK(second.IsDoubleStackSlot());
2783 DCHECK(first.Equals(out));
2784 __ imulq(first.AsRegister<CpuRegister>(),
2785 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002786 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002787 break;
2788 }
2789
Calin Juravleb5bfa962014-10-21 18:02:24 +01002790 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002791 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002792 if (second.IsFpuRegister()) {
2793 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2794 } else if (second.IsConstant()) {
2795 __ mulss(first.AsFpuRegister<XmmRegister>(),
2796 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2797 } else {
2798 DCHECK(second.IsStackSlot());
2799 __ mulss(first.AsFpuRegister<XmmRegister>(),
2800 Address(CpuRegister(RSP), second.GetStackIndex()));
2801 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002802 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002803 }
2804
2805 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002806 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002807 if (second.IsFpuRegister()) {
2808 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2809 } else if (second.IsConstant()) {
2810 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2811 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2812 } else {
2813 DCHECK(second.IsDoubleStackSlot());
2814 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2815 Address(CpuRegister(RSP), second.GetStackIndex()));
2816 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002817 break;
2818 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002819
2820 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002821 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002822 }
2823}
2824
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002825void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2826 uint32_t stack_adjustment, bool is_float) {
2827 if (source.IsStackSlot()) {
2828 DCHECK(is_float);
2829 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2830 } else if (source.IsDoubleStackSlot()) {
2831 DCHECK(!is_float);
2832 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2833 } else {
2834 // Write the value to the temporary location on the stack and load to FP stack.
2835 if (is_float) {
2836 Location stack_temp = Location::StackSlot(temp_offset);
2837 codegen_->Move(stack_temp, source);
2838 __ flds(Address(CpuRegister(RSP), temp_offset));
2839 } else {
2840 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2841 codegen_->Move(stack_temp, source);
2842 __ fldl(Address(CpuRegister(RSP), temp_offset));
2843 }
2844 }
2845}
2846
2847void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2848 Primitive::Type type = rem->GetResultType();
2849 bool is_float = type == Primitive::kPrimFloat;
2850 size_t elem_size = Primitive::ComponentSize(type);
2851 LocationSummary* locations = rem->GetLocations();
2852 Location first = locations->InAt(0);
2853 Location second = locations->InAt(1);
2854 Location out = locations->Out();
2855
2856 // Create stack space for 2 elements.
2857 // TODO: enhance register allocator to ask for stack temporaries.
2858 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2859
2860 // Load the values to the FP stack in reverse order, using temporaries if needed.
2861 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2862 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2863
2864 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002865 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002866 __ Bind(&retry);
2867 __ fprem();
2868
2869 // Move FP status to AX.
2870 __ fstsw();
2871
2872 // And see if the argument reduction is complete. This is signaled by the
2873 // C2 FPU flag bit set to 0.
2874 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2875 __ j(kNotEqual, &retry);
2876
2877 // We have settled on the final value. Retrieve it into an XMM register.
2878 // Store FP top of stack to real stack.
2879 if (is_float) {
2880 __ fsts(Address(CpuRegister(RSP), 0));
2881 } else {
2882 __ fstl(Address(CpuRegister(RSP), 0));
2883 }
2884
2885 // Pop the 2 items from the FP stack.
2886 __ fucompp();
2887
2888 // Load the value from the stack into an XMM register.
2889 DCHECK(out.IsFpuRegister()) << out;
2890 if (is_float) {
2891 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2892 } else {
2893 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2894 }
2895
2896 // And remove the temporary stack space we allocated.
2897 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2898}
2899
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002900void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2901 DCHECK(instruction->IsDiv() || instruction->IsRem());
2902
2903 LocationSummary* locations = instruction->GetLocations();
2904 Location second = locations->InAt(1);
2905 DCHECK(second.IsConstant());
2906
2907 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2908 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002909 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002910
2911 DCHECK(imm == 1 || imm == -1);
2912
2913 switch (instruction->GetResultType()) {
2914 case Primitive::kPrimInt: {
2915 if (instruction->IsRem()) {
2916 __ xorl(output_register, output_register);
2917 } else {
2918 __ movl(output_register, input_register);
2919 if (imm == -1) {
2920 __ negl(output_register);
2921 }
2922 }
2923 break;
2924 }
2925
2926 case Primitive::kPrimLong: {
2927 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002928 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002929 } else {
2930 __ movq(output_register, input_register);
2931 if (imm == -1) {
2932 __ negq(output_register);
2933 }
2934 }
2935 break;
2936 }
2937
2938 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002939 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002940 }
2941}
2942
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002943void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002944 LocationSummary* locations = instruction->GetLocations();
2945 Location second = locations->InAt(1);
2946
2947 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2948 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2949
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002950 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002951
2952 DCHECK(IsPowerOfTwo(std::abs(imm)));
2953
2954 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2955
2956 if (instruction->GetResultType() == Primitive::kPrimInt) {
2957 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2958 __ testl(numerator, numerator);
2959 __ cmov(kGreaterEqual, tmp, numerator);
2960 int shift = CTZ(imm);
2961 __ sarl(tmp, Immediate(shift));
2962
2963 if (imm < 0) {
2964 __ negl(tmp);
2965 }
2966
2967 __ movl(output_register, tmp);
2968 } else {
2969 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2970 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2971
Mark Mendell92e83bf2015-05-07 11:25:03 -04002972 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002973 __ addq(rdx, numerator);
2974 __ testq(numerator, numerator);
2975 __ cmov(kGreaterEqual, rdx, numerator);
2976 int shift = CTZ(imm);
2977 __ sarq(rdx, Immediate(shift));
2978
2979 if (imm < 0) {
2980 __ negq(rdx);
2981 }
2982
2983 __ movq(output_register, rdx);
2984 }
2985}
2986
2987void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2988 DCHECK(instruction->IsDiv() || instruction->IsRem());
2989
2990 LocationSummary* locations = instruction->GetLocations();
2991 Location second = locations->InAt(1);
2992
2993 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2994 : locations->GetTemp(0).AsRegister<CpuRegister>();
2995 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2996 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2997 : locations->Out().AsRegister<CpuRegister>();
2998 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2999
3000 DCHECK_EQ(RAX, eax.AsRegister());
3001 DCHECK_EQ(RDX, edx.AsRegister());
3002 if (instruction->IsDiv()) {
3003 DCHECK_EQ(RAX, out.AsRegister());
3004 } else {
3005 DCHECK_EQ(RDX, out.AsRegister());
3006 }
3007
3008 int64_t magic;
3009 int shift;
3010
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003011 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003012 if (instruction->GetResultType() == Primitive::kPrimInt) {
3013 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3014
3015 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3016
3017 __ movl(numerator, eax);
3018
Mark Mendell0c9497d2015-08-21 09:30:05 -04003019 NearLabel no_div;
3020 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003021 __ testl(eax, eax);
3022 __ j(kNotEqual, &no_div);
3023
3024 __ xorl(out, out);
3025 __ jmp(&end);
3026
3027 __ Bind(&no_div);
3028
3029 __ movl(eax, Immediate(magic));
3030 __ imull(numerator);
3031
3032 if (imm > 0 && magic < 0) {
3033 __ addl(edx, numerator);
3034 } else if (imm < 0 && magic > 0) {
3035 __ subl(edx, numerator);
3036 }
3037
3038 if (shift != 0) {
3039 __ sarl(edx, Immediate(shift));
3040 }
3041
3042 __ movl(eax, edx);
3043 __ shrl(edx, Immediate(31));
3044 __ addl(edx, eax);
3045
3046 if (instruction->IsRem()) {
3047 __ movl(eax, numerator);
3048 __ imull(edx, Immediate(imm));
3049 __ subl(eax, edx);
3050 __ movl(edx, eax);
3051 } else {
3052 __ movl(eax, edx);
3053 }
3054 __ Bind(&end);
3055 } else {
3056 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3057
3058 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3059
3060 CpuRegister rax = eax;
3061 CpuRegister rdx = edx;
3062
3063 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3064
3065 // Save the numerator.
3066 __ movq(numerator, rax);
3067
3068 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003069 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003070
3071 // RDX:RAX = magic * numerator
3072 __ imulq(numerator);
3073
3074 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003075 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003076 __ addq(rdx, numerator);
3077 } else if (imm < 0 && magic > 0) {
3078 // RDX -= numerator
3079 __ subq(rdx, numerator);
3080 }
3081
3082 // Shift if needed.
3083 if (shift != 0) {
3084 __ sarq(rdx, Immediate(shift));
3085 }
3086
3087 // RDX += 1 if RDX < 0
3088 __ movq(rax, rdx);
3089 __ shrq(rdx, Immediate(63));
3090 __ addq(rdx, rax);
3091
3092 if (instruction->IsRem()) {
3093 __ movq(rax, numerator);
3094
3095 if (IsInt<32>(imm)) {
3096 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3097 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003098 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003099 }
3100
3101 __ subq(rax, rdx);
3102 __ movq(rdx, rax);
3103 } else {
3104 __ movq(rax, rdx);
3105 }
3106 }
3107}
3108
Calin Juravlebacfec32014-11-14 15:54:36 +00003109void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3110 DCHECK(instruction->IsDiv() || instruction->IsRem());
3111 Primitive::Type type = instruction->GetResultType();
3112 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3113
3114 bool is_div = instruction->IsDiv();
3115 LocationSummary* locations = instruction->GetLocations();
3116
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003117 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3118 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003119
Roland Levillain271ab9c2014-11-27 15:23:57 +00003120 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003121 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003122
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003123 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003124 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003125
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003126 if (imm == 0) {
3127 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3128 } else if (imm == 1 || imm == -1) {
3129 DivRemOneOrMinusOne(instruction);
3130 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003131 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003132 } else {
3133 DCHECK(imm <= -2 || imm >= 2);
3134 GenerateDivRemWithAnyConstant(instruction);
3135 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003136 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003137 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003138 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3139 out.AsRegister(), type, is_div);
3140 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003141
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003142 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3143 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3144 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3145 // so it's safe to just use negl instead of more complex comparisons.
3146 if (type == Primitive::kPrimInt) {
3147 __ cmpl(second_reg, Immediate(-1));
3148 __ j(kEqual, slow_path->GetEntryLabel());
3149 // edx:eax <- sign-extended of eax
3150 __ cdq();
3151 // eax = quotient, edx = remainder
3152 __ idivl(second_reg);
3153 } else {
3154 __ cmpq(second_reg, Immediate(-1));
3155 __ j(kEqual, slow_path->GetEntryLabel());
3156 // rdx:rax <- sign-extended of rax
3157 __ cqo();
3158 // rax = quotient, rdx = remainder
3159 __ idivq(second_reg);
3160 }
3161 __ Bind(slow_path->GetExitLabel());
3162 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003163}
3164
Calin Juravle7c4954d2014-10-28 16:57:40 +00003165void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3166 LocationSummary* locations =
3167 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3168 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003169 case Primitive::kPrimInt:
3170 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003171 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003172 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003173 locations->SetOut(Location::SameAsFirstInput());
3174 // Intel uses edx:eax as the dividend.
3175 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003176 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3177 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3178 // output and request another temp.
3179 if (div->InputAt(1)->IsConstant()) {
3180 locations->AddTemp(Location::RequiresRegister());
3181 }
Calin Juravled0d48522014-11-04 16:40:20 +00003182 break;
3183 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003184
Calin Juravle7c4954d2014-10-28 16:57:40 +00003185 case Primitive::kPrimFloat:
3186 case Primitive::kPrimDouble: {
3187 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003188 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003189 locations->SetOut(Location::SameAsFirstInput());
3190 break;
3191 }
3192
3193 default:
3194 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3195 }
3196}
3197
3198void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3199 LocationSummary* locations = div->GetLocations();
3200 Location first = locations->InAt(0);
3201 Location second = locations->InAt(1);
3202 DCHECK(first.Equals(locations->Out()));
3203
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003204 Primitive::Type type = div->GetResultType();
3205 switch (type) {
3206 case Primitive::kPrimInt:
3207 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003208 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003209 break;
3210 }
3211
Calin Juravle7c4954d2014-10-28 16:57:40 +00003212 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003213 if (second.IsFpuRegister()) {
3214 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3215 } else if (second.IsConstant()) {
3216 __ divss(first.AsFpuRegister<XmmRegister>(),
3217 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3218 } else {
3219 DCHECK(second.IsStackSlot());
3220 __ divss(first.AsFpuRegister<XmmRegister>(),
3221 Address(CpuRegister(RSP), second.GetStackIndex()));
3222 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003223 break;
3224 }
3225
3226 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003227 if (second.IsFpuRegister()) {
3228 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3229 } else if (second.IsConstant()) {
3230 __ divsd(first.AsFpuRegister<XmmRegister>(),
3231 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3232 } else {
3233 DCHECK(second.IsDoubleStackSlot());
3234 __ divsd(first.AsFpuRegister<XmmRegister>(),
3235 Address(CpuRegister(RSP), second.GetStackIndex()));
3236 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003237 break;
3238 }
3239
3240 default:
3241 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3242 }
3243}
3244
Calin Juravlebacfec32014-11-14 15:54:36 +00003245void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003246 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003247 LocationSummary* locations =
3248 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003249
3250 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003251 case Primitive::kPrimInt:
3252 case Primitive::kPrimLong: {
3253 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003254 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003255 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3256 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003257 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3258 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3259 // output and request another temp.
3260 if (rem->InputAt(1)->IsConstant()) {
3261 locations->AddTemp(Location::RequiresRegister());
3262 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003263 break;
3264 }
3265
3266 case Primitive::kPrimFloat:
3267 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003268 locations->SetInAt(0, Location::Any());
3269 locations->SetInAt(1, Location::Any());
3270 locations->SetOut(Location::RequiresFpuRegister());
3271 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003272 break;
3273 }
3274
3275 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003276 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003277 }
3278}
3279
3280void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3281 Primitive::Type type = rem->GetResultType();
3282 switch (type) {
3283 case Primitive::kPrimInt:
3284 case Primitive::kPrimLong: {
3285 GenerateDivRemIntegral(rem);
3286 break;
3287 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003288 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003289 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003290 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003291 break;
3292 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003293 default:
3294 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3295 }
3296}
3297
Calin Juravled0d48522014-11-04 16:40:20 +00003298void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003299 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3300 ? LocationSummary::kCallOnSlowPath
3301 : LocationSummary::kNoCall;
3302 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003303 locations->SetInAt(0, Location::Any());
3304 if (instruction->HasUses()) {
3305 locations->SetOut(Location::SameAsFirstInput());
3306 }
3307}
3308
3309void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003310 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003311 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3312 codegen_->AddSlowPath(slow_path);
3313
3314 LocationSummary* locations = instruction->GetLocations();
3315 Location value = locations->InAt(0);
3316
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003317 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003318 case Primitive::kPrimByte:
3319 case Primitive::kPrimChar:
3320 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003321 case Primitive::kPrimInt: {
3322 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003323 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003324 __ j(kEqual, slow_path->GetEntryLabel());
3325 } else if (value.IsStackSlot()) {
3326 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3327 __ j(kEqual, slow_path->GetEntryLabel());
3328 } else {
3329 DCHECK(value.IsConstant()) << value;
3330 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3331 __ jmp(slow_path->GetEntryLabel());
3332 }
3333 }
3334 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003335 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003336 case Primitive::kPrimLong: {
3337 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003338 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003339 __ j(kEqual, slow_path->GetEntryLabel());
3340 } else if (value.IsDoubleStackSlot()) {
3341 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3342 __ j(kEqual, slow_path->GetEntryLabel());
3343 } else {
3344 DCHECK(value.IsConstant()) << value;
3345 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3346 __ jmp(slow_path->GetEntryLabel());
3347 }
3348 }
3349 break;
3350 }
3351 default:
3352 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003353 }
Calin Juravled0d48522014-11-04 16:40:20 +00003354}
3355
Calin Juravle9aec02f2014-11-18 23:06:35 +00003356void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3357 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3358
3359 LocationSummary* locations =
3360 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3361
3362 switch (op->GetResultType()) {
3363 case Primitive::kPrimInt:
3364 case Primitive::kPrimLong: {
3365 locations->SetInAt(0, Location::RequiresRegister());
3366 // The shift count needs to be in CL.
3367 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3368 locations->SetOut(Location::SameAsFirstInput());
3369 break;
3370 }
3371 default:
3372 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3373 }
3374}
3375
3376void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3377 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3378
3379 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003380 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003381 Location second = locations->InAt(1);
3382
3383 switch (op->GetResultType()) {
3384 case Primitive::kPrimInt: {
3385 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003386 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003387 if (op->IsShl()) {
3388 __ shll(first_reg, second_reg);
3389 } else if (op->IsShr()) {
3390 __ sarl(first_reg, second_reg);
3391 } else {
3392 __ shrl(first_reg, second_reg);
3393 }
3394 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003395 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003396 if (op->IsShl()) {
3397 __ shll(first_reg, imm);
3398 } else if (op->IsShr()) {
3399 __ sarl(first_reg, imm);
3400 } else {
3401 __ shrl(first_reg, imm);
3402 }
3403 }
3404 break;
3405 }
3406 case Primitive::kPrimLong: {
3407 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003408 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003409 if (op->IsShl()) {
3410 __ shlq(first_reg, second_reg);
3411 } else if (op->IsShr()) {
3412 __ sarq(first_reg, second_reg);
3413 } else {
3414 __ shrq(first_reg, second_reg);
3415 }
3416 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003417 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003418 if (op->IsShl()) {
3419 __ shlq(first_reg, imm);
3420 } else if (op->IsShr()) {
3421 __ sarq(first_reg, imm);
3422 } else {
3423 __ shrq(first_reg, imm);
3424 }
3425 }
3426 break;
3427 }
3428 default:
3429 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3430 }
3431}
3432
3433void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3434 HandleShift(shl);
3435}
3436
3437void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3438 HandleShift(shl);
3439}
3440
3441void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3442 HandleShift(shr);
3443}
3444
3445void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3446 HandleShift(shr);
3447}
3448
3449void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3450 HandleShift(ushr);
3451}
3452
3453void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3454 HandleShift(ushr);
3455}
3456
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003457void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003458 LocationSummary* locations =
3459 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003460 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003461 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003462 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003463 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003464}
3465
3466void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3467 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003468 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3469 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003470 // Note: if heap poisoning is enabled, the entry point takes cares
3471 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003472
Calin Juravle175dc732015-08-25 15:42:32 +01003473 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3474 instruction,
3475 instruction->GetDexPc(),
3476 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003477
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003478 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003479}
3480
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003481void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3482 LocationSummary* locations =
3483 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3484 InvokeRuntimeCallingConvention calling_convention;
3485 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003486 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003487 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003488 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003489}
3490
3491void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3492 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003493 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3494 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003495
Roland Levillain4d027112015-07-01 15:41:14 +01003496 // Note: if heap poisoning is enabled, the entry point takes cares
3497 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003498 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3499 instruction,
3500 instruction->GetDexPc(),
3501 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003502
3503 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003504}
3505
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003506void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003507 LocationSummary* locations =
3508 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003509 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3510 if (location.IsStackSlot()) {
3511 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3512 } else if (location.IsDoubleStackSlot()) {
3513 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3514 }
3515 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003516}
3517
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003518void InstructionCodeGeneratorX86_64::VisitParameterValue(
3519 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003520 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003521}
3522
3523void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3524 LocationSummary* locations =
3525 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3526 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3527}
3528
3529void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3530 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3531 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003532}
3533
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003534void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003535 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003536 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003537 locations->SetInAt(0, Location::RequiresRegister());
3538 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003539}
3540
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003541void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3542 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003543 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3544 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003545 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003546 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003547 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003548 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003549 break;
3550
3551 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003552 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003553 break;
3554
3555 default:
3556 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3557 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003558}
3559
David Brazdil66d126e2015-04-03 16:02:44 +01003560void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3561 LocationSummary* locations =
3562 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3563 locations->SetInAt(0, Location::RequiresRegister());
3564 locations->SetOut(Location::SameAsFirstInput());
3565}
3566
3567void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003568 LocationSummary* locations = bool_not->GetLocations();
3569 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3570 locations->Out().AsRegister<CpuRegister>().AsRegister());
3571 Location out = locations->Out();
3572 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3573}
3574
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003575void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003576 LocationSummary* locations =
3577 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003578 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3579 locations->SetInAt(i, Location::Any());
3580 }
3581 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003582}
3583
3584void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003585 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003586 LOG(FATAL) << "Unimplemented";
3587}
3588
Calin Juravle52c48962014-12-16 17:02:57 +00003589void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3590 /*
3591 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3592 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3593 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3594 */
3595 switch (kind) {
3596 case MemBarrierKind::kAnyAny: {
3597 __ mfence();
3598 break;
3599 }
3600 case MemBarrierKind::kAnyStore:
3601 case MemBarrierKind::kLoadAny:
3602 case MemBarrierKind::kStoreStore: {
3603 // nop
3604 break;
3605 }
3606 default:
3607 LOG(FATAL) << "Unexpected memory barier " << kind;
3608 }
3609}
3610
3611void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3612 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3613
Nicolas Geoffray39468442014-09-02 15:17:15 +01003614 LocationSummary* locations =
3615 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003616 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003617 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3618 locations->SetOut(Location::RequiresFpuRegister());
3619 } else {
3620 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3621 }
Calin Juravle52c48962014-12-16 17:02:57 +00003622}
3623
3624void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3625 const FieldInfo& field_info) {
3626 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3627
3628 LocationSummary* locations = instruction->GetLocations();
3629 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3630 Location out = locations->Out();
3631 bool is_volatile = field_info.IsVolatile();
3632 Primitive::Type field_type = field_info.GetFieldType();
3633 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3634
3635 switch (field_type) {
3636 case Primitive::kPrimBoolean: {
3637 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3638 break;
3639 }
3640
3641 case Primitive::kPrimByte: {
3642 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3643 break;
3644 }
3645
3646 case Primitive::kPrimShort: {
3647 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3648 break;
3649 }
3650
3651 case Primitive::kPrimChar: {
3652 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3653 break;
3654 }
3655
3656 case Primitive::kPrimInt:
3657 case Primitive::kPrimNot: {
3658 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3659 break;
3660 }
3661
3662 case Primitive::kPrimLong: {
3663 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3664 break;
3665 }
3666
3667 case Primitive::kPrimFloat: {
3668 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3669 break;
3670 }
3671
3672 case Primitive::kPrimDouble: {
3673 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3674 break;
3675 }
3676
3677 case Primitive::kPrimVoid:
3678 LOG(FATAL) << "Unreachable type " << field_type;
3679 UNREACHABLE();
3680 }
3681
Calin Juravle77520bc2015-01-12 18:45:46 +00003682 codegen_->MaybeRecordImplicitNullCheck(instruction);
3683
Calin Juravle52c48962014-12-16 17:02:57 +00003684 if (is_volatile) {
3685 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3686 }
Roland Levillain4d027112015-07-01 15:41:14 +01003687
3688 if (field_type == Primitive::kPrimNot) {
3689 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3690 }
Calin Juravle52c48962014-12-16 17:02:57 +00003691}
3692
3693void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3694 const FieldInfo& field_info) {
3695 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3696
3697 LocationSummary* locations =
3698 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003699 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003700 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003701 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003702
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003703 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003704 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3705 locations->SetInAt(1, Location::RequiresFpuRegister());
3706 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003707 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003708 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003709 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003710 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003711 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003712 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003713 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3714 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003715 locations->AddTemp(Location::RequiresRegister());
3716 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003717}
3718
Calin Juravle52c48962014-12-16 17:02:57 +00003719void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003720 const FieldInfo& field_info,
3721 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003722 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3723
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003724 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003725 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3726 Location value = locations->InAt(1);
3727 bool is_volatile = field_info.IsVolatile();
3728 Primitive::Type field_type = field_info.GetFieldType();
3729 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3730
3731 if (is_volatile) {
3732 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3733 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003734
3735 switch (field_type) {
3736 case Primitive::kPrimBoolean:
3737 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003738 if (value.IsConstant()) {
3739 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3740 __ movb(Address(base, offset), Immediate(v));
3741 } else {
3742 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3743 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003744 break;
3745 }
3746
3747 case Primitive::kPrimShort:
3748 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003749 if (value.IsConstant()) {
3750 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3751 __ movw(Address(base, offset), Immediate(v));
3752 } else {
3753 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3754 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003755 break;
3756 }
3757
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003758 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003759 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003760 if (value.IsConstant()) {
3761 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003762 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3763 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3764 // Note: if heap poisoning is enabled, no need to poison
3765 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003766 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003767 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003768 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3769 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3770 __ movl(temp, value.AsRegister<CpuRegister>());
3771 __ PoisonHeapReference(temp);
3772 __ movl(Address(base, offset), temp);
3773 } else {
3774 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3775 }
Mark Mendell40741f32015-04-20 22:10:34 -04003776 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003777 break;
3778 }
3779
3780 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003781 if (value.IsConstant()) {
3782 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3783 DCHECK(IsInt<32>(v));
3784 int32_t v_32 = v;
3785 __ movq(Address(base, offset), Immediate(v_32));
3786 } else {
3787 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3788 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003789 break;
3790 }
3791
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003792 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003793 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003794 break;
3795 }
3796
3797 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003798 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003799 break;
3800 }
3801
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003802 case Primitive::kPrimVoid:
3803 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003804 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003805 }
Calin Juravle52c48962014-12-16 17:02:57 +00003806
Calin Juravle77520bc2015-01-12 18:45:46 +00003807 codegen_->MaybeRecordImplicitNullCheck(instruction);
3808
3809 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3810 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3811 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003812 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003813 }
3814
Calin Juravle52c48962014-12-16 17:02:57 +00003815 if (is_volatile) {
3816 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3817 }
3818}
3819
3820void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3821 HandleFieldSet(instruction, instruction->GetFieldInfo());
3822}
3823
3824void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003825 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003826}
3827
3828void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003829 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003830}
3831
3832void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003833 HandleFieldGet(instruction, instruction->GetFieldInfo());
3834}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003835
Calin Juravle52c48962014-12-16 17:02:57 +00003836void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3837 HandleFieldGet(instruction);
3838}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003839
Calin Juravle52c48962014-12-16 17:02:57 +00003840void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3841 HandleFieldGet(instruction, instruction->GetFieldInfo());
3842}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003843
Calin Juravle52c48962014-12-16 17:02:57 +00003844void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3845 HandleFieldSet(instruction, instruction->GetFieldInfo());
3846}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003847
Calin Juravle52c48962014-12-16 17:02:57 +00003848void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003849 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003850}
3851
3852void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003853 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3854 ? LocationSummary::kCallOnSlowPath
3855 : LocationSummary::kNoCall;
3856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3857 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003858 ? Location::RequiresRegister()
3859 : Location::Any();
3860 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003861 if (instruction->HasUses()) {
3862 locations->SetOut(Location::SameAsFirstInput());
3863 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003864}
3865
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003866void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003867 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3868 return;
3869 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003870 LocationSummary* locations = instruction->GetLocations();
3871 Location obj = locations->InAt(0);
3872
3873 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3874 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3875}
3876
3877void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003878 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003879 codegen_->AddSlowPath(slow_path);
3880
3881 LocationSummary* locations = instruction->GetLocations();
3882 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003883
3884 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003885 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003886 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003887 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003888 } else {
3889 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00003890 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003891 __ jmp(slow_path->GetEntryLabel());
3892 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003893 }
3894 __ j(kEqual, slow_path->GetEntryLabel());
3895}
3896
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003897void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003898 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003899 GenerateImplicitNullCheck(instruction);
3900 } else {
3901 GenerateExplicitNullCheck(instruction);
3902 }
3903}
3904
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003905void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003906 LocationSummary* locations =
3907 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003908 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003909 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003910 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3911 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3912 } else {
3913 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3914 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003915}
3916
3917void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3918 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003919 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003920 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003921 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922
Roland Levillain4d027112015-07-01 15:41:14 +01003923 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003924 case Primitive::kPrimBoolean: {
3925 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003926 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003927 if (index.IsConstant()) {
3928 __ movzxb(out, Address(obj,
3929 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3930 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003931 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003932 }
3933 break;
3934 }
3935
3936 case Primitive::kPrimByte: {
3937 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003938 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003939 if (index.IsConstant()) {
3940 __ movsxb(out, Address(obj,
3941 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3942 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003943 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003944 }
3945 break;
3946 }
3947
3948 case Primitive::kPrimShort: {
3949 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003950 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003951 if (index.IsConstant()) {
3952 __ movsxw(out, Address(obj,
3953 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3954 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003955 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003956 }
3957 break;
3958 }
3959
3960 case Primitive::kPrimChar: {
3961 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003962 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003963 if (index.IsConstant()) {
3964 __ movzxw(out, Address(obj,
3965 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3966 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003967 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003968 }
3969 break;
3970 }
3971
3972 case Primitive::kPrimInt:
3973 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003974 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3975 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003976 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003977 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003978 if (index.IsConstant()) {
3979 __ movl(out, Address(obj,
3980 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3981 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003982 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003983 }
3984 break;
3985 }
3986
3987 case Primitive::kPrimLong: {
3988 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003989 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003990 if (index.IsConstant()) {
3991 __ movq(out, Address(obj,
3992 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3993 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003994 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003995 }
3996 break;
3997 }
3998
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003999 case Primitive::kPrimFloat: {
4000 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004001 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004002 if (index.IsConstant()) {
4003 __ movss(out, Address(obj,
4004 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4005 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004006 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004007 }
4008 break;
4009 }
4010
4011 case Primitive::kPrimDouble: {
4012 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004013 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004014 if (index.IsConstant()) {
4015 __ movsd(out, Address(obj,
4016 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4017 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004018 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004019 }
4020 break;
4021 }
4022
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004023 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004024 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004025 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004026 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004027 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004028
4029 if (type == Primitive::kPrimNot) {
4030 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4031 __ MaybeUnpoisonHeapReference(out);
4032 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004033}
4034
4035void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004036 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004037
4038 bool needs_write_barrier =
4039 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004040 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004041
Nicolas Geoffray39468442014-09-02 15:17:15 +01004042 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004043 instruction,
4044 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004045
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004046 locations->SetInAt(0, Location::RequiresRegister());
4047 locations->SetInAt(
4048 1, Location::RegisterOrConstant(instruction->InputAt(1)));
4049 locations->SetInAt(2, Location::RequiresRegister());
4050 if (value_type == Primitive::kPrimLong) {
4051 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
4052 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
4053 locations->SetInAt(2, Location::RequiresFpuRegister());
4054 } else {
4055 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4056 }
4057
4058 if (needs_write_barrier) {
4059 // Temporary registers for the write barrier.
4060 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4061 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004062 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004063}
4064
4065void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4066 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004067 CpuRegister array = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004068 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004069 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004070 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004071 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004072 bool needs_write_barrier =
4073 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004074 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4075 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4076 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004077
4078 switch (value_type) {
4079 case Primitive::kPrimBoolean:
4080 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004081 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4082 Address address = index.IsConstant()
4083 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4084 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4085 if (value.IsRegister()) {
4086 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004087 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004088 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004089 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004090 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004091 break;
4092 }
4093
4094 case Primitive::kPrimShort:
4095 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004096 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4097 Address address = index.IsConstant()
4098 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4099 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4100 if (value.IsRegister()) {
4101 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004102 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004103 DCHECK(value.IsConstant()) << value;
4104 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004105 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004106 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004107 break;
4108 }
4109
4110 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004111 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4112 Address address = index.IsConstant()
4113 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4114 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4115 if (!value.IsRegister()) {
4116 // Just setting null.
4117 DCHECK(instruction->InputAt(2)->IsNullConstant());
4118 DCHECK(value.IsConstant()) << value;
4119 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004120 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004121 DCHECK(!needs_write_barrier);
4122 DCHECK(!may_need_runtime_call);
4123 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004124 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004125
4126 DCHECK(needs_write_barrier);
4127 CpuRegister register_value = value.AsRegister<CpuRegister>();
4128 NearLabel done, not_null, do_put;
4129 SlowPathCode* slow_path = nullptr;
4130 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4131 if (may_need_runtime_call) {
4132 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4133 codegen_->AddSlowPath(slow_path);
4134 if (instruction->GetValueCanBeNull()) {
4135 __ testl(register_value, register_value);
4136 __ j(kNotEqual, &not_null);
4137 __ movl(address, Immediate(0));
4138 codegen_->MaybeRecordImplicitNullCheck(instruction);
4139 __ jmp(&done);
4140 __ Bind(&not_null);
4141 }
4142
4143 __ movl(temp, Address(array, class_offset));
4144 codegen_->MaybeRecordImplicitNullCheck(instruction);
4145 __ MaybeUnpoisonHeapReference(temp);
4146 __ movl(temp, Address(temp, component_offset));
4147 // No need to poison/unpoison, we're comparing two poisoned references.
4148 __ cmpl(temp, Address(register_value, class_offset));
4149 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4150 __ j(kEqual, &do_put);
4151 __ MaybeUnpoisonHeapReference(temp);
4152 __ movl(temp, Address(temp, super_offset));
4153 // No need to unpoison the result, we're comparing against null.
4154 __ testl(temp, temp);
4155 __ j(kNotEqual, slow_path->GetEntryLabel());
4156 __ Bind(&do_put);
4157 } else {
4158 __ j(kNotEqual, slow_path->GetEntryLabel());
4159 }
4160 }
4161
4162 if (kPoisonHeapReferences) {
4163 __ movl(temp, register_value);
4164 __ PoisonHeapReference(temp);
4165 __ movl(address, temp);
4166 } else {
4167 __ movl(address, register_value);
4168 }
4169 if (!may_need_runtime_call) {
4170 codegen_->MaybeRecordImplicitNullCheck(instruction);
4171 }
4172
4173 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4174 codegen_->MarkGCCard(
4175 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4176 __ Bind(&done);
4177
4178 if (slow_path != nullptr) {
4179 __ Bind(slow_path->GetExitLabel());
4180 }
4181
4182 break;
4183 }
4184 case Primitive::kPrimInt: {
4185 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4186 Address address = index.IsConstant()
4187 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4188 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4189 if (value.IsRegister()) {
4190 __ movl(address, value.AsRegister<CpuRegister>());
4191 } else {
4192 DCHECK(value.IsConstant()) << value;
4193 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4194 __ movl(address, Immediate(v));
4195 }
4196 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004197 break;
4198 }
4199
4200 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004201 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4202 Address address = index.IsConstant()
4203 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4204 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4205 if (value.IsRegister()) {
4206 __ movq(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004207 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004208 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4209 DCHECK(IsInt<32>(v));
4210 int32_t v_32 = v;
4211 __ movq(address, Immediate(v_32));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004212 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004213 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004214 break;
4215 }
4216
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004217 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004218 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4219 Address address = index.IsConstant()
4220 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4221 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4222 DCHECK(value.IsFpuRegister());
4223 __ movss(address, value.AsFpuRegister<XmmRegister>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004224 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004225 break;
4226 }
4227
4228 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004229 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4230 Address address = index.IsConstant()
4231 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4232 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4233 DCHECK(value.IsFpuRegister());
4234 __ movsd(address, value.AsFpuRegister<XmmRegister>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004235 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004236 break;
4237 }
4238
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004239 case Primitive::kPrimVoid:
4240 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004241 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004242 }
4243}
4244
4245void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004246 LocationSummary* locations =
4247 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004248 locations->SetInAt(0, Location::RequiresRegister());
4249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004250}
4251
4252void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4253 LocationSummary* locations = instruction->GetLocations();
4254 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004255 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4256 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004257 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004258 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004259}
4260
4261void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004262 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4263 ? LocationSummary::kCallOnSlowPath
4264 : LocationSummary::kNoCall;
4265 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004266 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004267 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004268 if (instruction->HasUses()) {
4269 locations->SetOut(Location::SameAsFirstInput());
4270 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004271}
4272
4273void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4274 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004275 Location index_loc = locations->InAt(0);
4276 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004277 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004278 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004279
Mark Mendell99dbd682015-04-22 16:18:52 -04004280 if (length_loc.IsConstant()) {
4281 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4282 if (index_loc.IsConstant()) {
4283 // BCE will remove the bounds check if we are guarenteed to pass.
4284 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4285 if (index < 0 || index >= length) {
4286 codegen_->AddSlowPath(slow_path);
4287 __ jmp(slow_path->GetEntryLabel());
4288 } else {
4289 // Some optimization after BCE may have generated this, and we should not
4290 // generate a bounds check if it is a valid range.
4291 }
4292 return;
4293 }
4294
4295 // We have to reverse the jump condition because the length is the constant.
4296 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4297 __ cmpl(index_reg, Immediate(length));
4298 codegen_->AddSlowPath(slow_path);
4299 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004300 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004301 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4302 if (index_loc.IsConstant()) {
4303 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4304 __ cmpl(length, Immediate(value));
4305 } else {
4306 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4307 }
4308 codegen_->AddSlowPath(slow_path);
4309 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004310 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004311}
4312
4313void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4314 CpuRegister card,
4315 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004316 CpuRegister value,
4317 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004318 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004319 if (value_can_be_null) {
4320 __ testl(value, value);
4321 __ j(kEqual, &is_null);
4322 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004323 __ gs()->movq(card, Address::Absolute(
4324 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4325 __ movq(temp, object);
4326 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004327 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004328 if (value_can_be_null) {
4329 __ Bind(&is_null);
4330 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004331}
4332
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004333void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4334 temp->SetLocations(nullptr);
4335}
4336
4337void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4338 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004339 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004340}
4341
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004342void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004343 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004344 LOG(FATAL) << "Unimplemented";
4345}
4346
4347void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004348 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4349}
4350
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004351void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4352 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4353}
4354
4355void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004356 HBasicBlock* block = instruction->GetBlock();
4357 if (block->GetLoopInformation() != nullptr) {
4358 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4359 // The back edge will generate the suspend check.
4360 return;
4361 }
4362 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4363 // The goto will generate the suspend check.
4364 return;
4365 }
4366 GenerateSuspendCheck(instruction, nullptr);
4367}
4368
4369void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4370 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004371 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004372 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4373 if (slow_path == nullptr) {
4374 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4375 instruction->SetSlowPath(slow_path);
4376 codegen_->AddSlowPath(slow_path);
4377 if (successor != nullptr) {
4378 DCHECK(successor->IsLoopHeader());
4379 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4380 }
4381 } else {
4382 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4383 }
4384
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004385 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004386 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004387 if (successor == nullptr) {
4388 __ j(kNotEqual, slow_path->GetEntryLabel());
4389 __ Bind(slow_path->GetReturnLabel());
4390 } else {
4391 __ j(kEqual, codegen_->GetLabelOf(successor));
4392 __ jmp(slow_path->GetEntryLabel());
4393 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004394}
4395
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004396X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4397 return codegen_->GetAssembler();
4398}
4399
4400void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004401 DCHECK_LT(index, moves_.size());
4402 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004403 Location source = move->GetSource();
4404 Location destination = move->GetDestination();
4405
4406 if (source.IsRegister()) {
4407 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004408 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004409 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004410 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004411 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004412 } else {
4413 DCHECK(destination.IsDoubleStackSlot());
4414 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004415 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004416 }
4417 } else if (source.IsStackSlot()) {
4418 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004419 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004420 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004421 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004422 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004423 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004424 } else {
4425 DCHECK(destination.IsStackSlot());
4426 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4427 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4428 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004429 } else if (source.IsDoubleStackSlot()) {
4430 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004431 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004432 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004433 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004434 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4435 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004436 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004437 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004438 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4439 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4440 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004441 } else if (source.IsConstant()) {
4442 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004443 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4444 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004445 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004446 if (value == 0) {
4447 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4448 } else {
4449 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4450 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004451 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004452 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004453 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004454 }
4455 } else if (constant->IsLongConstant()) {
4456 int64_t value = constant->AsLongConstant()->GetValue();
4457 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004458 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004459 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004460 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004461 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004462 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004463 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004464 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004465 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004466 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004467 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4468 if (value == 0) {
4469 // easy FP 0.0.
4470 __ xorps(dest, dest);
4471 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004472 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004473 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004474 } else {
4475 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004476 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004477 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4478 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004479 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004480 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004481 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004482 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004483 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004484 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4485 if (value == 0) {
4486 __ xorpd(dest, dest);
4487 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004488 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004489 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004490 } else {
4491 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004492 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004493 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004494 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004495 } else if (source.IsFpuRegister()) {
4496 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004497 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004498 } else if (destination.IsStackSlot()) {
4499 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004500 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004501 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004502 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004503 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004504 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004505 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004506 }
4507}
4508
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004509void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004510 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004511 __ movl(Address(CpuRegister(RSP), mem), reg);
4512 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004513}
4514
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004515void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004516 ScratchRegisterScope ensure_scratch(
4517 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4518
4519 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4520 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4521 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4522 Address(CpuRegister(RSP), mem2 + stack_offset));
4523 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4524 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4525 CpuRegister(ensure_scratch.GetRegister()));
4526}
4527
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004528void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4529 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4530 __ movq(Address(CpuRegister(RSP), mem), reg);
4531 __ movq(reg, CpuRegister(TMP));
4532}
4533
4534void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4535 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004536 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004537
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004538 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4539 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4540 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4541 Address(CpuRegister(RSP), mem2 + stack_offset));
4542 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4543 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4544 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004545}
4546
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004547void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4548 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4549 __ movss(Address(CpuRegister(RSP), mem), reg);
4550 __ movd(reg, CpuRegister(TMP));
4551}
4552
4553void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4554 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4555 __ movsd(Address(CpuRegister(RSP), mem), reg);
4556 __ movd(reg, CpuRegister(TMP));
4557}
4558
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004559void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004560 DCHECK_LT(index, moves_.size());
4561 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004562 Location source = move->GetSource();
4563 Location destination = move->GetDestination();
4564
4565 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004566 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004567 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004568 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004569 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004570 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004571 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004572 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4573 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004574 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004575 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004576 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004577 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4578 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004579 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004580 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4581 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4582 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004583 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004584 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004585 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004586 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004587 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004588 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004589 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004590 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004591 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004592 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004593 }
4594}
4595
4596
4597void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4598 __ pushq(CpuRegister(reg));
4599}
4600
4601
4602void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4603 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004604}
4605
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004606void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004607 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004608 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4609 Immediate(mirror::Class::kStatusInitialized));
4610 __ j(kLess, slow_path->GetEntryLabel());
4611 __ Bind(slow_path->GetExitLabel());
4612 // No need for memory fence, thanks to the X86_64 memory model.
4613}
4614
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004615void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004616 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4617 ? LocationSummary::kCallOnSlowPath
4618 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004619 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004620 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004621 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004622 locations->SetOut(Location::RequiresRegister());
4623}
4624
4625void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004626 LocationSummary* locations = cls->GetLocations();
4627 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4628 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004629 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004630 DCHECK(!cls->CanCallRuntime());
4631 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004632 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004633 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004634 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004635 __ movq(out, Address(
4636 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004637 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004638 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004639
Andreas Gampe85b62f22015-09-09 13:15:38 -07004640 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004641 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4642 codegen_->AddSlowPath(slow_path);
4643 __ testl(out, out);
4644 __ j(kEqual, slow_path->GetEntryLabel());
4645 if (cls->MustGenerateClinitCheck()) {
4646 GenerateClassInitializationCheck(slow_path, out);
4647 } else {
4648 __ Bind(slow_path->GetExitLabel());
4649 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004650 }
4651}
4652
4653void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4654 LocationSummary* locations =
4655 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4656 locations->SetInAt(0, Location::RequiresRegister());
4657 if (check->HasUses()) {
4658 locations->SetOut(Location::SameAsFirstInput());
4659 }
4660}
4661
4662void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004663 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004664 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004665 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004666 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004667 GenerateClassInitializationCheck(slow_path,
4668 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004669}
4670
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004671void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4672 LocationSummary* locations =
4673 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004674 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004675 locations->SetOut(Location::RequiresRegister());
4676}
4677
4678void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004679 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004680 codegen_->AddSlowPath(slow_path);
4681
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004682 LocationSummary* locations = load->GetLocations();
4683 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4684 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004685 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004686 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004687 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004688 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004689 __ testl(out, out);
4690 __ j(kEqual, slow_path->GetEntryLabel());
4691 __ Bind(slow_path->GetExitLabel());
4692}
4693
David Brazdilcb1c0552015-08-04 16:22:25 +01004694static Address GetExceptionTlsAddress() {
4695 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4696}
4697
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004698void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4699 LocationSummary* locations =
4700 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4701 locations->SetOut(Location::RequiresRegister());
4702}
4703
4704void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004705 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4706}
4707
4708void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4709 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4710}
4711
4712void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4713 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004714}
4715
4716void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4717 LocationSummary* locations =
4718 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4719 InvokeRuntimeCallingConvention calling_convention;
4720 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4721}
4722
4723void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004724 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4725 instruction,
4726 instruction->GetDexPc(),
4727 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004728}
4729
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004730void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004731 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4732 switch (instruction->GetTypeCheckKind()) {
4733 case TypeCheckKind::kExactCheck:
4734 case TypeCheckKind::kAbstractClassCheck:
4735 case TypeCheckKind::kClassHierarchyCheck:
4736 case TypeCheckKind::kArrayObjectCheck:
4737 call_kind = LocationSummary::kNoCall;
4738 break;
4739 case TypeCheckKind::kInterfaceCheck:
4740 call_kind = LocationSummary::kCall;
4741 break;
4742 case TypeCheckKind::kArrayCheck:
4743 call_kind = LocationSummary::kCallOnSlowPath;
4744 break;
4745 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004746 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004747 if (call_kind != LocationSummary::kCall) {
4748 locations->SetInAt(0, Location::RequiresRegister());
4749 locations->SetInAt(1, Location::Any());
4750 // Note that TypeCheckSlowPathX86_64 uses this register too.
4751 locations->SetOut(Location::RequiresRegister());
4752 } else {
4753 InvokeRuntimeCallingConvention calling_convention;
4754 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4755 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4756 locations->SetOut(Location::RegisterLocation(RAX));
4757 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004758}
4759
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004760void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004761 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004762 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004763 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004764 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004765 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004766 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4767 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4768 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004769 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004770 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004771
4772 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004773 // Avoid null check if we know obj is not null.
4774 if (instruction->MustDoNullCheck()) {
4775 __ testl(obj, obj);
4776 __ j(kEqual, &zero);
4777 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004778
4779 // In case of an interface check, we put the object class into the object register.
4780 // This is safe, as the register is caller-save, and the object must be in another
4781 // register if it survives the runtime call.
4782 CpuRegister target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
4783 ? obj
4784 : out;
4785 __ movl(target, Address(obj, class_offset));
4786 __ MaybeUnpoisonHeapReference(target);
4787
4788 switch (instruction->GetTypeCheckKind()) {
4789 case TypeCheckKind::kExactCheck: {
4790 if (cls.IsRegister()) {
4791 __ cmpl(out, cls.AsRegister<CpuRegister>());
4792 } else {
4793 DCHECK(cls.IsStackSlot()) << cls;
4794 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4795 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004796 if (zero.IsLinked()) {
4797 // Classes must be equal for the instanceof to succeed.
4798 __ j(kNotEqual, &zero);
4799 __ movl(out, Immediate(1));
4800 __ jmp(&done);
4801 } else {
4802 __ setcc(kEqual, out);
4803 // setcc only sets the low byte.
4804 __ andl(out, Immediate(1));
4805 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004806 break;
4807 }
4808 case TypeCheckKind::kAbstractClassCheck: {
4809 // If the class is abstract, we eagerly fetch the super class of the
4810 // object to avoid doing a comparison we know will fail.
4811 NearLabel loop, success;
4812 __ Bind(&loop);
4813 __ movl(out, Address(out, super_offset));
4814 __ MaybeUnpoisonHeapReference(out);
4815 __ testl(out, out);
4816 // If `out` is null, we use it for the result, and jump to `done`.
4817 __ j(kEqual, &done);
4818 if (cls.IsRegister()) {
4819 __ cmpl(out, cls.AsRegister<CpuRegister>());
4820 } else {
4821 DCHECK(cls.IsStackSlot()) << cls;
4822 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4823 }
4824 __ j(kNotEqual, &loop);
4825 __ movl(out, Immediate(1));
4826 if (zero.IsLinked()) {
4827 __ jmp(&done);
4828 }
4829 break;
4830 }
4831 case TypeCheckKind::kClassHierarchyCheck: {
4832 // Walk over the class hierarchy to find a match.
4833 NearLabel loop, success;
4834 __ Bind(&loop);
4835 if (cls.IsRegister()) {
4836 __ cmpl(out, cls.AsRegister<CpuRegister>());
4837 } else {
4838 DCHECK(cls.IsStackSlot()) << cls;
4839 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4840 }
4841 __ j(kEqual, &success);
4842 __ movl(out, Address(out, super_offset));
4843 __ MaybeUnpoisonHeapReference(out);
4844 __ testl(out, out);
4845 __ j(kNotEqual, &loop);
4846 // If `out` is null, we use it for the result, and jump to `done`.
4847 __ jmp(&done);
4848 __ Bind(&success);
4849 __ movl(out, Immediate(1));
4850 if (zero.IsLinked()) {
4851 __ jmp(&done);
4852 }
4853 break;
4854 }
4855 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004856 // Do an exact check.
4857 NearLabel exact_check;
4858 if (cls.IsRegister()) {
4859 __ cmpl(out, cls.AsRegister<CpuRegister>());
4860 } else {
4861 DCHECK(cls.IsStackSlot()) << cls;
4862 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4863 }
4864 __ j(kEqual, &exact_check);
4865 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004866 __ movl(out, Address(out, component_offset));
4867 __ MaybeUnpoisonHeapReference(out);
4868 __ testl(out, out);
4869 // If `out` is null, we use it for the result, and jump to `done`.
4870 __ j(kEqual, &done);
4871 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
4872 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004873 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004874 __ movl(out, Immediate(1));
4875 __ jmp(&done);
4876 break;
4877 }
4878 case TypeCheckKind::kArrayCheck: {
4879 if (cls.IsRegister()) {
4880 __ cmpl(out, cls.AsRegister<CpuRegister>());
4881 } else {
4882 DCHECK(cls.IsStackSlot()) << cls;
4883 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4884 }
4885 DCHECK(locations->OnlyCallsOnSlowPath());
4886 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4887 instruction, /* is_fatal */ false);
4888 codegen_->AddSlowPath(slow_path);
4889 __ j(kNotEqual, slow_path->GetEntryLabel());
4890 __ movl(out, Immediate(1));
4891 if (zero.IsLinked()) {
4892 __ jmp(&done);
4893 }
4894 break;
4895 }
4896
4897 case TypeCheckKind::kInterfaceCheck:
4898 default: {
4899 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4900 instruction,
4901 instruction->GetDexPc(),
4902 nullptr);
4903 if (zero.IsLinked()) {
4904 __ jmp(&done);
4905 }
4906 break;
4907 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004908 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004909
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004910 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004911 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004912 __ xorl(out, out);
4913 }
4914
4915 if (done.IsLinked()) {
4916 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004917 }
4918
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004919 if (slow_path != nullptr) {
4920 __ Bind(slow_path->GetExitLabel());
4921 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004922}
4923
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004924void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004925 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4926 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4927
4928 switch (instruction->GetTypeCheckKind()) {
4929 case TypeCheckKind::kExactCheck:
4930 case TypeCheckKind::kAbstractClassCheck:
4931 case TypeCheckKind::kClassHierarchyCheck:
4932 case TypeCheckKind::kArrayObjectCheck:
4933 call_kind = throws_into_catch
4934 ? LocationSummary::kCallOnSlowPath
4935 : LocationSummary::kNoCall;
4936 break;
4937 case TypeCheckKind::kInterfaceCheck:
4938 call_kind = LocationSummary::kCall;
4939 break;
4940 case TypeCheckKind::kArrayCheck:
4941 call_kind = LocationSummary::kCallOnSlowPath;
4942 break;
4943 }
4944
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004945 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004946 instruction, call_kind);
4947 if (call_kind != LocationSummary::kCall) {
4948 locations->SetInAt(0, Location::RequiresRegister());
4949 locations->SetInAt(1, Location::Any());
4950 // Note that TypeCheckSlowPathX86_64 uses this register too.
4951 locations->AddTemp(Location::RequiresRegister());
4952 } else {
4953 InvokeRuntimeCallingConvention calling_convention;
4954 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4955 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4956 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004957}
4958
4959void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4960 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004961 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004962 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004963 CpuRegister temp = locations->WillCall()
4964 ? CpuRegister(kNoRegister)
4965 : locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004966
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004967 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4968 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4969 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4970 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4971 SlowPathCode* slow_path = nullptr;
4972
4973 if (!locations->WillCall()) {
4974 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4975 instruction, !locations->CanCall());
4976 codegen_->AddSlowPath(slow_path);
4977 }
4978
4979 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004980 // Avoid null check if we know obj is not null.
4981 if (instruction->MustDoNullCheck()) {
4982 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004983 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004984 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004985
4986 if (locations->WillCall()) {
4987 __ movl(obj, Address(obj, class_offset));
4988 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004989 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004990 __ movl(temp, Address(obj, class_offset));
4991 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004992 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004993
4994 switch (instruction->GetTypeCheckKind()) {
4995 case TypeCheckKind::kExactCheck:
4996 case TypeCheckKind::kArrayCheck: {
4997 if (cls.IsRegister()) {
4998 __ cmpl(temp, cls.AsRegister<CpuRegister>());
4999 } else {
5000 DCHECK(cls.IsStackSlot()) << cls;
5001 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5002 }
5003 // Jump to slow path for throwing the exception or doing a
5004 // more involved array check.
5005 __ j(kNotEqual, slow_path->GetEntryLabel());
5006 break;
5007 }
5008 case TypeCheckKind::kAbstractClassCheck: {
5009 // If the class is abstract, we eagerly fetch the super class of the
5010 // object to avoid doing a comparison we know will fail.
5011 NearLabel loop;
5012 __ Bind(&loop);
5013 __ movl(temp, Address(temp, super_offset));
5014 __ MaybeUnpoisonHeapReference(temp);
5015 __ testl(temp, temp);
5016 // Jump to the slow path to throw the exception.
5017 __ j(kEqual, slow_path->GetEntryLabel());
5018 if (cls.IsRegister()) {
5019 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5020 } else {
5021 DCHECK(cls.IsStackSlot()) << cls;
5022 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5023 }
5024 __ j(kNotEqual, &loop);
5025 break;
5026 }
5027 case TypeCheckKind::kClassHierarchyCheck: {
5028 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005029 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005030 __ Bind(&loop);
5031 if (cls.IsRegister()) {
5032 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5033 } else {
5034 DCHECK(cls.IsStackSlot()) << cls;
5035 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5036 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005037 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005038 __ movl(temp, Address(temp, super_offset));
5039 __ MaybeUnpoisonHeapReference(temp);
5040 __ testl(temp, temp);
5041 __ j(kNotEqual, &loop);
5042 // Jump to the slow path to throw the exception.
5043 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005044 break;
5045 }
5046 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005047 // Do an exact check.
5048 if (cls.IsRegister()) {
5049 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5050 } else {
5051 DCHECK(cls.IsStackSlot()) << cls;
5052 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5053 }
5054 __ j(kEqual, &done);
5055 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005056 __ movl(temp, Address(temp, component_offset));
5057 __ MaybeUnpoisonHeapReference(temp);
5058 __ testl(temp, temp);
5059 __ j(kEqual, slow_path->GetEntryLabel());
5060 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5061 __ j(kNotEqual, slow_path->GetEntryLabel());
5062 break;
5063 }
5064 case TypeCheckKind::kInterfaceCheck:
5065 default:
5066 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5067 instruction,
5068 instruction->GetDexPc(),
5069 nullptr);
5070 break;
5071 }
5072 __ Bind(&done);
5073
5074 if (slow_path != nullptr) {
5075 __ Bind(slow_path->GetExitLabel());
5076 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005077}
5078
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005079void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5080 LocationSummary* locations =
5081 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5082 InvokeRuntimeCallingConvention calling_convention;
5083 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5084}
5085
5086void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005087 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5088 : QUICK_ENTRY_POINT(pUnlockObject),
5089 instruction,
5090 instruction->GetDexPc(),
5091 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005092}
5093
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005094void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5095void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5096void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5097
5098void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5099 LocationSummary* locations =
5100 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5101 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5102 || instruction->GetResultType() == Primitive::kPrimLong);
5103 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005104 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005105 locations->SetOut(Location::SameAsFirstInput());
5106}
5107
5108void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5109 HandleBitwiseOperation(instruction);
5110}
5111
5112void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5113 HandleBitwiseOperation(instruction);
5114}
5115
5116void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5117 HandleBitwiseOperation(instruction);
5118}
5119
5120void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5121 LocationSummary* locations = instruction->GetLocations();
5122 Location first = locations->InAt(0);
5123 Location second = locations->InAt(1);
5124 DCHECK(first.Equals(locations->Out()));
5125
5126 if (instruction->GetResultType() == Primitive::kPrimInt) {
5127 if (second.IsRegister()) {
5128 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005129 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005130 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005131 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005132 } else {
5133 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005134 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005135 }
5136 } else if (second.IsConstant()) {
5137 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5138 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005139 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005140 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005141 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005142 } else {
5143 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005144 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005145 }
5146 } else {
5147 Address address(CpuRegister(RSP), second.GetStackIndex());
5148 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005149 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005150 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005151 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005152 } else {
5153 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005154 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005155 }
5156 }
5157 } else {
5158 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005159 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5160 bool second_is_constant = false;
5161 int64_t value = 0;
5162 if (second.IsConstant()) {
5163 second_is_constant = true;
5164 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005165 }
Mark Mendell40741f32015-04-20 22:10:34 -04005166 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005167
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005168 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005169 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005170 if (is_int32_value) {
5171 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5172 } else {
5173 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5174 }
5175 } else if (second.IsDoubleStackSlot()) {
5176 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005177 } else {
5178 __ andq(first_reg, second.AsRegister<CpuRegister>());
5179 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005180 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005181 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005182 if (is_int32_value) {
5183 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5184 } else {
5185 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5186 }
5187 } else if (second.IsDoubleStackSlot()) {
5188 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005189 } else {
5190 __ orq(first_reg, second.AsRegister<CpuRegister>());
5191 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005192 } else {
5193 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005194 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005195 if (is_int32_value) {
5196 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5197 } else {
5198 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5199 }
5200 } else if (second.IsDoubleStackSlot()) {
5201 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005202 } else {
5203 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5204 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005205 }
5206 }
5207}
5208
Calin Juravleb1498f62015-02-16 13:13:29 +00005209void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
5210 // Nothing to do, this should be removed during prepare for register allocator.
5211 UNUSED(instruction);
5212 LOG(FATAL) << "Unreachable";
5213}
5214
5215void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
5216 // Nothing to do, this should be removed during prepare for register allocator.
5217 UNUSED(instruction);
5218 LOG(FATAL) << "Unreachable";
5219}
5220
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005221void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
5222 DCHECK(codegen_->IsBaseline());
5223 LocationSummary* locations =
5224 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5225 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5226}
5227
5228void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5229 DCHECK(codegen_->IsBaseline());
5230 // Will be generated at use site.
5231}
5232
Mark Mendellfe57faa2015-09-18 09:26:15 -04005233// Simple implementation of packed switch - generate cascaded compare/jumps.
5234void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5235 LocationSummary* locations =
5236 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5237 locations->SetInAt(0, Location::RequiresRegister());
5238}
5239
5240void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5241 int32_t lower_bound = switch_instr->GetStartValue();
5242 int32_t num_entries = switch_instr->GetNumEntries();
5243 LocationSummary* locations = switch_instr->GetLocations();
5244 CpuRegister value_reg = locations->InAt(0).AsRegister<CpuRegister>();
5245 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5246
5247 // Create a series of compare/jumps.
5248 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5249 for (int i = 0; i < num_entries; i++) {
5250 int32_t case_value = lower_bound + i;
5251 if (case_value == 0) {
5252 __ testl(value_reg, value_reg);
5253 } else {
5254 __ cmpl(value_reg, Immediate(case_value));
5255 }
5256 __ j(kEqual, codegen_->GetLabelOf(successors.at(i)));
5257 }
5258
5259 // And the default for any other value.
5260 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5261 __ jmp(codegen_->GetLabelOf(default_block));
5262 }
5263}
5264
Mark Mendell92e83bf2015-05-07 11:25:03 -04005265void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
5266 if (value == 0) {
5267 __ xorl(dest, dest);
5268 } else if (value > 0 && IsInt<32>(value)) {
5269 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
5270 __ movl(dest, Immediate(static_cast<int32_t>(value)));
5271 } else {
5272 __ movq(dest, Immediate(value));
5273 }
5274}
5275
Mark Mendellcfa410b2015-05-25 16:02:44 -04005276void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
5277 DCHECK(dest.IsDoubleStackSlot());
5278 if (IsInt<32>(value)) {
5279 // Can move directly as an int32 constant.
5280 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
5281 Immediate(static_cast<int32_t>(value)));
5282 } else {
5283 Load64BitValue(CpuRegister(TMP), value);
5284 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
5285 }
5286}
5287
Mark Mendellf55c3e02015-03-26 21:07:46 -04005288void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
5289 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04005290 X86_64Assembler* assembler = GetAssembler();
5291 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04005292 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5293 // byte values. If used for vectors at a later time, this will need to be
5294 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04005295 assembler->Align(4, 0);
5296 constant_area_start_ = assembler->CodeSize();
5297 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04005298 }
5299
5300 // And finish up.
5301 CodeGenerator::Finalize(allocator);
5302}
5303
5304/**
5305 * Class to handle late fixup of offsets into constant area.
5306 */
5307class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5308 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04005309 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04005310 : codegen_(codegen), offset_into_constant_area_(offset) {}
5311
5312 private:
5313 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5314 // Patch the correct offset for the instruction. We use the address of the
5315 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
5316 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5317 int relative_position = constant_offset - pos;
5318
5319 // Patch in the right value.
5320 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5321 }
5322
Mark Mendell39dcf552015-04-09 20:42:42 -04005323 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04005324
5325 // Location in constant area that the fixup refers to.
5326 int offset_into_constant_area_;
5327};
5328
5329Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
5330 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5331 return Address::RIP(fixup);
5332}
5333
5334Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
5335 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5336 return Address::RIP(fixup);
5337}
5338
5339Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
5340 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5341 return Address::RIP(fixup);
5342}
5343
5344Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
5345 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5346 return Address::RIP(fixup);
5347}
5348
Andreas Gampe85b62f22015-09-09 13:15:38 -07005349// TODO: trg as memory.
5350void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5351 if (!trg.IsValid()) {
5352 DCHECK(type == Primitive::kPrimVoid);
5353 return;
5354 }
5355
5356 DCHECK_NE(type, Primitive::kPrimVoid);
5357
5358 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
5359 if (trg.Equals(return_loc)) {
5360 return;
5361 }
5362
5363 // Let the parallel move resolver take care of all of this.
5364 HParallelMove parallel_move(GetGraph()->GetArena());
5365 parallel_move.AddMove(return_loc, trg, type, nullptr);
5366 GetMoveResolver()->EmitNativeCode(&parallel_move);
5367}
5368
Roland Levillain4d027112015-07-01 15:41:14 +01005369#undef __
5370
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005371} // namespace x86_64
5372} // namespace art