blob: e2ad6673c78ad9d495ede9809890f3b953c1cdd0 [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:
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000517 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
518 invoke->GetDexCacheArrayOffset());
Vladimir Marko58155012015-08-19 12:49:41 +0000519 __ movq(temp.AsRegister<CpuRegister>(),
520 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
521 // Bind the label at the end of the "movl" insn.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000522 __ Bind(&pc_relative_dex_cache_patches_.back().label);
Vladimir Marko58155012015-08-19 12:49:41 +0000523 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 =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000596 method_patches_.size() +
597 relative_call_patches_.size() +
598 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000599 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000600 // The label points to the end of the "movl" insn but the literal offset for method
601 // patch needs to point to the embedded constant which occupies the last 4 bytes.
602 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000603 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000604 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000605 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
606 info.target_method.dex_file,
607 info.target_method.dex_method_index));
608 }
609 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000610 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000611 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
612 info.target_method.dex_file,
613 info.target_method.dex_method_index));
614 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000615 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
616 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000617 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
618 &info.target_dex_file,
619 info.label.Position(),
620 info.element_offset));
621 }
622}
623
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100624void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100625 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100626}
627
628void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100629 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100630}
631
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100632size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
633 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
634 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100635}
636
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100637size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
638 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
639 return kX86_64WordSize;
640}
641
642size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
643 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
644 return kX86_64WordSize;
645}
646
647size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
648 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
649 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100650}
651
Calin Juravle175dc732015-08-25 15:42:32 +0100652void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
653 HInstruction* instruction,
654 uint32_t dex_pc,
655 SlowPathCode* slow_path) {
656 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
657 instruction,
658 dex_pc,
659 slow_path);
660}
661
662void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100663 HInstruction* instruction,
664 uint32_t dex_pc,
665 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100666 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100667 __ gs()->call(Address::Absolute(entry_point_offset, true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100668 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100669}
670
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000671static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000672// Use a fake return address register to mimic Quick.
673static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400674CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
675 const X86_64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100676 const CompilerOptions& compiler_options,
677 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000678 : CodeGenerator(graph,
679 kNumberOfCpuRegisters,
680 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000681 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000682 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
683 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000684 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000685 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
686 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100687 compiler_options,
688 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100689 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100690 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000691 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400692 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400693 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000694 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100695 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
696 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000697 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -0400698 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000699 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
700}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100701
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100702InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
703 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704 : HGraphVisitor(graph),
705 assembler_(codegen->GetAssembler()),
706 codegen_(codegen) {}
707
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100708Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100709 switch (type) {
710 case Primitive::kPrimLong:
711 case Primitive::kPrimByte:
712 case Primitive::kPrimBoolean:
713 case Primitive::kPrimChar:
714 case Primitive::kPrimShort:
715 case Primitive::kPrimInt:
716 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100717 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100718 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100719 }
720
721 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100722 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100723 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100724 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100725 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100726
727 case Primitive::kPrimVoid:
728 LOG(FATAL) << "Unreachable type " << type;
729 }
730
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100731 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100732}
733
Nicolas Geoffray98893962015-01-21 12:32:32 +0000734void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100735 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100736 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100737
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000738 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100739 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000740
Nicolas Geoffray98893962015-01-21 12:32:32 +0000741 if (is_baseline) {
742 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
743 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
744 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000745 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
746 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
747 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000748 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100749}
750
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100751static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100752 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100753}
David Srbecky9d8606d2015-04-12 09:35:32 +0100754
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100755static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100756 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100757}
758
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100759void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100760 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000761 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100762 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700763 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000764 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100765
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000766 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100767 __ testq(CpuRegister(RAX), Address(
768 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100769 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100770 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000771
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000772 if (HasEmptyFrame()) {
773 return;
774 }
775
Nicolas Geoffray98893962015-01-21 12:32:32 +0000776 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000777 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000778 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000779 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100780 __ cfi().AdjustCFAOffset(kX86_64WordSize);
781 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +0000782 }
783 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100784
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100785 int adjust = GetFrameSize() - GetCoreSpillSize();
786 __ subq(CpuRegister(RSP), Immediate(adjust));
787 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000788 uint32_t xmm_spill_location = GetFpuSpillStart();
789 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100790
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000791 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
792 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100793 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
794 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
795 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000796 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100797 }
798
Mathieu Chartiere401d142015-04-22 13:56:20 -0700799 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100800 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100801}
802
803void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100804 __ cfi().RememberState();
805 if (!HasEmptyFrame()) {
806 uint32_t xmm_spill_location = GetFpuSpillStart();
807 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
808 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
809 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
810 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
811 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
812 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
813 }
814 }
815
816 int adjust = GetFrameSize() - GetCoreSpillSize();
817 __ addq(CpuRegister(RSP), Immediate(adjust));
818 __ cfi().AdjustCFAOffset(-adjust);
819
820 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
821 Register reg = kCoreCalleeSaves[i];
822 if (allocated_registers_.ContainsCoreRegister(reg)) {
823 __ popq(CpuRegister(reg));
824 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
825 __ cfi().Restore(DWARFReg(reg));
826 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000827 }
828 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100829 __ ret();
830 __ cfi().RestoreState();
831 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100832}
833
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100834void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
835 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100836}
837
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100838Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
839 switch (load->GetType()) {
840 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100841 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100842 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100843
844 case Primitive::kPrimInt:
845 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100847 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100848
849 case Primitive::kPrimBoolean:
850 case Primitive::kPrimByte:
851 case Primitive::kPrimChar:
852 case Primitive::kPrimShort:
853 case Primitive::kPrimVoid:
854 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700855 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100856 }
857
858 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700859 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100860}
861
862void CodeGeneratorX86_64::Move(Location destination, Location source) {
863 if (source.Equals(destination)) {
864 return;
865 }
866 if (destination.IsRegister()) {
867 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000868 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100869 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000870 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100871 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000872 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100873 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100874 } else {
875 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000876 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100877 Address(CpuRegister(RSP), source.GetStackIndex()));
878 }
879 } else if (destination.IsFpuRegister()) {
880 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000881 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100882 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000883 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100884 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000885 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100886 Address(CpuRegister(RSP), source.GetStackIndex()));
887 } else {
888 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000889 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100890 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100891 }
892 } else if (destination.IsStackSlot()) {
893 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100894 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000895 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100896 } else if (source.IsFpuRegister()) {
897 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000898 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500899 } else if (source.IsConstant()) {
900 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000901 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500902 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100903 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500904 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000905 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
906 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100907 }
908 } else {
909 DCHECK(destination.IsDoubleStackSlot());
910 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100911 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000912 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100913 } else if (source.IsFpuRegister()) {
914 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000915 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500916 } else if (source.IsConstant()) {
917 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +0800918 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500919 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000920 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -0500921 } else {
922 DCHECK(constant->IsLongConstant());
923 value = constant->AsLongConstant()->GetValue();
924 }
Mark Mendellcfa410b2015-05-25 16:02:44 -0400925 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100926 } else {
927 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000928 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
929 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100930 }
931 }
932}
933
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100934void CodeGeneratorX86_64::Move(HInstruction* instruction,
935 Location location,
936 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000937 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100938 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700939 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100940 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000941 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100942 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000943 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000944 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
945 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000946 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000947 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000948 } else if (location.IsStackSlot()) {
949 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
950 } else {
951 DCHECK(location.IsConstant());
952 DCHECK_EQ(location.GetConstant(), const_to_move);
953 }
954 } else if (const_to_move->IsLongConstant()) {
955 int64_t value = const_to_move->AsLongConstant()->GetValue();
956 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400957 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000958 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400959 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000960 } else {
961 DCHECK(location.IsConstant());
962 DCHECK_EQ(location.GetConstant(), const_to_move);
963 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100964 }
Roland Levillain476df552014-10-09 17:51:36 +0100965 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100966 switch (instruction->GetType()) {
967 case Primitive::kPrimBoolean:
968 case Primitive::kPrimByte:
969 case Primitive::kPrimChar:
970 case Primitive::kPrimShort:
971 case Primitive::kPrimInt:
972 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100974 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
975 break;
976
977 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100978 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +0000979 Move(location,
980 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100981 break;
982
983 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100984 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100985 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000986 } else if (instruction->IsTemporary()) {
987 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
988 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100989 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100990 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100991 switch (instruction->GetType()) {
992 case Primitive::kPrimBoolean:
993 case Primitive::kPrimByte:
994 case Primitive::kPrimChar:
995 case Primitive::kPrimShort:
996 case Primitive::kPrimInt:
997 case Primitive::kPrimNot:
998 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100999 case Primitive::kPrimFloat:
1000 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001001 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001002 break;
1003
1004 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001005 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001006 }
1007 }
1008}
1009
Calin Juravle175dc732015-08-25 15:42:32 +01001010void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1011 DCHECK(location.IsRegister());
1012 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1013}
1014
Calin Juravlee460d1d2015-09-29 04:52:17 +01001015void CodeGeneratorX86_64::MoveLocation(
1016 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1017 Move(dst, src);
1018}
1019
1020void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1021 if (location.IsRegister()) {
1022 locations->AddTemp(location);
1023 } else {
1024 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1025 }
1026}
1027
David Brazdilfc6a86a2015-06-26 10:33:45 +00001028void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001029 DCHECK(!successor->IsExitBlock());
1030
1031 HBasicBlock* block = got->GetBlock();
1032 HInstruction* previous = got->GetPrevious();
1033
1034 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001035 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001036 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1037 return;
1038 }
1039
1040 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1041 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1042 }
1043 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001044 __ jmp(codegen_->GetLabelOf(successor));
1045 }
1046}
1047
David Brazdilfc6a86a2015-06-26 10:33:45 +00001048void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1049 got->SetLocations(nullptr);
1050}
1051
1052void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1053 HandleGoto(got, got->GetSuccessor());
1054}
1055
1056void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1057 try_boundary->SetLocations(nullptr);
1058}
1059
1060void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1061 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1062 if (!successor->IsExitBlock()) {
1063 HandleGoto(try_boundary, successor);
1064 }
1065}
1066
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001067void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1068 exit->SetLocations(nullptr);
1069}
1070
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001071void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001072}
1073
Mark Mendellc4701932015-04-10 13:18:51 -04001074void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
1075 Label* true_label,
1076 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001077 if (cond->IsFPConditionTrueIfNaN()) {
1078 __ j(kUnordered, true_label);
1079 } else if (cond->IsFPConditionFalseIfNaN()) {
1080 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001081 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001082 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001083}
1084
1085void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HIf* if_instr,
1086 HCondition* condition,
1087 Label* true_target,
1088 Label* false_target,
1089 Label* always_true_target) {
1090 LocationSummary* locations = condition->GetLocations();
1091 Location left = locations->InAt(0);
1092 Location right = locations->InAt(1);
1093
1094 // We don't want true_target as a nullptr.
1095 if (true_target == nullptr) {
1096 true_target = always_true_target;
1097 }
1098 bool falls_through = (false_target == nullptr);
1099
1100 // FP compares don't like null false_targets.
1101 if (false_target == nullptr) {
1102 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1103 }
1104
1105 Primitive::Type type = condition->InputAt(0)->GetType();
1106 switch (type) {
1107 case Primitive::kPrimLong: {
1108 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1109 if (right.IsConstant()) {
1110 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1111 if (IsInt<32>(value)) {
1112 if (value == 0) {
1113 __ testq(left_reg, left_reg);
1114 } else {
1115 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1116 }
1117 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001118 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001119 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1120 }
1121 } else if (right.IsDoubleStackSlot()) {
1122 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1123 } else {
1124 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1125 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001126 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001127 break;
1128 }
1129 case Primitive::kPrimFloat: {
1130 if (right.IsFpuRegister()) {
1131 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1132 } else if (right.IsConstant()) {
1133 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1134 codegen_->LiteralFloatAddress(
1135 right.GetConstant()->AsFloatConstant()->GetValue()));
1136 } else {
1137 DCHECK(right.IsStackSlot());
1138 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1139 Address(CpuRegister(RSP), right.GetStackIndex()));
1140 }
1141 GenerateFPJumps(condition, true_target, false_target);
1142 break;
1143 }
1144 case Primitive::kPrimDouble: {
1145 if (right.IsFpuRegister()) {
1146 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1147 } else if (right.IsConstant()) {
1148 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1149 codegen_->LiteralDoubleAddress(
1150 right.GetConstant()->AsDoubleConstant()->GetValue()));
1151 } else {
1152 DCHECK(right.IsDoubleStackSlot());
1153 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1154 Address(CpuRegister(RSP), right.GetStackIndex()));
1155 }
1156 GenerateFPJumps(condition, true_target, false_target);
1157 break;
1158 }
1159 default:
1160 LOG(FATAL) << "Unexpected condition type " << type;
1161 }
1162
1163 if (!falls_through) {
1164 __ jmp(false_target);
1165 }
1166}
1167
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001168void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
1169 Label* true_target,
1170 Label* false_target,
1171 Label* always_true_target) {
1172 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001173 if (cond->IsIntConstant()) {
1174 // Constant condition, statically compared against 1.
1175 int32_t cond_value = cond->AsIntConstant()->GetValue();
1176 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001177 if (always_true_target != nullptr) {
1178 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001179 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001180 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001181 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001182 DCHECK_EQ(cond_value, 0);
1183 }
1184 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001185 HCondition* condition = cond->AsCondition();
1186 bool is_materialized = condition == nullptr || condition->NeedsMaterialization();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001187 // Moves do not affect the eflags register, so if the condition is
1188 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001189 // again. We can't use the eflags on FP conditions if they are
1190 // materialized due to the complex branching.
Mark Mendellb8b97692015-05-22 16:58:19 -04001191 Primitive::Type type = (condition != nullptr)
1192 ? cond->InputAt(0)->GetType()
1193 : Primitive::kPrimInt;
1194 bool eflags_set = condition != nullptr
1195 && condition->IsBeforeWhenDisregardMoves(instruction)
Mark Mendellc4701932015-04-10 13:18:51 -04001196 && !Primitive::IsFloatingPointType(type);
Mark Mendellb8b97692015-05-22 16:58:19 -04001197 // Can we optimize the jump if we know that the next block is the true case?
1198 bool can_jump_to_false = CanReverseCondition(always_true_target, false_target, condition);
Mark Mendellc4701932015-04-10 13:18:51 -04001199
Roland Levillain4fa13f62015-07-06 18:11:54 +01001200 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001201 if (!eflags_set) {
1202 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001203 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001204 if (lhs.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001205 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001206 } else {
1207 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
1208 Immediate(0));
1209 }
Mark Mendellb8b97692015-05-22 16:58:19 -04001210 if (can_jump_to_false) {
1211 __ j(kEqual, false_target);
1212 return;
1213 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001214 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001215 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001216 if (can_jump_to_false) {
1217 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1218 return;
1219 }
1220 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001221 }
1222 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001223 // Condition has not been materialized, use its inputs as the
1224 // comparison and its condition as the branch condition.
1225
Mark Mendellc4701932015-04-10 13:18:51 -04001226 // Is this a long or FP comparison that has been folded into the HCondition?
1227 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001228 // Generate the comparison directly.
Mark Mendellb8b97692015-05-22 16:58:19 -04001229 GenerateCompareTestAndBranch(instruction->AsIf(), condition,
Mark Mendellc4701932015-04-10 13:18:51 -04001230 true_target, false_target, always_true_target);
1231 return;
1232 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001233
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001234 Location lhs = cond->GetLocations()->InAt(0);
1235 Location rhs = cond->GetLocations()->InAt(1);
1236 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001238 } else if (rhs.IsConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001239 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00001240 if (constant == 0) {
1241 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1242 } else {
1243 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1244 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001245 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001246 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001247 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1248 }
Mark Mendellb8b97692015-05-22 16:58:19 -04001249
1250 if (can_jump_to_false) {
1251 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1252 return;
1253 }
1254
1255 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001256 }
Dave Allison20dfc792014-06-16 20:44:29 -07001257 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001258 if (false_target != nullptr) {
1259 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260 }
1261}
1262
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001263void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
1264 LocationSummary* locations =
1265 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1266 HInstruction* cond = if_instr->InputAt(0);
1267 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1268 locations->SetInAt(0, Location::Any());
1269 }
1270}
1271
1272void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
1273 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1274 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1275 Label* always_true_target = true_target;
1276 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1277 if_instr->IfTrueSuccessor())) {
1278 always_true_target = nullptr;
1279 }
1280 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1281 if_instr->IfFalseSuccessor())) {
1282 false_target = nullptr;
1283 }
1284 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1285}
1286
1287void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1288 LocationSummary* locations = new (GetGraph()->GetArena())
1289 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1290 HInstruction* cond = deoptimize->InputAt(0);
Aart Bikbb245d12015-10-19 11:05:03 -07001291 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001292 locations->SetInAt(0, Location::Any());
1293 }
1294}
1295
1296void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001297 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001298 DeoptimizationSlowPathX86_64(deoptimize);
1299 codegen_->AddSlowPath(slow_path);
1300 Label* slow_path_entry = slow_path->GetEntryLabel();
1301 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1302}
1303
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001304void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1305 local->SetLocations(nullptr);
1306}
1307
1308void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1309 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1310}
1311
1312void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1313 local->SetLocations(nullptr);
1314}
1315
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001316void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001317 // Nothing to do, this is driven by the code generator.
1318}
1319
1320void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001321 LocationSummary* locations =
1322 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001323 switch (store->InputAt(1)->GetType()) {
1324 case Primitive::kPrimBoolean:
1325 case Primitive::kPrimByte:
1326 case Primitive::kPrimChar:
1327 case Primitive::kPrimShort:
1328 case Primitive::kPrimInt:
1329 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001330 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001331 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1332 break;
1333
1334 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001335 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001336 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1337 break;
1338
1339 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001340 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001341 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001342}
1343
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001344void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001345}
1346
Roland Levillain0d37cd02015-05-27 16:39:19 +01001347void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001348 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001349 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001350 // Handle the long/FP comparisons made in instruction simplification.
1351 switch (cond->InputAt(0)->GetType()) {
1352 case Primitive::kPrimLong:
1353 locations->SetInAt(0, Location::RequiresRegister());
1354 locations->SetInAt(1, Location::Any());
1355 break;
1356 case Primitive::kPrimFloat:
1357 case Primitive::kPrimDouble:
1358 locations->SetInAt(0, Location::RequiresFpuRegister());
1359 locations->SetInAt(1, Location::Any());
1360 break;
1361 default:
1362 locations->SetInAt(0, Location::RequiresRegister());
1363 locations->SetInAt(1, Location::Any());
1364 break;
1365 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001366 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001367 locations->SetOut(Location::RequiresRegister());
1368 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001369}
1370
Roland Levillain0d37cd02015-05-27 16:39:19 +01001371void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001372 if (!cond->NeedsMaterialization()) {
1373 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001374 }
Mark Mendellc4701932015-04-10 13:18:51 -04001375
1376 LocationSummary* locations = cond->GetLocations();
1377 Location lhs = locations->InAt(0);
1378 Location rhs = locations->InAt(1);
1379 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1380 Label true_label, false_label;
1381
1382 switch (cond->InputAt(0)->GetType()) {
1383 default:
1384 // Integer case.
1385
1386 // Clear output register: setcc only sets the low byte.
1387 __ xorl(reg, reg);
1388
1389 if (rhs.IsRegister()) {
1390 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1391 } else if (rhs.IsConstant()) {
1392 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1393 if (constant == 0) {
1394 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1395 } else {
1396 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1397 }
1398 } else {
1399 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1400 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001401 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001402 return;
1403 case Primitive::kPrimLong:
1404 // Clear output register: setcc only sets the low byte.
1405 __ xorl(reg, reg);
1406
1407 if (rhs.IsRegister()) {
1408 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1409 } else if (rhs.IsConstant()) {
1410 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1411 if (IsInt<32>(value)) {
1412 if (value == 0) {
1413 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1414 } else {
1415 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1416 }
1417 } else {
1418 // Value won't fit in an int.
1419 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1420 }
1421 } else {
1422 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1423 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001424 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001425 return;
1426 case Primitive::kPrimFloat: {
1427 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1428 if (rhs.IsConstant()) {
1429 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1430 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1431 } else if (rhs.IsStackSlot()) {
1432 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1433 } else {
1434 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1435 }
1436 GenerateFPJumps(cond, &true_label, &false_label);
1437 break;
1438 }
1439 case Primitive::kPrimDouble: {
1440 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1441 if (rhs.IsConstant()) {
1442 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1443 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1444 } else if (rhs.IsDoubleStackSlot()) {
1445 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1446 } else {
1447 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1448 }
1449 GenerateFPJumps(cond, &true_label, &false_label);
1450 break;
1451 }
1452 }
1453
1454 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001455 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001456
Roland Levillain4fa13f62015-07-06 18:11:54 +01001457 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001458 __ Bind(&false_label);
1459 __ xorl(reg, reg);
1460 __ jmp(&done_label);
1461
Roland Levillain4fa13f62015-07-06 18:11:54 +01001462 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001463 __ Bind(&true_label);
1464 __ movl(reg, Immediate(1));
1465 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001466}
1467
1468void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1469 VisitCondition(comp);
1470}
1471
1472void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1473 VisitCondition(comp);
1474}
1475
1476void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1477 VisitCondition(comp);
1478}
1479
1480void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1481 VisitCondition(comp);
1482}
1483
1484void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1485 VisitCondition(comp);
1486}
1487
1488void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1489 VisitCondition(comp);
1490}
1491
1492void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1493 VisitCondition(comp);
1494}
1495
1496void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1497 VisitCondition(comp);
1498}
1499
1500void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1501 VisitCondition(comp);
1502}
1503
1504void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1505 VisitCondition(comp);
1506}
1507
1508void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1509 VisitCondition(comp);
1510}
1511
1512void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1513 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001514}
1515
Aart Bike9f37602015-10-09 11:15:55 -07001516void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
1517 VisitCondition(comp);
1518}
1519
1520void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
1521 VisitCondition(comp);
1522}
1523
1524void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
1525 VisitCondition(comp);
1526}
1527
1528void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
1529 VisitCondition(comp);
1530}
1531
1532void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
1533 VisitCondition(comp);
1534}
1535
1536void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
1537 VisitCondition(comp);
1538}
1539
1540void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
1541 VisitCondition(comp);
1542}
1543
1544void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
1545 VisitCondition(comp);
1546}
1547
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001548void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001549 LocationSummary* locations =
1550 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001551 switch (compare->InputAt(0)->GetType()) {
1552 case Primitive::kPrimLong: {
1553 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001554 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001555 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1556 break;
1557 }
1558 case Primitive::kPrimFloat:
1559 case Primitive::kPrimDouble: {
1560 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001561 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001562 locations->SetOut(Location::RequiresRegister());
1563 break;
1564 }
1565 default:
1566 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1567 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001568}
1569
1570void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001571 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001572 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001573 Location left = locations->InAt(0);
1574 Location right = locations->InAt(1);
1575
Mark Mendell0c9497d2015-08-21 09:30:05 -04001576 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001577 Primitive::Type type = compare->InputAt(0)->GetType();
1578 switch (type) {
1579 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001580 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1581 if (right.IsConstant()) {
1582 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001583 if (IsInt<32>(value)) {
1584 if (value == 0) {
1585 __ testq(left_reg, left_reg);
1586 } else {
1587 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1588 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001589 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001590 // Value won't fit in an int.
1591 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001592 }
Mark Mendell40741f32015-04-20 22:10:34 -04001593 } else if (right.IsDoubleStackSlot()) {
1594 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001595 } else {
1596 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1597 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001598 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001599 }
1600 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001601 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1602 if (right.IsConstant()) {
1603 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1604 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1605 } else if (right.IsStackSlot()) {
1606 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1607 } else {
1608 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1609 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001610 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1611 break;
1612 }
1613 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001614 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1615 if (right.IsConstant()) {
1616 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1617 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1618 } else if (right.IsDoubleStackSlot()) {
1619 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1620 } else {
1621 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1622 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001623 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1624 break;
1625 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001626 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001627 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001628 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001629 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001630 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001631 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001632
Calin Juravle91debbc2014-11-26 19:01:09 +00001633 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001634 __ movl(out, Immediate(1));
1635 __ jmp(&done);
1636
1637 __ Bind(&less);
1638 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001639
1640 __ Bind(&done);
1641}
1642
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001643void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001644 LocationSummary* locations =
1645 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001646 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001647}
1648
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001649void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001650 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001651}
1652
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001653void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1654 LocationSummary* locations =
1655 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1656 locations->SetOut(Location::ConstantLocation(constant));
1657}
1658
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001659void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001660 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001661}
1662
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001663void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001664 LocationSummary* locations =
1665 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001666 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001667}
1668
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001669void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001670 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001671}
1672
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001673void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1674 LocationSummary* locations =
1675 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1676 locations->SetOut(Location::ConstantLocation(constant));
1677}
1678
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001679void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001680 // Will be generated at use site.
1681}
1682
1683void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1684 LocationSummary* locations =
1685 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1686 locations->SetOut(Location::ConstantLocation(constant));
1687}
1688
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001689void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1690 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001691 // Will be generated at use site.
1692}
1693
Calin Juravle27df7582015-04-17 19:12:31 +01001694void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1695 memory_barrier->SetLocations(nullptr);
1696}
1697
1698void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1699 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1700}
1701
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001702void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1703 ret->SetLocations(nullptr);
1704}
1705
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001706void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001707 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001708}
1709
1710void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001711 LocationSummary* locations =
1712 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001713 switch (ret->InputAt(0)->GetType()) {
1714 case Primitive::kPrimBoolean:
1715 case Primitive::kPrimByte:
1716 case Primitive::kPrimChar:
1717 case Primitive::kPrimShort:
1718 case Primitive::kPrimInt:
1719 case Primitive::kPrimNot:
1720 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001721 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001722 break;
1723
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001724 case Primitive::kPrimFloat:
1725 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001726 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001727 break;
1728
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001729 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001730 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001731 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001732}
1733
1734void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1735 if (kIsDebugBuild) {
1736 switch (ret->InputAt(0)->GetType()) {
1737 case Primitive::kPrimBoolean:
1738 case Primitive::kPrimByte:
1739 case Primitive::kPrimChar:
1740 case Primitive::kPrimShort:
1741 case Primitive::kPrimInt:
1742 case Primitive::kPrimNot:
1743 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001744 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001745 break;
1746
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001747 case Primitive::kPrimFloat:
1748 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001749 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001750 XMM0);
1751 break;
1752
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001753 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001754 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001755 }
1756 }
1757 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001758}
1759
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001760Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1761 switch (type) {
1762 case Primitive::kPrimBoolean:
1763 case Primitive::kPrimByte:
1764 case Primitive::kPrimChar:
1765 case Primitive::kPrimShort:
1766 case Primitive::kPrimInt:
1767 case Primitive::kPrimNot:
1768 case Primitive::kPrimLong:
1769 return Location::RegisterLocation(RAX);
1770
1771 case Primitive::kPrimVoid:
1772 return Location::NoLocation();
1773
1774 case Primitive::kPrimDouble:
1775 case Primitive::kPrimFloat:
1776 return Location::FpuRegisterLocation(XMM0);
1777 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001778
1779 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001780}
1781
1782Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
1783 return Location::RegisterLocation(kMethodRegisterArgument);
1784}
1785
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001786Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001787 switch (type) {
1788 case Primitive::kPrimBoolean:
1789 case Primitive::kPrimByte:
1790 case Primitive::kPrimChar:
1791 case Primitive::kPrimShort:
1792 case Primitive::kPrimInt:
1793 case Primitive::kPrimNot: {
1794 uint32_t index = gp_index_++;
1795 stack_index_++;
1796 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001797 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001798 } else {
1799 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1800 }
1801 }
1802
1803 case Primitive::kPrimLong: {
1804 uint32_t index = gp_index_;
1805 stack_index_ += 2;
1806 if (index < calling_convention.GetNumberOfRegisters()) {
1807 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001808 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001809 } else {
1810 gp_index_ += 2;
1811 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1812 }
1813 }
1814
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001815 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001816 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001817 stack_index_++;
1818 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001819 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001820 } else {
1821 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1822 }
1823 }
1824
1825 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001826 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001827 stack_index_ += 2;
1828 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001829 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001830 } else {
1831 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1832 }
1833 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001834
1835 case Primitive::kPrimVoid:
1836 LOG(FATAL) << "Unexpected parameter type " << type;
1837 break;
1838 }
1839 return Location();
1840}
1841
Calin Juravle175dc732015-08-25 15:42:32 +01001842void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1843 // The trampoline uses the same calling convention as dex calling conventions,
1844 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1845 // the method_idx.
1846 HandleInvoke(invoke);
1847}
1848
1849void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1850 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1851}
1852
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001853void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001854 // When we do not run baseline, explicit clinit checks triggered by static
1855 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1856 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001857
Mark Mendellfb8d2792015-03-31 22:16:59 -04001858 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001859 if (intrinsic.TryDispatch(invoke)) {
1860 return;
1861 }
1862
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001863 HandleInvoke(invoke);
1864}
1865
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001866static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1867 if (invoke->GetLocations()->Intrinsified()) {
1868 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
1869 intrinsic.Dispatch(invoke);
1870 return true;
1871 }
1872 return false;
1873}
1874
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001875void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001876 // When we do not run baseline, explicit clinit checks triggered by static
1877 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1878 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001879
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001880 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1881 return;
1882 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001883
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001884 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001885 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001886 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001887 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001888}
1889
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001890void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001891 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001892 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001893}
1894
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001895void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001896 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001897 if (intrinsic.TryDispatch(invoke)) {
1898 return;
1899 }
1900
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001901 HandleInvoke(invoke);
1902}
1903
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001904void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001905 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1906 return;
1907 }
1908
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001909 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001910
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001911 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001912 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001913}
1914
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001915void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1916 HandleInvoke(invoke);
1917 // Add the hidden argument.
1918 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1919}
1920
1921void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1922 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001923 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001924 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1925 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001926 LocationSummary* locations = invoke->GetLocations();
1927 Location receiver = locations->InAt(0);
1928 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1929
1930 // Set the hidden argument.
Mark Mendell92e83bf2015-05-07 11:25:03 -04001931 CpuRegister hidden_reg = invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>();
1932 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001933
1934 // temp = object->GetClass();
1935 if (receiver.IsStackSlot()) {
1936 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1937 __ movl(temp, Address(temp, class_offset));
1938 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001939 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001940 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001941 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001942 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001943 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001944 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001945 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001946 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001947 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001948
1949 DCHECK(!codegen_->IsLeafMethod());
1950 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1951}
1952
Roland Levillain88cb1752014-10-20 16:36:47 +01001953void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1954 LocationSummary* locations =
1955 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1956 switch (neg->GetResultType()) {
1957 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001958 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001959 locations->SetInAt(0, Location::RequiresRegister());
1960 locations->SetOut(Location::SameAsFirstInput());
1961 break;
1962
Roland Levillain88cb1752014-10-20 16:36:47 +01001963 case Primitive::kPrimFloat:
1964 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001965 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001966 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00001967 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001968 break;
1969
1970 default:
1971 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1972 }
1973}
1974
1975void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1976 LocationSummary* locations = neg->GetLocations();
1977 Location out = locations->Out();
1978 Location in = locations->InAt(0);
1979 switch (neg->GetResultType()) {
1980 case Primitive::kPrimInt:
1981 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001982 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001983 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001984 break;
1985
1986 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001987 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001988 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001989 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001990 break;
1991
Roland Levillain5368c212014-11-27 15:03:41 +00001992 case Primitive::kPrimFloat: {
1993 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04001994 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001995 // Implement float negation with an exclusive or with value
1996 // 0x80000000 (mask for bit 31, representing the sign of a
1997 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04001998 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001999 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002000 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002001 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002002
Roland Levillain5368c212014-11-27 15:03:41 +00002003 case Primitive::kPrimDouble: {
2004 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002005 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002006 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002007 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002008 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002009 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002010 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002011 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002012 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002013
2014 default:
2015 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2016 }
2017}
2018
Roland Levillaindff1f282014-11-05 14:15:05 +00002019void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2020 LocationSummary* locations =
2021 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2022 Primitive::Type result_type = conversion->GetResultType();
2023 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002024 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002025
David Brazdilb2bd1c52015-03-25 11:17:37 +00002026 // The Java language does not allow treating boolean as an integral type but
2027 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002028
Roland Levillaindff1f282014-11-05 14:15:05 +00002029 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002030 case Primitive::kPrimByte:
2031 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002032 case Primitive::kPrimBoolean:
2033 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002034 case Primitive::kPrimShort:
2035 case Primitive::kPrimInt:
2036 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002037 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002038 locations->SetInAt(0, Location::Any());
2039 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2040 break;
2041
2042 default:
2043 LOG(FATAL) << "Unexpected type conversion from " << input_type
2044 << " to " << result_type;
2045 }
2046 break;
2047
Roland Levillain01a8d712014-11-14 16:27:39 +00002048 case Primitive::kPrimShort:
2049 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002050 case Primitive::kPrimBoolean:
2051 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002052 case Primitive::kPrimByte:
2053 case Primitive::kPrimInt:
2054 case Primitive::kPrimChar:
2055 // Processing a Dex `int-to-short' instruction.
2056 locations->SetInAt(0, Location::Any());
2057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2058 break;
2059
2060 default:
2061 LOG(FATAL) << "Unexpected type conversion from " << input_type
2062 << " to " << result_type;
2063 }
2064 break;
2065
Roland Levillain946e1432014-11-11 17:35:19 +00002066 case Primitive::kPrimInt:
2067 switch (input_type) {
2068 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002069 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002070 locations->SetInAt(0, Location::Any());
2071 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2072 break;
2073
2074 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002075 // Processing a Dex `float-to-int' instruction.
2076 locations->SetInAt(0, Location::RequiresFpuRegister());
2077 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002078 break;
2079
Roland Levillain946e1432014-11-11 17:35:19 +00002080 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002081 // Processing a Dex `double-to-int' instruction.
2082 locations->SetInAt(0, Location::RequiresFpuRegister());
2083 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002084 break;
2085
2086 default:
2087 LOG(FATAL) << "Unexpected type conversion from " << input_type
2088 << " to " << result_type;
2089 }
2090 break;
2091
Roland Levillaindff1f282014-11-05 14:15:05 +00002092 case Primitive::kPrimLong:
2093 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002094 case Primitive::kPrimBoolean:
2095 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002096 case Primitive::kPrimByte:
2097 case Primitive::kPrimShort:
2098 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002099 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002100 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002101 // TODO: We would benefit from a (to-be-implemented)
2102 // Location::RegisterOrStackSlot requirement for this input.
2103 locations->SetInAt(0, Location::RequiresRegister());
2104 locations->SetOut(Location::RequiresRegister());
2105 break;
2106
2107 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002108 // Processing a Dex `float-to-long' instruction.
2109 locations->SetInAt(0, Location::RequiresFpuRegister());
2110 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002111 break;
2112
Roland Levillaindff1f282014-11-05 14:15:05 +00002113 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002114 // Processing a Dex `double-to-long' instruction.
2115 locations->SetInAt(0, Location::RequiresFpuRegister());
2116 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002117 break;
2118
2119 default:
2120 LOG(FATAL) << "Unexpected type conversion from " << input_type
2121 << " to " << result_type;
2122 }
2123 break;
2124
Roland Levillain981e4542014-11-14 11:47:14 +00002125 case Primitive::kPrimChar:
2126 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002127 case Primitive::kPrimBoolean:
2128 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002129 case Primitive::kPrimByte:
2130 case Primitive::kPrimShort:
2131 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002132 // Processing a Dex `int-to-char' instruction.
2133 locations->SetInAt(0, Location::Any());
2134 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2135 break;
2136
2137 default:
2138 LOG(FATAL) << "Unexpected type conversion from " << input_type
2139 << " to " << result_type;
2140 }
2141 break;
2142
Roland Levillaindff1f282014-11-05 14:15:05 +00002143 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002144 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002145 case Primitive::kPrimBoolean:
2146 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002147 case Primitive::kPrimByte:
2148 case Primitive::kPrimShort:
2149 case Primitive::kPrimInt:
2150 case Primitive::kPrimChar:
2151 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002152 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002153 locations->SetOut(Location::RequiresFpuRegister());
2154 break;
2155
2156 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002157 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002158 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002159 locations->SetOut(Location::RequiresFpuRegister());
2160 break;
2161
Roland Levillaincff13742014-11-17 14:32:17 +00002162 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002163 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002164 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002165 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002166 break;
2167
2168 default:
2169 LOG(FATAL) << "Unexpected type conversion from " << input_type
2170 << " to " << result_type;
2171 };
2172 break;
2173
Roland Levillaindff1f282014-11-05 14:15:05 +00002174 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002175 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002176 case Primitive::kPrimBoolean:
2177 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002178 case Primitive::kPrimByte:
2179 case Primitive::kPrimShort:
2180 case Primitive::kPrimInt:
2181 case Primitive::kPrimChar:
2182 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002183 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002184 locations->SetOut(Location::RequiresFpuRegister());
2185 break;
2186
2187 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002188 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002189 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002190 locations->SetOut(Location::RequiresFpuRegister());
2191 break;
2192
Roland Levillaincff13742014-11-17 14:32:17 +00002193 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002194 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002195 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002196 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002197 break;
2198
2199 default:
2200 LOG(FATAL) << "Unexpected type conversion from " << input_type
2201 << " to " << result_type;
2202 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002203 break;
2204
2205 default:
2206 LOG(FATAL) << "Unexpected type conversion from " << input_type
2207 << " to " << result_type;
2208 }
2209}
2210
2211void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2212 LocationSummary* locations = conversion->GetLocations();
2213 Location out = locations->Out();
2214 Location in = locations->InAt(0);
2215 Primitive::Type result_type = conversion->GetResultType();
2216 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002217 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002218 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002219 case Primitive::kPrimByte:
2220 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002221 case Primitive::kPrimBoolean:
2222 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002223 case Primitive::kPrimShort:
2224 case Primitive::kPrimInt:
2225 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002226 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002227 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002228 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002229 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002230 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002231 Address(CpuRegister(RSP), in.GetStackIndex()));
2232 } else {
2233 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002235 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2236 }
2237 break;
2238
2239 default:
2240 LOG(FATAL) << "Unexpected type conversion from " << input_type
2241 << " to " << result_type;
2242 }
2243 break;
2244
Roland Levillain01a8d712014-11-14 16:27:39 +00002245 case Primitive::kPrimShort:
2246 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002247 case Primitive::kPrimBoolean:
2248 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002249 case Primitive::kPrimByte:
2250 case Primitive::kPrimInt:
2251 case Primitive::kPrimChar:
2252 // Processing a Dex `int-to-short' instruction.
2253 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002254 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002255 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002256 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002257 Address(CpuRegister(RSP), in.GetStackIndex()));
2258 } else {
2259 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002260 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002261 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2262 }
2263 break;
2264
2265 default:
2266 LOG(FATAL) << "Unexpected type conversion from " << input_type
2267 << " to " << result_type;
2268 }
2269 break;
2270
Roland Levillain946e1432014-11-11 17:35:19 +00002271 case Primitive::kPrimInt:
2272 switch (input_type) {
2273 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002274 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002275 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002276 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002277 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002278 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002279 Address(CpuRegister(RSP), in.GetStackIndex()));
2280 } else {
2281 DCHECK(in.IsConstant());
2282 DCHECK(in.GetConstant()->IsLongConstant());
2283 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002284 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002285 }
2286 break;
2287
Roland Levillain3f8f9362014-12-02 17:45:01 +00002288 case Primitive::kPrimFloat: {
2289 // Processing a Dex `float-to-int' instruction.
2290 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2291 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002292 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002293
2294 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002295 // if input >= (float)INT_MAX goto done
2296 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002297 __ j(kAboveEqual, &done);
2298 // if input == NaN goto nan
2299 __ j(kUnordered, &nan);
2300 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002301 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002302 __ jmp(&done);
2303 __ Bind(&nan);
2304 // output = 0
2305 __ xorl(output, output);
2306 __ Bind(&done);
2307 break;
2308 }
2309
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002310 case Primitive::kPrimDouble: {
2311 // Processing a Dex `double-to-int' instruction.
2312 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2313 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002314 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002315
2316 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002317 // if input >= (double)INT_MAX goto done
2318 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002319 __ j(kAboveEqual, &done);
2320 // if input == NaN goto nan
2321 __ j(kUnordered, &nan);
2322 // output = double-to-int-truncate(input)
2323 __ cvttsd2si(output, input);
2324 __ jmp(&done);
2325 __ Bind(&nan);
2326 // output = 0
2327 __ xorl(output, output);
2328 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002329 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002330 }
Roland Levillain946e1432014-11-11 17:35:19 +00002331
2332 default:
2333 LOG(FATAL) << "Unexpected type conversion from " << input_type
2334 << " to " << result_type;
2335 }
2336 break;
2337
Roland Levillaindff1f282014-11-05 14:15:05 +00002338 case Primitive::kPrimLong:
2339 switch (input_type) {
2340 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002341 case Primitive::kPrimBoolean:
2342 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002343 case Primitive::kPrimByte:
2344 case Primitive::kPrimShort:
2345 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002346 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002347 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002348 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002349 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002350 break;
2351
Roland Levillain624279f2014-12-04 11:54:28 +00002352 case Primitive::kPrimFloat: {
2353 // Processing a Dex `float-to-long' instruction.
2354 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2355 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002356 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002357
Mark Mendell92e83bf2015-05-07 11:25:03 -04002358 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002359 // if input >= (float)LONG_MAX goto done
2360 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002361 __ j(kAboveEqual, &done);
2362 // if input == NaN goto nan
2363 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002364 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002365 __ cvttss2si(output, input, true);
2366 __ jmp(&done);
2367 __ Bind(&nan);
2368 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002369 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002370 __ Bind(&done);
2371 break;
2372 }
2373
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002374 case Primitive::kPrimDouble: {
2375 // Processing a Dex `double-to-long' instruction.
2376 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2377 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002378 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002379
Mark Mendell92e83bf2015-05-07 11:25:03 -04002380 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002381 // if input >= (double)LONG_MAX goto done
2382 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002383 __ j(kAboveEqual, &done);
2384 // if input == NaN goto nan
2385 __ j(kUnordered, &nan);
2386 // output = double-to-long-truncate(input)
2387 __ cvttsd2si(output, input, true);
2388 __ jmp(&done);
2389 __ Bind(&nan);
2390 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002391 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002392 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002393 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002394 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002395
2396 default:
2397 LOG(FATAL) << "Unexpected type conversion from " << input_type
2398 << " to " << result_type;
2399 }
2400 break;
2401
Roland Levillain981e4542014-11-14 11:47:14 +00002402 case Primitive::kPrimChar:
2403 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002404 case Primitive::kPrimBoolean:
2405 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002406 case Primitive::kPrimByte:
2407 case Primitive::kPrimShort:
2408 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002409 // Processing a Dex `int-to-char' instruction.
2410 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002411 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002412 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002413 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002414 Address(CpuRegister(RSP), in.GetStackIndex()));
2415 } else {
2416 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002417 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002418 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2419 }
2420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected type conversion from " << input_type
2424 << " to " << result_type;
2425 }
2426 break;
2427
Roland Levillaindff1f282014-11-05 14:15:05 +00002428 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002429 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002430 case Primitive::kPrimBoolean:
2431 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002432 case Primitive::kPrimByte:
2433 case Primitive::kPrimShort:
2434 case Primitive::kPrimInt:
2435 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002436 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002437 if (in.IsRegister()) {
2438 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2439 } else if (in.IsConstant()) {
2440 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2441 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2442 if (v == 0) {
2443 __ xorps(dest, dest);
2444 } else {
2445 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2446 }
2447 } else {
2448 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2449 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2450 }
Roland Levillaincff13742014-11-17 14:32:17 +00002451 break;
2452
2453 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002454 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002455 if (in.IsRegister()) {
2456 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2457 } else if (in.IsConstant()) {
2458 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2459 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2460 if (v == 0) {
2461 __ xorps(dest, dest);
2462 } else {
2463 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2464 }
2465 } else {
2466 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2467 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2468 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002469 break;
2470
Roland Levillaincff13742014-11-17 14:32:17 +00002471 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002472 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002473 if (in.IsFpuRegister()) {
2474 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2475 } else if (in.IsConstant()) {
2476 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2477 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2478 if (bit_cast<int64_t, double>(v) == 0) {
2479 __ xorps(dest, dest);
2480 } else {
2481 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2482 }
2483 } else {
2484 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2485 Address(CpuRegister(RSP), in.GetStackIndex()));
2486 }
Roland Levillaincff13742014-11-17 14:32:17 +00002487 break;
2488
2489 default:
2490 LOG(FATAL) << "Unexpected type conversion from " << input_type
2491 << " to " << result_type;
2492 };
2493 break;
2494
Roland Levillaindff1f282014-11-05 14:15:05 +00002495 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002496 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002497 case Primitive::kPrimBoolean:
2498 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002499 case Primitive::kPrimByte:
2500 case Primitive::kPrimShort:
2501 case Primitive::kPrimInt:
2502 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002503 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002504 if (in.IsRegister()) {
2505 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2506 } else if (in.IsConstant()) {
2507 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2508 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2509 if (v == 0) {
2510 __ xorpd(dest, dest);
2511 } else {
2512 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2513 }
2514 } else {
2515 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2516 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2517 }
Roland Levillaincff13742014-11-17 14:32:17 +00002518 break;
2519
2520 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002521 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002522 if (in.IsRegister()) {
2523 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2524 } else if (in.IsConstant()) {
2525 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2526 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2527 if (v == 0) {
2528 __ xorpd(dest, dest);
2529 } else {
2530 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2531 }
2532 } else {
2533 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2534 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2535 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002536 break;
2537
Roland Levillaincff13742014-11-17 14:32:17 +00002538 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002539 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002540 if (in.IsFpuRegister()) {
2541 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2542 } else if (in.IsConstant()) {
2543 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2544 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2545 if (bit_cast<int32_t, float>(v) == 0) {
2546 __ xorpd(dest, dest);
2547 } else {
2548 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2549 }
2550 } else {
2551 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2552 Address(CpuRegister(RSP), in.GetStackIndex()));
2553 }
Roland Levillaincff13742014-11-17 14:32:17 +00002554 break;
2555
2556 default:
2557 LOG(FATAL) << "Unexpected type conversion from " << input_type
2558 << " to " << result_type;
2559 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002560 break;
2561
2562 default:
2563 LOG(FATAL) << "Unexpected type conversion from " << input_type
2564 << " to " << result_type;
2565 }
2566}
2567
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002568void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002569 LocationSummary* locations =
2570 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002571 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002572 case Primitive::kPrimInt: {
2573 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002574 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2575 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002576 break;
2577 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002578
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002579 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002580 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002581 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002582 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002584 break;
2585 }
2586
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002587 case Primitive::kPrimDouble:
2588 case Primitive::kPrimFloat: {
2589 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002590 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002591 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002592 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002593 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002594
2595 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002596 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002597 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002598}
2599
2600void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2601 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002602 Location first = locations->InAt(0);
2603 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002604 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002605
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002606 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002607 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002608 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002609 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2610 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002611 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2612 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002613 } else {
2614 __ leal(out.AsRegister<CpuRegister>(), Address(
2615 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2616 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002617 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002618 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2619 __ addl(out.AsRegister<CpuRegister>(),
2620 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2621 } else {
2622 __ leal(out.AsRegister<CpuRegister>(), Address(
2623 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2624 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002625 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002626 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002627 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002628 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002629 break;
2630 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002631
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002632 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002633 if (second.IsRegister()) {
2634 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2635 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002636 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2637 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002638 } else {
2639 __ leaq(out.AsRegister<CpuRegister>(), Address(
2640 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2641 }
2642 } else {
2643 DCHECK(second.IsConstant());
2644 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2645 int32_t int32_value = Low32Bits(value);
2646 DCHECK_EQ(int32_value, value);
2647 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2648 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2649 } else {
2650 __ leaq(out.AsRegister<CpuRegister>(), Address(
2651 first.AsRegister<CpuRegister>(), int32_value));
2652 }
2653 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002654 break;
2655 }
2656
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002657 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002658 if (second.IsFpuRegister()) {
2659 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2660 } else if (second.IsConstant()) {
2661 __ addss(first.AsFpuRegister<XmmRegister>(),
2662 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2663 } else {
2664 DCHECK(second.IsStackSlot());
2665 __ addss(first.AsFpuRegister<XmmRegister>(),
2666 Address(CpuRegister(RSP), second.GetStackIndex()));
2667 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002668 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002669 }
2670
2671 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002672 if (second.IsFpuRegister()) {
2673 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2674 } else if (second.IsConstant()) {
2675 __ addsd(first.AsFpuRegister<XmmRegister>(),
2676 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2677 } else {
2678 DCHECK(second.IsDoubleStackSlot());
2679 __ addsd(first.AsFpuRegister<XmmRegister>(),
2680 Address(CpuRegister(RSP), second.GetStackIndex()));
2681 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002682 break;
2683 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002684
2685 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002686 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002687 }
2688}
2689
2690void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002691 LocationSummary* locations =
2692 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002693 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002694 case Primitive::kPrimInt: {
2695 locations->SetInAt(0, Location::RequiresRegister());
2696 locations->SetInAt(1, Location::Any());
2697 locations->SetOut(Location::SameAsFirstInput());
2698 break;
2699 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002700 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002701 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002702 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002703 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002704 break;
2705 }
Calin Juravle11351682014-10-23 15:38:15 +01002706 case Primitive::kPrimFloat:
2707 case Primitive::kPrimDouble: {
2708 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002709 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002710 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002711 break;
Calin Juravle11351682014-10-23 15:38:15 +01002712 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002713 default:
Calin Juravle11351682014-10-23 15:38:15 +01002714 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002715 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002716}
2717
2718void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2719 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002720 Location first = locations->InAt(0);
2721 Location second = locations->InAt(1);
2722 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002723 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002724 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002725 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002726 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002727 } else if (second.IsConstant()) {
2728 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002729 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002730 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002731 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002732 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002733 break;
2734 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002735 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002736 if (second.IsConstant()) {
2737 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2738 DCHECK(IsInt<32>(value));
2739 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2740 } else {
2741 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2742 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002743 break;
2744 }
2745
Calin Juravle11351682014-10-23 15:38:15 +01002746 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002747 if (second.IsFpuRegister()) {
2748 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2749 } else if (second.IsConstant()) {
2750 __ subss(first.AsFpuRegister<XmmRegister>(),
2751 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2752 } else {
2753 DCHECK(second.IsStackSlot());
2754 __ subss(first.AsFpuRegister<XmmRegister>(),
2755 Address(CpuRegister(RSP), second.GetStackIndex()));
2756 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002757 break;
Calin Juravle11351682014-10-23 15:38:15 +01002758 }
2759
2760 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002761 if (second.IsFpuRegister()) {
2762 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2763 } else if (second.IsConstant()) {
2764 __ subsd(first.AsFpuRegister<XmmRegister>(),
2765 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2766 } else {
2767 DCHECK(second.IsDoubleStackSlot());
2768 __ subsd(first.AsFpuRegister<XmmRegister>(),
2769 Address(CpuRegister(RSP), second.GetStackIndex()));
2770 }
Calin Juravle11351682014-10-23 15:38:15 +01002771 break;
2772 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002773
2774 default:
Calin Juravle11351682014-10-23 15:38:15 +01002775 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002776 }
2777}
2778
Calin Juravle34bacdf2014-10-07 20:23:36 +01002779void LocationsBuilderX86_64::VisitMul(HMul* mul) {
2780 LocationSummary* locations =
2781 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2782 switch (mul->GetResultType()) {
2783 case Primitive::kPrimInt: {
2784 locations->SetInAt(0, Location::RequiresRegister());
2785 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002786 if (mul->InputAt(1)->IsIntConstant()) {
2787 // Can use 3 operand multiply.
2788 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2789 } else {
2790 locations->SetOut(Location::SameAsFirstInput());
2791 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002792 break;
2793 }
2794 case Primitive::kPrimLong: {
2795 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002796 locations->SetInAt(1, Location::Any());
2797 if (mul->InputAt(1)->IsLongConstant() &&
2798 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002799 // Can use 3 operand multiply.
2800 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2801 } else {
2802 locations->SetOut(Location::SameAsFirstInput());
2803 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002804 break;
2805 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002806 case Primitive::kPrimFloat:
2807 case Primitive::kPrimDouble: {
2808 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002809 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002810 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002811 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002812 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002813
2814 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002815 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002816 }
2817}
2818
2819void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
2820 LocationSummary* locations = mul->GetLocations();
2821 Location first = locations->InAt(0);
2822 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002823 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002824 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002825 case Primitive::kPrimInt:
2826 // The constant may have ended up in a register, so test explicitly to avoid
2827 // problems where the output may not be the same as the first operand.
2828 if (mul->InputAt(1)->IsIntConstant()) {
2829 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2830 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
2831 } else if (second.IsRegister()) {
2832 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002833 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002834 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002835 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002836 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00002837 __ imull(first.AsRegister<CpuRegister>(),
2838 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002839 }
2840 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002841 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002842 // The constant may have ended up in a register, so test explicitly to avoid
2843 // problems where the output may not be the same as the first operand.
2844 if (mul->InputAt(1)->IsLongConstant()) {
2845 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
2846 if (IsInt<32>(value)) {
2847 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
2848 Immediate(static_cast<int32_t>(value)));
2849 } else {
2850 // Have to use the constant area.
2851 DCHECK(first.Equals(out));
2852 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
2853 }
2854 } else if (second.IsRegister()) {
2855 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002856 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002857 } else {
2858 DCHECK(second.IsDoubleStackSlot());
2859 DCHECK(first.Equals(out));
2860 __ imulq(first.AsRegister<CpuRegister>(),
2861 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002862 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002863 break;
2864 }
2865
Calin Juravleb5bfa962014-10-21 18:02:24 +01002866 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002867 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002868 if (second.IsFpuRegister()) {
2869 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2870 } else if (second.IsConstant()) {
2871 __ mulss(first.AsFpuRegister<XmmRegister>(),
2872 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2873 } else {
2874 DCHECK(second.IsStackSlot());
2875 __ mulss(first.AsFpuRegister<XmmRegister>(),
2876 Address(CpuRegister(RSP), second.GetStackIndex()));
2877 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002878 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002879 }
2880
2881 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002882 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002883 if (second.IsFpuRegister()) {
2884 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2885 } else if (second.IsConstant()) {
2886 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2887 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2888 } else {
2889 DCHECK(second.IsDoubleStackSlot());
2890 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2891 Address(CpuRegister(RSP), second.GetStackIndex()));
2892 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002893 break;
2894 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002895
2896 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002897 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002898 }
2899}
2900
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002901void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
2902 uint32_t stack_adjustment, bool is_float) {
2903 if (source.IsStackSlot()) {
2904 DCHECK(is_float);
2905 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2906 } else if (source.IsDoubleStackSlot()) {
2907 DCHECK(!is_float);
2908 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
2909 } else {
2910 // Write the value to the temporary location on the stack and load to FP stack.
2911 if (is_float) {
2912 Location stack_temp = Location::StackSlot(temp_offset);
2913 codegen_->Move(stack_temp, source);
2914 __ flds(Address(CpuRegister(RSP), temp_offset));
2915 } else {
2916 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2917 codegen_->Move(stack_temp, source);
2918 __ fldl(Address(CpuRegister(RSP), temp_offset));
2919 }
2920 }
2921}
2922
2923void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
2924 Primitive::Type type = rem->GetResultType();
2925 bool is_float = type == Primitive::kPrimFloat;
2926 size_t elem_size = Primitive::ComponentSize(type);
2927 LocationSummary* locations = rem->GetLocations();
2928 Location first = locations->InAt(0);
2929 Location second = locations->InAt(1);
2930 Location out = locations->Out();
2931
2932 // Create stack space for 2 elements.
2933 // TODO: enhance register allocator to ask for stack temporaries.
2934 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
2935
2936 // Load the values to the FP stack in reverse order, using temporaries if needed.
2937 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2938 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2939
2940 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002941 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002942 __ Bind(&retry);
2943 __ fprem();
2944
2945 // Move FP status to AX.
2946 __ fstsw();
2947
2948 // And see if the argument reduction is complete. This is signaled by the
2949 // C2 FPU flag bit set to 0.
2950 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
2951 __ j(kNotEqual, &retry);
2952
2953 // We have settled on the final value. Retrieve it into an XMM register.
2954 // Store FP top of stack to real stack.
2955 if (is_float) {
2956 __ fsts(Address(CpuRegister(RSP), 0));
2957 } else {
2958 __ fstl(Address(CpuRegister(RSP), 0));
2959 }
2960
2961 // Pop the 2 items from the FP stack.
2962 __ fucompp();
2963
2964 // Load the value from the stack into an XMM register.
2965 DCHECK(out.IsFpuRegister()) << out;
2966 if (is_float) {
2967 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2968 } else {
2969 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
2970 }
2971
2972 // And remove the temporary stack space we allocated.
2973 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
2974}
2975
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002976void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2977 DCHECK(instruction->IsDiv() || instruction->IsRem());
2978
2979 LocationSummary* locations = instruction->GetLocations();
2980 Location second = locations->InAt(1);
2981 DCHECK(second.IsConstant());
2982
2983 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
2984 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002985 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002986
2987 DCHECK(imm == 1 || imm == -1);
2988
2989 switch (instruction->GetResultType()) {
2990 case Primitive::kPrimInt: {
2991 if (instruction->IsRem()) {
2992 __ xorl(output_register, output_register);
2993 } else {
2994 __ movl(output_register, input_register);
2995 if (imm == -1) {
2996 __ negl(output_register);
2997 }
2998 }
2999 break;
3000 }
3001
3002 case Primitive::kPrimLong: {
3003 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003004 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003005 } else {
3006 __ movq(output_register, input_register);
3007 if (imm == -1) {
3008 __ negq(output_register);
3009 }
3010 }
3011 break;
3012 }
3013
3014 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003015 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003016 }
3017}
3018
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003019void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003020 LocationSummary* locations = instruction->GetLocations();
3021 Location second = locations->InAt(1);
3022
3023 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3024 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3025
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003026 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003027
3028 DCHECK(IsPowerOfTwo(std::abs(imm)));
3029
3030 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3031
3032 if (instruction->GetResultType() == Primitive::kPrimInt) {
3033 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
3034 __ testl(numerator, numerator);
3035 __ cmov(kGreaterEqual, tmp, numerator);
3036 int shift = CTZ(imm);
3037 __ sarl(tmp, Immediate(shift));
3038
3039 if (imm < 0) {
3040 __ negl(tmp);
3041 }
3042
3043 __ movl(output_register, tmp);
3044 } else {
3045 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3046 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3047
Mark Mendell92e83bf2015-05-07 11:25:03 -04003048 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003049 __ addq(rdx, numerator);
3050 __ testq(numerator, numerator);
3051 __ cmov(kGreaterEqual, rdx, numerator);
3052 int shift = CTZ(imm);
3053 __ sarq(rdx, Immediate(shift));
3054
3055 if (imm < 0) {
3056 __ negq(rdx);
3057 }
3058
3059 __ movq(output_register, rdx);
3060 }
3061}
3062
3063void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3064 DCHECK(instruction->IsDiv() || instruction->IsRem());
3065
3066 LocationSummary* locations = instruction->GetLocations();
3067 Location second = locations->InAt(1);
3068
3069 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3070 : locations->GetTemp(0).AsRegister<CpuRegister>();
3071 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3072 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3073 : locations->Out().AsRegister<CpuRegister>();
3074 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3075
3076 DCHECK_EQ(RAX, eax.AsRegister());
3077 DCHECK_EQ(RDX, edx.AsRegister());
3078 if (instruction->IsDiv()) {
3079 DCHECK_EQ(RAX, out.AsRegister());
3080 } else {
3081 DCHECK_EQ(RDX, out.AsRegister());
3082 }
3083
3084 int64_t magic;
3085 int shift;
3086
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003087 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003088 if (instruction->GetResultType() == Primitive::kPrimInt) {
3089 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3090
3091 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3092
3093 __ movl(numerator, eax);
3094
Mark Mendell0c9497d2015-08-21 09:30:05 -04003095 NearLabel no_div;
3096 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003097 __ testl(eax, eax);
3098 __ j(kNotEqual, &no_div);
3099
3100 __ xorl(out, out);
3101 __ jmp(&end);
3102
3103 __ Bind(&no_div);
3104
3105 __ movl(eax, Immediate(magic));
3106 __ imull(numerator);
3107
3108 if (imm > 0 && magic < 0) {
3109 __ addl(edx, numerator);
3110 } else if (imm < 0 && magic > 0) {
3111 __ subl(edx, numerator);
3112 }
3113
3114 if (shift != 0) {
3115 __ sarl(edx, Immediate(shift));
3116 }
3117
3118 __ movl(eax, edx);
3119 __ shrl(edx, Immediate(31));
3120 __ addl(edx, eax);
3121
3122 if (instruction->IsRem()) {
3123 __ movl(eax, numerator);
3124 __ imull(edx, Immediate(imm));
3125 __ subl(eax, edx);
3126 __ movl(edx, eax);
3127 } else {
3128 __ movl(eax, edx);
3129 }
3130 __ Bind(&end);
3131 } else {
3132 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3133
3134 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3135
3136 CpuRegister rax = eax;
3137 CpuRegister rdx = edx;
3138
3139 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3140
3141 // Save the numerator.
3142 __ movq(numerator, rax);
3143
3144 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003145 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003146
3147 // RDX:RAX = magic * numerator
3148 __ imulq(numerator);
3149
3150 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003151 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003152 __ addq(rdx, numerator);
3153 } else if (imm < 0 && magic > 0) {
3154 // RDX -= numerator
3155 __ subq(rdx, numerator);
3156 }
3157
3158 // Shift if needed.
3159 if (shift != 0) {
3160 __ sarq(rdx, Immediate(shift));
3161 }
3162
3163 // RDX += 1 if RDX < 0
3164 __ movq(rax, rdx);
3165 __ shrq(rdx, Immediate(63));
3166 __ addq(rdx, rax);
3167
3168 if (instruction->IsRem()) {
3169 __ movq(rax, numerator);
3170
3171 if (IsInt<32>(imm)) {
3172 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3173 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003174 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003175 }
3176
3177 __ subq(rax, rdx);
3178 __ movq(rdx, rax);
3179 } else {
3180 __ movq(rax, rdx);
3181 }
3182 }
3183}
3184
Calin Juravlebacfec32014-11-14 15:54:36 +00003185void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3186 DCHECK(instruction->IsDiv() || instruction->IsRem());
3187 Primitive::Type type = instruction->GetResultType();
3188 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3189
3190 bool is_div = instruction->IsDiv();
3191 LocationSummary* locations = instruction->GetLocations();
3192
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003193 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3194 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003195
Roland Levillain271ab9c2014-11-27 15:23:57 +00003196 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003197 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003198
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003199 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003200 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003201
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003202 if (imm == 0) {
3203 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3204 } else if (imm == 1 || imm == -1) {
3205 DivRemOneOrMinusOne(instruction);
3206 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003207 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003208 } else {
3209 DCHECK(imm <= -2 || imm >= 2);
3210 GenerateDivRemWithAnyConstant(instruction);
3211 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003212 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003213 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003214 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3215 out.AsRegister(), type, is_div);
3216 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003217
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003218 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3219 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3220 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3221 // so it's safe to just use negl instead of more complex comparisons.
3222 if (type == Primitive::kPrimInt) {
3223 __ cmpl(second_reg, Immediate(-1));
3224 __ j(kEqual, slow_path->GetEntryLabel());
3225 // edx:eax <- sign-extended of eax
3226 __ cdq();
3227 // eax = quotient, edx = remainder
3228 __ idivl(second_reg);
3229 } else {
3230 __ cmpq(second_reg, Immediate(-1));
3231 __ j(kEqual, slow_path->GetEntryLabel());
3232 // rdx:rax <- sign-extended of rax
3233 __ cqo();
3234 // rax = quotient, rdx = remainder
3235 __ idivq(second_reg);
3236 }
3237 __ Bind(slow_path->GetExitLabel());
3238 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003239}
3240
Calin Juravle7c4954d2014-10-28 16:57:40 +00003241void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3242 LocationSummary* locations =
3243 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3244 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003245 case Primitive::kPrimInt:
3246 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003247 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003248 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003249 locations->SetOut(Location::SameAsFirstInput());
3250 // Intel uses edx:eax as the dividend.
3251 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003252 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3253 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3254 // output and request another temp.
3255 if (div->InputAt(1)->IsConstant()) {
3256 locations->AddTemp(Location::RequiresRegister());
3257 }
Calin Juravled0d48522014-11-04 16:40:20 +00003258 break;
3259 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003260
Calin Juravle7c4954d2014-10-28 16:57:40 +00003261 case Primitive::kPrimFloat:
3262 case Primitive::kPrimDouble: {
3263 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003264 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003265 locations->SetOut(Location::SameAsFirstInput());
3266 break;
3267 }
3268
3269 default:
3270 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3271 }
3272}
3273
3274void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3275 LocationSummary* locations = div->GetLocations();
3276 Location first = locations->InAt(0);
3277 Location second = locations->InAt(1);
3278 DCHECK(first.Equals(locations->Out()));
3279
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003280 Primitive::Type type = div->GetResultType();
3281 switch (type) {
3282 case Primitive::kPrimInt:
3283 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003284 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003285 break;
3286 }
3287
Calin Juravle7c4954d2014-10-28 16:57:40 +00003288 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003289 if (second.IsFpuRegister()) {
3290 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3291 } else if (second.IsConstant()) {
3292 __ divss(first.AsFpuRegister<XmmRegister>(),
3293 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3294 } else {
3295 DCHECK(second.IsStackSlot());
3296 __ divss(first.AsFpuRegister<XmmRegister>(),
3297 Address(CpuRegister(RSP), second.GetStackIndex()));
3298 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003299 break;
3300 }
3301
3302 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003303 if (second.IsFpuRegister()) {
3304 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3305 } else if (second.IsConstant()) {
3306 __ divsd(first.AsFpuRegister<XmmRegister>(),
3307 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3308 } else {
3309 DCHECK(second.IsDoubleStackSlot());
3310 __ divsd(first.AsFpuRegister<XmmRegister>(),
3311 Address(CpuRegister(RSP), second.GetStackIndex()));
3312 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003313 break;
3314 }
3315
3316 default:
3317 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3318 }
3319}
3320
Calin Juravlebacfec32014-11-14 15:54:36 +00003321void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003322 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003323 LocationSummary* locations =
3324 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003325
3326 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003327 case Primitive::kPrimInt:
3328 case Primitive::kPrimLong: {
3329 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003330 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003331 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3332 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003333 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3334 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3335 // output and request another temp.
3336 if (rem->InputAt(1)->IsConstant()) {
3337 locations->AddTemp(Location::RequiresRegister());
3338 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003339 break;
3340 }
3341
3342 case Primitive::kPrimFloat:
3343 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003344 locations->SetInAt(0, Location::Any());
3345 locations->SetInAt(1, Location::Any());
3346 locations->SetOut(Location::RequiresFpuRegister());
3347 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003348 break;
3349 }
3350
3351 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003352 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003353 }
3354}
3355
3356void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3357 Primitive::Type type = rem->GetResultType();
3358 switch (type) {
3359 case Primitive::kPrimInt:
3360 case Primitive::kPrimLong: {
3361 GenerateDivRemIntegral(rem);
3362 break;
3363 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003364 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003365 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003366 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003367 break;
3368 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003369 default:
3370 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3371 }
3372}
3373
Calin Juravled0d48522014-11-04 16:40:20 +00003374void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003375 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3376 ? LocationSummary::kCallOnSlowPath
3377 : LocationSummary::kNoCall;
3378 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003379 locations->SetInAt(0, Location::Any());
3380 if (instruction->HasUses()) {
3381 locations->SetOut(Location::SameAsFirstInput());
3382 }
3383}
3384
3385void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003386 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003387 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3388 codegen_->AddSlowPath(slow_path);
3389
3390 LocationSummary* locations = instruction->GetLocations();
3391 Location value = locations->InAt(0);
3392
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003393 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003394 case Primitive::kPrimByte:
3395 case Primitive::kPrimChar:
3396 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003397 case Primitive::kPrimInt: {
3398 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003399 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003400 __ j(kEqual, slow_path->GetEntryLabel());
3401 } else if (value.IsStackSlot()) {
3402 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3403 __ j(kEqual, slow_path->GetEntryLabel());
3404 } else {
3405 DCHECK(value.IsConstant()) << value;
3406 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3407 __ jmp(slow_path->GetEntryLabel());
3408 }
3409 }
3410 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003411 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003412 case Primitive::kPrimLong: {
3413 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003414 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003415 __ j(kEqual, slow_path->GetEntryLabel());
3416 } else if (value.IsDoubleStackSlot()) {
3417 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3418 __ j(kEqual, slow_path->GetEntryLabel());
3419 } else {
3420 DCHECK(value.IsConstant()) << value;
3421 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3422 __ jmp(slow_path->GetEntryLabel());
3423 }
3424 }
3425 break;
3426 }
3427 default:
3428 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003429 }
Calin Juravled0d48522014-11-04 16:40:20 +00003430}
3431
Calin Juravle9aec02f2014-11-18 23:06:35 +00003432void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3433 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3434
3435 LocationSummary* locations =
3436 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3437
3438 switch (op->GetResultType()) {
3439 case Primitive::kPrimInt:
3440 case Primitive::kPrimLong: {
3441 locations->SetInAt(0, Location::RequiresRegister());
3442 // The shift count needs to be in CL.
3443 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3444 locations->SetOut(Location::SameAsFirstInput());
3445 break;
3446 }
3447 default:
3448 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3449 }
3450}
3451
3452void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3453 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3454
3455 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003456 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003457 Location second = locations->InAt(1);
3458
3459 switch (op->GetResultType()) {
3460 case Primitive::kPrimInt: {
3461 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003462 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003463 if (op->IsShl()) {
3464 __ shll(first_reg, second_reg);
3465 } else if (op->IsShr()) {
3466 __ sarl(first_reg, second_reg);
3467 } else {
3468 __ shrl(first_reg, second_reg);
3469 }
3470 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003471 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003472 if (op->IsShl()) {
3473 __ shll(first_reg, imm);
3474 } else if (op->IsShr()) {
3475 __ sarl(first_reg, imm);
3476 } else {
3477 __ shrl(first_reg, imm);
3478 }
3479 }
3480 break;
3481 }
3482 case Primitive::kPrimLong: {
3483 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003484 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003485 if (op->IsShl()) {
3486 __ shlq(first_reg, second_reg);
3487 } else if (op->IsShr()) {
3488 __ sarq(first_reg, second_reg);
3489 } else {
3490 __ shrq(first_reg, second_reg);
3491 }
3492 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003493 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003494 if (op->IsShl()) {
3495 __ shlq(first_reg, imm);
3496 } else if (op->IsShr()) {
3497 __ sarq(first_reg, imm);
3498 } else {
3499 __ shrq(first_reg, imm);
3500 }
3501 }
3502 break;
3503 }
3504 default:
3505 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3506 }
3507}
3508
3509void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3510 HandleShift(shl);
3511}
3512
3513void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3514 HandleShift(shl);
3515}
3516
3517void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3518 HandleShift(shr);
3519}
3520
3521void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3522 HandleShift(shr);
3523}
3524
3525void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3526 HandleShift(ushr);
3527}
3528
3529void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3530 HandleShift(ushr);
3531}
3532
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003533void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003534 LocationSummary* locations =
3535 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003536 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003537 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003538 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003539 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003540}
3541
3542void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3543 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003544 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3545 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003546 // Note: if heap poisoning is enabled, the entry point takes cares
3547 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003548
Calin Juravle175dc732015-08-25 15:42:32 +01003549 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3550 instruction,
3551 instruction->GetDexPc(),
3552 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003553
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003554 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003555}
3556
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003557void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3558 LocationSummary* locations =
3559 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3560 InvokeRuntimeCallingConvention calling_convention;
3561 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003562 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003563 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003564 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003565}
3566
3567void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3568 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003569 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3570 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003571
Roland Levillain4d027112015-07-01 15:41:14 +01003572 // Note: if heap poisoning is enabled, the entry point takes cares
3573 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003574 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3575 instruction,
3576 instruction->GetDexPc(),
3577 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003578
3579 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003580}
3581
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003582void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003583 LocationSummary* locations =
3584 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003585 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3586 if (location.IsStackSlot()) {
3587 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3588 } else if (location.IsDoubleStackSlot()) {
3589 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3590 }
3591 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003592}
3593
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003594void InstructionCodeGeneratorX86_64::VisitParameterValue(
3595 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003596 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003597}
3598
3599void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3600 LocationSummary* locations =
3601 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3602 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3603}
3604
3605void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3606 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3607 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003608}
3609
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003610void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003611 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003612 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003613 locations->SetInAt(0, Location::RequiresRegister());
3614 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003615}
3616
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003617void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3618 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003619 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3620 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003621 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003622 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003623 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003624 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003625 break;
3626
3627 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003628 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003629 break;
3630
3631 default:
3632 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3633 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003634}
3635
David Brazdil66d126e2015-04-03 16:02:44 +01003636void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3637 LocationSummary* locations =
3638 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3639 locations->SetInAt(0, Location::RequiresRegister());
3640 locations->SetOut(Location::SameAsFirstInput());
3641}
3642
3643void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003644 LocationSummary* locations = bool_not->GetLocations();
3645 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3646 locations->Out().AsRegister<CpuRegister>().AsRegister());
3647 Location out = locations->Out();
3648 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3649}
3650
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003651void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003652 LocationSummary* locations =
3653 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003654 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3655 locations->SetInAt(i, Location::Any());
3656 }
3657 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003658}
3659
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003660void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003661 LOG(FATAL) << "Unimplemented";
3662}
3663
Calin Juravle52c48962014-12-16 17:02:57 +00003664void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3665 /*
3666 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3667 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3668 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3669 */
3670 switch (kind) {
3671 case MemBarrierKind::kAnyAny: {
3672 __ mfence();
3673 break;
3674 }
3675 case MemBarrierKind::kAnyStore:
3676 case MemBarrierKind::kLoadAny:
3677 case MemBarrierKind::kStoreStore: {
3678 // nop
3679 break;
3680 }
3681 default:
3682 LOG(FATAL) << "Unexpected memory barier " << kind;
3683 }
3684}
3685
3686void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3687 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3688
Nicolas Geoffray39468442014-09-02 15:17:15 +01003689 LocationSummary* locations =
3690 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003691 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003692 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3693 locations->SetOut(Location::RequiresFpuRegister());
3694 } else {
3695 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3696 }
Calin Juravle52c48962014-12-16 17:02:57 +00003697}
3698
3699void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3700 const FieldInfo& field_info) {
3701 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3702
3703 LocationSummary* locations = instruction->GetLocations();
3704 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3705 Location out = locations->Out();
3706 bool is_volatile = field_info.IsVolatile();
3707 Primitive::Type field_type = field_info.GetFieldType();
3708 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3709
3710 switch (field_type) {
3711 case Primitive::kPrimBoolean: {
3712 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3713 break;
3714 }
3715
3716 case Primitive::kPrimByte: {
3717 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3718 break;
3719 }
3720
3721 case Primitive::kPrimShort: {
3722 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3723 break;
3724 }
3725
3726 case Primitive::kPrimChar: {
3727 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3728 break;
3729 }
3730
3731 case Primitive::kPrimInt:
3732 case Primitive::kPrimNot: {
3733 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3734 break;
3735 }
3736
3737 case Primitive::kPrimLong: {
3738 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3739 break;
3740 }
3741
3742 case Primitive::kPrimFloat: {
3743 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3744 break;
3745 }
3746
3747 case Primitive::kPrimDouble: {
3748 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3749 break;
3750 }
3751
3752 case Primitive::kPrimVoid:
3753 LOG(FATAL) << "Unreachable type " << field_type;
3754 UNREACHABLE();
3755 }
3756
Calin Juravle77520bc2015-01-12 18:45:46 +00003757 codegen_->MaybeRecordImplicitNullCheck(instruction);
3758
Calin Juravle52c48962014-12-16 17:02:57 +00003759 if (is_volatile) {
3760 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3761 }
Roland Levillain4d027112015-07-01 15:41:14 +01003762
3763 if (field_type == Primitive::kPrimNot) {
3764 __ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
3765 }
Calin Juravle52c48962014-12-16 17:02:57 +00003766}
3767
3768void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
3769 const FieldInfo& field_info) {
3770 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3771
3772 LocationSummary* locations =
3773 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01003774 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04003775 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003776 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01003777 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003778
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003779 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003780 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04003781 if (is_volatile) {
3782 // In order to satisfy the semantics of volatile, this must be a single instruction store.
3783 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
3784 } else {
3785 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
3786 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003787 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04003788 if (is_volatile) {
3789 // In order to satisfy the semantics of volatile, this must be a single instruction store.
3790 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
3791 } else {
3792 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3793 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003794 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003795 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003796 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003797 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003798 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01003799 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3800 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003801 locations->AddTemp(Location::RequiresRegister());
3802 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003803}
3804
Calin Juravle52c48962014-12-16 17:02:57 +00003805void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003806 const FieldInfo& field_info,
3807 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003808 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3809
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003810 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003811 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
3812 Location value = locations->InAt(1);
3813 bool is_volatile = field_info.IsVolatile();
3814 Primitive::Type field_type = field_info.GetFieldType();
3815 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3816
3817 if (is_volatile) {
3818 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3819 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003820
Mark Mendellea5af682015-10-22 17:35:49 -04003821 bool maybe_record_implicit_null_check_done = false;
3822
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003823 switch (field_type) {
3824 case Primitive::kPrimBoolean:
3825 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04003826 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04003827 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04003828 __ movb(Address(base, offset), Immediate(v));
3829 } else {
3830 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
3831 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003832 break;
3833 }
3834
3835 case Primitive::kPrimShort:
3836 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04003837 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04003838 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04003839 __ movw(Address(base, offset), Immediate(v));
3840 } else {
3841 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
3842 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003843 break;
3844 }
3845
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003846 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003847 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04003848 if (value.IsConstant()) {
3849 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01003850 // `field_type == Primitive::kPrimNot` implies `v == 0`.
3851 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
3852 // Note: if heap poisoning is enabled, no need to poison
3853 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01003854 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003855 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01003856 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
3857 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3858 __ movl(temp, value.AsRegister<CpuRegister>());
3859 __ PoisonHeapReference(temp);
3860 __ movl(Address(base, offset), temp);
3861 } else {
3862 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
3863 }
Mark Mendell40741f32015-04-20 22:10:34 -04003864 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003865 break;
3866 }
3867
3868 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04003869 if (value.IsConstant()) {
3870 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04003871 codegen_->MoveInt64ToAddress(Address(base, offset),
3872 Address(base, offset + sizeof(int32_t)),
3873 v,
3874 instruction);
3875 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04003876 } else {
3877 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
3878 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003879 break;
3880 }
3881
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003882 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04003883 if (value.IsConstant()) {
3884 int32_t v =
3885 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
3886 __ movl(Address(base, offset), Immediate(v));
3887 } else {
3888 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3889 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003890 break;
3891 }
3892
3893 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04003894 if (value.IsConstant()) {
3895 int64_t v =
3896 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
3897 codegen_->MoveInt64ToAddress(Address(base, offset),
3898 Address(base, offset + sizeof(int32_t)),
3899 v,
3900 instruction);
3901 maybe_record_implicit_null_check_done = true;
3902 } else {
3903 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3904 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003905 break;
3906 }
3907
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003908 case Primitive::kPrimVoid:
3909 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003910 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003911 }
Calin Juravle52c48962014-12-16 17:02:57 +00003912
Mark Mendellea5af682015-10-22 17:35:49 -04003913 if (!maybe_record_implicit_null_check_done) {
3914 codegen_->MaybeRecordImplicitNullCheck(instruction);
3915 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003916
3917 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3918 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3919 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003920 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003921 }
3922
Calin Juravle52c48962014-12-16 17:02:57 +00003923 if (is_volatile) {
3924 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3925 }
3926}
3927
3928void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3929 HandleFieldSet(instruction, instruction->GetFieldInfo());
3930}
3931
3932void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003933 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003934}
3935
3936void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003937 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003938}
3939
3940void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00003941 HandleFieldGet(instruction, instruction->GetFieldInfo());
3942}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003943
Calin Juravle52c48962014-12-16 17:02:57 +00003944void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3945 HandleFieldGet(instruction);
3946}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003947
Calin Juravle52c48962014-12-16 17:02:57 +00003948void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3949 HandleFieldGet(instruction, instruction->GetFieldInfo());
3950}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003951
Calin Juravle52c48962014-12-16 17:02:57 +00003952void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3953 HandleFieldSet(instruction, instruction->GetFieldInfo());
3954}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003955
Calin Juravle52c48962014-12-16 17:02:57 +00003956void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003957 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003958}
3959
Calin Juravlee460d1d2015-09-29 04:52:17 +01003960void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
3961 HUnresolvedInstanceFieldGet* instruction) {
3962 FieldAccessCallingConventionX86_64 calling_convention;
3963 codegen_->CreateUnresolvedFieldLocationSummary(
3964 instruction, instruction->GetFieldType(), calling_convention);
3965}
3966
3967void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
3968 HUnresolvedInstanceFieldGet* 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::VisitUnresolvedInstanceFieldSet(
3978 HUnresolvedInstanceFieldSet* instruction) {
3979 FieldAccessCallingConventionX86_64 calling_convention;
3980 codegen_->CreateUnresolvedFieldLocationSummary(
3981 instruction, instruction->GetFieldType(), calling_convention);
3982}
3983
3984void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
3985 HUnresolvedInstanceFieldSet* 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::VisitUnresolvedStaticFieldGet(
3995 HUnresolvedStaticFieldGet* instruction) {
3996 FieldAccessCallingConventionX86_64 calling_convention;
3997 codegen_->CreateUnresolvedFieldLocationSummary(
3998 instruction, instruction->GetFieldType(), calling_convention);
3999}
4000
4001void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4002 HUnresolvedStaticFieldGet* instruction) {
4003 FieldAccessCallingConventionX86_64 calling_convention;
4004 codegen_->GenerateUnresolvedFieldAccess(instruction,
4005 instruction->GetFieldType(),
4006 instruction->GetFieldIndex(),
4007 instruction->GetDexPc(),
4008 calling_convention);
4009}
4010
4011void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4012 HUnresolvedStaticFieldSet* instruction) {
4013 FieldAccessCallingConventionX86_64 calling_convention;
4014 codegen_->CreateUnresolvedFieldLocationSummary(
4015 instruction, instruction->GetFieldType(), calling_convention);
4016}
4017
4018void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4019 HUnresolvedStaticFieldSet* instruction) {
4020 FieldAccessCallingConventionX86_64 calling_convention;
4021 codegen_->GenerateUnresolvedFieldAccess(instruction,
4022 instruction->GetFieldType(),
4023 instruction->GetFieldIndex(),
4024 instruction->GetDexPc(),
4025 calling_convention);
4026}
4027
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004028void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004029 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4030 ? LocationSummary::kCallOnSlowPath
4031 : LocationSummary::kNoCall;
4032 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4033 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004034 ? Location::RequiresRegister()
4035 : Location::Any();
4036 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004037 if (instruction->HasUses()) {
4038 locations->SetOut(Location::SameAsFirstInput());
4039 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004040}
4041
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004042void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004043 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4044 return;
4045 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004046 LocationSummary* locations = instruction->GetLocations();
4047 Location obj = locations->InAt(0);
4048
4049 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4050 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4051}
4052
4053void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004054 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004055 codegen_->AddSlowPath(slow_path);
4056
4057 LocationSummary* locations = instruction->GetLocations();
4058 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004059
4060 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004061 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004062 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004063 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004064 } else {
4065 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004066 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004067 __ jmp(slow_path->GetEntryLabel());
4068 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004069 }
4070 __ j(kEqual, slow_path->GetEntryLabel());
4071}
4072
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004073void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004074 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004075 GenerateImplicitNullCheck(instruction);
4076 } else {
4077 GenerateExplicitNullCheck(instruction);
4078 }
4079}
4080
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004081void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004082 LocationSummary* locations =
4083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004084 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004085 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004086 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4087 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4088 } else {
4089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4090 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004091}
4092
4093void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4094 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004096 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004097 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004098
Roland Levillain4d027112015-07-01 15:41:14 +01004099 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004100 case Primitive::kPrimBoolean: {
4101 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004102 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004103 if (index.IsConstant()) {
4104 __ movzxb(out, Address(obj,
4105 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4106 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004107 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004108 }
4109 break;
4110 }
4111
4112 case Primitive::kPrimByte: {
4113 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004114 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004115 if (index.IsConstant()) {
4116 __ movsxb(out, Address(obj,
4117 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004119 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004120 }
4121 break;
4122 }
4123
4124 case Primitive::kPrimShort: {
4125 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004126 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004127 if (index.IsConstant()) {
4128 __ movsxw(out, Address(obj,
4129 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4130 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004131 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004132 }
4133 break;
4134 }
4135
4136 case Primitive::kPrimChar: {
4137 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004139 if (index.IsConstant()) {
4140 __ movzxw(out, Address(obj,
4141 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4142 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004143 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004144 }
4145 break;
4146 }
4147
4148 case Primitive::kPrimInt:
4149 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01004150 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4151 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004152 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004153 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004154 if (index.IsConstant()) {
4155 __ movl(out, Address(obj,
4156 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4157 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004158 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004159 }
4160 break;
4161 }
4162
4163 case Primitive::kPrimLong: {
4164 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004165 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004166 if (index.IsConstant()) {
4167 __ movq(out, Address(obj,
4168 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4169 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004170 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004171 }
4172 break;
4173 }
4174
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004175 case Primitive::kPrimFloat: {
4176 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004177 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004178 if (index.IsConstant()) {
4179 __ movss(out, Address(obj,
4180 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4181 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004182 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004183 }
4184 break;
4185 }
4186
4187 case Primitive::kPrimDouble: {
4188 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004189 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004190 if (index.IsConstant()) {
4191 __ movsd(out, Address(obj,
4192 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4193 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004194 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004195 }
4196 break;
4197 }
4198
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004199 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004200 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004201 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004202 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004203 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004204
4205 if (type == Primitive::kPrimNot) {
4206 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4207 __ MaybeUnpoisonHeapReference(out);
4208 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004209}
4210
4211void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004212 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004213
4214 bool needs_write_barrier =
4215 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004216 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004217
Nicolas Geoffray39468442014-09-02 15:17:15 +01004218 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004219 instruction,
4220 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004221
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004222 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004223 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4224 if (Primitive::IsFloatingPointType(value_type)) {
4225 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004226 } else {
4227 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4228 }
4229
4230 if (needs_write_barrier) {
4231 // Temporary registers for the write barrier.
4232 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4233 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004234 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004235}
4236
4237void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4238 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004239 CpuRegister array = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004241 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004242 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004243 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004244 bool needs_write_barrier =
4245 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004246 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4247 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4248 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004249
4250 switch (value_type) {
4251 case Primitive::kPrimBoolean:
4252 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004253 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4254 Address address = index.IsConstant()
4255 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4256 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4257 if (value.IsRegister()) {
4258 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004259 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004260 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004261 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004262 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004263 break;
4264 }
4265
4266 case Primitive::kPrimShort:
4267 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004268 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4269 Address address = index.IsConstant()
4270 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4271 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4272 if (value.IsRegister()) {
4273 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004274 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004275 DCHECK(value.IsConstant()) << value;
4276 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004277 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004278 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004279 break;
4280 }
4281
4282 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004283 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4284 Address address = index.IsConstant()
4285 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4286 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4287 if (!value.IsRegister()) {
4288 // Just setting null.
4289 DCHECK(instruction->InputAt(2)->IsNullConstant());
4290 DCHECK(value.IsConstant()) << value;
4291 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004292 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004293 DCHECK(!needs_write_barrier);
4294 DCHECK(!may_need_runtime_call);
4295 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004296 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004297
4298 DCHECK(needs_write_barrier);
4299 CpuRegister register_value = value.AsRegister<CpuRegister>();
4300 NearLabel done, not_null, do_put;
4301 SlowPathCode* slow_path = nullptr;
4302 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4303 if (may_need_runtime_call) {
4304 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4305 codegen_->AddSlowPath(slow_path);
4306 if (instruction->GetValueCanBeNull()) {
4307 __ testl(register_value, register_value);
4308 __ j(kNotEqual, &not_null);
4309 __ movl(address, Immediate(0));
4310 codegen_->MaybeRecordImplicitNullCheck(instruction);
4311 __ jmp(&done);
4312 __ Bind(&not_null);
4313 }
4314
4315 __ movl(temp, Address(array, class_offset));
4316 codegen_->MaybeRecordImplicitNullCheck(instruction);
4317 __ MaybeUnpoisonHeapReference(temp);
4318 __ movl(temp, Address(temp, component_offset));
4319 // No need to poison/unpoison, we're comparing two poisoned references.
4320 __ cmpl(temp, Address(register_value, class_offset));
4321 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4322 __ j(kEqual, &do_put);
4323 __ MaybeUnpoisonHeapReference(temp);
4324 __ movl(temp, Address(temp, super_offset));
4325 // No need to unpoison the result, we're comparing against null.
4326 __ testl(temp, temp);
4327 __ j(kNotEqual, slow_path->GetEntryLabel());
4328 __ Bind(&do_put);
4329 } else {
4330 __ j(kNotEqual, slow_path->GetEntryLabel());
4331 }
4332 }
4333
4334 if (kPoisonHeapReferences) {
4335 __ movl(temp, register_value);
4336 __ PoisonHeapReference(temp);
4337 __ movl(address, temp);
4338 } else {
4339 __ movl(address, register_value);
4340 }
4341 if (!may_need_runtime_call) {
4342 codegen_->MaybeRecordImplicitNullCheck(instruction);
4343 }
4344
4345 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4346 codegen_->MarkGCCard(
4347 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4348 __ Bind(&done);
4349
4350 if (slow_path != nullptr) {
4351 __ Bind(slow_path->GetExitLabel());
4352 }
4353
4354 break;
4355 }
4356 case Primitive::kPrimInt: {
4357 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4358 Address address = index.IsConstant()
4359 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4360 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4361 if (value.IsRegister()) {
4362 __ movl(address, value.AsRegister<CpuRegister>());
4363 } else {
4364 DCHECK(value.IsConstant()) << value;
4365 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4366 __ movl(address, Immediate(v));
4367 }
4368 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004369 break;
4370 }
4371
4372 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004373 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4374 Address address = index.IsConstant()
4375 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4376 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4377 if (value.IsRegister()) {
4378 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004379 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004380 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004381 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004382 Address address_high = index.IsConstant()
4383 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4384 offset + sizeof(int32_t))
4385 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4386 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004387 }
4388 break;
4389 }
4390
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004391 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004392 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4393 Address address = index.IsConstant()
4394 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4395 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004396 if (value.IsFpuRegister()) {
4397 __ movss(address, value.AsFpuRegister<XmmRegister>());
4398 } else {
4399 DCHECK(value.IsConstant());
4400 int32_t v =
4401 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4402 __ movl(address, Immediate(v));
4403 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004404 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004405 break;
4406 }
4407
4408 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004409 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4410 Address address = index.IsConstant()
4411 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4412 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004413 if (value.IsFpuRegister()) {
4414 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4415 codegen_->MaybeRecordImplicitNullCheck(instruction);
4416 } else {
4417 int64_t v =
4418 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4419 Address address_high = index.IsConstant()
4420 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4421 offset + sizeof(int32_t))
4422 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4423 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4424 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004425 break;
4426 }
4427
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004428 case Primitive::kPrimVoid:
4429 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004430 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004431 }
4432}
4433
4434void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004435 LocationSummary* locations =
4436 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004437 locations->SetInAt(0, Location::RequiresRegister());
4438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004439}
4440
4441void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4442 LocationSummary* locations = instruction->GetLocations();
4443 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004444 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4445 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004446 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004447 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004448}
4449
4450void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004451 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4452 ? LocationSummary::kCallOnSlowPath
4453 : LocationSummary::kNoCall;
4454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004455 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004456 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004457 if (instruction->HasUses()) {
4458 locations->SetOut(Location::SameAsFirstInput());
4459 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004460}
4461
4462void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4463 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004464 Location index_loc = locations->InAt(0);
4465 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004466 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004467 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004468
Mark Mendell99dbd682015-04-22 16:18:52 -04004469 if (length_loc.IsConstant()) {
4470 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4471 if (index_loc.IsConstant()) {
4472 // BCE will remove the bounds check if we are guarenteed to pass.
4473 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4474 if (index < 0 || index >= length) {
4475 codegen_->AddSlowPath(slow_path);
4476 __ jmp(slow_path->GetEntryLabel());
4477 } else {
4478 // Some optimization after BCE may have generated this, and we should not
4479 // generate a bounds check if it is a valid range.
4480 }
4481 return;
4482 }
4483
4484 // We have to reverse the jump condition because the length is the constant.
4485 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4486 __ cmpl(index_reg, Immediate(length));
4487 codegen_->AddSlowPath(slow_path);
4488 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004489 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004490 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4491 if (index_loc.IsConstant()) {
4492 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4493 __ cmpl(length, Immediate(value));
4494 } else {
4495 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4496 }
4497 codegen_->AddSlowPath(slow_path);
4498 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004499 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004500}
4501
4502void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4503 CpuRegister card,
4504 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004505 CpuRegister value,
4506 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004507 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004508 if (value_can_be_null) {
4509 __ testl(value, value);
4510 __ j(kEqual, &is_null);
4511 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004512 __ gs()->movq(card, Address::Absolute(
4513 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4514 __ movq(temp, object);
4515 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004516 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004517 if (value_can_be_null) {
4518 __ Bind(&is_null);
4519 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004520}
4521
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004522void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4523 temp->SetLocations(nullptr);
4524}
4525
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004526void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004527 // Nothing to do, this is driven by the code generator.
4528}
4529
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004530void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004531 LOG(FATAL) << "Unimplemented";
4532}
4533
4534void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004535 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4536}
4537
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004538void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4539 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4540}
4541
4542void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004543 HBasicBlock* block = instruction->GetBlock();
4544 if (block->GetLoopInformation() != nullptr) {
4545 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4546 // The back edge will generate the suspend check.
4547 return;
4548 }
4549 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4550 // The goto will generate the suspend check.
4551 return;
4552 }
4553 GenerateSuspendCheck(instruction, nullptr);
4554}
4555
4556void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4557 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004558 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004559 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4560 if (slow_path == nullptr) {
4561 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4562 instruction->SetSlowPath(slow_path);
4563 codegen_->AddSlowPath(slow_path);
4564 if (successor != nullptr) {
4565 DCHECK(successor->IsLoopHeader());
4566 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4567 }
4568 } else {
4569 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4570 }
4571
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004572 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004573 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004574 if (successor == nullptr) {
4575 __ j(kNotEqual, slow_path->GetEntryLabel());
4576 __ Bind(slow_path->GetReturnLabel());
4577 } else {
4578 __ j(kEqual, codegen_->GetLabelOf(successor));
4579 __ jmp(slow_path->GetEntryLabel());
4580 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004581}
4582
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004583X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4584 return codegen_->GetAssembler();
4585}
4586
4587void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004588 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004589 Location source = move->GetSource();
4590 Location destination = move->GetDestination();
4591
4592 if (source.IsRegister()) {
4593 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004594 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004595 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004596 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004597 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004598 } else {
4599 DCHECK(destination.IsDoubleStackSlot());
4600 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004601 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004602 }
4603 } else if (source.IsStackSlot()) {
4604 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004605 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004606 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004607 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004608 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004609 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004610 } else {
4611 DCHECK(destination.IsStackSlot());
4612 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4613 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4614 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004615 } else if (source.IsDoubleStackSlot()) {
4616 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004617 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004618 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004619 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004620 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4621 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004622 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004623 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004624 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4625 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4626 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004627 } else if (source.IsConstant()) {
4628 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004629 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4630 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004631 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004632 if (value == 0) {
4633 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4634 } else {
4635 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4636 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004637 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004638 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004639 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004640 }
4641 } else if (constant->IsLongConstant()) {
4642 int64_t value = constant->AsLongConstant()->GetValue();
4643 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004644 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004645 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004646 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004647 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004648 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004649 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004650 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004651 int32_t value = bit_cast<int32_t, float>(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 // easy FP 0.0.
4656 __ xorps(dest, dest);
4657 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004658 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004659 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004660 } else {
4661 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004662 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004663 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4664 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004665 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004666 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004667 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004668 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004669 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004670 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4671 if (value == 0) {
4672 __ xorpd(dest, dest);
4673 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004674 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004675 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004676 } else {
4677 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004678 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004679 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004680 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004681 } else if (source.IsFpuRegister()) {
4682 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004683 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004684 } else if (destination.IsStackSlot()) {
4685 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004686 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004687 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00004688 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004689 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004690 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004691 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004692 }
4693}
4694
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004695void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004696 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004697 __ movl(Address(CpuRegister(RSP), mem), reg);
4698 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004699}
4700
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004701void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004702 ScratchRegisterScope ensure_scratch(
4703 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
4704
4705 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4706 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4707 __ movl(CpuRegister(ensure_scratch.GetRegister()),
4708 Address(CpuRegister(RSP), mem2 + stack_offset));
4709 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4710 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
4711 CpuRegister(ensure_scratch.GetRegister()));
4712}
4713
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004714void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
4715 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4716 __ movq(Address(CpuRegister(RSP), mem), reg);
4717 __ movq(reg, CpuRegister(TMP));
4718}
4719
4720void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
4721 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004722 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004723
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004724 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
4725 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
4726 __ movq(CpuRegister(ensure_scratch.GetRegister()),
4727 Address(CpuRegister(RSP), mem2 + stack_offset));
4728 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
4729 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
4730 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004731}
4732
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004733void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
4734 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4735 __ movss(Address(CpuRegister(RSP), mem), reg);
4736 __ movd(reg, CpuRegister(TMP));
4737}
4738
4739void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
4740 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
4741 __ movsd(Address(CpuRegister(RSP), mem), reg);
4742 __ movd(reg, CpuRegister(TMP));
4743}
4744
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004745void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004746 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004747 Location source = move->GetSource();
4748 Location destination = move->GetDestination();
4749
4750 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004751 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004752 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004753 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004754 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004755 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004756 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004757 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
4758 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004759 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004760 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004761 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004762 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
4763 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004764 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004765 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
4766 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4767 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004768 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004769 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004770 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004771 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004772 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004773 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004774 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004775 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004776 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004777 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004778 }
4779}
4780
4781
4782void ParallelMoveResolverX86_64::SpillScratch(int reg) {
4783 __ pushq(CpuRegister(reg));
4784}
4785
4786
4787void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
4788 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004789}
4790
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004791void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004792 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004793 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4794 Immediate(mirror::Class::kStatusInitialized));
4795 __ j(kLess, slow_path->GetEntryLabel());
4796 __ Bind(slow_path->GetExitLabel());
4797 // No need for memory fence, thanks to the X86_64 memory model.
4798}
4799
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004800void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004801 InvokeRuntimeCallingConvention calling_convention;
4802 CodeGenerator::CreateLoadClassLocationSummary(
4803 cls,
4804 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4805 Location::RegisterLocation(RAX));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004806}
4807
4808void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004809 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004810 if (cls->NeedsAccessCheck()) {
4811 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4812 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4813 cls,
4814 cls->GetDexPc(),
4815 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004816 return;
4817 }
4818
4819 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4820 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
4821 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004822 DCHECK(!cls->CanCallRuntime());
4823 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004824 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004825 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004826 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01004827 __ movq(out, Address(
4828 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004829 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004830 // TODO: We will need a read barrier here.
Roland Levillain4d027112015-07-01 15:41:14 +01004831
Andreas Gampe85b62f22015-09-09 13:15:38 -07004832 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004833 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4834 codegen_->AddSlowPath(slow_path);
4835 __ testl(out, out);
4836 __ j(kEqual, slow_path->GetEntryLabel());
4837 if (cls->MustGenerateClinitCheck()) {
4838 GenerateClassInitializationCheck(slow_path, out);
4839 } else {
4840 __ Bind(slow_path->GetExitLabel());
4841 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004842 }
4843}
4844
4845void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
4846 LocationSummary* locations =
4847 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4848 locations->SetInAt(0, Location::RequiresRegister());
4849 if (check->HasUses()) {
4850 locations->SetOut(Location::SameAsFirstInput());
4851 }
4852}
4853
4854void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004855 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004856 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004857 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004858 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004859 GenerateClassInitializationCheck(slow_path,
4860 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004861}
4862
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004863void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
4864 LocationSummary* locations =
4865 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004866 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004867 locations->SetOut(Location::RequiresRegister());
4868}
4869
4870void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004871 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004872 codegen_->AddSlowPath(slow_path);
4873
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004874 LocationSummary* locations = load->GetLocations();
4875 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
4876 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004877 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004878 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004879 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004880 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004881 __ testl(out, out);
4882 __ j(kEqual, slow_path->GetEntryLabel());
4883 __ Bind(slow_path->GetExitLabel());
4884}
4885
David Brazdilcb1c0552015-08-04 16:22:25 +01004886static Address GetExceptionTlsAddress() {
4887 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
4888}
4889
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004890void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
4891 LocationSummary* locations =
4892 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4893 locations->SetOut(Location::RequiresRegister());
4894}
4895
4896void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004897 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
4898}
4899
4900void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
4901 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4902}
4903
4904void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4905 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004906}
4907
4908void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
4909 LocationSummary* locations =
4910 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4911 InvokeRuntimeCallingConvention calling_convention;
4912 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4913}
4914
4915void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004916 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4917 instruction,
4918 instruction->GetDexPc(),
4919 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004920}
4921
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004922void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004923 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4924 switch (instruction->GetTypeCheckKind()) {
4925 case TypeCheckKind::kExactCheck:
4926 case TypeCheckKind::kAbstractClassCheck:
4927 case TypeCheckKind::kClassHierarchyCheck:
4928 case TypeCheckKind::kArrayObjectCheck:
4929 call_kind = LocationSummary::kNoCall;
4930 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004931 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004932 case TypeCheckKind::kInterfaceCheck:
4933 call_kind = LocationSummary::kCall;
4934 break;
4935 case TypeCheckKind::kArrayCheck:
4936 call_kind = LocationSummary::kCallOnSlowPath;
4937 break;
4938 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004939 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004940 if (call_kind != LocationSummary::kCall) {
4941 locations->SetInAt(0, Location::RequiresRegister());
4942 locations->SetInAt(1, Location::Any());
4943 // Note that TypeCheckSlowPathX86_64 uses this register too.
4944 locations->SetOut(Location::RequiresRegister());
4945 } else {
4946 InvokeRuntimeCallingConvention calling_convention;
4947 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4948 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4949 locations->SetOut(Location::RegisterLocation(RAX));
4950 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004951}
4952
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004953void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004954 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004955 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004956 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004957 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004958 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004959 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4960 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4961 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004962 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004963 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004964
4965 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004966 // Avoid null check if we know obj is not null.
4967 if (instruction->MustDoNullCheck()) {
4968 __ testl(obj, obj);
4969 __ j(kEqual, &zero);
4970 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004971
Calin Juravle98893e12015-10-02 21:05:03 +01004972 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004973 // This is safe, as the register is caller-save, and the object must be in another
4974 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004975 CpuRegister target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4976 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004977 ? obj
4978 : out;
4979 __ movl(target, Address(obj, class_offset));
4980 __ MaybeUnpoisonHeapReference(target);
4981
4982 switch (instruction->GetTypeCheckKind()) {
4983 case TypeCheckKind::kExactCheck: {
4984 if (cls.IsRegister()) {
4985 __ cmpl(out, cls.AsRegister<CpuRegister>());
4986 } else {
4987 DCHECK(cls.IsStackSlot()) << cls;
4988 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
4989 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004990 if (zero.IsLinked()) {
4991 // Classes must be equal for the instanceof to succeed.
4992 __ j(kNotEqual, &zero);
4993 __ movl(out, Immediate(1));
4994 __ jmp(&done);
4995 } else {
4996 __ setcc(kEqual, out);
4997 // setcc only sets the low byte.
4998 __ andl(out, Immediate(1));
4999 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005000 break;
5001 }
5002 case TypeCheckKind::kAbstractClassCheck: {
5003 // If the class is abstract, we eagerly fetch the super class of the
5004 // object to avoid doing a comparison we know will fail.
5005 NearLabel loop, success;
5006 __ Bind(&loop);
5007 __ movl(out, Address(out, super_offset));
5008 __ MaybeUnpoisonHeapReference(out);
5009 __ testl(out, out);
5010 // If `out` is null, we use it for the result, and jump to `done`.
5011 __ j(kEqual, &done);
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(kNotEqual, &loop);
5019 __ movl(out, Immediate(1));
5020 if (zero.IsLinked()) {
5021 __ jmp(&done);
5022 }
5023 break;
5024 }
5025 case TypeCheckKind::kClassHierarchyCheck: {
5026 // Walk over the class hierarchy to find a match.
5027 NearLabel loop, success;
5028 __ Bind(&loop);
5029 if (cls.IsRegister()) {
5030 __ cmpl(out, cls.AsRegister<CpuRegister>());
5031 } else {
5032 DCHECK(cls.IsStackSlot()) << cls;
5033 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5034 }
5035 __ j(kEqual, &success);
5036 __ movl(out, Address(out, super_offset));
5037 __ MaybeUnpoisonHeapReference(out);
5038 __ testl(out, out);
5039 __ j(kNotEqual, &loop);
5040 // If `out` is null, we use it for the result, and jump to `done`.
5041 __ jmp(&done);
5042 __ Bind(&success);
5043 __ movl(out, Immediate(1));
5044 if (zero.IsLinked()) {
5045 __ jmp(&done);
5046 }
5047 break;
5048 }
5049 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005050 // Do an exact check.
5051 NearLabel exact_check;
5052 if (cls.IsRegister()) {
5053 __ cmpl(out, cls.AsRegister<CpuRegister>());
5054 } else {
5055 DCHECK(cls.IsStackSlot()) << cls;
5056 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5057 }
5058 __ j(kEqual, &exact_check);
5059 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005060 __ movl(out, Address(out, component_offset));
5061 __ MaybeUnpoisonHeapReference(out);
5062 __ testl(out, out);
5063 // If `out` is null, we use it for the result, and jump to `done`.
5064 __ j(kEqual, &done);
5065 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5066 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005067 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005068 __ movl(out, Immediate(1));
5069 __ jmp(&done);
5070 break;
5071 }
5072 case TypeCheckKind::kArrayCheck: {
5073 if (cls.IsRegister()) {
5074 __ cmpl(out, cls.AsRegister<CpuRegister>());
5075 } else {
5076 DCHECK(cls.IsStackSlot()) << cls;
5077 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5078 }
5079 DCHECK(locations->OnlyCallsOnSlowPath());
5080 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
5081 instruction, /* is_fatal */ false);
5082 codegen_->AddSlowPath(slow_path);
5083 __ j(kNotEqual, slow_path->GetEntryLabel());
5084 __ movl(out, Immediate(1));
5085 if (zero.IsLinked()) {
5086 __ jmp(&done);
5087 }
5088 break;
5089 }
Calin Juravle98893e12015-10-02 21:05:03 +01005090 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005091 case TypeCheckKind::kInterfaceCheck:
5092 default: {
5093 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
5094 instruction,
5095 instruction->GetDexPc(),
5096 nullptr);
5097 if (zero.IsLinked()) {
5098 __ jmp(&done);
5099 }
5100 break;
5101 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005102 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005103
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005104 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005105 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005106 __ xorl(out, out);
5107 }
5108
5109 if (done.IsLinked()) {
5110 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005111 }
5112
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005113 if (slow_path != nullptr) {
5114 __ Bind(slow_path->GetExitLabel());
5115 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005116}
5117
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005118void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005119 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5120 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5121
5122 switch (instruction->GetTypeCheckKind()) {
5123 case TypeCheckKind::kExactCheck:
5124 case TypeCheckKind::kAbstractClassCheck:
5125 case TypeCheckKind::kClassHierarchyCheck:
5126 case TypeCheckKind::kArrayObjectCheck:
5127 call_kind = throws_into_catch
5128 ? LocationSummary::kCallOnSlowPath
5129 : LocationSummary::kNoCall;
5130 break;
Calin Juravle98893e12015-10-02 21:05:03 +01005131 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005132 case TypeCheckKind::kInterfaceCheck:
5133 call_kind = LocationSummary::kCall;
5134 break;
5135 case TypeCheckKind::kArrayCheck:
5136 call_kind = LocationSummary::kCallOnSlowPath;
5137 break;
5138 }
5139
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005141 instruction, call_kind);
5142 if (call_kind != LocationSummary::kCall) {
5143 locations->SetInAt(0, Location::RequiresRegister());
5144 locations->SetInAt(1, Location::Any());
5145 // Note that TypeCheckSlowPathX86_64 uses this register too.
5146 locations->AddTemp(Location::RequiresRegister());
5147 } else {
5148 InvokeRuntimeCallingConvention calling_convention;
5149 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5150 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5151 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005152}
5153
5154void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
5155 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005156 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005157 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005158 CpuRegister temp = locations->WillCall()
5159 ? CpuRegister(kNoRegister)
5160 : locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005161
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005162 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5163 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5164 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5165 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5166 SlowPathCode* slow_path = nullptr;
5167
5168 if (!locations->WillCall()) {
5169 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
5170 instruction, !locations->CanCall());
5171 codegen_->AddSlowPath(slow_path);
5172 }
5173
5174 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005175 // Avoid null check if we know obj is not null.
5176 if (instruction->MustDoNullCheck()) {
5177 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005178 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005179 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005180
5181 if (locations->WillCall()) {
5182 __ movl(obj, Address(obj, class_offset));
5183 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005184 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005185 __ movl(temp, Address(obj, class_offset));
5186 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005187 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005188
5189 switch (instruction->GetTypeCheckKind()) {
5190 case TypeCheckKind::kExactCheck:
5191 case TypeCheckKind::kArrayCheck: {
5192 if (cls.IsRegister()) {
5193 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5194 } else {
5195 DCHECK(cls.IsStackSlot()) << cls;
5196 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5197 }
5198 // Jump to slow path for throwing the exception or doing a
5199 // more involved array check.
5200 __ j(kNotEqual, slow_path->GetEntryLabel());
5201 break;
5202 }
5203 case TypeCheckKind::kAbstractClassCheck: {
5204 // If the class is abstract, we eagerly fetch the super class of the
5205 // object to avoid doing a comparison we know will fail.
5206 NearLabel loop;
5207 __ Bind(&loop);
5208 __ movl(temp, Address(temp, super_offset));
5209 __ MaybeUnpoisonHeapReference(temp);
5210 __ testl(temp, temp);
5211 // Jump to the slow path to throw the exception.
5212 __ j(kEqual, slow_path->GetEntryLabel());
5213 if (cls.IsRegister()) {
5214 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5215 } else {
5216 DCHECK(cls.IsStackSlot()) << cls;
5217 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5218 }
5219 __ j(kNotEqual, &loop);
5220 break;
5221 }
5222 case TypeCheckKind::kClassHierarchyCheck: {
5223 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005224 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005225 __ Bind(&loop);
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 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005232 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005233 __ movl(temp, Address(temp, super_offset));
5234 __ MaybeUnpoisonHeapReference(temp);
5235 __ testl(temp, temp);
5236 __ j(kNotEqual, &loop);
5237 // Jump to the slow path to throw the exception.
5238 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005239 break;
5240 }
5241 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005242 // Do an exact check.
5243 if (cls.IsRegister()) {
5244 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5245 } else {
5246 DCHECK(cls.IsStackSlot()) << cls;
5247 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5248 }
5249 __ j(kEqual, &done);
5250 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005251 __ movl(temp, Address(temp, component_offset));
5252 __ MaybeUnpoisonHeapReference(temp);
5253 __ testl(temp, temp);
5254 __ j(kEqual, slow_path->GetEntryLabel());
5255 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5256 __ j(kNotEqual, slow_path->GetEntryLabel());
5257 break;
5258 }
Calin Juravle98893e12015-10-02 21:05:03 +01005259 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005260 case TypeCheckKind::kInterfaceCheck:
5261 default:
5262 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5263 instruction,
5264 instruction->GetDexPc(),
5265 nullptr);
5266 break;
5267 }
5268 __ Bind(&done);
5269
5270 if (slow_path != nullptr) {
5271 __ Bind(slow_path->GetExitLabel());
5272 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005273}
5274
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005275void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5276 LocationSummary* locations =
5277 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5278 InvokeRuntimeCallingConvention calling_convention;
5279 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5280}
5281
5282void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005283 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5284 : QUICK_ENTRY_POINT(pUnlockObject),
5285 instruction,
5286 instruction->GetDexPc(),
5287 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005288}
5289
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005290void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5291void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5292void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5293
5294void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5295 LocationSummary* locations =
5296 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5297 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5298 || instruction->GetResultType() == Primitive::kPrimLong);
5299 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005300 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005301 locations->SetOut(Location::SameAsFirstInput());
5302}
5303
5304void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5305 HandleBitwiseOperation(instruction);
5306}
5307
5308void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5309 HandleBitwiseOperation(instruction);
5310}
5311
5312void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5313 HandleBitwiseOperation(instruction);
5314}
5315
5316void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5317 LocationSummary* locations = instruction->GetLocations();
5318 Location first = locations->InAt(0);
5319 Location second = locations->InAt(1);
5320 DCHECK(first.Equals(locations->Out()));
5321
5322 if (instruction->GetResultType() == Primitive::kPrimInt) {
5323 if (second.IsRegister()) {
5324 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005325 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005326 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005327 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005328 } else {
5329 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005330 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005331 }
5332 } else if (second.IsConstant()) {
5333 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5334 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005335 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005336 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005337 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005338 } else {
5339 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005340 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005341 }
5342 } else {
5343 Address address(CpuRegister(RSP), second.GetStackIndex());
5344 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005345 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005346 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005347 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005348 } else {
5349 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005350 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005351 }
5352 }
5353 } else {
5354 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005355 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5356 bool second_is_constant = false;
5357 int64_t value = 0;
5358 if (second.IsConstant()) {
5359 second_is_constant = true;
5360 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005361 }
Mark Mendell40741f32015-04-20 22:10:34 -04005362 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005363
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005364 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005365 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005366 if (is_int32_value) {
5367 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5368 } else {
5369 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5370 }
5371 } else if (second.IsDoubleStackSlot()) {
5372 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005373 } else {
5374 __ andq(first_reg, second.AsRegister<CpuRegister>());
5375 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005376 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005377 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005378 if (is_int32_value) {
5379 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5380 } else {
5381 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5382 }
5383 } else if (second.IsDoubleStackSlot()) {
5384 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005385 } else {
5386 __ orq(first_reg, second.AsRegister<CpuRegister>());
5387 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005388 } else {
5389 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005390 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005391 if (is_int32_value) {
5392 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5393 } else {
5394 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5395 }
5396 } else if (second.IsDoubleStackSlot()) {
5397 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005398 } else {
5399 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5400 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005401 }
5402 }
5403}
5404
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005405void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005406 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005407 LOG(FATAL) << "Unreachable";
5408}
5409
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005410void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005411 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005412 LOG(FATAL) << "Unreachable";
5413}
5414
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005415void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
5416 DCHECK(codegen_->IsBaseline());
5417 LocationSummary* locations =
5418 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5419 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5420}
5421
5422void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5423 DCHECK(codegen_->IsBaseline());
5424 // Will be generated at use site.
5425}
5426
Mark Mendellfe57faa2015-09-18 09:26:15 -04005427// Simple implementation of packed switch - generate cascaded compare/jumps.
5428void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5429 LocationSummary* locations =
5430 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5431 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04005432 locations->AddTemp(Location::RequiresRegister());
5433 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04005434}
5435
5436void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5437 int32_t lower_bound = switch_instr->GetStartValue();
5438 int32_t num_entries = switch_instr->GetNumEntries();
5439 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04005440 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
5441 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
5442 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
5443
5444 // Remove the bias, if needed.
5445 Register value_reg_out = value_reg_in.AsRegister();
5446 if (lower_bound != 0) {
5447 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
5448 value_reg_out = temp_reg.AsRegister();
5449 }
5450 CpuRegister value_reg(value_reg_out);
5451
5452 // Is the value in range?
Mark Mendellfe57faa2015-09-18 09:26:15 -04005453 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
Mark Mendell9c86b482015-09-18 13:36:07 -04005454 __ cmpl(value_reg, Immediate(num_entries - 1));
5455 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005456
Mark Mendell9c86b482015-09-18 13:36:07 -04005457 // We are in the range of the table.
5458 // Load the address of the jump table in the constant area.
5459 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005460
Mark Mendell9c86b482015-09-18 13:36:07 -04005461 // Load the (signed) offset from the jump table.
5462 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
5463
5464 // Add the offset to the address of the table base.
5465 __ addq(temp_reg, base_reg);
5466
5467 // And jump.
5468 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005469}
5470
Mark Mendell92e83bf2015-05-07 11:25:03 -04005471void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
5472 if (value == 0) {
5473 __ xorl(dest, dest);
5474 } else if (value > 0 && IsInt<32>(value)) {
5475 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
5476 __ movl(dest, Immediate(static_cast<int32_t>(value)));
5477 } else {
5478 __ movq(dest, Immediate(value));
5479 }
5480}
5481
Mark Mendellcfa410b2015-05-25 16:02:44 -04005482void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
5483 DCHECK(dest.IsDoubleStackSlot());
5484 if (IsInt<32>(value)) {
5485 // Can move directly as an int32 constant.
5486 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
5487 Immediate(static_cast<int32_t>(value)));
5488 } else {
5489 Load64BitValue(CpuRegister(TMP), value);
5490 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
5491 }
5492}
5493
Mark Mendell9c86b482015-09-18 13:36:07 -04005494/**
5495 * Class to handle late fixup of offsets into constant area.
5496 */
5497class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
5498 public:
5499 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
5500 : codegen_(&codegen), offset_into_constant_area_(offset) {}
5501
5502 protected:
5503 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
5504
5505 CodeGeneratorX86_64* codegen_;
5506
5507 private:
5508 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5509 // Patch the correct offset for the instruction. We use the address of the
5510 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
5511 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
5512 int32_t relative_position = constant_offset - pos;
5513
5514 // Patch in the right value.
5515 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5516 }
5517
5518 // Location in constant area that the fixup refers to.
5519 size_t offset_into_constant_area_;
5520};
5521
5522/**
5523 t * Class to handle late fixup of offsets to a jump table that will be created in the
5524 * constant area.
5525 */
5526class JumpTableRIPFixup : public RIPFixup {
5527 public:
5528 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
5529 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
5530
5531 void CreateJumpTable() {
5532 X86_64Assembler* assembler = codegen_->GetAssembler();
5533
5534 // Ensure that the reference to the jump table has the correct offset.
5535 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
5536 SetOffset(offset_in_constant_table);
5537
5538 // Compute the offset from the start of the function to this jump table.
5539 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
5540
5541 // Populate the jump table with the correct values for the jump table.
5542 int32_t num_entries = switch_instr_->GetNumEntries();
5543 HBasicBlock* block = switch_instr_->GetBlock();
5544 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
5545 // The value that we want is the target offset - the position of the table.
5546 for (int32_t i = 0; i < num_entries; i++) {
5547 HBasicBlock* b = successors[i];
5548 Label* l = codegen_->GetLabelOf(b);
5549 DCHECK(l->IsBound());
5550 int32_t offset_to_block = l->Position() - current_table_offset;
5551 assembler->AppendInt32(offset_to_block);
5552 }
5553 }
5554
5555 private:
5556 const HPackedSwitch* switch_instr_;
5557};
5558
Mark Mendellf55c3e02015-03-26 21:07:46 -04005559void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
5560 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04005561 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04005562 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
5563 // 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 -04005564 assembler->Align(4, 0);
5565 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04005566
5567 // Populate any jump tables.
5568 for (auto jump_table : fixups_to_jump_tables_) {
5569 jump_table->CreateJumpTable();
5570 }
5571
5572 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04005573 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04005574 }
5575
5576 // And finish up.
5577 CodeGenerator::Finalize(allocator);
5578}
5579
Mark Mendellf55c3e02015-03-26 21:07:46 -04005580Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
5581 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5582 return Address::RIP(fixup);
5583}
5584
5585Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
5586 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5587 return Address::RIP(fixup);
5588}
5589
5590Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
5591 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5592 return Address::RIP(fixup);
5593}
5594
5595Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
5596 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5597 return Address::RIP(fixup);
5598}
5599
Andreas Gampe85b62f22015-09-09 13:15:38 -07005600// TODO: trg as memory.
5601void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5602 if (!trg.IsValid()) {
5603 DCHECK(type == Primitive::kPrimVoid);
5604 return;
5605 }
5606
5607 DCHECK_NE(type, Primitive::kPrimVoid);
5608
5609 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
5610 if (trg.Equals(return_loc)) {
5611 return;
5612 }
5613
5614 // Let the parallel move resolver take care of all of this.
5615 HParallelMove parallel_move(GetGraph()->GetArena());
5616 parallel_move.AddMove(return_loc, trg, type, nullptr);
5617 GetMoveResolver()->EmitNativeCode(&parallel_move);
5618}
5619
Mark Mendell9c86b482015-09-18 13:36:07 -04005620Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
5621 // Create a fixup to be used to create and address the jump table.
5622 JumpTableRIPFixup* table_fixup =
5623 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
5624
5625 // We have to populate the jump tables.
5626 fixups_to_jump_tables_.push_back(table_fixup);
5627 return Address::RIP(table_fixup);
5628}
5629
Mark Mendellea5af682015-10-22 17:35:49 -04005630void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
5631 const Address& addr_high,
5632 int64_t v,
5633 HInstruction* instruction) {
5634 if (IsInt<32>(v)) {
5635 int32_t v_32 = v;
5636 __ movq(addr_low, Immediate(v_32));
5637 MaybeRecordImplicitNullCheck(instruction);
5638 } else {
5639 // Didn't fit in a register. Do it in pieces.
5640 int32_t low_v = Low32Bits(v);
5641 int32_t high_v = High32Bits(v);
5642 __ movl(addr_low, Immediate(low_v));
5643 MaybeRecordImplicitNullCheck(instruction);
5644 __ movl(addr_high, Immediate(high_v));
5645 }
5646}
5647
Roland Levillain4d027112015-07-01 15:41:14 +01005648#undef __
5649
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005650} // namespace x86_64
5651} // namespace art