blob: f0d9420f87eeacc38f5a337a15931bc14e331fea [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 +010039static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010040static constexpr Register kMethodRegisterArgument = RDI;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010041
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000042static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000043static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Mark Mendell24f2dfa2015-01-14 19:51:45 -050045static constexpr int kC2ConditionMask = 0x400;
46
Roland Levillain62a46b22015-06-01 18:24:13 +010047#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010048#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049
Andreas Gampe85b62f22015-09-09 13:15:38 -070050class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010052 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Alexandre Rames2ed20af2015-03-06 13:55:35 +000054 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010055 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000057 if (instruction_->CanThrowIntoCatchBlock()) {
58 // Live registers will be restored in the catch block if caught.
59 SaveLiveRegisters(codegen, instruction_->GetLocations());
60 }
Alexandre Rames8158f282015-08-07 10:26:17 +010061 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
62 instruction_,
63 instruction_->GetDexPc(),
64 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065 }
66
Alexandre Rames8158f282015-08-07 10:26:17 +010067 bool IsFatal() const OVERRIDE { return true; }
68
Alexandre Rames9931f312015-06-19 14:47:01 +010069 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
70
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010072 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
74};
75
Andreas Gampe85b62f22015-09-09 13:15:38 -070076class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000077 public:
78 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
79
Alexandre Rames2ed20af2015-03-06 13:55:35 +000080 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010081 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000082 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000083 if (instruction_->CanThrowIntoCatchBlock()) {
84 // Live registers will be restored in the catch block if caught.
85 SaveLiveRegisters(codegen, instruction_->GetLocations());
86 }
Alexandre Rames8158f282015-08-07 10:26:17 +010087 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
88 instruction_,
89 instruction_->GetDexPc(),
90 this);
Calin Juravled0d48522014-11-04 16:40:20 +000091 }
92
Alexandre Rames8158f282015-08-07 10:26:17 +010093 bool IsFatal() const OVERRIDE { return true; }
94
Alexandre Rames9931f312015-06-19 14:47:01 +010095 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
96
Calin Juravled0d48522014-11-04 16:40:20 +000097 private:
98 HDivZeroCheck* const instruction_;
99 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
100};
101
Andreas Gampe85b62f22015-09-09 13:15:38 -0700102class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000103 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100104 DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
Calin Juravlebacfec32014-11-14 15:54:36 +0000105 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000106
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000107 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000109 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 if (is_div_) {
111 __ negl(cpu_reg_);
112 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400113 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 }
115
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000116 } else {
117 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 if (is_div_) {
119 __ negq(cpu_reg_);
120 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400121 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000123 }
Calin Juravled0d48522014-11-04 16:40:20 +0000124 __ jmp(GetExitLabel());
125 }
126
Alexandre Rames9931f312015-06-19 14:47:01 +0100127 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
128
Calin Juravled0d48522014-11-04 16:40:20 +0000129 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000130 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000131 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000132 const bool is_div_;
133 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000134};
135
Andreas Gampe85b62f22015-09-09 13:15:38 -0700136class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100138 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100139 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000141 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100142 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000144 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100145 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
146 instruction_,
147 instruction_->GetDexPc(),
148 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000149 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100150 if (successor_ == nullptr) {
151 __ jmp(GetReturnLabel());
152 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100153 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100154 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155 }
156
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 Label* GetReturnLabel() {
158 DCHECK(successor_ == nullptr);
159 return &return_label_;
160 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100162 HBasicBlock* GetSuccessor() const {
163 return successor_;
164 }
165
Alexandre Rames9931f312015-06-19 14:47:01 +0100166 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
167
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168 private:
169 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 Label return_label_;
172
173 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
174};
175
Andreas Gampe85b62f22015-09-09 13:15:38 -0700176class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
179 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000181 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100182 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames8158f282015-08-07 10:26:17 +0100183 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000185 if (instruction_->CanThrowIntoCatchBlock()) {
186 // Live registers will be restored in the catch block if caught.
187 SaveLiveRegisters(codegen, instruction_->GetLocations());
188 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000189 // We're moving two locations to locations that could overlap, so we need a parallel
190 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000192 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100193 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000194 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100195 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100196 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100197 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
198 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100199 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
200 instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100201 }
202
Alexandre Rames8158f282015-08-07 10:26:17 +0100203 bool IsFatal() const OVERRIDE { return true; }
204
Alexandre Rames9931f312015-06-19 14:47:01 +0100205 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
206
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100207 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100208 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100209
210 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
211};
212
Andreas Gampe85b62f22015-09-09 13:15:38 -0700213class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 LoadClassSlowPathX86_64(HLoadClass* cls,
216 HInstruction* at,
217 uint32_t dex_pc,
218 bool do_clinit)
219 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
220 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
221 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100222
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
226 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100227
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000228 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100232 x64_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
233 : QUICK_ENTRY_POINT(pInitializeType),
234 at_, dex_pc_, this);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000236 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000238 if (out.IsValid()) {
239 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
240 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 }
242
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000243 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100244 __ jmp(GetExitLabel());
245 }
246
Alexandre Rames9931f312015-06-19 14:47:01 +0100247 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
248
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100249 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 // The class this slow path will load.
251 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 // The instruction where this slow path is happening.
254 // (Might be the load class or an initialization check).
255 HInstruction* const at_;
256
257 // The dex PC of `at_`.
258 const uint32_t dex_pc_;
259
260 // Whether to initialize the class.
261 const bool do_clinit_;
262
263 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100264};
265
Andreas Gampe85b62f22015-09-09 13:15:38 -0700266class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000267 public:
268 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
269
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000270 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000271 LocationSummary* locations = instruction_->GetLocations();
272 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
273
274 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
275 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000277
278 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800279 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000280 Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100281 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
282 instruction_,
283 instruction_->GetDexPc(),
284 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000285 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000286 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000287 __ jmp(GetExitLabel());
288 }
289
Alexandre Rames9931f312015-06-19 14:47:01 +0100290 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
291
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000292 private:
293 HLoadString* const instruction_;
294
295 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
296};
297
Andreas Gampe85b62f22015-09-09 13:15:38 -0700298class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000300 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
301 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000303 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100305 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
306 : locations->Out();
307 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000308 DCHECK(instruction_->IsCheckCast()
309 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
311 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
312 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000313
314 if (instruction_->IsCheckCast()) {
315 // The codegen for the instruction overwrites `temp`, so put it back in place.
316 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
317 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
318 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
319 __ movl(temp, Address(obj, class_offset));
320 __ MaybeUnpoisonHeapReference(temp);
321 }
322
323 if (!is_fatal_) {
324 SaveLiveRegisters(codegen, locations);
325 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
327 // We're moving two locations to locations that could overlap, so we need a parallel
328 // move resolver.
329 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000330 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100331 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000332 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100333 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100334 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100335 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
336 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100339 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
340 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 dex_pc,
Alexandre Rames8158f282015-08-07 10:26:17 +0100342 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 } else {
344 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100345 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
346 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 dex_pc,
Alexandre Rames8158f282015-08-07 10:26:17 +0100348 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000349 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 if (!is_fatal_) {
352 if (instruction_->IsInstanceOf()) {
353 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
354 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000355
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000356 RestoreLiveRegisters(codegen, locations);
357 __ jmp(GetExitLabel());
358 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000359 }
360
Alexandre Rames9931f312015-06-19 14:47:01 +0100361 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
362
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000363 bool IsFatal() const OVERRIDE { return is_fatal_; }
364
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000365 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000366 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000367 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000368
369 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
370};
371
Andreas Gampe85b62f22015-09-09 13:15:38 -0700372class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700373 public:
374 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
375 : instruction_(instruction) {}
376
377 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100378 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700379 __ Bind(GetEntryLabel());
380 SaveLiveRegisters(codegen, instruction_->GetLocations());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381 DCHECK(instruction_->IsDeoptimize());
382 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
Alexandre Rames8158f282015-08-07 10:26:17 +0100383 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
384 deoptimize,
385 deoptimize->GetDexPc(),
386 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700387 }
388
Alexandre Rames9931f312015-06-19 14:47:01 +0100389 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
390
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 private:
392 HInstruction* const instruction_;
393 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
394};
395
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100396class ArraySetSlowPathX86_64 : public SlowPathCode {
397 public:
398 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : instruction_(instruction) {}
399
400 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
401 LocationSummary* locations = instruction_->GetLocations();
402 __ Bind(GetEntryLabel());
403 SaveLiveRegisters(codegen, locations);
404
405 InvokeRuntimeCallingConvention calling_convention;
406 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
407 parallel_move.AddMove(
408 locations->InAt(0),
409 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
410 Primitive::kPrimNot,
411 nullptr);
412 parallel_move.AddMove(
413 locations->InAt(1),
414 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
415 Primitive::kPrimInt,
416 nullptr);
417 parallel_move.AddMove(
418 locations->InAt(2),
419 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
420 Primitive::kPrimNot,
421 nullptr);
422 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
423
424 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
425 x64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
426 instruction_,
427 instruction_->GetDexPc(),
428 this);
429 RestoreLiveRegisters(codegen, locations);
430 __ jmp(GetExitLabel());
431 }
432
433 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
434
435 private:
436 HInstruction* const instruction_;
437
438 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
439};
440
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100441#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100442#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100443
Roland Levillain4fa13f62015-07-06 18:11:54 +0100444inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700445 switch (cond) {
446 case kCondEQ: return kEqual;
447 case kCondNE: return kNotEqual;
448 case kCondLT: return kLess;
449 case kCondLE: return kLessEqual;
450 case kCondGT: return kGreater;
451 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700452 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100453 LOG(FATAL) << "Unreachable";
454 UNREACHABLE();
455}
456
457inline Condition X86_64FPCondition(IfCondition cond) {
458 switch (cond) {
459 case kCondEQ: return kEqual;
460 case kCondNE: return kNotEqual;
461 case kCondLT: return kBelow;
462 case kCondLE: return kBelowEqual;
463 case kCondGT: return kAbove;
464 case kCondGE: return kAboveEqual;
465 };
466 LOG(FATAL) << "Unreachable";
467 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700468}
469
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800470void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100471 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800472 // All registers are assumed to be correctly set up.
473
Vladimir Marko58155012015-08-19 12:49:41 +0000474 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
475 switch (invoke->GetMethodLoadKind()) {
476 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
477 // temp = thread->string_init_entrypoint
478 __ gs()->movl(temp.AsRegister<CpuRegister>(),
479 Address::Absolute(invoke->GetStringInitOffset(), true));
480 break;
481 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
482 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
483 break;
484 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
485 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
486 break;
487 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
488 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
489 method_patches_.emplace_back(invoke->GetTargetMethod());
490 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
491 break;
492 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
493 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
494 invoke->GetDexCacheArrayOffset());
495 __ movq(temp.AsRegister<CpuRegister>(),
496 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
497 // Bind the label at the end of the "movl" insn.
498 __ Bind(&pc_rel_dex_cache_patches_.back().label);
499 break;
500 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
501 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
502 Register method_reg;
503 CpuRegister reg = temp.AsRegister<CpuRegister>();
504 if (current_method.IsRegister()) {
505 method_reg = current_method.AsRegister<Register>();
506 } else {
507 DCHECK(invoke->GetLocations()->Intrinsified());
508 DCHECK(!current_method.IsValid());
509 method_reg = reg.AsRegister();
510 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
511 }
512 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100513 __ movq(reg,
514 Address(CpuRegister(method_reg),
515 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000516 // temp = temp[index_in_cache]
517 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
518 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
519 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100520 }
Vladimir Marko58155012015-08-19 12:49:41 +0000521 }
522
523 switch (invoke->GetCodePtrLocation()) {
524 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
525 __ call(&frame_entry_label_);
526 break;
527 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
528 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
529 Label* label = &relative_call_patches_.back().label;
530 __ call(label); // Bind to the patch label, override at link time.
531 __ Bind(label); // Bind the label at the end of the "call" insn.
532 break;
533 }
534 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
535 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
536 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
537 FALLTHROUGH_INTENDED;
538 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
539 // (callee_method + offset_of_quick_compiled_code)()
540 __ call(Address(callee_method.AsRegister<CpuRegister>(),
541 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
542 kX86_64WordSize).SizeValue()));
543 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000544 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800545
546 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800547}
548
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000549void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
550 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
551 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
552 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
553 LocationSummary* locations = invoke->GetLocations();
554 Location receiver = locations->InAt(0);
555 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
556 // temp = object->GetClass();
557 DCHECK(receiver.IsRegister());
558 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
559 MaybeRecordImplicitNullCheck(invoke);
560 __ MaybeUnpoisonHeapReference(temp);
561 // temp = temp->GetMethodAt(method_offset);
562 __ movq(temp, Address(temp, method_offset));
563 // call temp->GetEntryPoint();
564 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
565 kX86_64WordSize).SizeValue()));
566}
567
Vladimir Marko58155012015-08-19 12:49:41 +0000568void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
569 DCHECK(linker_patches->empty());
570 size_t size =
571 method_patches_.size() + relative_call_patches_.size() + pc_rel_dex_cache_patches_.size();
572 linker_patches->reserve(size);
573 for (const MethodPatchInfo<Label>& info : method_patches_) {
574 // The label points to the end of the "movl" instruction but the literal offset for method
575 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
576 uint32_t literal_offset = info.label.Position() - 4;
577 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
578 info.target_method.dex_file,
579 info.target_method.dex_method_index));
580 }
581 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
582 // The label points to the end of the "call" instruction but the literal offset for method
583 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
584 uint32_t literal_offset = info.label.Position() - 4;
585 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
586 info.target_method.dex_file,
587 info.target_method.dex_method_index));
588 }
589 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
590 // The label points to the end of the "mov" instruction but the literal offset for method
591 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
592 uint32_t literal_offset = info.label.Position() - 4;
593 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
594 &info.target_dex_file,
595 info.label.Position(),
596 info.element_offset));
597 }
598}
599
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100600void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100601 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100602}
603
604void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100605 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100606}
607
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100608size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
609 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
610 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100611}
612
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100613size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
614 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
615 return kX86_64WordSize;
616}
617
618size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
619 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
620 return kX86_64WordSize;
621}
622
623size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
624 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
625 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100626}
627
Calin Juravle175dc732015-08-25 15:42:32 +0100628void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
629 HInstruction* instruction,
630 uint32_t dex_pc,
631 SlowPathCode* slow_path) {
632 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
633 instruction,
634 dex_pc,
635 slow_path);
636}
637
638void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100639 HInstruction* instruction,
640 uint32_t dex_pc,
641 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100642 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100643 __ gs()->call(Address::Absolute(entry_point_offset, true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100644 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100645}
646
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000647static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000648// Use a fake return address register to mimic Quick.
649static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400650CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
651 const X86_64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100652 const CompilerOptions& compiler_options,
653 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000654 : CodeGenerator(graph,
655 kNumberOfCpuRegisters,
656 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000657 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000658 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
659 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000660 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000661 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
662 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100663 compiler_options,
664 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100665 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100666 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000667 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400668 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400669 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000670 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100671 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
672 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -0400673 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
674 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000675 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
676}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100677
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100678InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
679 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100680 : HGraphVisitor(graph),
681 assembler_(codegen->GetAssembler()),
682 codegen_(codegen) {}
683
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100684Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100685 switch (type) {
686 case Primitive::kPrimLong:
687 case Primitive::kPrimByte:
688 case Primitive::kPrimBoolean:
689 case Primitive::kPrimChar:
690 case Primitive::kPrimShort:
691 case Primitive::kPrimInt:
692 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100693 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100694 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100695 }
696
697 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100698 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100699 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100700 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100701 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100702
703 case Primitive::kPrimVoid:
704 LOG(FATAL) << "Unreachable type " << type;
705 }
706
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100707 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100708}
709
Nicolas Geoffray98893962015-01-21 12:32:32 +0000710void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100711 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100712 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100713
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000714 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100715 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000716
Nicolas Geoffray98893962015-01-21 12:32:32 +0000717 if (is_baseline) {
718 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
719 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
720 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000721 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
722 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
723 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000724 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725}
726
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100727static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100728 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100729}
David Srbecky9d8606d2015-04-12 09:35:32 +0100730
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100731static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100732 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100733}
734
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100735void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100736 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000737 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100738 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700739 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000740 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100741
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000742 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100743 __ testq(CpuRegister(RAX), Address(
744 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100745 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100746 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000747
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000748 if (HasEmptyFrame()) {
749 return;
750 }
751
Nicolas Geoffray98893962015-01-21 12:32:32 +0000752 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000753 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000754 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000755 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100756 __ cfi().AdjustCFAOffset(kX86_64WordSize);
757 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000758 }
759 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100760
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100761 int adjust = GetFrameSize() - GetCoreSpillSize();
762 __ subq(CpuRegister(RSP), Immediate(adjust));
763 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000764 uint32_t xmm_spill_location = GetFpuSpillStart();
765 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100766
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000767 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
768 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100769 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
770 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
771 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000772 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100773 }
774
Mathieu Chartiere401d142015-04-22 13:56:20 -0700775 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100776 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100777}
778
779void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100780 __ cfi().RememberState();
781 if (!HasEmptyFrame()) {
782 uint32_t xmm_spill_location = GetFpuSpillStart();
783 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
784 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
785 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
786 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
787 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
788 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
789 }
790 }
791
792 int adjust = GetFrameSize() - GetCoreSpillSize();
793 __ addq(CpuRegister(RSP), Immediate(adjust));
794 __ cfi().AdjustCFAOffset(-adjust);
795
796 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
797 Register reg = kCoreCalleeSaves[i];
798 if (allocated_registers_.ContainsCoreRegister(reg)) {
799 __ popq(CpuRegister(reg));
800 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
801 __ cfi().Restore(DWARFReg(reg));
802 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000803 }
804 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100805 __ ret();
806 __ cfi().RestoreState();
807 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100808}
809
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100810void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
811 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100812}
813
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100814Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
815 switch (load->GetType()) {
816 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100817 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100818 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100819
820 case Primitive::kPrimInt:
821 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100822 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100823 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100824
825 case Primitive::kPrimBoolean:
826 case Primitive::kPrimByte:
827 case Primitive::kPrimChar:
828 case Primitive::kPrimShort:
829 case Primitive::kPrimVoid:
830 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700831 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100832 }
833
834 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700835 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100836}
837
838void CodeGeneratorX86_64::Move(Location destination, Location source) {
839 if (source.Equals(destination)) {
840 return;
841 }
842 if (destination.IsRegister()) {
843 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000844 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100845 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000846 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100847 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000848 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100849 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100850 } else {
851 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000852 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 Address(CpuRegister(RSP), source.GetStackIndex()));
854 }
855 } else if (destination.IsFpuRegister()) {
856 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000857 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000859 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100860 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000861 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100862 Address(CpuRegister(RSP), source.GetStackIndex()));
863 } else {
864 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000865 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100866 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100867 }
868 } else if (destination.IsStackSlot()) {
869 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100870 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000871 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100872 } else if (source.IsFpuRegister()) {
873 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000874 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500875 } else if (source.IsConstant()) {
876 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000877 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500878 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100879 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500880 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000881 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
882 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100883 }
884 } else {
885 DCHECK(destination.IsDoubleStackSlot());
886 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000888 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100889 } else if (source.IsFpuRegister()) {
890 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000891 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500892 } else if (source.IsConstant()) {
893 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800894 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500895 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000896 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500897 } else {
898 DCHECK(constant->IsLongConstant());
899 value = constant->AsLongConstant()->GetValue();
900 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400901 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100902 } else {
903 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000904 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
905 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100906 }
907 }
908}
909
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100910void CodeGeneratorX86_64::Move(HInstruction* instruction,
911 Location location,
912 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000913 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100914 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700915 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100916 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000917 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100918 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000919 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000920 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
921 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000922 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000923 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000924 } else if (location.IsStackSlot()) {
925 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
926 } else {
927 DCHECK(location.IsConstant());
928 DCHECK_EQ(location.GetConstant(), const_to_move);
929 }
930 } else if (const_to_move->IsLongConstant()) {
931 int64_t value = const_to_move->AsLongConstant()->GetValue();
932 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400933 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000934 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400935 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000936 } else {
937 DCHECK(location.IsConstant());
938 DCHECK_EQ(location.GetConstant(), const_to_move);
939 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100940 }
Roland Levillain476df552014-10-09 17:51:36 +0100941 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100942 switch (instruction->GetType()) {
943 case Primitive::kPrimBoolean:
944 case Primitive::kPrimByte:
945 case Primitive::kPrimChar:
946 case Primitive::kPrimShort:
947 case Primitive::kPrimInt:
948 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100949 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100950 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
951 break;
952
953 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100954 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000955 Move(location,
956 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100957 break;
958
959 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100960 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100961 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000962 } else if (instruction->IsTemporary()) {
963 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
964 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100965 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100966 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100967 switch (instruction->GetType()) {
968 case Primitive::kPrimBoolean:
969 case Primitive::kPrimByte:
970 case Primitive::kPrimChar:
971 case Primitive::kPrimShort:
972 case Primitive::kPrimInt:
973 case Primitive::kPrimNot:
974 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100975 case Primitive::kPrimFloat:
976 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000977 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100978 break;
979
980 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100981 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100982 }
983 }
984}
985
Calin Juravle175dc732015-08-25 15:42:32 +0100986void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
987 DCHECK(location.IsRegister());
988 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
989}
990
Calin Juravlee460d1d2015-09-29 04:52:17 +0100991void CodeGeneratorX86_64::MoveLocation(
992 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
993 Move(dst, src);
994}
995
996void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
997 if (location.IsRegister()) {
998 locations->AddTemp(location);
999 } else {
1000 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1001 }
1002}
1003
David Brazdilfc6a86a2015-06-26 10:33:45 +00001004void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001005 DCHECK(!successor->IsExitBlock());
1006
1007 HBasicBlock* block = got->GetBlock();
1008 HInstruction* previous = got->GetPrevious();
1009
1010 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001011 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001012 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1013 return;
1014 }
1015
1016 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1017 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1018 }
1019 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001020 __ jmp(codegen_->GetLabelOf(successor));
1021 }
1022}
1023
David Brazdilfc6a86a2015-06-26 10:33:45 +00001024void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1025 got->SetLocations(nullptr);
1026}
1027
1028void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1029 HandleGoto(got, got->GetSuccessor());
1030}
1031
1032void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1033 try_boundary->SetLocations(nullptr);
1034}
1035
1036void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1037 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1038 if (!successor->IsExitBlock()) {
1039 HandleGoto(try_boundary, successor);
1040 }
1041}
1042
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001043void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1044 exit->SetLocations(nullptr);
1045}
1046
1047void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001048 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001049}
1050
Mark Mendellc4701932015-04-10 13:18:51 -04001051void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
1052 Label* true_label,
1053 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001054 if (cond->IsFPConditionTrueIfNaN()) {
1055 __ j(kUnordered, true_label);
1056 } else if (cond->IsFPConditionFalseIfNaN()) {
1057 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001058 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001059 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001060}
1061
1062void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
1063 HCondition* condition,
1064 Label* true_target,
1065 Label* false_target,
1066 Label* always_true_target) {
1067 LocationSummary* locations = condition->GetLocations();
1068 Location left = locations->InAt(0);
1069 Location right = locations->InAt(1);
1070
1071 // We don't want true_target as a nullptr.
1072 if (true_target == nullptr) {
1073 true_target = always_true_target;
1074 }
1075 bool falls_through = (false_target == nullptr);
1076
1077 // FP compares don't like null false_targets.
1078 if (false_target == nullptr) {
1079 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1080 }
1081
1082 Primitive::Type type = condition->InputAt(0)->GetType();
1083 switch (type) {
1084 case Primitive::kPrimLong: {
1085 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1086 if (right.IsConstant()) {
1087 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1088 if (IsInt<32>(value)) {
1089 if (value == 0) {
1090 __ testq(left_reg, left_reg);
1091 } else {
1092 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1093 }
1094 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001095 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001096 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1097 }
1098 } else if (right.IsDoubleStackSlot()) {
1099 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1100 } else {
1101 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1102 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001103 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001104 break;
1105 }
1106 case Primitive::kPrimFloat: {
1107 if (right.IsFpuRegister()) {
1108 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1109 } else if (right.IsConstant()) {
1110 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1111 codegen_->LiteralFloatAddress(
1112 right.GetConstant()->AsFloatConstant()->GetValue()));
1113 } else {
1114 DCHECK(right.IsStackSlot());
1115 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1116 Address(CpuRegister(RSP), right.GetStackIndex()));
1117 }
1118 GenerateFPJumps(condition, true_target, false_target);
1119 break;
1120 }
1121 case Primitive::kPrimDouble: {
1122 if (right.IsFpuRegister()) {
1123 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1124 } else if (right.IsConstant()) {
1125 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1126 codegen_->LiteralDoubleAddress(
1127 right.GetConstant()->AsDoubleConstant()->GetValue()));
1128 } else {
1129 DCHECK(right.IsDoubleStackSlot());
1130 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1131 Address(CpuRegister(RSP), right.GetStackIndex()));
1132 }
1133 GenerateFPJumps(condition, true_target, false_target);
1134 break;
1135 }
1136 default:
1137 LOG(FATAL) << "Unexpected condition type " << type;
1138 }
1139
1140 if (!falls_through) {
1141 __ jmp(false_target);
1142 }
1143}
1144
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001145void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1146 Label* true_target,
1147 Label* false_target,
1148 Label* always_true_target) {
1149 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001150 if (cond->IsIntConstant()) {
1151 // Constant condition, statically compared against 1.
1152 int32_t cond_value = cond->AsIntConstant()->GetValue();
1153 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001154 if (always_true_target != nullptr) {
1155 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001156 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001157 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001158 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001159 DCHECK_EQ(cond_value, 0);
1160 }
1161 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001162 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001163 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1164 // Moves do not affect the eflags register, so if the condition is
1165 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001166 // again. We can't use the eflags on FP conditions if they are
1167 // materialized due to the complex branching.
1168 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001169 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001170 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1171 && !Primitive::IsFloatingPointType(type);
1172
Roland Levillain4fa13f62015-07-06 18:11:54 +01001173 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001174 if (!eflags_set) {
1175 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001176 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001177 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001178 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001179 } else {
1180 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1181 Immediate(0));
1182 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001183 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001184 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001185 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001186 }
1187 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001188 // Condition has not been materialized, use its inputs as the
1189 // comparison and its condition as the branch condition.
1190
Mark Mendellc4701932015-04-10 13:18:51 -04001191 // Is this a long or FP comparison that has been folded into the HCondition?
1192 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001193 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001194 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1195 true_target, false_target, always_true_target);
1196 return;
1197 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001198
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001199 Location lhs = cond->GetLocations()->InAt(0);
1200 Location rhs = cond->GetLocations()->InAt(1);
1201 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001202 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001203 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001204 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001205 if (constant == 0) {
1206 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1207 } else {
1208 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1209 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001210 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001211 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001212 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1213 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001214 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001215 }
Dave Allison20dfc792014-06-16 20:44:29 -07001216 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001217 if (false_target != nullptr) {
1218 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001219 }
1220}
1221
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001222void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1223 LocationSummary* locations =
1224 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1225 HInstruction* cond = if_instr->InputAt(0);
1226 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1227 locations->SetInAt(0, Location::Any());
1228 }
1229}
1230
1231void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1232 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1233 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1234 Label* always_true_target = true_target;
1235 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1236 if_instr->IfTrueSuccessor())) {
1237 always_true_target = nullptr;
1238 }
1239 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1240 if_instr->IfFalseSuccessor())) {
1241 false_target = nullptr;
1242 }
1243 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1244}
1245
1246void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1247 LocationSummary* locations = new (GetGraph()->GetArena())
1248 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1249 HInstruction* cond = deoptimize->InputAt(0);
1250 DCHECK(cond->IsCondition());
1251 if (cond->AsCondition()->NeedsMaterialization()) {
1252 locations->SetInAt(0, Location::Any());
1253 }
1254}
1255
1256void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001257 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001258 DeoptimizationSlowPathX86_64(deoptimize);
1259 codegen_->AddSlowPath(slow_path);
1260 Label* slow_path_entry = slow_path->GetEntryLabel();
1261 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1262}
1263
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1265 local->SetLocations(nullptr);
1266}
1267
1268void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1269 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1270}
1271
1272void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1273 local->SetLocations(nullptr);
1274}
1275
1276void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
1277 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001278 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001279}
1280
1281void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001282 LocationSummary* locations =
1283 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001284 switch (store->InputAt(1)->GetType()) {
1285 case Primitive::kPrimBoolean:
1286 case Primitive::kPrimByte:
1287 case Primitive::kPrimChar:
1288 case Primitive::kPrimShort:
1289 case Primitive::kPrimInt:
1290 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001291 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001292 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1293 break;
1294
1295 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001296 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001297 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1298 break;
1299
1300 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001301 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001302 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001303}
1304
1305void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001306 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001307}
1308
Roland Levillain0d37cd02015-05-27 16:39:19 +01001309void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001310 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001311 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001312 // Handle the long/FP comparisons made in instruction simplification.
1313 switch (cond->InputAt(0)->GetType()) {
1314 case Primitive::kPrimLong:
1315 locations->SetInAt(0, Location::RequiresRegister());
1316 locations->SetInAt(1, Location::Any());
1317 break;
1318 case Primitive::kPrimFloat:
1319 case Primitive::kPrimDouble:
1320 locations->SetInAt(0, Location::RequiresFpuRegister());
1321 locations->SetInAt(1, Location::Any());
1322 break;
1323 default:
1324 locations->SetInAt(0, Location::RequiresRegister());
1325 locations->SetInAt(1, Location::Any());
1326 break;
1327 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001328 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001329 locations->SetOut(Location::RequiresRegister());
1330 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001331}
1332
Roland Levillain0d37cd02015-05-27 16:39:19 +01001333void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001334 if (!cond->NeedsMaterialization()) {
1335 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001336 }
Mark Mendellc4701932015-04-10 13:18:51 -04001337
1338 LocationSummary* locations = cond->GetLocations();
1339 Location lhs = locations->InAt(0);
1340 Location rhs = locations->InAt(1);
1341 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1342 Label true_label, false_label;
1343
1344 switch (cond->InputAt(0)->GetType()) {
1345 default:
1346 // Integer case.
1347
1348 // Clear output register: setcc only sets the low byte.
1349 __ xorl(reg, reg);
1350
1351 if (rhs.IsRegister()) {
1352 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1353 } else if (rhs.IsConstant()) {
1354 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1355 if (constant == 0) {
1356 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1357 } else {
1358 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1359 }
1360 } else {
1361 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1362 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001363 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001364 return;
1365 case Primitive::kPrimLong:
1366 // Clear output register: setcc only sets the low byte.
1367 __ xorl(reg, reg);
1368
1369 if (rhs.IsRegister()) {
1370 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1371 } else if (rhs.IsConstant()) {
1372 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1373 if (IsInt<32>(value)) {
1374 if (value == 0) {
1375 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1376 } else {
1377 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1378 }
1379 } else {
1380 // Value won't fit in an int.
1381 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1382 }
1383 } else {
1384 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1385 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001386 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001387 return;
1388 case Primitive::kPrimFloat: {
1389 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1390 if (rhs.IsConstant()) {
1391 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1392 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1393 } else if (rhs.IsStackSlot()) {
1394 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1395 } else {
1396 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1397 }
1398 GenerateFPJumps(cond, &true_label, &false_label);
1399 break;
1400 }
1401 case Primitive::kPrimDouble: {
1402 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1403 if (rhs.IsConstant()) {
1404 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1405 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1406 } else if (rhs.IsDoubleStackSlot()) {
1407 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1408 } else {
1409 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1410 }
1411 GenerateFPJumps(cond, &true_label, &false_label);
1412 break;
1413 }
1414 }
1415
1416 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001417 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001418
Roland Levillain4fa13f62015-07-06 18:11:54 +01001419 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001420 __ Bind(&false_label);
1421 __ xorl(reg, reg);
1422 __ jmp(&done_label);
1423
Roland Levillain4fa13f62015-07-06 18:11:54 +01001424 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001425 __ Bind(&true_label);
1426 __ movl(reg, Immediate(1));
1427 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001428}
1429
1430void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1431 VisitCondition(comp);
1432}
1433
1434void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1435 VisitCondition(comp);
1436}
1437
1438void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1439 VisitCondition(comp);
1440}
1441
1442void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1443 VisitCondition(comp);
1444}
1445
1446void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1447 VisitCondition(comp);
1448}
1449
1450void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1451 VisitCondition(comp);
1452}
1453
1454void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1455 VisitCondition(comp);
1456}
1457
1458void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1459 VisitCondition(comp);
1460}
1461
1462void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1463 VisitCondition(comp);
1464}
1465
1466void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1467 VisitCondition(comp);
1468}
1469
1470void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1471 VisitCondition(comp);
1472}
1473
1474void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1475 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001476}
1477
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001478void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001479 LocationSummary* locations =
1480 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001481 switch (compare->InputAt(0)->GetType()) {
1482 case Primitive::kPrimLong: {
1483 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001484 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1486 break;
1487 }
1488 case Primitive::kPrimFloat:
1489 case Primitive::kPrimDouble: {
1490 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001491 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001492 locations->SetOut(Location::RequiresRegister());
1493 break;
1494 }
1495 default:
1496 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1497 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001498}
1499
1500void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001501 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001502 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001503 Location left = locations->InAt(0);
1504 Location right = locations->InAt(1);
1505
Mark Mendell0c9497d2015-08-21 09:30:05 -04001506 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001507 Primitive::Type type = compare->InputAt(0)->GetType();
1508 switch (type) {
1509 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001510 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1511 if (right.IsConstant()) {
1512 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001513 if (IsInt<32>(value)) {
1514 if (value == 0) {
1515 __ testq(left_reg, left_reg);
1516 } else {
1517 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1518 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001519 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001520 // Value won't fit in an int.
1521 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001522 }
Mark Mendell40741f32015-04-20 22:10:34 -04001523 } else if (right.IsDoubleStackSlot()) {
1524 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001525 } else {
1526 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1527 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001528 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001529 }
1530 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001531 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1532 if (right.IsConstant()) {
1533 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1534 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1535 } else if (right.IsStackSlot()) {
1536 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1537 } else {
1538 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1539 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001540 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1541 break;
1542 }
1543 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001544 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1545 if (right.IsConstant()) {
1546 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1547 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1548 } else if (right.IsDoubleStackSlot()) {
1549 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1550 } else {
1551 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1552 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001553 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1554 break;
1555 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001556 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001557 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001558 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001559 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001560 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001561 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001562
Calin Juravle91debbc2014-11-26 19:01:09 +00001563 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001564 __ movl(out, Immediate(1));
1565 __ jmp(&done);
1566
1567 __ Bind(&less);
1568 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001569
1570 __ Bind(&done);
1571}
1572
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001573void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001574 LocationSummary* locations =
1575 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001576 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001577}
1578
1579void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001580 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001581 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001582}
1583
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001584void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1585 LocationSummary* locations =
1586 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1587 locations->SetOut(Location::ConstantLocation(constant));
1588}
1589
1590void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant) {
1591 // Will be generated at use site.
1592 UNUSED(constant);
1593}
1594
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001595void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001596 LocationSummary* locations =
1597 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001598 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001599}
1600
1601void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001602 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001603 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001604}
1605
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001606void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1607 LocationSummary* locations =
1608 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1609 locations->SetOut(Location::ConstantLocation(constant));
1610}
1611
1612void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
1613 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001614 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001615}
1616
1617void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1618 LocationSummary* locations =
1619 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1620 locations->SetOut(Location::ConstantLocation(constant));
1621}
1622
1623void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1624 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001625 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001626}
1627
Calin Juravle27df7582015-04-17 19:12:31 +01001628void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1629 memory_barrier->SetLocations(nullptr);
1630}
1631
1632void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1633 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1634}
1635
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001636void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1637 ret->SetLocations(nullptr);
1638}
1639
1640void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001641 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001642 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001643}
1644
1645void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001646 LocationSummary* locations =
1647 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001648 switch (ret->InputAt(0)->GetType()) {
1649 case Primitive::kPrimBoolean:
1650 case Primitive::kPrimByte:
1651 case Primitive::kPrimChar:
1652 case Primitive::kPrimShort:
1653 case Primitive::kPrimInt:
1654 case Primitive::kPrimNot:
1655 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001656 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001657 break;
1658
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001659 case Primitive::kPrimFloat:
1660 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001661 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001662 break;
1663
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001664 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001665 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001666 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001667}
1668
1669void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1670 if (kIsDebugBuild) {
1671 switch (ret->InputAt(0)->GetType()) {
1672 case Primitive::kPrimBoolean:
1673 case Primitive::kPrimByte:
1674 case Primitive::kPrimChar:
1675 case Primitive::kPrimShort:
1676 case Primitive::kPrimInt:
1677 case Primitive::kPrimNot:
1678 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001680 break;
1681
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001682 case Primitive::kPrimFloat:
1683 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001684 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001685 XMM0);
1686 break;
1687
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001688 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001689 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001690 }
1691 }
1692 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001693}
1694
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001695Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1696 switch (type) {
1697 case Primitive::kPrimBoolean:
1698 case Primitive::kPrimByte:
1699 case Primitive::kPrimChar:
1700 case Primitive::kPrimShort:
1701 case Primitive::kPrimInt:
1702 case Primitive::kPrimNot:
1703 case Primitive::kPrimLong:
1704 return Location::RegisterLocation(RAX);
1705
1706 case Primitive::kPrimVoid:
1707 return Location::NoLocation();
1708
1709 case Primitive::kPrimDouble:
1710 case Primitive::kPrimFloat:
1711 return Location::FpuRegisterLocation(XMM0);
1712 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001713
1714 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001715}
1716
1717Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1718 return Location::RegisterLocation(kMethodRegisterArgument);
1719}
1720
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001721Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001722 switch (type) {
1723 case Primitive::kPrimBoolean:
1724 case Primitive::kPrimByte:
1725 case Primitive::kPrimChar:
1726 case Primitive::kPrimShort:
1727 case Primitive::kPrimInt:
1728 case Primitive::kPrimNot: {
1729 uint32_t index = gp_index_++;
1730 stack_index_++;
1731 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001732 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001733 } else {
1734 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1735 }
1736 }
1737
1738 case Primitive::kPrimLong: {
1739 uint32_t index = gp_index_;
1740 stack_index_ += 2;
1741 if (index < calling_convention.GetNumberOfRegisters()) {
1742 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001743 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001744 } else {
1745 gp_index_ += 2;
1746 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1747 }
1748 }
1749
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001750 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001751 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001752 stack_index_++;
1753 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001754 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001755 } else {
1756 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1757 }
1758 }
1759
1760 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001761 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001762 stack_index_ += 2;
1763 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001764 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001765 } else {
1766 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1767 }
1768 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001769
1770 case Primitive::kPrimVoid:
1771 LOG(FATAL) << "Unexpected parameter type " << type;
1772 break;
1773 }
1774 return Location();
1775}
1776
Calin Juravle175dc732015-08-25 15:42:32 +01001777void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1778 // The trampoline uses the same calling convention as dex calling conventions,
1779 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1780 // the method_idx.
1781 HandleInvoke(invoke);
1782}
1783
1784void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1785 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1786}
1787
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001788void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001789 // When we do not run baseline, explicit clinit checks triggered by static
1790 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1791 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001792
Mark Mendellfb8d2792015-03-31 22:16:59 -04001793 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001794 if (intrinsic.TryDispatch(invoke)) {
1795 return;
1796 }
1797
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001798 HandleInvoke(invoke);
1799}
1800
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001801static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1802 if (invoke->GetLocations()->Intrinsified()) {
1803 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1804 intrinsic.Dispatch(invoke);
1805 return true;
1806 }
1807 return false;
1808}
1809
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001810void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001811 // When we do not run baseline, explicit clinit checks triggered by static
1812 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1813 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001814
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001815 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1816 return;
1817 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001818
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001819 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001820 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001821 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001822 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001823}
1824
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001825void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001826 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001827 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001828}
1829
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001830void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001831 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001832 if (intrinsic.TryDispatch(invoke)) {
1833 return;
1834 }
1835
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001836 HandleInvoke(invoke);
1837}
1838
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001839void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001840 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1841 return;
1842 }
1843
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001844 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001845
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001846 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001847 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001848}
1849
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001850void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1851 HandleInvoke(invoke);
1852 // Add the hidden argument.
1853 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1854}
1855
1856void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1857 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001858 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001859 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1860 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001861 LocationSummary* locations = invoke->GetLocations();
1862 Location receiver = locations->InAt(0);
1863 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1864
1865 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001866 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1867 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001868
1869 // temp = object->GetClass();
1870 if (receiver.IsStackSlot()) {
1871 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1872 __ movl(temp, Address(temp, class_offset));
1873 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001874 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001875 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001876 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001877 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001878 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001879 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001880 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001881 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001882 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001883
1884 DCHECK(!codegen_->IsLeafMethod());
1885 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1886}
1887
Roland Levillain88cb1752014-10-20 16:36:47 +01001888void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1889 LocationSummary* locations =
1890 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1891 switch (neg->GetResultType()) {
1892 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001893 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001894 locations->SetInAt(0, Location::RequiresRegister());
1895 locations->SetOut(Location::SameAsFirstInput());
1896 break;
1897
Roland Levillain88cb1752014-10-20 16:36:47 +01001898 case Primitive::kPrimFloat:
1899 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001900 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001901 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001902 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001903 break;
1904
1905 default:
1906 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1907 }
1908}
1909
1910void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1911 LocationSummary* locations = neg->GetLocations();
1912 Location out = locations->Out();
1913 Location in = locations->InAt(0);
1914 switch (neg->GetResultType()) {
1915 case Primitive::kPrimInt:
1916 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001917 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001918 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001919 break;
1920
1921 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001922 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001923 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001924 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001925 break;
1926
Roland Levillain5368c212014-11-27 15:03:41 +00001927 case Primitive::kPrimFloat: {
1928 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001929 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001930 // Implement float negation with an exclusive or with value
1931 // 0x80000000 (mask for bit 31, representing the sign of a
1932 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001933 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001935 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001936 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001937
Roland Levillain5368c212014-11-27 15:03:41 +00001938 case Primitive::kPrimDouble: {
1939 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001940 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001941 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001942 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001943 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001944 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001945 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001946 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001947 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001948
1949 default:
1950 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1951 }
1952}
1953
Roland Levillaindff1f282014-11-05 14:15:05 +00001954void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1955 LocationSummary* locations =
1956 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1957 Primitive::Type result_type = conversion->GetResultType();
1958 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001959 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00001960
David Brazdilb2bd1c52015-03-25 11:17:37 +00001961 // The Java language does not allow treating boolean as an integral type but
1962 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001963
Roland Levillaindff1f282014-11-05 14:15:05 +00001964 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001965 case Primitive::kPrimByte:
1966 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001967 case Primitive::kPrimBoolean:
1968 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001969 case Primitive::kPrimShort:
1970 case Primitive::kPrimInt:
1971 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001972 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001973 locations->SetInAt(0, Location::Any());
1974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1975 break;
1976
1977 default:
1978 LOG(FATAL) << "Unexpected type conversion from " << input_type
1979 << " to " << result_type;
1980 }
1981 break;
1982
Roland Levillain01a8d712014-11-14 16:27:39 +00001983 case Primitive::kPrimShort:
1984 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001985 case Primitive::kPrimBoolean:
1986 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001987 case Primitive::kPrimByte:
1988 case Primitive::kPrimInt:
1989 case Primitive::kPrimChar:
1990 // Processing a Dex `int-to-short' instruction.
1991 locations->SetInAt(0, Location::Any());
1992 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1993 break;
1994
1995 default:
1996 LOG(FATAL) << "Unexpected type conversion from " << input_type
1997 << " to " << result_type;
1998 }
1999 break;
2000
Roland Levillain946e1432014-11-11 17:35:19 +00002001 case Primitive::kPrimInt:
2002 switch (input_type) {
2003 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002004 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002005 locations->SetInAt(0, Location::Any());
2006 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2007 break;
2008
2009 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002010 // Processing a Dex `float-to-int' instruction.
2011 locations->SetInAt(0, Location::RequiresFpuRegister());
2012 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002013 break;
2014
Roland Levillain946e1432014-11-11 17:35:19 +00002015 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002016 // Processing a Dex `double-to-int' instruction.
2017 locations->SetInAt(0, Location::RequiresFpuRegister());
2018 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002019 break;
2020
2021 default:
2022 LOG(FATAL) << "Unexpected type conversion from " << input_type
2023 << " to " << result_type;
2024 }
2025 break;
2026
Roland Levillaindff1f282014-11-05 14:15:05 +00002027 case Primitive::kPrimLong:
2028 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002029 case Primitive::kPrimBoolean:
2030 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002031 case Primitive::kPrimByte:
2032 case Primitive::kPrimShort:
2033 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002034 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002035 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002036 // TODO: We would benefit from a (to-be-implemented)
2037 // Location::RegisterOrStackSlot requirement for this input.
2038 locations->SetInAt(0, Location::RequiresRegister());
2039 locations->SetOut(Location::RequiresRegister());
2040 break;
2041
2042 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002043 // Processing a Dex `float-to-long' instruction.
2044 locations->SetInAt(0, Location::RequiresFpuRegister());
2045 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002046 break;
2047
Roland Levillaindff1f282014-11-05 14:15:05 +00002048 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002049 // Processing a Dex `double-to-long' instruction.
2050 locations->SetInAt(0, Location::RequiresFpuRegister());
2051 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002052 break;
2053
2054 default:
2055 LOG(FATAL) << "Unexpected type conversion from " << input_type
2056 << " to " << result_type;
2057 }
2058 break;
2059
Roland Levillain981e4542014-11-14 11:47:14 +00002060 case Primitive::kPrimChar:
2061 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002062 case Primitive::kPrimBoolean:
2063 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002064 case Primitive::kPrimByte:
2065 case Primitive::kPrimShort:
2066 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002067 // Processing a Dex `int-to-char' instruction.
2068 locations->SetInAt(0, Location::Any());
2069 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2070 break;
2071
2072 default:
2073 LOG(FATAL) << "Unexpected type conversion from " << input_type
2074 << " to " << result_type;
2075 }
2076 break;
2077
Roland Levillaindff1f282014-11-05 14:15:05 +00002078 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002079 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002080 case Primitive::kPrimBoolean:
2081 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002082 case Primitive::kPrimByte:
2083 case Primitive::kPrimShort:
2084 case Primitive::kPrimInt:
2085 case Primitive::kPrimChar:
2086 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002087 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002088 locations->SetOut(Location::RequiresFpuRegister());
2089 break;
2090
2091 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002092 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002093 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002094 locations->SetOut(Location::RequiresFpuRegister());
2095 break;
2096
Roland Levillaincff13742014-11-17 14:32:17 +00002097 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002098 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002099 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002100 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002101 break;
2102
2103 default:
2104 LOG(FATAL) << "Unexpected type conversion from " << input_type
2105 << " to " << result_type;
2106 };
2107 break;
2108
Roland Levillaindff1f282014-11-05 14:15:05 +00002109 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002110 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002111 case Primitive::kPrimBoolean:
2112 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002113 case Primitive::kPrimByte:
2114 case Primitive::kPrimShort:
2115 case Primitive::kPrimInt:
2116 case Primitive::kPrimChar:
2117 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002118 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002119 locations->SetOut(Location::RequiresFpuRegister());
2120 break;
2121
2122 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002123 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002124 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002125 locations->SetOut(Location::RequiresFpuRegister());
2126 break;
2127
Roland Levillaincff13742014-11-17 14:32:17 +00002128 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002129 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002130 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002131 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002132 break;
2133
2134 default:
2135 LOG(FATAL) << "Unexpected type conversion from " << input_type
2136 << " to " << result_type;
2137 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002138 break;
2139
2140 default:
2141 LOG(FATAL) << "Unexpected type conversion from " << input_type
2142 << " to " << result_type;
2143 }
2144}
2145
2146void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2147 LocationSummary* locations = conversion->GetLocations();
2148 Location out = locations->Out();
2149 Location in = locations->InAt(0);
2150 Primitive::Type result_type = conversion->GetResultType();
2151 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002152 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002153 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002154 case Primitive::kPrimByte:
2155 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002156 case Primitive::kPrimBoolean:
2157 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002158 case Primitive::kPrimShort:
2159 case Primitive::kPrimInt:
2160 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002161 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002162 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002163 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002164 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002165 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002166 Address(CpuRegister(RSP), in.GetStackIndex()));
2167 } else {
2168 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002170 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2171 }
2172 break;
2173
2174 default:
2175 LOG(FATAL) << "Unexpected type conversion from " << input_type
2176 << " to " << result_type;
2177 }
2178 break;
2179
Roland Levillain01a8d712014-11-14 16:27:39 +00002180 case Primitive::kPrimShort:
2181 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002182 case Primitive::kPrimBoolean:
2183 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002184 case Primitive::kPrimByte:
2185 case Primitive::kPrimInt:
2186 case Primitive::kPrimChar:
2187 // Processing a Dex `int-to-short' instruction.
2188 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002189 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002190 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002191 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002192 Address(CpuRegister(RSP), in.GetStackIndex()));
2193 } else {
2194 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002195 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002196 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2197 }
2198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 }
2204 break;
2205
Roland Levillain946e1432014-11-11 17:35:19 +00002206 case Primitive::kPrimInt:
2207 switch (input_type) {
2208 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002209 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002210 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002211 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002212 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002213 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002214 Address(CpuRegister(RSP), in.GetStackIndex()));
2215 } else {
2216 DCHECK(in.IsConstant());
2217 DCHECK(in.GetConstant()->IsLongConstant());
2218 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002219 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002220 }
2221 break;
2222
Roland Levillain3f8f9362014-12-02 17:45:01 +00002223 case Primitive::kPrimFloat: {
2224 // Processing a Dex `float-to-int' instruction.
2225 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2226 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002227 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002228
2229 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002230 // if input >= (float)INT_MAX goto done
2231 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002232 __ j(kAboveEqual, &done);
2233 // if input == NaN goto nan
2234 __ j(kUnordered, &nan);
2235 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002236 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002237 __ jmp(&done);
2238 __ Bind(&nan);
2239 // output = 0
2240 __ xorl(output, output);
2241 __ Bind(&done);
2242 break;
2243 }
2244
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002245 case Primitive::kPrimDouble: {
2246 // Processing a Dex `double-to-int' instruction.
2247 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2248 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002249 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002250
2251 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002252 // if input >= (double)INT_MAX goto done
2253 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002254 __ j(kAboveEqual, &done);
2255 // if input == NaN goto nan
2256 __ j(kUnordered, &nan);
2257 // output = double-to-int-truncate(input)
2258 __ cvttsd2si(output, input);
2259 __ jmp(&done);
2260 __ Bind(&nan);
2261 // output = 0
2262 __ xorl(output, output);
2263 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002264 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002265 }
Roland Levillain946e1432014-11-11 17:35:19 +00002266
2267 default:
2268 LOG(FATAL) << "Unexpected type conversion from " << input_type
2269 << " to " << result_type;
2270 }
2271 break;
2272
Roland Levillaindff1f282014-11-05 14:15:05 +00002273 case Primitive::kPrimLong:
2274 switch (input_type) {
2275 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002276 case Primitive::kPrimBoolean:
2277 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002278 case Primitive::kPrimByte:
2279 case Primitive::kPrimShort:
2280 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002281 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002282 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002283 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002284 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002285 break;
2286
Roland Levillain624279f2014-12-04 11:54:28 +00002287 case Primitive::kPrimFloat: {
2288 // Processing a Dex `float-to-long' instruction.
2289 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2290 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002291 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002292
Mark Mendell92e83bf2015-05-07 11:25:03 -04002293 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002294 // if input >= (float)LONG_MAX goto done
2295 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002296 __ j(kAboveEqual, &done);
2297 // if input == NaN goto nan
2298 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002299 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002300 __ cvttss2si(output, input, true);
2301 __ jmp(&done);
2302 __ Bind(&nan);
2303 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002304 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002305 __ Bind(&done);
2306 break;
2307 }
2308
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002309 case Primitive::kPrimDouble: {
2310 // Processing a Dex `double-to-long' instruction.
2311 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2312 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002313 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002314
Mark Mendell92e83bf2015-05-07 11:25:03 -04002315 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002316 // if input >= (double)LONG_MAX goto done
2317 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002318 __ j(kAboveEqual, &done);
2319 // if input == NaN goto nan
2320 __ j(kUnordered, &nan);
2321 // output = double-to-long-truncate(input)
2322 __ cvttsd2si(output, input, true);
2323 __ jmp(&done);
2324 __ Bind(&nan);
2325 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002326 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002327 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002328 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002329 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002330
2331 default:
2332 LOG(FATAL) << "Unexpected type conversion from " << input_type
2333 << " to " << result_type;
2334 }
2335 break;
2336
Roland Levillain981e4542014-11-14 11:47:14 +00002337 case Primitive::kPrimChar:
2338 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002339 case Primitive::kPrimBoolean:
2340 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002341 case Primitive::kPrimByte:
2342 case Primitive::kPrimShort:
2343 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002344 // Processing a Dex `int-to-char' instruction.
2345 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002346 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002347 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002348 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002349 Address(CpuRegister(RSP), in.GetStackIndex()));
2350 } else {
2351 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002352 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002353 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2354 }
2355 break;
2356
2357 default:
2358 LOG(FATAL) << "Unexpected type conversion from " << input_type
2359 << " to " << result_type;
2360 }
2361 break;
2362
Roland Levillaindff1f282014-11-05 14:15:05 +00002363 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002364 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002365 case Primitive::kPrimBoolean:
2366 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002367 case Primitive::kPrimByte:
2368 case Primitive::kPrimShort:
2369 case Primitive::kPrimInt:
2370 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002371 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002372 if (in.IsRegister()) {
2373 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2374 } else if (in.IsConstant()) {
2375 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2376 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2377 if (v == 0) {
2378 __ xorps(dest, dest);
2379 } else {
2380 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2381 }
2382 } else {
2383 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2384 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2385 }
Roland Levillaincff13742014-11-17 14:32:17 +00002386 break;
2387
2388 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002389 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002390 if (in.IsRegister()) {
2391 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2392 } else if (in.IsConstant()) {
2393 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2394 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2395 if (v == 0) {
2396 __ xorps(dest, dest);
2397 } else {
2398 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2399 }
2400 } else {
2401 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2402 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2403 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002404 break;
2405
Roland Levillaincff13742014-11-17 14:32:17 +00002406 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002407 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002408 if (in.IsFpuRegister()) {
2409 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2410 } else if (in.IsConstant()) {
2411 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2412 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2413 if (bit_cast<int64_t, double>(v) == 0) {
2414 __ xorps(dest, dest);
2415 } else {
2416 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2417 }
2418 } else {
2419 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2420 Address(CpuRegister(RSP), in.GetStackIndex()));
2421 }
Roland Levillaincff13742014-11-17 14:32:17 +00002422 break;
2423
2424 default:
2425 LOG(FATAL) << "Unexpected type conversion from " << input_type
2426 << " to " << result_type;
2427 };
2428 break;
2429
Roland Levillaindff1f282014-11-05 14:15:05 +00002430 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002431 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002432 case Primitive::kPrimBoolean:
2433 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002434 case Primitive::kPrimByte:
2435 case Primitive::kPrimShort:
2436 case Primitive::kPrimInt:
2437 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002438 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002439 if (in.IsRegister()) {
2440 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2441 } else if (in.IsConstant()) {
2442 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2443 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2444 if (v == 0) {
2445 __ xorpd(dest, dest);
2446 } else {
2447 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2448 }
2449 } else {
2450 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2451 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2452 }
Roland Levillaincff13742014-11-17 14:32:17 +00002453 break;
2454
2455 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002456 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002457 if (in.IsRegister()) {
2458 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2459 } else if (in.IsConstant()) {
2460 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2461 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2462 if (v == 0) {
2463 __ xorpd(dest, dest);
2464 } else {
2465 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2466 }
2467 } else {
2468 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2469 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2470 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002471 break;
2472
Roland Levillaincff13742014-11-17 14:32:17 +00002473 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002474 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002475 if (in.IsFpuRegister()) {
2476 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2477 } else if (in.IsConstant()) {
2478 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2479 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2480 if (bit_cast<int32_t, float>(v) == 0) {
2481 __ xorpd(dest, dest);
2482 } else {
2483 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2484 }
2485 } else {
2486 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2487 Address(CpuRegister(RSP), in.GetStackIndex()));
2488 }
Roland Levillaincff13742014-11-17 14:32:17 +00002489 break;
2490
2491 default:
2492 LOG(FATAL) << "Unexpected type conversion from " << input_type
2493 << " to " << result_type;
2494 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002495 break;
2496
2497 default:
2498 LOG(FATAL) << "Unexpected type conversion from " << input_type
2499 << " to " << result_type;
2500 }
2501}
2502
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002503void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002504 LocationSummary* locations =
2505 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002506 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002507 case Primitive::kPrimInt: {
2508 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002509 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2510 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002511 break;
2512 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002513
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002514 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002515 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002516 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002517 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002519 break;
2520 }
2521
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002522 case Primitive::kPrimDouble:
2523 case Primitive::kPrimFloat: {
2524 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002525 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002526 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002527 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002528 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002529
2530 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002531 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002532 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002533}
2534
2535void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2536 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002537 Location first = locations->InAt(0);
2538 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002539 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002540
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002541 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002542 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002543 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002544 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2545 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002546 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2547 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002548 } else {
2549 __ leal(out.AsRegister<CpuRegister>(), Address(
2550 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2551 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002552 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002553 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2554 __ addl(out.AsRegister<CpuRegister>(),
2555 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2556 } else {
2557 __ leal(out.AsRegister<CpuRegister>(), Address(
2558 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2559 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002560 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002561 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002562 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002563 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002564 break;
2565 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002566
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002567 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002568 if (second.IsRegister()) {
2569 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2570 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002571 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2572 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002573 } else {
2574 __ leaq(out.AsRegister<CpuRegister>(), Address(
2575 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2576 }
2577 } else {
2578 DCHECK(second.IsConstant());
2579 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2580 int32_t int32_value = Low32Bits(value);
2581 DCHECK_EQ(int32_value, value);
2582 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2583 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2584 } else {
2585 __ leaq(out.AsRegister<CpuRegister>(), Address(
2586 first.AsRegister<CpuRegister>(), int32_value));
2587 }
2588 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002589 break;
2590 }
2591
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002592 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002593 if (second.IsFpuRegister()) {
2594 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2595 } else if (second.IsConstant()) {
2596 __ addss(first.AsFpuRegister<XmmRegister>(),
2597 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2598 } else {
2599 DCHECK(second.IsStackSlot());
2600 __ addss(first.AsFpuRegister<XmmRegister>(),
2601 Address(CpuRegister(RSP), second.GetStackIndex()));
2602 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002603 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002604 }
2605
2606 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002607 if (second.IsFpuRegister()) {
2608 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2609 } else if (second.IsConstant()) {
2610 __ addsd(first.AsFpuRegister<XmmRegister>(),
2611 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2612 } else {
2613 DCHECK(second.IsDoubleStackSlot());
2614 __ addsd(first.AsFpuRegister<XmmRegister>(),
2615 Address(CpuRegister(RSP), second.GetStackIndex()));
2616 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002617 break;
2618 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002619
2620 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002621 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002622 }
2623}
2624
2625void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002626 LocationSummary* locations =
2627 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002628 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002629 case Primitive::kPrimInt: {
2630 locations->SetInAt(0, Location::RequiresRegister());
2631 locations->SetInAt(1, Location::Any());
2632 locations->SetOut(Location::SameAsFirstInput());
2633 break;
2634 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002635 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002636 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002637 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002638 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002639 break;
2640 }
Calin Juravle11351682014-10-23 15:38:15 +01002641 case Primitive::kPrimFloat:
2642 case Primitive::kPrimDouble: {
2643 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002644 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002645 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002646 break;
Calin Juravle11351682014-10-23 15:38:15 +01002647 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002648 default:
Calin Juravle11351682014-10-23 15:38:15 +01002649 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002650 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002651}
2652
2653void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2654 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002655 Location first = locations->InAt(0);
2656 Location second = locations->InAt(1);
2657 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002658 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002659 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002660 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002661 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002662 } else if (second.IsConstant()) {
2663 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002665 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002666 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002667 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002668 break;
2669 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002670 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002671 if (second.IsConstant()) {
2672 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2673 DCHECK(IsInt<32>(value));
2674 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2675 } else {
2676 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2677 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002678 break;
2679 }
2680
Calin Juravle11351682014-10-23 15:38:15 +01002681 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002682 if (second.IsFpuRegister()) {
2683 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2684 } else if (second.IsConstant()) {
2685 __ subss(first.AsFpuRegister<XmmRegister>(),
2686 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2687 } else {
2688 DCHECK(second.IsStackSlot());
2689 __ subss(first.AsFpuRegister<XmmRegister>(),
2690 Address(CpuRegister(RSP), second.GetStackIndex()));
2691 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002692 break;
Calin Juravle11351682014-10-23 15:38:15 +01002693 }
2694
2695 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002696 if (second.IsFpuRegister()) {
2697 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2698 } else if (second.IsConstant()) {
2699 __ subsd(first.AsFpuRegister<XmmRegister>(),
2700 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2701 } else {
2702 DCHECK(second.IsDoubleStackSlot());
2703 __ subsd(first.AsFpuRegister<XmmRegister>(),
2704 Address(CpuRegister(RSP), second.GetStackIndex()));
2705 }
Calin Juravle11351682014-10-23 15:38:15 +01002706 break;
2707 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002708
2709 default:
Calin Juravle11351682014-10-23 15:38:15 +01002710 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002711 }
2712}
2713
Calin Juravle34bacdf2014-10-07 20:23:36 +01002714void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2715 LocationSummary* locations =
2716 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2717 switch (mul->GetResultType()) {
2718 case Primitive::kPrimInt: {
2719 locations->SetInAt(0, Location::RequiresRegister());
2720 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002721 if (mul->InputAt(1)->IsIntConstant()) {
2722 // Can use 3 operand multiply.
2723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2724 } else {
2725 locations->SetOut(Location::SameAsFirstInput());
2726 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002727 break;
2728 }
2729 case Primitive::kPrimLong: {
2730 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002731 locations->SetInAt(1, Location::Any());
2732 if (mul->InputAt(1)->IsLongConstant() &&
2733 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002734 // Can use 3 operand multiply.
2735 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2736 } else {
2737 locations->SetOut(Location::SameAsFirstInput());
2738 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002739 break;
2740 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002741 case Primitive::kPrimFloat:
2742 case Primitive::kPrimDouble: {
2743 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002744 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002745 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002746 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002747 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002748
2749 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002750 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002751 }
2752}
2753
2754void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2755 LocationSummary* locations = mul->GetLocations();
2756 Location first = locations->InAt(0);
2757 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002758 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002759 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002760 case Primitive::kPrimInt:
2761 // The constant may have ended up in a register, so test explicitly to avoid
2762 // problems where the output may not be the same as the first operand.
2763 if (mul->InputAt(1)->IsIntConstant()) {
2764 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2765 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2766 } else if (second.IsRegister()) {
2767 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002768 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002769 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002770 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002771 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002772 __ imull(first.AsRegister<CpuRegister>(),
2773 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002774 }
2775 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002776 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002777 // The constant may have ended up in a register, so test explicitly to avoid
2778 // problems where the output may not be the same as the first operand.
2779 if (mul->InputAt(1)->IsLongConstant()) {
2780 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2781 if (IsInt<32>(value)) {
2782 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2783 Immediate(static_cast<int32_t>(value)));
2784 } else {
2785 // Have to use the constant area.
2786 DCHECK(first.Equals(out));
2787 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2788 }
2789 } else if (second.IsRegister()) {
2790 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002791 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002792 } else {
2793 DCHECK(second.IsDoubleStackSlot());
2794 DCHECK(first.Equals(out));
2795 __ imulq(first.AsRegister<CpuRegister>(),
2796 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002797 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002798 break;
2799 }
2800
Calin Juravleb5bfa962014-10-21 18:02:24 +01002801 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002802 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002803 if (second.IsFpuRegister()) {
2804 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2805 } else if (second.IsConstant()) {
2806 __ mulss(first.AsFpuRegister<XmmRegister>(),
2807 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2808 } else {
2809 DCHECK(second.IsStackSlot());
2810 __ mulss(first.AsFpuRegister<XmmRegister>(),
2811 Address(CpuRegister(RSP), second.GetStackIndex()));
2812 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002813 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002814 }
2815
2816 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002817 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002818 if (second.IsFpuRegister()) {
2819 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2820 } else if (second.IsConstant()) {
2821 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2822 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2823 } else {
2824 DCHECK(second.IsDoubleStackSlot());
2825 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2826 Address(CpuRegister(RSP), second.GetStackIndex()));
2827 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002828 break;
2829 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002830
2831 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002832 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002833 }
2834}
2835
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002836void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2837 uint32_t stack_adjustment, bool is_float) {
2838 if (source.IsStackSlot()) {
2839 DCHECK(is_float);
2840 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2841 } else if (source.IsDoubleStackSlot()) {
2842 DCHECK(!is_float);
2843 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2844 } else {
2845 // Write the value to the temporary location on the stack and load to FP stack.
2846 if (is_float) {
2847 Location stack_temp = Location::StackSlot(temp_offset);
2848 codegen_->Move(stack_temp, source);
2849 __ flds(Address(CpuRegister(RSP), temp_offset));
2850 } else {
2851 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2852 codegen_->Move(stack_temp, source);
2853 __ fldl(Address(CpuRegister(RSP), temp_offset));
2854 }
2855 }
2856}
2857
2858void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2859 Primitive::Type type = rem->GetResultType();
2860 bool is_float = type == Primitive::kPrimFloat;
2861 size_t elem_size = Primitive::ComponentSize(type);
2862 LocationSummary* locations = rem->GetLocations();
2863 Location first = locations->InAt(0);
2864 Location second = locations->InAt(1);
2865 Location out = locations->Out();
2866
2867 // Create stack space for 2 elements.
2868 // TODO: enhance register allocator to ask for stack temporaries.
2869 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2870
2871 // Load the values to the FP stack in reverse order, using temporaries if needed.
2872 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2873 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2874
2875 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002876 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002877 __ Bind(&retry);
2878 __ fprem();
2879
2880 // Move FP status to AX.
2881 __ fstsw();
2882
2883 // And see if the argument reduction is complete. This is signaled by the
2884 // C2 FPU flag bit set to 0.
2885 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2886 __ j(kNotEqual, &retry);
2887
2888 // We have settled on the final value. Retrieve it into an XMM register.
2889 // Store FP top of stack to real stack.
2890 if (is_float) {
2891 __ fsts(Address(CpuRegister(RSP), 0));
2892 } else {
2893 __ fstl(Address(CpuRegister(RSP), 0));
2894 }
2895
2896 // Pop the 2 items from the FP stack.
2897 __ fucompp();
2898
2899 // Load the value from the stack into an XMM register.
2900 DCHECK(out.IsFpuRegister()) << out;
2901 if (is_float) {
2902 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2903 } else {
2904 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2905 }
2906
2907 // And remove the temporary stack space we allocated.
2908 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2909}
2910
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002911void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2912 DCHECK(instruction->IsDiv() || instruction->IsRem());
2913
2914 LocationSummary* locations = instruction->GetLocations();
2915 Location second = locations->InAt(1);
2916 DCHECK(second.IsConstant());
2917
2918 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2919 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002920 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002921
2922 DCHECK(imm == 1 || imm == -1);
2923
2924 switch (instruction->GetResultType()) {
2925 case Primitive::kPrimInt: {
2926 if (instruction->IsRem()) {
2927 __ xorl(output_register, output_register);
2928 } else {
2929 __ movl(output_register, input_register);
2930 if (imm == -1) {
2931 __ negl(output_register);
2932 }
2933 }
2934 break;
2935 }
2936
2937 case Primitive::kPrimLong: {
2938 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002939 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002940 } else {
2941 __ movq(output_register, input_register);
2942 if (imm == -1) {
2943 __ negq(output_register);
2944 }
2945 }
2946 break;
2947 }
2948
2949 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002950 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002951 }
2952}
2953
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002954void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002955 LocationSummary* locations = instruction->GetLocations();
2956 Location second = locations->InAt(1);
2957
2958 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2959 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
2960
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002961 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002962
2963 DCHECK(IsPowerOfTwo(std::abs(imm)));
2964
2965 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2966
2967 if (instruction->GetResultType() == Primitive::kPrimInt) {
2968 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
2969 __ testl(numerator, numerator);
2970 __ cmov(kGreaterEqual, tmp, numerator);
2971 int shift = CTZ(imm);
2972 __ sarl(tmp, Immediate(shift));
2973
2974 if (imm < 0) {
2975 __ negl(tmp);
2976 }
2977
2978 __ movl(output_register, tmp);
2979 } else {
2980 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2981 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
2982
Mark Mendell92e83bf2015-05-07 11:25:03 -04002983 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002984 __ addq(rdx, numerator);
2985 __ testq(numerator, numerator);
2986 __ cmov(kGreaterEqual, rdx, numerator);
2987 int shift = CTZ(imm);
2988 __ sarq(rdx, Immediate(shift));
2989
2990 if (imm < 0) {
2991 __ negq(rdx);
2992 }
2993
2994 __ movq(output_register, rdx);
2995 }
2996}
2997
2998void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2999 DCHECK(instruction->IsDiv() || instruction->IsRem());
3000
3001 LocationSummary* locations = instruction->GetLocations();
3002 Location second = locations->InAt(1);
3003
3004 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3005 : locations->GetTemp(0).AsRegister<CpuRegister>();
3006 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3007 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3008 : locations->Out().AsRegister<CpuRegister>();
3009 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3010
3011 DCHECK_EQ(RAX, eax.AsRegister());
3012 DCHECK_EQ(RDX, edx.AsRegister());
3013 if (instruction->IsDiv()) {
3014 DCHECK_EQ(RAX, out.AsRegister());
3015 } else {
3016 DCHECK_EQ(RDX, out.AsRegister());
3017 }
3018
3019 int64_t magic;
3020 int shift;
3021
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003022 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003023 if (instruction->GetResultType() == Primitive::kPrimInt) {
3024 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3025
3026 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3027
3028 __ movl(numerator, eax);
3029
Mark Mendell0c9497d2015-08-21 09:30:05 -04003030 NearLabel no_div;
3031 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003032 __ testl(eax, eax);
3033 __ j(kNotEqual, &no_div);
3034
3035 __ xorl(out, out);
3036 __ jmp(&end);
3037
3038 __ Bind(&no_div);
3039
3040 __ movl(eax, Immediate(magic));
3041 __ imull(numerator);
3042
3043 if (imm > 0 && magic < 0) {
3044 __ addl(edx, numerator);
3045 } else if (imm < 0 && magic > 0) {
3046 __ subl(edx, numerator);
3047 }
3048
3049 if (shift != 0) {
3050 __ sarl(edx, Immediate(shift));
3051 }
3052
3053 __ movl(eax, edx);
3054 __ shrl(edx, Immediate(31));
3055 __ addl(edx, eax);
3056
3057 if (instruction->IsRem()) {
3058 __ movl(eax, numerator);
3059 __ imull(edx, Immediate(imm));
3060 __ subl(eax, edx);
3061 __ movl(edx, eax);
3062 } else {
3063 __ movl(eax, edx);
3064 }
3065 __ Bind(&end);
3066 } else {
3067 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3068
3069 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3070
3071 CpuRegister rax = eax;
3072 CpuRegister rdx = edx;
3073
3074 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3075
3076 // Save the numerator.
3077 __ movq(numerator, rax);
3078
3079 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003080 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003081
3082 // RDX:RAX = magic * numerator
3083 __ imulq(numerator);
3084
3085 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003086 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003087 __ addq(rdx, numerator);
3088 } else if (imm < 0 && magic > 0) {
3089 // RDX -= numerator
3090 __ subq(rdx, numerator);
3091 }
3092
3093 // Shift if needed.
3094 if (shift != 0) {
3095 __ sarq(rdx, Immediate(shift));
3096 }
3097
3098 // RDX += 1 if RDX < 0
3099 __ movq(rax, rdx);
3100 __ shrq(rdx, Immediate(63));
3101 __ addq(rdx, rax);
3102
3103 if (instruction->IsRem()) {
3104 __ movq(rax, numerator);
3105
3106 if (IsInt<32>(imm)) {
3107 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3108 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003109 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003110 }
3111
3112 __ subq(rax, rdx);
3113 __ movq(rdx, rax);
3114 } else {
3115 __ movq(rax, rdx);
3116 }
3117 }
3118}
3119
Calin Juravlebacfec32014-11-14 15:54:36 +00003120void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3121 DCHECK(instruction->IsDiv() || instruction->IsRem());
3122 Primitive::Type type = instruction->GetResultType();
3123 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3124
3125 bool is_div = instruction->IsDiv();
3126 LocationSummary* locations = instruction->GetLocations();
3127
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003128 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3129 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003130
Roland Levillain271ab9c2014-11-27 15:23:57 +00003131 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003132 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003133
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003134 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003135 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003136
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003137 if (imm == 0) {
3138 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3139 } else if (imm == 1 || imm == -1) {
3140 DivRemOneOrMinusOne(instruction);
3141 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003142 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003143 } else {
3144 DCHECK(imm <= -2 || imm >= 2);
3145 GenerateDivRemWithAnyConstant(instruction);
3146 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003147 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003148 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003149 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3150 out.AsRegister(), type, is_div);
3151 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003152
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003153 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3154 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3155 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3156 // so it's safe to just use negl instead of more complex comparisons.
3157 if (type == Primitive::kPrimInt) {
3158 __ cmpl(second_reg, Immediate(-1));
3159 __ j(kEqual, slow_path->GetEntryLabel());
3160 // edx:eax <- sign-extended of eax
3161 __ cdq();
3162 // eax = quotient, edx = remainder
3163 __ idivl(second_reg);
3164 } else {
3165 __ cmpq(second_reg, Immediate(-1));
3166 __ j(kEqual, slow_path->GetEntryLabel());
3167 // rdx:rax <- sign-extended of rax
3168 __ cqo();
3169 // rax = quotient, rdx = remainder
3170 __ idivq(second_reg);
3171 }
3172 __ Bind(slow_path->GetExitLabel());
3173 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003174}
3175
Calin Juravle7c4954d2014-10-28 16:57:40 +00003176void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3177 LocationSummary* locations =
3178 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3179 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003180 case Primitive::kPrimInt:
3181 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003182 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003183 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003184 locations->SetOut(Location::SameAsFirstInput());
3185 // Intel uses edx:eax as the dividend.
3186 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003187 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3188 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3189 // output and request another temp.
3190 if (div->InputAt(1)->IsConstant()) {
3191 locations->AddTemp(Location::RequiresRegister());
3192 }
Calin Juravled0d48522014-11-04 16:40:20 +00003193 break;
3194 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003195
Calin Juravle7c4954d2014-10-28 16:57:40 +00003196 case Primitive::kPrimFloat:
3197 case Primitive::kPrimDouble: {
3198 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003199 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003200 locations->SetOut(Location::SameAsFirstInput());
3201 break;
3202 }
3203
3204 default:
3205 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3206 }
3207}
3208
3209void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3210 LocationSummary* locations = div->GetLocations();
3211 Location first = locations->InAt(0);
3212 Location second = locations->InAt(1);
3213 DCHECK(first.Equals(locations->Out()));
3214
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003215 Primitive::Type type = div->GetResultType();
3216 switch (type) {
3217 case Primitive::kPrimInt:
3218 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003219 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003220 break;
3221 }
3222
Calin Juravle7c4954d2014-10-28 16:57:40 +00003223 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003224 if (second.IsFpuRegister()) {
3225 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3226 } else if (second.IsConstant()) {
3227 __ divss(first.AsFpuRegister<XmmRegister>(),
3228 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3229 } else {
3230 DCHECK(second.IsStackSlot());
3231 __ divss(first.AsFpuRegister<XmmRegister>(),
3232 Address(CpuRegister(RSP), second.GetStackIndex()));
3233 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003234 break;
3235 }
3236
3237 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003238 if (second.IsFpuRegister()) {
3239 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3240 } else if (second.IsConstant()) {
3241 __ divsd(first.AsFpuRegister<XmmRegister>(),
3242 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3243 } else {
3244 DCHECK(second.IsDoubleStackSlot());
3245 __ divsd(first.AsFpuRegister<XmmRegister>(),
3246 Address(CpuRegister(RSP), second.GetStackIndex()));
3247 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003248 break;
3249 }
3250
3251 default:
3252 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3253 }
3254}
3255
Calin Juravlebacfec32014-11-14 15:54:36 +00003256void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003257 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003258 LocationSummary* locations =
3259 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003260
3261 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003262 case Primitive::kPrimInt:
3263 case Primitive::kPrimLong: {
3264 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003265 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003266 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3267 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003268 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3269 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3270 // output and request another temp.
3271 if (rem->InputAt(1)->IsConstant()) {
3272 locations->AddTemp(Location::RequiresRegister());
3273 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003274 break;
3275 }
3276
3277 case Primitive::kPrimFloat:
3278 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003279 locations->SetInAt(0, Location::Any());
3280 locations->SetInAt(1, Location::Any());
3281 locations->SetOut(Location::RequiresFpuRegister());
3282 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003283 break;
3284 }
3285
3286 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003287 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003288 }
3289}
3290
3291void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3292 Primitive::Type type = rem->GetResultType();
3293 switch (type) {
3294 case Primitive::kPrimInt:
3295 case Primitive::kPrimLong: {
3296 GenerateDivRemIntegral(rem);
3297 break;
3298 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003299 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003300 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003301 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003302 break;
3303 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003304 default:
3305 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3306 }
3307}
3308
Calin Juravled0d48522014-11-04 16:40:20 +00003309void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003310 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3311 ? LocationSummary::kCallOnSlowPath
3312 : LocationSummary::kNoCall;
3313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003314 locations->SetInAt(0, Location::Any());
3315 if (instruction->HasUses()) {
3316 locations->SetOut(Location::SameAsFirstInput());
3317 }
3318}
3319
3320void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003321 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003322 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3323 codegen_->AddSlowPath(slow_path);
3324
3325 LocationSummary* locations = instruction->GetLocations();
3326 Location value = locations->InAt(0);
3327
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003328 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003329 case Primitive::kPrimByte:
3330 case Primitive::kPrimChar:
3331 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003332 case Primitive::kPrimInt: {
3333 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003334 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003335 __ j(kEqual, slow_path->GetEntryLabel());
3336 } else if (value.IsStackSlot()) {
3337 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3338 __ j(kEqual, slow_path->GetEntryLabel());
3339 } else {
3340 DCHECK(value.IsConstant()) << value;
3341 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3342 __ jmp(slow_path->GetEntryLabel());
3343 }
3344 }
3345 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003346 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003347 case Primitive::kPrimLong: {
3348 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003349 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003350 __ j(kEqual, slow_path->GetEntryLabel());
3351 } else if (value.IsDoubleStackSlot()) {
3352 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3353 __ j(kEqual, slow_path->GetEntryLabel());
3354 } else {
3355 DCHECK(value.IsConstant()) << value;
3356 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3357 __ jmp(slow_path->GetEntryLabel());
3358 }
3359 }
3360 break;
3361 }
3362 default:
3363 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003364 }
Calin Juravled0d48522014-11-04 16:40:20 +00003365}
3366
Calin Juravle9aec02f2014-11-18 23:06:35 +00003367void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3368 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3369
3370 LocationSummary* locations =
3371 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3372
3373 switch (op->GetResultType()) {
3374 case Primitive::kPrimInt:
3375 case Primitive::kPrimLong: {
3376 locations->SetInAt(0, Location::RequiresRegister());
3377 // The shift count needs to be in CL.
3378 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3379 locations->SetOut(Location::SameAsFirstInput());
3380 break;
3381 }
3382 default:
3383 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3384 }
3385}
3386
3387void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3388 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3389
3390 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003391 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003392 Location second = locations->InAt(1);
3393
3394 switch (op->GetResultType()) {
3395 case Primitive::kPrimInt: {
3396 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003397 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003398 if (op->IsShl()) {
3399 __ shll(first_reg, second_reg);
3400 } else if (op->IsShr()) {
3401 __ sarl(first_reg, second_reg);
3402 } else {
3403 __ shrl(first_reg, second_reg);
3404 }
3405 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003406 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003407 if (op->IsShl()) {
3408 __ shll(first_reg, imm);
3409 } else if (op->IsShr()) {
3410 __ sarl(first_reg, imm);
3411 } else {
3412 __ shrl(first_reg, imm);
3413 }
3414 }
3415 break;
3416 }
3417 case Primitive::kPrimLong: {
3418 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003419 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003420 if (op->IsShl()) {
3421 __ shlq(first_reg, second_reg);
3422 } else if (op->IsShr()) {
3423 __ sarq(first_reg, second_reg);
3424 } else {
3425 __ shrq(first_reg, second_reg);
3426 }
3427 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003428 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003429 if (op->IsShl()) {
3430 __ shlq(first_reg, imm);
3431 } else if (op->IsShr()) {
3432 __ sarq(first_reg, imm);
3433 } else {
3434 __ shrq(first_reg, imm);
3435 }
3436 }
3437 break;
3438 }
3439 default:
3440 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3441 }
3442}
3443
3444void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3445 HandleShift(shl);
3446}
3447
3448void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3449 HandleShift(shl);
3450}
3451
3452void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3453 HandleShift(shr);
3454}
3455
3456void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3457 HandleShift(shr);
3458}
3459
3460void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3461 HandleShift(ushr);
3462}
3463
3464void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3465 HandleShift(ushr);
3466}
3467
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003468void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003469 LocationSummary* locations =
3470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003471 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003472 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003473 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003474 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003475}
3476
3477void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3478 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003479 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3480 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003481 // Note: if heap poisoning is enabled, the entry point takes cares
3482 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003483
Calin Juravle175dc732015-08-25 15:42:32 +01003484 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3485 instruction,
3486 instruction->GetDexPc(),
3487 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003488
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003489 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003490}
3491
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003492void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3493 LocationSummary* locations =
3494 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3495 InvokeRuntimeCallingConvention calling_convention;
3496 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003497 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003498 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003499 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003500}
3501
3502void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3503 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003504 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3505 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003506
Roland Levillain4d027112015-07-01 15:41:14 +01003507 // Note: if heap poisoning is enabled, the entry point takes cares
3508 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003509 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3510 instruction,
3511 instruction->GetDexPc(),
3512 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003513
3514 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003515}
3516
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003517void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003518 LocationSummary* locations =
3519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003520 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3521 if (location.IsStackSlot()) {
3522 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3523 } else if (location.IsDoubleStackSlot()) {
3524 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3525 }
3526 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003527}
3528
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003529void InstructionCodeGeneratorX86_64::VisitParameterValue(
3530 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003531 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003532}
3533
3534void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3535 LocationSummary* locations =
3536 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3537 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3538}
3539
3540void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3541 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3542 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003543}
3544
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003545void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003546 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003547 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003548 locations->SetInAt(0, Location::RequiresRegister());
3549 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003550}
3551
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003552void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3553 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003554 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3555 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003556 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003557 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003558 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003559 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003560 break;
3561
3562 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003563 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003564 break;
3565
3566 default:
3567 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3568 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003569}
3570
David Brazdil66d126e2015-04-03 16:02:44 +01003571void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3572 LocationSummary* locations =
3573 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3574 locations->SetInAt(0, Location::RequiresRegister());
3575 locations->SetOut(Location::SameAsFirstInput());
3576}
3577
3578void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003579 LocationSummary* locations = bool_not->GetLocations();
3580 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3581 locations->Out().AsRegister<CpuRegister>().AsRegister());
3582 Location out = locations->Out();
3583 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3584}
3585
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003586void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003587 LocationSummary* locations =
3588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003589 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3590 locations->SetInAt(i, Location::Any());
3591 }
3592 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003593}
3594
3595void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003596 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003597 LOG(FATAL) << "Unimplemented";
3598}
3599
Calin Juravle52c48962014-12-16 17:02:57 +00003600void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3601 /*
3602 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3603 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3604 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3605 */
3606 switch (kind) {
3607 case MemBarrierKind::kAnyAny: {
3608 __ mfence();
3609 break;
3610 }
3611 case MemBarrierKind::kAnyStore:
3612 case MemBarrierKind::kLoadAny:
3613 case MemBarrierKind::kStoreStore: {
3614 // nop
3615 break;
3616 }
3617 default:
3618 LOG(FATAL) << "Unexpected memory barier " << kind;
3619 }
3620}
3621
3622void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3623 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3624
Nicolas Geoffray39468442014-09-02 15:17:15 +01003625 LocationSummary* locations =
3626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003627 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003628 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3629 locations->SetOut(Location::RequiresFpuRegister());
3630 } else {
3631 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3632 }
Calin Juravle52c48962014-12-16 17:02:57 +00003633}
3634
3635void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3636 const FieldInfo& field_info) {
3637 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3638
3639 LocationSummary* locations = instruction->GetLocations();
3640 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3641 Location out = locations->Out();
3642 bool is_volatile = field_info.IsVolatile();
3643 Primitive::Type field_type = field_info.GetFieldType();
3644 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3645
3646 switch (field_type) {
3647 case Primitive::kPrimBoolean: {
3648 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3649 break;
3650 }
3651
3652 case Primitive::kPrimByte: {
3653 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3654 break;
3655 }
3656
3657 case Primitive::kPrimShort: {
3658 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3659 break;
3660 }
3661
3662 case Primitive::kPrimChar: {
3663 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3664 break;
3665 }
3666
3667 case Primitive::kPrimInt:
3668 case Primitive::kPrimNot: {
3669 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3670 break;
3671 }
3672
3673 case Primitive::kPrimLong: {
3674 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3675 break;
3676 }
3677
3678 case Primitive::kPrimFloat: {
3679 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3680 break;
3681 }
3682
3683 case Primitive::kPrimDouble: {
3684 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3685 break;
3686 }
3687
3688 case Primitive::kPrimVoid:
3689 LOG(FATAL) << "Unreachable type " << field_type;
3690 UNREACHABLE();
3691 }
3692
Calin Juravle77520bc2015-01-12 18:45:46 +00003693 codegen_->MaybeRecordImplicitNullCheck(instruction);
3694
Calin Juravle52c48962014-12-16 17:02:57 +00003695 if (is_volatile) {
3696 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3697 }
Roland Levillain4d027112015-07-01 15:41:14 +01003698
3699 if (field_type == Primitive::kPrimNot) {
3700 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3701 }
Calin Juravle52c48962014-12-16 17:02:57 +00003702}
3703
3704void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3705 const FieldInfo& field_info) {
3706 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3707
3708 LocationSummary* locations =
3709 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003710 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003711 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003712 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003713
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003714 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003715 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3716 locations->SetInAt(1, Location::RequiresFpuRegister());
3717 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003718 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003719 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003720 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003721 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003722 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003723 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003724 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3725 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003726 locations->AddTemp(Location::RequiresRegister());
3727 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003728}
3729
Calin Juravle52c48962014-12-16 17:02:57 +00003730void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003731 const FieldInfo& field_info,
3732 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003733 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3734
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003735 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003736 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3737 Location value = locations->InAt(1);
3738 bool is_volatile = field_info.IsVolatile();
3739 Primitive::Type field_type = field_info.GetFieldType();
3740 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3741
3742 if (is_volatile) {
3743 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3744 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003745
3746 switch (field_type) {
3747 case Primitive::kPrimBoolean:
3748 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003749 if (value.IsConstant()) {
3750 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3751 __ movb(Address(base, offset), Immediate(v));
3752 } else {
3753 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3754 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003755 break;
3756 }
3757
3758 case Primitive::kPrimShort:
3759 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003760 if (value.IsConstant()) {
3761 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3762 __ movw(Address(base, offset), Immediate(v));
3763 } else {
3764 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3765 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003766 break;
3767 }
3768
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003769 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003770 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003771 if (value.IsConstant()) {
3772 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003773 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3774 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3775 // Note: if heap poisoning is enabled, no need to poison
3776 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003777 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003778 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003779 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3780 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3781 __ movl(temp, value.AsRegister<CpuRegister>());
3782 __ PoisonHeapReference(temp);
3783 __ movl(Address(base, offset), temp);
3784 } else {
3785 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3786 }
Mark Mendell40741f32015-04-20 22:10:34 -04003787 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003788 break;
3789 }
3790
3791 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003792 if (value.IsConstant()) {
3793 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3794 DCHECK(IsInt<32>(v));
3795 int32_t v_32 = v;
3796 __ movq(Address(base, offset), Immediate(v_32));
3797 } else {
3798 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3799 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003800 break;
3801 }
3802
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003803 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003804 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003805 break;
3806 }
3807
3808 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003809 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003810 break;
3811 }
3812
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003813 case Primitive::kPrimVoid:
3814 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003815 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003816 }
Calin Juravle52c48962014-12-16 17:02:57 +00003817
Calin Juravle77520bc2015-01-12 18:45:46 +00003818 codegen_->MaybeRecordImplicitNullCheck(instruction);
3819
3820 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3821 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3822 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003823 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003824 }
3825
Calin Juravle52c48962014-12-16 17:02:57 +00003826 if (is_volatile) {
3827 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3828 }
3829}
3830
3831void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3832 HandleFieldSet(instruction, instruction->GetFieldInfo());
3833}
3834
3835void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003836 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003837}
3838
3839void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003840 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003841}
3842
3843void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003844 HandleFieldGet(instruction, instruction->GetFieldInfo());
3845}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003846
Calin Juravle52c48962014-12-16 17:02:57 +00003847void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3848 HandleFieldGet(instruction);
3849}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003850
Calin Juravle52c48962014-12-16 17:02:57 +00003851void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3852 HandleFieldGet(instruction, instruction->GetFieldInfo());
3853}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003854
Calin Juravle52c48962014-12-16 17:02:57 +00003855void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3856 HandleFieldSet(instruction, instruction->GetFieldInfo());
3857}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003858
Calin Juravle52c48962014-12-16 17:02:57 +00003859void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003860 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003861}
3862
Calin Juravlee460d1d2015-09-29 04:52:17 +01003863void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
3864 HUnresolvedInstanceFieldGet* instruction) {
3865 FieldAccessCallingConventionX86_64 calling_convention;
3866 codegen_->CreateUnresolvedFieldLocationSummary(
3867 instruction, instruction->GetFieldType(), calling_convention);
3868}
3869
3870void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
3871 HUnresolvedInstanceFieldGet* instruction) {
3872 FieldAccessCallingConventionX86_64 calling_convention;
3873 codegen_->GenerateUnresolvedFieldAccess(instruction,
3874 instruction->GetFieldType(),
3875 instruction->GetFieldIndex(),
3876 instruction->GetDexPc(),
3877 calling_convention);
3878}
3879
3880void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
3881 HUnresolvedInstanceFieldSet* instruction) {
3882 FieldAccessCallingConventionX86_64 calling_convention;
3883 codegen_->CreateUnresolvedFieldLocationSummary(
3884 instruction, instruction->GetFieldType(), calling_convention);
3885}
3886
3887void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
3888 HUnresolvedInstanceFieldSet* instruction) {
3889 FieldAccessCallingConventionX86_64 calling_convention;
3890 codegen_->GenerateUnresolvedFieldAccess(instruction,
3891 instruction->GetFieldType(),
3892 instruction->GetFieldIndex(),
3893 instruction->GetDexPc(),
3894 calling_convention);
3895}
3896
3897void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
3898 HUnresolvedStaticFieldGet* instruction) {
3899 FieldAccessCallingConventionX86_64 calling_convention;
3900 codegen_->CreateUnresolvedFieldLocationSummary(
3901 instruction, instruction->GetFieldType(), calling_convention);
3902}
3903
3904void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
3905 HUnresolvedStaticFieldGet* instruction) {
3906 FieldAccessCallingConventionX86_64 calling_convention;
3907 codegen_->GenerateUnresolvedFieldAccess(instruction,
3908 instruction->GetFieldType(),
3909 instruction->GetFieldIndex(),
3910 instruction->GetDexPc(),
3911 calling_convention);
3912}
3913
3914void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
3915 HUnresolvedStaticFieldSet* instruction) {
3916 FieldAccessCallingConventionX86_64 calling_convention;
3917 codegen_->CreateUnresolvedFieldLocationSummary(
3918 instruction, instruction->GetFieldType(), calling_convention);
3919}
3920
3921void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
3922 HUnresolvedStaticFieldSet* instruction) {
3923 FieldAccessCallingConventionX86_64 calling_convention;
3924 codegen_->GenerateUnresolvedFieldAccess(instruction,
3925 instruction->GetFieldType(),
3926 instruction->GetFieldIndex(),
3927 instruction->GetDexPc(),
3928 calling_convention);
3929}
3930
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003931void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003932 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3933 ? LocationSummary::kCallOnSlowPath
3934 : LocationSummary::kNoCall;
3935 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3936 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003937 ? Location::RequiresRegister()
3938 : Location::Any();
3939 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003940 if (instruction->HasUses()) {
3941 locations->SetOut(Location::SameAsFirstInput());
3942 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003943}
3944
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003945void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003946 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3947 return;
3948 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003949 LocationSummary* locations = instruction->GetLocations();
3950 Location obj = locations->InAt(0);
3951
3952 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
3953 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3954}
3955
3956void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003957 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003958 codegen_->AddSlowPath(slow_path);
3959
3960 LocationSummary* locations = instruction->GetLocations();
3961 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003962
3963 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003964 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003965 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003966 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003967 } else {
3968 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00003969 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003970 __ jmp(slow_path->GetEntryLabel());
3971 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003972 }
3973 __ j(kEqual, slow_path->GetEntryLabel());
3974}
3975
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003976void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003977 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003978 GenerateImplicitNullCheck(instruction);
3979 } else {
3980 GenerateExplicitNullCheck(instruction);
3981 }
3982}
3983
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003984void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003985 LocationSummary* locations =
3986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003987 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04003988 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003989 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3990 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3991 } else {
3992 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3993 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003994}
3995
3996void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
3997 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003998 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003999 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004000 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004001
Roland Levillain4d027112015-07-01 15:41:14 +01004002 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004003 case Primitive::kPrimBoolean: {
4004 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004005 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004006 if (index.IsConstant()) {
4007 __ movzxb(out, Address(obj,
4008 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4009 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004010 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004011 }
4012 break;
4013 }
4014
4015 case Primitive::kPrimByte: {
4016 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004017 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004018 if (index.IsConstant()) {
4019 __ movsxb(out, Address(obj,
4020 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4021 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004022 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004023 }
4024 break;
4025 }
4026
4027 case Primitive::kPrimShort: {
4028 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004029 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004030 if (index.IsConstant()) {
4031 __ movsxw(out, Address(obj,
4032 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4033 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004034 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004035 }
4036 break;
4037 }
4038
4039 case Primitive::kPrimChar: {
4040 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004041 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004042 if (index.IsConstant()) {
4043 __ movzxw(out, Address(obj,
4044 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4045 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004046 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004047 }
4048 break;
4049 }
4050
4051 case Primitive::kPrimInt:
4052 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01004053 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4054 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004055 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004056 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004057 if (index.IsConstant()) {
4058 __ movl(out, Address(obj,
4059 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4060 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004061 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004062 }
4063 break;
4064 }
4065
4066 case Primitive::kPrimLong: {
4067 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004068 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004069 if (index.IsConstant()) {
4070 __ movq(out, Address(obj,
4071 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4072 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004073 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004074 }
4075 break;
4076 }
4077
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004078 case Primitive::kPrimFloat: {
4079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004080 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004081 if (index.IsConstant()) {
4082 __ movss(out, Address(obj,
4083 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4084 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004085 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004086 }
4087 break;
4088 }
4089
4090 case Primitive::kPrimDouble: {
4091 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004092 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004093 if (index.IsConstant()) {
4094 __ movsd(out, Address(obj,
4095 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4096 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004097 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004098 }
4099 break;
4100 }
4101
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004102 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004103 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004104 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004105 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004106 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004107
4108 if (type == Primitive::kPrimNot) {
4109 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4110 __ MaybeUnpoisonHeapReference(out);
4111 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004112}
4113
4114void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004115 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004116
4117 bool needs_write_barrier =
4118 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004119 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004120
Nicolas Geoffray39468442014-09-02 15:17:15 +01004121 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004122 instruction,
4123 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004124
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004125 locations->SetInAt(0, Location::RequiresRegister());
4126 locations->SetInAt(
4127 1, Location::RegisterOrConstant(instruction->InputAt(1)));
4128 locations->SetInAt(2, Location::RequiresRegister());
4129 if (value_type == Primitive::kPrimLong) {
4130 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
4131 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
4132 locations->SetInAt(2, Location::RequiresFpuRegister());
4133 } else {
4134 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4135 }
4136
4137 if (needs_write_barrier) {
4138 // Temporary registers for the write barrier.
4139 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4140 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004141 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004142}
4143
4144void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4145 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004146 CpuRegister array = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004147 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004148 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004149 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004150 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004151 bool needs_write_barrier =
4152 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004153 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4154 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4155 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004156
4157 switch (value_type) {
4158 case Primitive::kPrimBoolean:
4159 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004160 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4161 Address address = index.IsConstant()
4162 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4163 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4164 if (value.IsRegister()) {
4165 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004166 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004167 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004168 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004169 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004170 break;
4171 }
4172
4173 case Primitive::kPrimShort:
4174 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004175 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4176 Address address = index.IsConstant()
4177 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4178 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4179 if (value.IsRegister()) {
4180 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004181 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004182 DCHECK(value.IsConstant()) << value;
4183 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004184 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004185 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004186 break;
4187 }
4188
4189 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004190 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4191 Address address = index.IsConstant()
4192 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4193 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4194 if (!value.IsRegister()) {
4195 // Just setting null.
4196 DCHECK(instruction->InputAt(2)->IsNullConstant());
4197 DCHECK(value.IsConstant()) << value;
4198 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004199 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004200 DCHECK(!needs_write_barrier);
4201 DCHECK(!may_need_runtime_call);
4202 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004203 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004204
4205 DCHECK(needs_write_barrier);
4206 CpuRegister register_value = value.AsRegister<CpuRegister>();
4207 NearLabel done, not_null, do_put;
4208 SlowPathCode* slow_path = nullptr;
4209 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4210 if (may_need_runtime_call) {
4211 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4212 codegen_->AddSlowPath(slow_path);
4213 if (instruction->GetValueCanBeNull()) {
4214 __ testl(register_value, register_value);
4215 __ j(kNotEqual, &not_null);
4216 __ movl(address, Immediate(0));
4217 codegen_->MaybeRecordImplicitNullCheck(instruction);
4218 __ jmp(&done);
4219 __ Bind(&not_null);
4220 }
4221
4222 __ movl(temp, Address(array, class_offset));
4223 codegen_->MaybeRecordImplicitNullCheck(instruction);
4224 __ MaybeUnpoisonHeapReference(temp);
4225 __ movl(temp, Address(temp, component_offset));
4226 // No need to poison/unpoison, we're comparing two poisoned references.
4227 __ cmpl(temp, Address(register_value, class_offset));
4228 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4229 __ j(kEqual, &do_put);
4230 __ MaybeUnpoisonHeapReference(temp);
4231 __ movl(temp, Address(temp, super_offset));
4232 // No need to unpoison the result, we're comparing against null.
4233 __ testl(temp, temp);
4234 __ j(kNotEqual, slow_path->GetEntryLabel());
4235 __ Bind(&do_put);
4236 } else {
4237 __ j(kNotEqual, slow_path->GetEntryLabel());
4238 }
4239 }
4240
4241 if (kPoisonHeapReferences) {
4242 __ movl(temp, register_value);
4243 __ PoisonHeapReference(temp);
4244 __ movl(address, temp);
4245 } else {
4246 __ movl(address, register_value);
4247 }
4248 if (!may_need_runtime_call) {
4249 codegen_->MaybeRecordImplicitNullCheck(instruction);
4250 }
4251
4252 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4253 codegen_->MarkGCCard(
4254 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4255 __ Bind(&done);
4256
4257 if (slow_path != nullptr) {
4258 __ Bind(slow_path->GetExitLabel());
4259 }
4260
4261 break;
4262 }
4263 case Primitive::kPrimInt: {
4264 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4265 Address address = index.IsConstant()
4266 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4267 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4268 if (value.IsRegister()) {
4269 __ movl(address, value.AsRegister<CpuRegister>());
4270 } else {
4271 DCHECK(value.IsConstant()) << value;
4272 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4273 __ movl(address, Immediate(v));
4274 }
4275 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004276 break;
4277 }
4278
4279 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004280 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4281 Address address = index.IsConstant()
4282 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4283 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4284 if (value.IsRegister()) {
4285 __ movq(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004286 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004287 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4288 DCHECK(IsInt<32>(v));
4289 int32_t v_32 = v;
4290 __ movq(address, Immediate(v_32));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004291 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004292 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004293 break;
4294 }
4295
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004296 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004297 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4298 Address address = index.IsConstant()
4299 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4300 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4301 DCHECK(value.IsFpuRegister());
4302 __ movss(address, value.AsFpuRegister<XmmRegister>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004303 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004304 break;
4305 }
4306
4307 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004308 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4309 Address address = index.IsConstant()
4310 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4311 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4312 DCHECK(value.IsFpuRegister());
4313 __ movsd(address, value.AsFpuRegister<XmmRegister>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004314 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004315 break;
4316 }
4317
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004318 case Primitive::kPrimVoid:
4319 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004320 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004321 }
4322}
4323
4324void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004325 LocationSummary* locations =
4326 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004327 locations->SetInAt(0, Location::RequiresRegister());
4328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004329}
4330
4331void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4332 LocationSummary* locations = instruction->GetLocations();
4333 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004334 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4335 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004336 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004337 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004338}
4339
4340void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004341 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4342 ? LocationSummary::kCallOnSlowPath
4343 : LocationSummary::kNoCall;
4344 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004345 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004346 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004347 if (instruction->HasUses()) {
4348 locations->SetOut(Location::SameAsFirstInput());
4349 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004350}
4351
4352void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4353 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004354 Location index_loc = locations->InAt(0);
4355 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004356 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004357 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004358
Mark Mendell99dbd682015-04-22 16:18:52 -04004359 if (length_loc.IsConstant()) {
4360 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4361 if (index_loc.IsConstant()) {
4362 // BCE will remove the bounds check if we are guarenteed to pass.
4363 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4364 if (index < 0 || index >= length) {
4365 codegen_->AddSlowPath(slow_path);
4366 __ jmp(slow_path->GetEntryLabel());
4367 } else {
4368 // Some optimization after BCE may have generated this, and we should not
4369 // generate a bounds check if it is a valid range.
4370 }
4371 return;
4372 }
4373
4374 // We have to reverse the jump condition because the length is the constant.
4375 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4376 __ cmpl(index_reg, Immediate(length));
4377 codegen_->AddSlowPath(slow_path);
4378 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004379 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004380 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4381 if (index_loc.IsConstant()) {
4382 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4383 __ cmpl(length, Immediate(value));
4384 } else {
4385 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4386 }
4387 codegen_->AddSlowPath(slow_path);
4388 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004389 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004390}
4391
4392void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4393 CpuRegister card,
4394 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004395 CpuRegister value,
4396 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004397 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004398 if (value_can_be_null) {
4399 __ testl(value, value);
4400 __ j(kEqual, &is_null);
4401 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004402 __ gs()->movq(card, Address::Absolute(
4403 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4404 __ movq(temp, object);
4405 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004406 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004407 if (value_can_be_null) {
4408 __ Bind(&is_null);
4409 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004410}
4411
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004412void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4413 temp->SetLocations(nullptr);
4414}
4415
4416void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
4417 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004418 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004419}
4420
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004421void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004422 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004423 LOG(FATAL) << "Unimplemented";
4424}
4425
4426void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004427 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4428}
4429
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004430void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4432}
4433
4434void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004435 HBasicBlock* block = instruction->GetBlock();
4436 if (block->GetLoopInformation() != nullptr) {
4437 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4438 // The back edge will generate the suspend check.
4439 return;
4440 }
4441 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4442 // The goto will generate the suspend check.
4443 return;
4444 }
4445 GenerateSuspendCheck(instruction, nullptr);
4446}
4447
4448void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4449 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004450 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004451 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4452 if (slow_path == nullptr) {
4453 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4454 instruction->SetSlowPath(slow_path);
4455 codegen_->AddSlowPath(slow_path);
4456 if (successor != nullptr) {
4457 DCHECK(successor->IsLoopHeader());
4458 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4459 }
4460 } else {
4461 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4462 }
4463
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004464 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004465 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004466 if (successor == nullptr) {
4467 __ j(kNotEqual, slow_path->GetEntryLabel());
4468 __ Bind(slow_path->GetReturnLabel());
4469 } else {
4470 __ j(kEqual, codegen_->GetLabelOf(successor));
4471 __ jmp(slow_path->GetEntryLabel());
4472 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004473}
4474
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004475X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4476 return codegen_->GetAssembler();
4477}
4478
4479void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004480 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004481 Location source = move->GetSource();
4482 Location destination = move->GetDestination();
4483
4484 if (source.IsRegister()) {
4485 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004486 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004487 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004488 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004489 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004490 } else {
4491 DCHECK(destination.IsDoubleStackSlot());
4492 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004493 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004494 }
4495 } else if (source.IsStackSlot()) {
4496 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004497 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004498 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004499 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004500 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004501 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004502 } else {
4503 DCHECK(destination.IsStackSlot());
4504 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4505 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4506 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004507 } else if (source.IsDoubleStackSlot()) {
4508 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004509 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004510 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004511 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004512 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4513 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004514 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004515 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004516 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4517 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4518 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004519 } else if (source.IsConstant()) {
4520 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004521 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4522 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004523 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004524 if (value == 0) {
4525 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4526 } else {
4527 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4528 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004529 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004530 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004531 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004532 }
4533 } else if (constant->IsLongConstant()) {
4534 int64_t value = constant->AsLongConstant()->GetValue();
4535 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004536 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004537 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004538 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004539 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004540 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004541 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004542 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004543 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004544 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004545 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4546 if (value == 0) {
4547 // easy FP 0.0.
4548 __ xorps(dest, dest);
4549 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004550 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004551 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004552 } else {
4553 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004554 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004555 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4556 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004557 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004558 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004559 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004560 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004561 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004562 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4563 if (value == 0) {
4564 __ xorpd(dest, dest);
4565 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004566 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004567 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004568 } else {
4569 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004570 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004571 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004572 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004573 } else if (source.IsFpuRegister()) {
4574 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004575 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004576 } else if (destination.IsStackSlot()) {
4577 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004578 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004579 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004580 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004581 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004582 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004583 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004584 }
4585}
4586
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004587void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004588 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004589 __ movl(Address(CpuRegister(RSP), mem), reg);
4590 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004591}
4592
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004593void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004594 ScratchRegisterScope ensure_scratch(
4595 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4596
4597 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4598 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4599 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4600 Address(CpuRegister(RSP), mem2 + stack_offset));
4601 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4602 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4603 CpuRegister(ensure_scratch.GetRegister()));
4604}
4605
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004606void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4607 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4608 __ movq(Address(CpuRegister(RSP), mem), reg);
4609 __ movq(reg, CpuRegister(TMP));
4610}
4611
4612void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4613 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004614 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004615
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004616 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4617 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4618 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4619 Address(CpuRegister(RSP), mem2 + stack_offset));
4620 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4621 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4622 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004623}
4624
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004625void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4626 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4627 __ movss(Address(CpuRegister(RSP), mem), reg);
4628 __ movd(reg, CpuRegister(TMP));
4629}
4630
4631void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4632 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4633 __ movsd(Address(CpuRegister(RSP), mem), reg);
4634 __ movd(reg, CpuRegister(TMP));
4635}
4636
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004637void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004638 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004639 Location source = move->GetSource();
4640 Location destination = move->GetDestination();
4641
4642 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004643 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004644 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004645 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004646 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004647 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004648 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004649 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4650 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004651 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004652 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004653 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004654 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4655 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004656 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004657 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4658 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4659 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004660 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004661 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004662 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004663 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004664 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004665 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004666 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004667 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004668 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004669 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004670 }
4671}
4672
4673
4674void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4675 __ pushq(CpuRegister(reg));
4676}
4677
4678
4679void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4680 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004681}
4682
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004683void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004684 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004685 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4686 Immediate(mirror::Class::kStatusInitialized));
4687 __ j(kLess, slow_path->GetEntryLabel());
4688 __ Bind(slow_path->GetExitLabel());
4689 // No need for memory fence, thanks to the X86_64 memory model.
4690}
4691
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004692void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004693 InvokeRuntimeCallingConvention calling_convention;
4694 CodeGenerator::CreateLoadClassLocationSummary(
4695 cls,
4696 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4697 Location::RegisterLocation(RAX));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004698}
4699
4700void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004701 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004702 if (cls->NeedsAccessCheck()) {
4703 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4704 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4705 cls,
4706 cls->GetDexPc(),
4707 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004708 return;
4709 }
4710
4711 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4712 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
4713 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004714 DCHECK(!cls->CanCallRuntime());
4715 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004716 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004717 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004718 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004719 __ movq(out, Address(
4720 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004721 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004722 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004723
Andreas Gampe85b62f22015-09-09 13:15:38 -07004724 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004725 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4726 codegen_->AddSlowPath(slow_path);
4727 __ testl(out, out);
4728 __ j(kEqual, slow_path->GetEntryLabel());
4729 if (cls->MustGenerateClinitCheck()) {
4730 GenerateClassInitializationCheck(slow_path, out);
4731 } else {
4732 __ Bind(slow_path->GetExitLabel());
4733 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004734 }
4735}
4736
4737void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4738 LocationSummary* locations =
4739 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4740 locations->SetInAt(0, Location::RequiresRegister());
4741 if (check->HasUses()) {
4742 locations->SetOut(Location::SameAsFirstInput());
4743 }
4744}
4745
4746void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004747 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004748 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004749 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004750 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004751 GenerateClassInitializationCheck(slow_path,
4752 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004753}
4754
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004755void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4756 LocationSummary* locations =
4757 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004758 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004759 locations->SetOut(Location::RequiresRegister());
4760}
4761
4762void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004763 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004764 codegen_->AddSlowPath(slow_path);
4765
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004766 LocationSummary* locations = load->GetLocations();
4767 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4768 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004769 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004770 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004771 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004772 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004773 __ testl(out, out);
4774 __ j(kEqual, slow_path->GetEntryLabel());
4775 __ Bind(slow_path->GetExitLabel());
4776}
4777
David Brazdilcb1c0552015-08-04 16:22:25 +01004778static Address GetExceptionTlsAddress() {
4779 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4780}
4781
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004782void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4783 LocationSummary* locations =
4784 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4785 locations->SetOut(Location::RequiresRegister());
4786}
4787
4788void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004789 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4790}
4791
4792void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4793 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4794}
4795
4796void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4797 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004798}
4799
4800void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4801 LocationSummary* locations =
4802 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4803 InvokeRuntimeCallingConvention calling_convention;
4804 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4805}
4806
4807void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004808 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4809 instruction,
4810 instruction->GetDexPc(),
4811 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004812}
4813
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004814void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004815 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4816 switch (instruction->GetTypeCheckKind()) {
4817 case TypeCheckKind::kExactCheck:
4818 case TypeCheckKind::kAbstractClassCheck:
4819 case TypeCheckKind::kClassHierarchyCheck:
4820 case TypeCheckKind::kArrayObjectCheck:
4821 call_kind = LocationSummary::kNoCall;
4822 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004823 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004824 case TypeCheckKind::kInterfaceCheck:
4825 call_kind = LocationSummary::kCall;
4826 break;
4827 case TypeCheckKind::kArrayCheck:
4828 call_kind = LocationSummary::kCallOnSlowPath;
4829 break;
4830 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004832 if (call_kind != LocationSummary::kCall) {
4833 locations->SetInAt(0, Location::RequiresRegister());
4834 locations->SetInAt(1, Location::Any());
4835 // Note that TypeCheckSlowPathX86_64 uses this register too.
4836 locations->SetOut(Location::RequiresRegister());
4837 } else {
4838 InvokeRuntimeCallingConvention calling_convention;
4839 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4840 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4841 locations->SetOut(Location::RegisterLocation(RAX));
4842 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004843}
4844
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004845void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004846 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004847 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004848 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004849 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004850 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004851 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4852 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4853 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004854 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004855 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004856
4857 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004858 // Avoid null check if we know obj is not null.
4859 if (instruction->MustDoNullCheck()) {
4860 __ testl(obj, obj);
4861 __ j(kEqual, &zero);
4862 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004863
Calin Juravle98893e12015-10-02 21:05:03 +01004864 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004865 // This is safe, as the register is caller-save, and the object must be in another
4866 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004867 CpuRegister target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4868 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004869 ? obj
4870 : out;
4871 __ movl(target, Address(obj, class_offset));
4872 __ MaybeUnpoisonHeapReference(target);
4873
4874 switch (instruction->GetTypeCheckKind()) {
4875 case TypeCheckKind::kExactCheck: {
4876 if (cls.IsRegister()) {
4877 __ cmpl(out, cls.AsRegister<CpuRegister>());
4878 } else {
4879 DCHECK(cls.IsStackSlot()) << cls;
4880 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4881 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004882 if (zero.IsLinked()) {
4883 // Classes must be equal for the instanceof to succeed.
4884 __ j(kNotEqual, &zero);
4885 __ movl(out, Immediate(1));
4886 __ jmp(&done);
4887 } else {
4888 __ setcc(kEqual, out);
4889 // setcc only sets the low byte.
4890 __ andl(out, Immediate(1));
4891 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004892 break;
4893 }
4894 case TypeCheckKind::kAbstractClassCheck: {
4895 // If the class is abstract, we eagerly fetch the super class of the
4896 // object to avoid doing a comparison we know will fail.
4897 NearLabel loop, success;
4898 __ Bind(&loop);
4899 __ movl(out, Address(out, super_offset));
4900 __ MaybeUnpoisonHeapReference(out);
4901 __ testl(out, out);
4902 // If `out` is null, we use it for the result, and jump to `done`.
4903 __ j(kEqual, &done);
4904 if (cls.IsRegister()) {
4905 __ cmpl(out, cls.AsRegister<CpuRegister>());
4906 } else {
4907 DCHECK(cls.IsStackSlot()) << cls;
4908 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4909 }
4910 __ j(kNotEqual, &loop);
4911 __ movl(out, Immediate(1));
4912 if (zero.IsLinked()) {
4913 __ jmp(&done);
4914 }
4915 break;
4916 }
4917 case TypeCheckKind::kClassHierarchyCheck: {
4918 // Walk over the class hierarchy to find a match.
4919 NearLabel loop, success;
4920 __ Bind(&loop);
4921 if (cls.IsRegister()) {
4922 __ cmpl(out, cls.AsRegister<CpuRegister>());
4923 } else {
4924 DCHECK(cls.IsStackSlot()) << cls;
4925 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4926 }
4927 __ j(kEqual, &success);
4928 __ movl(out, Address(out, super_offset));
4929 __ MaybeUnpoisonHeapReference(out);
4930 __ testl(out, out);
4931 __ j(kNotEqual, &loop);
4932 // If `out` is null, we use it for the result, and jump to `done`.
4933 __ jmp(&done);
4934 __ Bind(&success);
4935 __ movl(out, Immediate(1));
4936 if (zero.IsLinked()) {
4937 __ jmp(&done);
4938 }
4939 break;
4940 }
4941 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004942 // Do an exact check.
4943 NearLabel exact_check;
4944 if (cls.IsRegister()) {
4945 __ cmpl(out, cls.AsRegister<CpuRegister>());
4946 } else {
4947 DCHECK(cls.IsStackSlot()) << cls;
4948 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4949 }
4950 __ j(kEqual, &exact_check);
4951 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004952 __ movl(out, Address(out, component_offset));
4953 __ MaybeUnpoisonHeapReference(out);
4954 __ testl(out, out);
4955 // If `out` is null, we use it for the result, and jump to `done`.
4956 __ j(kEqual, &done);
4957 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
4958 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004959 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004960 __ movl(out, Immediate(1));
4961 __ jmp(&done);
4962 break;
4963 }
4964 case TypeCheckKind::kArrayCheck: {
4965 if (cls.IsRegister()) {
4966 __ cmpl(out, cls.AsRegister<CpuRegister>());
4967 } else {
4968 DCHECK(cls.IsStackSlot()) << cls;
4969 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4970 }
4971 DCHECK(locations->OnlyCallsOnSlowPath());
4972 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
4973 instruction, /* is_fatal */ false);
4974 codegen_->AddSlowPath(slow_path);
4975 __ j(kNotEqual, slow_path->GetEntryLabel());
4976 __ movl(out, Immediate(1));
4977 if (zero.IsLinked()) {
4978 __ jmp(&done);
4979 }
4980 break;
4981 }
Calin Juravle98893e12015-10-02 21:05:03 +01004982 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004983 case TypeCheckKind::kInterfaceCheck:
4984 default: {
4985 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4986 instruction,
4987 instruction->GetDexPc(),
4988 nullptr);
4989 if (zero.IsLinked()) {
4990 __ jmp(&done);
4991 }
4992 break;
4993 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004994 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004995
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004996 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004997 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004998 __ xorl(out, out);
4999 }
5000
5001 if (done.IsLinked()) {
5002 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005003 }
5004
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005005 if (slow_path != nullptr) {
5006 __ Bind(slow_path->GetExitLabel());
5007 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005008}
5009
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005010void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005011 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5012 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5013
5014 switch (instruction->GetTypeCheckKind()) {
5015 case TypeCheckKind::kExactCheck:
5016 case TypeCheckKind::kAbstractClassCheck:
5017 case TypeCheckKind::kClassHierarchyCheck:
5018 case TypeCheckKind::kArrayObjectCheck:
5019 call_kind = throws_into_catch
5020 ? LocationSummary::kCallOnSlowPath
5021 : LocationSummary::kNoCall;
5022 break;
Calin Juravle98893e12015-10-02 21:05:03 +01005023 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005024 case TypeCheckKind::kInterfaceCheck:
5025 call_kind = LocationSummary::kCall;
5026 break;
5027 case TypeCheckKind::kArrayCheck:
5028 call_kind = LocationSummary::kCallOnSlowPath;
5029 break;
5030 }
5031
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005032 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005033 instruction, call_kind);
5034 if (call_kind != LocationSummary::kCall) {
5035 locations->SetInAt(0, Location::RequiresRegister());
5036 locations->SetInAt(1, Location::Any());
5037 // Note that TypeCheckSlowPathX86_64 uses this register too.
5038 locations->AddTemp(Location::RequiresRegister());
5039 } else {
5040 InvokeRuntimeCallingConvention calling_convention;
5041 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5042 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5043 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005044}
5045
5046void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
5047 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005048 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005049 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005050 CpuRegister temp = locations->WillCall()
5051 ? CpuRegister(kNoRegister)
5052 : locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005053
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005054 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5055 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5056 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5057 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5058 SlowPathCode* slow_path = nullptr;
5059
5060 if (!locations->WillCall()) {
5061 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
5062 instruction, !locations->CanCall());
5063 codegen_->AddSlowPath(slow_path);
5064 }
5065
5066 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005067 // Avoid null check if we know obj is not null.
5068 if (instruction->MustDoNullCheck()) {
5069 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005070 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005071 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005072
5073 if (locations->WillCall()) {
5074 __ movl(obj, Address(obj, class_offset));
5075 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005076 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005077 __ movl(temp, Address(obj, class_offset));
5078 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005079 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005080
5081 switch (instruction->GetTypeCheckKind()) {
5082 case TypeCheckKind::kExactCheck:
5083 case TypeCheckKind::kArrayCheck: {
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 // Jump to slow path for throwing the exception or doing a
5091 // more involved array check.
5092 __ j(kNotEqual, slow_path->GetEntryLabel());
5093 break;
5094 }
5095 case TypeCheckKind::kAbstractClassCheck: {
5096 // If the class is abstract, we eagerly fetch the super class of the
5097 // object to avoid doing a comparison we know will fail.
5098 NearLabel loop;
5099 __ Bind(&loop);
5100 __ movl(temp, Address(temp, super_offset));
5101 __ MaybeUnpoisonHeapReference(temp);
5102 __ testl(temp, temp);
5103 // Jump to the slow path to throw the exception.
5104 __ j(kEqual, slow_path->GetEntryLabel());
5105 if (cls.IsRegister()) {
5106 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5107 } else {
5108 DCHECK(cls.IsStackSlot()) << cls;
5109 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5110 }
5111 __ j(kNotEqual, &loop);
5112 break;
5113 }
5114 case TypeCheckKind::kClassHierarchyCheck: {
5115 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005116 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005117 __ Bind(&loop);
5118 if (cls.IsRegister()) {
5119 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5120 } else {
5121 DCHECK(cls.IsStackSlot()) << cls;
5122 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5123 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005124 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005125 __ movl(temp, Address(temp, super_offset));
5126 __ MaybeUnpoisonHeapReference(temp);
5127 __ testl(temp, temp);
5128 __ j(kNotEqual, &loop);
5129 // Jump to the slow path to throw the exception.
5130 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005131 break;
5132 }
5133 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005134 // Do an exact check.
5135 if (cls.IsRegister()) {
5136 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5137 } else {
5138 DCHECK(cls.IsStackSlot()) << cls;
5139 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5140 }
5141 __ j(kEqual, &done);
5142 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005143 __ movl(temp, Address(temp, component_offset));
5144 __ MaybeUnpoisonHeapReference(temp);
5145 __ testl(temp, temp);
5146 __ j(kEqual, slow_path->GetEntryLabel());
5147 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5148 __ j(kNotEqual, slow_path->GetEntryLabel());
5149 break;
5150 }
Calin Juravle98893e12015-10-02 21:05:03 +01005151 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005152 case TypeCheckKind::kInterfaceCheck:
5153 default:
5154 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5155 instruction,
5156 instruction->GetDexPc(),
5157 nullptr);
5158 break;
5159 }
5160 __ Bind(&done);
5161
5162 if (slow_path != nullptr) {
5163 __ Bind(slow_path->GetExitLabel());
5164 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005165}
5166
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005167void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5168 LocationSummary* locations =
5169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5170 InvokeRuntimeCallingConvention calling_convention;
5171 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5172}
5173
5174void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005175 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5176 : QUICK_ENTRY_POINT(pUnlockObject),
5177 instruction,
5178 instruction->GetDexPc(),
5179 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005180}
5181
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005182void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5183void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5184void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5185
5186void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5187 LocationSummary* locations =
5188 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5189 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5190 || instruction->GetResultType() == Primitive::kPrimLong);
5191 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005192 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005193 locations->SetOut(Location::SameAsFirstInput());
5194}
5195
5196void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5197 HandleBitwiseOperation(instruction);
5198}
5199
5200void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5201 HandleBitwiseOperation(instruction);
5202}
5203
5204void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5205 HandleBitwiseOperation(instruction);
5206}
5207
5208void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5209 LocationSummary* locations = instruction->GetLocations();
5210 Location first = locations->InAt(0);
5211 Location second = locations->InAt(1);
5212 DCHECK(first.Equals(locations->Out()));
5213
5214 if (instruction->GetResultType() == Primitive::kPrimInt) {
5215 if (second.IsRegister()) {
5216 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005217 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005218 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005219 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005220 } else {
5221 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005222 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005223 }
5224 } else if (second.IsConstant()) {
5225 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5226 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005227 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005228 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005229 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005230 } else {
5231 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005232 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005233 }
5234 } else {
5235 Address address(CpuRegister(RSP), second.GetStackIndex());
5236 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005237 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005238 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005239 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005240 } else {
5241 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005242 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005243 }
5244 }
5245 } else {
5246 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005247 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5248 bool second_is_constant = false;
5249 int64_t value = 0;
5250 if (second.IsConstant()) {
5251 second_is_constant = true;
5252 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005253 }
Mark Mendell40741f32015-04-20 22:10:34 -04005254 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005255
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005256 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005257 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005258 if (is_int32_value) {
5259 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5260 } else {
5261 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5262 }
5263 } else if (second.IsDoubleStackSlot()) {
5264 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005265 } else {
5266 __ andq(first_reg, second.AsRegister<CpuRegister>());
5267 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005268 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005269 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005270 if (is_int32_value) {
5271 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5272 } else {
5273 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5274 }
5275 } else if (second.IsDoubleStackSlot()) {
5276 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005277 } else {
5278 __ orq(first_reg, second.AsRegister<CpuRegister>());
5279 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005280 } else {
5281 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005282 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005283 if (is_int32_value) {
5284 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5285 } else {
5286 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5287 }
5288 } else if (second.IsDoubleStackSlot()) {
5289 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005290 } else {
5291 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5292 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005293 }
5294 }
5295}
5296
Calin Juravleb1498f62015-02-16 13:13:29 +00005297void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction) {
5298 // Nothing to do, this should be removed during prepare for register allocator.
5299 UNUSED(instruction);
5300 LOG(FATAL) << "Unreachable";
5301}
5302
5303void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction) {
5304 // Nothing to do, this should be removed during prepare for register allocator.
5305 UNUSED(instruction);
5306 LOG(FATAL) << "Unreachable";
5307}
5308
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005309void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
5310 DCHECK(codegen_->IsBaseline());
5311 LocationSummary* locations =
5312 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5313 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5314}
5315
5316void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5317 DCHECK(codegen_->IsBaseline());
5318 // Will be generated at use site.
5319}
5320
Mark Mendellfe57faa2015-09-18 09:26:15 -04005321// Simple implementation of packed switch - generate cascaded compare/jumps.
5322void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5323 LocationSummary* locations =
5324 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5325 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04005326 locations->AddTemp(Location::RequiresRegister());
5327 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04005328}
5329
5330void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5331 int32_t lower_bound = switch_instr->GetStartValue();
5332 int32_t num_entries = switch_instr->GetNumEntries();
5333 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04005334 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
5335 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
5336 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
5337
5338 // Remove the bias, if needed.
5339 Register value_reg_out = value_reg_in.AsRegister();
5340 if (lower_bound != 0) {
5341 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
5342 value_reg_out = temp_reg.AsRegister();
5343 }
5344 CpuRegister value_reg(value_reg_out);
5345
5346 // Is the value in range?
Mark Mendellfe57faa2015-09-18 09:26:15 -04005347 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
Mark Mendell9c86b482015-09-18 13:36:07 -04005348 __ cmpl(value_reg, Immediate(num_entries - 1));
5349 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005350
Mark Mendell9c86b482015-09-18 13:36:07 -04005351 // We are in the range of the table.
5352 // Load the address of the jump table in the constant area.
5353 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005354
Mark Mendell9c86b482015-09-18 13:36:07 -04005355 // Load the (signed) offset from the jump table.
5356 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
5357
5358 // Add the offset to the address of the table base.
5359 __ addq(temp_reg, base_reg);
5360
5361 // And jump.
5362 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005363}
5364
Mark Mendell92e83bf2015-05-07 11:25:03 -04005365void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
5366 if (value == 0) {
5367 __ xorl(dest, dest);
5368 } else if (value > 0 && IsInt<32>(value)) {
5369 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
5370 __ movl(dest, Immediate(static_cast<int32_t>(value)));
5371 } else {
5372 __ movq(dest, Immediate(value));
5373 }
5374}
5375
Mark Mendellcfa410b2015-05-25 16:02:44 -04005376void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
5377 DCHECK(dest.IsDoubleStackSlot());
5378 if (IsInt<32>(value)) {
5379 // Can move directly as an int32 constant.
5380 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
5381 Immediate(static_cast<int32_t>(value)));
5382 } else {
5383 Load64BitValue(CpuRegister(TMP), value);
5384 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
5385 }
5386}
5387
Mark Mendell9c86b482015-09-18 13:36:07 -04005388/**
5389 * Class to handle late fixup of offsets into constant area.
5390 */
5391class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
5392 public:
5393 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
5394 : codegen_(&codegen), offset_into_constant_area_(offset) {}
5395
5396 protected:
5397 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
5398
5399 CodeGeneratorX86_64* codegen_;
5400
5401 private:
5402 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5403 // Patch the correct offset for the instruction. We use the address of the
5404 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
5405 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
5406 int32_t relative_position = constant_offset - pos;
5407
5408 // Patch in the right value.
5409 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5410 }
5411
5412 // Location in constant area that the fixup refers to.
5413 size_t offset_into_constant_area_;
5414};
5415
5416/**
5417 t * Class to handle late fixup of offsets to a jump table that will be created in the
5418 * constant area.
5419 */
5420class JumpTableRIPFixup : public RIPFixup {
5421 public:
5422 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
5423 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
5424
5425 void CreateJumpTable() {
5426 X86_64Assembler* assembler = codegen_->GetAssembler();
5427
5428 // Ensure that the reference to the jump table has the correct offset.
5429 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
5430 SetOffset(offset_in_constant_table);
5431
5432 // Compute the offset from the start of the function to this jump table.
5433 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
5434
5435 // Populate the jump table with the correct values for the jump table.
5436 int32_t num_entries = switch_instr_->GetNumEntries();
5437 HBasicBlock* block = switch_instr_->GetBlock();
5438 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
5439 // The value that we want is the target offset - the position of the table.
5440 for (int32_t i = 0; i < num_entries; i++) {
5441 HBasicBlock* b = successors[i];
5442 Label* l = codegen_->GetLabelOf(b);
5443 DCHECK(l->IsBound());
5444 int32_t offset_to_block = l->Position() - current_table_offset;
5445 assembler->AppendInt32(offset_to_block);
5446 }
5447 }
5448
5449 private:
5450 const HPackedSwitch* switch_instr_;
5451};
5452
Mark Mendellf55c3e02015-03-26 21:07:46 -04005453void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
5454 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04005455 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04005456 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
5457 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8 byte values.
Mark Mendell39dcf552015-04-09 20:42:42 -04005458 assembler->Align(4, 0);
5459 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04005460
5461 // Populate any jump tables.
5462 for (auto jump_table : fixups_to_jump_tables_) {
5463 jump_table->CreateJumpTable();
5464 }
5465
5466 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04005467 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04005468 }
5469
5470 // And finish up.
5471 CodeGenerator::Finalize(allocator);
5472}
5473
Mark Mendellf55c3e02015-03-26 21:07:46 -04005474Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
5475 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5476 return Address::RIP(fixup);
5477}
5478
5479Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
5480 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5481 return Address::RIP(fixup);
5482}
5483
5484Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
5485 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5486 return Address::RIP(fixup);
5487}
5488
5489Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
5490 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5491 return Address::RIP(fixup);
5492}
5493
Andreas Gampe85b62f22015-09-09 13:15:38 -07005494// TODO: trg as memory.
5495void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5496 if (!trg.IsValid()) {
5497 DCHECK(type == Primitive::kPrimVoid);
5498 return;
5499 }
5500
5501 DCHECK_NE(type, Primitive::kPrimVoid);
5502
5503 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
5504 if (trg.Equals(return_loc)) {
5505 return;
5506 }
5507
5508 // Let the parallel move resolver take care of all of this.
5509 HParallelMove parallel_move(GetGraph()->GetArena());
5510 parallel_move.AddMove(return_loc, trg, type, nullptr);
5511 GetMoveResolver()->EmitNativeCode(&parallel_move);
5512}
5513
Mark Mendell9c86b482015-09-18 13:36:07 -04005514Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
5515 // Create a fixup to be used to create and address the jump table.
5516 JumpTableRIPFixup* table_fixup =
5517 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
5518
5519 // We have to populate the jump tables.
5520 fixups_to_jump_tables_.push_back(table_fixup);
5521 return Address::RIP(table_fixup);
5522}
5523
Roland Levillain4d027112015-07-01 15:41:14 +01005524#undef __
5525
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005526} // namespace x86_64
5527} // namespace art