blob: 4b7952b1e4baf40b66c35f69bdf9a48b33da2c3d [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 {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001186 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001187 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1188 // 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.
1192 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001193 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001194 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1195 && !Primitive::IsFloatingPointType(type);
1196
Roland Levillain4fa13f62015-07-06 18:11:54 +01001197 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001198 if (!eflags_set) {
1199 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001200 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001201 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001202 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001203 } else {
1204 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1205 Immediate(0));
1206 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001207 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001208 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001209 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001210 }
1211 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001212 // Condition has not been materialized, use its inputs as the
1213 // comparison and its condition as the branch condition.
1214
Mark Mendellc4701932015-04-10 13:18:51 -04001215 // Is this a long or FP comparison that has been folded into the HCondition?
1216 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001217 // Generate the comparison directly.
Mark Mendellc4701932015-04-10 13:18:51 -04001218 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1219 true_target, false_target, always_true_target);
1220 return;
1221 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001222
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001223 Location lhs = cond->GetLocations()->InAt(0);
1224 Location rhs = cond->GetLocations()->InAt(1);
1225 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001227 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001228 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001229 if (constant == 0) {
1230 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1231 } else {
1232 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1233 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001234 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001236 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1237 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001238 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001239 }
Dave Allison20dfc792014-06-16 20:44:29 -07001240 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001241 if (false_target != nullptr) {
1242 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001243 }
1244}
1245
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001246void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1247 LocationSummary* locations =
1248 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1249 HInstruction* cond = if_instr->InputAt(0);
1250 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1251 locations->SetInAt(0, Location::Any());
1252 }
1253}
1254
1255void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1256 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1257 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1258 Label* always_true_target = true_target;
1259 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1260 if_instr->IfTrueSuccessor())) {
1261 always_true_target = nullptr;
1262 }
1263 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1264 if_instr->IfFalseSuccessor())) {
1265 false_target = nullptr;
1266 }
1267 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1268}
1269
1270void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1271 LocationSummary* locations = new (GetGraph()->GetArena())
1272 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1273 HInstruction* cond = deoptimize->InputAt(0);
Aart Bikbb245d12015-10-19 11:05:03 -07001274 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001275 locations->SetInAt(0, Location::Any());
1276 }
1277}
1278
1279void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001280 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001281 DeoptimizationSlowPathX86_64(deoptimize);
1282 codegen_->AddSlowPath(slow_path);
1283 Label* slow_path_entry = slow_path->GetEntryLabel();
1284 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1285}
1286
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001287void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1288 local->SetLocations(nullptr);
1289}
1290
1291void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1292 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1293}
1294
1295void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1296 local->SetLocations(nullptr);
1297}
1298
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001299void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001300 // Nothing to do, this is driven by the code generator.
1301}
1302
1303void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001304 LocationSummary* locations =
1305 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001306 switch (store->InputAt(1)->GetType()) {
1307 case Primitive::kPrimBoolean:
1308 case Primitive::kPrimByte:
1309 case Primitive::kPrimChar:
1310 case Primitive::kPrimShort:
1311 case Primitive::kPrimInt:
1312 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001313 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001314 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1315 break;
1316
1317 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001318 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001319 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1320 break;
1321
1322 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001323 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001324 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001325}
1326
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001327void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001328}
1329
Roland Levillain0d37cd02015-05-27 16:39:19 +01001330void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001331 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001332 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001333 // Handle the long/FP comparisons made in instruction simplification.
1334 switch (cond->InputAt(0)->GetType()) {
1335 case Primitive::kPrimLong:
1336 locations->SetInAt(0, Location::RequiresRegister());
1337 locations->SetInAt(1, Location::Any());
1338 break;
1339 case Primitive::kPrimFloat:
1340 case Primitive::kPrimDouble:
1341 locations->SetInAt(0, Location::RequiresFpuRegister());
1342 locations->SetInAt(1, Location::Any());
1343 break;
1344 default:
1345 locations->SetInAt(0, Location::RequiresRegister());
1346 locations->SetInAt(1, Location::Any());
1347 break;
1348 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001349 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001350 locations->SetOut(Location::RequiresRegister());
1351 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001352}
1353
Roland Levillain0d37cd02015-05-27 16:39:19 +01001354void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001355 if (!cond->NeedsMaterialization()) {
1356 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001357 }
Mark Mendellc4701932015-04-10 13:18:51 -04001358
1359 LocationSummary* locations = cond->GetLocations();
1360 Location lhs = locations->InAt(0);
1361 Location rhs = locations->InAt(1);
1362 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1363 Label true_label, false_label;
1364
1365 switch (cond->InputAt(0)->GetType()) {
1366 default:
1367 // Integer case.
1368
1369 // Clear output register: setcc only sets the low byte.
1370 __ xorl(reg, reg);
1371
1372 if (rhs.IsRegister()) {
1373 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1374 } else if (rhs.IsConstant()) {
1375 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1376 if (constant == 0) {
1377 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1378 } else {
1379 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1380 }
1381 } else {
1382 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1383 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001384 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001385 return;
1386 case Primitive::kPrimLong:
1387 // Clear output register: setcc only sets the low byte.
1388 __ xorl(reg, reg);
1389
1390 if (rhs.IsRegister()) {
1391 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1392 } else if (rhs.IsConstant()) {
1393 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1394 if (IsInt<32>(value)) {
1395 if (value == 0) {
1396 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1397 } else {
1398 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1399 }
1400 } else {
1401 // Value won't fit in an int.
1402 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1403 }
1404 } else {
1405 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1406 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001407 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001408 return;
1409 case Primitive::kPrimFloat: {
1410 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1411 if (rhs.IsConstant()) {
1412 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1413 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1414 } else if (rhs.IsStackSlot()) {
1415 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1416 } else {
1417 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1418 }
1419 GenerateFPJumps(cond, &true_label, &false_label);
1420 break;
1421 }
1422 case Primitive::kPrimDouble: {
1423 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1424 if (rhs.IsConstant()) {
1425 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1426 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1427 } else if (rhs.IsDoubleStackSlot()) {
1428 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1429 } else {
1430 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1431 }
1432 GenerateFPJumps(cond, &true_label, &false_label);
1433 break;
1434 }
1435 }
1436
1437 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001438 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001439
Roland Levillain4fa13f62015-07-06 18:11:54 +01001440 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001441 __ Bind(&false_label);
1442 __ xorl(reg, reg);
1443 __ jmp(&done_label);
1444
Roland Levillain4fa13f62015-07-06 18:11:54 +01001445 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001446 __ Bind(&true_label);
1447 __ movl(reg, Immediate(1));
1448 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001449}
1450
1451void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1452 VisitCondition(comp);
1453}
1454
1455void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1456 VisitCondition(comp);
1457}
1458
1459void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1460 VisitCondition(comp);
1461}
1462
1463void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1464 VisitCondition(comp);
1465}
1466
1467void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1468 VisitCondition(comp);
1469}
1470
1471void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1472 VisitCondition(comp);
1473}
1474
1475void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1476 VisitCondition(comp);
1477}
1478
1479void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1480 VisitCondition(comp);
1481}
1482
1483void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1484 VisitCondition(comp);
1485}
1486
1487void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1488 VisitCondition(comp);
1489}
1490
1491void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1492 VisitCondition(comp);
1493}
1494
1495void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1496 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001497}
1498
Aart Bike9f37602015-10-09 11:15:55 -07001499void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
1500 VisitCondition(comp);
1501}
1502
1503void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
1504 VisitCondition(comp);
1505}
1506
1507void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
1508 VisitCondition(comp);
1509}
1510
1511void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
1512 VisitCondition(comp);
1513}
1514
1515void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
1516 VisitCondition(comp);
1517}
1518
1519void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
1520 VisitCondition(comp);
1521}
1522
1523void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
1524 VisitCondition(comp);
1525}
1526
1527void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
1528 VisitCondition(comp);
1529}
1530
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001531void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001532 LocationSummary* locations =
1533 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001534 switch (compare->InputAt(0)->GetType()) {
1535 case Primitive::kPrimLong: {
1536 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001537 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001538 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1539 break;
1540 }
1541 case Primitive::kPrimFloat:
1542 case Primitive::kPrimDouble: {
1543 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001544 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001545 locations->SetOut(Location::RequiresRegister());
1546 break;
1547 }
1548 default:
1549 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1550 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001551}
1552
1553void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001554 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001555 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001556 Location left = locations->InAt(0);
1557 Location right = locations->InAt(1);
1558
Mark Mendell0c9497d2015-08-21 09:30:05 -04001559 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001560 Primitive::Type type = compare->InputAt(0)->GetType();
1561 switch (type) {
1562 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001563 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1564 if (right.IsConstant()) {
1565 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001566 if (IsInt<32>(value)) {
1567 if (value == 0) {
1568 __ testq(left_reg, left_reg);
1569 } else {
1570 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1571 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001572 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001573 // Value won't fit in an int.
1574 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001575 }
Mark Mendell40741f32015-04-20 22:10:34 -04001576 } else if (right.IsDoubleStackSlot()) {
1577 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001578 } else {
1579 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1580 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001581 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001582 }
1583 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001584 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1585 if (right.IsConstant()) {
1586 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1587 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1588 } else if (right.IsStackSlot()) {
1589 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1590 } else {
1591 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1592 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001593 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1594 break;
1595 }
1596 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001597 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1598 if (right.IsConstant()) {
1599 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1600 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1601 } else if (right.IsDoubleStackSlot()) {
1602 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1603 } else {
1604 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1605 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001606 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1607 break;
1608 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001609 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001610 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001611 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001612 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001613 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001614 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001615
Calin Juravle91debbc2014-11-26 19:01:09 +00001616 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001617 __ movl(out, Immediate(1));
1618 __ jmp(&done);
1619
1620 __ Bind(&less);
1621 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001622
1623 __ Bind(&done);
1624}
1625
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001626void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001627 LocationSummary* locations =
1628 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001629 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001630}
1631
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001632void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001633 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001634}
1635
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001636void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1637 LocationSummary* locations =
1638 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1639 locations->SetOut(Location::ConstantLocation(constant));
1640}
1641
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001642void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001643 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001644}
1645
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001646void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001647 LocationSummary* locations =
1648 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001649 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001650}
1651
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001652void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001653 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001654}
1655
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001656void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1657 LocationSummary* locations =
1658 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1659 locations->SetOut(Location::ConstantLocation(constant));
1660}
1661
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001662void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001663 // Will be generated at use site.
1664}
1665
1666void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1667 LocationSummary* locations =
1668 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1669 locations->SetOut(Location::ConstantLocation(constant));
1670}
1671
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001672void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1673 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001674 // Will be generated at use site.
1675}
1676
Calin Juravle27df7582015-04-17 19:12:31 +01001677void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1678 memory_barrier->SetLocations(nullptr);
1679}
1680
1681void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1682 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1683}
1684
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001685void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1686 ret->SetLocations(nullptr);
1687}
1688
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001689void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001690 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001691}
1692
1693void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001694 LocationSummary* locations =
1695 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001696 switch (ret->InputAt(0)->GetType()) {
1697 case Primitive::kPrimBoolean:
1698 case Primitive::kPrimByte:
1699 case Primitive::kPrimChar:
1700 case Primitive::kPrimShort:
1701 case Primitive::kPrimInt:
1702 case Primitive::kPrimNot:
1703 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001704 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001705 break;
1706
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001707 case Primitive::kPrimFloat:
1708 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001709 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001710 break;
1711
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001712 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001713 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001714 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001715}
1716
1717void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1718 if (kIsDebugBuild) {
1719 switch (ret->InputAt(0)->GetType()) {
1720 case Primitive::kPrimBoolean:
1721 case Primitive::kPrimByte:
1722 case Primitive::kPrimChar:
1723 case Primitive::kPrimShort:
1724 case Primitive::kPrimInt:
1725 case Primitive::kPrimNot:
1726 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001727 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001728 break;
1729
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001730 case Primitive::kPrimFloat:
1731 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001732 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001733 XMM0);
1734 break;
1735
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001736 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001737 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001738 }
1739 }
1740 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001741}
1742
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001743Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1744 switch (type) {
1745 case Primitive::kPrimBoolean:
1746 case Primitive::kPrimByte:
1747 case Primitive::kPrimChar:
1748 case Primitive::kPrimShort:
1749 case Primitive::kPrimInt:
1750 case Primitive::kPrimNot:
1751 case Primitive::kPrimLong:
1752 return Location::RegisterLocation(RAX);
1753
1754 case Primitive::kPrimVoid:
1755 return Location::NoLocation();
1756
1757 case Primitive::kPrimDouble:
1758 case Primitive::kPrimFloat:
1759 return Location::FpuRegisterLocation(XMM0);
1760 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001761
1762 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001763}
1764
1765Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1766 return Location::RegisterLocation(kMethodRegisterArgument);
1767}
1768
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001769Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001770 switch (type) {
1771 case Primitive::kPrimBoolean:
1772 case Primitive::kPrimByte:
1773 case Primitive::kPrimChar:
1774 case Primitive::kPrimShort:
1775 case Primitive::kPrimInt:
1776 case Primitive::kPrimNot: {
1777 uint32_t index = gp_index_++;
1778 stack_index_++;
1779 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001780 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001781 } else {
1782 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1783 }
1784 }
1785
1786 case Primitive::kPrimLong: {
1787 uint32_t index = gp_index_;
1788 stack_index_ += 2;
1789 if (index < calling_convention.GetNumberOfRegisters()) {
1790 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001791 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001792 } else {
1793 gp_index_ += 2;
1794 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1795 }
1796 }
1797
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001798 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001799 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001800 stack_index_++;
1801 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001802 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001803 } else {
1804 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1805 }
1806 }
1807
1808 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001809 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001810 stack_index_ += 2;
1811 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001812 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001813 } else {
1814 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1815 }
1816 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001817
1818 case Primitive::kPrimVoid:
1819 LOG(FATAL) << "Unexpected parameter type " << type;
1820 break;
1821 }
1822 return Location();
1823}
1824
Calin Juravle175dc732015-08-25 15:42:32 +01001825void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1826 // The trampoline uses the same calling convention as dex calling conventions,
1827 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1828 // the method_idx.
1829 HandleInvoke(invoke);
1830}
1831
1832void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1833 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1834}
1835
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001836void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001837 // When we do not run baseline, explicit clinit checks triggered by static
1838 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1839 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001840
Mark Mendellfb8d2792015-03-31 22:16:59 -04001841 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001842 if (intrinsic.TryDispatch(invoke)) {
1843 return;
1844 }
1845
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001846 HandleInvoke(invoke);
1847}
1848
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001849static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1850 if (invoke->GetLocations()->Intrinsified()) {
1851 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1852 intrinsic.Dispatch(invoke);
1853 return true;
1854 }
1855 return false;
1856}
1857
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001858void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001859 // When we do not run baseline, explicit clinit checks triggered by static
1860 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1861 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001862
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001863 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1864 return;
1865 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001866
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001867 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001868 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001869 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001870 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001871}
1872
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001873void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001874 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001875 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001876}
1877
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001878void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001879 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001880 if (intrinsic.TryDispatch(invoke)) {
1881 return;
1882 }
1883
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001884 HandleInvoke(invoke);
1885}
1886
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001887void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001888 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1889 return;
1890 }
1891
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001892 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001893
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001894 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001895 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001896}
1897
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001898void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1899 HandleInvoke(invoke);
1900 // Add the hidden argument.
1901 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1902}
1903
1904void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1905 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001906 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001907 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1908 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001909 LocationSummary* locations = invoke->GetLocations();
1910 Location receiver = locations->InAt(0);
1911 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1912
1913 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001914 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1915 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001916
1917 // temp = object->GetClass();
1918 if (receiver.IsStackSlot()) {
1919 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1920 __ movl(temp, Address(temp, class_offset));
1921 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001922 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001923 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001924 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001925 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001926 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001927 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001928 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001929 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001930 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001931
1932 DCHECK(!codegen_->IsLeafMethod());
1933 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1934}
1935
Roland Levillain88cb1752014-10-20 16:36:47 +01001936void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1937 LocationSummary* locations =
1938 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1939 switch (neg->GetResultType()) {
1940 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001941 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001942 locations->SetInAt(0, Location::RequiresRegister());
1943 locations->SetOut(Location::SameAsFirstInput());
1944 break;
1945
Roland Levillain88cb1752014-10-20 16:36:47 +01001946 case Primitive::kPrimFloat:
1947 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001948 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001949 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001950 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001951 break;
1952
1953 default:
1954 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1955 }
1956}
1957
1958void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1959 LocationSummary* locations = neg->GetLocations();
1960 Location out = locations->Out();
1961 Location in = locations->InAt(0);
1962 switch (neg->GetResultType()) {
1963 case Primitive::kPrimInt:
1964 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001965 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001966 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001967 break;
1968
1969 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001970 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001971 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001972 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001973 break;
1974
Roland Levillain5368c212014-11-27 15:03:41 +00001975 case Primitive::kPrimFloat: {
1976 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001977 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001978 // Implement float negation with an exclusive or with value
1979 // 0x80000000 (mask for bit 31, representing the sign of a
1980 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001981 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001982 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001983 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001984 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001985
Roland Levillain5368c212014-11-27 15:03:41 +00001986 case Primitive::kPrimDouble: {
1987 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001988 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001989 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001990 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001991 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001992 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001993 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001994 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001995 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001996
1997 default:
1998 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1999 }
2000}
2001
Roland Levillaindff1f282014-11-05 14:15:05 +00002002void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2003 LocationSummary* locations =
2004 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2005 Primitive::Type result_type = conversion->GetResultType();
2006 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002007 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002008
David Brazdilb2bd1c52015-03-25 11:17:37 +00002009 // The Java language does not allow treating boolean as an integral type but
2010 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002011
Roland Levillaindff1f282014-11-05 14:15:05 +00002012 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002013 case Primitive::kPrimByte:
2014 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002015 case Primitive::kPrimBoolean:
2016 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002017 case Primitive::kPrimShort:
2018 case Primitive::kPrimInt:
2019 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002020 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002021 locations->SetInAt(0, Location::Any());
2022 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2023 break;
2024
2025 default:
2026 LOG(FATAL) << "Unexpected type conversion from " << input_type
2027 << " to " << result_type;
2028 }
2029 break;
2030
Roland Levillain01a8d712014-11-14 16:27:39 +00002031 case Primitive::kPrimShort:
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 Levillain01a8d712014-11-14 16:27:39 +00002035 case Primitive::kPrimByte:
2036 case Primitive::kPrimInt:
2037 case Primitive::kPrimChar:
2038 // Processing a Dex `int-to-short' instruction.
2039 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 Levillain946e1432014-11-11 17:35:19 +00002049 case Primitive::kPrimInt:
2050 switch (input_type) {
2051 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002052 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002053 locations->SetInAt(0, Location::Any());
2054 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2055 break;
2056
2057 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002058 // Processing a Dex `float-to-int' instruction.
2059 locations->SetInAt(0, Location::RequiresFpuRegister());
2060 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002061 break;
2062
Roland Levillain946e1432014-11-11 17:35:19 +00002063 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002064 // Processing a Dex `double-to-int' instruction.
2065 locations->SetInAt(0, Location::RequiresFpuRegister());
2066 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002067 break;
2068
2069 default:
2070 LOG(FATAL) << "Unexpected type conversion from " << input_type
2071 << " to " << result_type;
2072 }
2073 break;
2074
Roland Levillaindff1f282014-11-05 14:15:05 +00002075 case Primitive::kPrimLong:
2076 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002077 case Primitive::kPrimBoolean:
2078 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002079 case Primitive::kPrimByte:
2080 case Primitive::kPrimShort:
2081 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002082 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002083 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002084 // TODO: We would benefit from a (to-be-implemented)
2085 // Location::RegisterOrStackSlot requirement for this input.
2086 locations->SetInAt(0, Location::RequiresRegister());
2087 locations->SetOut(Location::RequiresRegister());
2088 break;
2089
2090 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002091 // Processing a Dex `float-to-long' instruction.
2092 locations->SetInAt(0, Location::RequiresFpuRegister());
2093 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002094 break;
2095
Roland Levillaindff1f282014-11-05 14:15:05 +00002096 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002097 // Processing a Dex `double-to-long' instruction.
2098 locations->SetInAt(0, Location::RequiresFpuRegister());
2099 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002100 break;
2101
2102 default:
2103 LOG(FATAL) << "Unexpected type conversion from " << input_type
2104 << " to " << result_type;
2105 }
2106 break;
2107
Roland Levillain981e4542014-11-14 11:47:14 +00002108 case Primitive::kPrimChar:
2109 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002110 case Primitive::kPrimBoolean:
2111 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002112 case Primitive::kPrimByte:
2113 case Primitive::kPrimShort:
2114 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002115 // Processing a Dex `int-to-char' instruction.
2116 locations->SetInAt(0, Location::Any());
2117 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2118 break;
2119
2120 default:
2121 LOG(FATAL) << "Unexpected type conversion from " << input_type
2122 << " to " << result_type;
2123 }
2124 break;
2125
Roland Levillaindff1f282014-11-05 14:15:05 +00002126 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002127 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002128 case Primitive::kPrimBoolean:
2129 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002130 case Primitive::kPrimByte:
2131 case Primitive::kPrimShort:
2132 case Primitive::kPrimInt:
2133 case Primitive::kPrimChar:
2134 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002135 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002136 locations->SetOut(Location::RequiresFpuRegister());
2137 break;
2138
2139 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002140 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002141 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002142 locations->SetOut(Location::RequiresFpuRegister());
2143 break;
2144
Roland Levillaincff13742014-11-17 14:32:17 +00002145 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002146 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002147 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002148 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002149 break;
2150
2151 default:
2152 LOG(FATAL) << "Unexpected type conversion from " << input_type
2153 << " to " << result_type;
2154 };
2155 break;
2156
Roland Levillaindff1f282014-11-05 14:15:05 +00002157 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002158 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002159 case Primitive::kPrimBoolean:
2160 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002161 case Primitive::kPrimByte:
2162 case Primitive::kPrimShort:
2163 case Primitive::kPrimInt:
2164 case Primitive::kPrimChar:
2165 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002166 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002167 locations->SetOut(Location::RequiresFpuRegister());
2168 break;
2169
2170 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002171 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002172 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002173 locations->SetOut(Location::RequiresFpuRegister());
2174 break;
2175
Roland Levillaincff13742014-11-17 14:32:17 +00002176 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002177 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002178 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002179 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002180 break;
2181
2182 default:
2183 LOG(FATAL) << "Unexpected type conversion from " << input_type
2184 << " to " << result_type;
2185 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002186 break;
2187
2188 default:
2189 LOG(FATAL) << "Unexpected type conversion from " << input_type
2190 << " to " << result_type;
2191 }
2192}
2193
2194void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2195 LocationSummary* locations = conversion->GetLocations();
2196 Location out = locations->Out();
2197 Location in = locations->InAt(0);
2198 Primitive::Type result_type = conversion->GetResultType();
2199 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002200 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002201 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002202 case Primitive::kPrimByte:
2203 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002204 case Primitive::kPrimBoolean:
2205 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002206 case Primitive::kPrimShort:
2207 case Primitive::kPrimInt:
2208 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002209 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002210 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002211 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002212 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002213 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002214 Address(CpuRegister(RSP), in.GetStackIndex()));
2215 } else {
2216 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002217 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002218 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2219 }
2220 break;
2221
2222 default:
2223 LOG(FATAL) << "Unexpected type conversion from " << input_type
2224 << " to " << result_type;
2225 }
2226 break;
2227
Roland Levillain01a8d712014-11-14 16:27:39 +00002228 case Primitive::kPrimShort:
2229 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002230 case Primitive::kPrimBoolean:
2231 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002232 case Primitive::kPrimByte:
2233 case Primitive::kPrimInt:
2234 case Primitive::kPrimChar:
2235 // Processing a Dex `int-to-short' instruction.
2236 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002237 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002238 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002239 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002240 Address(CpuRegister(RSP), in.GetStackIndex()));
2241 } else {
2242 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002243 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002244 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2245 }
2246 break;
2247
2248 default:
2249 LOG(FATAL) << "Unexpected type conversion from " << input_type
2250 << " to " << result_type;
2251 }
2252 break;
2253
Roland Levillain946e1432014-11-11 17:35:19 +00002254 case Primitive::kPrimInt:
2255 switch (input_type) {
2256 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002257 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002258 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002259 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002260 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002261 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002262 Address(CpuRegister(RSP), in.GetStackIndex()));
2263 } else {
2264 DCHECK(in.IsConstant());
2265 DCHECK(in.GetConstant()->IsLongConstant());
2266 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002267 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002268 }
2269 break;
2270
Roland Levillain3f8f9362014-12-02 17:45:01 +00002271 case Primitive::kPrimFloat: {
2272 // Processing a Dex `float-to-int' instruction.
2273 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2274 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002275 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002276
2277 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002278 // if input >= (float)INT_MAX goto done
2279 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002280 __ j(kAboveEqual, &done);
2281 // if input == NaN goto nan
2282 __ j(kUnordered, &nan);
2283 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002284 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002285 __ jmp(&done);
2286 __ Bind(&nan);
2287 // output = 0
2288 __ xorl(output, output);
2289 __ Bind(&done);
2290 break;
2291 }
2292
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002293 case Primitive::kPrimDouble: {
2294 // Processing a Dex `double-to-int' instruction.
2295 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2296 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002297 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002298
2299 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002300 // if input >= (double)INT_MAX goto done
2301 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002302 __ j(kAboveEqual, &done);
2303 // if input == NaN goto nan
2304 __ j(kUnordered, &nan);
2305 // output = double-to-int-truncate(input)
2306 __ cvttsd2si(output, input);
2307 __ jmp(&done);
2308 __ Bind(&nan);
2309 // output = 0
2310 __ xorl(output, output);
2311 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002312 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002313 }
Roland Levillain946e1432014-11-11 17:35:19 +00002314
2315 default:
2316 LOG(FATAL) << "Unexpected type conversion from " << input_type
2317 << " to " << result_type;
2318 }
2319 break;
2320
Roland Levillaindff1f282014-11-05 14:15:05 +00002321 case Primitive::kPrimLong:
2322 switch (input_type) {
2323 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002324 case Primitive::kPrimBoolean:
2325 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002326 case Primitive::kPrimByte:
2327 case Primitive::kPrimShort:
2328 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002329 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002330 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002331 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002332 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002333 break;
2334
Roland Levillain624279f2014-12-04 11:54:28 +00002335 case Primitive::kPrimFloat: {
2336 // Processing a Dex `float-to-long' instruction.
2337 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2338 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002339 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002340
Mark Mendell92e83bf2015-05-07 11:25:03 -04002341 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002342 // if input >= (float)LONG_MAX goto done
2343 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002344 __ j(kAboveEqual, &done);
2345 // if input == NaN goto nan
2346 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002347 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002348 __ cvttss2si(output, input, true);
2349 __ jmp(&done);
2350 __ Bind(&nan);
2351 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002352 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002353 __ Bind(&done);
2354 break;
2355 }
2356
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002357 case Primitive::kPrimDouble: {
2358 // Processing a Dex `double-to-long' instruction.
2359 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2360 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002361 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002362
Mark Mendell92e83bf2015-05-07 11:25:03 -04002363 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002364 // if input >= (double)LONG_MAX goto done
2365 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002366 __ j(kAboveEqual, &done);
2367 // if input == NaN goto nan
2368 __ j(kUnordered, &nan);
2369 // output = double-to-long-truncate(input)
2370 __ cvttsd2si(output, input, true);
2371 __ jmp(&done);
2372 __ Bind(&nan);
2373 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002374 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002375 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002376 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002377 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 }
2383 break;
2384
Roland Levillain981e4542014-11-14 11:47:14 +00002385 case Primitive::kPrimChar:
2386 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002387 case Primitive::kPrimBoolean:
2388 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002389 case Primitive::kPrimByte:
2390 case Primitive::kPrimShort:
2391 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002392 // Processing a Dex `int-to-char' instruction.
2393 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002394 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002395 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002396 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002397 Address(CpuRegister(RSP), in.GetStackIndex()));
2398 } else {
2399 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002400 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002401 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2402 }
2403 break;
2404
2405 default:
2406 LOG(FATAL) << "Unexpected type conversion from " << input_type
2407 << " to " << result_type;
2408 }
2409 break;
2410
Roland Levillaindff1f282014-11-05 14:15:05 +00002411 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002412 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002413 case Primitive::kPrimBoolean:
2414 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002415 case Primitive::kPrimByte:
2416 case Primitive::kPrimShort:
2417 case Primitive::kPrimInt:
2418 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002419 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002420 if (in.IsRegister()) {
2421 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2422 } else if (in.IsConstant()) {
2423 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2424 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2425 if (v == 0) {
2426 __ xorps(dest, dest);
2427 } else {
2428 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2429 }
2430 } else {
2431 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2432 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2433 }
Roland Levillaincff13742014-11-17 14:32:17 +00002434 break;
2435
2436 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002437 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002438 if (in.IsRegister()) {
2439 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2440 } else if (in.IsConstant()) {
2441 int64_t v = in.GetConstant()->AsLongConstant()->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()), true);
2451 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002452 break;
2453
Roland Levillaincff13742014-11-17 14:32:17 +00002454 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002455 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002456 if (in.IsFpuRegister()) {
2457 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2458 } else if (in.IsConstant()) {
2459 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2460 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2461 if (bit_cast<int64_t, double>(v) == 0) {
2462 __ xorps(dest, dest);
2463 } else {
2464 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2465 }
2466 } else {
2467 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2468 Address(CpuRegister(RSP), in.GetStackIndex()));
2469 }
Roland Levillaincff13742014-11-17 14:32:17 +00002470 break;
2471
2472 default:
2473 LOG(FATAL) << "Unexpected type conversion from " << input_type
2474 << " to " << result_type;
2475 };
2476 break;
2477
Roland Levillaindff1f282014-11-05 14:15:05 +00002478 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002479 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002480 case Primitive::kPrimBoolean:
2481 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002482 case Primitive::kPrimByte:
2483 case Primitive::kPrimShort:
2484 case Primitive::kPrimInt:
2485 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002486 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002487 if (in.IsRegister()) {
2488 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2489 } else if (in.IsConstant()) {
2490 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2491 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2492 if (v == 0) {
2493 __ xorpd(dest, dest);
2494 } else {
2495 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2496 }
2497 } else {
2498 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2499 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2500 }
Roland Levillaincff13742014-11-17 14:32:17 +00002501 break;
2502
2503 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002504 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002505 if (in.IsRegister()) {
2506 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2507 } else if (in.IsConstant()) {
2508 int64_t v = in.GetConstant()->AsLongConstant()->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()), true);
2518 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002519 break;
2520
Roland Levillaincff13742014-11-17 14:32:17 +00002521 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002522 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002523 if (in.IsFpuRegister()) {
2524 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2525 } else if (in.IsConstant()) {
2526 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2527 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2528 if (bit_cast<int32_t, float>(v) == 0) {
2529 __ xorpd(dest, dest);
2530 } else {
2531 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2532 }
2533 } else {
2534 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2535 Address(CpuRegister(RSP), in.GetStackIndex()));
2536 }
Roland Levillaincff13742014-11-17 14:32:17 +00002537 break;
2538
2539 default:
2540 LOG(FATAL) << "Unexpected type conversion from " << input_type
2541 << " to " << result_type;
2542 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002543 break;
2544
2545 default:
2546 LOG(FATAL) << "Unexpected type conversion from " << input_type
2547 << " to " << result_type;
2548 }
2549}
2550
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002551void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002552 LocationSummary* locations =
2553 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002554 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002555 case Primitive::kPrimInt: {
2556 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002557 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002559 break;
2560 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002561
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002562 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002563 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002564 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002565 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002567 break;
2568 }
2569
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002570 case Primitive::kPrimDouble:
2571 case Primitive::kPrimFloat: {
2572 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002573 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002574 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002575 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002576 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002577
2578 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002579 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002580 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002581}
2582
2583void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2584 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002585 Location first = locations->InAt(0);
2586 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002587 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002588
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002589 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002590 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002591 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002592 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2593 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002594 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2595 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002596 } else {
2597 __ leal(out.AsRegister<CpuRegister>(), Address(
2598 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2599 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002600 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002601 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2602 __ addl(out.AsRegister<CpuRegister>(),
2603 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2604 } else {
2605 __ leal(out.AsRegister<CpuRegister>(), Address(
2606 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2607 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002608 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002609 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002610 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002611 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002612 break;
2613 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002614
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002615 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002616 if (second.IsRegister()) {
2617 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2618 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002619 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2620 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002621 } else {
2622 __ leaq(out.AsRegister<CpuRegister>(), Address(
2623 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2624 }
2625 } else {
2626 DCHECK(second.IsConstant());
2627 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2628 int32_t int32_value = Low32Bits(value);
2629 DCHECK_EQ(int32_value, value);
2630 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2631 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2632 } else {
2633 __ leaq(out.AsRegister<CpuRegister>(), Address(
2634 first.AsRegister<CpuRegister>(), int32_value));
2635 }
2636 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002637 break;
2638 }
2639
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002640 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002641 if (second.IsFpuRegister()) {
2642 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2643 } else if (second.IsConstant()) {
2644 __ addss(first.AsFpuRegister<XmmRegister>(),
2645 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2646 } else {
2647 DCHECK(second.IsStackSlot());
2648 __ addss(first.AsFpuRegister<XmmRegister>(),
2649 Address(CpuRegister(RSP), second.GetStackIndex()));
2650 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002651 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002652 }
2653
2654 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002655 if (second.IsFpuRegister()) {
2656 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2657 } else if (second.IsConstant()) {
2658 __ addsd(first.AsFpuRegister<XmmRegister>(),
2659 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2660 } else {
2661 DCHECK(second.IsDoubleStackSlot());
2662 __ addsd(first.AsFpuRegister<XmmRegister>(),
2663 Address(CpuRegister(RSP), second.GetStackIndex()));
2664 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002665 break;
2666 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002667
2668 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002669 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002670 }
2671}
2672
2673void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002674 LocationSummary* locations =
2675 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002676 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002677 case Primitive::kPrimInt: {
2678 locations->SetInAt(0, Location::RequiresRegister());
2679 locations->SetInAt(1, Location::Any());
2680 locations->SetOut(Location::SameAsFirstInput());
2681 break;
2682 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002683 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002684 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002685 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002686 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002687 break;
2688 }
Calin Juravle11351682014-10-23 15:38:15 +01002689 case Primitive::kPrimFloat:
2690 case Primitive::kPrimDouble: {
2691 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002692 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002693 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002694 break;
Calin Juravle11351682014-10-23 15:38:15 +01002695 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002696 default:
Calin Juravle11351682014-10-23 15:38:15 +01002697 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002698 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002699}
2700
2701void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2702 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002703 Location first = locations->InAt(0);
2704 Location second = locations->InAt(1);
2705 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002706 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002707 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002708 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002710 } else if (second.IsConstant()) {
2711 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002712 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002713 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002714 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002715 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002716 break;
2717 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002718 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002719 if (second.IsConstant()) {
2720 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2721 DCHECK(IsInt<32>(value));
2722 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2723 } else {
2724 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2725 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002726 break;
2727 }
2728
Calin Juravle11351682014-10-23 15:38:15 +01002729 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002730 if (second.IsFpuRegister()) {
2731 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2732 } else if (second.IsConstant()) {
2733 __ subss(first.AsFpuRegister<XmmRegister>(),
2734 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2735 } else {
2736 DCHECK(second.IsStackSlot());
2737 __ subss(first.AsFpuRegister<XmmRegister>(),
2738 Address(CpuRegister(RSP), second.GetStackIndex()));
2739 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002740 break;
Calin Juravle11351682014-10-23 15:38:15 +01002741 }
2742
2743 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002744 if (second.IsFpuRegister()) {
2745 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2746 } else if (second.IsConstant()) {
2747 __ subsd(first.AsFpuRegister<XmmRegister>(),
2748 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2749 } else {
2750 DCHECK(second.IsDoubleStackSlot());
2751 __ subsd(first.AsFpuRegister<XmmRegister>(),
2752 Address(CpuRegister(RSP), second.GetStackIndex()));
2753 }
Calin Juravle11351682014-10-23 15:38:15 +01002754 break;
2755 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002756
2757 default:
Calin Juravle11351682014-10-23 15:38:15 +01002758 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002759 }
2760}
2761
Calin Juravle34bacdf2014-10-07 20:23:36 +01002762void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2763 LocationSummary* locations =
2764 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2765 switch (mul->GetResultType()) {
2766 case Primitive::kPrimInt: {
2767 locations->SetInAt(0, Location::RequiresRegister());
2768 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002769 if (mul->InputAt(1)->IsIntConstant()) {
2770 // Can use 3 operand multiply.
2771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2772 } else {
2773 locations->SetOut(Location::SameAsFirstInput());
2774 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002775 break;
2776 }
2777 case Primitive::kPrimLong: {
2778 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002779 locations->SetInAt(1, Location::Any());
2780 if (mul->InputAt(1)->IsLongConstant() &&
2781 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002782 // Can use 3 operand multiply.
2783 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2784 } else {
2785 locations->SetOut(Location::SameAsFirstInput());
2786 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002787 break;
2788 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002789 case Primitive::kPrimFloat:
2790 case Primitive::kPrimDouble: {
2791 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002792 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002793 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002794 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002795 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002796
2797 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002798 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002799 }
2800}
2801
2802void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2803 LocationSummary* locations = mul->GetLocations();
2804 Location first = locations->InAt(0);
2805 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002806 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002807 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002808 case Primitive::kPrimInt:
2809 // The constant may have ended up in a register, so test explicitly to avoid
2810 // problems where the output may not be the same as the first operand.
2811 if (mul->InputAt(1)->IsIntConstant()) {
2812 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2813 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2814 } else if (second.IsRegister()) {
2815 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002816 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002817 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002818 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002819 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002820 __ imull(first.AsRegister<CpuRegister>(),
2821 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002822 }
2823 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002824 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002825 // The constant may have ended up in a register, so test explicitly to avoid
2826 // problems where the output may not be the same as the first operand.
2827 if (mul->InputAt(1)->IsLongConstant()) {
2828 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2829 if (IsInt<32>(value)) {
2830 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2831 Immediate(static_cast<int32_t>(value)));
2832 } else {
2833 // Have to use the constant area.
2834 DCHECK(first.Equals(out));
2835 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2836 }
2837 } else if (second.IsRegister()) {
2838 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002839 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002840 } else {
2841 DCHECK(second.IsDoubleStackSlot());
2842 DCHECK(first.Equals(out));
2843 __ imulq(first.AsRegister<CpuRegister>(),
2844 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002845 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002846 break;
2847 }
2848
Calin Juravleb5bfa962014-10-21 18:02:24 +01002849 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002850 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002851 if (second.IsFpuRegister()) {
2852 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2853 } else if (second.IsConstant()) {
2854 __ mulss(first.AsFpuRegister<XmmRegister>(),
2855 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2856 } else {
2857 DCHECK(second.IsStackSlot());
2858 __ mulss(first.AsFpuRegister<XmmRegister>(),
2859 Address(CpuRegister(RSP), second.GetStackIndex()));
2860 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002861 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002862 }
2863
2864 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002865 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002866 if (second.IsFpuRegister()) {
2867 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2868 } else if (second.IsConstant()) {
2869 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2870 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2871 } else {
2872 DCHECK(second.IsDoubleStackSlot());
2873 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2874 Address(CpuRegister(RSP), second.GetStackIndex()));
2875 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002876 break;
2877 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002878
2879 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002880 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002881 }
2882}
2883
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002884void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2885 uint32_t stack_adjustment, bool is_float) {
2886 if (source.IsStackSlot()) {
2887 DCHECK(is_float);
2888 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2889 } else if (source.IsDoubleStackSlot()) {
2890 DCHECK(!is_float);
2891 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2892 } else {
2893 // Write the value to the temporary location on the stack and load to FP stack.
2894 if (is_float) {
2895 Location stack_temp = Location::StackSlot(temp_offset);
2896 codegen_->Move(stack_temp, source);
2897 __ flds(Address(CpuRegister(RSP), temp_offset));
2898 } else {
2899 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2900 codegen_->Move(stack_temp, source);
2901 __ fldl(Address(CpuRegister(RSP), temp_offset));
2902 }
2903 }
2904}
2905
2906void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2907 Primitive::Type type = rem->GetResultType();
2908 bool is_float = type == Primitive::kPrimFloat;
2909 size_t elem_size = Primitive::ComponentSize(type);
2910 LocationSummary* locations = rem->GetLocations();
2911 Location first = locations->InAt(0);
2912 Location second = locations->InAt(1);
2913 Location out = locations->Out();
2914
2915 // Create stack space for 2 elements.
2916 // TODO: enhance register allocator to ask for stack temporaries.
2917 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2918
2919 // Load the values to the FP stack in reverse order, using temporaries if needed.
2920 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2921 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2922
2923 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002924 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002925 __ Bind(&retry);
2926 __ fprem();
2927
2928 // Move FP status to AX.
2929 __ fstsw();
2930
2931 // And see if the argument reduction is complete. This is signaled by the
2932 // C2 FPU flag bit set to 0.
2933 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2934 __ j(kNotEqual, &retry);
2935
2936 // We have settled on the final value. Retrieve it into an XMM register.
2937 // Store FP top of stack to real stack.
2938 if (is_float) {
2939 __ fsts(Address(CpuRegister(RSP), 0));
2940 } else {
2941 __ fstl(Address(CpuRegister(RSP), 0));
2942 }
2943
2944 // Pop the 2 items from the FP stack.
2945 __ fucompp();
2946
2947 // Load the value from the stack into an XMM register.
2948 DCHECK(out.IsFpuRegister()) << out;
2949 if (is_float) {
2950 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2951 } else {
2952 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2953 }
2954
2955 // And remove the temporary stack space we allocated.
2956 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2957}
2958
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002959void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2960 DCHECK(instruction->IsDiv() || instruction->IsRem());
2961
2962 LocationSummary* locations = instruction->GetLocations();
2963 Location second = locations->InAt(1);
2964 DCHECK(second.IsConstant());
2965
2966 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2967 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002968 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002969
2970 DCHECK(imm == 1 || imm == -1);
2971
2972 switch (instruction->GetResultType()) {
2973 case Primitive::kPrimInt: {
2974 if (instruction->IsRem()) {
2975 __ xorl(output_register, output_register);
2976 } else {
2977 __ movl(output_register, input_register);
2978 if (imm == -1) {
2979 __ negl(output_register);
2980 }
2981 }
2982 break;
2983 }
2984
2985 case Primitive::kPrimLong: {
2986 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04002987 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002988 } else {
2989 __ movq(output_register, input_register);
2990 if (imm == -1) {
2991 __ negq(output_register);
2992 }
2993 }
2994 break;
2995 }
2996
2997 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002998 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002999 }
3000}
3001
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003002void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003003 LocationSummary* locations = instruction->GetLocations();
3004 Location second = locations->InAt(1);
3005
3006 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3007 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3008
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003009 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003010
3011 DCHECK(IsPowerOfTwo(std::abs(imm)));
3012
3013 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3014
3015 if (instruction->GetResultType() == Primitive::kPrimInt) {
3016 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
3017 __ testl(numerator, numerator);
3018 __ cmov(kGreaterEqual, tmp, numerator);
3019 int shift = CTZ(imm);
3020 __ sarl(tmp, Immediate(shift));
3021
3022 if (imm < 0) {
3023 __ negl(tmp);
3024 }
3025
3026 __ movl(output_register, tmp);
3027 } else {
3028 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3029 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3030
Mark Mendell92e83bf2015-05-07 11:25:03 -04003031 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003032 __ addq(rdx, numerator);
3033 __ testq(numerator, numerator);
3034 __ cmov(kGreaterEqual, rdx, numerator);
3035 int shift = CTZ(imm);
3036 __ sarq(rdx, Immediate(shift));
3037
3038 if (imm < 0) {
3039 __ negq(rdx);
3040 }
3041
3042 __ movq(output_register, rdx);
3043 }
3044}
3045
3046void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3047 DCHECK(instruction->IsDiv() || instruction->IsRem());
3048
3049 LocationSummary* locations = instruction->GetLocations();
3050 Location second = locations->InAt(1);
3051
3052 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3053 : locations->GetTemp(0).AsRegister<CpuRegister>();
3054 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3055 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3056 : locations->Out().AsRegister<CpuRegister>();
3057 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3058
3059 DCHECK_EQ(RAX, eax.AsRegister());
3060 DCHECK_EQ(RDX, edx.AsRegister());
3061 if (instruction->IsDiv()) {
3062 DCHECK_EQ(RAX, out.AsRegister());
3063 } else {
3064 DCHECK_EQ(RDX, out.AsRegister());
3065 }
3066
3067 int64_t magic;
3068 int shift;
3069
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003070 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003071 if (instruction->GetResultType() == Primitive::kPrimInt) {
3072 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3073
3074 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3075
3076 __ movl(numerator, eax);
3077
Mark Mendell0c9497d2015-08-21 09:30:05 -04003078 NearLabel no_div;
3079 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003080 __ testl(eax, eax);
3081 __ j(kNotEqual, &no_div);
3082
3083 __ xorl(out, out);
3084 __ jmp(&end);
3085
3086 __ Bind(&no_div);
3087
3088 __ movl(eax, Immediate(magic));
3089 __ imull(numerator);
3090
3091 if (imm > 0 && magic < 0) {
3092 __ addl(edx, numerator);
3093 } else if (imm < 0 && magic > 0) {
3094 __ subl(edx, numerator);
3095 }
3096
3097 if (shift != 0) {
3098 __ sarl(edx, Immediate(shift));
3099 }
3100
3101 __ movl(eax, edx);
3102 __ shrl(edx, Immediate(31));
3103 __ addl(edx, eax);
3104
3105 if (instruction->IsRem()) {
3106 __ movl(eax, numerator);
3107 __ imull(edx, Immediate(imm));
3108 __ subl(eax, edx);
3109 __ movl(edx, eax);
3110 } else {
3111 __ movl(eax, edx);
3112 }
3113 __ Bind(&end);
3114 } else {
3115 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3116
3117 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3118
3119 CpuRegister rax = eax;
3120 CpuRegister rdx = edx;
3121
3122 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3123
3124 // Save the numerator.
3125 __ movq(numerator, rax);
3126
3127 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003128 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003129
3130 // RDX:RAX = magic * numerator
3131 __ imulq(numerator);
3132
3133 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003134 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003135 __ addq(rdx, numerator);
3136 } else if (imm < 0 && magic > 0) {
3137 // RDX -= numerator
3138 __ subq(rdx, numerator);
3139 }
3140
3141 // Shift if needed.
3142 if (shift != 0) {
3143 __ sarq(rdx, Immediate(shift));
3144 }
3145
3146 // RDX += 1 if RDX < 0
3147 __ movq(rax, rdx);
3148 __ shrq(rdx, Immediate(63));
3149 __ addq(rdx, rax);
3150
3151 if (instruction->IsRem()) {
3152 __ movq(rax, numerator);
3153
3154 if (IsInt<32>(imm)) {
3155 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3156 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003157 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003158 }
3159
3160 __ subq(rax, rdx);
3161 __ movq(rdx, rax);
3162 } else {
3163 __ movq(rax, rdx);
3164 }
3165 }
3166}
3167
Calin Juravlebacfec32014-11-14 15:54:36 +00003168void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3169 DCHECK(instruction->IsDiv() || instruction->IsRem());
3170 Primitive::Type type = instruction->GetResultType();
3171 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3172
3173 bool is_div = instruction->IsDiv();
3174 LocationSummary* locations = instruction->GetLocations();
3175
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003176 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3177 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003178
Roland Levillain271ab9c2014-11-27 15:23:57 +00003179 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003180 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003181
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003182 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003183 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003184
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003185 if (imm == 0) {
3186 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3187 } else if (imm == 1 || imm == -1) {
3188 DivRemOneOrMinusOne(instruction);
3189 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003190 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003191 } else {
3192 DCHECK(imm <= -2 || imm >= 2);
3193 GenerateDivRemWithAnyConstant(instruction);
3194 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003195 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003196 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003197 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3198 out.AsRegister(), type, is_div);
3199 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003200
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003201 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3202 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3203 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3204 // so it's safe to just use negl instead of more complex comparisons.
3205 if (type == Primitive::kPrimInt) {
3206 __ cmpl(second_reg, Immediate(-1));
3207 __ j(kEqual, slow_path->GetEntryLabel());
3208 // edx:eax <- sign-extended of eax
3209 __ cdq();
3210 // eax = quotient, edx = remainder
3211 __ idivl(second_reg);
3212 } else {
3213 __ cmpq(second_reg, Immediate(-1));
3214 __ j(kEqual, slow_path->GetEntryLabel());
3215 // rdx:rax <- sign-extended of rax
3216 __ cqo();
3217 // rax = quotient, rdx = remainder
3218 __ idivq(second_reg);
3219 }
3220 __ Bind(slow_path->GetExitLabel());
3221 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003222}
3223
Calin Juravle7c4954d2014-10-28 16:57:40 +00003224void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3225 LocationSummary* locations =
3226 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3227 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003228 case Primitive::kPrimInt:
3229 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003230 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003231 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003232 locations->SetOut(Location::SameAsFirstInput());
3233 // Intel uses edx:eax as the dividend.
3234 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003235 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3236 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3237 // output and request another temp.
3238 if (div->InputAt(1)->IsConstant()) {
3239 locations->AddTemp(Location::RequiresRegister());
3240 }
Calin Juravled0d48522014-11-04 16:40:20 +00003241 break;
3242 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003243
Calin Juravle7c4954d2014-10-28 16:57:40 +00003244 case Primitive::kPrimFloat:
3245 case Primitive::kPrimDouble: {
3246 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003247 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003248 locations->SetOut(Location::SameAsFirstInput());
3249 break;
3250 }
3251
3252 default:
3253 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3254 }
3255}
3256
3257void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3258 LocationSummary* locations = div->GetLocations();
3259 Location first = locations->InAt(0);
3260 Location second = locations->InAt(1);
3261 DCHECK(first.Equals(locations->Out()));
3262
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003263 Primitive::Type type = div->GetResultType();
3264 switch (type) {
3265 case Primitive::kPrimInt:
3266 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003267 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003268 break;
3269 }
3270
Calin Juravle7c4954d2014-10-28 16:57:40 +00003271 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003272 if (second.IsFpuRegister()) {
3273 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3274 } else if (second.IsConstant()) {
3275 __ divss(first.AsFpuRegister<XmmRegister>(),
3276 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3277 } else {
3278 DCHECK(second.IsStackSlot());
3279 __ divss(first.AsFpuRegister<XmmRegister>(),
3280 Address(CpuRegister(RSP), second.GetStackIndex()));
3281 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003282 break;
3283 }
3284
3285 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003286 if (second.IsFpuRegister()) {
3287 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3288 } else if (second.IsConstant()) {
3289 __ divsd(first.AsFpuRegister<XmmRegister>(),
3290 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3291 } else {
3292 DCHECK(second.IsDoubleStackSlot());
3293 __ divsd(first.AsFpuRegister<XmmRegister>(),
3294 Address(CpuRegister(RSP), second.GetStackIndex()));
3295 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003296 break;
3297 }
3298
3299 default:
3300 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3301 }
3302}
3303
Calin Juravlebacfec32014-11-14 15:54:36 +00003304void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003305 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003306 LocationSummary* locations =
3307 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003308
3309 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003310 case Primitive::kPrimInt:
3311 case Primitive::kPrimLong: {
3312 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003313 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003314 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3315 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003316 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3317 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3318 // output and request another temp.
3319 if (rem->InputAt(1)->IsConstant()) {
3320 locations->AddTemp(Location::RequiresRegister());
3321 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003322 break;
3323 }
3324
3325 case Primitive::kPrimFloat:
3326 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003327 locations->SetInAt(0, Location::Any());
3328 locations->SetInAt(1, Location::Any());
3329 locations->SetOut(Location::RequiresFpuRegister());
3330 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003331 break;
3332 }
3333
3334 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003335 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003336 }
3337}
3338
3339void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3340 Primitive::Type type = rem->GetResultType();
3341 switch (type) {
3342 case Primitive::kPrimInt:
3343 case Primitive::kPrimLong: {
3344 GenerateDivRemIntegral(rem);
3345 break;
3346 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003347 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003348 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003349 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003350 break;
3351 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003352 default:
3353 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3354 }
3355}
3356
Calin Juravled0d48522014-11-04 16:40:20 +00003357void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003358 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3359 ? LocationSummary::kCallOnSlowPath
3360 : LocationSummary::kNoCall;
3361 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003362 locations->SetInAt(0, Location::Any());
3363 if (instruction->HasUses()) {
3364 locations->SetOut(Location::SameAsFirstInput());
3365 }
3366}
3367
3368void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003369 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003370 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3371 codegen_->AddSlowPath(slow_path);
3372
3373 LocationSummary* locations = instruction->GetLocations();
3374 Location value = locations->InAt(0);
3375
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003376 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003377 case Primitive::kPrimByte:
3378 case Primitive::kPrimChar:
3379 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003380 case Primitive::kPrimInt: {
3381 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003382 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003383 __ j(kEqual, slow_path->GetEntryLabel());
3384 } else if (value.IsStackSlot()) {
3385 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3386 __ j(kEqual, slow_path->GetEntryLabel());
3387 } else {
3388 DCHECK(value.IsConstant()) << value;
3389 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3390 __ jmp(slow_path->GetEntryLabel());
3391 }
3392 }
3393 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003394 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003395 case Primitive::kPrimLong: {
3396 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003397 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003398 __ j(kEqual, slow_path->GetEntryLabel());
3399 } else if (value.IsDoubleStackSlot()) {
3400 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3401 __ j(kEqual, slow_path->GetEntryLabel());
3402 } else {
3403 DCHECK(value.IsConstant()) << value;
3404 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3405 __ jmp(slow_path->GetEntryLabel());
3406 }
3407 }
3408 break;
3409 }
3410 default:
3411 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003412 }
Calin Juravled0d48522014-11-04 16:40:20 +00003413}
3414
Calin Juravle9aec02f2014-11-18 23:06:35 +00003415void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3416 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3417
3418 LocationSummary* locations =
3419 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3420
3421 switch (op->GetResultType()) {
3422 case Primitive::kPrimInt:
3423 case Primitive::kPrimLong: {
3424 locations->SetInAt(0, Location::RequiresRegister());
3425 // The shift count needs to be in CL.
3426 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3427 locations->SetOut(Location::SameAsFirstInput());
3428 break;
3429 }
3430 default:
3431 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3432 }
3433}
3434
3435void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3436 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3437
3438 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003439 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003440 Location second = locations->InAt(1);
3441
3442 switch (op->GetResultType()) {
3443 case Primitive::kPrimInt: {
3444 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003445 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003446 if (op->IsShl()) {
3447 __ shll(first_reg, second_reg);
3448 } else if (op->IsShr()) {
3449 __ sarl(first_reg, second_reg);
3450 } else {
3451 __ shrl(first_reg, second_reg);
3452 }
3453 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003454 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003455 if (op->IsShl()) {
3456 __ shll(first_reg, imm);
3457 } else if (op->IsShr()) {
3458 __ sarl(first_reg, imm);
3459 } else {
3460 __ shrl(first_reg, imm);
3461 }
3462 }
3463 break;
3464 }
3465 case Primitive::kPrimLong: {
3466 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003467 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003468 if (op->IsShl()) {
3469 __ shlq(first_reg, second_reg);
3470 } else if (op->IsShr()) {
3471 __ sarq(first_reg, second_reg);
3472 } else {
3473 __ shrq(first_reg, second_reg);
3474 }
3475 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003476 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003477 if (op->IsShl()) {
3478 __ shlq(first_reg, imm);
3479 } else if (op->IsShr()) {
3480 __ sarq(first_reg, imm);
3481 } else {
3482 __ shrq(first_reg, imm);
3483 }
3484 }
3485 break;
3486 }
3487 default:
3488 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3489 }
3490}
3491
3492void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3493 HandleShift(shl);
3494}
3495
3496void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3497 HandleShift(shl);
3498}
3499
3500void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3501 HandleShift(shr);
3502}
3503
3504void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3505 HandleShift(shr);
3506}
3507
3508void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3509 HandleShift(ushr);
3510}
3511
3512void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3513 HandleShift(ushr);
3514}
3515
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003516void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003517 LocationSummary* locations =
3518 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003519 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003520 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003521 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003522 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003523}
3524
3525void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3526 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003527 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3528 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003529 // Note: if heap poisoning is enabled, the entry point takes cares
3530 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003531
Calin Juravle175dc732015-08-25 15:42:32 +01003532 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3533 instruction,
3534 instruction->GetDexPc(),
3535 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003536
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003537 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003538}
3539
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003540void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3541 LocationSummary* locations =
3542 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3543 InvokeRuntimeCallingConvention calling_convention;
3544 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003545 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003546 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003547 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003548}
3549
3550void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3551 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003552 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3553 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003554
Roland Levillain4d027112015-07-01 15:41:14 +01003555 // Note: if heap poisoning is enabled, the entry point takes cares
3556 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003557 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3558 instruction,
3559 instruction->GetDexPc(),
3560 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003561
3562 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003563}
3564
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003565void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003566 LocationSummary* locations =
3567 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003568 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3569 if (location.IsStackSlot()) {
3570 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3571 } else if (location.IsDoubleStackSlot()) {
3572 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3573 }
3574 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003575}
3576
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003577void InstructionCodeGeneratorX86_64::VisitParameterValue(
3578 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003579 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003580}
3581
3582void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3583 LocationSummary* locations =
3584 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3585 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3586}
3587
3588void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3589 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3590 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003591}
3592
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003593void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003594 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003595 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003596 locations->SetInAt(0, Location::RequiresRegister());
3597 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003598}
3599
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003600void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3601 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003602 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3603 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003604 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003605 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003606 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003608 break;
3609
3610 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003611 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003612 break;
3613
3614 default:
3615 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3616 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003617}
3618
David Brazdil66d126e2015-04-03 16:02:44 +01003619void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3620 LocationSummary* locations =
3621 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3622 locations->SetInAt(0, Location::RequiresRegister());
3623 locations->SetOut(Location::SameAsFirstInput());
3624}
3625
3626void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003627 LocationSummary* locations = bool_not->GetLocations();
3628 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3629 locations->Out().AsRegister<CpuRegister>().AsRegister());
3630 Location out = locations->Out();
3631 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3632}
3633
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003634void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003635 LocationSummary* locations =
3636 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003637 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3638 locations->SetInAt(i, Location::Any());
3639 }
3640 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003641}
3642
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003643void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003644 LOG(FATAL) << "Unimplemented";
3645}
3646
Calin Juravle52c48962014-12-16 17:02:57 +00003647void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3648 /*
3649 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3650 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3651 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3652 */
3653 switch (kind) {
3654 case MemBarrierKind::kAnyAny: {
3655 __ mfence();
3656 break;
3657 }
3658 case MemBarrierKind::kAnyStore:
3659 case MemBarrierKind::kLoadAny:
3660 case MemBarrierKind::kStoreStore: {
3661 // nop
3662 break;
3663 }
3664 default:
3665 LOG(FATAL) << "Unexpected memory barier " << kind;
3666 }
3667}
3668
3669void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3670 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3671
Nicolas Geoffray39468442014-09-02 15:17:15 +01003672 LocationSummary* locations =
3673 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003674 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003675 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3676 locations->SetOut(Location::RequiresFpuRegister());
3677 } else {
3678 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3679 }
Calin Juravle52c48962014-12-16 17:02:57 +00003680}
3681
3682void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3683 const FieldInfo& field_info) {
3684 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3685
3686 LocationSummary* locations = instruction->GetLocations();
3687 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3688 Location out = locations->Out();
3689 bool is_volatile = field_info.IsVolatile();
3690 Primitive::Type field_type = field_info.GetFieldType();
3691 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3692
3693 switch (field_type) {
3694 case Primitive::kPrimBoolean: {
3695 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3696 break;
3697 }
3698
3699 case Primitive::kPrimByte: {
3700 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3701 break;
3702 }
3703
3704 case Primitive::kPrimShort: {
3705 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3706 break;
3707 }
3708
3709 case Primitive::kPrimChar: {
3710 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3711 break;
3712 }
3713
3714 case Primitive::kPrimInt:
3715 case Primitive::kPrimNot: {
3716 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3717 break;
3718 }
3719
3720 case Primitive::kPrimLong: {
3721 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3722 break;
3723 }
3724
3725 case Primitive::kPrimFloat: {
3726 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3727 break;
3728 }
3729
3730 case Primitive::kPrimDouble: {
3731 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3732 break;
3733 }
3734
3735 case Primitive::kPrimVoid:
3736 LOG(FATAL) << "Unreachable type " << field_type;
3737 UNREACHABLE();
3738 }
3739
Calin Juravle77520bc2015-01-12 18:45:46 +00003740 codegen_->MaybeRecordImplicitNullCheck(instruction);
3741
Calin Juravle52c48962014-12-16 17:02:57 +00003742 if (is_volatile) {
3743 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3744 }
Roland Levillain4d027112015-07-01 15:41:14 +01003745
3746 if (field_type == Primitive::kPrimNot) {
3747 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3748 }
Calin Juravle52c48962014-12-16 17:02:57 +00003749}
3750
3751void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3752 const FieldInfo& field_info) {
3753 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3754
3755 LocationSummary* locations =
3756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003757 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04003758 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003759 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003760 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003761
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003762 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003763 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04003764 if (is_volatile) {
3765 // In order to satisfy the semantics of volatile, this must be a single instruction store.
3766 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
3767 } else {
3768 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
3769 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003770 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04003771 if (is_volatile) {
3772 // In order to satisfy the semantics of volatile, this must be a single instruction store.
3773 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
3774 } else {
3775 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3776 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003777 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003778 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003779 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003780 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003781 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003782 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3783 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003784 locations->AddTemp(Location::RequiresRegister());
3785 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003786}
3787
Calin Juravle52c48962014-12-16 17:02:57 +00003788void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003789 const FieldInfo& field_info,
3790 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003791 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3792
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003793 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003794 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3795 Location value = locations->InAt(1);
3796 bool is_volatile = field_info.IsVolatile();
3797 Primitive::Type field_type = field_info.GetFieldType();
3798 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3799
3800 if (is_volatile) {
3801 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3802 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003803
Mark Mendellea5af682015-10-22 17:35:49 -04003804 bool maybe_record_implicit_null_check_done = false;
3805
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003806 switch (field_type) {
3807 case Primitive::kPrimBoolean:
3808 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003809 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04003810 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04003811 __ movb(Address(base, offset), Immediate(v));
3812 } else {
3813 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3814 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003815 break;
3816 }
3817
3818 case Primitive::kPrimShort:
3819 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003820 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04003821 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04003822 __ movw(Address(base, offset), Immediate(v));
3823 } else {
3824 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3825 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003826 break;
3827 }
3828
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003829 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003830 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003831 if (value.IsConstant()) {
3832 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003833 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3834 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3835 // Note: if heap poisoning is enabled, no need to poison
3836 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003837 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003838 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003839 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3840 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3841 __ movl(temp, value.AsRegister<CpuRegister>());
3842 __ PoisonHeapReference(temp);
3843 __ movl(Address(base, offset), temp);
3844 } else {
3845 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3846 }
Mark Mendell40741f32015-04-20 22:10:34 -04003847 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003848 break;
3849 }
3850
3851 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003852 if (value.IsConstant()) {
3853 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04003854 codegen_->MoveInt64ToAddress(Address(base, offset),
3855 Address(base, offset + sizeof(int32_t)),
3856 v,
3857 instruction);
3858 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04003859 } else {
3860 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3861 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003862 break;
3863 }
3864
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003865 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04003866 if (value.IsConstant()) {
3867 int32_t v =
3868 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
3869 __ movl(Address(base, offset), Immediate(v));
3870 } else {
3871 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3872 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003873 break;
3874 }
3875
3876 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04003877 if (value.IsConstant()) {
3878 int64_t v =
3879 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
3880 codegen_->MoveInt64ToAddress(Address(base, offset),
3881 Address(base, offset + sizeof(int32_t)),
3882 v,
3883 instruction);
3884 maybe_record_implicit_null_check_done = true;
3885 } else {
3886 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3887 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003888 break;
3889 }
3890
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003891 case Primitive::kPrimVoid:
3892 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003893 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003894 }
Calin Juravle52c48962014-12-16 17:02:57 +00003895
Mark Mendellea5af682015-10-22 17:35:49 -04003896 if (!maybe_record_implicit_null_check_done) {
3897 codegen_->MaybeRecordImplicitNullCheck(instruction);
3898 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003899
3900 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3901 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3902 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003903 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003904 }
3905
Calin Juravle52c48962014-12-16 17:02:57 +00003906 if (is_volatile) {
3907 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3908 }
3909}
3910
3911void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3912 HandleFieldSet(instruction, instruction->GetFieldInfo());
3913}
3914
3915void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003916 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003917}
3918
3919void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003920 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003921}
3922
3923void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003924 HandleFieldGet(instruction, instruction->GetFieldInfo());
3925}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003926
Calin Juravle52c48962014-12-16 17:02:57 +00003927void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3928 HandleFieldGet(instruction);
3929}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003930
Calin Juravle52c48962014-12-16 17:02:57 +00003931void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3932 HandleFieldGet(instruction, instruction->GetFieldInfo());
3933}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003934
Calin Juravle52c48962014-12-16 17:02:57 +00003935void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3936 HandleFieldSet(instruction, instruction->GetFieldInfo());
3937}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003938
Calin Juravle52c48962014-12-16 17:02:57 +00003939void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003940 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003941}
3942
Calin Juravlee460d1d2015-09-29 04:52:17 +01003943void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
3944 HUnresolvedInstanceFieldGet* instruction) {
3945 FieldAccessCallingConventionX86_64 calling_convention;
3946 codegen_->CreateUnresolvedFieldLocationSummary(
3947 instruction, instruction->GetFieldType(), calling_convention);
3948}
3949
3950void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
3951 HUnresolvedInstanceFieldGet* instruction) {
3952 FieldAccessCallingConventionX86_64 calling_convention;
3953 codegen_->GenerateUnresolvedFieldAccess(instruction,
3954 instruction->GetFieldType(),
3955 instruction->GetFieldIndex(),
3956 instruction->GetDexPc(),
3957 calling_convention);
3958}
3959
3960void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
3961 HUnresolvedInstanceFieldSet* instruction) {
3962 FieldAccessCallingConventionX86_64 calling_convention;
3963 codegen_->CreateUnresolvedFieldLocationSummary(
3964 instruction, instruction->GetFieldType(), calling_convention);
3965}
3966
3967void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
3968 HUnresolvedInstanceFieldSet* instruction) {
3969 FieldAccessCallingConventionX86_64 calling_convention;
3970 codegen_->GenerateUnresolvedFieldAccess(instruction,
3971 instruction->GetFieldType(),
3972 instruction->GetFieldIndex(),
3973 instruction->GetDexPc(),
3974 calling_convention);
3975}
3976
3977void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
3978 HUnresolvedStaticFieldGet* instruction) {
3979 FieldAccessCallingConventionX86_64 calling_convention;
3980 codegen_->CreateUnresolvedFieldLocationSummary(
3981 instruction, instruction->GetFieldType(), calling_convention);
3982}
3983
3984void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
3985 HUnresolvedStaticFieldGet* instruction) {
3986 FieldAccessCallingConventionX86_64 calling_convention;
3987 codegen_->GenerateUnresolvedFieldAccess(instruction,
3988 instruction->GetFieldType(),
3989 instruction->GetFieldIndex(),
3990 instruction->GetDexPc(),
3991 calling_convention);
3992}
3993
3994void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
3995 HUnresolvedStaticFieldSet* instruction) {
3996 FieldAccessCallingConventionX86_64 calling_convention;
3997 codegen_->CreateUnresolvedFieldLocationSummary(
3998 instruction, instruction->GetFieldType(), calling_convention);
3999}
4000
4001void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4002 HUnresolvedStaticFieldSet* instruction) {
4003 FieldAccessCallingConventionX86_64 calling_convention;
4004 codegen_->GenerateUnresolvedFieldAccess(instruction,
4005 instruction->GetFieldType(),
4006 instruction->GetFieldIndex(),
4007 instruction->GetDexPc(),
4008 calling_convention);
4009}
4010
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004011void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004012 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4013 ? LocationSummary::kCallOnSlowPath
4014 : LocationSummary::kNoCall;
4015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4016 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004017 ? Location::RequiresRegister()
4018 : Location::Any();
4019 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004020 if (instruction->HasUses()) {
4021 locations->SetOut(Location::SameAsFirstInput());
4022 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004023}
4024
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004025void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004026 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4027 return;
4028 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004029 LocationSummary* locations = instruction->GetLocations();
4030 Location obj = locations->InAt(0);
4031
4032 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4033 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4034}
4035
4036void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004037 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004038 codegen_->AddSlowPath(slow_path);
4039
4040 LocationSummary* locations = instruction->GetLocations();
4041 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004042
4043 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004044 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004045 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004046 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004047 } else {
4048 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004049 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004050 __ jmp(slow_path->GetEntryLabel());
4051 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004052 }
4053 __ j(kEqual, slow_path->GetEntryLabel());
4054}
4055
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004056void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004057 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004058 GenerateImplicitNullCheck(instruction);
4059 } else {
4060 GenerateExplicitNullCheck(instruction);
4061 }
4062}
4063
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004064void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004065 LocationSummary* locations =
4066 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004067 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004068 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004069 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4070 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4071 } else {
4072 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4073 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004074}
4075
4076void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4077 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004078 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004079 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004080 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004081
Roland Levillain4d027112015-07-01 15:41:14 +01004082 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004083 case Primitive::kPrimBoolean: {
4084 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004085 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004086 if (index.IsConstant()) {
4087 __ movzxb(out, Address(obj,
4088 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4089 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004090 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004091 }
4092 break;
4093 }
4094
4095 case Primitive::kPrimByte: {
4096 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004097 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004098 if (index.IsConstant()) {
4099 __ movsxb(out, Address(obj,
4100 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4101 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004102 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004103 }
4104 break;
4105 }
4106
4107 case Primitive::kPrimShort: {
4108 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004110 if (index.IsConstant()) {
4111 __ movsxw(out, Address(obj,
4112 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4113 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004114 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004115 }
4116 break;
4117 }
4118
4119 case Primitive::kPrimChar: {
4120 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_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 __ movzxw(out, Address(obj,
4124 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4125 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004126 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004127 }
4128 break;
4129 }
4130
4131 case Primitive::kPrimInt:
4132 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01004133 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4134 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004135 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004136 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004137 if (index.IsConstant()) {
4138 __ movl(out, Address(obj,
4139 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4140 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004141 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004142 }
4143 break;
4144 }
4145
4146 case Primitive::kPrimLong: {
4147 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004148 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004149 if (index.IsConstant()) {
4150 __ movq(out, Address(obj,
4151 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4152 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004153 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004154 }
4155 break;
4156 }
4157
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004158 case Primitive::kPrimFloat: {
4159 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004160 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004161 if (index.IsConstant()) {
4162 __ movss(out, Address(obj,
4163 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4164 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004165 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004166 }
4167 break;
4168 }
4169
4170 case Primitive::kPrimDouble: {
4171 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004172 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004173 if (index.IsConstant()) {
4174 __ movsd(out, Address(obj,
4175 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4176 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004177 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004178 }
4179 break;
4180 }
4181
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004182 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004183 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004184 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004185 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004186 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004187
4188 if (type == Primitive::kPrimNot) {
4189 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4190 __ MaybeUnpoisonHeapReference(out);
4191 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004192}
4193
4194void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004195 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004196
4197 bool needs_write_barrier =
4198 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004199 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004200
Nicolas Geoffray39468442014-09-02 15:17:15 +01004201 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004202 instruction,
4203 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004204
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004205 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004206 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4207 if (Primitive::IsFloatingPointType(value_type)) {
4208 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004209 } else {
4210 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4211 }
4212
4213 if (needs_write_barrier) {
4214 // Temporary registers for the write barrier.
4215 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4216 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004217 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004218}
4219
4220void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4221 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004222 CpuRegister array = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004223 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004224 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004225 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004226 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004227 bool needs_write_barrier =
4228 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004229 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4230 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4231 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232
4233 switch (value_type) {
4234 case Primitive::kPrimBoolean:
4235 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004236 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4237 Address address = index.IsConstant()
4238 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4239 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4240 if (value.IsRegister()) {
4241 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004242 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004243 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004244 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004245 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004246 break;
4247 }
4248
4249 case Primitive::kPrimShort:
4250 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004251 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4252 Address address = index.IsConstant()
4253 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4254 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4255 if (value.IsRegister()) {
4256 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004257 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004258 DCHECK(value.IsConstant()) << value;
4259 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004260 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004261 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004262 break;
4263 }
4264
4265 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004266 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4267 Address address = index.IsConstant()
4268 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4269 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4270 if (!value.IsRegister()) {
4271 // Just setting null.
4272 DCHECK(instruction->InputAt(2)->IsNullConstant());
4273 DCHECK(value.IsConstant()) << value;
4274 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004275 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004276 DCHECK(!needs_write_barrier);
4277 DCHECK(!may_need_runtime_call);
4278 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004279 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004280
4281 DCHECK(needs_write_barrier);
4282 CpuRegister register_value = value.AsRegister<CpuRegister>();
4283 NearLabel done, not_null, do_put;
4284 SlowPathCode* slow_path = nullptr;
4285 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4286 if (may_need_runtime_call) {
4287 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4288 codegen_->AddSlowPath(slow_path);
4289 if (instruction->GetValueCanBeNull()) {
4290 __ testl(register_value, register_value);
4291 __ j(kNotEqual, &not_null);
4292 __ movl(address, Immediate(0));
4293 codegen_->MaybeRecordImplicitNullCheck(instruction);
4294 __ jmp(&done);
4295 __ Bind(&not_null);
4296 }
4297
4298 __ movl(temp, Address(array, class_offset));
4299 codegen_->MaybeRecordImplicitNullCheck(instruction);
4300 __ MaybeUnpoisonHeapReference(temp);
4301 __ movl(temp, Address(temp, component_offset));
4302 // No need to poison/unpoison, we're comparing two poisoned references.
4303 __ cmpl(temp, Address(register_value, class_offset));
4304 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4305 __ j(kEqual, &do_put);
4306 __ MaybeUnpoisonHeapReference(temp);
4307 __ movl(temp, Address(temp, super_offset));
4308 // No need to unpoison the result, we're comparing against null.
4309 __ testl(temp, temp);
4310 __ j(kNotEqual, slow_path->GetEntryLabel());
4311 __ Bind(&do_put);
4312 } else {
4313 __ j(kNotEqual, slow_path->GetEntryLabel());
4314 }
4315 }
4316
4317 if (kPoisonHeapReferences) {
4318 __ movl(temp, register_value);
4319 __ PoisonHeapReference(temp);
4320 __ movl(address, temp);
4321 } else {
4322 __ movl(address, register_value);
4323 }
4324 if (!may_need_runtime_call) {
4325 codegen_->MaybeRecordImplicitNullCheck(instruction);
4326 }
4327
4328 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4329 codegen_->MarkGCCard(
4330 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4331 __ Bind(&done);
4332
4333 if (slow_path != nullptr) {
4334 __ Bind(slow_path->GetExitLabel());
4335 }
4336
4337 break;
4338 }
4339 case Primitive::kPrimInt: {
4340 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4341 Address address = index.IsConstant()
4342 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4343 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4344 if (value.IsRegister()) {
4345 __ movl(address, value.AsRegister<CpuRegister>());
4346 } else {
4347 DCHECK(value.IsConstant()) << value;
4348 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4349 __ movl(address, Immediate(v));
4350 }
4351 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004352 break;
4353 }
4354
4355 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004356 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4357 Address address = index.IsConstant()
4358 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4359 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4360 if (value.IsRegister()) {
4361 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004362 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004363 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004364 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004365 Address address_high = index.IsConstant()
4366 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4367 offset + sizeof(int32_t))
4368 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4369 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004370 }
4371 break;
4372 }
4373
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004374 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004375 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4376 Address address = index.IsConstant()
4377 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4378 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004379 if (value.IsFpuRegister()) {
4380 __ movss(address, value.AsFpuRegister<XmmRegister>());
4381 } else {
4382 DCHECK(value.IsConstant());
4383 int32_t v =
4384 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4385 __ movl(address, Immediate(v));
4386 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004387 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004388 break;
4389 }
4390
4391 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004392 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4393 Address address = index.IsConstant()
4394 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4395 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004396 if (value.IsFpuRegister()) {
4397 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4398 codegen_->MaybeRecordImplicitNullCheck(instruction);
4399 } else {
4400 int64_t v =
4401 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4402 Address address_high = index.IsConstant()
4403 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4404 offset + sizeof(int32_t))
4405 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4406 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4407 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004408 break;
4409 }
4410
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004411 case Primitive::kPrimVoid:
4412 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004413 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004414 }
4415}
4416
4417void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004418 LocationSummary* locations =
4419 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004420 locations->SetInAt(0, Location::RequiresRegister());
4421 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004422}
4423
4424void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4425 LocationSummary* locations = instruction->GetLocations();
4426 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004427 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4428 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004429 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004430 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004431}
4432
4433void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004434 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4435 ? LocationSummary::kCallOnSlowPath
4436 : LocationSummary::kNoCall;
4437 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004438 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004439 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004440 if (instruction->HasUses()) {
4441 locations->SetOut(Location::SameAsFirstInput());
4442 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004443}
4444
4445void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4446 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004447 Location index_loc = locations->InAt(0);
4448 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004449 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004450 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004451
Mark Mendell99dbd682015-04-22 16:18:52 -04004452 if (length_loc.IsConstant()) {
4453 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4454 if (index_loc.IsConstant()) {
4455 // BCE will remove the bounds check if we are guarenteed to pass.
4456 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4457 if (index < 0 || index >= length) {
4458 codegen_->AddSlowPath(slow_path);
4459 __ jmp(slow_path->GetEntryLabel());
4460 } else {
4461 // Some optimization after BCE may have generated this, and we should not
4462 // generate a bounds check if it is a valid range.
4463 }
4464 return;
4465 }
4466
4467 // We have to reverse the jump condition because the length is the constant.
4468 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4469 __ cmpl(index_reg, Immediate(length));
4470 codegen_->AddSlowPath(slow_path);
4471 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004472 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004473 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4474 if (index_loc.IsConstant()) {
4475 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4476 __ cmpl(length, Immediate(value));
4477 } else {
4478 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4479 }
4480 codegen_->AddSlowPath(slow_path);
4481 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004482 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004483}
4484
4485void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4486 CpuRegister card,
4487 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004488 CpuRegister value,
4489 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004490 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004491 if (value_can_be_null) {
4492 __ testl(value, value);
4493 __ j(kEqual, &is_null);
4494 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004495 __ gs()->movq(card, Address::Absolute(
4496 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4497 __ movq(temp, object);
4498 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004499 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004500 if (value_can_be_null) {
4501 __ Bind(&is_null);
4502 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004503}
4504
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4506 temp->SetLocations(nullptr);
4507}
4508
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004509void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004510 // Nothing to do, this is driven by the code generator.
4511}
4512
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004513void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004514 LOG(FATAL) << "Unimplemented";
4515}
4516
4517void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004518 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4519}
4520
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004521void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4522 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4523}
4524
4525void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004526 HBasicBlock* block = instruction->GetBlock();
4527 if (block->GetLoopInformation() != nullptr) {
4528 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4529 // The back edge will generate the suspend check.
4530 return;
4531 }
4532 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4533 // The goto will generate the suspend check.
4534 return;
4535 }
4536 GenerateSuspendCheck(instruction, nullptr);
4537}
4538
4539void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4540 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004541 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004542 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4543 if (slow_path == nullptr) {
4544 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4545 instruction->SetSlowPath(slow_path);
4546 codegen_->AddSlowPath(slow_path);
4547 if (successor != nullptr) {
4548 DCHECK(successor->IsLoopHeader());
4549 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4550 }
4551 } else {
4552 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4553 }
4554
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004555 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004556 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004557 if (successor == nullptr) {
4558 __ j(kNotEqual, slow_path->GetEntryLabel());
4559 __ Bind(slow_path->GetReturnLabel());
4560 } else {
4561 __ j(kEqual, codegen_->GetLabelOf(successor));
4562 __ jmp(slow_path->GetEntryLabel());
4563 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004564}
4565
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004566X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4567 return codegen_->GetAssembler();
4568}
4569
4570void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004571 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004572 Location source = move->GetSource();
4573 Location destination = move->GetDestination();
4574
4575 if (source.IsRegister()) {
4576 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004577 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004578 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004579 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004580 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004581 } else {
4582 DCHECK(destination.IsDoubleStackSlot());
4583 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004584 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004585 }
4586 } else if (source.IsStackSlot()) {
4587 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004588 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004589 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004590 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004591 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004592 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004593 } else {
4594 DCHECK(destination.IsStackSlot());
4595 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4596 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4597 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004598 } else if (source.IsDoubleStackSlot()) {
4599 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004600 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004601 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004602 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004603 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4604 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004605 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004606 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004607 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4608 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4609 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004610 } else if (source.IsConstant()) {
4611 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004612 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4613 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004614 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004615 if (value == 0) {
4616 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4617 } else {
4618 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4619 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004620 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004621 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004622 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004623 }
4624 } else if (constant->IsLongConstant()) {
4625 int64_t value = constant->AsLongConstant()->GetValue();
4626 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004627 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004628 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004629 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004630 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004631 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004632 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004633 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004634 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004635 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004636 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4637 if (value == 0) {
4638 // easy FP 0.0.
4639 __ xorps(dest, dest);
4640 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004641 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004642 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004643 } else {
4644 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004645 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004646 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4647 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004648 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004649 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004650 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004651 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004652 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004653 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4654 if (value == 0) {
4655 __ xorpd(dest, dest);
4656 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004657 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004658 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004659 } else {
4660 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004661 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004662 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004663 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004664 } else if (source.IsFpuRegister()) {
4665 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004666 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004667 } else if (destination.IsStackSlot()) {
4668 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004669 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004670 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004671 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004672 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004673 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004674 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004675 }
4676}
4677
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004678void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004679 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004680 __ movl(Address(CpuRegister(RSP), mem), reg);
4681 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004682}
4683
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004684void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004685 ScratchRegisterScope ensure_scratch(
4686 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4687
4688 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4689 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4690 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4691 Address(CpuRegister(RSP), mem2 + stack_offset));
4692 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4693 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4694 CpuRegister(ensure_scratch.GetRegister()));
4695}
4696
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004697void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4698 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4699 __ movq(Address(CpuRegister(RSP), mem), reg);
4700 __ movq(reg, CpuRegister(TMP));
4701}
4702
4703void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4704 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004705 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004706
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004707 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4708 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4709 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4710 Address(CpuRegister(RSP), mem2 + stack_offset));
4711 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4712 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4713 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004714}
4715
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004716void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4717 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4718 __ movss(Address(CpuRegister(RSP), mem), reg);
4719 __ movd(reg, CpuRegister(TMP));
4720}
4721
4722void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4723 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4724 __ movsd(Address(CpuRegister(RSP), mem), reg);
4725 __ movd(reg, CpuRegister(TMP));
4726}
4727
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004728void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004729 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004730 Location source = move->GetSource();
4731 Location destination = move->GetDestination();
4732
4733 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004734 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004735 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004736 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004737 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004738 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004739 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004740 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4741 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004742 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004743 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004744 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004745 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4746 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004747 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004748 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4749 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4750 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004751 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004752 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004753 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004754 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004755 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004756 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004757 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004758 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004759 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004760 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004761 }
4762}
4763
4764
4765void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4766 __ pushq(CpuRegister(reg));
4767}
4768
4769
4770void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4771 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004772}
4773
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004774void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004775 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004776 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4777 Immediate(mirror::Class::kStatusInitialized));
4778 __ j(kLess, slow_path->GetEntryLabel());
4779 __ Bind(slow_path->GetExitLabel());
4780 // No need for memory fence, thanks to the X86_64 memory model.
4781}
4782
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004783void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004784 InvokeRuntimeCallingConvention calling_convention;
4785 CodeGenerator::CreateLoadClassLocationSummary(
4786 cls,
4787 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4788 Location::RegisterLocation(RAX));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004789}
4790
4791void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004792 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004793 if (cls->NeedsAccessCheck()) {
4794 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4795 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4796 cls,
4797 cls->GetDexPc(),
4798 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004799 return;
4800 }
4801
4802 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4803 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
4804 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004805 DCHECK(!cls->CanCallRuntime());
4806 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004807 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004808 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004809 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004810 __ movq(out, Address(
4811 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004812 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004813 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004814
Andreas Gampe85b62f22015-09-09 13:15:38 -07004815 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004816 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4817 codegen_->AddSlowPath(slow_path);
4818 __ testl(out, out);
4819 __ j(kEqual, slow_path->GetEntryLabel());
4820 if (cls->MustGenerateClinitCheck()) {
4821 GenerateClassInitializationCheck(slow_path, out);
4822 } else {
4823 __ Bind(slow_path->GetExitLabel());
4824 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004825 }
4826}
4827
4828void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4829 LocationSummary* locations =
4830 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4831 locations->SetInAt(0, Location::RequiresRegister());
4832 if (check->HasUses()) {
4833 locations->SetOut(Location::SameAsFirstInput());
4834 }
4835}
4836
4837void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004838 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004839 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004840 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004841 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004842 GenerateClassInitializationCheck(slow_path,
4843 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004844}
4845
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004846void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4847 LocationSummary* locations =
4848 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004849 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004850 locations->SetOut(Location::RequiresRegister());
4851}
4852
4853void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004854 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004855 codegen_->AddSlowPath(slow_path);
4856
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004857 LocationSummary* locations = load->GetLocations();
4858 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4859 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004860 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004861 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004862 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004863 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004864 __ testl(out, out);
4865 __ j(kEqual, slow_path->GetEntryLabel());
4866 __ Bind(slow_path->GetExitLabel());
4867}
4868
David Brazdilcb1c0552015-08-04 16:22:25 +01004869static Address GetExceptionTlsAddress() {
4870 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4871}
4872
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004873void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4874 LocationSummary* locations =
4875 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4876 locations->SetOut(Location::RequiresRegister());
4877}
4878
4879void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004880 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4881}
4882
4883void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4884 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4885}
4886
4887void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4888 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004889}
4890
4891void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4892 LocationSummary* locations =
4893 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4894 InvokeRuntimeCallingConvention calling_convention;
4895 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4896}
4897
4898void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004899 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4900 instruction,
4901 instruction->GetDexPc(),
4902 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004903}
4904
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004905void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004906 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4907 switch (instruction->GetTypeCheckKind()) {
4908 case TypeCheckKind::kExactCheck:
4909 case TypeCheckKind::kAbstractClassCheck:
4910 case TypeCheckKind::kClassHierarchyCheck:
4911 case TypeCheckKind::kArrayObjectCheck:
4912 call_kind = LocationSummary::kNoCall;
4913 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004914 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004915 case TypeCheckKind::kInterfaceCheck:
4916 call_kind = LocationSummary::kCall;
4917 break;
4918 case TypeCheckKind::kArrayCheck:
4919 call_kind = LocationSummary::kCallOnSlowPath;
4920 break;
4921 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004922 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004923 if (call_kind != LocationSummary::kCall) {
4924 locations->SetInAt(0, Location::RequiresRegister());
4925 locations->SetInAt(1, Location::Any());
4926 // Note that TypeCheckSlowPathX86_64 uses this register too.
4927 locations->SetOut(Location::RequiresRegister());
4928 } else {
4929 InvokeRuntimeCallingConvention calling_convention;
4930 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4931 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4932 locations->SetOut(Location::RegisterLocation(RAX));
4933 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004934}
4935
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004936void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004937 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004938 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004939 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004940 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004941 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004942 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4943 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4944 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004945 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004946 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004947
4948 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004949 // Avoid null check if we know obj is not null.
4950 if (instruction->MustDoNullCheck()) {
4951 __ testl(obj, obj);
4952 __ j(kEqual, &zero);
4953 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004954
Calin Juravle98893e12015-10-02 21:05:03 +01004955 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004956 // This is safe, as the register is caller-save, and the object must be in another
4957 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004958 CpuRegister target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4959 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004960 ? obj
4961 : out;
4962 __ movl(target, Address(obj, class_offset));
4963 __ MaybeUnpoisonHeapReference(target);
4964
4965 switch (instruction->GetTypeCheckKind()) {
4966 case TypeCheckKind::kExactCheck: {
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 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004973 if (zero.IsLinked()) {
4974 // Classes must be equal for the instanceof to succeed.
4975 __ j(kNotEqual, &zero);
4976 __ movl(out, Immediate(1));
4977 __ jmp(&done);
4978 } else {
4979 __ setcc(kEqual, out);
4980 // setcc only sets the low byte.
4981 __ andl(out, Immediate(1));
4982 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004983 break;
4984 }
4985 case TypeCheckKind::kAbstractClassCheck: {
4986 // If the class is abstract, we eagerly fetch the super class of the
4987 // object to avoid doing a comparison we know will fail.
4988 NearLabel loop, success;
4989 __ Bind(&loop);
4990 __ movl(out, Address(out, super_offset));
4991 __ MaybeUnpoisonHeapReference(out);
4992 __ testl(out, out);
4993 // If `out` is null, we use it for the result, and jump to `done`.
4994 __ j(kEqual, &done);
4995 if (cls.IsRegister()) {
4996 __ cmpl(out, cls.AsRegister<CpuRegister>());
4997 } else {
4998 DCHECK(cls.IsStackSlot()) << cls;
4999 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5000 }
5001 __ j(kNotEqual, &loop);
5002 __ movl(out, Immediate(1));
5003 if (zero.IsLinked()) {
5004 __ jmp(&done);
5005 }
5006 break;
5007 }
5008 case TypeCheckKind::kClassHierarchyCheck: {
5009 // Walk over the class hierarchy to find a match.
5010 NearLabel loop, success;
5011 __ Bind(&loop);
5012 if (cls.IsRegister()) {
5013 __ cmpl(out, cls.AsRegister<CpuRegister>());
5014 } else {
5015 DCHECK(cls.IsStackSlot()) << cls;
5016 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5017 }
5018 __ j(kEqual, &success);
5019 __ movl(out, Address(out, super_offset));
5020 __ MaybeUnpoisonHeapReference(out);
5021 __ testl(out, out);
5022 __ j(kNotEqual, &loop);
5023 // If `out` is null, we use it for the result, and jump to `done`.
5024 __ jmp(&done);
5025 __ Bind(&success);
5026 __ movl(out, Immediate(1));
5027 if (zero.IsLinked()) {
5028 __ jmp(&done);
5029 }
5030 break;
5031 }
5032 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005033 // Do an exact check.
5034 NearLabel exact_check;
5035 if (cls.IsRegister()) {
5036 __ cmpl(out, cls.AsRegister<CpuRegister>());
5037 } else {
5038 DCHECK(cls.IsStackSlot()) << cls;
5039 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5040 }
5041 __ j(kEqual, &exact_check);
5042 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005043 __ movl(out, Address(out, component_offset));
5044 __ MaybeUnpoisonHeapReference(out);
5045 __ testl(out, out);
5046 // If `out` is null, we use it for the result, and jump to `done`.
5047 __ j(kEqual, &done);
5048 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5049 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005050 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005051 __ movl(out, Immediate(1));
5052 __ jmp(&done);
5053 break;
5054 }
5055 case TypeCheckKind::kArrayCheck: {
5056 if (cls.IsRegister()) {
5057 __ cmpl(out, cls.AsRegister<CpuRegister>());
5058 } else {
5059 DCHECK(cls.IsStackSlot()) << cls;
5060 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5061 }
5062 DCHECK(locations->OnlyCallsOnSlowPath());
5063 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
5064 instruction, /* is_fatal */ false);
5065 codegen_->AddSlowPath(slow_path);
5066 __ j(kNotEqual, slow_path->GetEntryLabel());
5067 __ movl(out, Immediate(1));
5068 if (zero.IsLinked()) {
5069 __ jmp(&done);
5070 }
5071 break;
5072 }
Calin Juravle98893e12015-10-02 21:05:03 +01005073 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005074 case TypeCheckKind::kInterfaceCheck:
5075 default: {
5076 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
5077 instruction,
5078 instruction->GetDexPc(),
5079 nullptr);
5080 if (zero.IsLinked()) {
5081 __ jmp(&done);
5082 }
5083 break;
5084 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005085 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005086
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005087 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005088 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005089 __ xorl(out, out);
5090 }
5091
5092 if (done.IsLinked()) {
5093 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005094 }
5095
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005096 if (slow_path != nullptr) {
5097 __ Bind(slow_path->GetExitLabel());
5098 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005099}
5100
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005101void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005102 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5103 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5104
5105 switch (instruction->GetTypeCheckKind()) {
5106 case TypeCheckKind::kExactCheck:
5107 case TypeCheckKind::kAbstractClassCheck:
5108 case TypeCheckKind::kClassHierarchyCheck:
5109 case TypeCheckKind::kArrayObjectCheck:
5110 call_kind = throws_into_catch
5111 ? LocationSummary::kCallOnSlowPath
5112 : LocationSummary::kNoCall;
5113 break;
Calin Juravle98893e12015-10-02 21:05:03 +01005114 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005115 case TypeCheckKind::kInterfaceCheck:
5116 call_kind = LocationSummary::kCall;
5117 break;
5118 case TypeCheckKind::kArrayCheck:
5119 call_kind = LocationSummary::kCallOnSlowPath;
5120 break;
5121 }
5122
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005123 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005124 instruction, call_kind);
5125 if (call_kind != LocationSummary::kCall) {
5126 locations->SetInAt(0, Location::RequiresRegister());
5127 locations->SetInAt(1, Location::Any());
5128 // Note that TypeCheckSlowPathX86_64 uses this register too.
5129 locations->AddTemp(Location::RequiresRegister());
5130 } else {
5131 InvokeRuntimeCallingConvention calling_convention;
5132 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5133 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5134 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005135}
5136
5137void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
5138 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005139 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005140 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005141 CpuRegister temp = locations->WillCall()
5142 ? CpuRegister(kNoRegister)
5143 : locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005144
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005145 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5146 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5147 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5148 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5149 SlowPathCode* slow_path = nullptr;
5150
5151 if (!locations->WillCall()) {
5152 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
5153 instruction, !locations->CanCall());
5154 codegen_->AddSlowPath(slow_path);
5155 }
5156
5157 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005158 // Avoid null check if we know obj is not null.
5159 if (instruction->MustDoNullCheck()) {
5160 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005161 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005162 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005163
5164 if (locations->WillCall()) {
5165 __ movl(obj, Address(obj, class_offset));
5166 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005167 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005168 __ movl(temp, Address(obj, class_offset));
5169 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005170 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005171
5172 switch (instruction->GetTypeCheckKind()) {
5173 case TypeCheckKind::kExactCheck:
5174 case TypeCheckKind::kArrayCheck: {
5175 if (cls.IsRegister()) {
5176 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5177 } else {
5178 DCHECK(cls.IsStackSlot()) << cls;
5179 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5180 }
5181 // Jump to slow path for throwing the exception or doing a
5182 // more involved array check.
5183 __ j(kNotEqual, slow_path->GetEntryLabel());
5184 break;
5185 }
5186 case TypeCheckKind::kAbstractClassCheck: {
5187 // If the class is abstract, we eagerly fetch the super class of the
5188 // object to avoid doing a comparison we know will fail.
5189 NearLabel loop;
5190 __ Bind(&loop);
5191 __ movl(temp, Address(temp, super_offset));
5192 __ MaybeUnpoisonHeapReference(temp);
5193 __ testl(temp, temp);
5194 // Jump to the slow path to throw the exception.
5195 __ j(kEqual, slow_path->GetEntryLabel());
5196 if (cls.IsRegister()) {
5197 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5198 } else {
5199 DCHECK(cls.IsStackSlot()) << cls;
5200 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5201 }
5202 __ j(kNotEqual, &loop);
5203 break;
5204 }
5205 case TypeCheckKind::kClassHierarchyCheck: {
5206 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005207 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005208 __ Bind(&loop);
5209 if (cls.IsRegister()) {
5210 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5211 } else {
5212 DCHECK(cls.IsStackSlot()) << cls;
5213 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5214 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005215 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005216 __ movl(temp, Address(temp, super_offset));
5217 __ MaybeUnpoisonHeapReference(temp);
5218 __ testl(temp, temp);
5219 __ j(kNotEqual, &loop);
5220 // Jump to the slow path to throw the exception.
5221 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005222 break;
5223 }
5224 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005225 // Do an exact check.
5226 if (cls.IsRegister()) {
5227 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5228 } else {
5229 DCHECK(cls.IsStackSlot()) << cls;
5230 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5231 }
5232 __ j(kEqual, &done);
5233 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005234 __ movl(temp, Address(temp, component_offset));
5235 __ MaybeUnpoisonHeapReference(temp);
5236 __ testl(temp, temp);
5237 __ j(kEqual, slow_path->GetEntryLabel());
5238 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5239 __ j(kNotEqual, slow_path->GetEntryLabel());
5240 break;
5241 }
Calin Juravle98893e12015-10-02 21:05:03 +01005242 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005243 case TypeCheckKind::kInterfaceCheck:
5244 default:
5245 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5246 instruction,
5247 instruction->GetDexPc(),
5248 nullptr);
5249 break;
5250 }
5251 __ Bind(&done);
5252
5253 if (slow_path != nullptr) {
5254 __ Bind(slow_path->GetExitLabel());
5255 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005256}
5257
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005258void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5259 LocationSummary* locations =
5260 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5261 InvokeRuntimeCallingConvention calling_convention;
5262 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5263}
5264
5265void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005266 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5267 : QUICK_ENTRY_POINT(pUnlockObject),
5268 instruction,
5269 instruction->GetDexPc(),
5270 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005271}
5272
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005273void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5274void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5275void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5276
5277void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5278 LocationSummary* locations =
5279 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5280 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5281 || instruction->GetResultType() == Primitive::kPrimLong);
5282 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005283 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005284 locations->SetOut(Location::SameAsFirstInput());
5285}
5286
5287void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5288 HandleBitwiseOperation(instruction);
5289}
5290
5291void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5292 HandleBitwiseOperation(instruction);
5293}
5294
5295void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5296 HandleBitwiseOperation(instruction);
5297}
5298
5299void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5300 LocationSummary* locations = instruction->GetLocations();
5301 Location first = locations->InAt(0);
5302 Location second = locations->InAt(1);
5303 DCHECK(first.Equals(locations->Out()));
5304
5305 if (instruction->GetResultType() == Primitive::kPrimInt) {
5306 if (second.IsRegister()) {
5307 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005308 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005309 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005310 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005311 } else {
5312 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005313 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005314 }
5315 } else if (second.IsConstant()) {
5316 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5317 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005318 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005319 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005320 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005321 } else {
5322 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005323 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005324 }
5325 } else {
5326 Address address(CpuRegister(RSP), second.GetStackIndex());
5327 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005328 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005329 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005330 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005331 } else {
5332 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005333 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005334 }
5335 }
5336 } else {
5337 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005338 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5339 bool second_is_constant = false;
5340 int64_t value = 0;
5341 if (second.IsConstant()) {
5342 second_is_constant = true;
5343 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005344 }
Mark Mendell40741f32015-04-20 22:10:34 -04005345 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005346
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005347 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005348 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005349 if (is_int32_value) {
5350 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5351 } else {
5352 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5353 }
5354 } else if (second.IsDoubleStackSlot()) {
5355 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005356 } else {
5357 __ andq(first_reg, second.AsRegister<CpuRegister>());
5358 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005359 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005360 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005361 if (is_int32_value) {
5362 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5363 } else {
5364 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5365 }
5366 } else if (second.IsDoubleStackSlot()) {
5367 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005368 } else {
5369 __ orq(first_reg, second.AsRegister<CpuRegister>());
5370 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005371 } else {
5372 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005373 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005374 if (is_int32_value) {
5375 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5376 } else {
5377 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5378 }
5379 } else if (second.IsDoubleStackSlot()) {
5380 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005381 } else {
5382 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5383 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005384 }
5385 }
5386}
5387
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005388void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005389 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005390 LOG(FATAL) << "Unreachable";
5391}
5392
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005393void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005394 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005395 LOG(FATAL) << "Unreachable";
5396}
5397
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005398void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
5399 DCHECK(codegen_->IsBaseline());
5400 LocationSummary* locations =
5401 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5402 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5403}
5404
5405void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5406 DCHECK(codegen_->IsBaseline());
5407 // Will be generated at use site.
5408}
5409
Mark Mendellfe57faa2015-09-18 09:26:15 -04005410// Simple implementation of packed switch - generate cascaded compare/jumps.
5411void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5412 LocationSummary* locations =
5413 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5414 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04005415 locations->AddTemp(Location::RequiresRegister());
5416 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04005417}
5418
5419void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5420 int32_t lower_bound = switch_instr->GetStartValue();
5421 int32_t num_entries = switch_instr->GetNumEntries();
5422 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04005423 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
5424 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
5425 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
5426
5427 // Remove the bias, if needed.
5428 Register value_reg_out = value_reg_in.AsRegister();
5429 if (lower_bound != 0) {
5430 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
5431 value_reg_out = temp_reg.AsRegister();
5432 }
5433 CpuRegister value_reg(value_reg_out);
5434
5435 // Is the value in range?
Mark Mendellfe57faa2015-09-18 09:26:15 -04005436 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
Mark Mendell9c86b482015-09-18 13:36:07 -04005437 __ cmpl(value_reg, Immediate(num_entries - 1));
5438 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005439
Mark Mendell9c86b482015-09-18 13:36:07 -04005440 // We are in the range of the table.
5441 // Load the address of the jump table in the constant area.
5442 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005443
Mark Mendell9c86b482015-09-18 13:36:07 -04005444 // Load the (signed) offset from the jump table.
5445 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
5446
5447 // Add the offset to the address of the table base.
5448 __ addq(temp_reg, base_reg);
5449
5450 // And jump.
5451 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005452}
5453
Mark Mendell92e83bf2015-05-07 11:25:03 -04005454void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
5455 if (value == 0) {
5456 __ xorl(dest, dest);
5457 } else if (value > 0 && IsInt<32>(value)) {
5458 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
5459 __ movl(dest, Immediate(static_cast<int32_t>(value)));
5460 } else {
5461 __ movq(dest, Immediate(value));
5462 }
5463}
5464
Mark Mendellcfa410b2015-05-25 16:02:44 -04005465void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
5466 DCHECK(dest.IsDoubleStackSlot());
5467 if (IsInt<32>(value)) {
5468 // Can move directly as an int32 constant.
5469 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
5470 Immediate(static_cast<int32_t>(value)));
5471 } else {
5472 Load64BitValue(CpuRegister(TMP), value);
5473 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
5474 }
5475}
5476
Mark Mendell9c86b482015-09-18 13:36:07 -04005477/**
5478 * Class to handle late fixup of offsets into constant area.
5479 */
5480class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
5481 public:
5482 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
5483 : codegen_(&codegen), offset_into_constant_area_(offset) {}
5484
5485 protected:
5486 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
5487
5488 CodeGeneratorX86_64* codegen_;
5489
5490 private:
5491 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5492 // Patch the correct offset for the instruction. We use the address of the
5493 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
5494 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
5495 int32_t relative_position = constant_offset - pos;
5496
5497 // Patch in the right value.
5498 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5499 }
5500
5501 // Location in constant area that the fixup refers to.
5502 size_t offset_into_constant_area_;
5503};
5504
5505/**
5506 t * Class to handle late fixup of offsets to a jump table that will be created in the
5507 * constant area.
5508 */
5509class JumpTableRIPFixup : public RIPFixup {
5510 public:
5511 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
5512 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
5513
5514 void CreateJumpTable() {
5515 X86_64Assembler* assembler = codegen_->GetAssembler();
5516
5517 // Ensure that the reference to the jump table has the correct offset.
5518 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
5519 SetOffset(offset_in_constant_table);
5520
5521 // Compute the offset from the start of the function to this jump table.
5522 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
5523
5524 // Populate the jump table with the correct values for the jump table.
5525 int32_t num_entries = switch_instr_->GetNumEntries();
5526 HBasicBlock* block = switch_instr_->GetBlock();
5527 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
5528 // The value that we want is the target offset - the position of the table.
5529 for (int32_t i = 0; i < num_entries; i++) {
5530 HBasicBlock* b = successors[i];
5531 Label* l = codegen_->GetLabelOf(b);
5532 DCHECK(l->IsBound());
5533 int32_t offset_to_block = l->Position() - current_table_offset;
5534 assembler->AppendInt32(offset_to_block);
5535 }
5536 }
5537
5538 private:
5539 const HPackedSwitch* switch_instr_;
5540};
5541
Mark Mendellf55c3e02015-03-26 21:07:46 -04005542void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
5543 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04005544 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04005545 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
5546 // 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 -04005547 assembler->Align(4, 0);
5548 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04005549
5550 // Populate any jump tables.
5551 for (auto jump_table : fixups_to_jump_tables_) {
5552 jump_table->CreateJumpTable();
5553 }
5554
5555 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04005556 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04005557 }
5558
5559 // And finish up.
5560 CodeGenerator::Finalize(allocator);
5561}
5562
Mark Mendellf55c3e02015-03-26 21:07:46 -04005563Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
5564 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5565 return Address::RIP(fixup);
5566}
5567
5568Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
5569 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5570 return Address::RIP(fixup);
5571}
5572
5573Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
5574 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5575 return Address::RIP(fixup);
5576}
5577
5578Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
5579 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5580 return Address::RIP(fixup);
5581}
5582
Andreas Gampe85b62f22015-09-09 13:15:38 -07005583// TODO: trg as memory.
5584void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5585 if (!trg.IsValid()) {
5586 DCHECK(type == Primitive::kPrimVoid);
5587 return;
5588 }
5589
5590 DCHECK_NE(type, Primitive::kPrimVoid);
5591
5592 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
5593 if (trg.Equals(return_loc)) {
5594 return;
5595 }
5596
5597 // Let the parallel move resolver take care of all of this.
5598 HParallelMove parallel_move(GetGraph()->GetArena());
5599 parallel_move.AddMove(return_loc, trg, type, nullptr);
5600 GetMoveResolver()->EmitNativeCode(&parallel_move);
5601}
5602
Mark Mendell9c86b482015-09-18 13:36:07 -04005603Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
5604 // Create a fixup to be used to create and address the jump table.
5605 JumpTableRIPFixup* table_fixup =
5606 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
5607
5608 // We have to populate the jump tables.
5609 fixups_to_jump_tables_.push_back(table_fixup);
5610 return Address::RIP(table_fixup);
5611}
5612
Mark Mendellea5af682015-10-22 17:35:49 -04005613void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
5614 const Address& addr_high,
5615 int64_t v,
5616 HInstruction* instruction) {
5617 if (IsInt<32>(v)) {
5618 int32_t v_32 = v;
5619 __ movq(addr_low, Immediate(v_32));
5620 MaybeRecordImplicitNullCheck(instruction);
5621 } else {
5622 // Didn't fit in a register. Do it in pieces.
5623 int32_t low_v = Low32Bits(v);
5624 int32_t high_v = High32Bits(v);
5625 __ movl(addr_low, Immediate(low_v));
5626 MaybeRecordImplicitNullCheck(instruction);
5627 __ movl(addr_high, Immediate(high_v));
5628 }
5629}
5630
Roland Levillain4d027112015-07-01 15:41:14 +01005631#undef __
5632
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005633} // namespace x86_64
5634} // namespace art