blob: 32a1db5475010c5b7394e310ad3931ee7bcbc7df [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 Geoffraye5038322014-07-04 09:41:32 +0100399#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100400#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100401
Roland Levillain4fa13f62015-07-06 18:11:54 +0100402inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700403 switch (cond) {
404 case kCondEQ: return kEqual;
405 case kCondNE: return kNotEqual;
406 case kCondLT: return kLess;
407 case kCondLE: return kLessEqual;
408 case kCondGT: return kGreater;
409 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700410 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100411 LOG(FATAL) << "Unreachable";
412 UNREACHABLE();
413}
414
415inline Condition X86_64FPCondition(IfCondition cond) {
416 switch (cond) {
417 case kCondEQ: return kEqual;
418 case kCondNE: return kNotEqual;
419 case kCondLT: return kBelow;
420 case kCondLE: return kBelowEqual;
421 case kCondGT: return kAbove;
422 case kCondGE: return kAboveEqual;
423 };
424 LOG(FATAL) << "Unreachable";
425 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700426}
427
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800428void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100429 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800430 // All registers are assumed to be correctly set up.
431
Vladimir Marko58155012015-08-19 12:49:41 +0000432 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
433 switch (invoke->GetMethodLoadKind()) {
434 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
435 // temp = thread->string_init_entrypoint
436 __ gs()->movl(temp.AsRegister<CpuRegister>(),
437 Address::Absolute(invoke->GetStringInitOffset(), true));
438 break;
439 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
440 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
441 break;
442 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
443 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
444 break;
445 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
446 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
447 method_patches_.emplace_back(invoke->GetTargetMethod());
448 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
449 break;
450 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
451 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
452 invoke->GetDexCacheArrayOffset());
453 __ movq(temp.AsRegister<CpuRegister>(),
454 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
455 // Bind the label at the end of the "movl" insn.
456 __ Bind(&pc_rel_dex_cache_patches_.back().label);
457 break;
458 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
459 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
460 Register method_reg;
461 CpuRegister reg = temp.AsRegister<CpuRegister>();
462 if (current_method.IsRegister()) {
463 method_reg = current_method.AsRegister<Register>();
464 } else {
465 DCHECK(invoke->GetLocations()->Intrinsified());
466 DCHECK(!current_method.IsValid());
467 method_reg = reg.AsRegister();
468 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
469 }
470 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100471 __ movq(reg,
472 Address(CpuRegister(method_reg),
473 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000474 // temp = temp[index_in_cache]
475 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
476 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
477 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100478 }
Vladimir Marko58155012015-08-19 12:49:41 +0000479 }
480
481 switch (invoke->GetCodePtrLocation()) {
482 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
483 __ call(&frame_entry_label_);
484 break;
485 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
486 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
487 Label* label = &relative_call_patches_.back().label;
488 __ call(label); // Bind to the patch label, override at link time.
489 __ Bind(label); // Bind the label at the end of the "call" insn.
490 break;
491 }
492 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
493 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
494 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
495 FALLTHROUGH_INTENDED;
496 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
497 // (callee_method + offset_of_quick_compiled_code)()
498 __ call(Address(callee_method.AsRegister<CpuRegister>(),
499 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
500 kX86_64WordSize).SizeValue()));
501 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000502 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800503
504 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800505}
506
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000507void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
508 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
509 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
510 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
511 LocationSummary* locations = invoke->GetLocations();
512 Location receiver = locations->InAt(0);
513 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
514 // temp = object->GetClass();
515 DCHECK(receiver.IsRegister());
516 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
517 MaybeRecordImplicitNullCheck(invoke);
518 __ MaybeUnpoisonHeapReference(temp);
519 // temp = temp->GetMethodAt(method_offset);
520 __ movq(temp, Address(temp, method_offset));
521 // call temp->GetEntryPoint();
522 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
523 kX86_64WordSize).SizeValue()));
524}
525
Vladimir Marko58155012015-08-19 12:49:41 +0000526void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
527 DCHECK(linker_patches->empty());
528 size_t size =
529 method_patches_.size() + relative_call_patches_.size() + pc_rel_dex_cache_patches_.size();
530 linker_patches->reserve(size);
531 for (const MethodPatchInfo<Label>& info : method_patches_) {
532 // The label points to the end of the "movl" instruction but the literal offset for method
533 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
534 uint32_t literal_offset = info.label.Position() - 4;
535 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
536 info.target_method.dex_file,
537 info.target_method.dex_method_index));
538 }
539 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
540 // The label points to the end of the "call" instruction but the literal offset for method
541 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
542 uint32_t literal_offset = info.label.Position() - 4;
543 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
544 info.target_method.dex_file,
545 info.target_method.dex_method_index));
546 }
547 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
548 // The label points to the end of the "mov" instruction but the literal offset for method
549 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
550 uint32_t literal_offset = info.label.Position() - 4;
551 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
552 &info.target_dex_file,
553 info.label.Position(),
554 info.element_offset));
555 }
556}
557
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100558void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100559 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100560}
561
562void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100563 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100564}
565
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100566size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
567 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
568 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100569}
570
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100571size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
572 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
573 return kX86_64WordSize;
574}
575
576size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
577 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
578 return kX86_64WordSize;
579}
580
581size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
582 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
583 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100584}
585
Calin Juravle175dc732015-08-25 15:42:32 +0100586void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
587 HInstruction* instruction,
588 uint32_t dex_pc,
589 SlowPathCode* slow_path) {
590 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
591 instruction,
592 dex_pc,
593 slow_path);
594}
595
596void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100597 HInstruction* instruction,
598 uint32_t dex_pc,
599 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100600 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100601 __ gs()->call(Address::Absolute(entry_point_offset, true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100602 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100603}
604
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000605static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000606// Use a fake return address register to mimic Quick.
607static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400608CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
609 const X86_64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100610 const CompilerOptions& compiler_options,
611 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000612 : CodeGenerator(graph,
613 kNumberOfCpuRegisters,
614 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000615 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000616 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
617 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000618 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000619 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
620 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100621 compiler_options,
622 stats),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100623 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100624 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000625 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400626 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400627 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000628 constant_area_start_(0),
629 method_patches_(graph->GetArena()->Adapter()),
630 relative_call_patches_(graph->GetArena()->Adapter()),
631 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000632 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
633}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100634
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100635InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
636 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100637 : HGraphVisitor(graph),
638 assembler_(codegen->GetAssembler()),
639 codegen_(codegen) {}
640
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100641Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100642 switch (type) {
643 case Primitive::kPrimLong:
644 case Primitive::kPrimByte:
645 case Primitive::kPrimBoolean:
646 case Primitive::kPrimChar:
647 case Primitive::kPrimShort:
648 case Primitive::kPrimInt:
649 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100650 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100651 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100652 }
653
654 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100655 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100656 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100657 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100658 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100659
660 case Primitive::kPrimVoid:
661 LOG(FATAL) << "Unreachable type " << type;
662 }
663
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100664 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100665}
666
Nicolas Geoffray98893962015-01-21 12:32:32 +0000667void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100668 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100669 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100670
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000671 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100672 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000673
Nicolas Geoffray98893962015-01-21 12:32:32 +0000674 if (is_baseline) {
675 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
676 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
677 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000678 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
679 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
680 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000681 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100682}
683
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100684static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100685 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100686}
David Srbecky9d8606d2015-04-12 09:35:32 +0100687
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100688static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100689 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100690}
691
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100692void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100693 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000694 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100695 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700696 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000697 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100698
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000699 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100700 __ testq(CpuRegister(RAX), Address(
701 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100702 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100703 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000704
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000705 if (HasEmptyFrame()) {
706 return;
707 }
708
Nicolas Geoffray98893962015-01-21 12:32:32 +0000709 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000710 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000711 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000712 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100713 __ cfi().AdjustCFAOffset(kX86_64WordSize);
714 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000715 }
716 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100717
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100718 int adjust = GetFrameSize() - GetCoreSpillSize();
719 __ subq(CpuRegister(RSP), Immediate(adjust));
720 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000721 uint32_t xmm_spill_location = GetFpuSpillStart();
722 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100723
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000724 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
725 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100726 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
727 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
728 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000729 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100730 }
731
Mathieu Chartiere401d142015-04-22 13:56:20 -0700732 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100733 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100734}
735
736void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100737 __ cfi().RememberState();
738 if (!HasEmptyFrame()) {
739 uint32_t xmm_spill_location = GetFpuSpillStart();
740 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
741 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
742 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
743 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
744 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
745 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
746 }
747 }
748
749 int adjust = GetFrameSize() - GetCoreSpillSize();
750 __ addq(CpuRegister(RSP), Immediate(adjust));
751 __ cfi().AdjustCFAOffset(-adjust);
752
753 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
754 Register reg = kCoreCalleeSaves[i];
755 if (allocated_registers_.ContainsCoreRegister(reg)) {
756 __ popq(CpuRegister(reg));
757 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
758 __ cfi().Restore(DWARFReg(reg));
759 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000760 }
761 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100762 __ ret();
763 __ cfi().RestoreState();
764 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100765}
766
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100767void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
768 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100769}
770
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100771Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
772 switch (load->GetType()) {
773 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100774 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100775 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100776
777 case Primitive::kPrimInt:
778 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100779 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100780 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100781
782 case Primitive::kPrimBoolean:
783 case Primitive::kPrimByte:
784 case Primitive::kPrimChar:
785 case Primitive::kPrimShort:
786 case Primitive::kPrimVoid:
787 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700788 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100789 }
790
791 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700792 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100793}
794
795void CodeGeneratorX86_64::Move(Location destination, Location source) {
796 if (source.Equals(destination)) {
797 return;
798 }
799 if (destination.IsRegister()) {
800 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000801 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100802 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000803 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100804 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000805 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100806 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100807 } else {
808 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000809 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100810 Address(CpuRegister(RSP), source.GetStackIndex()));
811 }
812 } else if (destination.IsFpuRegister()) {
813 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000814 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100815 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000816 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100817 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000818 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100819 Address(CpuRegister(RSP), source.GetStackIndex()));
820 } else {
821 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000822 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100823 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100824 }
825 } else if (destination.IsStackSlot()) {
826 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100827 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000828 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100829 } else if (source.IsFpuRegister()) {
830 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000831 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500832 } else if (source.IsConstant()) {
833 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000834 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500835 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100836 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500837 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000838 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
839 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100840 }
841 } else {
842 DCHECK(destination.IsDoubleStackSlot());
843 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100844 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000845 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100846 } else if (source.IsFpuRegister()) {
847 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000848 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500849 } else if (source.IsConstant()) {
850 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800851 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500852 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000853 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500854 } else {
855 DCHECK(constant->IsLongConstant());
856 value = constant->AsLongConstant()->GetValue();
857 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400858 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859 } else {
860 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000861 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
862 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100863 }
864 }
865}
866
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100867void CodeGeneratorX86_64::Move(HInstruction* instruction,
868 Location location,
869 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000870 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100871 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700872 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100873 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000874 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100875 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000876 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000877 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
878 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000879 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000880 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000881 } else if (location.IsStackSlot()) {
882 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
883 } else {
884 DCHECK(location.IsConstant());
885 DCHECK_EQ(location.GetConstant(), const_to_move);
886 }
887 } else if (const_to_move->IsLongConstant()) {
888 int64_t value = const_to_move->AsLongConstant()->GetValue();
889 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400890 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000891 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400892 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000893 } else {
894 DCHECK(location.IsConstant());
895 DCHECK_EQ(location.GetConstant(), const_to_move);
896 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100897 }
Roland Levillain476df552014-10-09 17:51:36 +0100898 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100899 switch (instruction->GetType()) {
900 case Primitive::kPrimBoolean:
901 case Primitive::kPrimByte:
902 case Primitive::kPrimChar:
903 case Primitive::kPrimShort:
904 case Primitive::kPrimInt:
905 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100906 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100907 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
908 break;
909
910 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100911 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000912 Move(location,
913 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100914 break;
915
916 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100917 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100918 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000919 } else if (instruction->IsTemporary()) {
920 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
921 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100922 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100923 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100924 switch (instruction->GetType()) {
925 case Primitive::kPrimBoolean:
926 case Primitive::kPrimByte:
927 case Primitive::kPrimChar:
928 case Primitive::kPrimShort:
929 case Primitive::kPrimInt:
930 case Primitive::kPrimNot:
931 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100932 case Primitive::kPrimFloat:
933 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000934 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100935 break;
936
937 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100938 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100939 }
940 }
941}
942
Calin Juravle175dc732015-08-25 15:42:32 +0100943void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
944 DCHECK(location.IsRegister());
945 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
946}
947
David Brazdilfc6a86a2015-06-26 10:33:45 +0000948void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100949 DCHECK(!successor->IsExitBlock());
950
951 HBasicBlock* block = got->GetBlock();
952 HInstruction* previous = got->GetPrevious();
953
954 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000955 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100956 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
957 return;
958 }
959
960 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
961 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
962 }
963 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100964 __ jmp(codegen_->GetLabelOf(successor));
965 }
966}
967
David Brazdilfc6a86a2015-06-26 10:33:45 +0000968void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
969 got->SetLocations(nullptr);
970}
971
972void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
973 HandleGoto(got, got->GetSuccessor());
974}
975
976void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
977 try_boundary->SetLocations(nullptr);
978}
979
980void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
981 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
982 if (!successor->IsExitBlock()) {
983 HandleGoto(try_boundary, successor);
984 }
985}
986
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100987void LocationsBuilderX86_64::VisitExit(HExit* exit) {
988 exit->SetLocations(nullptr);
989}
990
991void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700992 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100993}
994
Mark Mendellc4701932015-04-10 13:18:51 -0400995void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
996 Label* true_label,
997 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100998 if (cond->IsFPConditionTrueIfNaN()) {
999 __ j(kUnordered, true_label);
1000 } else if (cond->IsFPConditionFalseIfNaN()) {
1001 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001002 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001003 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001004}
1005
1006void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
1007 HCondition* condition,
1008 Label* true_target,
1009 Label* false_target,
1010 Label* always_true_target) {
1011 LocationSummary* locations = condition->GetLocations();
1012 Location left = locations->InAt(0);
1013 Location right = locations->InAt(1);
1014
1015 // We don't want true_target as a nullptr.
1016 if (true_target == nullptr) {
1017 true_target = always_true_target;
1018 }
1019 bool falls_through = (false_target == nullptr);
1020
1021 // FP compares don't like null false_targets.
1022 if (false_target == nullptr) {
1023 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1024 }
1025
1026 Primitive::Type type = condition->InputAt(0)->GetType();
1027 switch (type) {
1028 case Primitive::kPrimLong: {
1029 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1030 if (right.IsConstant()) {
1031 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1032 if (IsInt<32>(value)) {
1033 if (value == 0) {
1034 __ testq(left_reg, left_reg);
1035 } else {
1036 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1037 }
1038 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001039 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001040 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1041 }
1042 } else if (right.IsDoubleStackSlot()) {
1043 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1044 } else {
1045 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1046 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001047 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001048 break;
1049 }
1050 case Primitive::kPrimFloat: {
1051 if (right.IsFpuRegister()) {
1052 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1053 } else if (right.IsConstant()) {
1054 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1055 codegen_->LiteralFloatAddress(
1056 right.GetConstant()->AsFloatConstant()->GetValue()));
1057 } else {
1058 DCHECK(right.IsStackSlot());
1059 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1060 Address(CpuRegister(RSP), right.GetStackIndex()));
1061 }
1062 GenerateFPJumps(condition, true_target, false_target);
1063 break;
1064 }
1065 case Primitive::kPrimDouble: {
1066 if (right.IsFpuRegister()) {
1067 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1068 } else if (right.IsConstant()) {
1069 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1070 codegen_->LiteralDoubleAddress(
1071 right.GetConstant()->AsDoubleConstant()->GetValue()));
1072 } else {
1073 DCHECK(right.IsDoubleStackSlot());
1074 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1075 Address(CpuRegister(RSP), right.GetStackIndex()));
1076 }
1077 GenerateFPJumps(condition, true_target, false_target);
1078 break;
1079 }
1080 default:
1081 LOG(FATAL) << "Unexpected condition type " << type;
1082 }
1083
1084 if (!falls_through) {
1085 __ jmp(false_target);
1086 }
1087}
1088
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001089void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1090 Label* true_target,
1091 Label* false_target,
1092 Label* always_true_target) {
1093 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001094 if (cond->IsIntConstant()) {
1095 // Constant condition, statically compared against 1.
1096 int32_t cond_value = cond->AsIntConstant()->GetValue();
1097 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001098 if (always_true_target != nullptr) {
1099 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001100 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001101 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001102 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001103 DCHECK_EQ(cond_value, 0);
1104 }
1105 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001106 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001107 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1108 // Moves do not affect the eflags register, so if the condition is
1109 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001110 // again. We can't use the eflags on FP conditions if they are
1111 // materialized due to the complex branching.
1112 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001113 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001114 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1115 && !Primitive::IsFloatingPointType(type);
1116
Roland Levillain4fa13f62015-07-06 18:11:54 +01001117 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001118 if (!eflags_set) {
1119 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001120 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001121 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001122 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001123 } else {
1124 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1125 Immediate(0));
1126 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001127 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001128 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001129 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001130 }
1131 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001132 // Condition has not been materialized, use its inputs as the
1133 // comparison and its condition as the branch condition.
1134
Mark Mendellc4701932015-04-10 13:18:51 -04001135 // Is this a long or FP comparison that has been folded into the HCondition?
1136 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001137 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001138 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1139 true_target, false_target, always_true_target);
1140 return;
1141 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001142
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001143 Location lhs = cond->GetLocations()->InAt(0);
1144 Location rhs = cond->GetLocations()->InAt(1);
1145 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001146 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001147 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001148 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001149 if (constant == 0) {
1150 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1151 } else {
1152 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1153 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001154 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001155 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001156 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1157 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001158 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001159 }
Dave Allison20dfc792014-06-16 20:44:29 -07001160 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001161 if (false_target != nullptr) {
1162 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001163 }
1164}
1165
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001166void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1167 LocationSummary* locations =
1168 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1169 HInstruction* cond = if_instr->InputAt(0);
1170 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1171 locations->SetInAt(0, Location::Any());
1172 }
1173}
1174
1175void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1176 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1177 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1178 Label* always_true_target = true_target;
1179 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1180 if_instr->IfTrueSuccessor())) {
1181 always_true_target = nullptr;
1182 }
1183 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1184 if_instr->IfFalseSuccessor())) {
1185 false_target = nullptr;
1186 }
1187 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1188}
1189
1190void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1191 LocationSummary* locations = new (GetGraph()->GetArena())
1192 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1193 HInstruction* cond = deoptimize->InputAt(0);
1194 DCHECK(cond->IsCondition());
1195 if (cond->AsCondition()->NeedsMaterialization()) {
1196 locations->SetInAt(0, Location::Any());
1197 }
1198}
1199
1200void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001201 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001202 DeoptimizationSlowPathX86_64(deoptimize);
1203 codegen_->AddSlowPath(slow_path);
1204 Label* slow_path_entry = slow_path->GetEntryLabel();
1205 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1206}
1207
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001208void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1209 local->SetLocations(nullptr);
1210}
1211
1212void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1213 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1214}
1215
1216void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1217 local->SetLocations(nullptr);
1218}
1219
1220void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1221 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001222 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001223}
1224
1225void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001226 LocationSummary* locations =
1227 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 switch (store->InputAt(1)->GetType()) {
1229 case Primitive::kPrimBoolean:
1230 case Primitive::kPrimByte:
1231 case Primitive::kPrimChar:
1232 case Primitive::kPrimShort:
1233 case Primitive::kPrimInt:
1234 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001236 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1237 break;
1238
1239 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1242 break;
1243
1244 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001245 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001246 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001247}
1248
1249void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001250 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001251}
1252
Roland Levillain0d37cd02015-05-27 16:39:19 +01001253void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001254 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001255 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001256 // Handle the long/FP comparisons made in instruction simplification.
1257 switch (cond->InputAt(0)->GetType()) {
1258 case Primitive::kPrimLong:
1259 locations->SetInAt(0, Location::RequiresRegister());
1260 locations->SetInAt(1, Location::Any());
1261 break;
1262 case Primitive::kPrimFloat:
1263 case Primitive::kPrimDouble:
1264 locations->SetInAt(0, Location::RequiresFpuRegister());
1265 locations->SetInAt(1, Location::Any());
1266 break;
1267 default:
1268 locations->SetInAt(0, Location::RequiresRegister());
1269 locations->SetInAt(1, Location::Any());
1270 break;
1271 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001272 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001273 locations->SetOut(Location::RequiresRegister());
1274 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001275}
1276
Roland Levillain0d37cd02015-05-27 16:39:19 +01001277void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001278 if (!cond->NeedsMaterialization()) {
1279 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001280 }
Mark Mendellc4701932015-04-10 13:18:51 -04001281
1282 LocationSummary* locations = cond->GetLocations();
1283 Location lhs = locations->InAt(0);
1284 Location rhs = locations->InAt(1);
1285 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1286 Label true_label, false_label;
1287
1288 switch (cond->InputAt(0)->GetType()) {
1289 default:
1290 // Integer case.
1291
1292 // Clear output register: setcc only sets the low byte.
1293 __ xorl(reg, reg);
1294
1295 if (rhs.IsRegister()) {
1296 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1297 } else if (rhs.IsConstant()) {
1298 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1299 if (constant == 0) {
1300 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1301 } else {
1302 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1303 }
1304 } else {
1305 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1306 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001307 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001308 return;
1309 case Primitive::kPrimLong:
1310 // Clear output register: setcc only sets the low byte.
1311 __ xorl(reg, reg);
1312
1313 if (rhs.IsRegister()) {
1314 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1315 } else if (rhs.IsConstant()) {
1316 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1317 if (IsInt<32>(value)) {
1318 if (value == 0) {
1319 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1320 } else {
1321 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1322 }
1323 } else {
1324 // Value won't fit in an int.
1325 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1326 }
1327 } else {
1328 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1329 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001330 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001331 return;
1332 case Primitive::kPrimFloat: {
1333 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1334 if (rhs.IsConstant()) {
1335 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1336 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1337 } else if (rhs.IsStackSlot()) {
1338 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1339 } else {
1340 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1341 }
1342 GenerateFPJumps(cond, &true_label, &false_label);
1343 break;
1344 }
1345 case Primitive::kPrimDouble: {
1346 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1347 if (rhs.IsConstant()) {
1348 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1349 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1350 } else if (rhs.IsDoubleStackSlot()) {
1351 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1352 } else {
1353 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1354 }
1355 GenerateFPJumps(cond, &true_label, &false_label);
1356 break;
1357 }
1358 }
1359
1360 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001361 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001362
Roland Levillain4fa13f62015-07-06 18:11:54 +01001363 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001364 __ Bind(&false_label);
1365 __ xorl(reg, reg);
1366 __ jmp(&done_label);
1367
Roland Levillain4fa13f62015-07-06 18:11:54 +01001368 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001369 __ Bind(&true_label);
1370 __ movl(reg, Immediate(1));
1371 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001372}
1373
1374void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1375 VisitCondition(comp);
1376}
1377
1378void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1379 VisitCondition(comp);
1380}
1381
1382void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1383 VisitCondition(comp);
1384}
1385
1386void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1387 VisitCondition(comp);
1388}
1389
1390void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1391 VisitCondition(comp);
1392}
1393
1394void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1395 VisitCondition(comp);
1396}
1397
1398void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1399 VisitCondition(comp);
1400}
1401
1402void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1403 VisitCondition(comp);
1404}
1405
1406void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1407 VisitCondition(comp);
1408}
1409
1410void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1411 VisitCondition(comp);
1412}
1413
1414void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1415 VisitCondition(comp);
1416}
1417
1418void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1419 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001420}
1421
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001422void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001423 LocationSummary* locations =
1424 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001425 switch (compare->InputAt(0)->GetType()) {
1426 case Primitive::kPrimLong: {
1427 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001428 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1430 break;
1431 }
1432 case Primitive::kPrimFloat:
1433 case Primitive::kPrimDouble: {
1434 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001435 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001436 locations->SetOut(Location::RequiresRegister());
1437 break;
1438 }
1439 default:
1440 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1441 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001442}
1443
1444void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001445 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001446 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001447 Location left = locations->InAt(0);
1448 Location right = locations->InAt(1);
1449
Mark Mendell0c9497d2015-08-21 09:30:05 -04001450 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001451 Primitive::Type type = compare->InputAt(0)->GetType();
1452 switch (type) {
1453 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001454 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1455 if (right.IsConstant()) {
1456 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001457 if (IsInt<32>(value)) {
1458 if (value == 0) {
1459 __ testq(left_reg, left_reg);
1460 } else {
1461 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1462 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001463 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001464 // Value won't fit in an int.
1465 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001466 }
Mark Mendell40741f32015-04-20 22:10:34 -04001467 } else if (right.IsDoubleStackSlot()) {
1468 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001469 } else {
1470 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1471 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001472 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001473 }
1474 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001475 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1476 if (right.IsConstant()) {
1477 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1478 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1479 } else if (right.IsStackSlot()) {
1480 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1481 } else {
1482 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1483 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001484 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1485 break;
1486 }
1487 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001488 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1489 if (right.IsConstant()) {
1490 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1491 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1492 } else if (right.IsDoubleStackSlot()) {
1493 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1494 } else {
1495 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1496 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001497 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1498 break;
1499 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001500 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001501 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001502 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001503 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001504 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001505 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001506
Calin Juravle91debbc2014-11-26 19:01:09 +00001507 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001508 __ movl(out, Immediate(1));
1509 __ jmp(&done);
1510
1511 __ Bind(&less);
1512 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001513
1514 __ Bind(&done);
1515}
1516
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001517void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001518 LocationSummary* locations =
1519 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001520 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001521}
1522
1523void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001524 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001525 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001526}
1527
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001528void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1529 LocationSummary* locations =
1530 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1531 locations->SetOut(Location::ConstantLocation(constant));
1532}
1533
1534void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1535 // Will be generated at use site.
1536 UNUSED(constant);
1537}
1538
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001539void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001540 LocationSummary* locations =
1541 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001542 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001543}
1544
1545void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001546 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001547 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001548}
1549
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001550void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1551 LocationSummary* locations =
1552 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1553 locations->SetOut(Location::ConstantLocation(constant));
1554}
1555
1556void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1557 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001558 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001559}
1560
1561void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1562 LocationSummary* locations =
1563 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1564 locations->SetOut(Location::ConstantLocation(constant));
1565}
1566
1567void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1568 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001569 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001570}
1571
Calin Juravle27df7582015-04-17 19:12:31 +01001572void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1573 memory_barrier->SetLocations(nullptr);
1574}
1575
1576void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1577 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1578}
1579
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001580void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1581 ret->SetLocations(nullptr);
1582}
1583
1584void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001585 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001586 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001587}
1588
1589void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001590 LocationSummary* locations =
1591 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001592 switch (ret->InputAt(0)->GetType()) {
1593 case Primitive::kPrimBoolean:
1594 case Primitive::kPrimByte:
1595 case Primitive::kPrimChar:
1596 case Primitive::kPrimShort:
1597 case Primitive::kPrimInt:
1598 case Primitive::kPrimNot:
1599 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001600 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001601 break;
1602
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001603 case Primitive::kPrimFloat:
1604 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001605 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001606 break;
1607
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001608 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001609 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001610 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001611}
1612
1613void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1614 if (kIsDebugBuild) {
1615 switch (ret->InputAt(0)->GetType()) {
1616 case Primitive::kPrimBoolean:
1617 case Primitive::kPrimByte:
1618 case Primitive::kPrimChar:
1619 case Primitive::kPrimShort:
1620 case Primitive::kPrimInt:
1621 case Primitive::kPrimNot:
1622 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001623 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001624 break;
1625
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001626 case Primitive::kPrimFloat:
1627 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001628 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001629 XMM0);
1630 break;
1631
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001632 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001633 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001634 }
1635 }
1636 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001637}
1638
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001639Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1640 switch (type) {
1641 case Primitive::kPrimBoolean:
1642 case Primitive::kPrimByte:
1643 case Primitive::kPrimChar:
1644 case Primitive::kPrimShort:
1645 case Primitive::kPrimInt:
1646 case Primitive::kPrimNot:
1647 case Primitive::kPrimLong:
1648 return Location::RegisterLocation(RAX);
1649
1650 case Primitive::kPrimVoid:
1651 return Location::NoLocation();
1652
1653 case Primitive::kPrimDouble:
1654 case Primitive::kPrimFloat:
1655 return Location::FpuRegisterLocation(XMM0);
1656 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001657
1658 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001659}
1660
1661Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1662 return Location::RegisterLocation(kMethodRegisterArgument);
1663}
1664
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001665Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001666 switch (type) {
1667 case Primitive::kPrimBoolean:
1668 case Primitive::kPrimByte:
1669 case Primitive::kPrimChar:
1670 case Primitive::kPrimShort:
1671 case Primitive::kPrimInt:
1672 case Primitive::kPrimNot: {
1673 uint32_t index = gp_index_++;
1674 stack_index_++;
1675 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001676 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001677 } else {
1678 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1679 }
1680 }
1681
1682 case Primitive::kPrimLong: {
1683 uint32_t index = gp_index_;
1684 stack_index_ += 2;
1685 if (index < calling_convention.GetNumberOfRegisters()) {
1686 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001687 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001688 } else {
1689 gp_index_ += 2;
1690 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1691 }
1692 }
1693
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001694 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001695 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001696 stack_index_++;
1697 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001698 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001699 } else {
1700 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1701 }
1702 }
1703
1704 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001705 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001706 stack_index_ += 2;
1707 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001708 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001709 } else {
1710 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1711 }
1712 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001713
1714 case Primitive::kPrimVoid:
1715 LOG(FATAL) << "Unexpected parameter type " << type;
1716 break;
1717 }
1718 return Location();
1719}
1720
Calin Juravle175dc732015-08-25 15:42:32 +01001721void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1722 // The trampoline uses the same calling convention as dex calling conventions,
1723 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1724 // the method_idx.
1725 HandleInvoke(invoke);
1726}
1727
1728void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1729 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1730}
1731
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001732void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001733 // When we do not run baseline, explicit clinit checks triggered by static
1734 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1735 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001736
Mark Mendellfb8d2792015-03-31 22:16:59 -04001737 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001738 if (intrinsic.TryDispatch(invoke)) {
1739 return;
1740 }
1741
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001742 HandleInvoke(invoke);
1743}
1744
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001745static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1746 if (invoke->GetLocations()->Intrinsified()) {
1747 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1748 intrinsic.Dispatch(invoke);
1749 return true;
1750 }
1751 return false;
1752}
1753
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001754void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001755 // When we do not run baseline, explicit clinit checks triggered by static
1756 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1757 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001758
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001759 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1760 return;
1761 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001762
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001763 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001764 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001765 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001766 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001767}
1768
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001769void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001770 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001771 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001772}
1773
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001774void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001775 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001776 if (intrinsic.TryDispatch(invoke)) {
1777 return;
1778 }
1779
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001780 HandleInvoke(invoke);
1781}
1782
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001783void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001784 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1785 return;
1786 }
1787
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001788 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001789
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001790 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001791 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001792}
1793
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001794void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1795 HandleInvoke(invoke);
1796 // Add the hidden argument.
1797 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1798}
1799
1800void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1801 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001802 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001803 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1804 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001805 LocationSummary* locations = invoke->GetLocations();
1806 Location receiver = locations->InAt(0);
1807 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1808
1809 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001810 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1811 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001812
1813 // temp = object->GetClass();
1814 if (receiver.IsStackSlot()) {
1815 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1816 __ movl(temp, Address(temp, class_offset));
1817 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001818 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001819 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001820 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001821 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001822 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001823 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001824 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001825 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001826 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001827
1828 DCHECK(!codegen_->IsLeafMethod());
1829 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1830}
1831
Roland Levillain88cb1752014-10-20 16:36:47 +01001832void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1833 LocationSummary* locations =
1834 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1835 switch (neg->GetResultType()) {
1836 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001837 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001838 locations->SetInAt(0, Location::RequiresRegister());
1839 locations->SetOut(Location::SameAsFirstInput());
1840 break;
1841
Roland Levillain88cb1752014-10-20 16:36:47 +01001842 case Primitive::kPrimFloat:
1843 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001844 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001845 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001846 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001847 break;
1848
1849 default:
1850 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1851 }
1852}
1853
1854void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1855 LocationSummary* locations = neg->GetLocations();
1856 Location out = locations->Out();
1857 Location in = locations->InAt(0);
1858 switch (neg->GetResultType()) {
1859 case Primitive::kPrimInt:
1860 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001861 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001862 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001863 break;
1864
1865 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001866 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001867 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001868 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001869 break;
1870
Roland Levillain5368c212014-11-27 15:03:41 +00001871 case Primitive::kPrimFloat: {
1872 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001873 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001874 // Implement float negation with an exclusive or with value
1875 // 0x80000000 (mask for bit 31, representing the sign of a
1876 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001877 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001878 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001879 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001880 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001881
Roland Levillain5368c212014-11-27 15:03:41 +00001882 case Primitive::kPrimDouble: {
1883 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001884 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001885 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001886 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001887 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001888 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001889 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001890 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001891 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001892
1893 default:
1894 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1895 }
1896}
1897
Roland Levillaindff1f282014-11-05 14:15:05 +00001898void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1899 LocationSummary* locations =
1900 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1901 Primitive::Type result_type = conversion->GetResultType();
1902 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001903 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001904
David Brazdilb2bd1c52015-03-25 11:17:37 +00001905 // The Java language does not allow treating boolean as an integral type but
1906 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001907
Roland Levillaindff1f282014-11-05 14:15:05 +00001908 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001909 case Primitive::kPrimByte:
1910 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001911 case Primitive::kPrimBoolean:
1912 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001913 case Primitive::kPrimShort:
1914 case Primitive::kPrimInt:
1915 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001916 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001917 locations->SetInAt(0, Location::Any());
1918 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1919 break;
1920
1921 default:
1922 LOG(FATAL) << "Unexpected type conversion from " << input_type
1923 << " to " << result_type;
1924 }
1925 break;
1926
Roland Levillain01a8d712014-11-14 16:27:39 +00001927 case Primitive::kPrimShort:
1928 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001929 case Primitive::kPrimBoolean:
1930 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001931 case Primitive::kPrimByte:
1932 case Primitive::kPrimInt:
1933 case Primitive::kPrimChar:
1934 // Processing a Dex `int-to-short' instruction.
1935 locations->SetInAt(0, Location::Any());
1936 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1937 break;
1938
1939 default:
1940 LOG(FATAL) << "Unexpected type conversion from " << input_type
1941 << " to " << result_type;
1942 }
1943 break;
1944
Roland Levillain946e1432014-11-11 17:35:19 +00001945 case Primitive::kPrimInt:
1946 switch (input_type) {
1947 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001948 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001949 locations->SetInAt(0, Location::Any());
1950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1951 break;
1952
1953 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001954 // Processing a Dex `float-to-int' instruction.
1955 locations->SetInAt(0, Location::RequiresFpuRegister());
1956 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001957 break;
1958
Roland Levillain946e1432014-11-11 17:35:19 +00001959 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001960 // Processing a Dex `double-to-int' instruction.
1961 locations->SetInAt(0, Location::RequiresFpuRegister());
1962 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001963 break;
1964
1965 default:
1966 LOG(FATAL) << "Unexpected type conversion from " << input_type
1967 << " to " << result_type;
1968 }
1969 break;
1970
Roland Levillaindff1f282014-11-05 14:15:05 +00001971 case Primitive::kPrimLong:
1972 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001973 case Primitive::kPrimBoolean:
1974 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001975 case Primitive::kPrimByte:
1976 case Primitive::kPrimShort:
1977 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001978 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001979 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001980 // TODO: We would benefit from a (to-be-implemented)
1981 // Location::RegisterOrStackSlot requirement for this input.
1982 locations->SetInAt(0, Location::RequiresRegister());
1983 locations->SetOut(Location::RequiresRegister());
1984 break;
1985
1986 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001987 // Processing a Dex `float-to-long' instruction.
1988 locations->SetInAt(0, Location::RequiresFpuRegister());
1989 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00001990 break;
1991
Roland Levillaindff1f282014-11-05 14:15:05 +00001992 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001993 // Processing a Dex `double-to-long' instruction.
1994 locations->SetInAt(0, Location::RequiresFpuRegister());
1995 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00001996 break;
1997
1998 default:
1999 LOG(FATAL) << "Unexpected type conversion from " << input_type
2000 << " to " << result_type;
2001 }
2002 break;
2003
Roland Levillain981e4542014-11-14 11:47:14 +00002004 case Primitive::kPrimChar:
2005 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002006 case Primitive::kPrimBoolean:
2007 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002008 case Primitive::kPrimByte:
2009 case Primitive::kPrimShort:
2010 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002011 // Processing a Dex `int-to-char' instruction.
2012 locations->SetInAt(0, Location::Any());
2013 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2014 break;
2015
2016 default:
2017 LOG(FATAL) << "Unexpected type conversion from " << input_type
2018 << " to " << result_type;
2019 }
2020 break;
2021
Roland Levillaindff1f282014-11-05 14:15:05 +00002022 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002023 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002024 case Primitive::kPrimBoolean:
2025 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002026 case Primitive::kPrimByte:
2027 case Primitive::kPrimShort:
2028 case Primitive::kPrimInt:
2029 case Primitive::kPrimChar:
2030 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002031 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002032 locations->SetOut(Location::RequiresFpuRegister());
2033 break;
2034
2035 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002036 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002037 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002038 locations->SetOut(Location::RequiresFpuRegister());
2039 break;
2040
Roland Levillaincff13742014-11-17 14:32:17 +00002041 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002042 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002043 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002044 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002045 break;
2046
2047 default:
2048 LOG(FATAL) << "Unexpected type conversion from " << input_type
2049 << " to " << result_type;
2050 };
2051 break;
2052
Roland Levillaindff1f282014-11-05 14:15:05 +00002053 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002054 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002055 case Primitive::kPrimBoolean:
2056 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002057 case Primitive::kPrimByte:
2058 case Primitive::kPrimShort:
2059 case Primitive::kPrimInt:
2060 case Primitive::kPrimChar:
2061 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002062 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002063 locations->SetOut(Location::RequiresFpuRegister());
2064 break;
2065
2066 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002067 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002068 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002069 locations->SetOut(Location::RequiresFpuRegister());
2070 break;
2071
Roland Levillaincff13742014-11-17 14:32:17 +00002072 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002073 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002074 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002075 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002076 break;
2077
2078 default:
2079 LOG(FATAL) << "Unexpected type conversion from " << input_type
2080 << " to " << result_type;
2081 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002082 break;
2083
2084 default:
2085 LOG(FATAL) << "Unexpected type conversion from " << input_type
2086 << " to " << result_type;
2087 }
2088}
2089
2090void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2091 LocationSummary* locations = conversion->GetLocations();
2092 Location out = locations->Out();
2093 Location in = locations->InAt(0);
2094 Primitive::Type result_type = conversion->GetResultType();
2095 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002096 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002097 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002098 case Primitive::kPrimByte:
2099 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002100 case Primitive::kPrimBoolean:
2101 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002102 case Primitive::kPrimShort:
2103 case Primitive::kPrimInt:
2104 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002105 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002106 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002107 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002108 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002109 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002110 Address(CpuRegister(RSP), in.GetStackIndex()));
2111 } else {
2112 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002113 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002114 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2115 }
2116 break;
2117
2118 default:
2119 LOG(FATAL) << "Unexpected type conversion from " << input_type
2120 << " to " << result_type;
2121 }
2122 break;
2123
Roland Levillain01a8d712014-11-14 16:27:39 +00002124 case Primitive::kPrimShort:
2125 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002126 case Primitive::kPrimBoolean:
2127 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002128 case Primitive::kPrimByte:
2129 case Primitive::kPrimInt:
2130 case Primitive::kPrimChar:
2131 // Processing a Dex `int-to-short' instruction.
2132 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002134 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002136 Address(CpuRegister(RSP), in.GetStackIndex()));
2137 } else {
2138 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002139 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002140 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2141 }
2142 break;
2143
2144 default:
2145 LOG(FATAL) << "Unexpected type conversion from " << input_type
2146 << " to " << result_type;
2147 }
2148 break;
2149
Roland Levillain946e1432014-11-11 17:35:19 +00002150 case Primitive::kPrimInt:
2151 switch (input_type) {
2152 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002153 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002154 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002155 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002156 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002157 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002158 Address(CpuRegister(RSP), in.GetStackIndex()));
2159 } else {
2160 DCHECK(in.IsConstant());
2161 DCHECK(in.GetConstant()->IsLongConstant());
2162 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002163 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002164 }
2165 break;
2166
Roland Levillain3f8f9362014-12-02 17:45:01 +00002167 case Primitive::kPrimFloat: {
2168 // Processing a Dex `float-to-int' instruction.
2169 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2170 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002171 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002172
2173 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002174 // if input >= (float)INT_MAX goto done
2175 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002176 __ j(kAboveEqual, &done);
2177 // if input == NaN goto nan
2178 __ j(kUnordered, &nan);
2179 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002180 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002181 __ jmp(&done);
2182 __ Bind(&nan);
2183 // output = 0
2184 __ xorl(output, output);
2185 __ Bind(&done);
2186 break;
2187 }
2188
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002189 case Primitive::kPrimDouble: {
2190 // Processing a Dex `double-to-int' instruction.
2191 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2192 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002193 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002194
2195 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002196 // if input >= (double)INT_MAX goto done
2197 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002198 __ j(kAboveEqual, &done);
2199 // if input == NaN goto nan
2200 __ j(kUnordered, &nan);
2201 // output = double-to-int-truncate(input)
2202 __ cvttsd2si(output, input);
2203 __ jmp(&done);
2204 __ Bind(&nan);
2205 // output = 0
2206 __ xorl(output, output);
2207 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002208 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002209 }
Roland Levillain946e1432014-11-11 17:35:19 +00002210
2211 default:
2212 LOG(FATAL) << "Unexpected type conversion from " << input_type
2213 << " to " << result_type;
2214 }
2215 break;
2216
Roland Levillaindff1f282014-11-05 14:15:05 +00002217 case Primitive::kPrimLong:
2218 switch (input_type) {
2219 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002220 case Primitive::kPrimBoolean:
2221 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002222 case Primitive::kPrimByte:
2223 case Primitive::kPrimShort:
2224 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002225 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002226 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002227 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002228 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002229 break;
2230
Roland Levillain624279f2014-12-04 11:54:28 +00002231 case Primitive::kPrimFloat: {
2232 // Processing a Dex `float-to-long' instruction.
2233 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2234 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002235 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002236
Mark Mendell92e83bf2015-05-07 11:25:03 -04002237 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002238 // if input >= (float)LONG_MAX goto done
2239 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002240 __ j(kAboveEqual, &done);
2241 // if input == NaN goto nan
2242 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002243 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002244 __ cvttss2si(output, input, true);
2245 __ jmp(&done);
2246 __ Bind(&nan);
2247 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002248 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002249 __ Bind(&done);
2250 break;
2251 }
2252
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002253 case Primitive::kPrimDouble: {
2254 // Processing a Dex `double-to-long' instruction.
2255 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2256 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002257 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002258
Mark Mendell92e83bf2015-05-07 11:25:03 -04002259 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002260 // if input >= (double)LONG_MAX goto done
2261 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002262 __ j(kAboveEqual, &done);
2263 // if input == NaN goto nan
2264 __ j(kUnordered, &nan);
2265 // output = double-to-long-truncate(input)
2266 __ cvttsd2si(output, input, true);
2267 __ jmp(&done);
2268 __ Bind(&nan);
2269 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002270 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002271 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002272 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002273 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002274
2275 default:
2276 LOG(FATAL) << "Unexpected type conversion from " << input_type
2277 << " to " << result_type;
2278 }
2279 break;
2280
Roland Levillain981e4542014-11-14 11:47:14 +00002281 case Primitive::kPrimChar:
2282 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002283 case Primitive::kPrimBoolean:
2284 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002285 case Primitive::kPrimByte:
2286 case Primitive::kPrimShort:
2287 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002288 // Processing a Dex `int-to-char' instruction.
2289 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002290 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002291 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002292 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002293 Address(CpuRegister(RSP), in.GetStackIndex()));
2294 } else {
2295 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002296 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002297 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2298 }
2299 break;
2300
2301 default:
2302 LOG(FATAL) << "Unexpected type conversion from " << input_type
2303 << " to " << result_type;
2304 }
2305 break;
2306
Roland Levillaindff1f282014-11-05 14:15:05 +00002307 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002308 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002309 case Primitive::kPrimBoolean:
2310 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002311 case Primitive::kPrimByte:
2312 case Primitive::kPrimShort:
2313 case Primitive::kPrimInt:
2314 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002315 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002316 if (in.IsRegister()) {
2317 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2318 } else if (in.IsConstant()) {
2319 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2320 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2321 if (v == 0) {
2322 __ xorps(dest, dest);
2323 } else {
2324 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2325 }
2326 } else {
2327 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2328 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2329 }
Roland Levillaincff13742014-11-17 14:32:17 +00002330 break;
2331
2332 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002333 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002334 if (in.IsRegister()) {
2335 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2336 } else if (in.IsConstant()) {
2337 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2338 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2339 if (v == 0) {
2340 __ xorps(dest, dest);
2341 } else {
2342 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2343 }
2344 } else {
2345 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2346 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2347 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002348 break;
2349
Roland Levillaincff13742014-11-17 14:32:17 +00002350 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002351 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002352 if (in.IsFpuRegister()) {
2353 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2354 } else if (in.IsConstant()) {
2355 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2356 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2357 if (bit_cast<int64_t, double>(v) == 0) {
2358 __ xorps(dest, dest);
2359 } else {
2360 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2361 }
2362 } else {
2363 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2364 Address(CpuRegister(RSP), in.GetStackIndex()));
2365 }
Roland Levillaincff13742014-11-17 14:32:17 +00002366 break;
2367
2368 default:
2369 LOG(FATAL) << "Unexpected type conversion from " << input_type
2370 << " to " << result_type;
2371 };
2372 break;
2373
Roland Levillaindff1f282014-11-05 14:15:05 +00002374 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002375 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002376 case Primitive::kPrimBoolean:
2377 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002378 case Primitive::kPrimByte:
2379 case Primitive::kPrimShort:
2380 case Primitive::kPrimInt:
2381 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002382 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002383 if (in.IsRegister()) {
2384 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2385 } else if (in.IsConstant()) {
2386 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2387 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2388 if (v == 0) {
2389 __ xorpd(dest, dest);
2390 } else {
2391 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2392 }
2393 } else {
2394 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2395 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2396 }
Roland Levillaincff13742014-11-17 14:32:17 +00002397 break;
2398
2399 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002400 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002401 if (in.IsRegister()) {
2402 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2403 } else if (in.IsConstant()) {
2404 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2405 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2406 if (v == 0) {
2407 __ xorpd(dest, dest);
2408 } else {
2409 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2410 }
2411 } else {
2412 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2413 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2414 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002415 break;
2416
Roland Levillaincff13742014-11-17 14:32:17 +00002417 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002418 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002419 if (in.IsFpuRegister()) {
2420 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2421 } else if (in.IsConstant()) {
2422 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2423 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2424 if (bit_cast<int32_t, float>(v) == 0) {
2425 __ xorpd(dest, dest);
2426 } else {
2427 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2428 }
2429 } else {
2430 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2431 Address(CpuRegister(RSP), in.GetStackIndex()));
2432 }
Roland Levillaincff13742014-11-17 14:32:17 +00002433 break;
2434
2435 default:
2436 LOG(FATAL) << "Unexpected type conversion from " << input_type
2437 << " to " << result_type;
2438 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002439 break;
2440
2441 default:
2442 LOG(FATAL) << "Unexpected type conversion from " << input_type
2443 << " to " << result_type;
2444 }
2445}
2446
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002447void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002448 LocationSummary* locations =
2449 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002450 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002451 case Primitive::kPrimInt: {
2452 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002453 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2454 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002455 break;
2456 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002457
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002458 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002459 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002460 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002461 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002463 break;
2464 }
2465
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002466 case Primitive::kPrimDouble:
2467 case Primitive::kPrimFloat: {
2468 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002469 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002470 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002471 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002472 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002473
2474 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002475 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002476 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002477}
2478
2479void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2480 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002481 Location first = locations->InAt(0);
2482 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002483 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002484
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002485 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002486 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002487 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002488 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2489 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002490 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2491 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002492 } else {
2493 __ leal(out.AsRegister<CpuRegister>(), Address(
2494 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2495 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002496 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002497 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2498 __ addl(out.AsRegister<CpuRegister>(),
2499 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2500 } else {
2501 __ leal(out.AsRegister<CpuRegister>(), Address(
2502 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2503 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002504 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002505 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002506 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002507 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002508 break;
2509 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002510
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002511 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002512 if (second.IsRegister()) {
2513 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2514 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002515 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2516 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002517 } else {
2518 __ leaq(out.AsRegister<CpuRegister>(), Address(
2519 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2520 }
2521 } else {
2522 DCHECK(second.IsConstant());
2523 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2524 int32_t int32_value = Low32Bits(value);
2525 DCHECK_EQ(int32_value, value);
2526 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2527 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2528 } else {
2529 __ leaq(out.AsRegister<CpuRegister>(), Address(
2530 first.AsRegister<CpuRegister>(), int32_value));
2531 }
2532 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002533 break;
2534 }
2535
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002536 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002537 if (second.IsFpuRegister()) {
2538 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2539 } else if (second.IsConstant()) {
2540 __ addss(first.AsFpuRegister<XmmRegister>(),
2541 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2542 } else {
2543 DCHECK(second.IsStackSlot());
2544 __ addss(first.AsFpuRegister<XmmRegister>(),
2545 Address(CpuRegister(RSP), second.GetStackIndex()));
2546 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002547 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002548 }
2549
2550 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002551 if (second.IsFpuRegister()) {
2552 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2553 } else if (second.IsConstant()) {
2554 __ addsd(first.AsFpuRegister<XmmRegister>(),
2555 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2556 } else {
2557 DCHECK(second.IsDoubleStackSlot());
2558 __ addsd(first.AsFpuRegister<XmmRegister>(),
2559 Address(CpuRegister(RSP), second.GetStackIndex()));
2560 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002561 break;
2562 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002563
2564 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002565 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002566 }
2567}
2568
2569void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002570 LocationSummary* locations =
2571 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002572 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002573 case Primitive::kPrimInt: {
2574 locations->SetInAt(0, Location::RequiresRegister());
2575 locations->SetInAt(1, Location::Any());
2576 locations->SetOut(Location::SameAsFirstInput());
2577 break;
2578 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002579 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002580 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002581 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002582 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002583 break;
2584 }
Calin Juravle11351682014-10-23 15:38:15 +01002585 case Primitive::kPrimFloat:
2586 case Primitive::kPrimDouble: {
2587 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002588 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002589 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002590 break;
Calin Juravle11351682014-10-23 15:38:15 +01002591 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002592 default:
Calin Juravle11351682014-10-23 15:38:15 +01002593 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002594 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002595}
2596
2597void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2598 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002599 Location first = locations->InAt(0);
2600 Location second = locations->InAt(1);
2601 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002602 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002603 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002604 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002605 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002606 } else if (second.IsConstant()) {
2607 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002608 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002609 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002610 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002611 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002612 break;
2613 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002614 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002615 if (second.IsConstant()) {
2616 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2617 DCHECK(IsInt<32>(value));
2618 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2619 } else {
2620 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2621 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002622 break;
2623 }
2624
Calin Juravle11351682014-10-23 15:38:15 +01002625 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002626 if (second.IsFpuRegister()) {
2627 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2628 } else if (second.IsConstant()) {
2629 __ subss(first.AsFpuRegister<XmmRegister>(),
2630 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2631 } else {
2632 DCHECK(second.IsStackSlot());
2633 __ subss(first.AsFpuRegister<XmmRegister>(),
2634 Address(CpuRegister(RSP), second.GetStackIndex()));
2635 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002636 break;
Calin Juravle11351682014-10-23 15:38:15 +01002637 }
2638
2639 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002640 if (second.IsFpuRegister()) {
2641 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2642 } else if (second.IsConstant()) {
2643 __ subsd(first.AsFpuRegister<XmmRegister>(),
2644 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2645 } else {
2646 DCHECK(second.IsDoubleStackSlot());
2647 __ subsd(first.AsFpuRegister<XmmRegister>(),
2648 Address(CpuRegister(RSP), second.GetStackIndex()));
2649 }
Calin Juravle11351682014-10-23 15:38:15 +01002650 break;
2651 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002652
2653 default:
Calin Juravle11351682014-10-23 15:38:15 +01002654 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002655 }
2656}
2657
Calin Juravle34bacdf2014-10-07 20:23:36 +01002658void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2659 LocationSummary* locations =
2660 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2661 switch (mul->GetResultType()) {
2662 case Primitive::kPrimInt: {
2663 locations->SetInAt(0, Location::RequiresRegister());
2664 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002665 if (mul->InputAt(1)->IsIntConstant()) {
2666 // Can use 3 operand multiply.
2667 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2668 } else {
2669 locations->SetOut(Location::SameAsFirstInput());
2670 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002671 break;
2672 }
2673 case Primitive::kPrimLong: {
2674 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002675 locations->SetInAt(1, Location::Any());
2676 if (mul->InputAt(1)->IsLongConstant() &&
2677 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002678 // Can use 3 operand multiply.
2679 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2680 } else {
2681 locations->SetOut(Location::SameAsFirstInput());
2682 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002683 break;
2684 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002685 case Primitive::kPrimFloat:
2686 case Primitive::kPrimDouble: {
2687 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002688 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002689 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002690 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002691 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002692
2693 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002694 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002695 }
2696}
2697
2698void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2699 LocationSummary* locations = mul->GetLocations();
2700 Location first = locations->InAt(0);
2701 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002702 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002703 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002704 case Primitive::kPrimInt:
2705 // The constant may have ended up in a register, so test explicitly to avoid
2706 // problems where the output may not be the same as the first operand.
2707 if (mul->InputAt(1)->IsIntConstant()) {
2708 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2709 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2710 } else if (second.IsRegister()) {
2711 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002712 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002713 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002714 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002715 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002716 __ imull(first.AsRegister<CpuRegister>(),
2717 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002718 }
2719 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002720 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002721 // The constant may have ended up in a register, so test explicitly to avoid
2722 // problems where the output may not be the same as the first operand.
2723 if (mul->InputAt(1)->IsLongConstant()) {
2724 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2725 if (IsInt<32>(value)) {
2726 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2727 Immediate(static_cast<int32_t>(value)));
2728 } else {
2729 // Have to use the constant area.
2730 DCHECK(first.Equals(out));
2731 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2732 }
2733 } else if (second.IsRegister()) {
2734 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002735 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002736 } else {
2737 DCHECK(second.IsDoubleStackSlot());
2738 DCHECK(first.Equals(out));
2739 __ imulq(first.AsRegister<CpuRegister>(),
2740 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002741 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002742 break;
2743 }
2744
Calin Juravleb5bfa962014-10-21 18:02:24 +01002745 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002746 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002747 if (second.IsFpuRegister()) {
2748 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2749 } else if (second.IsConstant()) {
2750 __ mulss(first.AsFpuRegister<XmmRegister>(),
2751 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2752 } else {
2753 DCHECK(second.IsStackSlot());
2754 __ mulss(first.AsFpuRegister<XmmRegister>(),
2755 Address(CpuRegister(RSP), second.GetStackIndex()));
2756 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002757 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002758 }
2759
2760 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002761 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002762 if (second.IsFpuRegister()) {
2763 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2764 } else if (second.IsConstant()) {
2765 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2766 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2767 } else {
2768 DCHECK(second.IsDoubleStackSlot());
2769 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2770 Address(CpuRegister(RSP), second.GetStackIndex()));
2771 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002772 break;
2773 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002774
2775 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002776 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002777 }
2778}
2779
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002780void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2781 uint32_t stack_adjustment, bool is_float) {
2782 if (source.IsStackSlot()) {
2783 DCHECK(is_float);
2784 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2785 } else if (source.IsDoubleStackSlot()) {
2786 DCHECK(!is_float);
2787 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2788 } else {
2789 // Write the value to the temporary location on the stack and load to FP stack.
2790 if (is_float) {
2791 Location stack_temp = Location::StackSlot(temp_offset);
2792 codegen_->Move(stack_temp, source);
2793 __ flds(Address(CpuRegister(RSP), temp_offset));
2794 } else {
2795 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2796 codegen_->Move(stack_temp, source);
2797 __ fldl(Address(CpuRegister(RSP), temp_offset));
2798 }
2799 }
2800}
2801
2802void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2803 Primitive::Type type = rem->GetResultType();
2804 bool is_float = type == Primitive::kPrimFloat;
2805 size_t elem_size = Primitive::ComponentSize(type);
2806 LocationSummary* locations = rem->GetLocations();
2807 Location first = locations->InAt(0);
2808 Location second = locations->InAt(1);
2809 Location out = locations->Out();
2810
2811 // Create stack space for 2 elements.
2812 // TODO: enhance register allocator to ask for stack temporaries.
2813 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2814
2815 // Load the values to the FP stack in reverse order, using temporaries if needed.
2816 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2817 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2818
2819 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002820 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002821 __ Bind(&retry);
2822 __ fprem();
2823
2824 // Move FP status to AX.
2825 __ fstsw();
2826
2827 // And see if the argument reduction is complete. This is signaled by the
2828 // C2 FPU flag bit set to 0.
2829 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2830 __ j(kNotEqual, &retry);
2831
2832 // We have settled on the final value. Retrieve it into an XMM register.
2833 // Store FP top of stack to real stack.
2834 if (is_float) {
2835 __ fsts(Address(CpuRegister(RSP), 0));
2836 } else {
2837 __ fstl(Address(CpuRegister(RSP), 0));
2838 }
2839
2840 // Pop the 2 items from the FP stack.
2841 __ fucompp();
2842
2843 // Load the value from the stack into an XMM register.
2844 DCHECK(out.IsFpuRegister()) << out;
2845 if (is_float) {
2846 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2847 } else {
2848 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2849 }
2850
2851 // And remove the temporary stack space we allocated.
2852 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2853}
2854
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002855void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2856 DCHECK(instruction->IsDiv() || instruction->IsRem());
2857
2858 LocationSummary* locations = instruction->GetLocations();
2859 Location second = locations->InAt(1);
2860 DCHECK(second.IsConstant());
2861
2862 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2863 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002864 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002865
2866 DCHECK(imm == 1 || imm == -1);
2867
2868 switch (instruction->GetResultType()) {
2869 case Primitive::kPrimInt: {
2870 if (instruction->IsRem()) {
2871 __ xorl(output_register, output_register);
2872 } else {
2873 __ movl(output_register, input_register);
2874 if (imm == -1) {
2875 __ negl(output_register);
2876 }
2877 }
2878 break;
2879 }
2880
2881 case Primitive::kPrimLong: {
2882 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002883 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002884 } else {
2885 __ movq(output_register, input_register);
2886 if (imm == -1) {
2887 __ negq(output_register);
2888 }
2889 }
2890 break;
2891 }
2892
2893 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002894 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002895 }
2896}
2897
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002898void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002899 LocationSummary* locations = instruction->GetLocations();
2900 Location second = locations->InAt(1);
2901
2902 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2903 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2904
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002905 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002906
2907 DCHECK(IsPowerOfTwo(std::abs(imm)));
2908
2909 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2910
2911 if (instruction->GetResultType() == Primitive::kPrimInt) {
2912 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2913 __ testl(numerator, numerator);
2914 __ cmov(kGreaterEqual, tmp, numerator);
2915 int shift = CTZ(imm);
2916 __ sarl(tmp, Immediate(shift));
2917
2918 if (imm < 0) {
2919 __ negl(tmp);
2920 }
2921
2922 __ movl(output_register, tmp);
2923 } else {
2924 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2925 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2926
Mark Mendell92e83bf2015-05-07 11:25:03 -04002927 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002928 __ addq(rdx, numerator);
2929 __ testq(numerator, numerator);
2930 __ cmov(kGreaterEqual, rdx, numerator);
2931 int shift = CTZ(imm);
2932 __ sarq(rdx, Immediate(shift));
2933
2934 if (imm < 0) {
2935 __ negq(rdx);
2936 }
2937
2938 __ movq(output_register, rdx);
2939 }
2940}
2941
2942void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2943 DCHECK(instruction->IsDiv() || instruction->IsRem());
2944
2945 LocationSummary* locations = instruction->GetLocations();
2946 Location second = locations->InAt(1);
2947
2948 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2949 : locations->GetTemp(0).AsRegister<CpuRegister>();
2950 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2951 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2952 : locations->Out().AsRegister<CpuRegister>();
2953 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2954
2955 DCHECK_EQ(RAX, eax.AsRegister());
2956 DCHECK_EQ(RDX, edx.AsRegister());
2957 if (instruction->IsDiv()) {
2958 DCHECK_EQ(RAX, out.AsRegister());
2959 } else {
2960 DCHECK_EQ(RDX, out.AsRegister());
2961 }
2962
2963 int64_t magic;
2964 int shift;
2965
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002966 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002967 if (instruction->GetResultType() == Primitive::kPrimInt) {
2968 int imm = second.GetConstant()->AsIntConstant()->GetValue();
2969
2970 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2971
2972 __ movl(numerator, eax);
2973
Mark Mendell0c9497d2015-08-21 09:30:05 -04002974 NearLabel no_div;
2975 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002976 __ testl(eax, eax);
2977 __ j(kNotEqual, &no_div);
2978
2979 __ xorl(out, out);
2980 __ jmp(&end);
2981
2982 __ Bind(&no_div);
2983
2984 __ movl(eax, Immediate(magic));
2985 __ imull(numerator);
2986
2987 if (imm > 0 && magic < 0) {
2988 __ addl(edx, numerator);
2989 } else if (imm < 0 && magic > 0) {
2990 __ subl(edx, numerator);
2991 }
2992
2993 if (shift != 0) {
2994 __ sarl(edx, Immediate(shift));
2995 }
2996
2997 __ movl(eax, edx);
2998 __ shrl(edx, Immediate(31));
2999 __ addl(edx, eax);
3000
3001 if (instruction->IsRem()) {
3002 __ movl(eax, numerator);
3003 __ imull(edx, Immediate(imm));
3004 __ subl(eax, edx);
3005 __ movl(edx, eax);
3006 } else {
3007 __ movl(eax, edx);
3008 }
3009 __ Bind(&end);
3010 } else {
3011 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3012
3013 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3014
3015 CpuRegister rax = eax;
3016 CpuRegister rdx = edx;
3017
3018 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3019
3020 // Save the numerator.
3021 __ movq(numerator, rax);
3022
3023 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003024 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003025
3026 // RDX:RAX = magic * numerator
3027 __ imulq(numerator);
3028
3029 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003030 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003031 __ addq(rdx, numerator);
3032 } else if (imm < 0 && magic > 0) {
3033 // RDX -= numerator
3034 __ subq(rdx, numerator);
3035 }
3036
3037 // Shift if needed.
3038 if (shift != 0) {
3039 __ sarq(rdx, Immediate(shift));
3040 }
3041
3042 // RDX += 1 if RDX < 0
3043 __ movq(rax, rdx);
3044 __ shrq(rdx, Immediate(63));
3045 __ addq(rdx, rax);
3046
3047 if (instruction->IsRem()) {
3048 __ movq(rax, numerator);
3049
3050 if (IsInt<32>(imm)) {
3051 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3052 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003053 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003054 }
3055
3056 __ subq(rax, rdx);
3057 __ movq(rdx, rax);
3058 } else {
3059 __ movq(rax, rdx);
3060 }
3061 }
3062}
3063
Calin Juravlebacfec32014-11-14 15:54:36 +00003064void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3065 DCHECK(instruction->IsDiv() || instruction->IsRem());
3066 Primitive::Type type = instruction->GetResultType();
3067 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3068
3069 bool is_div = instruction->IsDiv();
3070 LocationSummary* locations = instruction->GetLocations();
3071
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003072 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3073 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003074
Roland Levillain271ab9c2014-11-27 15:23:57 +00003075 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003076 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003077
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003078 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003079 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003080
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003081 if (imm == 0) {
3082 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3083 } else if (imm == 1 || imm == -1) {
3084 DivRemOneOrMinusOne(instruction);
3085 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003086 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003087 } else {
3088 DCHECK(imm <= -2 || imm >= 2);
3089 GenerateDivRemWithAnyConstant(instruction);
3090 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003091 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003092 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003093 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3094 out.AsRegister(), type, is_div);
3095 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003096
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003097 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3098 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3099 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3100 // so it's safe to just use negl instead of more complex comparisons.
3101 if (type == Primitive::kPrimInt) {
3102 __ cmpl(second_reg, Immediate(-1));
3103 __ j(kEqual, slow_path->GetEntryLabel());
3104 // edx:eax <- sign-extended of eax
3105 __ cdq();
3106 // eax = quotient, edx = remainder
3107 __ idivl(second_reg);
3108 } else {
3109 __ cmpq(second_reg, Immediate(-1));
3110 __ j(kEqual, slow_path->GetEntryLabel());
3111 // rdx:rax <- sign-extended of rax
3112 __ cqo();
3113 // rax = quotient, rdx = remainder
3114 __ idivq(second_reg);
3115 }
3116 __ Bind(slow_path->GetExitLabel());
3117 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003118}
3119
Calin Juravle7c4954d2014-10-28 16:57:40 +00003120void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3121 LocationSummary* locations =
3122 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3123 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003124 case Primitive::kPrimInt:
3125 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003126 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003127 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003128 locations->SetOut(Location::SameAsFirstInput());
3129 // Intel uses edx:eax as the dividend.
3130 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003131 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3132 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3133 // output and request another temp.
3134 if (div->InputAt(1)->IsConstant()) {
3135 locations->AddTemp(Location::RequiresRegister());
3136 }
Calin Juravled0d48522014-11-04 16:40:20 +00003137 break;
3138 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003139
Calin Juravle7c4954d2014-10-28 16:57:40 +00003140 case Primitive::kPrimFloat:
3141 case Primitive::kPrimDouble: {
3142 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003143 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003144 locations->SetOut(Location::SameAsFirstInput());
3145 break;
3146 }
3147
3148 default:
3149 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3150 }
3151}
3152
3153void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3154 LocationSummary* locations = div->GetLocations();
3155 Location first = locations->InAt(0);
3156 Location second = locations->InAt(1);
3157 DCHECK(first.Equals(locations->Out()));
3158
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003159 Primitive::Type type = div->GetResultType();
3160 switch (type) {
3161 case Primitive::kPrimInt:
3162 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003163 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003164 break;
3165 }
3166
Calin Juravle7c4954d2014-10-28 16:57:40 +00003167 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003168 if (second.IsFpuRegister()) {
3169 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3170 } else if (second.IsConstant()) {
3171 __ divss(first.AsFpuRegister<XmmRegister>(),
3172 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3173 } else {
3174 DCHECK(second.IsStackSlot());
3175 __ divss(first.AsFpuRegister<XmmRegister>(),
3176 Address(CpuRegister(RSP), second.GetStackIndex()));
3177 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003178 break;
3179 }
3180
3181 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003182 if (second.IsFpuRegister()) {
3183 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3184 } else if (second.IsConstant()) {
3185 __ divsd(first.AsFpuRegister<XmmRegister>(),
3186 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3187 } else {
3188 DCHECK(second.IsDoubleStackSlot());
3189 __ divsd(first.AsFpuRegister<XmmRegister>(),
3190 Address(CpuRegister(RSP), second.GetStackIndex()));
3191 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003192 break;
3193 }
3194
3195 default:
3196 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3197 }
3198}
3199
Calin Juravlebacfec32014-11-14 15:54:36 +00003200void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003201 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003202 LocationSummary* locations =
3203 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003204
3205 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003206 case Primitive::kPrimInt:
3207 case Primitive::kPrimLong: {
3208 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003209 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003210 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3211 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003212 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3213 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3214 // output and request another temp.
3215 if (rem->InputAt(1)->IsConstant()) {
3216 locations->AddTemp(Location::RequiresRegister());
3217 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003218 break;
3219 }
3220
3221 case Primitive::kPrimFloat:
3222 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003223 locations->SetInAt(0, Location::Any());
3224 locations->SetInAt(1, Location::Any());
3225 locations->SetOut(Location::RequiresFpuRegister());
3226 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003227 break;
3228 }
3229
3230 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003231 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003232 }
3233}
3234
3235void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3236 Primitive::Type type = rem->GetResultType();
3237 switch (type) {
3238 case Primitive::kPrimInt:
3239 case Primitive::kPrimLong: {
3240 GenerateDivRemIntegral(rem);
3241 break;
3242 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003243 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003244 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003245 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003246 break;
3247 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003248 default:
3249 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3250 }
3251}
3252
Calin Juravled0d48522014-11-04 16:40:20 +00003253void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003254 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3255 ? LocationSummary::kCallOnSlowPath
3256 : LocationSummary::kNoCall;
3257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003258 locations->SetInAt(0, Location::Any());
3259 if (instruction->HasUses()) {
3260 locations->SetOut(Location::SameAsFirstInput());
3261 }
3262}
3263
3264void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003265 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003266 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3267 codegen_->AddSlowPath(slow_path);
3268
3269 LocationSummary* locations = instruction->GetLocations();
3270 Location value = locations->InAt(0);
3271
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003272 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003273 case Primitive::kPrimByte:
3274 case Primitive::kPrimChar:
3275 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003276 case Primitive::kPrimInt: {
3277 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003278 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003279 __ j(kEqual, slow_path->GetEntryLabel());
3280 } else if (value.IsStackSlot()) {
3281 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3282 __ j(kEqual, slow_path->GetEntryLabel());
3283 } else {
3284 DCHECK(value.IsConstant()) << value;
3285 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3286 __ jmp(slow_path->GetEntryLabel());
3287 }
3288 }
3289 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003290 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003291 case Primitive::kPrimLong: {
3292 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003293 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003294 __ j(kEqual, slow_path->GetEntryLabel());
3295 } else if (value.IsDoubleStackSlot()) {
3296 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3297 __ j(kEqual, slow_path->GetEntryLabel());
3298 } else {
3299 DCHECK(value.IsConstant()) << value;
3300 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3301 __ jmp(slow_path->GetEntryLabel());
3302 }
3303 }
3304 break;
3305 }
3306 default:
3307 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003308 }
Calin Juravled0d48522014-11-04 16:40:20 +00003309}
3310
Calin Juravle9aec02f2014-11-18 23:06:35 +00003311void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3312 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3313
3314 LocationSummary* locations =
3315 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3316
3317 switch (op->GetResultType()) {
3318 case Primitive::kPrimInt:
3319 case Primitive::kPrimLong: {
3320 locations->SetInAt(0, Location::RequiresRegister());
3321 // The shift count needs to be in CL.
3322 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3323 locations->SetOut(Location::SameAsFirstInput());
3324 break;
3325 }
3326 default:
3327 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3328 }
3329}
3330
3331void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3332 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3333
3334 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003335 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003336 Location second = locations->InAt(1);
3337
3338 switch (op->GetResultType()) {
3339 case Primitive::kPrimInt: {
3340 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003341 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003342 if (op->IsShl()) {
3343 __ shll(first_reg, second_reg);
3344 } else if (op->IsShr()) {
3345 __ sarl(first_reg, second_reg);
3346 } else {
3347 __ shrl(first_reg, second_reg);
3348 }
3349 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003350 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003351 if (op->IsShl()) {
3352 __ shll(first_reg, imm);
3353 } else if (op->IsShr()) {
3354 __ sarl(first_reg, imm);
3355 } else {
3356 __ shrl(first_reg, imm);
3357 }
3358 }
3359 break;
3360 }
3361 case Primitive::kPrimLong: {
3362 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003363 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003364 if (op->IsShl()) {
3365 __ shlq(first_reg, second_reg);
3366 } else if (op->IsShr()) {
3367 __ sarq(first_reg, second_reg);
3368 } else {
3369 __ shrq(first_reg, second_reg);
3370 }
3371 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003372 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003373 if (op->IsShl()) {
3374 __ shlq(first_reg, imm);
3375 } else if (op->IsShr()) {
3376 __ sarq(first_reg, imm);
3377 } else {
3378 __ shrq(first_reg, imm);
3379 }
3380 }
3381 break;
3382 }
3383 default:
3384 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3385 }
3386}
3387
3388void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3389 HandleShift(shl);
3390}
3391
3392void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3393 HandleShift(shl);
3394}
3395
3396void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3397 HandleShift(shr);
3398}
3399
3400void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3401 HandleShift(shr);
3402}
3403
3404void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3405 HandleShift(ushr);
3406}
3407
3408void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3409 HandleShift(ushr);
3410}
3411
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003412void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003413 LocationSummary* locations =
3414 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003415 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003416 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003417 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003418 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003419}
3420
3421void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3422 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003423 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3424 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003425 // Note: if heap poisoning is enabled, the entry point takes cares
3426 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003427
Calin Juravle175dc732015-08-25 15:42:32 +01003428 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3429 instruction,
3430 instruction->GetDexPc(),
3431 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003432
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003433 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003434}
3435
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003436void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3437 LocationSummary* locations =
3438 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3439 InvokeRuntimeCallingConvention calling_convention;
3440 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003441 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003442 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003443 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003444}
3445
3446void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3447 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003448 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3449 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003450
Roland Levillain4d027112015-07-01 15:41:14 +01003451 // Note: if heap poisoning is enabled, the entry point takes cares
3452 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003453 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3454 instruction,
3455 instruction->GetDexPc(),
3456 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003457
3458 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003459}
3460
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003461void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003462 LocationSummary* locations =
3463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003464 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3465 if (location.IsStackSlot()) {
3466 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3467 } else if (location.IsDoubleStackSlot()) {
3468 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3469 }
3470 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003471}
3472
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003473void InstructionCodeGeneratorX86_64::VisitParameterValue(
3474 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003475 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003476}
3477
3478void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3479 LocationSummary* locations =
3480 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3481 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3482}
3483
3484void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3485 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3486 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003487}
3488
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003489void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003490 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003491 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003492 locations->SetInAt(0, Location::RequiresRegister());
3493 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003494}
3495
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003496void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3497 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003498 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3499 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003500 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003501 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003502 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003504 break;
3505
3506 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003507 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003508 break;
3509
3510 default:
3511 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3512 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003513}
3514
David Brazdil66d126e2015-04-03 16:02:44 +01003515void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3516 LocationSummary* locations =
3517 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3518 locations->SetInAt(0, Location::RequiresRegister());
3519 locations->SetOut(Location::SameAsFirstInput());
3520}
3521
3522void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003523 LocationSummary* locations = bool_not->GetLocations();
3524 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3525 locations->Out().AsRegister<CpuRegister>().AsRegister());
3526 Location out = locations->Out();
3527 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3528}
3529
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003530void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003531 LocationSummary* locations =
3532 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003533 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3534 locations->SetInAt(i, Location::Any());
3535 }
3536 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003537}
3538
3539void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003540 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003541 LOG(FATAL) << "Unimplemented";
3542}
3543
Calin Juravle52c48962014-12-16 17:02:57 +00003544void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3545 /*
3546 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3547 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3548 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3549 */
3550 switch (kind) {
3551 case MemBarrierKind::kAnyAny: {
3552 __ mfence();
3553 break;
3554 }
3555 case MemBarrierKind::kAnyStore:
3556 case MemBarrierKind::kLoadAny:
3557 case MemBarrierKind::kStoreStore: {
3558 // nop
3559 break;
3560 }
3561 default:
3562 LOG(FATAL) << "Unexpected memory barier " << kind;
3563 }
3564}
3565
3566void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3567 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3568
Nicolas Geoffray39468442014-09-02 15:17:15 +01003569 LocationSummary* locations =
3570 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003571 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003572 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3573 locations->SetOut(Location::RequiresFpuRegister());
3574 } else {
3575 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3576 }
Calin Juravle52c48962014-12-16 17:02:57 +00003577}
3578
3579void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3580 const FieldInfo& field_info) {
3581 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3582
3583 LocationSummary* locations = instruction->GetLocations();
3584 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3585 Location out = locations->Out();
3586 bool is_volatile = field_info.IsVolatile();
3587 Primitive::Type field_type = field_info.GetFieldType();
3588 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3589
3590 switch (field_type) {
3591 case Primitive::kPrimBoolean: {
3592 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3593 break;
3594 }
3595
3596 case Primitive::kPrimByte: {
3597 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3598 break;
3599 }
3600
3601 case Primitive::kPrimShort: {
3602 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3603 break;
3604 }
3605
3606 case Primitive::kPrimChar: {
3607 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3608 break;
3609 }
3610
3611 case Primitive::kPrimInt:
3612 case Primitive::kPrimNot: {
3613 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3614 break;
3615 }
3616
3617 case Primitive::kPrimLong: {
3618 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3619 break;
3620 }
3621
3622 case Primitive::kPrimFloat: {
3623 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3624 break;
3625 }
3626
3627 case Primitive::kPrimDouble: {
3628 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3629 break;
3630 }
3631
3632 case Primitive::kPrimVoid:
3633 LOG(FATAL) << "Unreachable type " << field_type;
3634 UNREACHABLE();
3635 }
3636
Calin Juravle77520bc2015-01-12 18:45:46 +00003637 codegen_->MaybeRecordImplicitNullCheck(instruction);
3638
Calin Juravle52c48962014-12-16 17:02:57 +00003639 if (is_volatile) {
3640 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3641 }
Roland Levillain4d027112015-07-01 15:41:14 +01003642
3643 if (field_type == Primitive::kPrimNot) {
3644 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3645 }
Calin Juravle52c48962014-12-16 17:02:57 +00003646}
3647
3648void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3649 const FieldInfo& field_info) {
3650 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3651
3652 LocationSummary* locations =
3653 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003654 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003655 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003656 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003657
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003658 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003659 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3660 locations->SetInAt(1, Location::RequiresFpuRegister());
3661 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003662 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003663 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003664 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003665 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003666 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003667 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003668 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3669 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003670 locations->AddTemp(Location::RequiresRegister());
3671 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003672}
3673
Calin Juravle52c48962014-12-16 17:02:57 +00003674void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003675 const FieldInfo& field_info,
3676 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003677 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3678
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003679 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003680 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3681 Location value = locations->InAt(1);
3682 bool is_volatile = field_info.IsVolatile();
3683 Primitive::Type field_type = field_info.GetFieldType();
3684 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3685
3686 if (is_volatile) {
3687 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3688 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003689
3690 switch (field_type) {
3691 case Primitive::kPrimBoolean:
3692 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003693 if (value.IsConstant()) {
3694 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3695 __ movb(Address(base, offset), Immediate(v));
3696 } else {
3697 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3698 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003699 break;
3700 }
3701
3702 case Primitive::kPrimShort:
3703 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003704 if (value.IsConstant()) {
3705 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3706 __ movw(Address(base, offset), Immediate(v));
3707 } else {
3708 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3709 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003710 break;
3711 }
3712
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003713 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003714 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003715 if (value.IsConstant()) {
3716 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003717 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3718 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3719 // Note: if heap poisoning is enabled, no need to poison
3720 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003721 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003722 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003723 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3724 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3725 __ movl(temp, value.AsRegister<CpuRegister>());
3726 __ PoisonHeapReference(temp);
3727 __ movl(Address(base, offset), temp);
3728 } else {
3729 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3730 }
Mark Mendell40741f32015-04-20 22:10:34 -04003731 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003732 break;
3733 }
3734
3735 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003736 if (value.IsConstant()) {
3737 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3738 DCHECK(IsInt<32>(v));
3739 int32_t v_32 = v;
3740 __ movq(Address(base, offset), Immediate(v_32));
3741 } else {
3742 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3743 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003744 break;
3745 }
3746
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003747 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003748 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003749 break;
3750 }
3751
3752 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003753 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003754 break;
3755 }
3756
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003757 case Primitive::kPrimVoid:
3758 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003759 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003760 }
Calin Juravle52c48962014-12-16 17:02:57 +00003761
Calin Juravle77520bc2015-01-12 18:45:46 +00003762 codegen_->MaybeRecordImplicitNullCheck(instruction);
3763
3764 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3765 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3766 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003767 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003768 }
3769
Calin Juravle52c48962014-12-16 17:02:57 +00003770 if (is_volatile) {
3771 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3772 }
3773}
3774
3775void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3776 HandleFieldSet(instruction, instruction->GetFieldInfo());
3777}
3778
3779void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003780 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003781}
3782
3783void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003784 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003785}
3786
3787void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003788 HandleFieldGet(instruction, instruction->GetFieldInfo());
3789}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003790
Calin Juravle52c48962014-12-16 17:02:57 +00003791void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3792 HandleFieldGet(instruction);
3793}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003794
Calin Juravle52c48962014-12-16 17:02:57 +00003795void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3796 HandleFieldGet(instruction, instruction->GetFieldInfo());
3797}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003798
Calin Juravle52c48962014-12-16 17:02:57 +00003799void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3800 HandleFieldSet(instruction, instruction->GetFieldInfo());
3801}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003802
Calin Juravle52c48962014-12-16 17:02:57 +00003803void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003804 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003805}
3806
3807void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003808 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3809 ? LocationSummary::kCallOnSlowPath
3810 : LocationSummary::kNoCall;
3811 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3812 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003813 ? Location::RequiresRegister()
3814 : Location::Any();
3815 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003816 if (instruction->HasUses()) {
3817 locations->SetOut(Location::SameAsFirstInput());
3818 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003819}
3820
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003821void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003822 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3823 return;
3824 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003825 LocationSummary* locations = instruction->GetLocations();
3826 Location obj = locations->InAt(0);
3827
3828 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3829 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3830}
3831
3832void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003833 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003834 codegen_->AddSlowPath(slow_path);
3835
3836 LocationSummary* locations = instruction->GetLocations();
3837 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003838
3839 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003840 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003841 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003842 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003843 } else {
3844 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00003845 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003846 __ jmp(slow_path->GetEntryLabel());
3847 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003848 }
3849 __ j(kEqual, slow_path->GetEntryLabel());
3850}
3851
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003852void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003853 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003854 GenerateImplicitNullCheck(instruction);
3855 } else {
3856 GenerateExplicitNullCheck(instruction);
3857 }
3858}
3859
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003860void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003861 LocationSummary* locations =
3862 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003863 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003864 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003865 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3866 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3867 } else {
3868 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3869 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003870}
3871
3872void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3873 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003874 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003875 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003876 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003877
Roland Levillain4d027112015-07-01 15:41:14 +01003878 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879 case Primitive::kPrimBoolean: {
3880 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003881 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003882 if (index.IsConstant()) {
3883 __ movzxb(out, Address(obj,
3884 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3885 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003886 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003887 }
3888 break;
3889 }
3890
3891 case Primitive::kPrimByte: {
3892 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003893 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003894 if (index.IsConstant()) {
3895 __ movsxb(out, Address(obj,
3896 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3897 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003898 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003899 }
3900 break;
3901 }
3902
3903 case Primitive::kPrimShort: {
3904 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003905 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003906 if (index.IsConstant()) {
3907 __ movsxw(out, Address(obj,
3908 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3909 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003910 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003911 }
3912 break;
3913 }
3914
3915 case Primitive::kPrimChar: {
3916 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003917 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003918 if (index.IsConstant()) {
3919 __ movzxw(out, Address(obj,
3920 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3921 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003922 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003923 }
3924 break;
3925 }
3926
3927 case Primitive::kPrimInt:
3928 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003929 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3930 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003931 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003932 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003933 if (index.IsConstant()) {
3934 __ movl(out, Address(obj,
3935 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3936 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003937 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003938 }
3939 break;
3940 }
3941
3942 case Primitive::kPrimLong: {
3943 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003944 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003945 if (index.IsConstant()) {
3946 __ movq(out, Address(obj,
3947 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3948 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003949 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003950 }
3951 break;
3952 }
3953
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003954 case Primitive::kPrimFloat: {
3955 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003956 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003957 if (index.IsConstant()) {
3958 __ movss(out, Address(obj,
3959 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3960 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003961 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003962 }
3963 break;
3964 }
3965
3966 case Primitive::kPrimDouble: {
3967 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003968 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003969 if (index.IsConstant()) {
3970 __ movsd(out, Address(obj,
3971 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3972 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003973 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003974 }
3975 break;
3976 }
3977
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003978 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003979 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003980 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003981 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003983
3984 if (type == Primitive::kPrimNot) {
3985 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3986 __ MaybeUnpoisonHeapReference(out);
3987 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003988}
3989
3990void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003991 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003992
3993 bool needs_write_barrier =
3994 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3995 bool needs_runtime_call = instruction->NeedsTypeCheck();
3996
Nicolas Geoffray39468442014-09-02 15:17:15 +01003997 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003998 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3999 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004000 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004001 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4002 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4003 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004004 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004005 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004006 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004007 1, Location::RegisterOrConstant(instruction->InputAt(1)));
4008 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004009 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04004010 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004011 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
4012 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004013 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004014 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004015 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004016
4017 if (needs_write_barrier) {
4018 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004019 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004020 locations->AddTemp(Location::RequiresRegister());
4021 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004022 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004023}
4024
4025void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4026 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004027 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004028 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004029 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004030 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004031 bool needs_runtime_call = locations->WillCall();
4032 bool needs_write_barrier =
4033 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004034
4035 switch (value_type) {
4036 case Primitive::kPrimBoolean:
4037 case Primitive::kPrimByte: {
4038 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004039 if (index.IsConstant()) {
4040 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004041 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004042 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004043 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00004044 __ movb(Address(obj, offset),
4045 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004046 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004047 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004048 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004049 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
4050 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004051 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004052 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004053 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4054 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004055 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004056 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004057 break;
4058 }
4059
4060 case Primitive::kPrimShort:
4061 case Primitive::kPrimChar: {
4062 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004063 if (index.IsConstant()) {
4064 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004065 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004066 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004067 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004068 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00004069 __ movw(Address(obj, offset),
4070 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004071 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004072 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004073 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004074 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
4076 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004077 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004078 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00004079 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004080 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4081 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004082 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004083 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004084 break;
4085 }
4086
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004087 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004088 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004089 if (!needs_runtime_call) {
4090 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4091 if (index.IsConstant()) {
4092 size_t offset =
4093 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4094 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004095 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4096 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4097 __ movl(temp, value.AsRegister<CpuRegister>());
4098 __ PoisonHeapReference(temp);
4099 __ movl(Address(obj, offset), temp);
4100 } else {
4101 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
4102 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004103 } else {
4104 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004105 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004106 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4107 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4108 // Note: if heap poisoning is enabled, no need to poison
4109 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04004110 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004111 }
4112 } else {
4113 DCHECK(index.IsRegister()) << index;
4114 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004115 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4116 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4117 __ movl(temp, value.AsRegister<CpuRegister>());
4118 __ PoisonHeapReference(temp);
4119 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4120 } else {
4121 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4122 value.AsRegister<CpuRegister>());
4123 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004124 } else {
4125 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004126 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004127 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4128 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4129 // Note: if heap poisoning is enabled, no need to poison
4130 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004131 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004132 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004133 }
4134 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004135 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004136 if (needs_write_barrier) {
4137 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4139 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004140 codegen_->MarkGCCard(
4141 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004142 }
4143 } else {
4144 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004145 // Note: if heap poisoning is enabled, pAputObject takes cares
4146 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004147 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4148 instruction,
4149 instruction->GetDexPc(),
4150 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004151 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004152 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004153 break;
4154 }
4155
4156 case Primitive::kPrimLong: {
4157 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004158 if (index.IsConstant()) {
4159 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004160 if (value.IsRegister()) {
4161 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4162 } else {
4163 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4164 DCHECK(IsInt<32>(v));
4165 int32_t v_32 = v;
4166 __ movq(Address(obj, offset), Immediate(v_32));
4167 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004168 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004169 if (value.IsRegister()) {
4170 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4171 value.AsRegister<CpuRegister>());
4172 } else {
4173 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4174 DCHECK(IsInt<32>(v));
4175 int32_t v_32 = v;
4176 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4177 Immediate(v_32));
4178 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004180 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004181 break;
4182 }
4183
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004184 case Primitive::kPrimFloat: {
4185 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4186 if (index.IsConstant()) {
4187 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4188 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004189 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004190 } else {
4191 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004192 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4193 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004194 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004195 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004196 break;
4197 }
4198
4199 case Primitive::kPrimDouble: {
4200 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4201 if (index.IsConstant()) {
4202 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4203 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004204 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004205 } else {
4206 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004207 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4208 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004209 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004210 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004211 break;
4212 }
4213
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004214 case Primitive::kPrimVoid:
4215 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004216 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004217 }
4218}
4219
4220void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004221 LocationSummary* locations =
4222 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004223 locations->SetInAt(0, Location::RequiresRegister());
4224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004225}
4226
4227void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4228 LocationSummary* locations = instruction->GetLocations();
4229 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004230 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4231 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004233 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004234}
4235
4236void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004237 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4238 ? LocationSummary::kCallOnSlowPath
4239 : LocationSummary::kNoCall;
4240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004241 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004242 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004243 if (instruction->HasUses()) {
4244 locations->SetOut(Location::SameAsFirstInput());
4245 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004246}
4247
4248void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4249 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004250 Location index_loc = locations->InAt(0);
4251 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004252 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004253 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004254
Mark Mendell99dbd682015-04-22 16:18:52 -04004255 if (length_loc.IsConstant()) {
4256 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4257 if (index_loc.IsConstant()) {
4258 // BCE will remove the bounds check if we are guarenteed to pass.
4259 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4260 if (index < 0 || index >= length) {
4261 codegen_->AddSlowPath(slow_path);
4262 __ jmp(slow_path->GetEntryLabel());
4263 } else {
4264 // Some optimization after BCE may have generated this, and we should not
4265 // generate a bounds check if it is a valid range.
4266 }
4267 return;
4268 }
4269
4270 // We have to reverse the jump condition because the length is the constant.
4271 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4272 __ cmpl(index_reg, Immediate(length));
4273 codegen_->AddSlowPath(slow_path);
4274 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004275 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004276 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4277 if (index_loc.IsConstant()) {
4278 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4279 __ cmpl(length, Immediate(value));
4280 } else {
4281 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4282 }
4283 codegen_->AddSlowPath(slow_path);
4284 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004285 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004286}
4287
4288void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4289 CpuRegister card,
4290 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004291 CpuRegister value,
4292 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004293 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004294 if (value_can_be_null) {
4295 __ testl(value, value);
4296 __ j(kEqual, &is_null);
4297 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004298 __ gs()->movq(card, Address::Absolute(
4299 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4300 __ movq(temp, object);
4301 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004302 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004303 if (value_can_be_null) {
4304 __ Bind(&is_null);
4305 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004306}
4307
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004308void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4309 temp->SetLocations(nullptr);
4310}
4311
4312void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4313 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004314 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004315}
4316
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004317void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004318 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004319 LOG(FATAL) << "Unimplemented";
4320}
4321
4322void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004323 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4324}
4325
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004326void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4327 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4328}
4329
4330void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004331 HBasicBlock* block = instruction->GetBlock();
4332 if (block->GetLoopInformation() != nullptr) {
4333 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4334 // The back edge will generate the suspend check.
4335 return;
4336 }
4337 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4338 // The goto will generate the suspend check.
4339 return;
4340 }
4341 GenerateSuspendCheck(instruction, nullptr);
4342}
4343
4344void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4345 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004346 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004347 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4348 if (slow_path == nullptr) {
4349 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4350 instruction->SetSlowPath(slow_path);
4351 codegen_->AddSlowPath(slow_path);
4352 if (successor != nullptr) {
4353 DCHECK(successor->IsLoopHeader());
4354 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4355 }
4356 } else {
4357 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4358 }
4359
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004360 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004361 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004362 if (successor == nullptr) {
4363 __ j(kNotEqual, slow_path->GetEntryLabel());
4364 __ Bind(slow_path->GetReturnLabel());
4365 } else {
4366 __ j(kEqual, codegen_->GetLabelOf(successor));
4367 __ jmp(slow_path->GetEntryLabel());
4368 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004369}
4370
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004371X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4372 return codegen_->GetAssembler();
4373}
4374
4375void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4376 MoveOperands* move = moves_.Get(index);
4377 Location source = move->GetSource();
4378 Location destination = move->GetDestination();
4379
4380 if (source.IsRegister()) {
4381 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004382 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004383 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004384 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004385 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004386 } else {
4387 DCHECK(destination.IsDoubleStackSlot());
4388 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004389 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004390 }
4391 } else if (source.IsStackSlot()) {
4392 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004393 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004394 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004395 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004396 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004397 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004398 } else {
4399 DCHECK(destination.IsStackSlot());
4400 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4401 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4402 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004403 } else if (source.IsDoubleStackSlot()) {
4404 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004405 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004406 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004407 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004408 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4409 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004410 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004411 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004412 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4413 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4414 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004415 } else if (source.IsConstant()) {
4416 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004417 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4418 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004419 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004420 if (value == 0) {
4421 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4422 } else {
4423 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4424 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004425 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004426 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004427 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004428 }
4429 } else if (constant->IsLongConstant()) {
4430 int64_t value = constant->AsLongConstant()->GetValue();
4431 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004432 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004433 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004434 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004435 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004436 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004437 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004438 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004439 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004440 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004441 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4442 if (value == 0) {
4443 // easy FP 0.0.
4444 __ xorps(dest, dest);
4445 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004446 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004447 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004448 } else {
4449 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004450 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004451 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4452 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004453 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004454 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004455 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004456 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004457 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004458 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4459 if (value == 0) {
4460 __ xorpd(dest, dest);
4461 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004462 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004463 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004464 } else {
4465 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004466 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004467 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004468 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004469 } else if (source.IsFpuRegister()) {
4470 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004471 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004472 } else if (destination.IsStackSlot()) {
4473 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004474 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004475 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004476 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004477 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004478 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004479 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004480 }
4481}
4482
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004483void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004484 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004485 __ movl(Address(CpuRegister(RSP), mem), reg);
4486 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004487}
4488
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004489void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004490 ScratchRegisterScope ensure_scratch(
4491 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4492
4493 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4494 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4495 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4496 Address(CpuRegister(RSP), mem2 + stack_offset));
4497 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4498 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4499 CpuRegister(ensure_scratch.GetRegister()));
4500}
4501
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004502void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4503 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4504 __ movq(Address(CpuRegister(RSP), mem), reg);
4505 __ movq(reg, CpuRegister(TMP));
4506}
4507
4508void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4509 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004510 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004511
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004512 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4513 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4514 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4515 Address(CpuRegister(RSP), mem2 + stack_offset));
4516 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4517 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4518 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004519}
4520
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004521void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4522 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4523 __ movss(Address(CpuRegister(RSP), mem), reg);
4524 __ movd(reg, CpuRegister(TMP));
4525}
4526
4527void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4528 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4529 __ movsd(Address(CpuRegister(RSP), mem), reg);
4530 __ movd(reg, CpuRegister(TMP));
4531}
4532
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004533void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4534 MoveOperands* move = moves_.Get(index);
4535 Location source = move->GetSource();
4536 Location destination = move->GetDestination();
4537
4538 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004539 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004540 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004541 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004542 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004543 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004544 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004545 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4546 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004547 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004548 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004549 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004550 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4551 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004552 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004553 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4554 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4555 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004556 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004557 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004558 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004559 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004560 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004561 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004562 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004563 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004564 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004565 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004566 }
4567}
4568
4569
4570void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4571 __ pushq(CpuRegister(reg));
4572}
4573
4574
4575void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4576 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004577}
4578
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004579void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004580 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004581 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4582 Immediate(mirror::Class::kStatusInitialized));
4583 __ j(kLess, slow_path->GetEntryLabel());
4584 __ Bind(slow_path->GetExitLabel());
4585 // No need for memory fence, thanks to the X86_64 memory model.
4586}
4587
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004588void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004589 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4590 ? LocationSummary::kCallOnSlowPath
4591 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004592 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004593 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004594 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004595 locations->SetOut(Location::RequiresRegister());
4596}
4597
4598void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004599 LocationSummary* locations = cls->GetLocations();
4600 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4601 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004602 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004603 DCHECK(!cls->CanCallRuntime());
4604 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004605 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004606 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004607 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004608 __ movq(out, Address(
4609 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004610 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004611 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004612
Andreas Gampe85b62f22015-09-09 13:15:38 -07004613 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004614 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4615 codegen_->AddSlowPath(slow_path);
4616 __ testl(out, out);
4617 __ j(kEqual, slow_path->GetEntryLabel());
4618 if (cls->MustGenerateClinitCheck()) {
4619 GenerateClassInitializationCheck(slow_path, out);
4620 } else {
4621 __ Bind(slow_path->GetExitLabel());
4622 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004623 }
4624}
4625
4626void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4627 LocationSummary* locations =
4628 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4629 locations->SetInAt(0, Location::RequiresRegister());
4630 if (check->HasUses()) {
4631 locations->SetOut(Location::SameAsFirstInput());
4632 }
4633}
4634
4635void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004636 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004637 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004638 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004639 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004640 GenerateClassInitializationCheck(slow_path,
4641 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004642}
4643
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004644void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4645 LocationSummary* locations =
4646 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004647 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004648 locations->SetOut(Location::RequiresRegister());
4649}
4650
4651void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004652 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004653 codegen_->AddSlowPath(slow_path);
4654
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004655 LocationSummary* locations = load->GetLocations();
4656 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4657 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004658 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004659 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004660 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004661 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004662 __ testl(out, out);
4663 __ j(kEqual, slow_path->GetEntryLabel());
4664 __ Bind(slow_path->GetExitLabel());
4665}
4666
David Brazdilcb1c0552015-08-04 16:22:25 +01004667static Address GetExceptionTlsAddress() {
4668 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4669}
4670
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004671void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4672 LocationSummary* locations =
4673 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4674 locations->SetOut(Location::RequiresRegister());
4675}
4676
4677void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004678 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4679}
4680
4681void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4682 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4683}
4684
4685void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4686 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004687}
4688
4689void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4690 LocationSummary* locations =
4691 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4692 InvokeRuntimeCallingConvention calling_convention;
4693 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4694}
4695
4696void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004697 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4698 instruction,
4699 instruction->GetDexPc(),
4700 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004701}
4702
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004703void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004704 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4705 switch (instruction->GetTypeCheckKind()) {
4706 case TypeCheckKind::kExactCheck:
4707 case TypeCheckKind::kAbstractClassCheck:
4708 case TypeCheckKind::kClassHierarchyCheck:
4709 case TypeCheckKind::kArrayObjectCheck:
4710 call_kind = LocationSummary::kNoCall;
4711 break;
4712 case TypeCheckKind::kInterfaceCheck:
4713 call_kind = LocationSummary::kCall;
4714 break;
4715 case TypeCheckKind::kArrayCheck:
4716 call_kind = LocationSummary::kCallOnSlowPath;
4717 break;
4718 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004720 if (call_kind != LocationSummary::kCall) {
4721 locations->SetInAt(0, Location::RequiresRegister());
4722 locations->SetInAt(1, Location::Any());
4723 // Note that TypeCheckSlowPathX86_64 uses this register too.
4724 locations->SetOut(Location::RequiresRegister());
4725 } else {
4726 InvokeRuntimeCallingConvention calling_convention;
4727 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4728 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4729 locations->SetOut(Location::RegisterLocation(RAX));
4730 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004731}
4732
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004733void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004734 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004735 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004736 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004737 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004738 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004739 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4740 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4741 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004742 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004743 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004744
4745 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004746 // Avoid null check if we know obj is not null.
4747 if (instruction->MustDoNullCheck()) {
4748 __ testl(obj, obj);
4749 __ j(kEqual, &zero);
4750 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004751
4752 // In case of an interface check, we put the object class into the object register.
4753 // This is safe, as the register is caller-save, and the object must be in another
4754 // register if it survives the runtime call.
4755 CpuRegister target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
4756 ? obj
4757 : out;
4758 __ movl(target, Address(obj, class_offset));
4759 __ MaybeUnpoisonHeapReference(target);
4760
4761 switch (instruction->GetTypeCheckKind()) {
4762 case TypeCheckKind::kExactCheck: {
4763 if (cls.IsRegister()) {
4764 __ cmpl(out, cls.AsRegister<CpuRegister>());
4765 } else {
4766 DCHECK(cls.IsStackSlot()) << cls;
4767 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4768 }
4769 // Classes must be equal for the instanceof to succeed.
4770 __ j(kNotEqual, &zero);
4771 __ movl(out, Immediate(1));
4772 __ jmp(&done);
4773 break;
4774 }
4775 case TypeCheckKind::kAbstractClassCheck: {
4776 // If the class is abstract, we eagerly fetch the super class of the
4777 // object to avoid doing a comparison we know will fail.
4778 NearLabel loop, success;
4779 __ Bind(&loop);
4780 __ movl(out, Address(out, super_offset));
4781 __ MaybeUnpoisonHeapReference(out);
4782 __ testl(out, out);
4783 // If `out` is null, we use it for the result, and jump to `done`.
4784 __ j(kEqual, &done);
4785 if (cls.IsRegister()) {
4786 __ cmpl(out, cls.AsRegister<CpuRegister>());
4787 } else {
4788 DCHECK(cls.IsStackSlot()) << cls;
4789 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4790 }
4791 __ j(kNotEqual, &loop);
4792 __ movl(out, Immediate(1));
4793 if (zero.IsLinked()) {
4794 __ jmp(&done);
4795 }
4796 break;
4797 }
4798 case TypeCheckKind::kClassHierarchyCheck: {
4799 // Walk over the class hierarchy to find a match.
4800 NearLabel loop, success;
4801 __ Bind(&loop);
4802 if (cls.IsRegister()) {
4803 __ cmpl(out, cls.AsRegister<CpuRegister>());
4804 } else {
4805 DCHECK(cls.IsStackSlot()) << cls;
4806 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4807 }
4808 __ j(kEqual, &success);
4809 __ movl(out, Address(out, super_offset));
4810 __ MaybeUnpoisonHeapReference(out);
4811 __ testl(out, out);
4812 __ j(kNotEqual, &loop);
4813 // If `out` is null, we use it for the result, and jump to `done`.
4814 __ jmp(&done);
4815 __ Bind(&success);
4816 __ movl(out, Immediate(1));
4817 if (zero.IsLinked()) {
4818 __ jmp(&done);
4819 }
4820 break;
4821 }
4822 case TypeCheckKind::kArrayObjectCheck: {
4823 // Just need to check that the object's class is a non primitive array.
4824 __ movl(out, Address(out, component_offset));
4825 __ MaybeUnpoisonHeapReference(out);
4826 __ testl(out, out);
4827 // If `out` is null, we use it for the result, and jump to `done`.
4828 __ j(kEqual, &done);
4829 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
4830 __ j(kNotEqual, &zero);
4831 __ movl(out, Immediate(1));
4832 __ jmp(&done);
4833 break;
4834 }
4835 case TypeCheckKind::kArrayCheck: {
4836 if (cls.IsRegister()) {
4837 __ cmpl(out, cls.AsRegister<CpuRegister>());
4838 } else {
4839 DCHECK(cls.IsStackSlot()) << cls;
4840 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4841 }
4842 DCHECK(locations->OnlyCallsOnSlowPath());
4843 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4844 instruction, /* is_fatal */ false);
4845 codegen_->AddSlowPath(slow_path);
4846 __ j(kNotEqual, slow_path->GetEntryLabel());
4847 __ movl(out, Immediate(1));
4848 if (zero.IsLinked()) {
4849 __ jmp(&done);
4850 }
4851 break;
4852 }
4853
4854 case TypeCheckKind::kInterfaceCheck:
4855 default: {
4856 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4857 instruction,
4858 instruction->GetDexPc(),
4859 nullptr);
4860 if (zero.IsLinked()) {
4861 __ jmp(&done);
4862 }
4863 break;
4864 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004865 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004866
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004867 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004868 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004869 __ xorl(out, out);
4870 }
4871
4872 if (done.IsLinked()) {
4873 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004874 }
4875
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004876 if (slow_path != nullptr) {
4877 __ Bind(slow_path->GetExitLabel());
4878 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004879}
4880
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004881void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004882 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4883 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4884
4885 switch (instruction->GetTypeCheckKind()) {
4886 case TypeCheckKind::kExactCheck:
4887 case TypeCheckKind::kAbstractClassCheck:
4888 case TypeCheckKind::kClassHierarchyCheck:
4889 case TypeCheckKind::kArrayObjectCheck:
4890 call_kind = throws_into_catch
4891 ? LocationSummary::kCallOnSlowPath
4892 : LocationSummary::kNoCall;
4893 break;
4894 case TypeCheckKind::kInterfaceCheck:
4895 call_kind = LocationSummary::kCall;
4896 break;
4897 case TypeCheckKind::kArrayCheck:
4898 call_kind = LocationSummary::kCallOnSlowPath;
4899 break;
4900 }
4901
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004903 instruction, call_kind);
4904 if (call_kind != LocationSummary::kCall) {
4905 locations->SetInAt(0, Location::RequiresRegister());
4906 locations->SetInAt(1, Location::Any());
4907 // Note that TypeCheckSlowPathX86_64 uses this register too.
4908 locations->AddTemp(Location::RequiresRegister());
4909 } else {
4910 InvokeRuntimeCallingConvention calling_convention;
4911 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4912 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4913 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004914}
4915
4916void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
4917 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004918 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004919 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004920 CpuRegister temp = locations->WillCall()
4921 ? CpuRegister(kNoRegister)
4922 : locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004923
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004924 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4925 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4926 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4927 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4928 SlowPathCode* slow_path = nullptr;
4929
4930 if (!locations->WillCall()) {
4931 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4932 instruction, !locations->CanCall());
4933 codegen_->AddSlowPath(slow_path);
4934 }
4935
4936 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004937 // Avoid null check if we know obj is not null.
4938 if (instruction->MustDoNullCheck()) {
4939 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004940 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004941 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004942
4943 if (locations->WillCall()) {
4944 __ movl(obj, Address(obj, class_offset));
4945 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004946 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004947 __ movl(temp, Address(obj, class_offset));
4948 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004949 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004950
4951 switch (instruction->GetTypeCheckKind()) {
4952 case TypeCheckKind::kExactCheck:
4953 case TypeCheckKind::kArrayCheck: {
4954 if (cls.IsRegister()) {
4955 __ cmpl(temp, cls.AsRegister<CpuRegister>());
4956 } else {
4957 DCHECK(cls.IsStackSlot()) << cls;
4958 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4959 }
4960 // Jump to slow path for throwing the exception or doing a
4961 // more involved array check.
4962 __ j(kNotEqual, slow_path->GetEntryLabel());
4963 break;
4964 }
4965 case TypeCheckKind::kAbstractClassCheck: {
4966 // If the class is abstract, we eagerly fetch the super class of the
4967 // object to avoid doing a comparison we know will fail.
4968 NearLabel loop;
4969 __ Bind(&loop);
4970 __ movl(temp, Address(temp, super_offset));
4971 __ MaybeUnpoisonHeapReference(temp);
4972 __ testl(temp, temp);
4973 // Jump to the slow path to throw the exception.
4974 __ j(kEqual, slow_path->GetEntryLabel());
4975 if (cls.IsRegister()) {
4976 __ cmpl(temp, cls.AsRegister<CpuRegister>());
4977 } else {
4978 DCHECK(cls.IsStackSlot()) << cls;
4979 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4980 }
4981 __ j(kNotEqual, &loop);
4982 break;
4983 }
4984 case TypeCheckKind::kClassHierarchyCheck: {
4985 // Walk over the class hierarchy to find a match.
4986 NearLabel loop, success;
4987 __ Bind(&loop);
4988 if (cls.IsRegister()) {
4989 __ cmpl(temp, cls.AsRegister<CpuRegister>());
4990 } else {
4991 DCHECK(cls.IsStackSlot()) << cls;
4992 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
4993 }
4994 __ j(kEqual, &success);
4995 __ movl(temp, Address(temp, super_offset));
4996 __ MaybeUnpoisonHeapReference(temp);
4997 __ testl(temp, temp);
4998 __ j(kNotEqual, &loop);
4999 // Jump to the slow path to throw the exception.
5000 __ jmp(slow_path->GetEntryLabel());
5001 __ Bind(&success);
5002 break;
5003 }
5004 case TypeCheckKind::kArrayObjectCheck: {
5005 // Just need to check that the object's class is a non primitive array.
5006 __ movl(temp, Address(temp, component_offset));
5007 __ MaybeUnpoisonHeapReference(temp);
5008 __ testl(temp, temp);
5009 __ j(kEqual, slow_path->GetEntryLabel());
5010 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5011 __ j(kNotEqual, slow_path->GetEntryLabel());
5012 break;
5013 }
5014 case TypeCheckKind::kInterfaceCheck:
5015 default:
5016 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5017 instruction,
5018 instruction->GetDexPc(),
5019 nullptr);
5020 break;
5021 }
5022 __ Bind(&done);
5023
5024 if (slow_path != nullptr) {
5025 __ Bind(slow_path->GetExitLabel());
5026 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005027}
5028
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005029void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5030 LocationSummary* locations =
5031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5032 InvokeRuntimeCallingConvention calling_convention;
5033 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5034}
5035
5036void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005037 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5038 : QUICK_ENTRY_POINT(pUnlockObject),
5039 instruction,
5040 instruction->GetDexPc(),
5041 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005042}
5043
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005044void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5045void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5046void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5047
5048void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5049 LocationSummary* locations =
5050 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5051 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5052 || instruction->GetResultType() == Primitive::kPrimLong);
5053 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005054 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005055 locations->SetOut(Location::SameAsFirstInput());
5056}
5057
5058void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5059 HandleBitwiseOperation(instruction);
5060}
5061
5062void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5063 HandleBitwiseOperation(instruction);
5064}
5065
5066void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5067 HandleBitwiseOperation(instruction);
5068}
5069
5070void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5071 LocationSummary* locations = instruction->GetLocations();
5072 Location first = locations->InAt(0);
5073 Location second = locations->InAt(1);
5074 DCHECK(first.Equals(locations->Out()));
5075
5076 if (instruction->GetResultType() == Primitive::kPrimInt) {
5077 if (second.IsRegister()) {
5078 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005079 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005080 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005081 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005082 } else {
5083 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005084 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005085 }
5086 } else if (second.IsConstant()) {
5087 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5088 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005089 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005090 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005091 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005092 } else {
5093 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005094 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005095 }
5096 } else {
5097 Address address(CpuRegister(RSP), second.GetStackIndex());
5098 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005099 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005100 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005101 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005102 } else {
5103 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005104 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005105 }
5106 }
5107 } else {
5108 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005109 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5110 bool second_is_constant = false;
5111 int64_t value = 0;
5112 if (second.IsConstant()) {
5113 second_is_constant = true;
5114 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005115 }
Mark Mendell40741f32015-04-20 22:10:34 -04005116 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005117
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005118 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005119 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005120 if (is_int32_value) {
5121 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5122 } else {
5123 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5124 }
5125 } else if (second.IsDoubleStackSlot()) {
5126 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005127 } else {
5128 __ andq(first_reg, second.AsRegister<CpuRegister>());
5129 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005130 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005131 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005132 if (is_int32_value) {
5133 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5134 } else {
5135 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5136 }
5137 } else if (second.IsDoubleStackSlot()) {
5138 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005139 } else {
5140 __ orq(first_reg, second.AsRegister<CpuRegister>());
5141 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005142 } else {
5143 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005144 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005145 if (is_int32_value) {
5146 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5147 } else {
5148 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5149 }
5150 } else if (second.IsDoubleStackSlot()) {
5151 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005152 } else {
5153 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5154 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005155 }
5156 }
5157}
5158
Calin Juravleb1498f62015-02-16 13:13:29 +00005159void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
5160 // Nothing to do, this should be removed during prepare for register allocator.
5161 UNUSED(instruction);
5162 LOG(FATAL) << "Unreachable";
5163}
5164
5165void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
5166 // Nothing to do, this should be removed during prepare for register allocator.
5167 UNUSED(instruction);
5168 LOG(FATAL) << "Unreachable";
5169}
5170
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005171void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
5172 DCHECK(codegen_->IsBaseline());
5173 LocationSummary* locations =
5174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5175 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5176}
5177
5178void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5179 DCHECK(codegen_->IsBaseline());
5180 // Will be generated at use site.
5181}
5182
Mark Mendell92e83bf2015-05-07 11:25:03 -04005183void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
5184 if (value == 0) {
5185 __ xorl(dest, dest);
5186 } else if (value > 0 && IsInt<32>(value)) {
5187 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
5188 __ movl(dest, Immediate(static_cast<int32_t>(value)));
5189 } else {
5190 __ movq(dest, Immediate(value));
5191 }
5192}
5193
Mark Mendellcfa410b2015-05-25 16:02:44 -04005194void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
5195 DCHECK(dest.IsDoubleStackSlot());
5196 if (IsInt<32>(value)) {
5197 // Can move directly as an int32 constant.
5198 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
5199 Immediate(static_cast<int32_t>(value)));
5200 } else {
5201 Load64BitValue(CpuRegister(TMP), value);
5202 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
5203 }
5204}
5205
Mark Mendellf55c3e02015-03-26 21:07:46 -04005206void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
5207 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04005208 X86_64Assembler* assembler = GetAssembler();
5209 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04005210 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5211 // byte values. If used for vectors at a later time, this will need to be
5212 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04005213 assembler->Align(4, 0);
5214 constant_area_start_ = assembler->CodeSize();
5215 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04005216 }
5217
5218 // And finish up.
5219 CodeGenerator::Finalize(allocator);
5220}
5221
5222/**
5223 * Class to handle late fixup of offsets into constant area.
5224 */
5225class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5226 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04005227 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04005228 : codegen_(codegen), offset_into_constant_area_(offset) {}
5229
5230 private:
5231 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5232 // Patch the correct offset for the instruction. We use the address of the
5233 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
5234 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5235 int relative_position = constant_offset - pos;
5236
5237 // Patch in the right value.
5238 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5239 }
5240
Mark Mendell39dcf552015-04-09 20:42:42 -04005241 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04005242
5243 // Location in constant area that the fixup refers to.
5244 int offset_into_constant_area_;
5245};
5246
5247Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
5248 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5249 return Address::RIP(fixup);
5250}
5251
5252Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
5253 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5254 return Address::RIP(fixup);
5255}
5256
5257Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
5258 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5259 return Address::RIP(fixup);
5260}
5261
5262Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
5263 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5264 return Address::RIP(fixup);
5265}
5266
Andreas Gampe85b62f22015-09-09 13:15:38 -07005267// TODO: trg as memory.
5268void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5269 if (!trg.IsValid()) {
5270 DCHECK(type == Primitive::kPrimVoid);
5271 return;
5272 }
5273
5274 DCHECK_NE(type, Primitive::kPrimVoid);
5275
5276 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
5277 if (trg.Equals(return_loc)) {
5278 return;
5279 }
5280
5281 // Let the parallel move resolver take care of all of this.
5282 HParallelMove parallel_move(GetGraph()->GetArena());
5283 parallel_move.AddMove(return_loc, trg, type, nullptr);
5284 GetMoveResolver()->EmitNativeCode(&parallel_move);
5285}
5286
Roland Levillain4d027112015-07-01 15:41:14 +01005287#undef __
5288
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005289} // namespace x86_64
5290} // namespace art