blob: c85275877e7652a134390feb47fdb63ff5fe105e [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
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010053class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Alexandre Rames2ed20af2015-03-06 13:55:35 +000057 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010058 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000060 if (instruction_->CanThrowIntoCatchBlock()) {
61 // Live registers will be restored in the catch block if caught.
62 SaveLiveRegisters(codegen, instruction_->GetLocations());
63 }
Alexandre Rames8158f282015-08-07 10:26:17 +010064 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
65 instruction_,
66 instruction_->GetDexPc(),
67 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 }
69
Alexandre Rames8158f282015-08-07 10:26:17 +010070 bool IsFatal() const OVERRIDE { return true; }
71
Alexandre Rames9931f312015-06-19 14:47:01 +010072 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
73
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
77};
78
Calin Juravled0d48522014-11-04 16:40:20 +000079class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
80 public:
81 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
82
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010084 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000085 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000086 if (instruction_->CanThrowIntoCatchBlock()) {
87 // Live registers will be restored in the catch block if caught.
88 SaveLiveRegisters(codegen, instruction_->GetLocations());
89 }
Alexandre Rames8158f282015-08-07 10:26:17 +010090 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
91 instruction_,
92 instruction_->GetDexPc(),
93 this);
Calin Juravled0d48522014-11-04 16:40:20 +000094 }
95
Alexandre Rames8158f282015-08-07 10:26:17 +010096 bool IsFatal() const OVERRIDE { return true; }
97
Alexandre Rames9931f312015-06-19 14:47:01 +010098 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
99
Calin Juravled0d48522014-11-04 16:40:20 +0000100 private:
101 HDivZeroCheck* const instruction_;
102 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
103};
104
Calin Juravlebacfec32014-11-14 15:54:36 +0000105class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +0000106 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100107 DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000110 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000112 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(cpu_reg_);
115 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400116 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000117 }
118
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000119 } else {
120 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000121 if (is_div_) {
122 __ negq(cpu_reg_);
123 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400124 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 }
Calin Juravled0d48522014-11-04 16:40:20 +0000127 __ jmp(GetExitLabel());
128 }
129
Alexandre Rames9931f312015-06-19 14:47:01 +0100130 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
131
Calin Juravled0d48522014-11-04 16:40:20 +0000132 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000133 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000134 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000135 const bool is_div_;
136 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000137};
138
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100139class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100141 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000144 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100145 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000147 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100148 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
149 instruction_,
150 instruction_->GetDexPc(),
151 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000152 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100153 if (successor_ == nullptr) {
154 __ jmp(GetReturnLabel());
155 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100156 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 }
159
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 Label* GetReturnLabel() {
161 DCHECK(successor_ == nullptr);
162 return &return_label_;
163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100165 HBasicBlock* GetSuccessor() const {
166 return successor_;
167 }
168
Alexandre Rames9931f312015-06-19 14:47:01 +0100169 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
170
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 private:
172 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
177};
178
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100179class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100181 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
182 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000184 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100185 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames8158f282015-08-07 10:26:17 +0100186 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000188 if (instruction_->CanThrowIntoCatchBlock()) {
189 // Live registers will be restored in the catch block if caught.
190 SaveLiveRegisters(codegen, instruction_->GetLocations());
191 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000192 // We're moving two locations to locations that could overlap, so we need a parallel
193 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000195 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100196 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000197 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100198 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100199 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100200 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
201 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100202 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
203 instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100204 }
205
Alexandre Rames8158f282015-08-07 10:26:17 +0100206 bool IsFatal() const OVERRIDE { return true; }
207
Alexandre Rames9931f312015-06-19 14:47:01 +0100208 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
209
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100210 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100211 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100212
213 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
214};
215
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100217 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000218 LoadClassSlowPathX86_64(HLoadClass* cls,
219 HInstruction* at,
220 uint32_t dex_pc,
221 bool do_clinit)
222 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
223 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
224 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
229 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000231 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100233 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100235 x64_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
236 : QUICK_ENTRY_POINT(pInitializeType),
237 at_, dex_pc_, this);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100238
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000239 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000241 if (out.IsValid()) {
242 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
243 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 }
245
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000246 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100247 __ jmp(GetExitLabel());
248 }
249
Alexandre Rames9931f312015-06-19 14:47:01 +0100250 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
251
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 // The class this slow path will load.
254 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100255
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 // The instruction where this slow path is happening.
257 // (Might be the load class or an initialization check).
258 HInstruction* const at_;
259
260 // The dex PC of `at_`.
261 const uint32_t dex_pc_;
262
263 // Whether to initialize the class.
264 const bool do_clinit_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100267};
268
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000269class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
270 public:
271 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
272
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274 LocationSummary* locations = instruction_->GetLocations();
275 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
276
277 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
278 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000279 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000280
281 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800282 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000283 Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100284 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
285 instruction_,
286 instruction_->GetDexPc(),
287 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000288 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000289 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000290 __ jmp(GetExitLabel());
291 }
292
Alexandre Rames9931f312015-06-19 14:47:01 +0100293 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
294
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000295 private:
296 HLoadString* const instruction_;
297
298 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
299};
300
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
302 public:
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100303 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 Geoffray64acf302015-09-14 22:20:29 +0100316
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 Geoffray64acf302015-09-14 22:20:29 +0100354 if (!is_fatal_) {
355 if (instruction_->IsInstanceOf()) {
356 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
357 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000358
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100359 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 Geoffray64acf302015-09-14 22:20:29 +0100366 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 Geoffray64acf302015-09-14 22:20:29 +0100370 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000371
372 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
373};
374
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700375class DeoptimizationSlowPathX86_64 : public SlowPathCodeX86_64 {
376 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
Calin Juravle23a8e352015-09-08 19:56:31 +0100948void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
949 if (location.IsRegister()) {
950 locations->AddTemp(location);
951 } else {
952 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
953 }
954}
955
956void CodeGeneratorX86_64::MoveLocationToTemp(Location source,
957 const LocationSummary& locations,
958 int temp_index,
959 Primitive::Type type) {
960 if (!Primitive::IsFloatingPointType(type)) {
961 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
962 }
963
964 DCHECK(source.IsFpuRegister()) << source;
965 __ movd(locations.GetTemp(temp_index).AsRegister<CpuRegister>(),
966 source.AsFpuRegister<XmmRegister>(),
967 Primitive::Is64BitType(type));
968}
969
970void CodeGeneratorX86_64::MoveTempToLocation(const LocationSummary& locations,
971 int temp_index,
972 Location destination,
973 Primitive::Type type) {
974 if (!Primitive::IsFloatingPointType(type)) {
975 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
976 }
977
978 DCHECK(destination.IsFpuRegister()) << destination;
979 __ movd(destination.AsFpuRegister<XmmRegister>(),
980 locations.GetTemp(temp_index).AsRegister<CpuRegister>(),
981 Primitive::Is64BitType(type));
982}
983
David Brazdilfc6a86a2015-06-26 10:33:45 +0000984void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100985 DCHECK(!successor->IsExitBlock());
986
987 HBasicBlock* block = got->GetBlock();
988 HInstruction* previous = got->GetPrevious();
989
990 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000991 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100992 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
993 return;
994 }
995
996 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
997 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
998 }
999 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001000 __ jmp(codegen_->GetLabelOf(successor));
1001 }
1002}
1003
David Brazdilfc6a86a2015-06-26 10:33:45 +00001004void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1005 got->SetLocations(nullptr);
1006}
1007
1008void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1009 HandleGoto(got, got->GetSuccessor());
1010}
1011
1012void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1013 try_boundary->SetLocations(nullptr);
1014}
1015
1016void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1017 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1018 if (!successor->IsExitBlock()) {
1019 HandleGoto(try_boundary, successor);
1020 }
1021}
1022
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001023void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1024 exit->SetLocations(nullptr);
1025}
1026
1027void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001028 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001029}
1030
Mark Mendellc4701932015-04-10 13:18:51 -04001031void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
1032 Label* true_label,
1033 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001034 if (cond->IsFPConditionTrueIfNaN()) {
1035 __ j(kUnordered, true_label);
1036 } else if (cond->IsFPConditionFalseIfNaN()) {
1037 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001038 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001039 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001040}
1041
1042void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
1043 HCondition* condition,
1044 Label* true_target,
1045 Label* false_target,
1046 Label* always_true_target) {
1047 LocationSummary* locations = condition->GetLocations();
1048 Location left = locations->InAt(0);
1049 Location right = locations->InAt(1);
1050
1051 // We don't want true_target as a nullptr.
1052 if (true_target == nullptr) {
1053 true_target = always_true_target;
1054 }
1055 bool falls_through = (false_target == nullptr);
1056
1057 // FP compares don't like null false_targets.
1058 if (false_target == nullptr) {
1059 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1060 }
1061
1062 Primitive::Type type = condition->InputAt(0)->GetType();
1063 switch (type) {
1064 case Primitive::kPrimLong: {
1065 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1066 if (right.IsConstant()) {
1067 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1068 if (IsInt<32>(value)) {
1069 if (value == 0) {
1070 __ testq(left_reg, left_reg);
1071 } else {
1072 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1073 }
1074 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001075 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001076 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1077 }
1078 } else if (right.IsDoubleStackSlot()) {
1079 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1080 } else {
1081 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1082 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001083 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001084 break;
1085 }
1086 case Primitive::kPrimFloat: {
1087 if (right.IsFpuRegister()) {
1088 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1089 } else if (right.IsConstant()) {
1090 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1091 codegen_->LiteralFloatAddress(
1092 right.GetConstant()->AsFloatConstant()->GetValue()));
1093 } else {
1094 DCHECK(right.IsStackSlot());
1095 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1096 Address(CpuRegister(RSP), right.GetStackIndex()));
1097 }
1098 GenerateFPJumps(condition, true_target, false_target);
1099 break;
1100 }
1101 case Primitive::kPrimDouble: {
1102 if (right.IsFpuRegister()) {
1103 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1104 } else if (right.IsConstant()) {
1105 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1106 codegen_->LiteralDoubleAddress(
1107 right.GetConstant()->AsDoubleConstant()->GetValue()));
1108 } else {
1109 DCHECK(right.IsDoubleStackSlot());
1110 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1111 Address(CpuRegister(RSP), right.GetStackIndex()));
1112 }
1113 GenerateFPJumps(condition, true_target, false_target);
1114 break;
1115 }
1116 default:
1117 LOG(FATAL) << "Unexpected condition type " << type;
1118 }
1119
1120 if (!falls_through) {
1121 __ jmp(false_target);
1122 }
1123}
1124
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001125void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1126 Label* true_target,
1127 Label* false_target,
1128 Label* always_true_target) {
1129 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001130 if (cond->IsIntConstant()) {
1131 // Constant condition, statically compared against 1.
1132 int32_t cond_value = cond->AsIntConstant()->GetValue();
1133 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001134 if (always_true_target != nullptr) {
1135 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001136 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001137 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001138 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001139 DCHECK_EQ(cond_value, 0);
1140 }
1141 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001142 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001143 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1144 // Moves do not affect the eflags register, so if the condition is
1145 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001146 // again. We can't use the eflags on FP conditions if they are
1147 // materialized due to the complex branching.
1148 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001149 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001150 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1151 && !Primitive::IsFloatingPointType(type);
1152
Roland Levillain4fa13f62015-07-06 18:11:54 +01001153 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001154 if (!eflags_set) {
1155 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001156 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001157 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001158 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001159 } else {
1160 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1161 Immediate(0));
1162 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001163 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001164 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001165 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001166 }
1167 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001168 // Condition has not been materialized, use its inputs as the
1169 // comparison and its condition as the branch condition.
1170
Mark Mendellc4701932015-04-10 13:18:51 -04001171 // Is this a long or FP comparison that has been folded into the HCondition?
1172 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001173 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001174 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1175 true_target, false_target, always_true_target);
1176 return;
1177 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001178
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001179 Location lhs = cond->GetLocations()->InAt(0);
1180 Location rhs = cond->GetLocations()->InAt(1);
1181 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001182 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001183 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001184 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001185 if (constant == 0) {
1186 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1187 } else {
1188 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1189 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001190 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001191 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001192 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1193 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001194 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001195 }
Dave Allison20dfc792014-06-16 20:44:29 -07001196 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001197 if (false_target != nullptr) {
1198 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001199 }
1200}
1201
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001202void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1203 LocationSummary* locations =
1204 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1205 HInstruction* cond = if_instr->InputAt(0);
1206 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1207 locations->SetInAt(0, Location::Any());
1208 }
1209}
1210
1211void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1212 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1213 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1214 Label* always_true_target = true_target;
1215 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1216 if_instr->IfTrueSuccessor())) {
1217 always_true_target = nullptr;
1218 }
1219 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1220 if_instr->IfFalseSuccessor())) {
1221 false_target = nullptr;
1222 }
1223 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1224}
1225
1226void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1227 LocationSummary* locations = new (GetGraph()->GetArena())
1228 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1229 HInstruction* cond = deoptimize->InputAt(0);
1230 DCHECK(cond->IsCondition());
1231 if (cond->AsCondition()->NeedsMaterialization()) {
1232 locations->SetInAt(0, Location::Any());
1233 }
1234}
1235
1236void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1237 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena())
1238 DeoptimizationSlowPathX86_64(deoptimize);
1239 codegen_->AddSlowPath(slow_path);
1240 Label* slow_path_entry = slow_path->GetEntryLabel();
1241 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1242}
1243
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001244void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1245 local->SetLocations(nullptr);
1246}
1247
1248void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1249 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1250}
1251
1252void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1253 local->SetLocations(nullptr);
1254}
1255
1256void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1257 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001258 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001259}
1260
1261void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001262 LocationSummary* locations =
1263 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264 switch (store->InputAt(1)->GetType()) {
1265 case Primitive::kPrimBoolean:
1266 case Primitive::kPrimByte:
1267 case Primitive::kPrimChar:
1268 case Primitive::kPrimShort:
1269 case Primitive::kPrimInt:
1270 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001271 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001272 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1273 break;
1274
1275 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001276 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001277 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1278 break;
1279
1280 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001281 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001282 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001283}
1284
1285void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001286 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001287}
1288
Roland Levillain0d37cd02015-05-27 16:39:19 +01001289void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001290 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001291 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001292 // Handle the long/FP comparisons made in instruction simplification.
1293 switch (cond->InputAt(0)->GetType()) {
1294 case Primitive::kPrimLong:
1295 locations->SetInAt(0, Location::RequiresRegister());
1296 locations->SetInAt(1, Location::Any());
1297 break;
1298 case Primitive::kPrimFloat:
1299 case Primitive::kPrimDouble:
1300 locations->SetInAt(0, Location::RequiresFpuRegister());
1301 locations->SetInAt(1, Location::Any());
1302 break;
1303 default:
1304 locations->SetInAt(0, Location::RequiresRegister());
1305 locations->SetInAt(1, Location::Any());
1306 break;
1307 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001308 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001309 locations->SetOut(Location::RequiresRegister());
1310 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001311}
1312
Roland Levillain0d37cd02015-05-27 16:39:19 +01001313void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001314 if (!cond->NeedsMaterialization()) {
1315 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001316 }
Mark Mendellc4701932015-04-10 13:18:51 -04001317
1318 LocationSummary* locations = cond->GetLocations();
1319 Location lhs = locations->InAt(0);
1320 Location rhs = locations->InAt(1);
1321 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1322 Label true_label, false_label;
1323
1324 switch (cond->InputAt(0)->GetType()) {
1325 default:
1326 // Integer case.
1327
1328 // Clear output register: setcc only sets the low byte.
1329 __ xorl(reg, reg);
1330
1331 if (rhs.IsRegister()) {
1332 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1333 } else if (rhs.IsConstant()) {
1334 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1335 if (constant == 0) {
1336 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1337 } else {
1338 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1339 }
1340 } else {
1341 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1342 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001343 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001344 return;
1345 case Primitive::kPrimLong:
1346 // Clear output register: setcc only sets the low byte.
1347 __ xorl(reg, reg);
1348
1349 if (rhs.IsRegister()) {
1350 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1351 } else if (rhs.IsConstant()) {
1352 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1353 if (IsInt<32>(value)) {
1354 if (value == 0) {
1355 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1356 } else {
1357 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1358 }
1359 } else {
1360 // Value won't fit in an int.
1361 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1362 }
1363 } else {
1364 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1365 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001366 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001367 return;
1368 case Primitive::kPrimFloat: {
1369 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1370 if (rhs.IsConstant()) {
1371 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1372 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1373 } else if (rhs.IsStackSlot()) {
1374 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1375 } else {
1376 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1377 }
1378 GenerateFPJumps(cond, &true_label, &false_label);
1379 break;
1380 }
1381 case Primitive::kPrimDouble: {
1382 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1383 if (rhs.IsConstant()) {
1384 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1385 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1386 } else if (rhs.IsDoubleStackSlot()) {
1387 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1388 } else {
1389 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1390 }
1391 GenerateFPJumps(cond, &true_label, &false_label);
1392 break;
1393 }
1394 }
1395
1396 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001397 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001398
Roland Levillain4fa13f62015-07-06 18:11:54 +01001399 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001400 __ Bind(&false_label);
1401 __ xorl(reg, reg);
1402 __ jmp(&done_label);
1403
Roland Levillain4fa13f62015-07-06 18:11:54 +01001404 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001405 __ Bind(&true_label);
1406 __ movl(reg, Immediate(1));
1407 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001408}
1409
1410void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1411 VisitCondition(comp);
1412}
1413
1414void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1415 VisitCondition(comp);
1416}
1417
1418void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1419 VisitCondition(comp);
1420}
1421
1422void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1423 VisitCondition(comp);
1424}
1425
1426void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1427 VisitCondition(comp);
1428}
1429
1430void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1431 VisitCondition(comp);
1432}
1433
1434void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1435 VisitCondition(comp);
1436}
1437
1438void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1439 VisitCondition(comp);
1440}
1441
1442void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1443 VisitCondition(comp);
1444}
1445
1446void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1447 VisitCondition(comp);
1448}
1449
1450void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1451 VisitCondition(comp);
1452}
1453
1454void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1455 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001456}
1457
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001458void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001459 LocationSummary* locations =
1460 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001461 switch (compare->InputAt(0)->GetType()) {
1462 case Primitive::kPrimLong: {
1463 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001464 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1466 break;
1467 }
1468 case Primitive::kPrimFloat:
1469 case Primitive::kPrimDouble: {
1470 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001471 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001472 locations->SetOut(Location::RequiresRegister());
1473 break;
1474 }
1475 default:
1476 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1477 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001478}
1479
1480void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001481 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001482 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001483 Location left = locations->InAt(0);
1484 Location right = locations->InAt(1);
1485
Mark Mendell0c9497d2015-08-21 09:30:05 -04001486 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001487 Primitive::Type type = compare->InputAt(0)->GetType();
1488 switch (type) {
1489 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001490 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1491 if (right.IsConstant()) {
1492 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001493 if (IsInt<32>(value)) {
1494 if (value == 0) {
1495 __ testq(left_reg, left_reg);
1496 } else {
1497 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1498 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001499 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001500 // Value won't fit in an int.
1501 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001502 }
Mark Mendell40741f32015-04-20 22:10:34 -04001503 } else if (right.IsDoubleStackSlot()) {
1504 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001505 } else {
1506 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1507 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001508 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001509 }
1510 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001511 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1512 if (right.IsConstant()) {
1513 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1514 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1515 } else if (right.IsStackSlot()) {
1516 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1517 } else {
1518 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1519 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001520 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1521 break;
1522 }
1523 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001524 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1525 if (right.IsConstant()) {
1526 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1527 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1528 } else if (right.IsDoubleStackSlot()) {
1529 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1530 } else {
1531 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1532 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001533 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1534 break;
1535 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001536 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001537 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001538 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001539 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001540 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001541 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001542
Calin Juravle91debbc2014-11-26 19:01:09 +00001543 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001544 __ movl(out, Immediate(1));
1545 __ jmp(&done);
1546
1547 __ Bind(&less);
1548 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001549
1550 __ Bind(&done);
1551}
1552
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001553void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001554 LocationSummary* locations =
1555 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001556 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001557}
1558
1559void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001560 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001561 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001562}
1563
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001564void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1565 LocationSummary* locations =
1566 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1567 locations->SetOut(Location::ConstantLocation(constant));
1568}
1569
1570void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1571 // Will be generated at use site.
1572 UNUSED(constant);
1573}
1574
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001575void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001576 LocationSummary* locations =
1577 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001578 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001579}
1580
1581void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001582 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001583 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001584}
1585
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001586void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1587 LocationSummary* locations =
1588 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1589 locations->SetOut(Location::ConstantLocation(constant));
1590}
1591
1592void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1593 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001594 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001595}
1596
1597void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1598 LocationSummary* locations =
1599 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1600 locations->SetOut(Location::ConstantLocation(constant));
1601}
1602
1603void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1604 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001605 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001606}
1607
Calin Juravle27df7582015-04-17 19:12:31 +01001608void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1609 memory_barrier->SetLocations(nullptr);
1610}
1611
1612void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1613 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1614}
1615
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001616void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1617 ret->SetLocations(nullptr);
1618}
1619
1620void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001621 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001622 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001623}
1624
1625void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001626 LocationSummary* locations =
1627 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001628 switch (ret->InputAt(0)->GetType()) {
1629 case Primitive::kPrimBoolean:
1630 case Primitive::kPrimByte:
1631 case Primitive::kPrimChar:
1632 case Primitive::kPrimShort:
1633 case Primitive::kPrimInt:
1634 case Primitive::kPrimNot:
1635 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001636 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001637 break;
1638
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001639 case Primitive::kPrimFloat:
1640 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001641 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001642 break;
1643
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001644 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001645 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001646 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001647}
1648
1649void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1650 if (kIsDebugBuild) {
1651 switch (ret->InputAt(0)->GetType()) {
1652 case Primitive::kPrimBoolean:
1653 case Primitive::kPrimByte:
1654 case Primitive::kPrimChar:
1655 case Primitive::kPrimShort:
1656 case Primitive::kPrimInt:
1657 case Primitive::kPrimNot:
1658 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001659 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001660 break;
1661
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001662 case Primitive::kPrimFloat:
1663 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001664 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001665 XMM0);
1666 break;
1667
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001668 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001669 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001670 }
1671 }
1672 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001673}
1674
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001675Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1676 switch (type) {
1677 case Primitive::kPrimBoolean:
1678 case Primitive::kPrimByte:
1679 case Primitive::kPrimChar:
1680 case Primitive::kPrimShort:
1681 case Primitive::kPrimInt:
1682 case Primitive::kPrimNot:
1683 case Primitive::kPrimLong:
1684 return Location::RegisterLocation(RAX);
1685
1686 case Primitive::kPrimVoid:
1687 return Location::NoLocation();
1688
1689 case Primitive::kPrimDouble:
1690 case Primitive::kPrimFloat:
1691 return Location::FpuRegisterLocation(XMM0);
1692 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001693
1694 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001695}
1696
1697Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1698 return Location::RegisterLocation(kMethodRegisterArgument);
1699}
1700
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001701Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001702 switch (type) {
1703 case Primitive::kPrimBoolean:
1704 case Primitive::kPrimByte:
1705 case Primitive::kPrimChar:
1706 case Primitive::kPrimShort:
1707 case Primitive::kPrimInt:
1708 case Primitive::kPrimNot: {
1709 uint32_t index = gp_index_++;
1710 stack_index_++;
1711 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001712 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001713 } else {
1714 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1715 }
1716 }
1717
1718 case Primitive::kPrimLong: {
1719 uint32_t index = gp_index_;
1720 stack_index_ += 2;
1721 if (index < calling_convention.GetNumberOfRegisters()) {
1722 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001723 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001724 } else {
1725 gp_index_ += 2;
1726 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1727 }
1728 }
1729
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001730 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001731 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001732 stack_index_++;
1733 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001734 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001735 } else {
1736 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1737 }
1738 }
1739
1740 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001741 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001742 stack_index_ += 2;
1743 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001744 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001745 } else {
1746 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1747 }
1748 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001749
1750 case Primitive::kPrimVoid:
1751 LOG(FATAL) << "Unexpected parameter type " << type;
1752 break;
1753 }
1754 return Location();
1755}
1756
Calin Juravle175dc732015-08-25 15:42:32 +01001757void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1758 // The trampoline uses the same calling convention as dex calling conventions,
1759 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1760 // the method_idx.
1761 HandleInvoke(invoke);
1762}
1763
1764void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1765 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1766}
1767
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001768void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001769 // When we do not run baseline, explicit clinit checks triggered by static
1770 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1771 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001772
Mark Mendellfb8d2792015-03-31 22:16:59 -04001773 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001774 if (intrinsic.TryDispatch(invoke)) {
1775 return;
1776 }
1777
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001778 HandleInvoke(invoke);
1779}
1780
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001781static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1782 if (invoke->GetLocations()->Intrinsified()) {
1783 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1784 intrinsic.Dispatch(invoke);
1785 return true;
1786 }
1787 return false;
1788}
1789
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001790void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001791 // When we do not run baseline, explicit clinit checks triggered by static
1792 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1793 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001794
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001795 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1796 return;
1797 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001798
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001799 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001800 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001801 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001802 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001803}
1804
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001805void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001806 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001807 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001808}
1809
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001810void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001811 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001812 if (intrinsic.TryDispatch(invoke)) {
1813 return;
1814 }
1815
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001816 HandleInvoke(invoke);
1817}
1818
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001819void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001820 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1821 return;
1822 }
1823
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001824 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001825
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001826 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001827 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001828}
1829
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001830void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1831 HandleInvoke(invoke);
1832 // Add the hidden argument.
1833 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1834}
1835
1836void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1837 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001838 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001839 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1840 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001841 LocationSummary* locations = invoke->GetLocations();
1842 Location receiver = locations->InAt(0);
1843 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1844
1845 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001846 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1847 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001848
1849 // temp = object->GetClass();
1850 if (receiver.IsStackSlot()) {
1851 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1852 __ movl(temp, Address(temp, class_offset));
1853 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001854 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001855 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001856 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001857 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001858 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001859 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001860 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001861 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001862 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001863
1864 DCHECK(!codegen_->IsLeafMethod());
1865 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1866}
1867
Roland Levillain88cb1752014-10-20 16:36:47 +01001868void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1869 LocationSummary* locations =
1870 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1871 switch (neg->GetResultType()) {
1872 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001873 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001874 locations->SetInAt(0, Location::RequiresRegister());
1875 locations->SetOut(Location::SameAsFirstInput());
1876 break;
1877
Roland Levillain88cb1752014-10-20 16:36:47 +01001878 case Primitive::kPrimFloat:
1879 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001880 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001881 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001882 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001883 break;
1884
1885 default:
1886 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1887 }
1888}
1889
1890void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1891 LocationSummary* locations = neg->GetLocations();
1892 Location out = locations->Out();
1893 Location in = locations->InAt(0);
1894 switch (neg->GetResultType()) {
1895 case Primitive::kPrimInt:
1896 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001897 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001898 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001899 break;
1900
1901 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001902 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001903 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001904 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001905 break;
1906
Roland Levillain5368c212014-11-27 15:03:41 +00001907 case Primitive::kPrimFloat: {
1908 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001909 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001910 // Implement float negation with an exclusive or with value
1911 // 0x80000000 (mask for bit 31, representing the sign of a
1912 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001913 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001914 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001915 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001916 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001917
Roland Levillain5368c212014-11-27 15:03:41 +00001918 case Primitive::kPrimDouble: {
1919 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001920 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001921 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001922 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001923 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001924 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001925 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001926 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001927 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001928
1929 default:
1930 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1931 }
1932}
1933
Roland Levillaindff1f282014-11-05 14:15:05 +00001934void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1935 LocationSummary* locations =
1936 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1937 Primitive::Type result_type = conversion->GetResultType();
1938 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001939 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001940
David Brazdilb2bd1c52015-03-25 11:17:37 +00001941 // The Java language does not allow treating boolean as an integral type but
1942 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001943
Roland Levillaindff1f282014-11-05 14:15:05 +00001944 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001945 case Primitive::kPrimByte:
1946 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001947 case Primitive::kPrimBoolean:
1948 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001949 case Primitive::kPrimShort:
1950 case Primitive::kPrimInt:
1951 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001952 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001953 locations->SetInAt(0, Location::Any());
1954 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1955 break;
1956
1957 default:
1958 LOG(FATAL) << "Unexpected type conversion from " << input_type
1959 << " to " << result_type;
1960 }
1961 break;
1962
Roland Levillain01a8d712014-11-14 16:27:39 +00001963 case Primitive::kPrimShort:
1964 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001965 case Primitive::kPrimBoolean:
1966 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001967 case Primitive::kPrimByte:
1968 case Primitive::kPrimInt:
1969 case Primitive::kPrimChar:
1970 // Processing a Dex `int-to-short' instruction.
1971 locations->SetInAt(0, Location::Any());
1972 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1973 break;
1974
1975 default:
1976 LOG(FATAL) << "Unexpected type conversion from " << input_type
1977 << " to " << result_type;
1978 }
1979 break;
1980
Roland Levillain946e1432014-11-11 17:35:19 +00001981 case Primitive::kPrimInt:
1982 switch (input_type) {
1983 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001984 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001985 locations->SetInAt(0, Location::Any());
1986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1987 break;
1988
1989 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001990 // Processing a Dex `float-to-int' instruction.
1991 locations->SetInAt(0, Location::RequiresFpuRegister());
1992 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00001993 break;
1994
Roland Levillain946e1432014-11-11 17:35:19 +00001995 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001996 // Processing a Dex `double-to-int' instruction.
1997 locations->SetInAt(0, Location::RequiresFpuRegister());
1998 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001999 break;
2000
2001 default:
2002 LOG(FATAL) << "Unexpected type conversion from " << input_type
2003 << " to " << result_type;
2004 }
2005 break;
2006
Roland Levillaindff1f282014-11-05 14:15:05 +00002007 case Primitive::kPrimLong:
2008 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002009 case Primitive::kPrimBoolean:
2010 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002011 case Primitive::kPrimByte:
2012 case Primitive::kPrimShort:
2013 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002014 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002015 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002016 // TODO: We would benefit from a (to-be-implemented)
2017 // Location::RegisterOrStackSlot requirement for this input.
2018 locations->SetInAt(0, Location::RequiresRegister());
2019 locations->SetOut(Location::RequiresRegister());
2020 break;
2021
2022 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002023 // Processing a Dex `float-to-long' instruction.
2024 locations->SetInAt(0, Location::RequiresFpuRegister());
2025 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002026 break;
2027
Roland Levillaindff1f282014-11-05 14:15:05 +00002028 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002029 // Processing a Dex `double-to-long' instruction.
2030 locations->SetInAt(0, Location::RequiresFpuRegister());
2031 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002032 break;
2033
2034 default:
2035 LOG(FATAL) << "Unexpected type conversion from " << input_type
2036 << " to " << result_type;
2037 }
2038 break;
2039
Roland Levillain981e4542014-11-14 11:47:14 +00002040 case Primitive::kPrimChar:
2041 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002042 case Primitive::kPrimBoolean:
2043 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002044 case Primitive::kPrimByte:
2045 case Primitive::kPrimShort:
2046 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002047 // Processing a Dex `int-to-char' instruction.
2048 locations->SetInAt(0, Location::Any());
2049 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2050 break;
2051
2052 default:
2053 LOG(FATAL) << "Unexpected type conversion from " << input_type
2054 << " to " << result_type;
2055 }
2056 break;
2057
Roland Levillaindff1f282014-11-05 14:15:05 +00002058 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002059 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002060 case Primitive::kPrimBoolean:
2061 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002062 case Primitive::kPrimByte:
2063 case Primitive::kPrimShort:
2064 case Primitive::kPrimInt:
2065 case Primitive::kPrimChar:
2066 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002067 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002068 locations->SetOut(Location::RequiresFpuRegister());
2069 break;
2070
2071 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002072 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002073 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002074 locations->SetOut(Location::RequiresFpuRegister());
2075 break;
2076
Roland Levillaincff13742014-11-17 14:32:17 +00002077 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002078 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002079 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002080 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002081 break;
2082
2083 default:
2084 LOG(FATAL) << "Unexpected type conversion from " << input_type
2085 << " to " << result_type;
2086 };
2087 break;
2088
Roland Levillaindff1f282014-11-05 14:15:05 +00002089 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002090 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002091 case Primitive::kPrimBoolean:
2092 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002093 case Primitive::kPrimByte:
2094 case Primitive::kPrimShort:
2095 case Primitive::kPrimInt:
2096 case Primitive::kPrimChar:
2097 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002098 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002099 locations->SetOut(Location::RequiresFpuRegister());
2100 break;
2101
2102 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002103 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002104 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002105 locations->SetOut(Location::RequiresFpuRegister());
2106 break;
2107
Roland Levillaincff13742014-11-17 14:32:17 +00002108 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002109 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002110 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002111 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002112 break;
2113
2114 default:
2115 LOG(FATAL) << "Unexpected type conversion from " << input_type
2116 << " to " << result_type;
2117 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002118 break;
2119
2120 default:
2121 LOG(FATAL) << "Unexpected type conversion from " << input_type
2122 << " to " << result_type;
2123 }
2124}
2125
2126void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2127 LocationSummary* locations = conversion->GetLocations();
2128 Location out = locations->Out();
2129 Location in = locations->InAt(0);
2130 Primitive::Type result_type = conversion->GetResultType();
2131 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002132 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002133 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002134 case Primitive::kPrimByte:
2135 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002136 case Primitive::kPrimBoolean:
2137 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002138 case Primitive::kPrimShort:
2139 case Primitive::kPrimInt:
2140 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002141 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002142 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002143 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002144 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002146 Address(CpuRegister(RSP), in.GetStackIndex()));
2147 } else {
2148 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002149 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002150 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2151 }
2152 break;
2153
2154 default:
2155 LOG(FATAL) << "Unexpected type conversion from " << input_type
2156 << " to " << result_type;
2157 }
2158 break;
2159
Roland Levillain01a8d712014-11-14 16:27:39 +00002160 case Primitive::kPrimShort:
2161 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002162 case Primitive::kPrimBoolean:
2163 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002164 case Primitive::kPrimByte:
2165 case Primitive::kPrimInt:
2166 case Primitive::kPrimChar:
2167 // Processing a Dex `int-to-short' instruction.
2168 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002170 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002171 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002172 Address(CpuRegister(RSP), in.GetStackIndex()));
2173 } else {
2174 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002175 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002176 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2177 }
2178 break;
2179
2180 default:
2181 LOG(FATAL) << "Unexpected type conversion from " << input_type
2182 << " to " << result_type;
2183 }
2184 break;
2185
Roland Levillain946e1432014-11-11 17:35:19 +00002186 case Primitive::kPrimInt:
2187 switch (input_type) {
2188 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002189 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002190 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002191 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002192 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002193 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002194 Address(CpuRegister(RSP), in.GetStackIndex()));
2195 } else {
2196 DCHECK(in.IsConstant());
2197 DCHECK(in.GetConstant()->IsLongConstant());
2198 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002199 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002200 }
2201 break;
2202
Roland Levillain3f8f9362014-12-02 17:45:01 +00002203 case Primitive::kPrimFloat: {
2204 // Processing a Dex `float-to-int' instruction.
2205 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2206 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002207 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002208
2209 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002210 // if input >= (float)INT_MAX goto done
2211 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002212 __ j(kAboveEqual, &done);
2213 // if input == NaN goto nan
2214 __ j(kUnordered, &nan);
2215 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002216 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002217 __ jmp(&done);
2218 __ Bind(&nan);
2219 // output = 0
2220 __ xorl(output, output);
2221 __ Bind(&done);
2222 break;
2223 }
2224
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002225 case Primitive::kPrimDouble: {
2226 // Processing a Dex `double-to-int' instruction.
2227 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2228 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002229 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002230
2231 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002232 // if input >= (double)INT_MAX goto done
2233 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002234 __ j(kAboveEqual, &done);
2235 // if input == NaN goto nan
2236 __ j(kUnordered, &nan);
2237 // output = double-to-int-truncate(input)
2238 __ cvttsd2si(output, input);
2239 __ jmp(&done);
2240 __ Bind(&nan);
2241 // output = 0
2242 __ xorl(output, output);
2243 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002244 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002245 }
Roland Levillain946e1432014-11-11 17:35:19 +00002246
2247 default:
2248 LOG(FATAL) << "Unexpected type conversion from " << input_type
2249 << " to " << result_type;
2250 }
2251 break;
2252
Roland Levillaindff1f282014-11-05 14:15:05 +00002253 case Primitive::kPrimLong:
2254 switch (input_type) {
2255 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002256 case Primitive::kPrimBoolean:
2257 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002258 case Primitive::kPrimByte:
2259 case Primitive::kPrimShort:
2260 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002261 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002262 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002263 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002264 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002265 break;
2266
Roland Levillain624279f2014-12-04 11:54:28 +00002267 case Primitive::kPrimFloat: {
2268 // Processing a Dex `float-to-long' instruction.
2269 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2270 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002271 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002272
Mark Mendell92e83bf2015-05-07 11:25:03 -04002273 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002274 // if input >= (float)LONG_MAX goto done
2275 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002276 __ j(kAboveEqual, &done);
2277 // if input == NaN goto nan
2278 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002279 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002280 __ cvttss2si(output, input, true);
2281 __ jmp(&done);
2282 __ Bind(&nan);
2283 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002284 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002285 __ Bind(&done);
2286 break;
2287 }
2288
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002289 case Primitive::kPrimDouble: {
2290 // Processing a Dex `double-to-long' instruction.
2291 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2292 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002293 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002294
Mark Mendell92e83bf2015-05-07 11:25:03 -04002295 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002296 // if input >= (double)LONG_MAX goto done
2297 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002298 __ j(kAboveEqual, &done);
2299 // if input == NaN goto nan
2300 __ j(kUnordered, &nan);
2301 // output = double-to-long-truncate(input)
2302 __ cvttsd2si(output, input, true);
2303 __ jmp(&done);
2304 __ Bind(&nan);
2305 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002306 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002307 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002308 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002309 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002310
2311 default:
2312 LOG(FATAL) << "Unexpected type conversion from " << input_type
2313 << " to " << result_type;
2314 }
2315 break;
2316
Roland Levillain981e4542014-11-14 11:47:14 +00002317 case Primitive::kPrimChar:
2318 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002319 case Primitive::kPrimBoolean:
2320 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002321 case Primitive::kPrimByte:
2322 case Primitive::kPrimShort:
2323 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002324 // Processing a Dex `int-to-char' instruction.
2325 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002326 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002327 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002328 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002329 Address(CpuRegister(RSP), in.GetStackIndex()));
2330 } else {
2331 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002332 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002333 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2334 }
2335 break;
2336
2337 default:
2338 LOG(FATAL) << "Unexpected type conversion from " << input_type
2339 << " to " << result_type;
2340 }
2341 break;
2342
Roland Levillaindff1f282014-11-05 14:15:05 +00002343 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002344 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002345 case Primitive::kPrimBoolean:
2346 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002347 case Primitive::kPrimByte:
2348 case Primitive::kPrimShort:
2349 case Primitive::kPrimInt:
2350 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002351 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002352 if (in.IsRegister()) {
2353 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2354 } else if (in.IsConstant()) {
2355 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2356 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2357 if (v == 0) {
2358 __ xorps(dest, dest);
2359 } else {
2360 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2361 }
2362 } else {
2363 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2364 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2365 }
Roland Levillaincff13742014-11-17 14:32:17 +00002366 break;
2367
2368 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002369 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002370 if (in.IsRegister()) {
2371 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2372 } else if (in.IsConstant()) {
2373 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2374 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2375 if (v == 0) {
2376 __ xorps(dest, dest);
2377 } else {
2378 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2379 }
2380 } else {
2381 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2382 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2383 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002384 break;
2385
Roland Levillaincff13742014-11-17 14:32:17 +00002386 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002387 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002388 if (in.IsFpuRegister()) {
2389 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2390 } else if (in.IsConstant()) {
2391 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2392 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2393 if (bit_cast<int64_t, double>(v) == 0) {
2394 __ xorps(dest, dest);
2395 } else {
2396 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2397 }
2398 } else {
2399 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2400 Address(CpuRegister(RSP), in.GetStackIndex()));
2401 }
Roland Levillaincff13742014-11-17 14:32:17 +00002402 break;
2403
2404 default:
2405 LOG(FATAL) << "Unexpected type conversion from " << input_type
2406 << " to " << result_type;
2407 };
2408 break;
2409
Roland Levillaindff1f282014-11-05 14:15:05 +00002410 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002411 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002412 case Primitive::kPrimBoolean:
2413 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002414 case Primitive::kPrimByte:
2415 case Primitive::kPrimShort:
2416 case Primitive::kPrimInt:
2417 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002418 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002419 if (in.IsRegister()) {
2420 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2421 } else if (in.IsConstant()) {
2422 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2423 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2424 if (v == 0) {
2425 __ xorpd(dest, dest);
2426 } else {
2427 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2428 }
2429 } else {
2430 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2431 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2432 }
Roland Levillaincff13742014-11-17 14:32:17 +00002433 break;
2434
2435 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002436 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002437 if (in.IsRegister()) {
2438 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2439 } else if (in.IsConstant()) {
2440 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2441 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2442 if (v == 0) {
2443 __ xorpd(dest, dest);
2444 } else {
2445 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2446 }
2447 } else {
2448 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2449 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2450 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002451 break;
2452
Roland Levillaincff13742014-11-17 14:32:17 +00002453 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002454 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002455 if (in.IsFpuRegister()) {
2456 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2457 } else if (in.IsConstant()) {
2458 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2459 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2460 if (bit_cast<int32_t, float>(v) == 0) {
2461 __ xorpd(dest, dest);
2462 } else {
2463 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2464 }
2465 } else {
2466 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2467 Address(CpuRegister(RSP), in.GetStackIndex()));
2468 }
Roland Levillaincff13742014-11-17 14:32:17 +00002469 break;
2470
2471 default:
2472 LOG(FATAL) << "Unexpected type conversion from " << input_type
2473 << " to " << result_type;
2474 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002475 break;
2476
2477 default:
2478 LOG(FATAL) << "Unexpected type conversion from " << input_type
2479 << " to " << result_type;
2480 }
2481}
2482
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002483void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002484 LocationSummary* locations =
2485 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002486 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002487 case Primitive::kPrimInt: {
2488 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002489 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002491 break;
2492 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002493
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002494 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002495 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002496 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002497 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002499 break;
2500 }
2501
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002502 case Primitive::kPrimDouble:
2503 case Primitive::kPrimFloat: {
2504 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002505 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002506 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002507 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002508 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002509
2510 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002511 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002512 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002513}
2514
2515void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2516 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002517 Location first = locations->InAt(0);
2518 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002519 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002520
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002521 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002522 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002523 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002524 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2525 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002526 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2527 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002528 } else {
2529 __ leal(out.AsRegister<CpuRegister>(), Address(
2530 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2531 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002532 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002533 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2534 __ addl(out.AsRegister<CpuRegister>(),
2535 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2536 } else {
2537 __ leal(out.AsRegister<CpuRegister>(), Address(
2538 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2539 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002540 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002541 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002542 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002543 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002544 break;
2545 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002546
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002547 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002548 if (second.IsRegister()) {
2549 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2550 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002551 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2552 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002553 } else {
2554 __ leaq(out.AsRegister<CpuRegister>(), Address(
2555 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2556 }
2557 } else {
2558 DCHECK(second.IsConstant());
2559 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2560 int32_t int32_value = Low32Bits(value);
2561 DCHECK_EQ(int32_value, value);
2562 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2563 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2564 } else {
2565 __ leaq(out.AsRegister<CpuRegister>(), Address(
2566 first.AsRegister<CpuRegister>(), int32_value));
2567 }
2568 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002569 break;
2570 }
2571
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002572 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002573 if (second.IsFpuRegister()) {
2574 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2575 } else if (second.IsConstant()) {
2576 __ addss(first.AsFpuRegister<XmmRegister>(),
2577 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2578 } else {
2579 DCHECK(second.IsStackSlot());
2580 __ addss(first.AsFpuRegister<XmmRegister>(),
2581 Address(CpuRegister(RSP), second.GetStackIndex()));
2582 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002583 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002584 }
2585
2586 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002587 if (second.IsFpuRegister()) {
2588 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2589 } else if (second.IsConstant()) {
2590 __ addsd(first.AsFpuRegister<XmmRegister>(),
2591 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2592 } else {
2593 DCHECK(second.IsDoubleStackSlot());
2594 __ addsd(first.AsFpuRegister<XmmRegister>(),
2595 Address(CpuRegister(RSP), second.GetStackIndex()));
2596 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002597 break;
2598 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002599
2600 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002601 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002602 }
2603}
2604
2605void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002606 LocationSummary* locations =
2607 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002608 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002609 case Primitive::kPrimInt: {
2610 locations->SetInAt(0, Location::RequiresRegister());
2611 locations->SetInAt(1, Location::Any());
2612 locations->SetOut(Location::SameAsFirstInput());
2613 break;
2614 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002615 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002616 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002617 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002618 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002619 break;
2620 }
Calin Juravle11351682014-10-23 15:38:15 +01002621 case Primitive::kPrimFloat:
2622 case Primitive::kPrimDouble: {
2623 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002624 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002625 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002626 break;
Calin Juravle11351682014-10-23 15:38:15 +01002627 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002628 default:
Calin Juravle11351682014-10-23 15:38:15 +01002629 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002630 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002631}
2632
2633void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2634 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002635 Location first = locations->InAt(0);
2636 Location second = locations->InAt(1);
2637 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002638 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002639 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002640 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002641 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002642 } else if (second.IsConstant()) {
2643 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002645 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002646 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002647 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002648 break;
2649 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002650 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002651 if (second.IsConstant()) {
2652 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2653 DCHECK(IsInt<32>(value));
2654 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2655 } else {
2656 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2657 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002658 break;
2659 }
2660
Calin Juravle11351682014-10-23 15:38:15 +01002661 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002662 if (second.IsFpuRegister()) {
2663 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2664 } else if (second.IsConstant()) {
2665 __ subss(first.AsFpuRegister<XmmRegister>(),
2666 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2667 } else {
2668 DCHECK(second.IsStackSlot());
2669 __ subss(first.AsFpuRegister<XmmRegister>(),
2670 Address(CpuRegister(RSP), second.GetStackIndex()));
2671 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002672 break;
Calin Juravle11351682014-10-23 15:38:15 +01002673 }
2674
2675 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002676 if (second.IsFpuRegister()) {
2677 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2678 } else if (second.IsConstant()) {
2679 __ subsd(first.AsFpuRegister<XmmRegister>(),
2680 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2681 } else {
2682 DCHECK(second.IsDoubleStackSlot());
2683 __ subsd(first.AsFpuRegister<XmmRegister>(),
2684 Address(CpuRegister(RSP), second.GetStackIndex()));
2685 }
Calin Juravle11351682014-10-23 15:38:15 +01002686 break;
2687 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002688
2689 default:
Calin Juravle11351682014-10-23 15:38:15 +01002690 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002691 }
2692}
2693
Calin Juravle34bacdf2014-10-07 20:23:36 +01002694void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2695 LocationSummary* locations =
2696 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2697 switch (mul->GetResultType()) {
2698 case Primitive::kPrimInt: {
2699 locations->SetInAt(0, Location::RequiresRegister());
2700 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002701 if (mul->InputAt(1)->IsIntConstant()) {
2702 // Can use 3 operand multiply.
2703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2704 } else {
2705 locations->SetOut(Location::SameAsFirstInput());
2706 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002707 break;
2708 }
2709 case Primitive::kPrimLong: {
2710 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002711 locations->SetInAt(1, Location::Any());
2712 if (mul->InputAt(1)->IsLongConstant() &&
2713 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002714 // Can use 3 operand multiply.
2715 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2716 } else {
2717 locations->SetOut(Location::SameAsFirstInput());
2718 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002719 break;
2720 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002721 case Primitive::kPrimFloat:
2722 case Primitive::kPrimDouble: {
2723 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002724 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002725 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002726 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002727 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002728
2729 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002730 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002731 }
2732}
2733
2734void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2735 LocationSummary* locations = mul->GetLocations();
2736 Location first = locations->InAt(0);
2737 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002738 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002739 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002740 case Primitive::kPrimInt:
2741 // The constant may have ended up in a register, so test explicitly to avoid
2742 // problems where the output may not be the same as the first operand.
2743 if (mul->InputAt(1)->IsIntConstant()) {
2744 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2745 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2746 } else if (second.IsRegister()) {
2747 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002748 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002749 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002750 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002751 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002752 __ imull(first.AsRegister<CpuRegister>(),
2753 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002754 }
2755 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002756 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002757 // The constant may have ended up in a register, so test explicitly to avoid
2758 // problems where the output may not be the same as the first operand.
2759 if (mul->InputAt(1)->IsLongConstant()) {
2760 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2761 if (IsInt<32>(value)) {
2762 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2763 Immediate(static_cast<int32_t>(value)));
2764 } else {
2765 // Have to use the constant area.
2766 DCHECK(first.Equals(out));
2767 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2768 }
2769 } else if (second.IsRegister()) {
2770 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002771 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002772 } else {
2773 DCHECK(second.IsDoubleStackSlot());
2774 DCHECK(first.Equals(out));
2775 __ imulq(first.AsRegister<CpuRegister>(),
2776 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002777 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002778 break;
2779 }
2780
Calin Juravleb5bfa962014-10-21 18:02:24 +01002781 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002782 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002783 if (second.IsFpuRegister()) {
2784 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2785 } else if (second.IsConstant()) {
2786 __ mulss(first.AsFpuRegister<XmmRegister>(),
2787 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2788 } else {
2789 DCHECK(second.IsStackSlot());
2790 __ mulss(first.AsFpuRegister<XmmRegister>(),
2791 Address(CpuRegister(RSP), second.GetStackIndex()));
2792 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002793 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002794 }
2795
2796 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002797 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002798 if (second.IsFpuRegister()) {
2799 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2800 } else if (second.IsConstant()) {
2801 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2802 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2803 } else {
2804 DCHECK(second.IsDoubleStackSlot());
2805 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2806 Address(CpuRegister(RSP), second.GetStackIndex()));
2807 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002808 break;
2809 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002810
2811 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002812 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002813 }
2814}
2815
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002816void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2817 uint32_t stack_adjustment, bool is_float) {
2818 if (source.IsStackSlot()) {
2819 DCHECK(is_float);
2820 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2821 } else if (source.IsDoubleStackSlot()) {
2822 DCHECK(!is_float);
2823 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2824 } else {
2825 // Write the value to the temporary location on the stack and load to FP stack.
2826 if (is_float) {
2827 Location stack_temp = Location::StackSlot(temp_offset);
2828 codegen_->Move(stack_temp, source);
2829 __ flds(Address(CpuRegister(RSP), temp_offset));
2830 } else {
2831 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2832 codegen_->Move(stack_temp, source);
2833 __ fldl(Address(CpuRegister(RSP), temp_offset));
2834 }
2835 }
2836}
2837
2838void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2839 Primitive::Type type = rem->GetResultType();
2840 bool is_float = type == Primitive::kPrimFloat;
2841 size_t elem_size = Primitive::ComponentSize(type);
2842 LocationSummary* locations = rem->GetLocations();
2843 Location first = locations->InAt(0);
2844 Location second = locations->InAt(1);
2845 Location out = locations->Out();
2846
2847 // Create stack space for 2 elements.
2848 // TODO: enhance register allocator to ask for stack temporaries.
2849 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2850
2851 // Load the values to the FP stack in reverse order, using temporaries if needed.
2852 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2853 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2854
2855 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002856 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002857 __ Bind(&retry);
2858 __ fprem();
2859
2860 // Move FP status to AX.
2861 __ fstsw();
2862
2863 // And see if the argument reduction is complete. This is signaled by the
2864 // C2 FPU flag bit set to 0.
2865 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2866 __ j(kNotEqual, &retry);
2867
2868 // We have settled on the final value. Retrieve it into an XMM register.
2869 // Store FP top of stack to real stack.
2870 if (is_float) {
2871 __ fsts(Address(CpuRegister(RSP), 0));
2872 } else {
2873 __ fstl(Address(CpuRegister(RSP), 0));
2874 }
2875
2876 // Pop the 2 items from the FP stack.
2877 __ fucompp();
2878
2879 // Load the value from the stack into an XMM register.
2880 DCHECK(out.IsFpuRegister()) << out;
2881 if (is_float) {
2882 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2883 } else {
2884 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2885 }
2886
2887 // And remove the temporary stack space we allocated.
2888 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2889}
2890
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002891void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2892 DCHECK(instruction->IsDiv() || instruction->IsRem());
2893
2894 LocationSummary* locations = instruction->GetLocations();
2895 Location second = locations->InAt(1);
2896 DCHECK(second.IsConstant());
2897
2898 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2899 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002900 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002901
2902 DCHECK(imm == 1 || imm == -1);
2903
2904 switch (instruction->GetResultType()) {
2905 case Primitive::kPrimInt: {
2906 if (instruction->IsRem()) {
2907 __ xorl(output_register, output_register);
2908 } else {
2909 __ movl(output_register, input_register);
2910 if (imm == -1) {
2911 __ negl(output_register);
2912 }
2913 }
2914 break;
2915 }
2916
2917 case Primitive::kPrimLong: {
2918 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002919 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002920 } else {
2921 __ movq(output_register, input_register);
2922 if (imm == -1) {
2923 __ negq(output_register);
2924 }
2925 }
2926 break;
2927 }
2928
2929 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002930 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002931 }
2932}
2933
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002934void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002935 LocationSummary* locations = instruction->GetLocations();
2936 Location second = locations->InAt(1);
2937
2938 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2939 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2940
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002941 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002942
2943 DCHECK(IsPowerOfTwo(std::abs(imm)));
2944
2945 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2946
2947 if (instruction->GetResultType() == Primitive::kPrimInt) {
2948 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2949 __ testl(numerator, numerator);
2950 __ cmov(kGreaterEqual, tmp, numerator);
2951 int shift = CTZ(imm);
2952 __ sarl(tmp, Immediate(shift));
2953
2954 if (imm < 0) {
2955 __ negl(tmp);
2956 }
2957
2958 __ movl(output_register, tmp);
2959 } else {
2960 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2961 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2962
Mark Mendell92e83bf2015-05-07 11:25:03 -04002963 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002964 __ addq(rdx, numerator);
2965 __ testq(numerator, numerator);
2966 __ cmov(kGreaterEqual, rdx, numerator);
2967 int shift = CTZ(imm);
2968 __ sarq(rdx, Immediate(shift));
2969
2970 if (imm < 0) {
2971 __ negq(rdx);
2972 }
2973
2974 __ movq(output_register, rdx);
2975 }
2976}
2977
2978void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2979 DCHECK(instruction->IsDiv() || instruction->IsRem());
2980
2981 LocationSummary* locations = instruction->GetLocations();
2982 Location second = locations->InAt(1);
2983
2984 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
2985 : locations->GetTemp(0).AsRegister<CpuRegister>();
2986 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
2987 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
2988 : locations->Out().AsRegister<CpuRegister>();
2989 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2990
2991 DCHECK_EQ(RAX, eax.AsRegister());
2992 DCHECK_EQ(RDX, edx.AsRegister());
2993 if (instruction->IsDiv()) {
2994 DCHECK_EQ(RAX, out.AsRegister());
2995 } else {
2996 DCHECK_EQ(RDX, out.AsRegister());
2997 }
2998
2999 int64_t magic;
3000 int shift;
3001
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003002 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003003 if (instruction->GetResultType() == Primitive::kPrimInt) {
3004 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3005
3006 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3007
3008 __ movl(numerator, eax);
3009
Mark Mendell0c9497d2015-08-21 09:30:05 -04003010 NearLabel no_div;
3011 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003012 __ testl(eax, eax);
3013 __ j(kNotEqual, &no_div);
3014
3015 __ xorl(out, out);
3016 __ jmp(&end);
3017
3018 __ Bind(&no_div);
3019
3020 __ movl(eax, Immediate(magic));
3021 __ imull(numerator);
3022
3023 if (imm > 0 && magic < 0) {
3024 __ addl(edx, numerator);
3025 } else if (imm < 0 && magic > 0) {
3026 __ subl(edx, numerator);
3027 }
3028
3029 if (shift != 0) {
3030 __ sarl(edx, Immediate(shift));
3031 }
3032
3033 __ movl(eax, edx);
3034 __ shrl(edx, Immediate(31));
3035 __ addl(edx, eax);
3036
3037 if (instruction->IsRem()) {
3038 __ movl(eax, numerator);
3039 __ imull(edx, Immediate(imm));
3040 __ subl(eax, edx);
3041 __ movl(edx, eax);
3042 } else {
3043 __ movl(eax, edx);
3044 }
3045 __ Bind(&end);
3046 } else {
3047 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3048
3049 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3050
3051 CpuRegister rax = eax;
3052 CpuRegister rdx = edx;
3053
3054 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3055
3056 // Save the numerator.
3057 __ movq(numerator, rax);
3058
3059 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003060 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003061
3062 // RDX:RAX = magic * numerator
3063 __ imulq(numerator);
3064
3065 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003066 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003067 __ addq(rdx, numerator);
3068 } else if (imm < 0 && magic > 0) {
3069 // RDX -= numerator
3070 __ subq(rdx, numerator);
3071 }
3072
3073 // Shift if needed.
3074 if (shift != 0) {
3075 __ sarq(rdx, Immediate(shift));
3076 }
3077
3078 // RDX += 1 if RDX < 0
3079 __ movq(rax, rdx);
3080 __ shrq(rdx, Immediate(63));
3081 __ addq(rdx, rax);
3082
3083 if (instruction->IsRem()) {
3084 __ movq(rax, numerator);
3085
3086 if (IsInt<32>(imm)) {
3087 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3088 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003089 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003090 }
3091
3092 __ subq(rax, rdx);
3093 __ movq(rdx, rax);
3094 } else {
3095 __ movq(rax, rdx);
3096 }
3097 }
3098}
3099
Calin Juravlebacfec32014-11-14 15:54:36 +00003100void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3101 DCHECK(instruction->IsDiv() || instruction->IsRem());
3102 Primitive::Type type = instruction->GetResultType();
3103 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3104
3105 bool is_div = instruction->IsDiv();
3106 LocationSummary* locations = instruction->GetLocations();
3107
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003108 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3109 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003110
Roland Levillain271ab9c2014-11-27 15:23:57 +00003111 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003112 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003113
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003114 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003115 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003116
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003117 if (imm == 0) {
3118 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3119 } else if (imm == 1 || imm == -1) {
3120 DivRemOneOrMinusOne(instruction);
3121 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003122 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003123 } else {
3124 DCHECK(imm <= -2 || imm >= 2);
3125 GenerateDivRemWithAnyConstant(instruction);
3126 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003127 } else {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003128 SlowPathCodeX86_64* slow_path =
3129 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3130 out.AsRegister(), type, is_div);
3131 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003132
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003133 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3134 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3135 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3136 // so it's safe to just use negl instead of more complex comparisons.
3137 if (type == Primitive::kPrimInt) {
3138 __ cmpl(second_reg, Immediate(-1));
3139 __ j(kEqual, slow_path->GetEntryLabel());
3140 // edx:eax <- sign-extended of eax
3141 __ cdq();
3142 // eax = quotient, edx = remainder
3143 __ idivl(second_reg);
3144 } else {
3145 __ cmpq(second_reg, Immediate(-1));
3146 __ j(kEqual, slow_path->GetEntryLabel());
3147 // rdx:rax <- sign-extended of rax
3148 __ cqo();
3149 // rax = quotient, rdx = remainder
3150 __ idivq(second_reg);
3151 }
3152 __ Bind(slow_path->GetExitLabel());
3153 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003154}
3155
Calin Juravle7c4954d2014-10-28 16:57:40 +00003156void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3157 LocationSummary* locations =
3158 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3159 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003160 case Primitive::kPrimInt:
3161 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003162 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003163 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003164 locations->SetOut(Location::SameAsFirstInput());
3165 // Intel uses edx:eax as the dividend.
3166 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003167 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3168 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3169 // output and request another temp.
3170 if (div->InputAt(1)->IsConstant()) {
3171 locations->AddTemp(Location::RequiresRegister());
3172 }
Calin Juravled0d48522014-11-04 16:40:20 +00003173 break;
3174 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003175
Calin Juravle7c4954d2014-10-28 16:57:40 +00003176 case Primitive::kPrimFloat:
3177 case Primitive::kPrimDouble: {
3178 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003179 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003180 locations->SetOut(Location::SameAsFirstInput());
3181 break;
3182 }
3183
3184 default:
3185 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3186 }
3187}
3188
3189void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3190 LocationSummary* locations = div->GetLocations();
3191 Location first = locations->InAt(0);
3192 Location second = locations->InAt(1);
3193 DCHECK(first.Equals(locations->Out()));
3194
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003195 Primitive::Type type = div->GetResultType();
3196 switch (type) {
3197 case Primitive::kPrimInt:
3198 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003199 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003200 break;
3201 }
3202
Calin Juravle7c4954d2014-10-28 16:57:40 +00003203 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003204 if (second.IsFpuRegister()) {
3205 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3206 } else if (second.IsConstant()) {
3207 __ divss(first.AsFpuRegister<XmmRegister>(),
3208 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3209 } else {
3210 DCHECK(second.IsStackSlot());
3211 __ divss(first.AsFpuRegister<XmmRegister>(),
3212 Address(CpuRegister(RSP), second.GetStackIndex()));
3213 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003214 break;
3215 }
3216
3217 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003218 if (second.IsFpuRegister()) {
3219 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3220 } else if (second.IsConstant()) {
3221 __ divsd(first.AsFpuRegister<XmmRegister>(),
3222 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3223 } else {
3224 DCHECK(second.IsDoubleStackSlot());
3225 __ divsd(first.AsFpuRegister<XmmRegister>(),
3226 Address(CpuRegister(RSP), second.GetStackIndex()));
3227 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003228 break;
3229 }
3230
3231 default:
3232 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3233 }
3234}
3235
Calin Juravlebacfec32014-11-14 15:54:36 +00003236void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003237 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003238 LocationSummary* locations =
3239 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003240
3241 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003242 case Primitive::kPrimInt:
3243 case Primitive::kPrimLong: {
3244 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003245 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003246 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3247 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003248 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3249 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3250 // output and request another temp.
3251 if (rem->InputAt(1)->IsConstant()) {
3252 locations->AddTemp(Location::RequiresRegister());
3253 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003254 break;
3255 }
3256
3257 case Primitive::kPrimFloat:
3258 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003259 locations->SetInAt(0, Location::Any());
3260 locations->SetInAt(1, Location::Any());
3261 locations->SetOut(Location::RequiresFpuRegister());
3262 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003263 break;
3264 }
3265
3266 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003267 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003268 }
3269}
3270
3271void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3272 Primitive::Type type = rem->GetResultType();
3273 switch (type) {
3274 case Primitive::kPrimInt:
3275 case Primitive::kPrimLong: {
3276 GenerateDivRemIntegral(rem);
3277 break;
3278 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003279 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003280 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003281 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003282 break;
3283 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003284 default:
3285 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3286 }
3287}
3288
Calin Juravled0d48522014-11-04 16:40:20 +00003289void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003290 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3291 ? LocationSummary::kCallOnSlowPath
3292 : LocationSummary::kNoCall;
3293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003294 locations->SetInAt(0, Location::Any());
3295 if (instruction->HasUses()) {
3296 locations->SetOut(Location::SameAsFirstInput());
3297 }
3298}
3299
3300void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3301 SlowPathCodeX86_64* slow_path =
3302 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3303 codegen_->AddSlowPath(slow_path);
3304
3305 LocationSummary* locations = instruction->GetLocations();
3306 Location value = locations->InAt(0);
3307
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003308 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003309 case Primitive::kPrimByte:
3310 case Primitive::kPrimChar:
3311 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003312 case Primitive::kPrimInt: {
3313 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003314 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003315 __ j(kEqual, slow_path->GetEntryLabel());
3316 } else if (value.IsStackSlot()) {
3317 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3318 __ j(kEqual, slow_path->GetEntryLabel());
3319 } else {
3320 DCHECK(value.IsConstant()) << value;
3321 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3322 __ jmp(slow_path->GetEntryLabel());
3323 }
3324 }
3325 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003326 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003327 case Primitive::kPrimLong: {
3328 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003329 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003330 __ j(kEqual, slow_path->GetEntryLabel());
3331 } else if (value.IsDoubleStackSlot()) {
3332 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3333 __ j(kEqual, slow_path->GetEntryLabel());
3334 } else {
3335 DCHECK(value.IsConstant()) << value;
3336 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3337 __ jmp(slow_path->GetEntryLabel());
3338 }
3339 }
3340 break;
3341 }
3342 default:
3343 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003344 }
Calin Juravled0d48522014-11-04 16:40:20 +00003345}
3346
Calin Juravle9aec02f2014-11-18 23:06:35 +00003347void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3348 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3349
3350 LocationSummary* locations =
3351 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3352
3353 switch (op->GetResultType()) {
3354 case Primitive::kPrimInt:
3355 case Primitive::kPrimLong: {
3356 locations->SetInAt(0, Location::RequiresRegister());
3357 // The shift count needs to be in CL.
3358 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3359 locations->SetOut(Location::SameAsFirstInput());
3360 break;
3361 }
3362 default:
3363 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3364 }
3365}
3366
3367void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3368 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3369
3370 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003371 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003372 Location second = locations->InAt(1);
3373
3374 switch (op->GetResultType()) {
3375 case Primitive::kPrimInt: {
3376 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003377 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003378 if (op->IsShl()) {
3379 __ shll(first_reg, second_reg);
3380 } else if (op->IsShr()) {
3381 __ sarl(first_reg, second_reg);
3382 } else {
3383 __ shrl(first_reg, second_reg);
3384 }
3385 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003386 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003387 if (op->IsShl()) {
3388 __ shll(first_reg, imm);
3389 } else if (op->IsShr()) {
3390 __ sarl(first_reg, imm);
3391 } else {
3392 __ shrl(first_reg, imm);
3393 }
3394 }
3395 break;
3396 }
3397 case Primitive::kPrimLong: {
3398 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003399 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003400 if (op->IsShl()) {
3401 __ shlq(first_reg, second_reg);
3402 } else if (op->IsShr()) {
3403 __ sarq(first_reg, second_reg);
3404 } else {
3405 __ shrq(first_reg, second_reg);
3406 }
3407 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003408 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003409 if (op->IsShl()) {
3410 __ shlq(first_reg, imm);
3411 } else if (op->IsShr()) {
3412 __ sarq(first_reg, imm);
3413 } else {
3414 __ shrq(first_reg, imm);
3415 }
3416 }
3417 break;
3418 }
3419 default:
3420 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3421 }
3422}
3423
3424void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3425 HandleShift(shl);
3426}
3427
3428void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3429 HandleShift(shl);
3430}
3431
3432void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3433 HandleShift(shr);
3434}
3435
3436void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3437 HandleShift(shr);
3438}
3439
3440void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3441 HandleShift(ushr);
3442}
3443
3444void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3445 HandleShift(ushr);
3446}
3447
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003448void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003449 LocationSummary* locations =
3450 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003451 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003452 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003453 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003454 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003455}
3456
3457void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3458 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003459 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3460 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003461 // Note: if heap poisoning is enabled, the entry point takes cares
3462 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003463
Calin Juravle175dc732015-08-25 15:42:32 +01003464 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3465 instruction,
3466 instruction->GetDexPc(),
3467 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003468
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003469 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003470}
3471
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003472void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3473 LocationSummary* locations =
3474 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3475 InvokeRuntimeCallingConvention calling_convention;
3476 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003477 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003478 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003479 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003480}
3481
3482void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3483 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003484 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3485 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003486
Roland Levillain4d027112015-07-01 15:41:14 +01003487 // Note: if heap poisoning is enabled, the entry point takes cares
3488 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003489 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3490 instruction,
3491 instruction->GetDexPc(),
3492 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003493
3494 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003495}
3496
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003497void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003498 LocationSummary* locations =
3499 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003500 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3501 if (location.IsStackSlot()) {
3502 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3503 } else if (location.IsDoubleStackSlot()) {
3504 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3505 }
3506 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003507}
3508
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003509void InstructionCodeGeneratorX86_64::VisitParameterValue(
3510 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003511 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003512}
3513
3514void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3515 LocationSummary* locations =
3516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3517 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3518}
3519
3520void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3521 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3522 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003523}
3524
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003525void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003526 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003527 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003528 locations->SetInAt(0, Location::RequiresRegister());
3529 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003530}
3531
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003532void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3533 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003534 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3535 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003536 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003537 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003538 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003539 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003540 break;
3541
3542 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003543 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003544 break;
3545
3546 default:
3547 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3548 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003549}
3550
David Brazdil66d126e2015-04-03 16:02:44 +01003551void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3552 LocationSummary* locations =
3553 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3554 locations->SetInAt(0, Location::RequiresRegister());
3555 locations->SetOut(Location::SameAsFirstInput());
3556}
3557
3558void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003559 LocationSummary* locations = bool_not->GetLocations();
3560 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3561 locations->Out().AsRegister<CpuRegister>().AsRegister());
3562 Location out = locations->Out();
3563 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3564}
3565
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003566void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003567 LocationSummary* locations =
3568 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003569 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3570 locations->SetInAt(i, Location::Any());
3571 }
3572 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003573}
3574
3575void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003576 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003577 LOG(FATAL) << "Unimplemented";
3578}
3579
Calin Juravle52c48962014-12-16 17:02:57 +00003580void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3581 /*
3582 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3583 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3584 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3585 */
3586 switch (kind) {
3587 case MemBarrierKind::kAnyAny: {
3588 __ mfence();
3589 break;
3590 }
3591 case MemBarrierKind::kAnyStore:
3592 case MemBarrierKind::kLoadAny:
3593 case MemBarrierKind::kStoreStore: {
3594 // nop
3595 break;
3596 }
3597 default:
3598 LOG(FATAL) << "Unexpected memory barier " << kind;
3599 }
3600}
3601
3602void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3603 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3604
Nicolas Geoffray39468442014-09-02 15:17:15 +01003605 LocationSummary* locations =
3606 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003607 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003608 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3609 locations->SetOut(Location::RequiresFpuRegister());
3610 } else {
3611 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3612 }
Calin Juravle52c48962014-12-16 17:02:57 +00003613}
3614
3615void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3616 const FieldInfo& field_info) {
3617 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3618
3619 LocationSummary* locations = instruction->GetLocations();
3620 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3621 Location out = locations->Out();
3622 bool is_volatile = field_info.IsVolatile();
3623 Primitive::Type field_type = field_info.GetFieldType();
3624 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3625
3626 switch (field_type) {
3627 case Primitive::kPrimBoolean: {
3628 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3629 break;
3630 }
3631
3632 case Primitive::kPrimByte: {
3633 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3634 break;
3635 }
3636
3637 case Primitive::kPrimShort: {
3638 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3639 break;
3640 }
3641
3642 case Primitive::kPrimChar: {
3643 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3644 break;
3645 }
3646
3647 case Primitive::kPrimInt:
3648 case Primitive::kPrimNot: {
3649 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3650 break;
3651 }
3652
3653 case Primitive::kPrimLong: {
3654 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3655 break;
3656 }
3657
3658 case Primitive::kPrimFloat: {
3659 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3660 break;
3661 }
3662
3663 case Primitive::kPrimDouble: {
3664 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3665 break;
3666 }
3667
3668 case Primitive::kPrimVoid:
3669 LOG(FATAL) << "Unreachable type " << field_type;
3670 UNREACHABLE();
3671 }
3672
Calin Juravle77520bc2015-01-12 18:45:46 +00003673 codegen_->MaybeRecordImplicitNullCheck(instruction);
3674
Calin Juravle52c48962014-12-16 17:02:57 +00003675 if (is_volatile) {
3676 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3677 }
Roland Levillain4d027112015-07-01 15:41:14 +01003678
3679 if (field_type == Primitive::kPrimNot) {
3680 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3681 }
Calin Juravle52c48962014-12-16 17:02:57 +00003682}
3683
3684void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3685 const FieldInfo& field_info) {
3686 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3687
3688 LocationSummary* locations =
3689 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003690 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003691 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003692 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003693
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003694 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003695 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3696 locations->SetInAt(1, Location::RequiresFpuRegister());
3697 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003698 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003699 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003700 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003701 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003702 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003703 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003704 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3705 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003706 locations->AddTemp(Location::RequiresRegister());
3707 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003708}
3709
Calin Juravle52c48962014-12-16 17:02:57 +00003710void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003711 const FieldInfo& field_info,
3712 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003713 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3714
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003715 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003716 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3717 Location value = locations->InAt(1);
3718 bool is_volatile = field_info.IsVolatile();
3719 Primitive::Type field_type = field_info.GetFieldType();
3720 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3721
3722 if (is_volatile) {
3723 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3724 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003725
3726 switch (field_type) {
3727 case Primitive::kPrimBoolean:
3728 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003729 if (value.IsConstant()) {
3730 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3731 __ movb(Address(base, offset), Immediate(v));
3732 } else {
3733 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3734 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003735 break;
3736 }
3737
3738 case Primitive::kPrimShort:
3739 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003740 if (value.IsConstant()) {
3741 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3742 __ movw(Address(base, offset), Immediate(v));
3743 } else {
3744 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3745 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003746 break;
3747 }
3748
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003749 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003750 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003751 if (value.IsConstant()) {
3752 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003753 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3754 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3755 // Note: if heap poisoning is enabled, no need to poison
3756 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003757 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003758 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003759 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3760 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3761 __ movl(temp, value.AsRegister<CpuRegister>());
3762 __ PoisonHeapReference(temp);
3763 __ movl(Address(base, offset), temp);
3764 } else {
3765 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3766 }
Mark Mendell40741f32015-04-20 22:10:34 -04003767 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003768 break;
3769 }
3770
3771 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003772 if (value.IsConstant()) {
3773 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3774 DCHECK(IsInt<32>(v));
3775 int32_t v_32 = v;
3776 __ movq(Address(base, offset), Immediate(v_32));
3777 } else {
3778 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3779 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003780 break;
3781 }
3782
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003783 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003784 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003785 break;
3786 }
3787
3788 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003789 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003790 break;
3791 }
3792
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003793 case Primitive::kPrimVoid:
3794 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003795 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003796 }
Calin Juravle52c48962014-12-16 17:02:57 +00003797
Calin Juravle77520bc2015-01-12 18:45:46 +00003798 codegen_->MaybeRecordImplicitNullCheck(instruction);
3799
3800 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3801 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3802 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003803 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003804 }
3805
Calin Juravle52c48962014-12-16 17:02:57 +00003806 if (is_volatile) {
3807 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3808 }
3809}
3810
3811void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3812 HandleFieldSet(instruction, instruction->GetFieldInfo());
3813}
3814
3815void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003816 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003817}
3818
3819void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003820 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003821}
3822
3823void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003824 HandleFieldGet(instruction, instruction->GetFieldInfo());
3825}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003826
Calin Juravle52c48962014-12-16 17:02:57 +00003827void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3828 HandleFieldGet(instruction);
3829}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003830
Calin Juravle52c48962014-12-16 17:02:57 +00003831void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3832 HandleFieldGet(instruction, instruction->GetFieldInfo());
3833}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003834
Calin Juravle52c48962014-12-16 17:02:57 +00003835void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3836 HandleFieldSet(instruction, instruction->GetFieldInfo());
3837}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003838
Calin Juravle52c48962014-12-16 17:02:57 +00003839void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003840 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003841}
3842
Calin Juravle23a8e352015-09-08 19:56:31 +01003843void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
3844 HUnresolvedInstanceFieldGet* instruction) {
3845 FieldAccessCallingConvetionX86_64 calling_convention;
3846 codegen_->CreateUnresolvedFieldLocationSummary(
3847 instruction, instruction->GetFieldType(), calling_convention);
3848}
3849
3850void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
3851 HUnresolvedInstanceFieldGet* instruction) {
3852 codegen_->GenerateUnresolvedFieldAccess(instruction,
3853 instruction->GetFieldType(),
3854 instruction->GetFieldIndex(),
3855 instruction->GetDexPc());
3856}
3857
3858void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
3859 HUnresolvedInstanceFieldSet* instruction) {
3860 FieldAccessCallingConvetionX86_64 calling_convention;
3861 codegen_->CreateUnresolvedFieldLocationSummary(
3862 instruction, instruction->GetFieldType(), calling_convention);
3863}
3864
3865void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
3866 HUnresolvedInstanceFieldSet* instruction) {
3867 codegen_->GenerateUnresolvedFieldAccess(instruction,
3868 instruction->GetFieldType(),
3869 instruction->GetFieldIndex(),
3870 instruction->GetDexPc());
3871}
3872
3873void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
3874 HUnresolvedStaticFieldGet* instruction) {
3875 FieldAccessCallingConvetionX86_64 calling_convention;
3876 codegen_->CreateUnresolvedFieldLocationSummary(
3877 instruction, instruction->GetFieldType(), calling_convention);
3878}
3879
3880void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
3881 HUnresolvedStaticFieldGet* instruction) {
3882 codegen_->GenerateUnresolvedFieldAccess(instruction,
3883 instruction->GetFieldType(),
3884 instruction->GetFieldIndex(),
3885 instruction->GetDexPc());
3886}
3887
3888void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
3889 HUnresolvedStaticFieldSet* instruction) {
3890 FieldAccessCallingConvetionX86_64 calling_convention;
3891 codegen_->CreateUnresolvedFieldLocationSummary(
3892 instruction, instruction->GetFieldType(), calling_convention);
3893}
3894
3895void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
3896 HUnresolvedStaticFieldSet* instruction) {
3897 codegen_->GenerateUnresolvedFieldAccess(instruction,
3898 instruction->GetFieldType(),
3899 instruction->GetFieldIndex(),
3900 instruction->GetDexPc());
3901}
3902
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003903void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003904 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3905 ? LocationSummary::kCallOnSlowPath
3906 : LocationSummary::kNoCall;
3907 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3908 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003909 ? Location::RequiresRegister()
3910 : Location::Any();
3911 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003912 if (instruction->HasUses()) {
3913 locations->SetOut(Location::SameAsFirstInput());
3914 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003915}
3916
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003917void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003918 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3919 return;
3920 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003921 LocationSummary* locations = instruction->GetLocations();
3922 Location obj = locations->InAt(0);
3923
3924 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3925 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3926}
3927
3928void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003929 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003930 codegen_->AddSlowPath(slow_path);
3931
3932 LocationSummary* locations = instruction->GetLocations();
3933 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003934
3935 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003936 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003937 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003938 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003939 } else {
3940 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00003941 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003942 __ jmp(slow_path->GetEntryLabel());
3943 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003944 }
3945 __ j(kEqual, slow_path->GetEntryLabel());
3946}
3947
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003948void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003949 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003950 GenerateImplicitNullCheck(instruction);
3951 } else {
3952 GenerateExplicitNullCheck(instruction);
3953 }
3954}
3955
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003956void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003957 LocationSummary* locations =
3958 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003959 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003960 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003961 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3962 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3963 } else {
3964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3965 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003966}
3967
3968void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3969 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003970 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003971 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003972 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003973
Roland Levillain4d027112015-07-01 15:41:14 +01003974 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003975 case Primitive::kPrimBoolean: {
3976 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003977 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003978 if (index.IsConstant()) {
3979 __ movzxb(out, Address(obj,
3980 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3981 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003982 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003983 }
3984 break;
3985 }
3986
3987 case Primitive::kPrimByte: {
3988 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003989 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003990 if (index.IsConstant()) {
3991 __ movsxb(out, Address(obj,
3992 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3993 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003994 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003995 }
3996 break;
3997 }
3998
3999 case Primitive::kPrimShort: {
4000 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004001 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004002 if (index.IsConstant()) {
4003 __ movsxw(out, Address(obj,
4004 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4005 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004006 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004007 }
4008 break;
4009 }
4010
4011 case Primitive::kPrimChar: {
4012 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004013 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004014 if (index.IsConstant()) {
4015 __ movzxw(out, Address(obj,
4016 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4017 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004018 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004019 }
4020 break;
4021 }
4022
4023 case Primitive::kPrimInt:
4024 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01004025 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4026 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004027 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004028 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004029 if (index.IsConstant()) {
4030 __ movl(out, Address(obj,
4031 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4032 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004033 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004034 }
4035 break;
4036 }
4037
4038 case Primitive::kPrimLong: {
4039 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004040 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004041 if (index.IsConstant()) {
4042 __ movq(out, Address(obj,
4043 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4044 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004045 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004046 }
4047 break;
4048 }
4049
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004050 case Primitive::kPrimFloat: {
4051 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004052 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004053 if (index.IsConstant()) {
4054 __ movss(out, Address(obj,
4055 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4056 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004057 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004058 }
4059 break;
4060 }
4061
4062 case Primitive::kPrimDouble: {
4063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004064 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004065 if (index.IsConstant()) {
4066 __ movsd(out, Address(obj,
4067 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4068 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004069 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004070 }
4071 break;
4072 }
4073
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004074 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004075 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004076 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004077 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004078 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004079
4080 if (type == Primitive::kPrimNot) {
4081 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4082 __ MaybeUnpoisonHeapReference(out);
4083 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004084}
4085
4086void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004087 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004088
4089 bool needs_write_barrier =
4090 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4091 bool needs_runtime_call = instruction->NeedsTypeCheck();
4092
Nicolas Geoffray39468442014-09-02 15:17:15 +01004093 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004094 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
4095 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004096 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004097 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4098 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4099 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004100 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004101 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004102 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004103 1, Location::RegisterOrConstant(instruction->InputAt(1)));
4104 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004105 if (value_type == Primitive::kPrimLong) {
Mark Mendell40741f32015-04-20 22:10:34 -04004106 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004107 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
4108 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004109 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004110 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004111 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004112
4113 if (needs_write_barrier) {
4114 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004115 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004116 locations->AddTemp(Location::RequiresRegister());
4117 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004118 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004119}
4120
4121void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4122 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004123 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004124 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004125 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004126 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004127 bool needs_runtime_call = locations->WillCall();
4128 bool needs_write_barrier =
4129 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004130
4131 switch (value_type) {
4132 case Primitive::kPrimBoolean:
4133 case Primitive::kPrimByte: {
4134 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004135 if (index.IsConstant()) {
4136 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004137 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004139 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00004140 __ movb(Address(obj, offset),
4141 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004142 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004143 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004144 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004145 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
4146 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004147 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004148 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004149 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4150 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004151 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004152 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004153 break;
4154 }
4155
4156 case Primitive::kPrimShort:
4157 case Primitive::kPrimChar: {
4158 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004159 if (index.IsConstant()) {
4160 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004161 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004162 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004163 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004164 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00004165 __ movw(Address(obj, offset),
4166 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004167 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004168 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004169 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004170 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004171 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
4172 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004173 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004174 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00004175 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004176 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4177 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004178 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004179 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004180 break;
4181 }
4182
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004183 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004184 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004185 if (!needs_runtime_call) {
4186 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4187 if (index.IsConstant()) {
4188 size_t offset =
4189 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4190 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004191 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4192 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4193 __ movl(temp, value.AsRegister<CpuRegister>());
4194 __ PoisonHeapReference(temp);
4195 __ movl(Address(obj, offset), temp);
4196 } else {
4197 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
4198 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004199 } else {
4200 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004201 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004202 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4203 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4204 // Note: if heap poisoning is enabled, no need to poison
4205 // (negate) `v` if it is a reference, as it would be null.
Mark Mendell40741f32015-04-20 22:10:34 -04004206 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004207 }
4208 } else {
4209 DCHECK(index.IsRegister()) << index;
4210 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004211 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4212 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4213 __ movl(temp, value.AsRegister<CpuRegister>());
4214 __ PoisonHeapReference(temp);
4215 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset), temp);
4216 } else {
4217 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4218 value.AsRegister<CpuRegister>());
4219 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004220 } else {
4221 DCHECK(value.IsConstant()) << value;
Mark Mendell40741f32015-04-20 22:10:34 -04004222 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004223 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4224 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4225 // Note: if heap poisoning is enabled, no need to poison
4226 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004227 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Mark Mendell40741f32015-04-20 22:10:34 -04004228 Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004229 }
4230 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004231 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004232 if (needs_write_barrier) {
4233 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004234 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4235 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004236 codegen_->MarkGCCard(
4237 temp, card, obj, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004238 }
4239 } else {
4240 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01004241 // Note: if heap poisoning is enabled, pAputObject takes cares
4242 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004243 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4244 instruction,
4245 instruction->GetDexPc(),
4246 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004247 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004248 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004249 break;
4250 }
4251
4252 case Primitive::kPrimLong: {
4253 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004254 if (index.IsConstant()) {
4255 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Mark Mendell40741f32015-04-20 22:10:34 -04004256 if (value.IsRegister()) {
4257 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
4258 } else {
4259 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4260 DCHECK(IsInt<32>(v));
4261 int32_t v_32 = v;
4262 __ movq(Address(obj, offset), Immediate(v_32));
4263 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004264 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04004265 if (value.IsRegister()) {
4266 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4267 value.AsRegister<CpuRegister>());
4268 } else {
4269 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4270 DCHECK(IsInt<32>(v));
4271 int32_t v_32 = v;
4272 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4273 Immediate(v_32));
4274 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004275 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004276 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004277 break;
4278 }
4279
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004280 case Primitive::kPrimFloat: {
4281 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4282 if (index.IsConstant()) {
4283 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4284 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004285 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004286 } else {
4287 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004288 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
4289 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004290 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004291 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004292 break;
4293 }
4294
4295 case Primitive::kPrimDouble: {
4296 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4297 if (index.IsConstant()) {
4298 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4299 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004300 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004301 } else {
4302 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004303 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
4304 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004305 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004306 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004307 break;
4308 }
4309
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004310 case Primitive::kPrimVoid:
4311 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004312 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004313 }
4314}
4315
4316void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004317 LocationSummary* locations =
4318 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004319 locations->SetInAt(0, Location::RequiresRegister());
4320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004321}
4322
4323void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4324 LocationSummary* locations = instruction->GetLocations();
4325 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004326 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4327 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004328 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004329 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004330}
4331
4332void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004333 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4334 ? LocationSummary::kCallOnSlowPath
4335 : LocationSummary::kNoCall;
4336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004337 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004338 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004339 if (instruction->HasUses()) {
4340 locations->SetOut(Location::SameAsFirstInput());
4341 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004342}
4343
4344void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4345 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004346 Location index_loc = locations->InAt(0);
4347 Location length_loc = locations->InAt(1);
4348 SlowPathCodeX86_64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004349 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004350
Mark Mendell99dbd682015-04-22 16:18:52 -04004351 if (length_loc.IsConstant()) {
4352 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4353 if (index_loc.IsConstant()) {
4354 // BCE will remove the bounds check if we are guarenteed to pass.
4355 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4356 if (index < 0 || index >= length) {
4357 codegen_->AddSlowPath(slow_path);
4358 __ jmp(slow_path->GetEntryLabel());
4359 } else {
4360 // Some optimization after BCE may have generated this, and we should not
4361 // generate a bounds check if it is a valid range.
4362 }
4363 return;
4364 }
4365
4366 // We have to reverse the jump condition because the length is the constant.
4367 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4368 __ cmpl(index_reg, Immediate(length));
4369 codegen_->AddSlowPath(slow_path);
4370 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004371 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004372 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4373 if (index_loc.IsConstant()) {
4374 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4375 __ cmpl(length, Immediate(value));
4376 } else {
4377 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4378 }
4379 codegen_->AddSlowPath(slow_path);
4380 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004381 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004382}
4383
4384void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4385 CpuRegister card,
4386 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004387 CpuRegister value,
4388 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004389 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004390 if (value_can_be_null) {
4391 __ testl(value, value);
4392 __ j(kEqual, &is_null);
4393 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004394 __ gs()->movq(card, Address::Absolute(
4395 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4396 __ movq(temp, object);
4397 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004398 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004399 if (value_can_be_null) {
4400 __ Bind(&is_null);
4401 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004402}
4403
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004404void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4405 temp->SetLocations(nullptr);
4406}
4407
4408void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4409 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004410 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004411}
4412
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004413void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004414 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004415 LOG(FATAL) << "Unimplemented";
4416}
4417
4418void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004419 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4420}
4421
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004422void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4423 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4424}
4425
4426void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004427 HBasicBlock* block = instruction->GetBlock();
4428 if (block->GetLoopInformation() != nullptr) {
4429 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4430 // The back edge will generate the suspend check.
4431 return;
4432 }
4433 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4434 // The goto will generate the suspend check.
4435 return;
4436 }
4437 GenerateSuspendCheck(instruction, nullptr);
4438}
4439
4440void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4441 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004442 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004443 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4444 if (slow_path == nullptr) {
4445 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4446 instruction->SetSlowPath(slow_path);
4447 codegen_->AddSlowPath(slow_path);
4448 if (successor != nullptr) {
4449 DCHECK(successor->IsLoopHeader());
4450 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4451 }
4452 } else {
4453 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4454 }
4455
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004456 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004457 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004458 if (successor == nullptr) {
4459 __ j(kNotEqual, slow_path->GetEntryLabel());
4460 __ Bind(slow_path->GetReturnLabel());
4461 } else {
4462 __ j(kEqual, codegen_->GetLabelOf(successor));
4463 __ jmp(slow_path->GetEntryLabel());
4464 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004465}
4466
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004467X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4468 return codegen_->GetAssembler();
4469}
4470
4471void ParallelMoveResolverX86_64::EmitMove(size_t index) {
4472 MoveOperands* move = moves_.Get(index);
4473 Location source = move->GetSource();
4474 Location destination = move->GetDestination();
4475
4476 if (source.IsRegister()) {
4477 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004478 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004479 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004480 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004481 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004482 } else {
4483 DCHECK(destination.IsDoubleStackSlot());
4484 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004485 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004486 }
4487 } else if (source.IsStackSlot()) {
4488 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004489 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004490 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004491 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004492 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004493 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004494 } else {
4495 DCHECK(destination.IsStackSlot());
4496 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4497 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4498 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004499 } else if (source.IsDoubleStackSlot()) {
4500 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004501 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004502 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004503 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004504 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4505 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004506 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004507 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004508 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4509 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4510 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004511 } else if (source.IsConstant()) {
4512 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004513 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4514 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004515 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004516 if (value == 0) {
4517 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4518 } else {
4519 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4520 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004521 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004522 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004523 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004524 }
4525 } else if (constant->IsLongConstant()) {
4526 int64_t value = constant->AsLongConstant()->GetValue();
4527 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004528 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004529 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004530 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004531 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004532 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004533 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004534 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004535 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004536 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004537 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4538 if (value == 0) {
4539 // easy FP 0.0.
4540 __ xorps(dest, dest);
4541 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004542 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004543 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004544 } else {
4545 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004546 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004547 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4548 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004549 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004550 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004551 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004552 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004553 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004554 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4555 if (value == 0) {
4556 __ xorpd(dest, dest);
4557 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004558 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004559 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004560 } else {
4561 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004562 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004563 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004564 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004565 } else if (source.IsFpuRegister()) {
4566 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004567 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004568 } else if (destination.IsStackSlot()) {
4569 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004570 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004571 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004572 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004573 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004574 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004575 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004576 }
4577}
4578
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004579void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004580 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004581 __ movl(Address(CpuRegister(RSP), mem), reg);
4582 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004583}
4584
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004585void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004586 ScratchRegisterScope ensure_scratch(
4587 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4588
4589 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4590 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4591 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4592 Address(CpuRegister(RSP), mem2 + stack_offset));
4593 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4594 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4595 CpuRegister(ensure_scratch.GetRegister()));
4596}
4597
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004598void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4599 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4600 __ movq(Address(CpuRegister(RSP), mem), reg);
4601 __ movq(reg, CpuRegister(TMP));
4602}
4603
4604void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4605 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004606 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004607
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004608 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4609 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4610 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4611 Address(CpuRegister(RSP), mem2 + stack_offset));
4612 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4613 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4614 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004615}
4616
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004617void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4618 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4619 __ movss(Address(CpuRegister(RSP), mem), reg);
4620 __ movd(reg, CpuRegister(TMP));
4621}
4622
4623void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4624 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4625 __ movsd(Address(CpuRegister(RSP), mem), reg);
4626 __ movd(reg, CpuRegister(TMP));
4627}
4628
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004629void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
4630 MoveOperands* move = moves_.Get(index);
4631 Location source = move->GetSource();
4632 Location destination = move->GetDestination();
4633
4634 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004635 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004636 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004637 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004638 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004639 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004640 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004641 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4642 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004643 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004644 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004645 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004646 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4647 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004648 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004649 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4650 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4651 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004652 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004653 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004654 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004655 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004656 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004657 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004658 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004659 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004660 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004661 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004662 }
4663}
4664
4665
4666void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4667 __ pushq(CpuRegister(reg));
4668}
4669
4670
4671void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4672 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004673}
4674
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004675void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
4676 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
4677 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4678 Immediate(mirror::Class::kStatusInitialized));
4679 __ j(kLess, slow_path->GetEntryLabel());
4680 __ Bind(slow_path->GetExitLabel());
4681 // No need for memory fence, thanks to the X86_64 memory model.
4682}
4683
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004684void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004685 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4686 ? LocationSummary::kCallOnSlowPath
4687 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004688 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004689 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004690 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004691 locations->SetOut(Location::RequiresRegister());
4692}
4693
4694void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004695 LocationSummary* locations = cls->GetLocations();
4696 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4697 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004698 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004699 DCHECK(!cls->CanCallRuntime());
4700 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004701 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004702 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004703 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004704 __ movq(out, Address(
4705 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004706 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004707 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004708
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004709 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4710 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4711 codegen_->AddSlowPath(slow_path);
4712 __ testl(out, out);
4713 __ j(kEqual, slow_path->GetEntryLabel());
4714 if (cls->MustGenerateClinitCheck()) {
4715 GenerateClassInitializationCheck(slow_path, out);
4716 } else {
4717 __ Bind(slow_path->GetExitLabel());
4718 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004719 }
4720}
4721
4722void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4723 LocationSummary* locations =
4724 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4725 locations->SetInAt(0, Location::RequiresRegister());
4726 if (check->HasUses()) {
4727 locations->SetOut(Location::SameAsFirstInput());
4728 }
4729}
4730
4731void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004732 // We assume the class to not be null.
4733 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
4734 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004735 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004736 GenerateClassInitializationCheck(slow_path,
4737 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004738}
4739
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004740void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4741 LocationSummary* locations =
4742 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004743 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004744 locations->SetOut(Location::RequiresRegister());
4745}
4746
4747void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
4748 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
4749 codegen_->AddSlowPath(slow_path);
4750
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004751 LocationSummary* locations = load->GetLocations();
4752 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4753 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004754 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004755 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004756 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004757 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004758 __ testl(out, out);
4759 __ j(kEqual, slow_path->GetEntryLabel());
4760 __ Bind(slow_path->GetExitLabel());
4761}
4762
David Brazdilcb1c0552015-08-04 16:22:25 +01004763static Address GetExceptionTlsAddress() {
4764 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4765}
4766
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004767void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4768 LocationSummary* locations =
4769 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4770 locations->SetOut(Location::RequiresRegister());
4771}
4772
4773void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004774 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4775}
4776
4777void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4778 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4779}
4780
4781void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4782 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004783}
4784
4785void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4786 LocationSummary* locations =
4787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4788 InvokeRuntimeCallingConvention calling_convention;
4789 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4790}
4791
4792void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004793 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4794 instruction,
4795 instruction->GetDexPc(),
4796 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004797}
4798
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004799void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004800 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4801 switch (instruction->GetTypeCheckKind()) {
4802 case TypeCheckKind::kExactCheck:
4803 case TypeCheckKind::kAbstractClassCheck:
4804 case TypeCheckKind::kClassHierarchyCheck:
4805 case TypeCheckKind::kArrayObjectCheck:
4806 call_kind = LocationSummary::kNoCall;
4807 break;
4808 case TypeCheckKind::kInterfaceCheck:
4809 call_kind = LocationSummary::kCall;
4810 break;
4811 case TypeCheckKind::kArrayCheck:
4812 call_kind = LocationSummary::kCallOnSlowPath;
4813 break;
4814 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004816 if (call_kind != LocationSummary::kCall) {
4817 locations->SetInAt(0, Location::RequiresRegister());
4818 locations->SetInAt(1, Location::Any());
4819 // Note that TypeCheckSlowPathX86_64 uses this register too.
4820 locations->SetOut(Location::RequiresRegister());
4821 } else {
4822 InvokeRuntimeCallingConvention calling_convention;
4823 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4824 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4825 locations->SetOut(Location::RegisterLocation(RAX));
4826 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004827}
4828
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004829void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004830 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004831 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004832 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004833 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004834 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004835 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4836 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4837 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004838 SlowPathCodeX86_64* slow_path = nullptr;
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004839 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004840
4841 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004842 // Avoid null check if we know obj is not null.
4843 if (instruction->MustDoNullCheck()) {
4844 __ testl(obj, obj);
4845 __ j(kEqual, &zero);
4846 }
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004847
4848 // In case of an interface check, we put the object class into the object register.
4849 // This is safe, as the register is caller-save, and the object must be in another
4850 // register if it survives the runtime call.
4851 CpuRegister target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
4852 ? obj
4853 : out;
4854 __ movl(target, Address(obj, class_offset));
4855 __ MaybeUnpoisonHeapReference(target);
4856
4857 switch (instruction->GetTypeCheckKind()) {
4858 case TypeCheckKind::kExactCheck: {
4859 if (cls.IsRegister()) {
4860 __ cmpl(out, cls.AsRegister<CpuRegister>());
4861 } else {
4862 DCHECK(cls.IsStackSlot()) << cls;
4863 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4864 }
4865 // Classes must be equal for the instanceof to succeed.
4866 __ j(kNotEqual, &zero);
4867 __ movl(out, Immediate(1));
4868 __ jmp(&done);
4869 break;
4870 }
4871 case TypeCheckKind::kAbstractClassCheck: {
4872 // If the class is abstract, we eagerly fetch the super class of the
4873 // object to avoid doing a comparison we know will fail.
4874 NearLabel loop, success;
4875 __ Bind(&loop);
4876 __ movl(out, Address(out, super_offset));
4877 __ MaybeUnpoisonHeapReference(out);
4878 __ testl(out, out);
4879 // If `out` is null, we use it for the result, and jump to `done`.
4880 __ j(kEqual, &done);
4881 if (cls.IsRegister()) {
4882 __ cmpl(out, cls.AsRegister<CpuRegister>());
4883 } else {
4884 DCHECK(cls.IsStackSlot()) << cls;
4885 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4886 }
4887 __ j(kNotEqual, &loop);
4888 __ movl(out, Immediate(1));
4889 if (zero.IsLinked()) {
4890 __ jmp(&done);
4891 }
4892 break;
4893 }
4894 case TypeCheckKind::kClassHierarchyCheck: {
4895 // Walk over the class hierarchy to find a match.
4896 NearLabel loop, success;
4897 __ Bind(&loop);
4898 if (cls.IsRegister()) {
4899 __ cmpl(out, cls.AsRegister<CpuRegister>());
4900 } else {
4901 DCHECK(cls.IsStackSlot()) << cls;
4902 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4903 }
4904 __ j(kEqual, &success);
4905 __ movl(out, Address(out, super_offset));
4906 __ MaybeUnpoisonHeapReference(out);
4907 __ testl(out, out);
4908 __ j(kNotEqual, &loop);
4909 // If `out` is null, we use it for the result, and jump to `done`.
4910 __ jmp(&done);
4911 __ Bind(&success);
4912 __ movl(out, Immediate(1));
4913 if (zero.IsLinked()) {
4914 __ jmp(&done);
4915 }
4916 break;
4917 }
4918 case TypeCheckKind::kArrayObjectCheck: {
4919 // Just need to check that the object's class is a non primitive array.
4920 __ movl(out, Address(out, component_offset));
4921 __ MaybeUnpoisonHeapReference(out);
4922 __ testl(out, out);
4923 // If `out` is null, we use it for the result, and jump to `done`.
4924 __ j(kEqual, &done);
4925 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
4926 __ j(kNotEqual, &zero);
4927 __ movl(out, Immediate(1));
4928 __ jmp(&done);
4929 break;
4930 }
4931 case TypeCheckKind::kArrayCheck: {
4932 if (cls.IsRegister()) {
4933 __ cmpl(out, cls.AsRegister<CpuRegister>());
4934 } else {
4935 DCHECK(cls.IsStackSlot()) << cls;
4936 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4937 }
4938 DCHECK(locations->OnlyCallsOnSlowPath());
4939 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4940 instruction, /* is_fatal */ false);
4941 codegen_->AddSlowPath(slow_path);
4942 __ j(kNotEqual, slow_path->GetEntryLabel());
4943 __ movl(out, Immediate(1));
4944 if (zero.IsLinked()) {
4945 __ jmp(&done);
4946 }
4947 break;
4948 }
4949
4950 case TypeCheckKind::kInterfaceCheck:
4951 default: {
4952 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4953 instruction,
4954 instruction->GetDexPc(),
4955 nullptr);
4956 if (zero.IsLinked()) {
4957 __ jmp(&done);
4958 }
4959 break;
4960 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004961 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004962
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004963 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004964 __ Bind(&zero);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004965 __ xorl(out, out);
4966 }
4967
4968 if (done.IsLinked()) {
4969 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004970 }
4971
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004972 if (slow_path != nullptr) {
4973 __ Bind(slow_path->GetExitLabel());
4974 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004975}
4976
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004977void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004978 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4979 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4980
4981 switch (instruction->GetTypeCheckKind()) {
4982 case TypeCheckKind::kExactCheck:
4983 case TypeCheckKind::kAbstractClassCheck:
4984 case TypeCheckKind::kClassHierarchyCheck:
4985 case TypeCheckKind::kArrayObjectCheck:
4986 call_kind = throws_into_catch
4987 ? LocationSummary::kCallOnSlowPath
4988 : LocationSummary::kNoCall;
4989 break;
4990 case TypeCheckKind::kInterfaceCheck:
4991 call_kind = LocationSummary::kCall;
4992 break;
4993 case TypeCheckKind::kArrayCheck:
4994 call_kind = LocationSummary::kCallOnSlowPath;
4995 break;
4996 }
4997
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004998 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004999 instruction, call_kind);
5000 if (call_kind != LocationSummary::kCall) {
5001 locations->SetInAt(0, Location::RequiresRegister());
5002 locations->SetInAt(1, Location::Any());
5003 // Note that TypeCheckSlowPathX86_64 uses this register too.
5004 locations->AddTemp(Location::RequiresRegister());
5005 } else {
5006 InvokeRuntimeCallingConvention calling_convention;
5007 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5008 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5009 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005010}
5011
5012void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
5013 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005014 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005015 Location cls = locations->InAt(1);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005016 CpuRegister temp = locations->WillCall()
5017 ? CpuRegister(kNoRegister)
5018 : locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005019
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005020 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5021 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5022 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5023 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5024 SlowPathCodeX86_64* slow_path = nullptr;
5025
5026 if (!locations->WillCall()) {
5027 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
5028 instruction, !locations->CanCall());
5029 codegen_->AddSlowPath(slow_path);
5030 }
5031
5032 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005033 // Avoid null check if we know obj is not null.
5034 if (instruction->MustDoNullCheck()) {
5035 __ testl(obj, obj);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005036 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005037 }
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005038
5039 if (locations->WillCall()) {
5040 __ movl(obj, Address(obj, class_offset));
5041 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005042 } else {
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005043 __ movl(temp, Address(obj, class_offset));
5044 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005045 }
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005046
5047 switch (instruction->GetTypeCheckKind()) {
5048 case TypeCheckKind::kExactCheck:
5049 case TypeCheckKind::kArrayCheck: {
5050 if (cls.IsRegister()) {
5051 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5052 } else {
5053 DCHECK(cls.IsStackSlot()) << cls;
5054 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5055 }
5056 // Jump to slow path for throwing the exception or doing a
5057 // more involved array check.
5058 __ j(kNotEqual, slow_path->GetEntryLabel());
5059 break;
5060 }
5061 case TypeCheckKind::kAbstractClassCheck: {
5062 // If the class is abstract, we eagerly fetch the super class of the
5063 // object to avoid doing a comparison we know will fail.
5064 NearLabel loop;
5065 __ Bind(&loop);
5066 __ movl(temp, Address(temp, super_offset));
5067 __ MaybeUnpoisonHeapReference(temp);
5068 __ testl(temp, temp);
5069 // Jump to the slow path to throw the exception.
5070 __ j(kEqual, slow_path->GetEntryLabel());
5071 if (cls.IsRegister()) {
5072 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5073 } else {
5074 DCHECK(cls.IsStackSlot()) << cls;
5075 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5076 }
5077 __ j(kNotEqual, &loop);
5078 break;
5079 }
5080 case TypeCheckKind::kClassHierarchyCheck: {
5081 // Walk over the class hierarchy to find a match.
5082 NearLabel loop, success;
5083 __ Bind(&loop);
5084 if (cls.IsRegister()) {
5085 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5086 } else {
5087 DCHECK(cls.IsStackSlot()) << cls;
5088 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5089 }
5090 __ j(kEqual, &success);
5091 __ movl(temp, Address(temp, super_offset));
5092 __ MaybeUnpoisonHeapReference(temp);
5093 __ testl(temp, temp);
5094 __ j(kNotEqual, &loop);
5095 // Jump to the slow path to throw the exception.
5096 __ jmp(slow_path->GetEntryLabel());
5097 __ Bind(&success);
5098 break;
5099 }
5100 case TypeCheckKind::kArrayObjectCheck: {
5101 // Just need to check that the object's class is a non primitive array.
5102 __ movl(temp, Address(temp, component_offset));
5103 __ MaybeUnpoisonHeapReference(temp);
5104 __ testl(temp, temp);
5105 __ j(kEqual, slow_path->GetEntryLabel());
5106 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5107 __ j(kNotEqual, slow_path->GetEntryLabel());
5108 break;
5109 }
5110 case TypeCheckKind::kInterfaceCheck:
5111 default:
5112 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5113 instruction,
5114 instruction->GetDexPc(),
5115 nullptr);
5116 break;
5117 }
5118 __ Bind(&done);
5119
5120 if (slow_path != nullptr) {
5121 __ Bind(slow_path->GetExitLabel());
5122 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005123}
5124
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005125void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5126 LocationSummary* locations =
5127 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5128 InvokeRuntimeCallingConvention calling_convention;
5129 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5130}
5131
5132void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005133 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5134 : QUICK_ENTRY_POINT(pUnlockObject),
5135 instruction,
5136 instruction->GetDexPc(),
5137 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005138}
5139
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005140void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5141void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5142void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5143
5144void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5145 LocationSummary* locations =
5146 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5147 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5148 || instruction->GetResultType() == Primitive::kPrimLong);
5149 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005150 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005151 locations->SetOut(Location::SameAsFirstInput());
5152}
5153
5154void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5155 HandleBitwiseOperation(instruction);
5156}
5157
5158void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5159 HandleBitwiseOperation(instruction);
5160}
5161
5162void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5163 HandleBitwiseOperation(instruction);
5164}
5165
5166void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5167 LocationSummary* locations = instruction->GetLocations();
5168 Location first = locations->InAt(0);
5169 Location second = locations->InAt(1);
5170 DCHECK(first.Equals(locations->Out()));
5171
5172 if (instruction->GetResultType() == Primitive::kPrimInt) {
5173 if (second.IsRegister()) {
5174 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005175 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005176 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005177 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005178 } else {
5179 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005180 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005181 }
5182 } else if (second.IsConstant()) {
5183 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5184 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005185 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005186 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005187 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005188 } else {
5189 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005190 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005191 }
5192 } else {
5193 Address address(CpuRegister(RSP), second.GetStackIndex());
5194 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005195 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005196 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005197 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005198 } else {
5199 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005200 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005201 }
5202 }
5203 } else {
5204 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005205 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5206 bool second_is_constant = false;
5207 int64_t value = 0;
5208 if (second.IsConstant()) {
5209 second_is_constant = true;
5210 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005211 }
Mark Mendell40741f32015-04-20 22:10:34 -04005212 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005213
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005214 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005215 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005216 if (is_int32_value) {
5217 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5218 } else {
5219 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5220 }
5221 } else if (second.IsDoubleStackSlot()) {
5222 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005223 } else {
5224 __ andq(first_reg, second.AsRegister<CpuRegister>());
5225 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005226 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005227 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005228 if (is_int32_value) {
5229 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5230 } else {
5231 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5232 }
5233 } else if (second.IsDoubleStackSlot()) {
5234 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005235 } else {
5236 __ orq(first_reg, second.AsRegister<CpuRegister>());
5237 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005238 } else {
5239 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005240 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005241 if (is_int32_value) {
5242 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5243 } else {
5244 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5245 }
5246 } else if (second.IsDoubleStackSlot()) {
5247 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005248 } else {
5249 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5250 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005251 }
5252 }
5253}
5254
Calin Juravleb1498f62015-02-16 13:13:29 +00005255void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
5256 // Nothing to do, this should be removed during prepare for register allocator.
5257 UNUSED(instruction);
5258 LOG(FATAL) << "Unreachable";
5259}
5260
5261void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
5262 // Nothing to do, this should be removed during prepare for register allocator.
5263 UNUSED(instruction);
5264 LOG(FATAL) << "Unreachable";
5265}
5266
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005267void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
5268 DCHECK(codegen_->IsBaseline());
5269 LocationSummary* locations =
5270 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5271 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5272}
5273
5274void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5275 DCHECK(codegen_->IsBaseline());
5276 // Will be generated at use site.
5277}
5278
Mark Mendell92e83bf2015-05-07 11:25:03 -04005279void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
5280 if (value == 0) {
5281 __ xorl(dest, dest);
5282 } else if (value > 0 && IsInt<32>(value)) {
5283 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
5284 __ movl(dest, Immediate(static_cast<int32_t>(value)));
5285 } else {
5286 __ movq(dest, Immediate(value));
5287 }
5288}
5289
Mark Mendellcfa410b2015-05-25 16:02:44 -04005290void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
5291 DCHECK(dest.IsDoubleStackSlot());
5292 if (IsInt<32>(value)) {
5293 // Can move directly as an int32 constant.
5294 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
5295 Immediate(static_cast<int32_t>(value)));
5296 } else {
5297 Load64BitValue(CpuRegister(TMP), value);
5298 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
5299 }
5300}
5301
Mark Mendellf55c3e02015-03-26 21:07:46 -04005302void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
5303 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04005304 X86_64Assembler* assembler = GetAssembler();
5305 if (!assembler->IsConstantAreaEmpty()) {
Mark Mendellf55c3e02015-03-26 21:07:46 -04005306 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5307 // byte values. If used for vectors at a later time, this will need to be
5308 // updated to 16 bytes with the appropriate offset.
Mark Mendell39dcf552015-04-09 20:42:42 -04005309 assembler->Align(4, 0);
5310 constant_area_start_ = assembler->CodeSize();
5311 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04005312 }
5313
5314 // And finish up.
5315 CodeGenerator::Finalize(allocator);
5316}
5317
5318/**
5319 * Class to handle late fixup of offsets into constant area.
5320 */
5321class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5322 public:
Mark Mendell39dcf552015-04-09 20:42:42 -04005323 RIPFixup(const CodeGeneratorX86_64& codegen, int offset)
Mark Mendellf55c3e02015-03-26 21:07:46 -04005324 : codegen_(codegen), offset_into_constant_area_(offset) {}
5325
5326 private:
5327 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5328 // Patch the correct offset for the instruction. We use the address of the
5329 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
5330 int constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5331 int relative_position = constant_offset - pos;
5332
5333 // Patch in the right value.
5334 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5335 }
5336
Mark Mendell39dcf552015-04-09 20:42:42 -04005337 const CodeGeneratorX86_64& codegen_;
Mark Mendellf55c3e02015-03-26 21:07:46 -04005338
5339 // Location in constant area that the fixup refers to.
5340 int offset_into_constant_area_;
5341};
5342
5343Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
5344 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5345 return Address::RIP(fixup);
5346}
5347
5348Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
5349 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5350 return Address::RIP(fixup);
5351}
5352
5353Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
5354 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5355 return Address::RIP(fixup);
5356}
5357
5358Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
5359 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5360 return Address::RIP(fixup);
5361}
5362
Roland Levillain4d027112015-07-01 15:41:14 +01005363#undef __
5364
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005365} // namespace x86_64
5366} // namespace art