blob: b5abec24d186d980330624cfaceedc0399368e2d [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;
Aart Bike9f37602015-10-09 11:15:55 -0700452 case kCondB: return kBelow;
453 case kCondBE: return kBelowEqual;
454 case kCondA: return kAbove;
455 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700456 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100457 LOG(FATAL) << "Unreachable";
458 UNREACHABLE();
459}
460
Aart Bike9f37602015-10-09 11:15:55 -0700461// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100462inline Condition X86_64FPCondition(IfCondition cond) {
463 switch (cond) {
464 case kCondEQ: return kEqual;
465 case kCondNE: return kNotEqual;
466 case kCondLT: return kBelow;
467 case kCondLE: return kBelowEqual;
468 case kCondGT: return kAbove;
469 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700470 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100471 };
472 LOG(FATAL) << "Unreachable";
473 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700474}
475
Vladimir Markodc151b22015-10-15 18:02:30 +0100476HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
477 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
478 MethodReference target_method ATTRIBUTE_UNUSED) {
479 switch (desired_dispatch_info.code_ptr_location) {
480 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
481 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
482 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
483 return HInvokeStaticOrDirect::DispatchInfo {
484 desired_dispatch_info.method_load_kind,
485 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
486 desired_dispatch_info.method_load_data,
487 0u
488 };
489 default:
490 return desired_dispatch_info;
491 }
492}
493
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800494void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100495 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800496 // All registers are assumed to be correctly set up.
497
Vladimir Marko58155012015-08-19 12:49:41 +0000498 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
499 switch (invoke->GetMethodLoadKind()) {
500 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
501 // temp = thread->string_init_entrypoint
502 __ gs()->movl(temp.AsRegister<CpuRegister>(),
503 Address::Absolute(invoke->GetStringInitOffset(), true));
504 break;
505 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
506 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
507 break;
508 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
509 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
510 break;
511 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
512 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
513 method_patches_.emplace_back(invoke->GetTargetMethod());
514 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
515 break;
516 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
517 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
518 invoke->GetDexCacheArrayOffset());
519 __ movq(temp.AsRegister<CpuRegister>(),
520 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
521 // Bind the label at the end of the "movl" insn.
522 __ Bind(&pc_rel_dex_cache_patches_.back().label);
523 break;
524 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
525 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
526 Register method_reg;
527 CpuRegister reg = temp.AsRegister<CpuRegister>();
528 if (current_method.IsRegister()) {
529 method_reg = current_method.AsRegister<Register>();
530 } else {
531 DCHECK(invoke->GetLocations()->Intrinsified());
532 DCHECK(!current_method.IsValid());
533 method_reg = reg.AsRegister();
534 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
535 }
536 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100537 __ movq(reg,
538 Address(CpuRegister(method_reg),
539 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000540 // temp = temp[index_in_cache]
541 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
542 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
543 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100544 }
Vladimir Marko58155012015-08-19 12:49:41 +0000545 }
546
547 switch (invoke->GetCodePtrLocation()) {
548 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
549 __ call(&frame_entry_label_);
550 break;
551 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
552 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
553 Label* label = &relative_call_patches_.back().label;
554 __ call(label); // Bind to the patch label, override at link time.
555 __ Bind(label); // Bind the label at the end of the "call" insn.
556 break;
557 }
558 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
559 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100560 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
561 LOG(FATAL) << "Unsupported";
562 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000563 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
564 // (callee_method + offset_of_quick_compiled_code)()
565 __ call(Address(callee_method.AsRegister<CpuRegister>(),
566 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
567 kX86_64WordSize).SizeValue()));
568 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000569 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800570
571 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800572}
573
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000574void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
575 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
576 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
577 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
578 LocationSummary* locations = invoke->GetLocations();
579 Location receiver = locations->InAt(0);
580 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
581 // temp = object->GetClass();
582 DCHECK(receiver.IsRegister());
583 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
584 MaybeRecordImplicitNullCheck(invoke);
585 __ MaybeUnpoisonHeapReference(temp);
586 // temp = temp->GetMethodAt(method_offset);
587 __ movq(temp, Address(temp, method_offset));
588 // call temp->GetEntryPoint();
589 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
590 kX86_64WordSize).SizeValue()));
591}
592
Vladimir Marko58155012015-08-19 12:49:41 +0000593void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
594 DCHECK(linker_patches->empty());
595 size_t size =
596 method_patches_.size() + relative_call_patches_.size() + pc_rel_dex_cache_patches_.size();
597 linker_patches->reserve(size);
598 for (const MethodPatchInfo<Label>& info : method_patches_) {
599 // The label points to the end of the "movl" instruction but the literal offset for method
600 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
601 uint32_t literal_offset = info.label.Position() - 4;
602 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
603 info.target_method.dex_file,
604 info.target_method.dex_method_index));
605 }
606 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
607 // The label points to the end of the "call" instruction but the literal offset for method
608 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
609 uint32_t literal_offset = info.label.Position() - 4;
610 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
611 info.target_method.dex_file,
612 info.target_method.dex_method_index));
613 }
614 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
615 // The label points to the end of the "mov" instruction but the literal offset for method
616 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
617 uint32_t literal_offset = info.label.Position() - 4;
618 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
619 &info.target_dex_file,
620 info.label.Position(),
621 info.element_offset));
622 }
623}
624
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100625void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100626 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100627}
628
629void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100630 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100631}
632
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100633size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
634 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
635 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100636}
637
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100638size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
639 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
640 return kX86_64WordSize;
641}
642
643size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
644 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
645 return kX86_64WordSize;
646}
647
648size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
649 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
650 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100651}
652
Calin Juravle175dc732015-08-25 15:42:32 +0100653void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
654 HInstruction* instruction,
655 uint32_t dex_pc,
656 SlowPathCode* slow_path) {
657 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
658 instruction,
659 dex_pc,
660 slow_path);
661}
662
663void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100664 HInstruction* instruction,
665 uint32_t dex_pc,
666 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100667 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100668 __ gs()->call(Address::Absolute(entry_point_offset, true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100669 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100670}
671
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000672static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000673// Use a fake return address register to mimic Quick.
674static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400675CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
676 const X86_64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100677 const CompilerOptions& compiler_options,
678 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000679 : CodeGenerator(graph,
680 kNumberOfCpuRegisters,
681 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000682 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000683 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
684 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000685 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000686 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
687 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100688 compiler_options,
689 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100690 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100691 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000692 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400693 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400694 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000695 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100696 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
697 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -0400698 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
699 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000700 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
701}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100702
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100703InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
704 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100705 : HGraphVisitor(graph),
706 assembler_(codegen->GetAssembler()),
707 codegen_(codegen) {}
708
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100709Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100710 switch (type) {
711 case Primitive::kPrimLong:
712 case Primitive::kPrimByte:
713 case Primitive::kPrimBoolean:
714 case Primitive::kPrimChar:
715 case Primitive::kPrimShort:
716 case Primitive::kPrimInt:
717 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100718 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100719 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100720 }
721
722 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100724 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100725 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100726 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100727
728 case Primitive::kPrimVoid:
729 LOG(FATAL) << "Unreachable type " << type;
730 }
731
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100732 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100733}
734
Nicolas Geoffray98893962015-01-21 12:32:32 +0000735void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100736 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100737 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100738
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000739 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100740 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000741
Nicolas Geoffray98893962015-01-21 12:32:32 +0000742 if (is_baseline) {
743 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
744 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
745 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000746 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
747 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
748 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000749 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100750}
751
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100752static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100753 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100754}
David Srbecky9d8606d2015-04-12 09:35:32 +0100755
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100756static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100757 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100758}
759
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100760void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100761 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000762 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100763 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700764 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000765 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100766
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000767 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100768 __ testq(CpuRegister(RAX), Address(
769 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100770 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100771 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000772
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000773 if (HasEmptyFrame()) {
774 return;
775 }
776
Nicolas Geoffray98893962015-01-21 12:32:32 +0000777 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000778 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000779 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000780 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100781 __ cfi().AdjustCFAOffset(kX86_64WordSize);
782 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000783 }
784 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100785
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100786 int adjust = GetFrameSize() - GetCoreSpillSize();
787 __ subq(CpuRegister(RSP), Immediate(adjust));
788 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000789 uint32_t xmm_spill_location = GetFpuSpillStart();
790 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100791
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000792 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
793 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100794 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
795 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
796 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000797 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100798 }
799
Mathieu Chartiere401d142015-04-22 13:56:20 -0700800 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100801 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100802}
803
804void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100805 __ cfi().RememberState();
806 if (!HasEmptyFrame()) {
807 uint32_t xmm_spill_location = GetFpuSpillStart();
808 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
809 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
810 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
811 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
812 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
813 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
814 }
815 }
816
817 int adjust = GetFrameSize() - GetCoreSpillSize();
818 __ addq(CpuRegister(RSP), Immediate(adjust));
819 __ cfi().AdjustCFAOffset(-adjust);
820
821 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
822 Register reg = kCoreCalleeSaves[i];
823 if (allocated_registers_.ContainsCoreRegister(reg)) {
824 __ popq(CpuRegister(reg));
825 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
826 __ cfi().Restore(DWARFReg(reg));
827 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000828 }
829 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100830 __ ret();
831 __ cfi().RestoreState();
832 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100833}
834
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100835void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
836 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100837}
838
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100839Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
840 switch (load->GetType()) {
841 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100842 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100843 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100844
845 case Primitive::kPrimInt:
846 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100847 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100848 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100849
850 case Primitive::kPrimBoolean:
851 case Primitive::kPrimByte:
852 case Primitive::kPrimChar:
853 case Primitive::kPrimShort:
854 case Primitive::kPrimVoid:
855 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700856 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100857 }
858
859 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700860 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100861}
862
863void CodeGeneratorX86_64::Move(Location destination, Location source) {
864 if (source.Equals(destination)) {
865 return;
866 }
867 if (destination.IsRegister()) {
868 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000869 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100870 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000871 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100872 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000873 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100874 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100875 } else {
876 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000877 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100878 Address(CpuRegister(RSP), source.GetStackIndex()));
879 }
880 } else if (destination.IsFpuRegister()) {
881 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000882 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100883 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000884 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100885 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000886 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 Address(CpuRegister(RSP), source.GetStackIndex()));
888 } else {
889 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000890 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100891 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100892 }
893 } else if (destination.IsStackSlot()) {
894 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100895 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000896 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100897 } else if (source.IsFpuRegister()) {
898 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000899 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500900 } else if (source.IsConstant()) {
901 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000902 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500903 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100904 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500905 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000906 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
907 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100908 }
909 } else {
910 DCHECK(destination.IsDoubleStackSlot());
911 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100912 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000913 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100914 } else if (source.IsFpuRegister()) {
915 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000916 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500917 } else if (source.IsConstant()) {
918 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800919 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500920 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000921 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500922 } else {
923 DCHECK(constant->IsLongConstant());
924 value = constant->AsLongConstant()->GetValue();
925 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400926 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927 } else {
928 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000929 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
930 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100931 }
932 }
933}
934
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100935void CodeGeneratorX86_64::Move(HInstruction* instruction,
936 Location location,
937 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000938 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100939 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700940 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100941 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000942 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100943 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000944 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000945 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
946 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000947 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000948 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000949 } else if (location.IsStackSlot()) {
950 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
951 } else {
952 DCHECK(location.IsConstant());
953 DCHECK_EQ(location.GetConstant(), const_to_move);
954 }
955 } else if (const_to_move->IsLongConstant()) {
956 int64_t value = const_to_move->AsLongConstant()->GetValue();
957 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400958 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000959 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400960 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000961 } else {
962 DCHECK(location.IsConstant());
963 DCHECK_EQ(location.GetConstant(), const_to_move);
964 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100965 }
Roland Levillain476df552014-10-09 17:51:36 +0100966 } else if (instruction->IsLoadLocal()) {
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:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100974 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100975 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
976 break;
977
978 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100979 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000980 Move(location,
981 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100982 break;
983
984 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100985 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100986 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000987 } else if (instruction->IsTemporary()) {
988 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
989 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100990 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100991 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100992 switch (instruction->GetType()) {
993 case Primitive::kPrimBoolean:
994 case Primitive::kPrimByte:
995 case Primitive::kPrimChar:
996 case Primitive::kPrimShort:
997 case Primitive::kPrimInt:
998 case Primitive::kPrimNot:
999 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001000 case Primitive::kPrimFloat:
1001 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001002 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001003 break;
1004
1005 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001007 }
1008 }
1009}
1010
Calin Juravle175dc732015-08-25 15:42:32 +01001011void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1012 DCHECK(location.IsRegister());
1013 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1014}
1015
Calin Juravlee460d1d2015-09-29 04:52:17 +01001016void CodeGeneratorX86_64::MoveLocation(
1017 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1018 Move(dst, src);
1019}
1020
1021void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1022 if (location.IsRegister()) {
1023 locations->AddTemp(location);
1024 } else {
1025 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1026 }
1027}
1028
David Brazdilfc6a86a2015-06-26 10:33:45 +00001029void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001030 DCHECK(!successor->IsExitBlock());
1031
1032 HBasicBlock* block = got->GetBlock();
1033 HInstruction* previous = got->GetPrevious();
1034
1035 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001036 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001037 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1038 return;
1039 }
1040
1041 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1042 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1043 }
1044 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001045 __ jmp(codegen_->GetLabelOf(successor));
1046 }
1047}
1048
David Brazdilfc6a86a2015-06-26 10:33:45 +00001049void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1050 got->SetLocations(nullptr);
1051}
1052
1053void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1054 HandleGoto(got, got->GetSuccessor());
1055}
1056
1057void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1058 try_boundary->SetLocations(nullptr);
1059}
1060
1061void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1062 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1063 if (!successor->IsExitBlock()) {
1064 HandleGoto(try_boundary, successor);
1065 }
1066}
1067
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1069 exit->SetLocations(nullptr);
1070}
1071
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001072void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001073}
1074
Mark Mendellc4701932015-04-10 13:18:51 -04001075void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
1076 Label* true_label,
1077 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001078 if (cond->IsFPConditionTrueIfNaN()) {
1079 __ j(kUnordered, true_label);
1080 } else if (cond->IsFPConditionFalseIfNaN()) {
1081 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001082 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001083 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001084}
1085
1086void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
1087 HCondition* condition,
1088 Label* true_target,
1089 Label* false_target,
1090 Label* always_true_target) {
1091 LocationSummary* locations = condition->GetLocations();
1092 Location left = locations->InAt(0);
1093 Location right = locations->InAt(1);
1094
1095 // We don't want true_target as a nullptr.
1096 if (true_target == nullptr) {
1097 true_target = always_true_target;
1098 }
1099 bool falls_through = (false_target == nullptr);
1100
1101 // FP compares don't like null false_targets.
1102 if (false_target == nullptr) {
1103 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1104 }
1105
1106 Primitive::Type type = condition->InputAt(0)->GetType();
1107 switch (type) {
1108 case Primitive::kPrimLong: {
1109 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1110 if (right.IsConstant()) {
1111 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1112 if (IsInt<32>(value)) {
1113 if (value == 0) {
1114 __ testq(left_reg, left_reg);
1115 } else {
1116 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1117 }
1118 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001119 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001120 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1121 }
1122 } else if (right.IsDoubleStackSlot()) {
1123 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1124 } else {
1125 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1126 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001127 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001128 break;
1129 }
1130 case Primitive::kPrimFloat: {
1131 if (right.IsFpuRegister()) {
1132 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1133 } else if (right.IsConstant()) {
1134 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1135 codegen_->LiteralFloatAddress(
1136 right.GetConstant()->AsFloatConstant()->GetValue()));
1137 } else {
1138 DCHECK(right.IsStackSlot());
1139 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1140 Address(CpuRegister(RSP), right.GetStackIndex()));
1141 }
1142 GenerateFPJumps(condition, true_target, false_target);
1143 break;
1144 }
1145 case Primitive::kPrimDouble: {
1146 if (right.IsFpuRegister()) {
1147 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1148 } else if (right.IsConstant()) {
1149 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1150 codegen_->LiteralDoubleAddress(
1151 right.GetConstant()->AsDoubleConstant()->GetValue()));
1152 } else {
1153 DCHECK(right.IsDoubleStackSlot());
1154 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1155 Address(CpuRegister(RSP), right.GetStackIndex()));
1156 }
1157 GenerateFPJumps(condition, true_target, false_target);
1158 break;
1159 }
1160 default:
1161 LOG(FATAL) << "Unexpected condition type " << type;
1162 }
1163
1164 if (!falls_through) {
1165 __ jmp(false_target);
1166 }
1167}
1168
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001169void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1170 Label* true_target,
1171 Label* false_target,
1172 Label* always_true_target) {
1173 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001174 if (cond->IsIntConstant()) {
1175 // Constant condition, statically compared against 1.
1176 int32_t cond_value = cond->AsIntConstant()->GetValue();
1177 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001178 if (always_true_target != nullptr) {
1179 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001180 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001181 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001183 DCHECK_EQ(cond_value, 0);
1184 }
1185 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001186 HCondition* condition = cond->AsCondition();
1187 bool is_materialized = condition == nullptr || condition->NeedsMaterialization();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001188 // Moves do not affect the eflags register, so if the condition is
1189 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001190 // again. We can't use the eflags on FP conditions if they are
1191 // materialized due to the complex branching.
Mark Mendellb8b97692015-05-22 16:58:19 -04001192 Primitive::Type type = (condition != nullptr)
1193 ? cond->InputAt(0)->GetType()
1194 : Primitive::kPrimInt;
1195 bool eflags_set = condition != nullptr
1196 && condition->IsBeforeWhenDisregardMoves(instruction)
Mark Mendellc4701932015-04-10 13:18:51 -04001197 && !Primitive::IsFloatingPointType(type);
Mark Mendellb8b97692015-05-22 16:58:19 -04001198 // Can we optimize the jump if we know that the next block is the true case?
1199 bool can_jump_to_false = CanReverseCondition(always_true_target, false_target, condition);
Mark Mendellc4701932015-04-10 13:18:51 -04001200
Roland Levillain4fa13f62015-07-06 18:11:54 +01001201 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001202 if (!eflags_set) {
1203 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001204 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001205 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001206 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001207 } else {
1208 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1209 Immediate(0));
1210 }
Mark Mendellb8b97692015-05-22 16:58:19 -04001211 if (can_jump_to_false) {
1212 __ j(kEqual, false_target);
1213 return;
1214 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001215 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001216 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001217 if (can_jump_to_false) {
1218 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1219 return;
1220 }
1221 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001222 }
1223 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001224 // Condition has not been materialized, use its inputs as the
1225 // comparison and its condition as the branch condition.
1226
Mark Mendellc4701932015-04-10 13:18:51 -04001227 // Is this a long or FP comparison that has been folded into the HCondition?
1228 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001229 // Generate the comparison directly.
Mark Mendellb8b97692015-05-22 16:58:19 -04001230 GenerateCompareTestAndBranch(instruction->AsIf(), condition,
Mark Mendellc4701932015-04-10 13:18:51 -04001231 true_target, false_target, always_true_target);
1232 return;
1233 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001234
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001235 Location lhs = cond->GetLocations()->InAt(0);
1236 Location rhs = cond->GetLocations()->InAt(1);
1237 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001238 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001239 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001240 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001241 if (constant == 0) {
1242 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1243 } else {
1244 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1245 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001246 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001247 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001248 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1249 }
Mark Mendellb8b97692015-05-22 16:58:19 -04001250
1251 if (can_jump_to_false) {
1252 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1253 return;
1254 }
1255
1256 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001257 }
Dave Allison20dfc792014-06-16 20:44:29 -07001258 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001259 if (false_target != nullptr) {
1260 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001261 }
1262}
1263
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001264void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1265 LocationSummary* locations =
1266 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1267 HInstruction* cond = if_instr->InputAt(0);
1268 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1269 locations->SetInAt(0, Location::Any());
1270 }
1271}
1272
1273void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1274 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1275 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1276 Label* always_true_target = true_target;
1277 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1278 if_instr->IfTrueSuccessor())) {
1279 always_true_target = nullptr;
1280 }
1281 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1282 if_instr->IfFalseSuccessor())) {
1283 false_target = nullptr;
1284 }
1285 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1286}
1287
1288void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1289 LocationSummary* locations = new (GetGraph()->GetArena())
1290 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1291 HInstruction* cond = deoptimize->InputAt(0);
Aart Bikbb245d12015-10-19 11:05:03 -07001292 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001293 locations->SetInAt(0, Location::Any());
1294 }
1295}
1296
1297void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001298 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001299 DeoptimizationSlowPathX86_64(deoptimize);
1300 codegen_->AddSlowPath(slow_path);
1301 Label* slow_path_entry = slow_path->GetEntryLabel();
1302 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1303}
1304
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001305void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1306 local->SetLocations(nullptr);
1307}
1308
1309void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1310 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1311}
1312
1313void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1314 local->SetLocations(nullptr);
1315}
1316
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001317void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001318 // Nothing to do, this is driven by the code generator.
1319}
1320
1321void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001322 LocationSummary* locations =
1323 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001324 switch (store->InputAt(1)->GetType()) {
1325 case Primitive::kPrimBoolean:
1326 case Primitive::kPrimByte:
1327 case Primitive::kPrimChar:
1328 case Primitive::kPrimShort:
1329 case Primitive::kPrimInt:
1330 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001331 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001332 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1333 break;
1334
1335 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001336 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001337 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1338 break;
1339
1340 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001341 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001342 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001343}
1344
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001345void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001346}
1347
Roland Levillain0d37cd02015-05-27 16:39:19 +01001348void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001349 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001350 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001351 // Handle the long/FP comparisons made in instruction simplification.
1352 switch (cond->InputAt(0)->GetType()) {
1353 case Primitive::kPrimLong:
1354 locations->SetInAt(0, Location::RequiresRegister());
1355 locations->SetInAt(1, Location::Any());
1356 break;
1357 case Primitive::kPrimFloat:
1358 case Primitive::kPrimDouble:
1359 locations->SetInAt(0, Location::RequiresFpuRegister());
1360 locations->SetInAt(1, Location::Any());
1361 break;
1362 default:
1363 locations->SetInAt(0, Location::RequiresRegister());
1364 locations->SetInAt(1, Location::Any());
1365 break;
1366 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001367 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001368 locations->SetOut(Location::RequiresRegister());
1369 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001370}
1371
Roland Levillain0d37cd02015-05-27 16:39:19 +01001372void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001373 if (!cond->NeedsMaterialization()) {
1374 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001375 }
Mark Mendellc4701932015-04-10 13:18:51 -04001376
1377 LocationSummary* locations = cond->GetLocations();
1378 Location lhs = locations->InAt(0);
1379 Location rhs = locations->InAt(1);
1380 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1381 Label true_label, false_label;
1382
1383 switch (cond->InputAt(0)->GetType()) {
1384 default:
1385 // Integer case.
1386
1387 // Clear output register: setcc only sets the low byte.
1388 __ xorl(reg, reg);
1389
1390 if (rhs.IsRegister()) {
1391 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1392 } else if (rhs.IsConstant()) {
1393 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1394 if (constant == 0) {
1395 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1396 } else {
1397 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1398 }
1399 } else {
1400 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1401 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001402 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001403 return;
1404 case Primitive::kPrimLong:
1405 // Clear output register: setcc only sets the low byte.
1406 __ xorl(reg, reg);
1407
1408 if (rhs.IsRegister()) {
1409 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1410 } else if (rhs.IsConstant()) {
1411 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1412 if (IsInt<32>(value)) {
1413 if (value == 0) {
1414 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1415 } else {
1416 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1417 }
1418 } else {
1419 // Value won't fit in an int.
1420 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1421 }
1422 } else {
1423 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1424 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001425 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001426 return;
1427 case Primitive::kPrimFloat: {
1428 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1429 if (rhs.IsConstant()) {
1430 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1431 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1432 } else if (rhs.IsStackSlot()) {
1433 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1434 } else {
1435 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1436 }
1437 GenerateFPJumps(cond, &true_label, &false_label);
1438 break;
1439 }
1440 case Primitive::kPrimDouble: {
1441 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1442 if (rhs.IsConstant()) {
1443 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1444 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1445 } else if (rhs.IsDoubleStackSlot()) {
1446 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1447 } else {
1448 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1449 }
1450 GenerateFPJumps(cond, &true_label, &false_label);
1451 break;
1452 }
1453 }
1454
1455 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001456 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001457
Roland Levillain4fa13f62015-07-06 18:11:54 +01001458 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001459 __ Bind(&false_label);
1460 __ xorl(reg, reg);
1461 __ jmp(&done_label);
1462
Roland Levillain4fa13f62015-07-06 18:11:54 +01001463 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001464 __ Bind(&true_label);
1465 __ movl(reg, Immediate(1));
1466 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001467}
1468
1469void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1470 VisitCondition(comp);
1471}
1472
1473void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1474 VisitCondition(comp);
1475}
1476
1477void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1478 VisitCondition(comp);
1479}
1480
1481void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1482 VisitCondition(comp);
1483}
1484
1485void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1486 VisitCondition(comp);
1487}
1488
1489void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1490 VisitCondition(comp);
1491}
1492
1493void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1494 VisitCondition(comp);
1495}
1496
1497void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1498 VisitCondition(comp);
1499}
1500
1501void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1502 VisitCondition(comp);
1503}
1504
1505void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1506 VisitCondition(comp);
1507}
1508
1509void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1510 VisitCondition(comp);
1511}
1512
1513void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1514 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001515}
1516
Aart Bike9f37602015-10-09 11:15:55 -07001517void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
1518 VisitCondition(comp);
1519}
1520
1521void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
1522 VisitCondition(comp);
1523}
1524
1525void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
1526 VisitCondition(comp);
1527}
1528
1529void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
1530 VisitCondition(comp);
1531}
1532
1533void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
1534 VisitCondition(comp);
1535}
1536
1537void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
1538 VisitCondition(comp);
1539}
1540
1541void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
1542 VisitCondition(comp);
1543}
1544
1545void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
1546 VisitCondition(comp);
1547}
1548
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001549void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001550 LocationSummary* locations =
1551 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001552 switch (compare->InputAt(0)->GetType()) {
1553 case Primitive::kPrimLong: {
1554 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001555 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001556 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1557 break;
1558 }
1559 case Primitive::kPrimFloat:
1560 case Primitive::kPrimDouble: {
1561 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001562 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001563 locations->SetOut(Location::RequiresRegister());
1564 break;
1565 }
1566 default:
1567 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1568 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001569}
1570
1571void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001572 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001573 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001574 Location left = locations->InAt(0);
1575 Location right = locations->InAt(1);
1576
Mark Mendell0c9497d2015-08-21 09:30:05 -04001577 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001578 Primitive::Type type = compare->InputAt(0)->GetType();
1579 switch (type) {
1580 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001581 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1582 if (right.IsConstant()) {
1583 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001584 if (IsInt<32>(value)) {
1585 if (value == 0) {
1586 __ testq(left_reg, left_reg);
1587 } else {
1588 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1589 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001590 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001591 // Value won't fit in an int.
1592 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001593 }
Mark Mendell40741f32015-04-20 22:10:34 -04001594 } else if (right.IsDoubleStackSlot()) {
1595 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001596 } else {
1597 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1598 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001599 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001600 }
1601 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001602 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1603 if (right.IsConstant()) {
1604 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1605 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1606 } else if (right.IsStackSlot()) {
1607 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1608 } else {
1609 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1610 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001611 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1612 break;
1613 }
1614 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001615 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1616 if (right.IsConstant()) {
1617 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1618 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1619 } else if (right.IsDoubleStackSlot()) {
1620 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1621 } else {
1622 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1623 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001624 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1625 break;
1626 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001627 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001628 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001629 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001630 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001631 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001632 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001633
Calin Juravle91debbc2014-11-26 19:01:09 +00001634 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001635 __ movl(out, Immediate(1));
1636 __ jmp(&done);
1637
1638 __ Bind(&less);
1639 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001640
1641 __ Bind(&done);
1642}
1643
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001644void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001645 LocationSummary* locations =
1646 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001647 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001648}
1649
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001650void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001651 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001652}
1653
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001654void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1655 LocationSummary* locations =
1656 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1657 locations->SetOut(Location::ConstantLocation(constant));
1658}
1659
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001660void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001661 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001662}
1663
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001664void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001665 LocationSummary* locations =
1666 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001667 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001668}
1669
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001670void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001671 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001672}
1673
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001674void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1675 LocationSummary* locations =
1676 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1677 locations->SetOut(Location::ConstantLocation(constant));
1678}
1679
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001680void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001681 // Will be generated at use site.
1682}
1683
1684void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1685 LocationSummary* locations =
1686 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1687 locations->SetOut(Location::ConstantLocation(constant));
1688}
1689
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001690void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1691 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001692 // Will be generated at use site.
1693}
1694
Calin Juravle27df7582015-04-17 19:12:31 +01001695void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1696 memory_barrier->SetLocations(nullptr);
1697}
1698
1699void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1700 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1701}
1702
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001703void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1704 ret->SetLocations(nullptr);
1705}
1706
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001707void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001708 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001709}
1710
1711void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001712 LocationSummary* locations =
1713 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001714 switch (ret->InputAt(0)->GetType()) {
1715 case Primitive::kPrimBoolean:
1716 case Primitive::kPrimByte:
1717 case Primitive::kPrimChar:
1718 case Primitive::kPrimShort:
1719 case Primitive::kPrimInt:
1720 case Primitive::kPrimNot:
1721 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001722 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001723 break;
1724
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001725 case Primitive::kPrimFloat:
1726 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001727 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001728 break;
1729
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001730 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001731 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001732 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001733}
1734
1735void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1736 if (kIsDebugBuild) {
1737 switch (ret->InputAt(0)->GetType()) {
1738 case Primitive::kPrimBoolean:
1739 case Primitive::kPrimByte:
1740 case Primitive::kPrimChar:
1741 case Primitive::kPrimShort:
1742 case Primitive::kPrimInt:
1743 case Primitive::kPrimNot:
1744 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001745 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001746 break;
1747
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001748 case Primitive::kPrimFloat:
1749 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001750 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001751 XMM0);
1752 break;
1753
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001754 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001755 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001756 }
1757 }
1758 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001759}
1760
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001761Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1762 switch (type) {
1763 case Primitive::kPrimBoolean:
1764 case Primitive::kPrimByte:
1765 case Primitive::kPrimChar:
1766 case Primitive::kPrimShort:
1767 case Primitive::kPrimInt:
1768 case Primitive::kPrimNot:
1769 case Primitive::kPrimLong:
1770 return Location::RegisterLocation(RAX);
1771
1772 case Primitive::kPrimVoid:
1773 return Location::NoLocation();
1774
1775 case Primitive::kPrimDouble:
1776 case Primitive::kPrimFloat:
1777 return Location::FpuRegisterLocation(XMM0);
1778 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001779
1780 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001781}
1782
1783Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1784 return Location::RegisterLocation(kMethodRegisterArgument);
1785}
1786
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001787Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001788 switch (type) {
1789 case Primitive::kPrimBoolean:
1790 case Primitive::kPrimByte:
1791 case Primitive::kPrimChar:
1792 case Primitive::kPrimShort:
1793 case Primitive::kPrimInt:
1794 case Primitive::kPrimNot: {
1795 uint32_t index = gp_index_++;
1796 stack_index_++;
1797 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001798 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001799 } else {
1800 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1801 }
1802 }
1803
1804 case Primitive::kPrimLong: {
1805 uint32_t index = gp_index_;
1806 stack_index_ += 2;
1807 if (index < calling_convention.GetNumberOfRegisters()) {
1808 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001809 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001810 } else {
1811 gp_index_ += 2;
1812 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1813 }
1814 }
1815
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001816 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001817 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001818 stack_index_++;
1819 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001820 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001821 } else {
1822 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1823 }
1824 }
1825
1826 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001827 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001828 stack_index_ += 2;
1829 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001830 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001831 } else {
1832 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1833 }
1834 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001835
1836 case Primitive::kPrimVoid:
1837 LOG(FATAL) << "Unexpected parameter type " << type;
1838 break;
1839 }
1840 return Location();
1841}
1842
Calin Juravle175dc732015-08-25 15:42:32 +01001843void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1844 // The trampoline uses the same calling convention as dex calling conventions,
1845 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1846 // the method_idx.
1847 HandleInvoke(invoke);
1848}
1849
1850void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1851 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1852}
1853
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001854void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001855 // When we do not run baseline, explicit clinit checks triggered by static
1856 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1857 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001858
Mark Mendellfb8d2792015-03-31 22:16:59 -04001859 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001860 if (intrinsic.TryDispatch(invoke)) {
1861 return;
1862 }
1863
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001864 HandleInvoke(invoke);
1865}
1866
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001867static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1868 if (invoke->GetLocations()->Intrinsified()) {
1869 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1870 intrinsic.Dispatch(invoke);
1871 return true;
1872 }
1873 return false;
1874}
1875
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001876void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001877 // When we do not run baseline, explicit clinit checks triggered by static
1878 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1879 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001880
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001881 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1882 return;
1883 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001884
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001885 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001886 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001887 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001888 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001889}
1890
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001891void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001892 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001893 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001894}
1895
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001896void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001897 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001898 if (intrinsic.TryDispatch(invoke)) {
1899 return;
1900 }
1901
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001902 HandleInvoke(invoke);
1903}
1904
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001905void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001906 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1907 return;
1908 }
1909
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001910 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001911
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001912 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001913 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001914}
1915
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001916void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1917 HandleInvoke(invoke);
1918 // Add the hidden argument.
1919 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1920}
1921
1922void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1923 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001924 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001925 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1926 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001927 LocationSummary* locations = invoke->GetLocations();
1928 Location receiver = locations->InAt(0);
1929 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1930
1931 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001932 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1933 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001934
1935 // temp = object->GetClass();
1936 if (receiver.IsStackSlot()) {
1937 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1938 __ movl(temp, Address(temp, class_offset));
1939 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001940 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001941 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001942 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001943 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001944 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001945 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001946 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001947 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001948 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001949
1950 DCHECK(!codegen_->IsLeafMethod());
1951 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1952}
1953
Roland Levillain88cb1752014-10-20 16:36:47 +01001954void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1955 LocationSummary* locations =
1956 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1957 switch (neg->GetResultType()) {
1958 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001959 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001960 locations->SetInAt(0, Location::RequiresRegister());
1961 locations->SetOut(Location::SameAsFirstInput());
1962 break;
1963
Roland Levillain88cb1752014-10-20 16:36:47 +01001964 case Primitive::kPrimFloat:
1965 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001966 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001967 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001968 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001969 break;
1970
1971 default:
1972 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1973 }
1974}
1975
1976void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1977 LocationSummary* locations = neg->GetLocations();
1978 Location out = locations->Out();
1979 Location in = locations->InAt(0);
1980 switch (neg->GetResultType()) {
1981 case Primitive::kPrimInt:
1982 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001983 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001984 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001985 break;
1986
1987 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001988 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001989 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001990 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001991 break;
1992
Roland Levillain5368c212014-11-27 15:03:41 +00001993 case Primitive::kPrimFloat: {
1994 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001995 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001996 // Implement float negation with an exclusive or with value
1997 // 0x80000000 (mask for bit 31, representing the sign of a
1998 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001999 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002000 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002001 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002002 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002003
Roland Levillain5368c212014-11-27 15:03:41 +00002004 case Primitive::kPrimDouble: {
2005 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002006 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002007 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002008 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002009 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002010 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002011 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002012 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002013 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002014
2015 default:
2016 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2017 }
2018}
2019
Roland Levillaindff1f282014-11-05 14:15:05 +00002020void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2023 Primitive::Type result_type = conversion->GetResultType();
2024 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002025 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002026
David Brazdilb2bd1c52015-03-25 11:17:37 +00002027 // The Java language does not allow treating boolean as an integral type but
2028 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002029
Roland Levillaindff1f282014-11-05 14:15:05 +00002030 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002031 case Primitive::kPrimByte:
2032 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002033 case Primitive::kPrimBoolean:
2034 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002035 case Primitive::kPrimShort:
2036 case Primitive::kPrimInt:
2037 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002038 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002039 locations->SetInAt(0, Location::Any());
2040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2041 break;
2042
2043 default:
2044 LOG(FATAL) << "Unexpected type conversion from " << input_type
2045 << " to " << result_type;
2046 }
2047 break;
2048
Roland Levillain01a8d712014-11-14 16:27:39 +00002049 case Primitive::kPrimShort:
2050 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002051 case Primitive::kPrimBoolean:
2052 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002053 case Primitive::kPrimByte:
2054 case Primitive::kPrimInt:
2055 case Primitive::kPrimChar:
2056 // Processing a Dex `int-to-short' instruction.
2057 locations->SetInAt(0, Location::Any());
2058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2059 break;
2060
2061 default:
2062 LOG(FATAL) << "Unexpected type conversion from " << input_type
2063 << " to " << result_type;
2064 }
2065 break;
2066
Roland Levillain946e1432014-11-11 17:35:19 +00002067 case Primitive::kPrimInt:
2068 switch (input_type) {
2069 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002070 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002071 locations->SetInAt(0, Location::Any());
2072 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2073 break;
2074
2075 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002076 // Processing a Dex `float-to-int' instruction.
2077 locations->SetInAt(0, Location::RequiresFpuRegister());
2078 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002079 break;
2080
Roland Levillain946e1432014-11-11 17:35:19 +00002081 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002082 // Processing a Dex `double-to-int' instruction.
2083 locations->SetInAt(0, Location::RequiresFpuRegister());
2084 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002085 break;
2086
2087 default:
2088 LOG(FATAL) << "Unexpected type conversion from " << input_type
2089 << " to " << result_type;
2090 }
2091 break;
2092
Roland Levillaindff1f282014-11-05 14:15:05 +00002093 case Primitive::kPrimLong:
2094 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002095 case Primitive::kPrimBoolean:
2096 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002097 case Primitive::kPrimByte:
2098 case Primitive::kPrimShort:
2099 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002100 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002101 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002102 // TODO: We would benefit from a (to-be-implemented)
2103 // Location::RegisterOrStackSlot requirement for this input.
2104 locations->SetInAt(0, Location::RequiresRegister());
2105 locations->SetOut(Location::RequiresRegister());
2106 break;
2107
2108 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002109 // Processing a Dex `float-to-long' instruction.
2110 locations->SetInAt(0, Location::RequiresFpuRegister());
2111 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002112 break;
2113
Roland Levillaindff1f282014-11-05 14:15:05 +00002114 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002115 // Processing a Dex `double-to-long' instruction.
2116 locations->SetInAt(0, Location::RequiresFpuRegister());
2117 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002118 break;
2119
2120 default:
2121 LOG(FATAL) << "Unexpected type conversion from " << input_type
2122 << " to " << result_type;
2123 }
2124 break;
2125
Roland Levillain981e4542014-11-14 11:47:14 +00002126 case Primitive::kPrimChar:
2127 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002128 case Primitive::kPrimBoolean:
2129 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002130 case Primitive::kPrimByte:
2131 case Primitive::kPrimShort:
2132 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002133 // Processing a Dex `int-to-char' instruction.
2134 locations->SetInAt(0, Location::Any());
2135 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2136 break;
2137
2138 default:
2139 LOG(FATAL) << "Unexpected type conversion from " << input_type
2140 << " to " << result_type;
2141 }
2142 break;
2143
Roland Levillaindff1f282014-11-05 14:15:05 +00002144 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002145 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002146 case Primitive::kPrimBoolean:
2147 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002148 case Primitive::kPrimByte:
2149 case Primitive::kPrimShort:
2150 case Primitive::kPrimInt:
2151 case Primitive::kPrimChar:
2152 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002153 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002154 locations->SetOut(Location::RequiresFpuRegister());
2155 break;
2156
2157 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002158 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002159 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002160 locations->SetOut(Location::RequiresFpuRegister());
2161 break;
2162
Roland Levillaincff13742014-11-17 14:32:17 +00002163 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002164 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002165 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002166 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002167 break;
2168
2169 default:
2170 LOG(FATAL) << "Unexpected type conversion from " << input_type
2171 << " to " << result_type;
2172 };
2173 break;
2174
Roland Levillaindff1f282014-11-05 14:15:05 +00002175 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002176 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002177 case Primitive::kPrimBoolean:
2178 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002179 case Primitive::kPrimByte:
2180 case Primitive::kPrimShort:
2181 case Primitive::kPrimInt:
2182 case Primitive::kPrimChar:
2183 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002184 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002185 locations->SetOut(Location::RequiresFpuRegister());
2186 break;
2187
2188 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002189 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002190 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002191 locations->SetOut(Location::RequiresFpuRegister());
2192 break;
2193
Roland Levillaincff13742014-11-17 14:32:17 +00002194 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002195 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002196 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002197 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002204 break;
2205
2206 default:
2207 LOG(FATAL) << "Unexpected type conversion from " << input_type
2208 << " to " << result_type;
2209 }
2210}
2211
2212void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2213 LocationSummary* locations = conversion->GetLocations();
2214 Location out = locations->Out();
2215 Location in = locations->InAt(0);
2216 Primitive::Type result_type = conversion->GetResultType();
2217 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002218 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002219 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002220 case Primitive::kPrimByte:
2221 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002222 case Primitive::kPrimBoolean:
2223 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002224 case Primitive::kPrimShort:
2225 case Primitive::kPrimInt:
2226 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002227 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002228 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002229 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002230 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002231 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002232 Address(CpuRegister(RSP), in.GetStackIndex()));
2233 } else {
2234 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002235 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002236 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2237 }
2238 break;
2239
2240 default:
2241 LOG(FATAL) << "Unexpected type conversion from " << input_type
2242 << " to " << result_type;
2243 }
2244 break;
2245
Roland Levillain01a8d712014-11-14 16:27:39 +00002246 case Primitive::kPrimShort:
2247 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002248 case Primitive::kPrimBoolean:
2249 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002250 case Primitive::kPrimByte:
2251 case Primitive::kPrimInt:
2252 case Primitive::kPrimChar:
2253 // Processing a Dex `int-to-short' instruction.
2254 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002255 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002256 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002257 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002258 Address(CpuRegister(RSP), in.GetStackIndex()));
2259 } else {
2260 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002261 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002262 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2263 }
2264 break;
2265
2266 default:
2267 LOG(FATAL) << "Unexpected type conversion from " << input_type
2268 << " to " << result_type;
2269 }
2270 break;
2271
Roland Levillain946e1432014-11-11 17:35:19 +00002272 case Primitive::kPrimInt:
2273 switch (input_type) {
2274 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002275 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002276 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002277 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002278 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002279 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002280 Address(CpuRegister(RSP), in.GetStackIndex()));
2281 } else {
2282 DCHECK(in.IsConstant());
2283 DCHECK(in.GetConstant()->IsLongConstant());
2284 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002285 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002286 }
2287 break;
2288
Roland Levillain3f8f9362014-12-02 17:45:01 +00002289 case Primitive::kPrimFloat: {
2290 // Processing a Dex `float-to-int' instruction.
2291 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2292 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002293 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002294
2295 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002296 // if input >= (float)INT_MAX goto done
2297 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002298 __ j(kAboveEqual, &done);
2299 // if input == NaN goto nan
2300 __ j(kUnordered, &nan);
2301 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002302 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002303 __ jmp(&done);
2304 __ Bind(&nan);
2305 // output = 0
2306 __ xorl(output, output);
2307 __ Bind(&done);
2308 break;
2309 }
2310
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002311 case Primitive::kPrimDouble: {
2312 // Processing a Dex `double-to-int' instruction.
2313 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2314 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002315 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002316
2317 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002318 // if input >= (double)INT_MAX goto done
2319 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002320 __ j(kAboveEqual, &done);
2321 // if input == NaN goto nan
2322 __ j(kUnordered, &nan);
2323 // output = double-to-int-truncate(input)
2324 __ cvttsd2si(output, input);
2325 __ jmp(&done);
2326 __ Bind(&nan);
2327 // output = 0
2328 __ xorl(output, output);
2329 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002330 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002331 }
Roland Levillain946e1432014-11-11 17:35:19 +00002332
2333 default:
2334 LOG(FATAL) << "Unexpected type conversion from " << input_type
2335 << " to " << result_type;
2336 }
2337 break;
2338
Roland Levillaindff1f282014-11-05 14:15:05 +00002339 case Primitive::kPrimLong:
2340 switch (input_type) {
2341 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002342 case Primitive::kPrimBoolean:
2343 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002344 case Primitive::kPrimByte:
2345 case Primitive::kPrimShort:
2346 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002347 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002348 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002349 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002351 break;
2352
Roland Levillain624279f2014-12-04 11:54:28 +00002353 case Primitive::kPrimFloat: {
2354 // Processing a Dex `float-to-long' instruction.
2355 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2356 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002357 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002358
Mark Mendell92e83bf2015-05-07 11:25:03 -04002359 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002360 // if input >= (float)LONG_MAX goto done
2361 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002362 __ j(kAboveEqual, &done);
2363 // if input == NaN goto nan
2364 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002365 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002366 __ cvttss2si(output, input, true);
2367 __ jmp(&done);
2368 __ Bind(&nan);
2369 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002370 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002371 __ Bind(&done);
2372 break;
2373 }
2374
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002375 case Primitive::kPrimDouble: {
2376 // Processing a Dex `double-to-long' instruction.
2377 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2378 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002379 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002380
Mark Mendell92e83bf2015-05-07 11:25:03 -04002381 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002382 // if input >= (double)LONG_MAX goto done
2383 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002384 __ j(kAboveEqual, &done);
2385 // if input == NaN goto nan
2386 __ j(kUnordered, &nan);
2387 // output = double-to-long-truncate(input)
2388 __ cvttsd2si(output, input, true);
2389 __ jmp(&done);
2390 __ Bind(&nan);
2391 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002392 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002393 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002394 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002395 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002396
2397 default:
2398 LOG(FATAL) << "Unexpected type conversion from " << input_type
2399 << " to " << result_type;
2400 }
2401 break;
2402
Roland Levillain981e4542014-11-14 11:47:14 +00002403 case Primitive::kPrimChar:
2404 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002405 case Primitive::kPrimBoolean:
2406 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002407 case Primitive::kPrimByte:
2408 case Primitive::kPrimShort:
2409 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002410 // Processing a Dex `int-to-char' instruction.
2411 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002412 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002413 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002414 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002415 Address(CpuRegister(RSP), in.GetStackIndex()));
2416 } else {
2417 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002418 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002419 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2420 }
2421 break;
2422
2423 default:
2424 LOG(FATAL) << "Unexpected type conversion from " << input_type
2425 << " to " << result_type;
2426 }
2427 break;
2428
Roland Levillaindff1f282014-11-05 14:15:05 +00002429 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002430 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002431 case Primitive::kPrimBoolean:
2432 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002433 case Primitive::kPrimByte:
2434 case Primitive::kPrimShort:
2435 case Primitive::kPrimInt:
2436 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002437 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002438 if (in.IsRegister()) {
2439 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2440 } else if (in.IsConstant()) {
2441 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2442 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2443 if (v == 0) {
2444 __ xorps(dest, dest);
2445 } else {
2446 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2447 }
2448 } else {
2449 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2450 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2451 }
Roland Levillaincff13742014-11-17 14:32:17 +00002452 break;
2453
2454 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002455 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002456 if (in.IsRegister()) {
2457 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2458 } else if (in.IsConstant()) {
2459 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2460 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2461 if (v == 0) {
2462 __ xorps(dest, dest);
2463 } else {
2464 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2465 }
2466 } else {
2467 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2468 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2469 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002470 break;
2471
Roland Levillaincff13742014-11-17 14:32:17 +00002472 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002473 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002474 if (in.IsFpuRegister()) {
2475 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2476 } else if (in.IsConstant()) {
2477 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2478 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2479 if (bit_cast<int64_t, double>(v) == 0) {
2480 __ xorps(dest, dest);
2481 } else {
2482 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2483 }
2484 } else {
2485 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2486 Address(CpuRegister(RSP), in.GetStackIndex()));
2487 }
Roland Levillaincff13742014-11-17 14:32:17 +00002488 break;
2489
2490 default:
2491 LOG(FATAL) << "Unexpected type conversion from " << input_type
2492 << " to " << result_type;
2493 };
2494 break;
2495
Roland Levillaindff1f282014-11-05 14:15:05 +00002496 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002497 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002498 case Primitive::kPrimBoolean:
2499 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002500 case Primitive::kPrimByte:
2501 case Primitive::kPrimShort:
2502 case Primitive::kPrimInt:
2503 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002504 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002505 if (in.IsRegister()) {
2506 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2507 } else if (in.IsConstant()) {
2508 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2509 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2510 if (v == 0) {
2511 __ xorpd(dest, dest);
2512 } else {
2513 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2514 }
2515 } else {
2516 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2517 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2518 }
Roland Levillaincff13742014-11-17 14:32:17 +00002519 break;
2520
2521 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002522 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002523 if (in.IsRegister()) {
2524 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2525 } else if (in.IsConstant()) {
2526 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2527 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2528 if (v == 0) {
2529 __ xorpd(dest, dest);
2530 } else {
2531 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2532 }
2533 } else {
2534 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2535 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2536 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002537 break;
2538
Roland Levillaincff13742014-11-17 14:32:17 +00002539 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002540 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002541 if (in.IsFpuRegister()) {
2542 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2543 } else if (in.IsConstant()) {
2544 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2545 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2546 if (bit_cast<int32_t, float>(v) == 0) {
2547 __ xorpd(dest, dest);
2548 } else {
2549 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2550 }
2551 } else {
2552 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2553 Address(CpuRegister(RSP), in.GetStackIndex()));
2554 }
Roland Levillaincff13742014-11-17 14:32:17 +00002555 break;
2556
2557 default:
2558 LOG(FATAL) << "Unexpected type conversion from " << input_type
2559 << " to " << result_type;
2560 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002561 break;
2562
2563 default:
2564 LOG(FATAL) << "Unexpected type conversion from " << input_type
2565 << " to " << result_type;
2566 }
2567}
2568
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002569void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002570 LocationSummary* locations =
2571 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002572 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002573 case Primitive::kPrimInt: {
2574 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002575 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002577 break;
2578 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002579
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002580 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002581 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002582 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002583 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002584 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002585 break;
2586 }
2587
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002588 case Primitive::kPrimDouble:
2589 case Primitive::kPrimFloat: {
2590 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002591 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002592 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002593 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002594 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002595
2596 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002597 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002598 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002599}
2600
2601void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2602 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002603 Location first = locations->InAt(0);
2604 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002605 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002606
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002607 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002608 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002609 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002610 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2611 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002612 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2613 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002614 } else {
2615 __ leal(out.AsRegister<CpuRegister>(), Address(
2616 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2617 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002618 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002619 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2620 __ addl(out.AsRegister<CpuRegister>(),
2621 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2622 } else {
2623 __ leal(out.AsRegister<CpuRegister>(), Address(
2624 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2625 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002626 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002627 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002628 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002629 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002630 break;
2631 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002632
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002633 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002634 if (second.IsRegister()) {
2635 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2636 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002637 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2638 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002639 } else {
2640 __ leaq(out.AsRegister<CpuRegister>(), Address(
2641 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2642 }
2643 } else {
2644 DCHECK(second.IsConstant());
2645 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2646 int32_t int32_value = Low32Bits(value);
2647 DCHECK_EQ(int32_value, value);
2648 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2649 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2650 } else {
2651 __ leaq(out.AsRegister<CpuRegister>(), Address(
2652 first.AsRegister<CpuRegister>(), int32_value));
2653 }
2654 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002655 break;
2656 }
2657
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002658 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002659 if (second.IsFpuRegister()) {
2660 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2661 } else if (second.IsConstant()) {
2662 __ addss(first.AsFpuRegister<XmmRegister>(),
2663 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2664 } else {
2665 DCHECK(second.IsStackSlot());
2666 __ addss(first.AsFpuRegister<XmmRegister>(),
2667 Address(CpuRegister(RSP), second.GetStackIndex()));
2668 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002669 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002670 }
2671
2672 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002673 if (second.IsFpuRegister()) {
2674 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2675 } else if (second.IsConstant()) {
2676 __ addsd(first.AsFpuRegister<XmmRegister>(),
2677 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2678 } else {
2679 DCHECK(second.IsDoubleStackSlot());
2680 __ addsd(first.AsFpuRegister<XmmRegister>(),
2681 Address(CpuRegister(RSP), second.GetStackIndex()));
2682 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002683 break;
2684 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002685
2686 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002687 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002688 }
2689}
2690
2691void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002692 LocationSummary* locations =
2693 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002694 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002695 case Primitive::kPrimInt: {
2696 locations->SetInAt(0, Location::RequiresRegister());
2697 locations->SetInAt(1, Location::Any());
2698 locations->SetOut(Location::SameAsFirstInput());
2699 break;
2700 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002701 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002702 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002703 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002704 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002705 break;
2706 }
Calin Juravle11351682014-10-23 15:38:15 +01002707 case Primitive::kPrimFloat:
2708 case Primitive::kPrimDouble: {
2709 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002710 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002711 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002712 break;
Calin Juravle11351682014-10-23 15:38:15 +01002713 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002714 default:
Calin Juravle11351682014-10-23 15:38:15 +01002715 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002716 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002717}
2718
2719void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2720 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002721 Location first = locations->InAt(0);
2722 Location second = locations->InAt(1);
2723 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002724 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002725 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002726 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002727 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002728 } else if (second.IsConstant()) {
2729 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002730 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002731 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002732 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002733 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002734 break;
2735 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002736 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002737 if (second.IsConstant()) {
2738 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2739 DCHECK(IsInt<32>(value));
2740 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2741 } else {
2742 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2743 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002744 break;
2745 }
2746
Calin Juravle11351682014-10-23 15:38:15 +01002747 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002748 if (second.IsFpuRegister()) {
2749 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2750 } else if (second.IsConstant()) {
2751 __ subss(first.AsFpuRegister<XmmRegister>(),
2752 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2753 } else {
2754 DCHECK(second.IsStackSlot());
2755 __ subss(first.AsFpuRegister<XmmRegister>(),
2756 Address(CpuRegister(RSP), second.GetStackIndex()));
2757 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002758 break;
Calin Juravle11351682014-10-23 15:38:15 +01002759 }
2760
2761 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002762 if (second.IsFpuRegister()) {
2763 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2764 } else if (second.IsConstant()) {
2765 __ subsd(first.AsFpuRegister<XmmRegister>(),
2766 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2767 } else {
2768 DCHECK(second.IsDoubleStackSlot());
2769 __ subsd(first.AsFpuRegister<XmmRegister>(),
2770 Address(CpuRegister(RSP), second.GetStackIndex()));
2771 }
Calin Juravle11351682014-10-23 15:38:15 +01002772 break;
2773 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002774
2775 default:
Calin Juravle11351682014-10-23 15:38:15 +01002776 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002777 }
2778}
2779
Calin Juravle34bacdf2014-10-07 20:23:36 +01002780void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2781 LocationSummary* locations =
2782 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2783 switch (mul->GetResultType()) {
2784 case Primitive::kPrimInt: {
2785 locations->SetInAt(0, Location::RequiresRegister());
2786 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002787 if (mul->InputAt(1)->IsIntConstant()) {
2788 // Can use 3 operand multiply.
2789 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2790 } else {
2791 locations->SetOut(Location::SameAsFirstInput());
2792 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002793 break;
2794 }
2795 case Primitive::kPrimLong: {
2796 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002797 locations->SetInAt(1, Location::Any());
2798 if (mul->InputAt(1)->IsLongConstant() &&
2799 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002800 // Can use 3 operand multiply.
2801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2802 } else {
2803 locations->SetOut(Location::SameAsFirstInput());
2804 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002805 break;
2806 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002807 case Primitive::kPrimFloat:
2808 case Primitive::kPrimDouble: {
2809 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002810 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002811 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002812 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002813 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002814
2815 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002816 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002817 }
2818}
2819
2820void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2821 LocationSummary* locations = mul->GetLocations();
2822 Location first = locations->InAt(0);
2823 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002824 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002825 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002826 case Primitive::kPrimInt:
2827 // The constant may have ended up in a register, so test explicitly to avoid
2828 // problems where the output may not be the same as the first operand.
2829 if (mul->InputAt(1)->IsIntConstant()) {
2830 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2831 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2832 } else if (second.IsRegister()) {
2833 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002834 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002835 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002836 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002837 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002838 __ imull(first.AsRegister<CpuRegister>(),
2839 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002840 }
2841 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002842 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002843 // The constant may have ended up in a register, so test explicitly to avoid
2844 // problems where the output may not be the same as the first operand.
2845 if (mul->InputAt(1)->IsLongConstant()) {
2846 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2847 if (IsInt<32>(value)) {
2848 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2849 Immediate(static_cast<int32_t>(value)));
2850 } else {
2851 // Have to use the constant area.
2852 DCHECK(first.Equals(out));
2853 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2854 }
2855 } else if (second.IsRegister()) {
2856 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002857 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002858 } else {
2859 DCHECK(second.IsDoubleStackSlot());
2860 DCHECK(first.Equals(out));
2861 __ imulq(first.AsRegister<CpuRegister>(),
2862 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002863 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002864 break;
2865 }
2866
Calin Juravleb5bfa962014-10-21 18:02:24 +01002867 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002868 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002869 if (second.IsFpuRegister()) {
2870 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2871 } else if (second.IsConstant()) {
2872 __ mulss(first.AsFpuRegister<XmmRegister>(),
2873 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2874 } else {
2875 DCHECK(second.IsStackSlot());
2876 __ mulss(first.AsFpuRegister<XmmRegister>(),
2877 Address(CpuRegister(RSP), second.GetStackIndex()));
2878 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002879 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002880 }
2881
2882 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002883 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002884 if (second.IsFpuRegister()) {
2885 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2886 } else if (second.IsConstant()) {
2887 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2888 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2889 } else {
2890 DCHECK(second.IsDoubleStackSlot());
2891 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2892 Address(CpuRegister(RSP), second.GetStackIndex()));
2893 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002894 break;
2895 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002896
2897 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002898 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002899 }
2900}
2901
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002902void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2903 uint32_t stack_adjustment, bool is_float) {
2904 if (source.IsStackSlot()) {
2905 DCHECK(is_float);
2906 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2907 } else if (source.IsDoubleStackSlot()) {
2908 DCHECK(!is_float);
2909 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2910 } else {
2911 // Write the value to the temporary location on the stack and load to FP stack.
2912 if (is_float) {
2913 Location stack_temp = Location::StackSlot(temp_offset);
2914 codegen_->Move(stack_temp, source);
2915 __ flds(Address(CpuRegister(RSP), temp_offset));
2916 } else {
2917 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2918 codegen_->Move(stack_temp, source);
2919 __ fldl(Address(CpuRegister(RSP), temp_offset));
2920 }
2921 }
2922}
2923
2924void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2925 Primitive::Type type = rem->GetResultType();
2926 bool is_float = type == Primitive::kPrimFloat;
2927 size_t elem_size = Primitive::ComponentSize(type);
2928 LocationSummary* locations = rem->GetLocations();
2929 Location first = locations->InAt(0);
2930 Location second = locations->InAt(1);
2931 Location out = locations->Out();
2932
2933 // Create stack space for 2 elements.
2934 // TODO: enhance register allocator to ask for stack temporaries.
2935 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2936
2937 // Load the values to the FP stack in reverse order, using temporaries if needed.
2938 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2939 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2940
2941 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002942 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002943 __ Bind(&retry);
2944 __ fprem();
2945
2946 // Move FP status to AX.
2947 __ fstsw();
2948
2949 // And see if the argument reduction is complete. This is signaled by the
2950 // C2 FPU flag bit set to 0.
2951 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2952 __ j(kNotEqual, &retry);
2953
2954 // We have settled on the final value. Retrieve it into an XMM register.
2955 // Store FP top of stack to real stack.
2956 if (is_float) {
2957 __ fsts(Address(CpuRegister(RSP), 0));
2958 } else {
2959 __ fstl(Address(CpuRegister(RSP), 0));
2960 }
2961
2962 // Pop the 2 items from the FP stack.
2963 __ fucompp();
2964
2965 // Load the value from the stack into an XMM register.
2966 DCHECK(out.IsFpuRegister()) << out;
2967 if (is_float) {
2968 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2969 } else {
2970 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2971 }
2972
2973 // And remove the temporary stack space we allocated.
2974 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2975}
2976
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002977void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2978 DCHECK(instruction->IsDiv() || instruction->IsRem());
2979
2980 LocationSummary* locations = instruction->GetLocations();
2981 Location second = locations->InAt(1);
2982 DCHECK(second.IsConstant());
2983
2984 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2985 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002986 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002987
2988 DCHECK(imm == 1 || imm == -1);
2989
2990 switch (instruction->GetResultType()) {
2991 case Primitive::kPrimInt: {
2992 if (instruction->IsRem()) {
2993 __ xorl(output_register, output_register);
2994 } else {
2995 __ movl(output_register, input_register);
2996 if (imm == -1) {
2997 __ negl(output_register);
2998 }
2999 }
3000 break;
3001 }
3002
3003 case Primitive::kPrimLong: {
3004 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003005 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003006 } else {
3007 __ movq(output_register, input_register);
3008 if (imm == -1) {
3009 __ negq(output_register);
3010 }
3011 }
3012 break;
3013 }
3014
3015 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003016 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003017 }
3018}
3019
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003020void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003021 LocationSummary* locations = instruction->GetLocations();
3022 Location second = locations->InAt(1);
3023
3024 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3025 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3026
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003027 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003028
3029 DCHECK(IsPowerOfTwo(std::abs(imm)));
3030
3031 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3032
3033 if (instruction->GetResultType() == Primitive::kPrimInt) {
3034 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
3035 __ testl(numerator, numerator);
3036 __ cmov(kGreaterEqual, tmp, numerator);
3037 int shift = CTZ(imm);
3038 __ sarl(tmp, Immediate(shift));
3039
3040 if (imm < 0) {
3041 __ negl(tmp);
3042 }
3043
3044 __ movl(output_register, tmp);
3045 } else {
3046 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3047 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3048
Mark Mendell92e83bf2015-05-07 11:25:03 -04003049 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003050 __ addq(rdx, numerator);
3051 __ testq(numerator, numerator);
3052 __ cmov(kGreaterEqual, rdx, numerator);
3053 int shift = CTZ(imm);
3054 __ sarq(rdx, Immediate(shift));
3055
3056 if (imm < 0) {
3057 __ negq(rdx);
3058 }
3059
3060 __ movq(output_register, rdx);
3061 }
3062}
3063
3064void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3065 DCHECK(instruction->IsDiv() || instruction->IsRem());
3066
3067 LocationSummary* locations = instruction->GetLocations();
3068 Location second = locations->InAt(1);
3069
3070 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3071 : locations->GetTemp(0).AsRegister<CpuRegister>();
3072 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3073 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3074 : locations->Out().AsRegister<CpuRegister>();
3075 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3076
3077 DCHECK_EQ(RAX, eax.AsRegister());
3078 DCHECK_EQ(RDX, edx.AsRegister());
3079 if (instruction->IsDiv()) {
3080 DCHECK_EQ(RAX, out.AsRegister());
3081 } else {
3082 DCHECK_EQ(RDX, out.AsRegister());
3083 }
3084
3085 int64_t magic;
3086 int shift;
3087
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003088 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003089 if (instruction->GetResultType() == Primitive::kPrimInt) {
3090 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3091
3092 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3093
3094 __ movl(numerator, eax);
3095
Mark Mendell0c9497d2015-08-21 09:30:05 -04003096 NearLabel no_div;
3097 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003098 __ testl(eax, eax);
3099 __ j(kNotEqual, &no_div);
3100
3101 __ xorl(out, out);
3102 __ jmp(&end);
3103
3104 __ Bind(&no_div);
3105
3106 __ movl(eax, Immediate(magic));
3107 __ imull(numerator);
3108
3109 if (imm > 0 && magic < 0) {
3110 __ addl(edx, numerator);
3111 } else if (imm < 0 && magic > 0) {
3112 __ subl(edx, numerator);
3113 }
3114
3115 if (shift != 0) {
3116 __ sarl(edx, Immediate(shift));
3117 }
3118
3119 __ movl(eax, edx);
3120 __ shrl(edx, Immediate(31));
3121 __ addl(edx, eax);
3122
3123 if (instruction->IsRem()) {
3124 __ movl(eax, numerator);
3125 __ imull(edx, Immediate(imm));
3126 __ subl(eax, edx);
3127 __ movl(edx, eax);
3128 } else {
3129 __ movl(eax, edx);
3130 }
3131 __ Bind(&end);
3132 } else {
3133 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3134
3135 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3136
3137 CpuRegister rax = eax;
3138 CpuRegister rdx = edx;
3139
3140 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3141
3142 // Save the numerator.
3143 __ movq(numerator, rax);
3144
3145 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003146 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003147
3148 // RDX:RAX = magic * numerator
3149 __ imulq(numerator);
3150
3151 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003152 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003153 __ addq(rdx, numerator);
3154 } else if (imm < 0 && magic > 0) {
3155 // RDX -= numerator
3156 __ subq(rdx, numerator);
3157 }
3158
3159 // Shift if needed.
3160 if (shift != 0) {
3161 __ sarq(rdx, Immediate(shift));
3162 }
3163
3164 // RDX += 1 if RDX < 0
3165 __ movq(rax, rdx);
3166 __ shrq(rdx, Immediate(63));
3167 __ addq(rdx, rax);
3168
3169 if (instruction->IsRem()) {
3170 __ movq(rax, numerator);
3171
3172 if (IsInt<32>(imm)) {
3173 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3174 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003175 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003176 }
3177
3178 __ subq(rax, rdx);
3179 __ movq(rdx, rax);
3180 } else {
3181 __ movq(rax, rdx);
3182 }
3183 }
3184}
3185
Calin Juravlebacfec32014-11-14 15:54:36 +00003186void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3187 DCHECK(instruction->IsDiv() || instruction->IsRem());
3188 Primitive::Type type = instruction->GetResultType();
3189 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3190
3191 bool is_div = instruction->IsDiv();
3192 LocationSummary* locations = instruction->GetLocations();
3193
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003194 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3195 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003196
Roland Levillain271ab9c2014-11-27 15:23:57 +00003197 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003198 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003199
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003200 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003201 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003202
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003203 if (imm == 0) {
3204 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3205 } else if (imm == 1 || imm == -1) {
3206 DivRemOneOrMinusOne(instruction);
3207 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003208 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003209 } else {
3210 DCHECK(imm <= -2 || imm >= 2);
3211 GenerateDivRemWithAnyConstant(instruction);
3212 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003213 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003214 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003215 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3216 out.AsRegister(), type, is_div);
3217 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003218
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003219 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3220 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3221 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3222 // so it's safe to just use negl instead of more complex comparisons.
3223 if (type == Primitive::kPrimInt) {
3224 __ cmpl(second_reg, Immediate(-1));
3225 __ j(kEqual, slow_path->GetEntryLabel());
3226 // edx:eax <- sign-extended of eax
3227 __ cdq();
3228 // eax = quotient, edx = remainder
3229 __ idivl(second_reg);
3230 } else {
3231 __ cmpq(second_reg, Immediate(-1));
3232 __ j(kEqual, slow_path->GetEntryLabel());
3233 // rdx:rax <- sign-extended of rax
3234 __ cqo();
3235 // rax = quotient, rdx = remainder
3236 __ idivq(second_reg);
3237 }
3238 __ Bind(slow_path->GetExitLabel());
3239 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003240}
3241
Calin Juravle7c4954d2014-10-28 16:57:40 +00003242void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3243 LocationSummary* locations =
3244 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3245 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003246 case Primitive::kPrimInt:
3247 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003248 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003249 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003250 locations->SetOut(Location::SameAsFirstInput());
3251 // Intel uses edx:eax as the dividend.
3252 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003253 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3254 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3255 // output and request another temp.
3256 if (div->InputAt(1)->IsConstant()) {
3257 locations->AddTemp(Location::RequiresRegister());
3258 }
Calin Juravled0d48522014-11-04 16:40:20 +00003259 break;
3260 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003261
Calin Juravle7c4954d2014-10-28 16:57:40 +00003262 case Primitive::kPrimFloat:
3263 case Primitive::kPrimDouble: {
3264 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003265 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003266 locations->SetOut(Location::SameAsFirstInput());
3267 break;
3268 }
3269
3270 default:
3271 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3272 }
3273}
3274
3275void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3276 LocationSummary* locations = div->GetLocations();
3277 Location first = locations->InAt(0);
3278 Location second = locations->InAt(1);
3279 DCHECK(first.Equals(locations->Out()));
3280
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003281 Primitive::Type type = div->GetResultType();
3282 switch (type) {
3283 case Primitive::kPrimInt:
3284 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003285 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003286 break;
3287 }
3288
Calin Juravle7c4954d2014-10-28 16:57:40 +00003289 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003290 if (second.IsFpuRegister()) {
3291 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3292 } else if (second.IsConstant()) {
3293 __ divss(first.AsFpuRegister<XmmRegister>(),
3294 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3295 } else {
3296 DCHECK(second.IsStackSlot());
3297 __ divss(first.AsFpuRegister<XmmRegister>(),
3298 Address(CpuRegister(RSP), second.GetStackIndex()));
3299 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003300 break;
3301 }
3302
3303 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003304 if (second.IsFpuRegister()) {
3305 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3306 } else if (second.IsConstant()) {
3307 __ divsd(first.AsFpuRegister<XmmRegister>(),
3308 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3309 } else {
3310 DCHECK(second.IsDoubleStackSlot());
3311 __ divsd(first.AsFpuRegister<XmmRegister>(),
3312 Address(CpuRegister(RSP), second.GetStackIndex()));
3313 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003314 break;
3315 }
3316
3317 default:
3318 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3319 }
3320}
3321
Calin Juravlebacfec32014-11-14 15:54:36 +00003322void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003323 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003324 LocationSummary* locations =
3325 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003326
3327 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003328 case Primitive::kPrimInt:
3329 case Primitive::kPrimLong: {
3330 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003331 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003332 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3333 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003334 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3335 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3336 // output and request another temp.
3337 if (rem->InputAt(1)->IsConstant()) {
3338 locations->AddTemp(Location::RequiresRegister());
3339 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003340 break;
3341 }
3342
3343 case Primitive::kPrimFloat:
3344 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003345 locations->SetInAt(0, Location::Any());
3346 locations->SetInAt(1, Location::Any());
3347 locations->SetOut(Location::RequiresFpuRegister());
3348 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003349 break;
3350 }
3351
3352 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003353 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003354 }
3355}
3356
3357void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3358 Primitive::Type type = rem->GetResultType();
3359 switch (type) {
3360 case Primitive::kPrimInt:
3361 case Primitive::kPrimLong: {
3362 GenerateDivRemIntegral(rem);
3363 break;
3364 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003365 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003366 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003367 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003368 break;
3369 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003370 default:
3371 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3372 }
3373}
3374
Calin Juravled0d48522014-11-04 16:40:20 +00003375void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003376 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3377 ? LocationSummary::kCallOnSlowPath
3378 : LocationSummary::kNoCall;
3379 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003380 locations->SetInAt(0, Location::Any());
3381 if (instruction->HasUses()) {
3382 locations->SetOut(Location::SameAsFirstInput());
3383 }
3384}
3385
3386void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003387 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003388 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3389 codegen_->AddSlowPath(slow_path);
3390
3391 LocationSummary* locations = instruction->GetLocations();
3392 Location value = locations->InAt(0);
3393
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003394 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003395 case Primitive::kPrimByte:
3396 case Primitive::kPrimChar:
3397 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003398 case Primitive::kPrimInt: {
3399 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003400 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003401 __ j(kEqual, slow_path->GetEntryLabel());
3402 } else if (value.IsStackSlot()) {
3403 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3404 __ j(kEqual, slow_path->GetEntryLabel());
3405 } else {
3406 DCHECK(value.IsConstant()) << value;
3407 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3408 __ jmp(slow_path->GetEntryLabel());
3409 }
3410 }
3411 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003412 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003413 case Primitive::kPrimLong: {
3414 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003415 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003416 __ j(kEqual, slow_path->GetEntryLabel());
3417 } else if (value.IsDoubleStackSlot()) {
3418 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3419 __ j(kEqual, slow_path->GetEntryLabel());
3420 } else {
3421 DCHECK(value.IsConstant()) << value;
3422 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3423 __ jmp(slow_path->GetEntryLabel());
3424 }
3425 }
3426 break;
3427 }
3428 default:
3429 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003430 }
Calin Juravled0d48522014-11-04 16:40:20 +00003431}
3432
Calin Juravle9aec02f2014-11-18 23:06:35 +00003433void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3434 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3435
3436 LocationSummary* locations =
3437 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3438
3439 switch (op->GetResultType()) {
3440 case Primitive::kPrimInt:
3441 case Primitive::kPrimLong: {
3442 locations->SetInAt(0, Location::RequiresRegister());
3443 // The shift count needs to be in CL.
3444 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3445 locations->SetOut(Location::SameAsFirstInput());
3446 break;
3447 }
3448 default:
3449 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3450 }
3451}
3452
3453void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3454 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3455
3456 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003457 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003458 Location second = locations->InAt(1);
3459
3460 switch (op->GetResultType()) {
3461 case Primitive::kPrimInt: {
3462 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003463 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003464 if (op->IsShl()) {
3465 __ shll(first_reg, second_reg);
3466 } else if (op->IsShr()) {
3467 __ sarl(first_reg, second_reg);
3468 } else {
3469 __ shrl(first_reg, second_reg);
3470 }
3471 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003472 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003473 if (op->IsShl()) {
3474 __ shll(first_reg, imm);
3475 } else if (op->IsShr()) {
3476 __ sarl(first_reg, imm);
3477 } else {
3478 __ shrl(first_reg, imm);
3479 }
3480 }
3481 break;
3482 }
3483 case Primitive::kPrimLong: {
3484 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003485 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003486 if (op->IsShl()) {
3487 __ shlq(first_reg, second_reg);
3488 } else if (op->IsShr()) {
3489 __ sarq(first_reg, second_reg);
3490 } else {
3491 __ shrq(first_reg, second_reg);
3492 }
3493 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003494 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003495 if (op->IsShl()) {
3496 __ shlq(first_reg, imm);
3497 } else if (op->IsShr()) {
3498 __ sarq(first_reg, imm);
3499 } else {
3500 __ shrq(first_reg, imm);
3501 }
3502 }
3503 break;
3504 }
3505 default:
3506 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3507 }
3508}
3509
3510void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3511 HandleShift(shl);
3512}
3513
3514void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3515 HandleShift(shl);
3516}
3517
3518void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3519 HandleShift(shr);
3520}
3521
3522void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3523 HandleShift(shr);
3524}
3525
3526void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3527 HandleShift(ushr);
3528}
3529
3530void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3531 HandleShift(ushr);
3532}
3533
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003534void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003535 LocationSummary* locations =
3536 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003537 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003538 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003539 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003540 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003541}
3542
3543void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3544 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003545 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3546 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003547 // Note: if heap poisoning is enabled, the entry point takes cares
3548 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003549
Calin Juravle175dc732015-08-25 15:42:32 +01003550 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3551 instruction,
3552 instruction->GetDexPc(),
3553 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003554
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003555 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003556}
3557
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003558void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3559 LocationSummary* locations =
3560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3561 InvokeRuntimeCallingConvention calling_convention;
3562 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003563 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003564 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003565 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003566}
3567
3568void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3569 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003570 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3571 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003572
Roland Levillain4d027112015-07-01 15:41:14 +01003573 // Note: if heap poisoning is enabled, the entry point takes cares
3574 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003575 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3576 instruction,
3577 instruction->GetDexPc(),
3578 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003579
3580 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003581}
3582
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003583void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003584 LocationSummary* locations =
3585 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003586 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3587 if (location.IsStackSlot()) {
3588 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3589 } else if (location.IsDoubleStackSlot()) {
3590 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3591 }
3592 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003593}
3594
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003595void InstructionCodeGeneratorX86_64::VisitParameterValue(
3596 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003597 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003598}
3599
3600void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3601 LocationSummary* locations =
3602 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3603 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3604}
3605
3606void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3607 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3608 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003609}
3610
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003611void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003612 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003613 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003614 locations->SetInAt(0, Location::RequiresRegister());
3615 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003616}
3617
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003618void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3619 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003620 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3621 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003622 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003623 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003624 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003625 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003626 break;
3627
3628 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003629 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003630 break;
3631
3632 default:
3633 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3634 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003635}
3636
David Brazdil66d126e2015-04-03 16:02:44 +01003637void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3638 LocationSummary* locations =
3639 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3640 locations->SetInAt(0, Location::RequiresRegister());
3641 locations->SetOut(Location::SameAsFirstInput());
3642}
3643
3644void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003645 LocationSummary* locations = bool_not->GetLocations();
3646 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3647 locations->Out().AsRegister<CpuRegister>().AsRegister());
3648 Location out = locations->Out();
3649 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3650}
3651
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003652void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003653 LocationSummary* locations =
3654 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003655 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3656 locations->SetInAt(i, Location::Any());
3657 }
3658 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003659}
3660
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003661void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003662 LOG(FATAL) << "Unimplemented";
3663}
3664
Calin Juravle52c48962014-12-16 17:02:57 +00003665void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3666 /*
3667 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3668 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3669 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3670 */
3671 switch (kind) {
3672 case MemBarrierKind::kAnyAny: {
3673 __ mfence();
3674 break;
3675 }
3676 case MemBarrierKind::kAnyStore:
3677 case MemBarrierKind::kLoadAny:
3678 case MemBarrierKind::kStoreStore: {
3679 // nop
3680 break;
3681 }
3682 default:
3683 LOG(FATAL) << "Unexpected memory barier " << kind;
3684 }
3685}
3686
3687void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3688 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3689
Nicolas Geoffray39468442014-09-02 15:17:15 +01003690 LocationSummary* locations =
3691 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003692 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003693 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3694 locations->SetOut(Location::RequiresFpuRegister());
3695 } else {
3696 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3697 }
Calin Juravle52c48962014-12-16 17:02:57 +00003698}
3699
3700void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3701 const FieldInfo& field_info) {
3702 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3703
3704 LocationSummary* locations = instruction->GetLocations();
3705 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3706 Location out = locations->Out();
3707 bool is_volatile = field_info.IsVolatile();
3708 Primitive::Type field_type = field_info.GetFieldType();
3709 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3710
3711 switch (field_type) {
3712 case Primitive::kPrimBoolean: {
3713 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3714 break;
3715 }
3716
3717 case Primitive::kPrimByte: {
3718 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3719 break;
3720 }
3721
3722 case Primitive::kPrimShort: {
3723 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3724 break;
3725 }
3726
3727 case Primitive::kPrimChar: {
3728 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3729 break;
3730 }
3731
3732 case Primitive::kPrimInt:
3733 case Primitive::kPrimNot: {
3734 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3735 break;
3736 }
3737
3738 case Primitive::kPrimLong: {
3739 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3740 break;
3741 }
3742
3743 case Primitive::kPrimFloat: {
3744 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3745 break;
3746 }
3747
3748 case Primitive::kPrimDouble: {
3749 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3750 break;
3751 }
3752
3753 case Primitive::kPrimVoid:
3754 LOG(FATAL) << "Unreachable type " << field_type;
3755 UNREACHABLE();
3756 }
3757
Calin Juravle77520bc2015-01-12 18:45:46 +00003758 codegen_->MaybeRecordImplicitNullCheck(instruction);
3759
Calin Juravle52c48962014-12-16 17:02:57 +00003760 if (is_volatile) {
3761 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3762 }
Roland Levillain4d027112015-07-01 15:41:14 +01003763
3764 if (field_type == Primitive::kPrimNot) {
3765 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3766 }
Calin Juravle52c48962014-12-16 17:02:57 +00003767}
3768
3769void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3770 const FieldInfo& field_info) {
3771 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3772
3773 LocationSummary* locations =
3774 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003775 Primitive::Type field_type = field_info.GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003776 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003777 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003778
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003779 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003780 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3781 locations->SetInAt(1, Location::RequiresFpuRegister());
3782 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04003783 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003784 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003785 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003786 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003787 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003788 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003789 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3790 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003791 locations->AddTemp(Location::RequiresRegister());
3792 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003793}
3794
Calin Juravle52c48962014-12-16 17:02:57 +00003795void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003796 const FieldInfo& field_info,
3797 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003798 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3799
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003800 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003801 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3802 Location value = locations->InAt(1);
3803 bool is_volatile = field_info.IsVolatile();
3804 Primitive::Type field_type = field_info.GetFieldType();
3805 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3806
3807 if (is_volatile) {
3808 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3809 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003810
3811 switch (field_type) {
3812 case Primitive::kPrimBoolean:
3813 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003814 if (value.IsConstant()) {
3815 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3816 __ movb(Address(base, offset), Immediate(v));
3817 } else {
3818 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3819 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003820 break;
3821 }
3822
3823 case Primitive::kPrimShort:
3824 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003825 if (value.IsConstant()) {
3826 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3827 __ movw(Address(base, offset), Immediate(v));
3828 } else {
3829 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3830 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003831 break;
3832 }
3833
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003834 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003835 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003836 if (value.IsConstant()) {
3837 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003838 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3839 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3840 // Note: if heap poisoning is enabled, no need to poison
3841 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003842 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003843 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003844 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3845 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3846 __ movl(temp, value.AsRegister<CpuRegister>());
3847 __ PoisonHeapReference(temp);
3848 __ movl(Address(base, offset), temp);
3849 } else {
3850 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3851 }
Mark Mendell40741f32015-04-20 22:10:34 -04003852 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003853 break;
3854 }
3855
3856 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003857 if (value.IsConstant()) {
3858 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
3859 DCHECK(IsInt<32>(v));
3860 int32_t v_32 = v;
3861 __ movq(Address(base, offset), Immediate(v_32));
3862 } else {
3863 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3864 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003865 break;
3866 }
3867
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003868 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003869 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003870 break;
3871 }
3872
3873 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003874 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003875 break;
3876 }
3877
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003878 case Primitive::kPrimVoid:
3879 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003880 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003881 }
Calin Juravle52c48962014-12-16 17:02:57 +00003882
Calin Juravle77520bc2015-01-12 18:45:46 +00003883 codegen_->MaybeRecordImplicitNullCheck(instruction);
3884
3885 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3886 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3887 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003888 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003889 }
3890
Calin Juravle52c48962014-12-16 17:02:57 +00003891 if (is_volatile) {
3892 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3893 }
3894}
3895
3896void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3897 HandleFieldSet(instruction, instruction->GetFieldInfo());
3898}
3899
3900void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003901 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003902}
3903
3904void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003905 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003906}
3907
3908void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003909 HandleFieldGet(instruction, instruction->GetFieldInfo());
3910}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003911
Calin Juravle52c48962014-12-16 17:02:57 +00003912void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3913 HandleFieldGet(instruction);
3914}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003915
Calin Juravle52c48962014-12-16 17:02:57 +00003916void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3917 HandleFieldGet(instruction, instruction->GetFieldInfo());
3918}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003919
Calin Juravle52c48962014-12-16 17:02:57 +00003920void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3921 HandleFieldSet(instruction, instruction->GetFieldInfo());
3922}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003923
Calin Juravle52c48962014-12-16 17:02:57 +00003924void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003925 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003926}
3927
Calin Juravlee460d1d2015-09-29 04:52:17 +01003928void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
3929 HUnresolvedInstanceFieldGet* instruction) {
3930 FieldAccessCallingConventionX86_64 calling_convention;
3931 codegen_->CreateUnresolvedFieldLocationSummary(
3932 instruction, instruction->GetFieldType(), calling_convention);
3933}
3934
3935void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
3936 HUnresolvedInstanceFieldGet* instruction) {
3937 FieldAccessCallingConventionX86_64 calling_convention;
3938 codegen_->GenerateUnresolvedFieldAccess(instruction,
3939 instruction->GetFieldType(),
3940 instruction->GetFieldIndex(),
3941 instruction->GetDexPc(),
3942 calling_convention);
3943}
3944
3945void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
3946 HUnresolvedInstanceFieldSet* instruction) {
3947 FieldAccessCallingConventionX86_64 calling_convention;
3948 codegen_->CreateUnresolvedFieldLocationSummary(
3949 instruction, instruction->GetFieldType(), calling_convention);
3950}
3951
3952void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
3953 HUnresolvedInstanceFieldSet* instruction) {
3954 FieldAccessCallingConventionX86_64 calling_convention;
3955 codegen_->GenerateUnresolvedFieldAccess(instruction,
3956 instruction->GetFieldType(),
3957 instruction->GetFieldIndex(),
3958 instruction->GetDexPc(),
3959 calling_convention);
3960}
3961
3962void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
3963 HUnresolvedStaticFieldGet* instruction) {
3964 FieldAccessCallingConventionX86_64 calling_convention;
3965 codegen_->CreateUnresolvedFieldLocationSummary(
3966 instruction, instruction->GetFieldType(), calling_convention);
3967}
3968
3969void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
3970 HUnresolvedStaticFieldGet* instruction) {
3971 FieldAccessCallingConventionX86_64 calling_convention;
3972 codegen_->GenerateUnresolvedFieldAccess(instruction,
3973 instruction->GetFieldType(),
3974 instruction->GetFieldIndex(),
3975 instruction->GetDexPc(),
3976 calling_convention);
3977}
3978
3979void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
3980 HUnresolvedStaticFieldSet* instruction) {
3981 FieldAccessCallingConventionX86_64 calling_convention;
3982 codegen_->CreateUnresolvedFieldLocationSummary(
3983 instruction, instruction->GetFieldType(), calling_convention);
3984}
3985
3986void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
3987 HUnresolvedStaticFieldSet* instruction) {
3988 FieldAccessCallingConventionX86_64 calling_convention;
3989 codegen_->GenerateUnresolvedFieldAccess(instruction,
3990 instruction->GetFieldType(),
3991 instruction->GetFieldIndex(),
3992 instruction->GetDexPc(),
3993 calling_convention);
3994}
3995
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003996void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003997 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3998 ? LocationSummary::kCallOnSlowPath
3999 : LocationSummary::kNoCall;
4000 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4001 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004002 ? Location::RequiresRegister()
4003 : Location::Any();
4004 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004005 if (instruction->HasUses()) {
4006 locations->SetOut(Location::SameAsFirstInput());
4007 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004008}
4009
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004010void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004011 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4012 return;
4013 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004014 LocationSummary* locations = instruction->GetLocations();
4015 Location obj = locations->InAt(0);
4016
4017 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4018 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4019}
4020
4021void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004022 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004023 codegen_->AddSlowPath(slow_path);
4024
4025 LocationSummary* locations = instruction->GetLocations();
4026 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004027
4028 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004029 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004030 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004031 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004032 } else {
4033 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004034 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004035 __ jmp(slow_path->GetEntryLabel());
4036 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004037 }
4038 __ j(kEqual, slow_path->GetEntryLabel());
4039}
4040
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004041void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004042 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004043 GenerateImplicitNullCheck(instruction);
4044 } else {
4045 GenerateExplicitNullCheck(instruction);
4046 }
4047}
4048
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004049void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004050 LocationSummary* locations =
4051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004052 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004053 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004054 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4055 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4056 } else {
4057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4058 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004059}
4060
4061void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4062 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004063 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004064 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004065 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004066
Roland Levillain4d027112015-07-01 15:41:14 +01004067 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004068 case Primitive::kPrimBoolean: {
4069 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004070 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004071 if (index.IsConstant()) {
4072 __ movzxb(out, Address(obj,
4073 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4074 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004076 }
4077 break;
4078 }
4079
4080 case Primitive::kPrimByte: {
4081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004082 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004083 if (index.IsConstant()) {
4084 __ movsxb(out, Address(obj,
4085 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4086 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004087 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004088 }
4089 break;
4090 }
4091
4092 case Primitive::kPrimShort: {
4093 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004094 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004095 if (index.IsConstant()) {
4096 __ movsxw(out, Address(obj,
4097 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4098 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004099 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004100 }
4101 break;
4102 }
4103
4104 case Primitive::kPrimChar: {
4105 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004106 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004107 if (index.IsConstant()) {
4108 __ movzxw(out, Address(obj,
4109 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4110 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004111 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004112 }
4113 break;
4114 }
4115
4116 case Primitive::kPrimInt:
4117 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01004118 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4119 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004120 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004121 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004122 if (index.IsConstant()) {
4123 __ movl(out, Address(obj,
4124 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4125 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004126 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004127 }
4128 break;
4129 }
4130
4131 case Primitive::kPrimLong: {
4132 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004133 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004134 if (index.IsConstant()) {
4135 __ movq(out, Address(obj,
4136 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4137 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004139 }
4140 break;
4141 }
4142
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004143 case Primitive::kPrimFloat: {
4144 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004145 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004146 if (index.IsConstant()) {
4147 __ movss(out, Address(obj,
4148 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4149 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004150 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004151 }
4152 break;
4153 }
4154
4155 case Primitive::kPrimDouble: {
4156 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004157 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004158 if (index.IsConstant()) {
4159 __ movsd(out, Address(obj,
4160 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4161 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004162 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004163 }
4164 break;
4165 }
4166
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004167 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004168 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004169 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004170 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004171 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004172
4173 if (type == Primitive::kPrimNot) {
4174 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4175 __ MaybeUnpoisonHeapReference(out);
4176 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004177}
4178
4179void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004180 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004181
4182 bool needs_write_barrier =
4183 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004184 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004185
Nicolas Geoffray39468442014-09-02 15:17:15 +01004186 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004187 instruction,
4188 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004189
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004190 locations->SetInAt(0, Location::RequiresRegister());
4191 locations->SetInAt(
4192 1, Location::RegisterOrConstant(instruction->InputAt(1)));
4193 locations->SetInAt(2, Location::RequiresRegister());
4194 if (value_type == Primitive::kPrimLong) {
4195 locations->SetInAt(2, Location::RegisterOrInt32LongConstant(instruction->InputAt(2)));
4196 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
4197 locations->SetInAt(2, Location::RequiresFpuRegister());
4198 } else {
4199 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4200 }
4201
4202 if (needs_write_barrier) {
4203 // Temporary registers for the write barrier.
4204 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4205 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004206 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004207}
4208
4209void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4210 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004211 CpuRegister array = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004212 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004213 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004214 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004215 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004216 bool needs_write_barrier =
4217 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004218 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4219 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4220 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004221
4222 switch (value_type) {
4223 case Primitive::kPrimBoolean:
4224 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004225 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4226 Address address = index.IsConstant()
4227 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4228 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4229 if (value.IsRegister()) {
4230 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004231 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004232 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004233 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004234 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004235 break;
4236 }
4237
4238 case Primitive::kPrimShort:
4239 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004240 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4241 Address address = index.IsConstant()
4242 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4243 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4244 if (value.IsRegister()) {
4245 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004246 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004247 DCHECK(value.IsConstant()) << value;
4248 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004249 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004250 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004251 break;
4252 }
4253
4254 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004255 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4256 Address address = index.IsConstant()
4257 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4258 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4259 if (!value.IsRegister()) {
4260 // Just setting null.
4261 DCHECK(instruction->InputAt(2)->IsNullConstant());
4262 DCHECK(value.IsConstant()) << value;
4263 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004264 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004265 DCHECK(!needs_write_barrier);
4266 DCHECK(!may_need_runtime_call);
4267 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004268 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004269
4270 DCHECK(needs_write_barrier);
4271 CpuRegister register_value = value.AsRegister<CpuRegister>();
4272 NearLabel done, not_null, do_put;
4273 SlowPathCode* slow_path = nullptr;
4274 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4275 if (may_need_runtime_call) {
4276 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4277 codegen_->AddSlowPath(slow_path);
4278 if (instruction->GetValueCanBeNull()) {
4279 __ testl(register_value, register_value);
4280 __ j(kNotEqual, &not_null);
4281 __ movl(address, Immediate(0));
4282 codegen_->MaybeRecordImplicitNullCheck(instruction);
4283 __ jmp(&done);
4284 __ Bind(&not_null);
4285 }
4286
4287 __ movl(temp, Address(array, class_offset));
4288 codegen_->MaybeRecordImplicitNullCheck(instruction);
4289 __ MaybeUnpoisonHeapReference(temp);
4290 __ movl(temp, Address(temp, component_offset));
4291 // No need to poison/unpoison, we're comparing two poisoned references.
4292 __ cmpl(temp, Address(register_value, class_offset));
4293 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4294 __ j(kEqual, &do_put);
4295 __ MaybeUnpoisonHeapReference(temp);
4296 __ movl(temp, Address(temp, super_offset));
4297 // No need to unpoison the result, we're comparing against null.
4298 __ testl(temp, temp);
4299 __ j(kNotEqual, slow_path->GetEntryLabel());
4300 __ Bind(&do_put);
4301 } else {
4302 __ j(kNotEqual, slow_path->GetEntryLabel());
4303 }
4304 }
4305
4306 if (kPoisonHeapReferences) {
4307 __ movl(temp, register_value);
4308 __ PoisonHeapReference(temp);
4309 __ movl(address, temp);
4310 } else {
4311 __ movl(address, register_value);
4312 }
4313 if (!may_need_runtime_call) {
4314 codegen_->MaybeRecordImplicitNullCheck(instruction);
4315 }
4316
4317 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4318 codegen_->MarkGCCard(
4319 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4320 __ Bind(&done);
4321
4322 if (slow_path != nullptr) {
4323 __ Bind(slow_path->GetExitLabel());
4324 }
4325
4326 break;
4327 }
4328 case Primitive::kPrimInt: {
4329 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4330 Address address = index.IsConstant()
4331 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4332 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4333 if (value.IsRegister()) {
4334 __ movl(address, value.AsRegister<CpuRegister>());
4335 } else {
4336 DCHECK(value.IsConstant()) << value;
4337 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4338 __ movl(address, Immediate(v));
4339 }
4340 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004341 break;
4342 }
4343
4344 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004345 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4346 Address address = index.IsConstant()
4347 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4348 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4349 if (value.IsRegister()) {
4350 __ movq(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004351 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004352 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
4353 DCHECK(IsInt<32>(v));
4354 int32_t v_32 = v;
4355 __ movq(address, Immediate(v_32));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004356 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004357 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004358 break;
4359 }
4360
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004361 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004362 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4363 Address address = index.IsConstant()
4364 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4365 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4366 DCHECK(value.IsFpuRegister());
4367 __ movss(address, value.AsFpuRegister<XmmRegister>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004368 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004369 break;
4370 }
4371
4372 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004373 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4374 Address address = index.IsConstant()
4375 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4376 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4377 DCHECK(value.IsFpuRegister());
4378 __ movsd(address, value.AsFpuRegister<XmmRegister>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004379 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004380 break;
4381 }
4382
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004383 case Primitive::kPrimVoid:
4384 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004385 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004386 }
4387}
4388
4389void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004390 LocationSummary* locations =
4391 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004392 locations->SetInAt(0, Location::RequiresRegister());
4393 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004394}
4395
4396void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4397 LocationSummary* locations = instruction->GetLocations();
4398 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004399 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4400 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004401 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004402 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403}
4404
4405void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004406 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4407 ? LocationSummary::kCallOnSlowPath
4408 : LocationSummary::kNoCall;
4409 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004410 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004411 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004412 if (instruction->HasUses()) {
4413 locations->SetOut(Location::SameAsFirstInput());
4414 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004415}
4416
4417void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4418 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004419 Location index_loc = locations->InAt(0);
4420 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004421 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004422 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004423
Mark Mendell99dbd682015-04-22 16:18:52 -04004424 if (length_loc.IsConstant()) {
4425 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4426 if (index_loc.IsConstant()) {
4427 // BCE will remove the bounds check if we are guarenteed to pass.
4428 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4429 if (index < 0 || index >= length) {
4430 codegen_->AddSlowPath(slow_path);
4431 __ jmp(slow_path->GetEntryLabel());
4432 } else {
4433 // Some optimization after BCE may have generated this, and we should not
4434 // generate a bounds check if it is a valid range.
4435 }
4436 return;
4437 }
4438
4439 // We have to reverse the jump condition because the length is the constant.
4440 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4441 __ cmpl(index_reg, Immediate(length));
4442 codegen_->AddSlowPath(slow_path);
4443 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004444 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004445 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4446 if (index_loc.IsConstant()) {
4447 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4448 __ cmpl(length, Immediate(value));
4449 } else {
4450 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4451 }
4452 codegen_->AddSlowPath(slow_path);
4453 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004454 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004455}
4456
4457void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4458 CpuRegister card,
4459 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004460 CpuRegister value,
4461 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004462 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004463 if (value_can_be_null) {
4464 __ testl(value, value);
4465 __ j(kEqual, &is_null);
4466 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004467 __ gs()->movq(card, Address::Absolute(
4468 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4469 __ movq(temp, object);
4470 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004471 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004472 if (value_can_be_null) {
4473 __ Bind(&is_null);
4474 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004475}
4476
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4478 temp->SetLocations(nullptr);
4479}
4480
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004481void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482 // Nothing to do, this is driven by the code generator.
4483}
4484
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004485void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004486 LOG(FATAL) << "Unimplemented";
4487}
4488
4489void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004490 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4491}
4492
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004493void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4494 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4495}
4496
4497void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004498 HBasicBlock* block = instruction->GetBlock();
4499 if (block->GetLoopInformation() != nullptr) {
4500 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4501 // The back edge will generate the suspend check.
4502 return;
4503 }
4504 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4505 // The goto will generate the suspend check.
4506 return;
4507 }
4508 GenerateSuspendCheck(instruction, nullptr);
4509}
4510
4511void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4512 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004513 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004514 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4515 if (slow_path == nullptr) {
4516 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4517 instruction->SetSlowPath(slow_path);
4518 codegen_->AddSlowPath(slow_path);
4519 if (successor != nullptr) {
4520 DCHECK(successor->IsLoopHeader());
4521 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4522 }
4523 } else {
4524 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4525 }
4526
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004527 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004528 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004529 if (successor == nullptr) {
4530 __ j(kNotEqual, slow_path->GetEntryLabel());
4531 __ Bind(slow_path->GetReturnLabel());
4532 } else {
4533 __ j(kEqual, codegen_->GetLabelOf(successor));
4534 __ jmp(slow_path->GetEntryLabel());
4535 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004536}
4537
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004538X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4539 return codegen_->GetAssembler();
4540}
4541
4542void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004543 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004544 Location source = move->GetSource();
4545 Location destination = move->GetDestination();
4546
4547 if (source.IsRegister()) {
4548 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004549 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004550 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004551 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004552 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004553 } else {
4554 DCHECK(destination.IsDoubleStackSlot());
4555 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004556 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004557 }
4558 } else if (source.IsStackSlot()) {
4559 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004560 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004561 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004562 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004563 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004564 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004565 } else {
4566 DCHECK(destination.IsStackSlot());
4567 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4568 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4569 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004570 } else if (source.IsDoubleStackSlot()) {
4571 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004572 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004573 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004574 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004575 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4576 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004577 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004578 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004579 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4580 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4581 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004582 } else if (source.IsConstant()) {
4583 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004584 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4585 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004586 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004587 if (value == 0) {
4588 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4589 } else {
4590 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4591 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004592 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004593 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004594 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004595 }
4596 } else if (constant->IsLongConstant()) {
4597 int64_t value = constant->AsLongConstant()->GetValue();
4598 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004599 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004600 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004601 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004602 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004603 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004604 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004605 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004606 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004607 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004608 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4609 if (value == 0) {
4610 // easy FP 0.0.
4611 __ xorps(dest, dest);
4612 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004613 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004614 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004615 } else {
4616 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004617 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004618 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4619 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004620 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004621 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004622 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004623 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004624 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004625 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4626 if (value == 0) {
4627 __ xorpd(dest, dest);
4628 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004629 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004630 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004631 } else {
4632 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004633 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004634 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004635 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004636 } else if (source.IsFpuRegister()) {
4637 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004638 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004639 } else if (destination.IsStackSlot()) {
4640 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004641 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004642 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004643 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004644 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004645 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004646 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004647 }
4648}
4649
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004650void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004651 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004652 __ movl(Address(CpuRegister(RSP), mem), reg);
4653 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004654}
4655
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004656void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004657 ScratchRegisterScope ensure_scratch(
4658 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4659
4660 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4661 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4662 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4663 Address(CpuRegister(RSP), mem2 + stack_offset));
4664 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4665 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4666 CpuRegister(ensure_scratch.GetRegister()));
4667}
4668
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004669void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4670 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4671 __ movq(Address(CpuRegister(RSP), mem), reg);
4672 __ movq(reg, CpuRegister(TMP));
4673}
4674
4675void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4676 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004677 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004678
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004679 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4680 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4681 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4682 Address(CpuRegister(RSP), mem2 + stack_offset));
4683 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4684 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4685 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004686}
4687
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004688void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4689 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4690 __ movss(Address(CpuRegister(RSP), mem), reg);
4691 __ movd(reg, CpuRegister(TMP));
4692}
4693
4694void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4695 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4696 __ movsd(Address(CpuRegister(RSP), mem), reg);
4697 __ movd(reg, CpuRegister(TMP));
4698}
4699
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004700void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004701 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004702 Location source = move->GetSource();
4703 Location destination = move->GetDestination();
4704
4705 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004706 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004707 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004708 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004709 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004710 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004711 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004712 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4713 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004714 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004715 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004716 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004717 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4718 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004719 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004720 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4721 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4722 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004723 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004724 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004725 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004726 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004727 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004728 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004729 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004730 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004731 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004732 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004733 }
4734}
4735
4736
4737void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4738 __ pushq(CpuRegister(reg));
4739}
4740
4741
4742void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4743 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004744}
4745
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004746void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004747 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004748 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4749 Immediate(mirror::Class::kStatusInitialized));
4750 __ j(kLess, slow_path->GetEntryLabel());
4751 __ Bind(slow_path->GetExitLabel());
4752 // No need for memory fence, thanks to the X86_64 memory model.
4753}
4754
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004755void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004756 InvokeRuntimeCallingConvention calling_convention;
4757 CodeGenerator::CreateLoadClassLocationSummary(
4758 cls,
4759 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4760 Location::RegisterLocation(RAX));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004761}
4762
4763void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004764 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004765 if (cls->NeedsAccessCheck()) {
4766 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4767 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4768 cls,
4769 cls->GetDexPc(),
4770 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004771 return;
4772 }
4773
4774 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4775 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
4776 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004777 DCHECK(!cls->CanCallRuntime());
4778 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004779 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004780 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004781 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004782 __ movq(out, Address(
4783 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004784 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004785 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004786
Andreas Gampe85b62f22015-09-09 13:15:38 -07004787 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004788 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4789 codegen_->AddSlowPath(slow_path);
4790 __ testl(out, out);
4791 __ j(kEqual, slow_path->GetEntryLabel());
4792 if (cls->MustGenerateClinitCheck()) {
4793 GenerateClassInitializationCheck(slow_path, out);
4794 } else {
4795 __ Bind(slow_path->GetExitLabel());
4796 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004797 }
4798}
4799
4800void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4801 LocationSummary* locations =
4802 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4803 locations->SetInAt(0, Location::RequiresRegister());
4804 if (check->HasUses()) {
4805 locations->SetOut(Location::SameAsFirstInput());
4806 }
4807}
4808
4809void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004810 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004811 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004812 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004813 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004814 GenerateClassInitializationCheck(slow_path,
4815 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004816}
4817
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004818void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4819 LocationSummary* locations =
4820 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004821 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004822 locations->SetOut(Location::RequiresRegister());
4823}
4824
4825void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004826 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004827 codegen_->AddSlowPath(slow_path);
4828
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004829 LocationSummary* locations = load->GetLocations();
4830 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4831 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004832 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004833 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004834 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004835 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004836 __ testl(out, out);
4837 __ j(kEqual, slow_path->GetEntryLabel());
4838 __ Bind(slow_path->GetExitLabel());
4839}
4840
David Brazdilcb1c0552015-08-04 16:22:25 +01004841static Address GetExceptionTlsAddress() {
4842 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4843}
4844
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004845void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4846 LocationSummary* locations =
4847 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4848 locations->SetOut(Location::RequiresRegister());
4849}
4850
4851void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004852 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4853}
4854
4855void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4856 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4857}
4858
4859void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4860 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004861}
4862
4863void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4864 LocationSummary* locations =
4865 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4866 InvokeRuntimeCallingConvention calling_convention;
4867 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4868}
4869
4870void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004871 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4872 instruction,
4873 instruction->GetDexPc(),
4874 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004875}
4876
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004877void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004878 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4879 switch (instruction->GetTypeCheckKind()) {
4880 case TypeCheckKind::kExactCheck:
4881 case TypeCheckKind::kAbstractClassCheck:
4882 case TypeCheckKind::kClassHierarchyCheck:
4883 case TypeCheckKind::kArrayObjectCheck:
4884 call_kind = LocationSummary::kNoCall;
4885 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004886 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004887 case TypeCheckKind::kInterfaceCheck:
4888 call_kind = LocationSummary::kCall;
4889 break;
4890 case TypeCheckKind::kArrayCheck:
4891 call_kind = LocationSummary::kCallOnSlowPath;
4892 break;
4893 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004894 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004895 if (call_kind != LocationSummary::kCall) {
4896 locations->SetInAt(0, Location::RequiresRegister());
4897 locations->SetInAt(1, Location::Any());
4898 // Note that TypeCheckSlowPathX86_64 uses this register too.
4899 locations->SetOut(Location::RequiresRegister());
4900 } else {
4901 InvokeRuntimeCallingConvention calling_convention;
4902 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4903 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4904 locations->SetOut(Location::RegisterLocation(RAX));
4905 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004906}
4907
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004908void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004909 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004910 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004911 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004912 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004913 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004914 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4915 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4916 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004917 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004918 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004919
4920 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004921 // Avoid null check if we know obj is not null.
4922 if (instruction->MustDoNullCheck()) {
4923 __ testl(obj, obj);
4924 __ j(kEqual, &zero);
4925 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004926
Calin Juravle98893e12015-10-02 21:05:03 +01004927 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004928 // This is safe, as the register is caller-save, and the object must be in another
4929 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004930 CpuRegister target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4931 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004932 ? obj
4933 : out;
4934 __ movl(target, Address(obj, class_offset));
4935 __ MaybeUnpoisonHeapReference(target);
4936
4937 switch (instruction->GetTypeCheckKind()) {
4938 case TypeCheckKind::kExactCheck: {
4939 if (cls.IsRegister()) {
4940 __ cmpl(out, cls.AsRegister<CpuRegister>());
4941 } else {
4942 DCHECK(cls.IsStackSlot()) << cls;
4943 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4944 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004945 if (zero.IsLinked()) {
4946 // Classes must be equal for the instanceof to succeed.
4947 __ j(kNotEqual, &zero);
4948 __ movl(out, Immediate(1));
4949 __ jmp(&done);
4950 } else {
4951 __ setcc(kEqual, out);
4952 // setcc only sets the low byte.
4953 __ andl(out, Immediate(1));
4954 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004955 break;
4956 }
4957 case TypeCheckKind::kAbstractClassCheck: {
4958 // If the class is abstract, we eagerly fetch the super class of the
4959 // object to avoid doing a comparison we know will fail.
4960 NearLabel loop, success;
4961 __ Bind(&loop);
4962 __ movl(out, Address(out, super_offset));
4963 __ MaybeUnpoisonHeapReference(out);
4964 __ testl(out, out);
4965 // If `out` is null, we use it for the result, and jump to `done`.
4966 __ j(kEqual, &done);
4967 if (cls.IsRegister()) {
4968 __ cmpl(out, cls.AsRegister<CpuRegister>());
4969 } else {
4970 DCHECK(cls.IsStackSlot()) << cls;
4971 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4972 }
4973 __ j(kNotEqual, &loop);
4974 __ movl(out, Immediate(1));
4975 if (zero.IsLinked()) {
4976 __ jmp(&done);
4977 }
4978 break;
4979 }
4980 case TypeCheckKind::kClassHierarchyCheck: {
4981 // Walk over the class hierarchy to find a match.
4982 NearLabel loop, success;
4983 __ Bind(&loop);
4984 if (cls.IsRegister()) {
4985 __ cmpl(out, cls.AsRegister<CpuRegister>());
4986 } else {
4987 DCHECK(cls.IsStackSlot()) << cls;
4988 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4989 }
4990 __ j(kEqual, &success);
4991 __ movl(out, Address(out, super_offset));
4992 __ MaybeUnpoisonHeapReference(out);
4993 __ testl(out, out);
4994 __ j(kNotEqual, &loop);
4995 // If `out` is null, we use it for the result, and jump to `done`.
4996 __ jmp(&done);
4997 __ Bind(&success);
4998 __ movl(out, Immediate(1));
4999 if (zero.IsLinked()) {
5000 __ jmp(&done);
5001 }
5002 break;
5003 }
5004 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005005 // Do an exact check.
5006 NearLabel exact_check;
5007 if (cls.IsRegister()) {
5008 __ cmpl(out, cls.AsRegister<CpuRegister>());
5009 } else {
5010 DCHECK(cls.IsStackSlot()) << cls;
5011 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5012 }
5013 __ j(kEqual, &exact_check);
5014 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005015 __ movl(out, Address(out, component_offset));
5016 __ MaybeUnpoisonHeapReference(out);
5017 __ testl(out, out);
5018 // If `out` is null, we use it for the result, and jump to `done`.
5019 __ j(kEqual, &done);
5020 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5021 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005022 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005023 __ movl(out, Immediate(1));
5024 __ jmp(&done);
5025 break;
5026 }
5027 case TypeCheckKind::kArrayCheck: {
5028 if (cls.IsRegister()) {
5029 __ cmpl(out, cls.AsRegister<CpuRegister>());
5030 } else {
5031 DCHECK(cls.IsStackSlot()) << cls;
5032 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5033 }
5034 DCHECK(locations->OnlyCallsOnSlowPath());
5035 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
5036 instruction, /* is_fatal */ false);
5037 codegen_->AddSlowPath(slow_path);
5038 __ j(kNotEqual, slow_path->GetEntryLabel());
5039 __ movl(out, Immediate(1));
5040 if (zero.IsLinked()) {
5041 __ jmp(&done);
5042 }
5043 break;
5044 }
Calin Juravle98893e12015-10-02 21:05:03 +01005045 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005046 case TypeCheckKind::kInterfaceCheck:
5047 default: {
5048 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
5049 instruction,
5050 instruction->GetDexPc(),
5051 nullptr);
5052 if (zero.IsLinked()) {
5053 __ jmp(&done);
5054 }
5055 break;
5056 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005057 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005058
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005059 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005060 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005061 __ xorl(out, out);
5062 }
5063
5064 if (done.IsLinked()) {
5065 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005066 }
5067
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005068 if (slow_path != nullptr) {
5069 __ Bind(slow_path->GetExitLabel());
5070 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005071}
5072
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005073void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005074 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5075 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5076
5077 switch (instruction->GetTypeCheckKind()) {
5078 case TypeCheckKind::kExactCheck:
5079 case TypeCheckKind::kAbstractClassCheck:
5080 case TypeCheckKind::kClassHierarchyCheck:
5081 case TypeCheckKind::kArrayObjectCheck:
5082 call_kind = throws_into_catch
5083 ? LocationSummary::kCallOnSlowPath
5084 : LocationSummary::kNoCall;
5085 break;
Calin Juravle98893e12015-10-02 21:05:03 +01005086 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005087 case TypeCheckKind::kInterfaceCheck:
5088 call_kind = LocationSummary::kCall;
5089 break;
5090 case TypeCheckKind::kArrayCheck:
5091 call_kind = LocationSummary::kCallOnSlowPath;
5092 break;
5093 }
5094
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005095 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005096 instruction, call_kind);
5097 if (call_kind != LocationSummary::kCall) {
5098 locations->SetInAt(0, Location::RequiresRegister());
5099 locations->SetInAt(1, Location::Any());
5100 // Note that TypeCheckSlowPathX86_64 uses this register too.
5101 locations->AddTemp(Location::RequiresRegister());
5102 } else {
5103 InvokeRuntimeCallingConvention calling_convention;
5104 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5105 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5106 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005107}
5108
5109void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
5110 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005111 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005112 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005113 CpuRegister temp = locations->WillCall()
5114 ? CpuRegister(kNoRegister)
5115 : locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005116
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005117 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5118 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5119 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5120 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5121 SlowPathCode* slow_path = nullptr;
5122
5123 if (!locations->WillCall()) {
5124 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
5125 instruction, !locations->CanCall());
5126 codegen_->AddSlowPath(slow_path);
5127 }
5128
5129 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005130 // Avoid null check if we know obj is not null.
5131 if (instruction->MustDoNullCheck()) {
5132 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005133 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005134 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005135
5136 if (locations->WillCall()) {
5137 __ movl(obj, Address(obj, class_offset));
5138 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005139 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005140 __ movl(temp, Address(obj, class_offset));
5141 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005142 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005143
5144 switch (instruction->GetTypeCheckKind()) {
5145 case TypeCheckKind::kExactCheck:
5146 case TypeCheckKind::kArrayCheck: {
5147 if (cls.IsRegister()) {
5148 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5149 } else {
5150 DCHECK(cls.IsStackSlot()) << cls;
5151 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5152 }
5153 // Jump to slow path for throwing the exception or doing a
5154 // more involved array check.
5155 __ j(kNotEqual, slow_path->GetEntryLabel());
5156 break;
5157 }
5158 case TypeCheckKind::kAbstractClassCheck: {
5159 // If the class is abstract, we eagerly fetch the super class of the
5160 // object to avoid doing a comparison we know will fail.
5161 NearLabel loop;
5162 __ Bind(&loop);
5163 __ movl(temp, Address(temp, super_offset));
5164 __ MaybeUnpoisonHeapReference(temp);
5165 __ testl(temp, temp);
5166 // Jump to the slow path to throw the exception.
5167 __ j(kEqual, slow_path->GetEntryLabel());
5168 if (cls.IsRegister()) {
5169 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5170 } else {
5171 DCHECK(cls.IsStackSlot()) << cls;
5172 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5173 }
5174 __ j(kNotEqual, &loop);
5175 break;
5176 }
5177 case TypeCheckKind::kClassHierarchyCheck: {
5178 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005179 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005180 __ Bind(&loop);
5181 if (cls.IsRegister()) {
5182 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5183 } else {
5184 DCHECK(cls.IsStackSlot()) << cls;
5185 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5186 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005187 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005188 __ movl(temp, Address(temp, super_offset));
5189 __ MaybeUnpoisonHeapReference(temp);
5190 __ testl(temp, temp);
5191 __ j(kNotEqual, &loop);
5192 // Jump to the slow path to throw the exception.
5193 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005194 break;
5195 }
5196 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005197 // Do an exact check.
5198 if (cls.IsRegister()) {
5199 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5200 } else {
5201 DCHECK(cls.IsStackSlot()) << cls;
5202 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5203 }
5204 __ j(kEqual, &done);
5205 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005206 __ movl(temp, Address(temp, component_offset));
5207 __ MaybeUnpoisonHeapReference(temp);
5208 __ testl(temp, temp);
5209 __ j(kEqual, slow_path->GetEntryLabel());
5210 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5211 __ j(kNotEqual, slow_path->GetEntryLabel());
5212 break;
5213 }
Calin Juravle98893e12015-10-02 21:05:03 +01005214 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005215 case TypeCheckKind::kInterfaceCheck:
5216 default:
5217 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5218 instruction,
5219 instruction->GetDexPc(),
5220 nullptr);
5221 break;
5222 }
5223 __ Bind(&done);
5224
5225 if (slow_path != nullptr) {
5226 __ Bind(slow_path->GetExitLabel());
5227 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005228}
5229
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005230void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5231 LocationSummary* locations =
5232 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5233 InvokeRuntimeCallingConvention calling_convention;
5234 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5235}
5236
5237void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005238 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5239 : QUICK_ENTRY_POINT(pUnlockObject),
5240 instruction,
5241 instruction->GetDexPc(),
5242 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005243}
5244
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005245void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5246void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5247void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5248
5249void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5250 LocationSummary* locations =
5251 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5252 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5253 || instruction->GetResultType() == Primitive::kPrimLong);
5254 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005255 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005256 locations->SetOut(Location::SameAsFirstInput());
5257}
5258
5259void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5260 HandleBitwiseOperation(instruction);
5261}
5262
5263void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5264 HandleBitwiseOperation(instruction);
5265}
5266
5267void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5268 HandleBitwiseOperation(instruction);
5269}
5270
5271void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5272 LocationSummary* locations = instruction->GetLocations();
5273 Location first = locations->InAt(0);
5274 Location second = locations->InAt(1);
5275 DCHECK(first.Equals(locations->Out()));
5276
5277 if (instruction->GetResultType() == Primitive::kPrimInt) {
5278 if (second.IsRegister()) {
5279 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005280 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005281 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005282 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005283 } else {
5284 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005285 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005286 }
5287 } else if (second.IsConstant()) {
5288 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5289 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005290 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005291 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005292 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005293 } else {
5294 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005295 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005296 }
5297 } else {
5298 Address address(CpuRegister(RSP), second.GetStackIndex());
5299 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005300 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005301 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005302 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005303 } else {
5304 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005305 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005306 }
5307 }
5308 } else {
5309 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005310 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5311 bool second_is_constant = false;
5312 int64_t value = 0;
5313 if (second.IsConstant()) {
5314 second_is_constant = true;
5315 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005316 }
Mark Mendell40741f32015-04-20 22:10:34 -04005317 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005318
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005319 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005320 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005321 if (is_int32_value) {
5322 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5323 } else {
5324 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5325 }
5326 } else if (second.IsDoubleStackSlot()) {
5327 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005328 } else {
5329 __ andq(first_reg, second.AsRegister<CpuRegister>());
5330 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005331 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005332 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005333 if (is_int32_value) {
5334 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5335 } else {
5336 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5337 }
5338 } else if (second.IsDoubleStackSlot()) {
5339 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005340 } else {
5341 __ orq(first_reg, second.AsRegister<CpuRegister>());
5342 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005343 } else {
5344 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005345 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005346 if (is_int32_value) {
5347 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5348 } else {
5349 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5350 }
5351 } else if (second.IsDoubleStackSlot()) {
5352 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005353 } else {
5354 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5355 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005356 }
5357 }
5358}
5359
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005360void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005361 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005362 LOG(FATAL) << "Unreachable";
5363}
5364
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005365void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005366 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005367 LOG(FATAL) << "Unreachable";
5368}
5369
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005370void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
5371 DCHECK(codegen_->IsBaseline());
5372 LocationSummary* locations =
5373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5374 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5375}
5376
5377void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5378 DCHECK(codegen_->IsBaseline());
5379 // Will be generated at use site.
5380}
5381
Mark Mendellfe57faa2015-09-18 09:26:15 -04005382// Simple implementation of packed switch - generate cascaded compare/jumps.
5383void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5384 LocationSummary* locations =
5385 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5386 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04005387 locations->AddTemp(Location::RequiresRegister());
5388 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04005389}
5390
5391void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5392 int32_t lower_bound = switch_instr->GetStartValue();
5393 int32_t num_entries = switch_instr->GetNumEntries();
5394 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04005395 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
5396 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
5397 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
5398
5399 // Remove the bias, if needed.
5400 Register value_reg_out = value_reg_in.AsRegister();
5401 if (lower_bound != 0) {
5402 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
5403 value_reg_out = temp_reg.AsRegister();
5404 }
5405 CpuRegister value_reg(value_reg_out);
5406
5407 // Is the value in range?
Mark Mendellfe57faa2015-09-18 09:26:15 -04005408 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
Mark Mendell9c86b482015-09-18 13:36:07 -04005409 __ cmpl(value_reg, Immediate(num_entries - 1));
5410 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005411
Mark Mendell9c86b482015-09-18 13:36:07 -04005412 // We are in the range of the table.
5413 // Load the address of the jump table in the constant area.
5414 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005415
Mark Mendell9c86b482015-09-18 13:36:07 -04005416 // Load the (signed) offset from the jump table.
5417 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
5418
5419 // Add the offset to the address of the table base.
5420 __ addq(temp_reg, base_reg);
5421
5422 // And jump.
5423 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005424}
5425
Mark Mendell92e83bf2015-05-07 11:25:03 -04005426void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
5427 if (value == 0) {
5428 __ xorl(dest, dest);
5429 } else if (value > 0 && IsInt<32>(value)) {
5430 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
5431 __ movl(dest, Immediate(static_cast<int32_t>(value)));
5432 } else {
5433 __ movq(dest, Immediate(value));
5434 }
5435}
5436
Mark Mendellcfa410b2015-05-25 16:02:44 -04005437void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
5438 DCHECK(dest.IsDoubleStackSlot());
5439 if (IsInt<32>(value)) {
5440 // Can move directly as an int32 constant.
5441 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
5442 Immediate(static_cast<int32_t>(value)));
5443 } else {
5444 Load64BitValue(CpuRegister(TMP), value);
5445 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
5446 }
5447}
5448
Mark Mendell9c86b482015-09-18 13:36:07 -04005449/**
5450 * Class to handle late fixup of offsets into constant area.
5451 */
5452class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
5453 public:
5454 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
5455 : codegen_(&codegen), offset_into_constant_area_(offset) {}
5456
5457 protected:
5458 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
5459
5460 CodeGeneratorX86_64* codegen_;
5461
5462 private:
5463 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5464 // Patch the correct offset for the instruction. We use the address of the
5465 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
5466 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
5467 int32_t relative_position = constant_offset - pos;
5468
5469 // Patch in the right value.
5470 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5471 }
5472
5473 // Location in constant area that the fixup refers to.
5474 size_t offset_into_constant_area_;
5475};
5476
5477/**
5478 t * Class to handle late fixup of offsets to a jump table that will be created in the
5479 * constant area.
5480 */
5481class JumpTableRIPFixup : public RIPFixup {
5482 public:
5483 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
5484 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
5485
5486 void CreateJumpTable() {
5487 X86_64Assembler* assembler = codegen_->GetAssembler();
5488
5489 // Ensure that the reference to the jump table has the correct offset.
5490 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
5491 SetOffset(offset_in_constant_table);
5492
5493 // Compute the offset from the start of the function to this jump table.
5494 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
5495
5496 // Populate the jump table with the correct values for the jump table.
5497 int32_t num_entries = switch_instr_->GetNumEntries();
5498 HBasicBlock* block = switch_instr_->GetBlock();
5499 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
5500 // The value that we want is the target offset - the position of the table.
5501 for (int32_t i = 0; i < num_entries; i++) {
5502 HBasicBlock* b = successors[i];
5503 Label* l = codegen_->GetLabelOf(b);
5504 DCHECK(l->IsBound());
5505 int32_t offset_to_block = l->Position() - current_table_offset;
5506 assembler->AppendInt32(offset_to_block);
5507 }
5508 }
5509
5510 private:
5511 const HPackedSwitch* switch_instr_;
5512};
5513
Mark Mendellf55c3e02015-03-26 21:07:46 -04005514void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
5515 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04005516 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04005517 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
5518 // 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 -04005519 assembler->Align(4, 0);
5520 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04005521
5522 // Populate any jump tables.
5523 for (auto jump_table : fixups_to_jump_tables_) {
5524 jump_table->CreateJumpTable();
5525 }
5526
5527 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04005528 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04005529 }
5530
5531 // And finish up.
5532 CodeGenerator::Finalize(allocator);
5533}
5534
Mark Mendellf55c3e02015-03-26 21:07:46 -04005535Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
5536 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5537 return Address::RIP(fixup);
5538}
5539
5540Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
5541 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5542 return Address::RIP(fixup);
5543}
5544
5545Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
5546 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5547 return Address::RIP(fixup);
5548}
5549
5550Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
5551 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5552 return Address::RIP(fixup);
5553}
5554
Andreas Gampe85b62f22015-09-09 13:15:38 -07005555// TODO: trg as memory.
5556void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5557 if (!trg.IsValid()) {
5558 DCHECK(type == Primitive::kPrimVoid);
5559 return;
5560 }
5561
5562 DCHECK_NE(type, Primitive::kPrimVoid);
5563
5564 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
5565 if (trg.Equals(return_loc)) {
5566 return;
5567 }
5568
5569 // Let the parallel move resolver take care of all of this.
5570 HParallelMove parallel_move(GetGraph()->GetArena());
5571 parallel_move.AddMove(return_loc, trg, type, nullptr);
5572 GetMoveResolver()->EmitNativeCode(&parallel_move);
5573}
5574
Mark Mendell9c86b482015-09-18 13:36:07 -04005575Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
5576 // Create a fixup to be used to create and address the jump table.
5577 JumpTableRIPFixup* table_fixup =
5578 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
5579
5580 // We have to populate the jump tables.
5581 fixups_to_jump_tables_.push_back(table_fixup);
5582 return Address::RIP(table_fixup);
5583}
5584
Roland Levillain4d027112015-07-01 15:41:14 +01005585#undef __
5586
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005587} // namespace x86_64
5588} // namespace art