blob: 86e5f7c5f483ba21107d79d99722ee98a86ea8c7 [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
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace x86_64 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000044// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
45// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
46// generates less code/data with a small num_entries.
47static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000049static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Mark Mendell24f2dfa2015-01-14 19:51:45 -050052static constexpr int kC2ConditionMask = 0x400;
53
Roland Levillain62a46b22015-06-01 18:24:13 +010054#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010055#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Andreas Gampe85b62f22015-09-09 13:15:38 -070057class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060
Alexandre Rames2ed20af2015-03-06 13:55:35 +000061 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000062 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000064 if (instruction_->CanThrowIntoCatchBlock()) {
65 // Live registers will be restored in the catch block if caught.
66 SaveLiveRegisters(codegen, instruction_->GetLocations());
67 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000068 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
69 instruction_,
70 instruction_->GetDexPc(),
71 this);
Roland Levillain888d0672015-11-23 18:53:50 +000072 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 }
74
Alexandre Rames8158f282015-08-07 10:26:17 +010075 bool IsFatal() const OVERRIDE { return true; }
76
Alexandre Rames9931f312015-06-19 14:47:01 +010077 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
78
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
82};
83
Andreas Gampe85b62f22015-09-09 13:15:38 -070084class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000085 public:
86 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
87
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000089 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000091 if (instruction_->CanThrowIntoCatchBlock()) {
92 // Live registers will be restored in the catch block if caught.
93 SaveLiveRegisters(codegen, instruction_->GetLocations());
94 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000095 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
96 instruction_,
97 instruction_->GetDexPc(),
98 this);
Roland Levillain888d0672015-11-23 18:53:50 +000099 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000100 }
101
Alexandre Rames8158f282015-08-07 10:26:17 +0100102 bool IsFatal() const OVERRIDE { return true; }
103
Alexandre Rames9931f312015-06-19 14:47:01 +0100104 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
105
Calin Juravled0d48522014-11-04 16:40:20 +0000106 private:
107 HDivZeroCheck* const instruction_;
108 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
109};
110
Andreas Gampe85b62f22015-09-09 13:15:38 -0700111class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100113 DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000115
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000116 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000117 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000118 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000119 if (is_div_) {
120 __ negl(cpu_reg_);
121 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400122 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 }
124
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000125 } else {
126 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 if (is_div_) {
128 __ negq(cpu_reg_);
129 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400130 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000131 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000132 }
Calin Juravled0d48522014-11-04 16:40:20 +0000133 __ jmp(GetExitLabel());
134 }
135
Alexandre Rames9931f312015-06-19 14:47:01 +0100136 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
137
Calin Juravled0d48522014-11-04 16:40:20 +0000138 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000139 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000140 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000141 const bool is_div_;
142 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000143};
144
Andreas Gampe85b62f22015-09-09 13:15:38 -0700145class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100147 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000150 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000151 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000152 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000153 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000154 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
155 instruction_,
156 instruction_->GetDexPc(),
157 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000158 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000159 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 if (successor_ == nullptr) {
161 __ jmp(GetReturnLabel());
162 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000163 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100164 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 }
166
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100167 Label* GetReturnLabel() {
168 DCHECK(successor_ == nullptr);
169 return &return_label_;
170 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100172 HBasicBlock* GetSuccessor() const {
173 return successor_;
174 }
175
Alexandre Rames9931f312015-06-19 14:47:01 +0100176 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
177
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000178 private:
179 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100180 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181 Label return_label_;
182
183 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
184};
185
Andreas Gampe85b62f22015-09-09 13:15:38 -0700186class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100188 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
189 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000191 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100192 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000193 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000195 if (instruction_->CanThrowIntoCatchBlock()) {
196 // Live registers will be restored in the catch block if caught.
197 SaveLiveRegisters(codegen, instruction_->GetLocations());
198 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000199 // We're moving two locations to locations that could overlap, so we need a parallel
200 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100201 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000202 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100203 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000204 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100205 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100206 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100207 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
208 Primitive::kPrimInt);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000209 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
210 instruction_,
211 instruction_->GetDexPc(),
212 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000213 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100214 }
215
Alexandre Rames8158f282015-08-07 10:26:17 +0100216 bool IsFatal() const OVERRIDE { return true; }
217
Alexandre Rames9931f312015-06-19 14:47:01 +0100218 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
219
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100220 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100221 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100222
223 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
224};
225
Andreas Gampe85b62f22015-09-09 13:15:38 -0700226class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100227 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 LoadClassSlowPathX86_64(HLoadClass* cls,
229 HInstruction* at,
230 uint32_t dex_pc,
231 bool do_clinit)
232 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
233 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
234 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000236 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000238 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000241 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000242
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100243 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000245 x86_64_codegen->InvokeRuntime(do_clinit_ ?
246 QUICK_ENTRY_POINT(pInitializeStaticStorage) :
247 QUICK_ENTRY_POINT(pInitializeType),
248 at_,
249 dex_pc_,
250 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000251 if (do_clinit_) {
252 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
253 } else {
254 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
255 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100256
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 if (out.IsValid()) {
260 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000261 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 }
263
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000264 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100265 __ jmp(GetExitLabel());
266 }
267
Alexandre Rames9931f312015-06-19 14:47:01 +0100268 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
269
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100270 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 // The class this slow path will load.
272 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100273
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 // The instruction where this slow path is happening.
275 // (Might be the load class or an initialization check).
276 HInstruction* const at_;
277
278 // The dex PC of `at_`.
279 const uint32_t dex_pc_;
280
281 // Whether to initialize the class.
282 const bool do_clinit_;
283
284 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100285};
286
Andreas Gampe85b62f22015-09-09 13:15:38 -0700287class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000288 public:
289 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
290
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000291 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000292 LocationSummary* locations = instruction_->GetLocations();
293 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
294
Roland Levillain0d5a2812015-11-13 10:07:31 +0000295 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000296 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000297 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000298
299 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800300 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000301 Immediate(instruction_->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000302 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
303 instruction_,
304 instruction_->GetDexPc(),
305 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000306 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000307 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000308 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000309 __ jmp(GetExitLabel());
310 }
311
Alexandre Rames9931f312015-06-19 14:47:01 +0100312 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
313
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000314 private:
315 HLoadString* const instruction_;
316
317 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
318};
319
Andreas Gampe85b62f22015-09-09 13:15:38 -0700320class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000322 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
323 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000325 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100327 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
328 : locations->Out();
329 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 DCHECK(instruction_->IsCheckCast()
331 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
Roland Levillain0d5a2812015-11-13 10:07:31 +0000333 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000336 if (!is_fatal_) {
337 SaveLiveRegisters(codegen, locations);
338 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339
340 // We're moving two locations to locations that could overlap, so we need a parallel
341 // move resolver.
342 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000343 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100344 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000345 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100346 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100348 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
349 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000350
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000351 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000352 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
353 instruction_,
354 dex_pc,
355 this);
356 CheckEntrypointTypes<
357 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000358 } else {
359 DCHECK(instruction_->IsCheckCast());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000360 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
361 instruction_,
362 dex_pc,
363 this);
364 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000365 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000366
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000367 if (!is_fatal_) {
368 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000369 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 RestoreLiveRegisters(codegen, locations);
373 __ jmp(GetExitLabel());
374 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000375 }
376
Alexandre Rames9931f312015-06-19 14:47:01 +0100377 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
378
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000379 bool IsFatal() const OVERRIDE { return is_fatal_; }
380
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000381 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000382 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000383 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000384
385 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
386};
387
Andreas Gampe85b62f22015-09-09 13:15:38 -0700388class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700389 public:
Aart Bik42249c32016-01-07 15:33:50 -0800390 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 : instruction_(instruction) {}
392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000394 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395 __ Bind(GetEntryLabel());
396 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000397 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
Aart Bik42249c32016-01-07 15:33:50 -0800398 instruction_,
399 instruction_->GetDexPc(),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000400 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000401 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700402 }
403
Alexandre Rames9931f312015-06-19 14:47:01 +0100404 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
405
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700406 private:
Aart Bik42249c32016-01-07 15:33:50 -0800407 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700408 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
409};
410
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100411class ArraySetSlowPathX86_64 : public SlowPathCode {
412 public:
413 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : instruction_(instruction) {}
414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
416 LocationSummary* locations = instruction_->GetLocations();
417 __ Bind(GetEntryLabel());
418 SaveLiveRegisters(codegen, locations);
419
420 InvokeRuntimeCallingConvention calling_convention;
421 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
422 parallel_move.AddMove(
423 locations->InAt(0),
424 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
425 Primitive::kPrimNot,
426 nullptr);
427 parallel_move.AddMove(
428 locations->InAt(1),
429 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
430 Primitive::kPrimInt,
431 nullptr);
432 parallel_move.AddMove(
433 locations->InAt(2),
434 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
435 Primitive::kPrimNot,
436 nullptr);
437 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
438
Roland Levillain0d5a2812015-11-13 10:07:31 +0000439 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
440 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
441 instruction_,
442 instruction_->GetDexPc(),
443 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000444 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100445 RestoreLiveRegisters(codegen, locations);
446 __ jmp(GetExitLabel());
447 }
448
449 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
450
451 private:
452 HInstruction* const instruction_;
453
454 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
455};
456
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000457// Slow path marking an object during a read barrier.
458class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
459 public:
460 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location out, Location obj)
461 : instruction_(instruction), out_(out), obj_(obj) {
462 DCHECK(kEmitCompilerReadBarrier);
463 }
464
465 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
466
467 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
468 LocationSummary* locations = instruction_->GetLocations();
469 Register reg_out = out_.AsRegister<Register>();
470 DCHECK(locations->CanCall());
471 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
472 DCHECK(instruction_->IsInstanceFieldGet() ||
473 instruction_->IsStaticFieldGet() ||
474 instruction_->IsArrayGet() ||
475 instruction_->IsLoadClass() ||
476 instruction_->IsLoadString() ||
477 instruction_->IsInstanceOf() ||
478 instruction_->IsCheckCast())
479 << "Unexpected instruction in read barrier marking slow path: "
480 << instruction_->DebugName();
481
482 __ Bind(GetEntryLabel());
483 SaveLiveRegisters(codegen, locations);
484
485 InvokeRuntimeCallingConvention calling_convention;
486 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
487 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
488 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
489 instruction_,
490 instruction_->GetDexPc(),
491 this);
492 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
493 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
494
495 RestoreLiveRegisters(codegen, locations);
496 __ jmp(GetExitLabel());
497 }
498
499 private:
500 HInstruction* const instruction_;
501 const Location out_;
502 const Location obj_;
503
504 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
505};
506
Roland Levillain0d5a2812015-11-13 10:07:31 +0000507// Slow path generating a read barrier for a heap reference.
508class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
509 public:
510 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
511 Location out,
512 Location ref,
513 Location obj,
514 uint32_t offset,
515 Location index)
516 : instruction_(instruction),
517 out_(out),
518 ref_(ref),
519 obj_(obj),
520 offset_(offset),
521 index_(index) {
522 DCHECK(kEmitCompilerReadBarrier);
523 // If `obj` is equal to `out` or `ref`, it means the initial
524 // object has been overwritten by (or after) the heap object
525 // reference load to be instrumented, e.g.:
526 //
527 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000528 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000529 //
530 // In that case, we have lost the information about the original
531 // object, and the emitted read barrier cannot work properly.
532 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
533 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
534}
535
536 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
537 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
538 LocationSummary* locations = instruction_->GetLocations();
539 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
540 DCHECK(locations->CanCall());
541 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
542 DCHECK(!instruction_->IsInvoke() ||
543 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000544 instruction_->GetLocations()->Intrinsified()))
545 << "Unexpected instruction in read barrier for heap reference slow path: "
546 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000547
548 __ Bind(GetEntryLabel());
549 SaveLiveRegisters(codegen, locations);
550
551 // We may have to change the index's value, but as `index_` is a
552 // constant member (like other "inputs" of this slow path),
553 // introduce a copy of it, `index`.
554 Location index = index_;
555 if (index_.IsValid()) {
556 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
557 if (instruction_->IsArrayGet()) {
558 // Compute real offset and store it in index_.
559 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
560 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
561 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
562 // We are about to change the value of `index_reg` (see the
563 // calls to art::x86_64::X86_64Assembler::shll and
564 // art::x86_64::X86_64Assembler::AddImmediate below), but it
565 // has not been saved by the previous call to
566 // art::SlowPathCode::SaveLiveRegisters, as it is a
567 // callee-save register --
568 // art::SlowPathCode::SaveLiveRegisters does not consider
569 // callee-save registers, as it has been designed with the
570 // assumption that callee-save registers are supposed to be
571 // handled by the called function. So, as a callee-save
572 // register, `index_reg` _would_ eventually be saved onto
573 // the stack, but it would be too late: we would have
574 // changed its value earlier. Therefore, we manually save
575 // it here into another freely available register,
576 // `free_reg`, chosen of course among the caller-save
577 // registers (as a callee-save `free_reg` register would
578 // exhibit the same problem).
579 //
580 // Note we could have requested a temporary register from
581 // the register allocator instead; but we prefer not to, as
582 // this is a slow path, and we know we can find a
583 // caller-save register that is available.
584 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
585 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
586 index_reg = free_reg;
587 index = Location::RegisterLocation(index_reg);
588 } else {
589 // The initial register stored in `index_` has already been
590 // saved in the call to art::SlowPathCode::SaveLiveRegisters
591 // (as it is not a callee-save register), so we can freely
592 // use it.
593 }
594 // Shifting the index value contained in `index_reg` by the
595 // scale factor (2) cannot overflow in practice, as the
596 // runtime is unable to allocate object arrays with a size
597 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
598 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
599 static_assert(
600 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
601 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
602 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
603 } else {
604 DCHECK(instruction_->IsInvoke());
605 DCHECK(instruction_->GetLocations()->Intrinsified());
606 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
607 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
608 << instruction_->AsInvoke()->GetIntrinsic();
609 DCHECK_EQ(offset_, 0U);
610 DCHECK(index_.IsRegister());
611 }
612 }
613
614 // We're moving two or three locations to locations that could
615 // overlap, so we need a parallel move resolver.
616 InvokeRuntimeCallingConvention calling_convention;
617 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
618 parallel_move.AddMove(ref_,
619 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
620 Primitive::kPrimNot,
621 nullptr);
622 parallel_move.AddMove(obj_,
623 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
624 Primitive::kPrimNot,
625 nullptr);
626 if (index.IsValid()) {
627 parallel_move.AddMove(index,
628 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
629 Primitive::kPrimInt,
630 nullptr);
631 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
632 } else {
633 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
634 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
635 }
636 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
637 instruction_,
638 instruction_->GetDexPc(),
639 this);
640 CheckEntrypointTypes<
641 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
642 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
643
644 RestoreLiveRegisters(codegen, locations);
645 __ jmp(GetExitLabel());
646 }
647
648 const char* GetDescription() const OVERRIDE {
649 return "ReadBarrierForHeapReferenceSlowPathX86_64";
650 }
651
652 private:
653 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
654 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
655 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
656 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
657 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
658 return static_cast<CpuRegister>(i);
659 }
660 }
661 // We shall never fail to find a free caller-save register, as
662 // there are more than two core caller-save registers on x86-64
663 // (meaning it is possible to find one which is different from
664 // `ref` and `obj`).
665 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
666 LOG(FATAL) << "Could not find a free caller-save register";
667 UNREACHABLE();
668 }
669
670 HInstruction* const instruction_;
671 const Location out_;
672 const Location ref_;
673 const Location obj_;
674 const uint32_t offset_;
675 // An additional location containing an index to an array.
676 // Only used for HArrayGet and the UnsafeGetObject &
677 // UnsafeGetObjectVolatile intrinsics.
678 const Location index_;
679
680 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
681};
682
683// Slow path generating a read barrier for a GC root.
684class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
685 public:
686 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000687 : instruction_(instruction), out_(out), root_(root) {
688 DCHECK(kEmitCompilerReadBarrier);
689 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000690
691 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
692 LocationSummary* locations = instruction_->GetLocations();
693 DCHECK(locations->CanCall());
694 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000695 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
696 << "Unexpected instruction in read barrier for GC root slow path: "
697 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000698
699 __ Bind(GetEntryLabel());
700 SaveLiveRegisters(codegen, locations);
701
702 InvokeRuntimeCallingConvention calling_convention;
703 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
704 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
705 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
706 instruction_,
707 instruction_->GetDexPc(),
708 this);
709 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
710 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
711
712 RestoreLiveRegisters(codegen, locations);
713 __ jmp(GetExitLabel());
714 }
715
716 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
717
718 private:
719 HInstruction* const instruction_;
720 const Location out_;
721 const Location root_;
722
723 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
724};
725
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100726#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100727#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100728
Roland Levillain4fa13f62015-07-06 18:11:54 +0100729inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700730 switch (cond) {
731 case kCondEQ: return kEqual;
732 case kCondNE: return kNotEqual;
733 case kCondLT: return kLess;
734 case kCondLE: return kLessEqual;
735 case kCondGT: return kGreater;
736 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700737 case kCondB: return kBelow;
738 case kCondBE: return kBelowEqual;
739 case kCondA: return kAbove;
740 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700741 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100742 LOG(FATAL) << "Unreachable";
743 UNREACHABLE();
744}
745
Aart Bike9f37602015-10-09 11:15:55 -0700746// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100747inline Condition X86_64FPCondition(IfCondition cond) {
748 switch (cond) {
749 case kCondEQ: return kEqual;
750 case kCondNE: return kNotEqual;
751 case kCondLT: return kBelow;
752 case kCondLE: return kBelowEqual;
753 case kCondGT: return kAbove;
754 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700755 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100756 };
757 LOG(FATAL) << "Unreachable";
758 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700759}
760
Vladimir Markodc151b22015-10-15 18:02:30 +0100761HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
762 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
763 MethodReference target_method ATTRIBUTE_UNUSED) {
764 switch (desired_dispatch_info.code_ptr_location) {
765 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
766 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
767 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
768 return HInvokeStaticOrDirect::DispatchInfo {
769 desired_dispatch_info.method_load_kind,
770 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
771 desired_dispatch_info.method_load_data,
772 0u
773 };
774 default:
775 return desired_dispatch_info;
776 }
777}
778
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800779void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100780 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800781 // All registers are assumed to be correctly set up.
782
Vladimir Marko58155012015-08-19 12:49:41 +0000783 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
784 switch (invoke->GetMethodLoadKind()) {
785 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
786 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000787 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000788 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000789 break;
790 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000791 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000792 break;
793 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
794 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
795 break;
796 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
797 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
798 method_patches_.emplace_back(invoke->GetTargetMethod());
799 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
800 break;
801 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000802 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
803 invoke->GetDexCacheArrayOffset());
Vladimir Marko58155012015-08-19 12:49:41 +0000804 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000805 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko58155012015-08-19 12:49:41 +0000806 // Bind the label at the end of the "movl" insn.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000807 __ Bind(&pc_relative_dex_cache_patches_.back().label);
Vladimir Marko58155012015-08-19 12:49:41 +0000808 break;
809 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000810 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000811 Register method_reg;
812 CpuRegister reg = temp.AsRegister<CpuRegister>();
813 if (current_method.IsRegister()) {
814 method_reg = current_method.AsRegister<Register>();
815 } else {
816 DCHECK(invoke->GetLocations()->Intrinsified());
817 DCHECK(!current_method.IsValid());
818 method_reg = reg.AsRegister();
819 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
820 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000821 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100822 __ movq(reg,
823 Address(CpuRegister(method_reg),
824 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000825 // temp = temp[index_in_cache]
826 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
827 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
828 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100829 }
Vladimir Marko58155012015-08-19 12:49:41 +0000830 }
831
832 switch (invoke->GetCodePtrLocation()) {
833 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
834 __ call(&frame_entry_label_);
835 break;
836 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
837 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
838 Label* label = &relative_call_patches_.back().label;
839 __ call(label); // Bind to the patch label, override at link time.
840 __ Bind(label); // Bind the label at the end of the "call" insn.
841 break;
842 }
843 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
844 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100845 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
846 LOG(FATAL) << "Unsupported";
847 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000848 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
849 // (callee_method + offset_of_quick_compiled_code)()
850 __ call(Address(callee_method.AsRegister<CpuRegister>(),
851 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
852 kX86_64WordSize).SizeValue()));
853 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000854 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800855
856 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800857}
858
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000859void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
860 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
861 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
862 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000863
864 // Use the calling convention instead of the location of the receiver, as
865 // intrinsics may have put the receiver in a different register. In the intrinsics
866 // slow path, the arguments have been moved to the right place, so here we are
867 // guaranteed that the receiver is the first register of the calling convention.
868 InvokeDexCallingConvention calling_convention;
869 Register receiver = calling_convention.GetRegisterAt(0);
870
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000871 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000872 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000873 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000874 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000875 // Instead of simply (possibly) unpoisoning `temp` here, we should
876 // emit a read barrier for the previous class reference load.
877 // However this is not required in practice, as this is an
878 // intermediate/temporary reference and because the current
879 // concurrent copying collector keeps the from-space memory
880 // intact/accessible until the end of the marking phase (the
881 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000882 __ MaybeUnpoisonHeapReference(temp);
883 // temp = temp->GetMethodAt(method_offset);
884 __ movq(temp, Address(temp, method_offset));
885 // call temp->GetEntryPoint();
886 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
887 kX86_64WordSize).SizeValue()));
888}
889
Vladimir Marko58155012015-08-19 12:49:41 +0000890void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
891 DCHECK(linker_patches->empty());
892 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000893 method_patches_.size() +
894 relative_call_patches_.size() +
895 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000896 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000897 // The label points to the end of the "movl" insn but the literal offset for method
898 // patch needs to point to the embedded constant which occupies the last 4 bytes.
899 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000900 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000901 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000902 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
903 info.target_method.dex_file,
904 info.target_method.dex_method_index));
905 }
906 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000907 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000908 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
909 info.target_method.dex_file,
910 info.target_method.dex_method_index));
911 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000912 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
913 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000914 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
915 &info.target_dex_file,
916 info.label.Position(),
917 info.element_offset));
918 }
919}
920
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100921void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100922 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923}
924
925void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100926 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927}
928
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100929size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
930 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
931 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100932}
933
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100934size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
935 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
936 return kX86_64WordSize;
937}
938
939size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
940 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
941 return kX86_64WordSize;
942}
943
944size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
945 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
946 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100947}
948
Calin Juravle175dc732015-08-25 15:42:32 +0100949void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
950 HInstruction* instruction,
951 uint32_t dex_pc,
952 SlowPathCode* slow_path) {
953 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
954 instruction,
955 dex_pc,
956 slow_path);
957}
958
959void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100960 HInstruction* instruction,
961 uint32_t dex_pc,
962 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100963 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000964 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100965 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100966}
967
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000968static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000969// Use a fake return address register to mimic Quick.
970static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400971CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000972 const X86_64InstructionSetFeatures& isa_features,
973 const CompilerOptions& compiler_options,
974 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000975 : CodeGenerator(graph,
976 kNumberOfCpuRegisters,
977 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000978 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000979 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
980 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000981 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000982 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
983 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100984 compiler_options,
985 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100986 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100987 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000988 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400989 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400990 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000991 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100992 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
993 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000994 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -0400995 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000996 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
997}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100998
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100999InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1000 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001001 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001002 assembler_(codegen->GetAssembler()),
1003 codegen_(codegen) {}
1004
David Brazdil58282f42016-01-14 12:45:10 +00001005void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001006 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001007 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001008
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001009 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001010 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001011}
1012
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001013static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001014 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001015}
David Srbecky9d8606d2015-04-12 09:35:32 +01001016
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001017static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001018 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001019}
1020
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001021void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001022 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001023 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001024 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001025 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001026 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001027
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001028 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001029 __ testq(CpuRegister(RAX), Address(
1030 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001031 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001032 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001033
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001034 if (HasEmptyFrame()) {
1035 return;
1036 }
1037
Nicolas Geoffray98893962015-01-21 12:32:32 +00001038 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001039 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001040 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001041 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001042 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1043 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001044 }
1045 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001046
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001047 int adjust = GetFrameSize() - GetCoreSpillSize();
1048 __ subq(CpuRegister(RSP), Immediate(adjust));
1049 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001050 uint32_t xmm_spill_location = GetFpuSpillStart();
1051 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001052
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001053 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1054 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001055 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1056 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1057 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001058 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001059 }
1060
Mathieu Chartiere401d142015-04-22 13:56:20 -07001061 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001062 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001063}
1064
1065void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001066 __ cfi().RememberState();
1067 if (!HasEmptyFrame()) {
1068 uint32_t xmm_spill_location = GetFpuSpillStart();
1069 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1070 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1071 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1072 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1073 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1074 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1075 }
1076 }
1077
1078 int adjust = GetFrameSize() - GetCoreSpillSize();
1079 __ addq(CpuRegister(RSP), Immediate(adjust));
1080 __ cfi().AdjustCFAOffset(-adjust);
1081
1082 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1083 Register reg = kCoreCalleeSaves[i];
1084 if (allocated_registers_.ContainsCoreRegister(reg)) {
1085 __ popq(CpuRegister(reg));
1086 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1087 __ cfi().Restore(DWARFReg(reg));
1088 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001089 }
1090 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001091 __ ret();
1092 __ cfi().RestoreState();
1093 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094}
1095
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001096void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1097 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098}
1099
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001100Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
1101 switch (load->GetType()) {
1102 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001104 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001105
1106 case Primitive::kPrimInt:
1107 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001109 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001110
1111 case Primitive::kPrimBoolean:
1112 case Primitive::kPrimByte:
1113 case Primitive::kPrimChar:
1114 case Primitive::kPrimShort:
1115 case Primitive::kPrimVoid:
1116 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -07001117 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001118 }
1119
1120 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -07001121 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001122}
1123
1124void CodeGeneratorX86_64::Move(Location destination, Location source) {
1125 if (source.Equals(destination)) {
1126 return;
1127 }
1128 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001129 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001130 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001131 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001133 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001134 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001135 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1136 } else if (source.IsConstant()) {
1137 HConstant* constant = source.GetConstant();
1138 if (constant->IsLongConstant()) {
1139 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1140 } else {
1141 Load32BitValue(dest, GetInt32ValueOf(constant));
1142 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001143 } else {
1144 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001145 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001146 }
1147 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001148 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001149 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001150 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001151 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001152 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1153 } else if (source.IsConstant()) {
1154 HConstant* constant = source.GetConstant();
1155 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1156 if (constant->IsFloatConstant()) {
1157 Load32BitValue(dest, static_cast<int32_t>(value));
1158 } else {
1159 Load64BitValue(dest, value);
1160 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001161 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001162 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001163 } else {
1164 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001165 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001166 }
1167 } else if (destination.IsStackSlot()) {
1168 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001169 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001170 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 } else if (source.IsFpuRegister()) {
1172 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001173 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001174 } else if (source.IsConstant()) {
1175 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001176 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001177 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001178 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001179 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001180 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1181 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001182 }
1183 } else {
1184 DCHECK(destination.IsDoubleStackSlot());
1185 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001186 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001187 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001188 } else if (source.IsFpuRegister()) {
1189 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001190 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001191 } else if (source.IsConstant()) {
1192 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001193 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001194 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001195 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001196 } else {
1197 DCHECK(constant->IsLongConstant());
1198 value = constant->AsLongConstant()->GetValue();
1199 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001200 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001201 } else {
1202 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001203 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1204 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001205 }
1206 }
1207}
1208
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001209void CodeGeneratorX86_64::Move(HInstruction* instruction,
1210 Location location,
1211 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001212 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001213 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -07001214 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001215 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001216 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001217 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001218 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001219 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1220 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001221 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001222 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001223 } else if (location.IsStackSlot()) {
1224 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
1225 } else {
1226 DCHECK(location.IsConstant());
1227 DCHECK_EQ(location.GetConstant(), const_to_move);
1228 }
1229 } else if (const_to_move->IsLongConstant()) {
1230 int64_t value = const_to_move->AsLongConstant()->GetValue();
1231 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04001232 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001233 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -04001234 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001235 } else {
1236 DCHECK(location.IsConstant());
1237 DCHECK_EQ(location.GetConstant(), const_to_move);
1238 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001239 }
Roland Levillain476df552014-10-09 17:51:36 +01001240 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 switch (instruction->GetType()) {
1242 case Primitive::kPrimBoolean:
1243 case Primitive::kPrimByte:
1244 case Primitive::kPrimChar:
1245 case Primitive::kPrimShort:
1246 case Primitive::kPrimInt:
1247 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001249 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
1250 break;
1251
1252 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001253 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +00001254 Move(location,
1255 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001256 break;
1257
1258 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001259 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001261 } else if (instruction->IsTemporary()) {
1262 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
1263 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001265 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001266 switch (instruction->GetType()) {
1267 case Primitive::kPrimBoolean:
1268 case Primitive::kPrimByte:
1269 case Primitive::kPrimChar:
1270 case Primitive::kPrimShort:
1271 case Primitive::kPrimInt:
1272 case Primitive::kPrimNot:
1273 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001274 case Primitive::kPrimFloat:
1275 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001276 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001277 break;
1278
1279 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001280 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001281 }
1282 }
1283}
1284
Calin Juravle175dc732015-08-25 15:42:32 +01001285void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1286 DCHECK(location.IsRegister());
1287 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1288}
1289
Calin Juravlee460d1d2015-09-29 04:52:17 +01001290void CodeGeneratorX86_64::MoveLocation(
1291 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1292 Move(dst, src);
1293}
1294
1295void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1296 if (location.IsRegister()) {
1297 locations->AddTemp(location);
1298 } else {
1299 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1300 }
1301}
1302
David Brazdilfc6a86a2015-06-26 10:33:45 +00001303void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001304 DCHECK(!successor->IsExitBlock());
1305
1306 HBasicBlock* block = got->GetBlock();
1307 HInstruction* previous = got->GetPrevious();
1308
1309 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001310 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001311 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1312 return;
1313 }
1314
1315 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1316 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1317 }
1318 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001319 __ jmp(codegen_->GetLabelOf(successor));
1320 }
1321}
1322
David Brazdilfc6a86a2015-06-26 10:33:45 +00001323void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1324 got->SetLocations(nullptr);
1325}
1326
1327void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1328 HandleGoto(got, got->GetSuccessor());
1329}
1330
1331void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1332 try_boundary->SetLocations(nullptr);
1333}
1334
1335void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1336 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1337 if (!successor->IsExitBlock()) {
1338 HandleGoto(try_boundary, successor);
1339 }
1340}
1341
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001342void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1343 exit->SetLocations(nullptr);
1344}
1345
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001346void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001347}
1348
Mark Mendell152408f2015-12-31 12:28:50 -05001349template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001350void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001351 LabelType* true_label,
1352 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001353 if (cond->IsFPConditionTrueIfNaN()) {
1354 __ j(kUnordered, true_label);
1355 } else if (cond->IsFPConditionFalseIfNaN()) {
1356 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001357 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001358 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001359}
1360
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001361void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001362 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001363
Mark Mendellc4701932015-04-10 13:18:51 -04001364 Location left = locations->InAt(0);
1365 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001366 Primitive::Type type = condition->InputAt(0)->GetType();
1367 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001368 case Primitive::kPrimBoolean:
1369 case Primitive::kPrimByte:
1370 case Primitive::kPrimChar:
1371 case Primitive::kPrimShort:
1372 case Primitive::kPrimInt:
1373 case Primitive::kPrimNot: {
1374 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1375 if (right.IsConstant()) {
1376 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1377 if (value == 0) {
1378 __ testl(left_reg, left_reg);
1379 } else {
1380 __ cmpl(left_reg, Immediate(value));
1381 }
1382 } else if (right.IsStackSlot()) {
1383 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1384 } else {
1385 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1386 }
1387 break;
1388 }
Mark Mendellc4701932015-04-10 13:18:51 -04001389 case Primitive::kPrimLong: {
1390 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1391 if (right.IsConstant()) {
1392 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1393 if (IsInt<32>(value)) {
1394 if (value == 0) {
1395 __ testq(left_reg, left_reg);
1396 } else {
1397 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1398 }
1399 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001400 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001401 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1402 }
1403 } else if (right.IsDoubleStackSlot()) {
1404 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1405 } else {
1406 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1407 }
Mark Mendellc4701932015-04-10 13:18:51 -04001408 break;
1409 }
1410 case Primitive::kPrimFloat: {
1411 if (right.IsFpuRegister()) {
1412 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1413 } else if (right.IsConstant()) {
1414 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1415 codegen_->LiteralFloatAddress(
1416 right.GetConstant()->AsFloatConstant()->GetValue()));
1417 } else {
1418 DCHECK(right.IsStackSlot());
1419 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1420 Address(CpuRegister(RSP), right.GetStackIndex()));
1421 }
Mark Mendellc4701932015-04-10 13:18:51 -04001422 break;
1423 }
1424 case Primitive::kPrimDouble: {
1425 if (right.IsFpuRegister()) {
1426 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1427 } else if (right.IsConstant()) {
1428 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1429 codegen_->LiteralDoubleAddress(
1430 right.GetConstant()->AsDoubleConstant()->GetValue()));
1431 } else {
1432 DCHECK(right.IsDoubleStackSlot());
1433 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1434 Address(CpuRegister(RSP), right.GetStackIndex()));
1435 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001436 break;
1437 }
1438 default:
1439 LOG(FATAL) << "Unexpected condition type " << type;
1440 }
1441}
1442
1443template<class LabelType>
1444void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1445 LabelType* true_target_in,
1446 LabelType* false_target_in) {
1447 // Generated branching requires both targets to be explicit. If either of the
1448 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1449 LabelType fallthrough_target;
1450 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1451 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1452
1453 // Generate the comparison to set the CC.
1454 GenerateCompareTest(condition);
1455
1456 // Now generate the correct jump(s).
1457 Primitive::Type type = condition->InputAt(0)->GetType();
1458 switch (type) {
1459 case Primitive::kPrimLong: {
1460 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1461 break;
1462 }
1463 case Primitive::kPrimFloat: {
1464 GenerateFPJumps(condition, true_target, false_target);
1465 break;
1466 }
1467 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001468 GenerateFPJumps(condition, true_target, false_target);
1469 break;
1470 }
1471 default:
1472 LOG(FATAL) << "Unexpected condition type " << type;
1473 }
1474
David Brazdil0debae72015-11-12 18:37:00 +00001475 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001476 __ jmp(false_target);
1477 }
David Brazdil0debae72015-11-12 18:37:00 +00001478
1479 if (fallthrough_target.IsLinked()) {
1480 __ Bind(&fallthrough_target);
1481 }
Mark Mendellc4701932015-04-10 13:18:51 -04001482}
1483
David Brazdil0debae72015-11-12 18:37:00 +00001484static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1485 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1486 // are set only strictly before `branch`. We can't use the eflags on long
1487 // conditions if they are materialized due to the complex branching.
1488 return cond->IsCondition() &&
1489 cond->GetNext() == branch &&
1490 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1491}
1492
Mark Mendell152408f2015-12-31 12:28:50 -05001493template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001494void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001495 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001496 LabelType* true_target,
1497 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001498 HInstruction* cond = instruction->InputAt(condition_input_index);
1499
1500 if (true_target == nullptr && false_target == nullptr) {
1501 // Nothing to do. The code always falls through.
1502 return;
1503 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001504 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001505 if (cond->AsIntConstant()->IsOne()) {
1506 if (true_target != nullptr) {
1507 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001508 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001509 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001510 DCHECK(cond->AsIntConstant()->IsZero());
1511 if (false_target != nullptr) {
1512 __ jmp(false_target);
1513 }
1514 }
1515 return;
1516 }
1517
1518 // The following code generates these patterns:
1519 // (1) true_target == nullptr && false_target != nullptr
1520 // - opposite condition true => branch to false_target
1521 // (2) true_target != nullptr && false_target == nullptr
1522 // - condition true => branch to true_target
1523 // (3) true_target != nullptr && false_target != nullptr
1524 // - condition true => branch to true_target
1525 // - branch to false_target
1526 if (IsBooleanValueOrMaterializedCondition(cond)) {
1527 if (AreEflagsSetFrom(cond, instruction)) {
1528 if (true_target == nullptr) {
1529 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1530 } else {
1531 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1532 }
1533 } else {
1534 // Materialized condition, compare against 0.
1535 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1536 if (lhs.IsRegister()) {
1537 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1538 } else {
1539 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1540 }
1541 if (true_target == nullptr) {
1542 __ j(kEqual, false_target);
1543 } else {
1544 __ j(kNotEqual, true_target);
1545 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001546 }
1547 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001548 // Condition has not been materialized, use its inputs as the
1549 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001550 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001551
David Brazdil0debae72015-11-12 18:37:00 +00001552 // If this is a long or FP comparison that has been folded into
1553 // the HCondition, generate the comparison directly.
1554 Primitive::Type type = condition->InputAt(0)->GetType();
1555 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1556 GenerateCompareTestAndBranch(condition, true_target, false_target);
1557 return;
1558 }
1559
1560 Location lhs = condition->GetLocations()->InAt(0);
1561 Location rhs = condition->GetLocations()->InAt(1);
1562 if (rhs.IsRegister()) {
1563 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1564 } else if (rhs.IsConstant()) {
1565 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1566 if (constant == 0) {
1567 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001568 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001569 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001570 }
1571 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001572 __ cmpl(lhs.AsRegister<CpuRegister>(),
1573 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1574 }
1575 if (true_target == nullptr) {
1576 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1577 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001578 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001579 }
Dave Allison20dfc792014-06-16 20:44:29 -07001580 }
David Brazdil0debae72015-11-12 18:37:00 +00001581
1582 // If neither branch falls through (case 3), the conditional branch to `true_target`
1583 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1584 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001585 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001586 }
1587}
1588
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001589void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1591 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001592 locations->SetInAt(0, Location::Any());
1593 }
1594}
1595
1596void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001597 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1598 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1599 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1600 nullptr : codegen_->GetLabelOf(true_successor);
1601 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1602 nullptr : codegen_->GetLabelOf(false_successor);
1603 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001604}
1605
1606void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1607 LocationSummary* locations = new (GetGraph()->GetArena())
1608 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001609 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001610 locations->SetInAt(0, Location::Any());
1611 }
1612}
1613
1614void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001615 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001616 GenerateTestAndBranch<Label>(deoptimize,
1617 /* condition_input_index */ 0,
1618 slow_path->GetEntryLabel(),
1619 /* false_target */ nullptr);
1620}
1621
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001622static bool SelectCanUseCMOV(HSelect* select) {
1623 // There are no conditional move instructions for XMMs.
1624 if (Primitive::IsFloatingPointType(select->GetType())) {
1625 return false;
1626 }
1627
1628 // A FP condition doesn't generate the single CC that we need.
1629 HInstruction* condition = select->GetCondition();
1630 if (condition->IsCondition() &&
1631 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1632 return false;
1633 }
1634
1635 // We can generate a CMOV for this Select.
1636 return true;
1637}
1638
David Brazdil74eb1b22015-12-14 11:44:01 +00001639void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1641 if (Primitive::IsFloatingPointType(select->GetType())) {
1642 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001643 // Since we can't use CMOV, there is no need to force 'true' into a register.
1644 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001645 } else {
1646 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001647 if (SelectCanUseCMOV(select)) {
1648 locations->SetInAt(1, Location::RequiresRegister());
1649 } else {
1650 // Since we can't use CMOV, there is no need to force 'true' into a register.
1651 locations->SetInAt(1, Location::Any());
1652 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001653 }
1654 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1655 locations->SetInAt(2, Location::RequiresRegister());
1656 }
1657 locations->SetOut(Location::SameAsFirstInput());
1658}
1659
1660void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1661 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001662 if (SelectCanUseCMOV(select)) {
1663 // If both the condition and the source types are integer, we can generate
1664 // a CMOV to implement Select.
1665 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
1666 CpuRegister value_true = locations->InAt(1).AsRegister<CpuRegister>();
1667 DCHECK(locations->InAt(0).Equals(locations->Out()));
1668
1669 HInstruction* select_condition = select->GetCondition();
1670 Condition cond = kNotEqual;
1671
1672 // Figure out how to test the 'condition'.
1673 if (select_condition->IsCondition()) {
1674 HCondition* condition = select_condition->AsCondition();
1675 if (!condition->IsEmittedAtUseSite()) {
1676 // This was a previously materialized condition.
1677 // Can we use the existing condition code?
1678 if (AreEflagsSetFrom(condition, select)) {
1679 // Materialization was the previous instruction. Condition codes are right.
1680 cond = X86_64IntegerCondition(condition->GetCondition());
1681 } else {
1682 // No, we have to recreate the condition code.
1683 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1684 __ testl(cond_reg, cond_reg);
1685 }
1686 } else {
1687 GenerateCompareTest(condition);
1688 cond = X86_64IntegerCondition(condition->GetCondition());
1689 }
1690 } else {
1691 // Must be a boolean condition, which needs to be compared to 0.
1692 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1693 __ testl(cond_reg, cond_reg);
1694 }
1695
1696 // If the condition is true, overwrite the output, which already contains false.
1697 // Generate the correct sized CMOV.
1698 __ cmov(cond, value_false, value_true, select->GetType() == Primitive::kPrimLong);
1699 } else {
1700 NearLabel false_target;
1701 GenerateTestAndBranch<NearLabel>(select,
1702 /* condition_input_index */ 2,
1703 /* true_target */ nullptr,
1704 &false_target);
1705 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1706 __ Bind(&false_target);
1707 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001708}
1709
David Srbecky0cf44932015-12-09 14:09:59 +00001710void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1711 new (GetGraph()->GetArena()) LocationSummary(info);
1712}
1713
1714void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001715 if (codegen_->HasStackMapAtCurrentPc()) {
1716 // Ensure that we do not collide with the stack map of the previous instruction.
1717 __ nop();
1718 }
David Srbecky0cf44932015-12-09 14:09:59 +00001719 codegen_->RecordPcInfo(info, info->GetDexPc());
1720}
1721
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001722void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1723 local->SetLocations(nullptr);
1724}
1725
1726void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1727 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1728}
1729
1730void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1731 local->SetLocations(nullptr);
1732}
1733
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001734void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001735 // Nothing to do, this is driven by the code generator.
1736}
1737
1738void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001739 LocationSummary* locations =
1740 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001741 switch (store->InputAt(1)->GetType()) {
1742 case Primitive::kPrimBoolean:
1743 case Primitive::kPrimByte:
1744 case Primitive::kPrimChar:
1745 case Primitive::kPrimShort:
1746 case Primitive::kPrimInt:
1747 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001748 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001749 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1750 break;
1751
1752 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001753 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001754 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1755 break;
1756
1757 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001758 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001759 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001760}
1761
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001762void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001763}
1764
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001766 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001767 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001768 // Handle the long/FP comparisons made in instruction simplification.
1769 switch (cond->InputAt(0)->GetType()) {
1770 case Primitive::kPrimLong:
1771 locations->SetInAt(0, Location::RequiresRegister());
1772 locations->SetInAt(1, Location::Any());
1773 break;
1774 case Primitive::kPrimFloat:
1775 case Primitive::kPrimDouble:
1776 locations->SetInAt(0, Location::RequiresFpuRegister());
1777 locations->SetInAt(1, Location::Any());
1778 break;
1779 default:
1780 locations->SetInAt(0, Location::RequiresRegister());
1781 locations->SetInAt(1, Location::Any());
1782 break;
1783 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001784 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001785 locations->SetOut(Location::RequiresRegister());
1786 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001787}
1788
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001790 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001791 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001792 }
Mark Mendellc4701932015-04-10 13:18:51 -04001793
1794 LocationSummary* locations = cond->GetLocations();
1795 Location lhs = locations->InAt(0);
1796 Location rhs = locations->InAt(1);
1797 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001798 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001799
1800 switch (cond->InputAt(0)->GetType()) {
1801 default:
1802 // Integer case.
1803
1804 // Clear output register: setcc only sets the low byte.
1805 __ xorl(reg, reg);
1806
1807 if (rhs.IsRegister()) {
1808 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1809 } else if (rhs.IsConstant()) {
1810 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1811 if (constant == 0) {
1812 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1813 } else {
1814 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1815 }
1816 } else {
1817 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1818 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001819 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001820 return;
1821 case Primitive::kPrimLong:
1822 // Clear output register: setcc only sets the low byte.
1823 __ xorl(reg, reg);
1824
1825 if (rhs.IsRegister()) {
1826 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1827 } else if (rhs.IsConstant()) {
1828 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1829 if (IsInt<32>(value)) {
1830 if (value == 0) {
1831 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1832 } else {
1833 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1834 }
1835 } else {
1836 // Value won't fit in an int.
1837 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1838 }
1839 } else {
1840 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1841 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001842 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001843 return;
1844 case Primitive::kPrimFloat: {
1845 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1846 if (rhs.IsConstant()) {
1847 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1848 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1849 } else if (rhs.IsStackSlot()) {
1850 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1851 } else {
1852 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1853 }
1854 GenerateFPJumps(cond, &true_label, &false_label);
1855 break;
1856 }
1857 case Primitive::kPrimDouble: {
1858 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1859 if (rhs.IsConstant()) {
1860 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1861 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1862 } else if (rhs.IsDoubleStackSlot()) {
1863 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1864 } else {
1865 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1866 }
1867 GenerateFPJumps(cond, &true_label, &false_label);
1868 break;
1869 }
1870 }
1871
1872 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001873 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001874
Roland Levillain4fa13f62015-07-06 18:11:54 +01001875 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001876 __ Bind(&false_label);
1877 __ xorl(reg, reg);
1878 __ jmp(&done_label);
1879
Roland Levillain4fa13f62015-07-06 18:11:54 +01001880 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001881 __ Bind(&true_label);
1882 __ movl(reg, Immediate(1));
1883 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001884}
1885
1886void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001887 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001888}
1889
1890void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001891 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001892}
1893
1894void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001895 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001896}
1897
1898void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001899 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001900}
1901
1902void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001903 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001904}
1905
1906void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001907 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001908}
1909
1910void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001911 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001912}
1913
1914void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001915 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001916}
1917
1918void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001919 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001920}
1921
1922void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001923 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001924}
1925
1926void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001927 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001928}
1929
1930void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001931 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001932}
1933
Aart Bike9f37602015-10-09 11:15:55 -07001934void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001935 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001936}
1937
1938void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001939 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001940}
1941
1942void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001943 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001944}
1945
1946void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001947 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001948}
1949
1950void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001951 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001952}
1953
1954void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001955 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001956}
1957
1958void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001959 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001960}
1961
1962void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001964}
1965
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001966void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001967 LocationSummary* locations =
1968 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001969 switch (compare->InputAt(0)->GetType()) {
1970 case Primitive::kPrimLong: {
1971 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001972 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001973 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1974 break;
1975 }
1976 case Primitive::kPrimFloat:
1977 case Primitive::kPrimDouble: {
1978 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001979 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001980 locations->SetOut(Location::RequiresRegister());
1981 break;
1982 }
1983 default:
1984 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1985 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001986}
1987
1988void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001989 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001990 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001991 Location left = locations->InAt(0);
1992 Location right = locations->InAt(1);
1993
Mark Mendell0c9497d2015-08-21 09:30:05 -04001994 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001995 Primitive::Type type = compare->InputAt(0)->GetType();
1996 switch (type) {
1997 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001998 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1999 if (right.IsConstant()) {
2000 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04002001 if (IsInt<32>(value)) {
2002 if (value == 0) {
2003 __ testq(left_reg, left_reg);
2004 } else {
2005 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
2006 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002007 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04002008 // Value won't fit in an int.
2009 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002010 }
Mark Mendell40741f32015-04-20 22:10:34 -04002011 } else if (right.IsDoubleStackSlot()) {
2012 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002013 } else {
2014 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
2015 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002016 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002017 }
2018 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04002019 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2020 if (right.IsConstant()) {
2021 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2022 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2023 } else if (right.IsStackSlot()) {
2024 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2025 } else {
2026 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2027 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002028 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2029 break;
2030 }
2031 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04002032 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2033 if (right.IsConstant()) {
2034 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2035 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2036 } else if (right.IsDoubleStackSlot()) {
2037 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2038 } else {
2039 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2040 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002041 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2042 break;
2043 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002044 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002045 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002046 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002047 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002048 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00002049 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00002050
Calin Juravle91debbc2014-11-26 19:01:09 +00002051 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002052 __ movl(out, Immediate(1));
2053 __ jmp(&done);
2054
2055 __ Bind(&less);
2056 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002057
2058 __ Bind(&done);
2059}
2060
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002061void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002062 LocationSummary* locations =
2063 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002064 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002065}
2066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002067void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002068 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002069}
2070
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002071void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2072 LocationSummary* locations =
2073 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2074 locations->SetOut(Location::ConstantLocation(constant));
2075}
2076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002077void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002078 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002079}
2080
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002081void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002082 LocationSummary* locations =
2083 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002084 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002085}
2086
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002087void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002088 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002089}
2090
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002091void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2092 LocationSummary* locations =
2093 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2094 locations->SetOut(Location::ConstantLocation(constant));
2095}
2096
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002097void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002098 // Will be generated at use site.
2099}
2100
2101void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2102 LocationSummary* locations =
2103 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2104 locations->SetOut(Location::ConstantLocation(constant));
2105}
2106
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002107void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2108 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002109 // Will be generated at use site.
2110}
2111
Calin Juravle27df7582015-04-17 19:12:31 +01002112void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2113 memory_barrier->SetLocations(nullptr);
2114}
2115
2116void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002117 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002118}
2119
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002120void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2121 ret->SetLocations(nullptr);
2122}
2123
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002124void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002125 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002126}
2127
2128void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002129 LocationSummary* locations =
2130 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002131 switch (ret->InputAt(0)->GetType()) {
2132 case Primitive::kPrimBoolean:
2133 case Primitive::kPrimByte:
2134 case Primitive::kPrimChar:
2135 case Primitive::kPrimShort:
2136 case Primitive::kPrimInt:
2137 case Primitive::kPrimNot:
2138 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002139 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002140 break;
2141
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002142 case Primitive::kPrimFloat:
2143 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002144 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002145 break;
2146
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002147 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002148 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002149 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002150}
2151
2152void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2153 if (kIsDebugBuild) {
2154 switch (ret->InputAt(0)->GetType()) {
2155 case Primitive::kPrimBoolean:
2156 case Primitive::kPrimByte:
2157 case Primitive::kPrimChar:
2158 case Primitive::kPrimShort:
2159 case Primitive::kPrimInt:
2160 case Primitive::kPrimNot:
2161 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002162 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002163 break;
2164
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002165 case Primitive::kPrimFloat:
2166 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002167 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002168 XMM0);
2169 break;
2170
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002171 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002172 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002173 }
2174 }
2175 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002176}
2177
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002178Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2179 switch (type) {
2180 case Primitive::kPrimBoolean:
2181 case Primitive::kPrimByte:
2182 case Primitive::kPrimChar:
2183 case Primitive::kPrimShort:
2184 case Primitive::kPrimInt:
2185 case Primitive::kPrimNot:
2186 case Primitive::kPrimLong:
2187 return Location::RegisterLocation(RAX);
2188
2189 case Primitive::kPrimVoid:
2190 return Location::NoLocation();
2191
2192 case Primitive::kPrimDouble:
2193 case Primitive::kPrimFloat:
2194 return Location::FpuRegisterLocation(XMM0);
2195 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002196
2197 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002198}
2199
2200Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2201 return Location::RegisterLocation(kMethodRegisterArgument);
2202}
2203
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002204Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002205 switch (type) {
2206 case Primitive::kPrimBoolean:
2207 case Primitive::kPrimByte:
2208 case Primitive::kPrimChar:
2209 case Primitive::kPrimShort:
2210 case Primitive::kPrimInt:
2211 case Primitive::kPrimNot: {
2212 uint32_t index = gp_index_++;
2213 stack_index_++;
2214 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002215 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002216 } else {
2217 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2218 }
2219 }
2220
2221 case Primitive::kPrimLong: {
2222 uint32_t index = gp_index_;
2223 stack_index_ += 2;
2224 if (index < calling_convention.GetNumberOfRegisters()) {
2225 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002226 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002227 } else {
2228 gp_index_ += 2;
2229 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2230 }
2231 }
2232
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002233 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002234 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002235 stack_index_++;
2236 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002237 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002238 } else {
2239 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2240 }
2241 }
2242
2243 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002244 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002245 stack_index_ += 2;
2246 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002247 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002248 } else {
2249 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2250 }
2251 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002252
2253 case Primitive::kPrimVoid:
2254 LOG(FATAL) << "Unexpected parameter type " << type;
2255 break;
2256 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002257 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002258}
2259
Calin Juravle175dc732015-08-25 15:42:32 +01002260void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2261 // The trampoline uses the same calling convention as dex calling conventions,
2262 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2263 // the method_idx.
2264 HandleInvoke(invoke);
2265}
2266
2267void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2268 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2269}
2270
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002271void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002272 // Explicit clinit checks triggered by static invokes must have been pruned by
2273 // art::PrepareForRegisterAllocation.
2274 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002275
Mark Mendellfb8d2792015-03-31 22:16:59 -04002276 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002277 if (intrinsic.TryDispatch(invoke)) {
2278 return;
2279 }
2280
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002281 HandleInvoke(invoke);
2282}
2283
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002284static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2285 if (invoke->GetLocations()->Intrinsified()) {
2286 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2287 intrinsic.Dispatch(invoke);
2288 return true;
2289 }
2290 return false;
2291}
2292
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002293void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002294 // Explicit clinit checks triggered by static invokes must have been pruned by
2295 // art::PrepareForRegisterAllocation.
2296 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002297
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002298 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2299 return;
2300 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002301
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002302 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002303 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002304 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002305 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002306}
2307
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002308void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002309 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002310 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002311}
2312
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002313void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002314 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002315 if (intrinsic.TryDispatch(invoke)) {
2316 return;
2317 }
2318
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002319 HandleInvoke(invoke);
2320}
2321
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002322void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002323 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2324 return;
2325 }
2326
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002327 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002328 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002329 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002330}
2331
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002332void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2333 HandleInvoke(invoke);
2334 // Add the hidden argument.
2335 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2336}
2337
2338void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2339 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002340 LocationSummary* locations = invoke->GetLocations();
2341 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2342 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002343 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2344 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002345 Location receiver = locations->InAt(0);
2346 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2347
Roland Levillain0d5a2812015-11-13 10:07:31 +00002348 // Set the hidden argument. This is safe to do this here, as RAX
2349 // won't be modified thereafter, before the `call` instruction.
2350 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002351 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002352
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002353 if (receiver.IsStackSlot()) {
2354 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002355 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002356 __ movl(temp, Address(temp, class_offset));
2357 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002358 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002359 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002360 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002361 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002362 // Instead of simply (possibly) unpoisoning `temp` here, we should
2363 // emit a read barrier for the previous class reference load.
2364 // However this is not required in practice, as this is an
2365 // intermediate/temporary reference and because the current
2366 // concurrent copying collector keeps the from-space memory
2367 // intact/accessible until the end of the marking phase (the
2368 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002369 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002370 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002371 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002372 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002373 __ call(Address(temp,
2374 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002375
2376 DCHECK(!codegen_->IsLeafMethod());
2377 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2378}
2379
Roland Levillain88cb1752014-10-20 16:36:47 +01002380void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2381 LocationSummary* locations =
2382 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2383 switch (neg->GetResultType()) {
2384 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002385 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002386 locations->SetInAt(0, Location::RequiresRegister());
2387 locations->SetOut(Location::SameAsFirstInput());
2388 break;
2389
Roland Levillain88cb1752014-10-20 16:36:47 +01002390 case Primitive::kPrimFloat:
2391 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002392 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002393 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002394 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002395 break;
2396
2397 default:
2398 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2399 }
2400}
2401
2402void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2403 LocationSummary* locations = neg->GetLocations();
2404 Location out = locations->Out();
2405 Location in = locations->InAt(0);
2406 switch (neg->GetResultType()) {
2407 case Primitive::kPrimInt:
2408 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002409 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002410 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002411 break;
2412
2413 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002414 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002415 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002416 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002417 break;
2418
Roland Levillain5368c212014-11-27 15:03:41 +00002419 case Primitive::kPrimFloat: {
2420 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002421 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002422 // Implement float negation with an exclusive or with value
2423 // 0x80000000 (mask for bit 31, representing the sign of a
2424 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002425 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002426 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002427 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002428 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002429
Roland Levillain5368c212014-11-27 15:03:41 +00002430 case Primitive::kPrimDouble: {
2431 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002432 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002433 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002434 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002435 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002436 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002437 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002438 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002439 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002440
2441 default:
2442 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2443 }
2444}
2445
Roland Levillaindff1f282014-11-05 14:15:05 +00002446void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2447 LocationSummary* locations =
2448 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2449 Primitive::Type result_type = conversion->GetResultType();
2450 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002451 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002452
David Brazdilb2bd1c52015-03-25 11:17:37 +00002453 // The Java language does not allow treating boolean as an integral type but
2454 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002455
Roland Levillaindff1f282014-11-05 14:15:05 +00002456 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002457 case Primitive::kPrimByte:
2458 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002459 case Primitive::kPrimBoolean:
2460 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002461 case Primitive::kPrimShort:
2462 case Primitive::kPrimInt:
2463 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002464 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002465 locations->SetInAt(0, Location::Any());
2466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2467 break;
2468
2469 default:
2470 LOG(FATAL) << "Unexpected type conversion from " << input_type
2471 << " to " << result_type;
2472 }
2473 break;
2474
Roland Levillain01a8d712014-11-14 16:27:39 +00002475 case Primitive::kPrimShort:
2476 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002477 case Primitive::kPrimBoolean:
2478 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002479 case Primitive::kPrimByte:
2480 case Primitive::kPrimInt:
2481 case Primitive::kPrimChar:
2482 // Processing a Dex `int-to-short' instruction.
2483 locations->SetInAt(0, Location::Any());
2484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2485 break;
2486
2487 default:
2488 LOG(FATAL) << "Unexpected type conversion from " << input_type
2489 << " to " << result_type;
2490 }
2491 break;
2492
Roland Levillain946e1432014-11-11 17:35:19 +00002493 case Primitive::kPrimInt:
2494 switch (input_type) {
2495 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002496 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002497 locations->SetInAt(0, Location::Any());
2498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2499 break;
2500
2501 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002502 // Processing a Dex `float-to-int' instruction.
2503 locations->SetInAt(0, Location::RequiresFpuRegister());
2504 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002505 break;
2506
Roland Levillain946e1432014-11-11 17:35:19 +00002507 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002508 // Processing a Dex `double-to-int' instruction.
2509 locations->SetInAt(0, Location::RequiresFpuRegister());
2510 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002511 break;
2512
2513 default:
2514 LOG(FATAL) << "Unexpected type conversion from " << input_type
2515 << " to " << result_type;
2516 }
2517 break;
2518
Roland Levillaindff1f282014-11-05 14:15:05 +00002519 case Primitive::kPrimLong:
2520 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002521 case Primitive::kPrimBoolean:
2522 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002523 case Primitive::kPrimByte:
2524 case Primitive::kPrimShort:
2525 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002526 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002527 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002528 // TODO: We would benefit from a (to-be-implemented)
2529 // Location::RegisterOrStackSlot requirement for this input.
2530 locations->SetInAt(0, Location::RequiresRegister());
2531 locations->SetOut(Location::RequiresRegister());
2532 break;
2533
2534 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002535 // Processing a Dex `float-to-long' instruction.
2536 locations->SetInAt(0, Location::RequiresFpuRegister());
2537 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002538 break;
2539
Roland Levillaindff1f282014-11-05 14:15:05 +00002540 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002541 // Processing a Dex `double-to-long' instruction.
2542 locations->SetInAt(0, Location::RequiresFpuRegister());
2543 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002544 break;
2545
2546 default:
2547 LOG(FATAL) << "Unexpected type conversion from " << input_type
2548 << " to " << result_type;
2549 }
2550 break;
2551
Roland Levillain981e4542014-11-14 11:47:14 +00002552 case Primitive::kPrimChar:
2553 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002554 case Primitive::kPrimBoolean:
2555 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002556 case Primitive::kPrimByte:
2557 case Primitive::kPrimShort:
2558 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002559 // Processing a Dex `int-to-char' instruction.
2560 locations->SetInAt(0, Location::Any());
2561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2562 break;
2563
2564 default:
2565 LOG(FATAL) << "Unexpected type conversion from " << input_type
2566 << " to " << result_type;
2567 }
2568 break;
2569
Roland Levillaindff1f282014-11-05 14:15:05 +00002570 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002571 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002572 case Primitive::kPrimBoolean:
2573 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002574 case Primitive::kPrimByte:
2575 case Primitive::kPrimShort:
2576 case Primitive::kPrimInt:
2577 case Primitive::kPrimChar:
2578 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002579 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002580 locations->SetOut(Location::RequiresFpuRegister());
2581 break;
2582
2583 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002584 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002585 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002586 locations->SetOut(Location::RequiresFpuRegister());
2587 break;
2588
Roland Levillaincff13742014-11-17 14:32:17 +00002589 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002590 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002591 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002592 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002593 break;
2594
2595 default:
2596 LOG(FATAL) << "Unexpected type conversion from " << input_type
2597 << " to " << result_type;
2598 };
2599 break;
2600
Roland Levillaindff1f282014-11-05 14:15:05 +00002601 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002602 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002603 case Primitive::kPrimBoolean:
2604 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002605 case Primitive::kPrimByte:
2606 case Primitive::kPrimShort:
2607 case Primitive::kPrimInt:
2608 case Primitive::kPrimChar:
2609 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002610 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002611 locations->SetOut(Location::RequiresFpuRegister());
2612 break;
2613
2614 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002615 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002616 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002617 locations->SetOut(Location::RequiresFpuRegister());
2618 break;
2619
Roland Levillaincff13742014-11-17 14:32:17 +00002620 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002621 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002622 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002623 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002624 break;
2625
2626 default:
2627 LOG(FATAL) << "Unexpected type conversion from " << input_type
2628 << " to " << result_type;
2629 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002630 break;
2631
2632 default:
2633 LOG(FATAL) << "Unexpected type conversion from " << input_type
2634 << " to " << result_type;
2635 }
2636}
2637
2638void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2639 LocationSummary* locations = conversion->GetLocations();
2640 Location out = locations->Out();
2641 Location in = locations->InAt(0);
2642 Primitive::Type result_type = conversion->GetResultType();
2643 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002644 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002645 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002646 case Primitive::kPrimByte:
2647 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002648 case Primitive::kPrimBoolean:
2649 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002650 case Primitive::kPrimShort:
2651 case Primitive::kPrimInt:
2652 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002653 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002654 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002655 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002656 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002657 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002658 Address(CpuRegister(RSP), in.GetStackIndex()));
2659 } else {
2660 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002661 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002662 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2663 }
2664 break;
2665
2666 default:
2667 LOG(FATAL) << "Unexpected type conversion from " << input_type
2668 << " to " << result_type;
2669 }
2670 break;
2671
Roland Levillain01a8d712014-11-14 16:27:39 +00002672 case Primitive::kPrimShort:
2673 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002674 case Primitive::kPrimBoolean:
2675 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002676 case Primitive::kPrimByte:
2677 case Primitive::kPrimInt:
2678 case Primitive::kPrimChar:
2679 // Processing a Dex `int-to-short' instruction.
2680 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002681 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002682 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002683 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002684 Address(CpuRegister(RSP), in.GetStackIndex()));
2685 } else {
2686 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002688 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2689 }
2690 break;
2691
2692 default:
2693 LOG(FATAL) << "Unexpected type conversion from " << input_type
2694 << " to " << result_type;
2695 }
2696 break;
2697
Roland Levillain946e1432014-11-11 17:35:19 +00002698 case Primitive::kPrimInt:
2699 switch (input_type) {
2700 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002701 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002702 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002703 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002704 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002705 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002706 Address(CpuRegister(RSP), in.GetStackIndex()));
2707 } else {
2708 DCHECK(in.IsConstant());
2709 DCHECK(in.GetConstant()->IsLongConstant());
2710 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002711 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002712 }
2713 break;
2714
Roland Levillain3f8f9362014-12-02 17:45:01 +00002715 case Primitive::kPrimFloat: {
2716 // Processing a Dex `float-to-int' instruction.
2717 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2718 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002719 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002720
2721 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002722 // if input >= (float)INT_MAX goto done
2723 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002724 __ j(kAboveEqual, &done);
2725 // if input == NaN goto nan
2726 __ j(kUnordered, &nan);
2727 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002728 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002729 __ jmp(&done);
2730 __ Bind(&nan);
2731 // output = 0
2732 __ xorl(output, output);
2733 __ Bind(&done);
2734 break;
2735 }
2736
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002737 case Primitive::kPrimDouble: {
2738 // Processing a Dex `double-to-int' instruction.
2739 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2740 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002741 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002742
2743 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002744 // if input >= (double)INT_MAX goto done
2745 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002746 __ j(kAboveEqual, &done);
2747 // if input == NaN goto nan
2748 __ j(kUnordered, &nan);
2749 // output = double-to-int-truncate(input)
2750 __ cvttsd2si(output, input);
2751 __ jmp(&done);
2752 __ Bind(&nan);
2753 // output = 0
2754 __ xorl(output, output);
2755 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002756 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002757 }
Roland Levillain946e1432014-11-11 17:35:19 +00002758
2759 default:
2760 LOG(FATAL) << "Unexpected type conversion from " << input_type
2761 << " to " << result_type;
2762 }
2763 break;
2764
Roland Levillaindff1f282014-11-05 14:15:05 +00002765 case Primitive::kPrimLong:
2766 switch (input_type) {
2767 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002768 case Primitive::kPrimBoolean:
2769 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002770 case Primitive::kPrimByte:
2771 case Primitive::kPrimShort:
2772 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002773 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002774 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002775 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002776 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002777 break;
2778
Roland Levillain624279f2014-12-04 11:54:28 +00002779 case Primitive::kPrimFloat: {
2780 // Processing a Dex `float-to-long' instruction.
2781 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2782 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002783 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002784
Mark Mendell92e83bf2015-05-07 11:25:03 -04002785 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002786 // if input >= (float)LONG_MAX goto done
2787 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002788 __ j(kAboveEqual, &done);
2789 // if input == NaN goto nan
2790 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002791 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002792 __ cvttss2si(output, input, true);
2793 __ jmp(&done);
2794 __ Bind(&nan);
2795 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002796 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002797 __ Bind(&done);
2798 break;
2799 }
2800
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002801 case Primitive::kPrimDouble: {
2802 // Processing a Dex `double-to-long' instruction.
2803 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2804 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002805 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002806
Mark Mendell92e83bf2015-05-07 11:25:03 -04002807 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002808 // if input >= (double)LONG_MAX goto done
2809 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002810 __ j(kAboveEqual, &done);
2811 // if input == NaN goto nan
2812 __ j(kUnordered, &nan);
2813 // output = double-to-long-truncate(input)
2814 __ cvttsd2si(output, input, true);
2815 __ jmp(&done);
2816 __ Bind(&nan);
2817 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002818 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002819 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002820 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002821 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002822
2823 default:
2824 LOG(FATAL) << "Unexpected type conversion from " << input_type
2825 << " to " << result_type;
2826 }
2827 break;
2828
Roland Levillain981e4542014-11-14 11:47:14 +00002829 case Primitive::kPrimChar:
2830 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002831 case Primitive::kPrimBoolean:
2832 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002833 case Primitive::kPrimByte:
2834 case Primitive::kPrimShort:
2835 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002836 // Processing a Dex `int-to-char' instruction.
2837 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002838 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002839 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002840 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002841 Address(CpuRegister(RSP), in.GetStackIndex()));
2842 } else {
2843 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002844 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002845 Immediate(static_cast<uint16_t>(
2846 in.GetConstant()->AsIntConstant()->GetValue())));
Roland Levillain981e4542014-11-14 11:47:14 +00002847 }
2848 break;
2849
2850 default:
2851 LOG(FATAL) << "Unexpected type conversion from " << input_type
2852 << " to " << result_type;
2853 }
2854 break;
2855
Roland Levillaindff1f282014-11-05 14:15:05 +00002856 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002857 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002858 case Primitive::kPrimBoolean:
2859 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002860 case Primitive::kPrimByte:
2861 case Primitive::kPrimShort:
2862 case Primitive::kPrimInt:
2863 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002864 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002865 if (in.IsRegister()) {
2866 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2867 } else if (in.IsConstant()) {
2868 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2869 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002870 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002871 } else {
2872 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2873 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2874 }
Roland Levillaincff13742014-11-17 14:32:17 +00002875 break;
2876
2877 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002878 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002879 if (in.IsRegister()) {
2880 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2881 } else if (in.IsConstant()) {
2882 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2883 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002884 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002885 } else {
2886 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2887 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2888 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002889 break;
2890
Roland Levillaincff13742014-11-17 14:32:17 +00002891 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002892 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002893 if (in.IsFpuRegister()) {
2894 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2895 } else if (in.IsConstant()) {
2896 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2897 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002898 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002899 } else {
2900 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2901 Address(CpuRegister(RSP), in.GetStackIndex()));
2902 }
Roland Levillaincff13742014-11-17 14:32:17 +00002903 break;
2904
2905 default:
2906 LOG(FATAL) << "Unexpected type conversion from " << input_type
2907 << " to " << result_type;
2908 };
2909 break;
2910
Roland Levillaindff1f282014-11-05 14:15:05 +00002911 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002912 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002913 case Primitive::kPrimBoolean:
2914 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002915 case Primitive::kPrimByte:
2916 case Primitive::kPrimShort:
2917 case Primitive::kPrimInt:
2918 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002919 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002920 if (in.IsRegister()) {
2921 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2922 } else if (in.IsConstant()) {
2923 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2924 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002925 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002926 } else {
2927 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2928 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2929 }
Roland Levillaincff13742014-11-17 14:32:17 +00002930 break;
2931
2932 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002933 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002934 if (in.IsRegister()) {
2935 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2936 } else if (in.IsConstant()) {
2937 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2938 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002939 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002940 } else {
2941 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2942 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2943 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002944 break;
2945
Roland Levillaincff13742014-11-17 14:32:17 +00002946 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002947 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002948 if (in.IsFpuRegister()) {
2949 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2950 } else if (in.IsConstant()) {
2951 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2952 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002953 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002954 } else {
2955 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2956 Address(CpuRegister(RSP), in.GetStackIndex()));
2957 }
Roland Levillaincff13742014-11-17 14:32:17 +00002958 break;
2959
2960 default:
2961 LOG(FATAL) << "Unexpected type conversion from " << input_type
2962 << " to " << result_type;
2963 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002964 break;
2965
2966 default:
2967 LOG(FATAL) << "Unexpected type conversion from " << input_type
2968 << " to " << result_type;
2969 }
2970}
2971
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002972void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002973 LocationSummary* locations =
2974 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002975 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002976 case Primitive::kPrimInt: {
2977 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002978 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002980 break;
2981 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002983 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002984 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002985 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002986 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002988 break;
2989 }
2990
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002991 case Primitive::kPrimDouble:
2992 case Primitive::kPrimFloat: {
2993 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002994 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002995 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002996 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002997 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002998
2999 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003000 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003001 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003002}
3003
3004void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3005 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003006 Location first = locations->InAt(0);
3007 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003008 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003009
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003010 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003011 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003012 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003013 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3014 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003015 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3016 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003017 } else {
3018 __ leal(out.AsRegister<CpuRegister>(), Address(
3019 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3020 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003021 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003022 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3023 __ addl(out.AsRegister<CpuRegister>(),
3024 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3025 } else {
3026 __ leal(out.AsRegister<CpuRegister>(), Address(
3027 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3028 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003029 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003030 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003031 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003032 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003033 break;
3034 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003035
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003036 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003037 if (second.IsRegister()) {
3038 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3039 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003040 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3041 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003042 } else {
3043 __ leaq(out.AsRegister<CpuRegister>(), Address(
3044 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3045 }
3046 } else {
3047 DCHECK(second.IsConstant());
3048 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3049 int32_t int32_value = Low32Bits(value);
3050 DCHECK_EQ(int32_value, value);
3051 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3052 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3053 } else {
3054 __ leaq(out.AsRegister<CpuRegister>(), Address(
3055 first.AsRegister<CpuRegister>(), int32_value));
3056 }
3057 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003058 break;
3059 }
3060
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003061 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003062 if (second.IsFpuRegister()) {
3063 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3064 } else if (second.IsConstant()) {
3065 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003066 codegen_->LiteralFloatAddress(
3067 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003068 } else {
3069 DCHECK(second.IsStackSlot());
3070 __ addss(first.AsFpuRegister<XmmRegister>(),
3071 Address(CpuRegister(RSP), second.GetStackIndex()));
3072 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003073 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003074 }
3075
3076 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003077 if (second.IsFpuRegister()) {
3078 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3079 } else if (second.IsConstant()) {
3080 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003081 codegen_->LiteralDoubleAddress(
3082 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003083 } else {
3084 DCHECK(second.IsDoubleStackSlot());
3085 __ addsd(first.AsFpuRegister<XmmRegister>(),
3086 Address(CpuRegister(RSP), second.GetStackIndex()));
3087 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003088 break;
3089 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003090
3091 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003092 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003093 }
3094}
3095
3096void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003097 LocationSummary* locations =
3098 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003099 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003100 case Primitive::kPrimInt: {
3101 locations->SetInAt(0, Location::RequiresRegister());
3102 locations->SetInAt(1, Location::Any());
3103 locations->SetOut(Location::SameAsFirstInput());
3104 break;
3105 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003106 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003107 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003108 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003109 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003110 break;
3111 }
Calin Juravle11351682014-10-23 15:38:15 +01003112 case Primitive::kPrimFloat:
3113 case Primitive::kPrimDouble: {
3114 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003115 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003116 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003117 break;
Calin Juravle11351682014-10-23 15:38:15 +01003118 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003119 default:
Calin Juravle11351682014-10-23 15:38:15 +01003120 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003121 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003122}
3123
3124void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3125 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003126 Location first = locations->InAt(0);
3127 Location second = locations->InAt(1);
3128 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003129 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003130 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003131 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003132 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003133 } else if (second.IsConstant()) {
3134 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003135 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003136 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003137 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003138 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003139 break;
3140 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003141 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003142 if (second.IsConstant()) {
3143 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3144 DCHECK(IsInt<32>(value));
3145 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3146 } else {
3147 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3148 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003149 break;
3150 }
3151
Calin Juravle11351682014-10-23 15:38:15 +01003152 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003153 if (second.IsFpuRegister()) {
3154 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3155 } else if (second.IsConstant()) {
3156 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003157 codegen_->LiteralFloatAddress(
3158 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003159 } else {
3160 DCHECK(second.IsStackSlot());
3161 __ subss(first.AsFpuRegister<XmmRegister>(),
3162 Address(CpuRegister(RSP), second.GetStackIndex()));
3163 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003164 break;
Calin Juravle11351682014-10-23 15:38:15 +01003165 }
3166
3167 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003168 if (second.IsFpuRegister()) {
3169 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3170 } else if (second.IsConstant()) {
3171 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003172 codegen_->LiteralDoubleAddress(
3173 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003174 } else {
3175 DCHECK(second.IsDoubleStackSlot());
3176 __ subsd(first.AsFpuRegister<XmmRegister>(),
3177 Address(CpuRegister(RSP), second.GetStackIndex()));
3178 }
Calin Juravle11351682014-10-23 15:38:15 +01003179 break;
3180 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003181
3182 default:
Calin Juravle11351682014-10-23 15:38:15 +01003183 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003184 }
3185}
3186
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3188 LocationSummary* locations =
3189 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3190 switch (mul->GetResultType()) {
3191 case Primitive::kPrimInt: {
3192 locations->SetInAt(0, Location::RequiresRegister());
3193 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003194 if (mul->InputAt(1)->IsIntConstant()) {
3195 // Can use 3 operand multiply.
3196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3197 } else {
3198 locations->SetOut(Location::SameAsFirstInput());
3199 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 break;
3201 }
3202 case Primitive::kPrimLong: {
3203 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003204 locations->SetInAt(1, Location::Any());
3205 if (mul->InputAt(1)->IsLongConstant() &&
3206 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003207 // Can use 3 operand multiply.
3208 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3209 } else {
3210 locations->SetOut(Location::SameAsFirstInput());
3211 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003212 break;
3213 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003214 case Primitive::kPrimFloat:
3215 case Primitive::kPrimDouble: {
3216 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003217 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003218 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003219 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003220 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003221
3222 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003223 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003224 }
3225}
3226
3227void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3228 LocationSummary* locations = mul->GetLocations();
3229 Location first = locations->InAt(0);
3230 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003231 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003232 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003233 case Primitive::kPrimInt:
3234 // The constant may have ended up in a register, so test explicitly to avoid
3235 // problems where the output may not be the same as the first operand.
3236 if (mul->InputAt(1)->IsIntConstant()) {
3237 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3238 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3239 } else if (second.IsRegister()) {
3240 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003241 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003242 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003243 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003244 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003245 __ imull(first.AsRegister<CpuRegister>(),
3246 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003247 }
3248 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003249 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003250 // The constant may have ended up in a register, so test explicitly to avoid
3251 // problems where the output may not be the same as the first operand.
3252 if (mul->InputAt(1)->IsLongConstant()) {
3253 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3254 if (IsInt<32>(value)) {
3255 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3256 Immediate(static_cast<int32_t>(value)));
3257 } else {
3258 // Have to use the constant area.
3259 DCHECK(first.Equals(out));
3260 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3261 }
3262 } else if (second.IsRegister()) {
3263 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003264 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003265 } else {
3266 DCHECK(second.IsDoubleStackSlot());
3267 DCHECK(first.Equals(out));
3268 __ imulq(first.AsRegister<CpuRegister>(),
3269 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003270 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003271 break;
3272 }
3273
Calin Juravleb5bfa962014-10-21 18:02:24 +01003274 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003275 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003276 if (second.IsFpuRegister()) {
3277 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3278 } else if (second.IsConstant()) {
3279 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003280 codegen_->LiteralFloatAddress(
3281 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003282 } else {
3283 DCHECK(second.IsStackSlot());
3284 __ mulss(first.AsFpuRegister<XmmRegister>(),
3285 Address(CpuRegister(RSP), second.GetStackIndex()));
3286 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003287 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003288 }
3289
3290 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003291 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003292 if (second.IsFpuRegister()) {
3293 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3294 } else if (second.IsConstant()) {
3295 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003296 codegen_->LiteralDoubleAddress(
3297 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003298 } else {
3299 DCHECK(second.IsDoubleStackSlot());
3300 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3301 Address(CpuRegister(RSP), second.GetStackIndex()));
3302 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003303 break;
3304 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003305
3306 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003307 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003308 }
3309}
3310
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003311void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3312 uint32_t stack_adjustment, bool is_float) {
3313 if (source.IsStackSlot()) {
3314 DCHECK(is_float);
3315 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3316 } else if (source.IsDoubleStackSlot()) {
3317 DCHECK(!is_float);
3318 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3319 } else {
3320 // Write the value to the temporary location on the stack and load to FP stack.
3321 if (is_float) {
3322 Location stack_temp = Location::StackSlot(temp_offset);
3323 codegen_->Move(stack_temp, source);
3324 __ flds(Address(CpuRegister(RSP), temp_offset));
3325 } else {
3326 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3327 codegen_->Move(stack_temp, source);
3328 __ fldl(Address(CpuRegister(RSP), temp_offset));
3329 }
3330 }
3331}
3332
3333void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3334 Primitive::Type type = rem->GetResultType();
3335 bool is_float = type == Primitive::kPrimFloat;
3336 size_t elem_size = Primitive::ComponentSize(type);
3337 LocationSummary* locations = rem->GetLocations();
3338 Location first = locations->InAt(0);
3339 Location second = locations->InAt(1);
3340 Location out = locations->Out();
3341
3342 // Create stack space for 2 elements.
3343 // TODO: enhance register allocator to ask for stack temporaries.
3344 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3345
3346 // Load the values to the FP stack in reverse order, using temporaries if needed.
3347 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3348 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3349
3350 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003351 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003352 __ Bind(&retry);
3353 __ fprem();
3354
3355 // Move FP status to AX.
3356 __ fstsw();
3357
3358 // And see if the argument reduction is complete. This is signaled by the
3359 // C2 FPU flag bit set to 0.
3360 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3361 __ j(kNotEqual, &retry);
3362
3363 // We have settled on the final value. Retrieve it into an XMM register.
3364 // Store FP top of stack to real stack.
3365 if (is_float) {
3366 __ fsts(Address(CpuRegister(RSP), 0));
3367 } else {
3368 __ fstl(Address(CpuRegister(RSP), 0));
3369 }
3370
3371 // Pop the 2 items from the FP stack.
3372 __ fucompp();
3373
3374 // Load the value from the stack into an XMM register.
3375 DCHECK(out.IsFpuRegister()) << out;
3376 if (is_float) {
3377 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3378 } else {
3379 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3380 }
3381
3382 // And remove the temporary stack space we allocated.
3383 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3384}
3385
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003386void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3387 DCHECK(instruction->IsDiv() || instruction->IsRem());
3388
3389 LocationSummary* locations = instruction->GetLocations();
3390 Location second = locations->InAt(1);
3391 DCHECK(second.IsConstant());
3392
3393 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3394 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003395 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003396
3397 DCHECK(imm == 1 || imm == -1);
3398
3399 switch (instruction->GetResultType()) {
3400 case Primitive::kPrimInt: {
3401 if (instruction->IsRem()) {
3402 __ xorl(output_register, output_register);
3403 } else {
3404 __ movl(output_register, input_register);
3405 if (imm == -1) {
3406 __ negl(output_register);
3407 }
3408 }
3409 break;
3410 }
3411
3412 case Primitive::kPrimLong: {
3413 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003414 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003415 } else {
3416 __ movq(output_register, input_register);
3417 if (imm == -1) {
3418 __ negq(output_register);
3419 }
3420 }
3421 break;
3422 }
3423
3424 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003425 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003426 }
3427}
3428
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003429void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003430 LocationSummary* locations = instruction->GetLocations();
3431 Location second = locations->InAt(1);
3432
3433 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3434 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3435
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003436 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003437 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3438 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003439
3440 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3441
3442 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003443 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003444 __ testl(numerator, numerator);
3445 __ cmov(kGreaterEqual, tmp, numerator);
3446 int shift = CTZ(imm);
3447 __ sarl(tmp, Immediate(shift));
3448
3449 if (imm < 0) {
3450 __ negl(tmp);
3451 }
3452
3453 __ movl(output_register, tmp);
3454 } else {
3455 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3456 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3457
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003458 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003459 __ addq(rdx, numerator);
3460 __ testq(numerator, numerator);
3461 __ cmov(kGreaterEqual, rdx, numerator);
3462 int shift = CTZ(imm);
3463 __ sarq(rdx, Immediate(shift));
3464
3465 if (imm < 0) {
3466 __ negq(rdx);
3467 }
3468
3469 __ movq(output_register, rdx);
3470 }
3471}
3472
3473void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3474 DCHECK(instruction->IsDiv() || instruction->IsRem());
3475
3476 LocationSummary* locations = instruction->GetLocations();
3477 Location second = locations->InAt(1);
3478
3479 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3480 : locations->GetTemp(0).AsRegister<CpuRegister>();
3481 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3482 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3483 : locations->Out().AsRegister<CpuRegister>();
3484 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3485
3486 DCHECK_EQ(RAX, eax.AsRegister());
3487 DCHECK_EQ(RDX, edx.AsRegister());
3488 if (instruction->IsDiv()) {
3489 DCHECK_EQ(RAX, out.AsRegister());
3490 } else {
3491 DCHECK_EQ(RDX, out.AsRegister());
3492 }
3493
3494 int64_t magic;
3495 int shift;
3496
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003497 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 if (instruction->GetResultType() == Primitive::kPrimInt) {
3499 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3500
3501 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3502
3503 __ movl(numerator, eax);
3504
Mark Mendell0c9497d2015-08-21 09:30:05 -04003505 NearLabel no_div;
3506 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003507 __ testl(eax, eax);
3508 __ j(kNotEqual, &no_div);
3509
3510 __ xorl(out, out);
3511 __ jmp(&end);
3512
3513 __ Bind(&no_div);
3514
3515 __ movl(eax, Immediate(magic));
3516 __ imull(numerator);
3517
3518 if (imm > 0 && magic < 0) {
3519 __ addl(edx, numerator);
3520 } else if (imm < 0 && magic > 0) {
3521 __ subl(edx, numerator);
3522 }
3523
3524 if (shift != 0) {
3525 __ sarl(edx, Immediate(shift));
3526 }
3527
3528 __ movl(eax, edx);
3529 __ shrl(edx, Immediate(31));
3530 __ addl(edx, eax);
3531
3532 if (instruction->IsRem()) {
3533 __ movl(eax, numerator);
3534 __ imull(edx, Immediate(imm));
3535 __ subl(eax, edx);
3536 __ movl(edx, eax);
3537 } else {
3538 __ movl(eax, edx);
3539 }
3540 __ Bind(&end);
3541 } else {
3542 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3543
3544 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3545
3546 CpuRegister rax = eax;
3547 CpuRegister rdx = edx;
3548
3549 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3550
3551 // Save the numerator.
3552 __ movq(numerator, rax);
3553
3554 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003555 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556
3557 // RDX:RAX = magic * numerator
3558 __ imulq(numerator);
3559
3560 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003561 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003562 __ addq(rdx, numerator);
3563 } else if (imm < 0 && magic > 0) {
3564 // RDX -= numerator
3565 __ subq(rdx, numerator);
3566 }
3567
3568 // Shift if needed.
3569 if (shift != 0) {
3570 __ sarq(rdx, Immediate(shift));
3571 }
3572
3573 // RDX += 1 if RDX < 0
3574 __ movq(rax, rdx);
3575 __ shrq(rdx, Immediate(63));
3576 __ addq(rdx, rax);
3577
3578 if (instruction->IsRem()) {
3579 __ movq(rax, numerator);
3580
3581 if (IsInt<32>(imm)) {
3582 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3583 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003584 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003585 }
3586
3587 __ subq(rax, rdx);
3588 __ movq(rdx, rax);
3589 } else {
3590 __ movq(rax, rdx);
3591 }
3592 }
3593}
3594
Calin Juravlebacfec32014-11-14 15:54:36 +00003595void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3596 DCHECK(instruction->IsDiv() || instruction->IsRem());
3597 Primitive::Type type = instruction->GetResultType();
3598 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3599
3600 bool is_div = instruction->IsDiv();
3601 LocationSummary* locations = instruction->GetLocations();
3602
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003603 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3604 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003605
Roland Levillain271ab9c2014-11-27 15:23:57 +00003606 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003607 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003608
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003609 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003610 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003611
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003612 if (imm == 0) {
3613 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3614 } else if (imm == 1 || imm == -1) {
3615 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003616 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003617 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003618 } else {
3619 DCHECK(imm <= -2 || imm >= 2);
3620 GenerateDivRemWithAnyConstant(instruction);
3621 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003622 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003623 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003624 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3625 out.AsRegister(), type, is_div);
3626 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003627
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003628 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3629 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3630 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3631 // so it's safe to just use negl instead of more complex comparisons.
3632 if (type == Primitive::kPrimInt) {
3633 __ cmpl(second_reg, Immediate(-1));
3634 __ j(kEqual, slow_path->GetEntryLabel());
3635 // edx:eax <- sign-extended of eax
3636 __ cdq();
3637 // eax = quotient, edx = remainder
3638 __ idivl(second_reg);
3639 } else {
3640 __ cmpq(second_reg, Immediate(-1));
3641 __ j(kEqual, slow_path->GetEntryLabel());
3642 // rdx:rax <- sign-extended of rax
3643 __ cqo();
3644 // rax = quotient, rdx = remainder
3645 __ idivq(second_reg);
3646 }
3647 __ Bind(slow_path->GetExitLabel());
3648 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003649}
3650
Calin Juravle7c4954d2014-10-28 16:57:40 +00003651void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3652 LocationSummary* locations =
3653 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3654 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003655 case Primitive::kPrimInt:
3656 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003657 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003658 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003659 locations->SetOut(Location::SameAsFirstInput());
3660 // Intel uses edx:eax as the dividend.
3661 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003662 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3663 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3664 // output and request another temp.
3665 if (div->InputAt(1)->IsConstant()) {
3666 locations->AddTemp(Location::RequiresRegister());
3667 }
Calin Juravled0d48522014-11-04 16:40:20 +00003668 break;
3669 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003670
Calin Juravle7c4954d2014-10-28 16:57:40 +00003671 case Primitive::kPrimFloat:
3672 case Primitive::kPrimDouble: {
3673 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003674 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003675 locations->SetOut(Location::SameAsFirstInput());
3676 break;
3677 }
3678
3679 default:
3680 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3681 }
3682}
3683
3684void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3685 LocationSummary* locations = div->GetLocations();
3686 Location first = locations->InAt(0);
3687 Location second = locations->InAt(1);
3688 DCHECK(first.Equals(locations->Out()));
3689
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003690 Primitive::Type type = div->GetResultType();
3691 switch (type) {
3692 case Primitive::kPrimInt:
3693 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003694 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003695 break;
3696 }
3697
Calin Juravle7c4954d2014-10-28 16:57:40 +00003698 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003699 if (second.IsFpuRegister()) {
3700 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3701 } else if (second.IsConstant()) {
3702 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003703 codegen_->LiteralFloatAddress(
3704 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003705 } else {
3706 DCHECK(second.IsStackSlot());
3707 __ divss(first.AsFpuRegister<XmmRegister>(),
3708 Address(CpuRegister(RSP), second.GetStackIndex()));
3709 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003710 break;
3711 }
3712
3713 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003714 if (second.IsFpuRegister()) {
3715 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3716 } else if (second.IsConstant()) {
3717 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003718 codegen_->LiteralDoubleAddress(
3719 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003720 } else {
3721 DCHECK(second.IsDoubleStackSlot());
3722 __ divsd(first.AsFpuRegister<XmmRegister>(),
3723 Address(CpuRegister(RSP), second.GetStackIndex()));
3724 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003725 break;
3726 }
3727
3728 default:
3729 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3730 }
3731}
3732
Calin Juravlebacfec32014-11-14 15:54:36 +00003733void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003734 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003735 LocationSummary* locations =
3736 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003737
3738 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003739 case Primitive::kPrimInt:
3740 case Primitive::kPrimLong: {
3741 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003742 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003743 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3744 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003745 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3746 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3747 // output and request another temp.
3748 if (rem->InputAt(1)->IsConstant()) {
3749 locations->AddTemp(Location::RequiresRegister());
3750 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003751 break;
3752 }
3753
3754 case Primitive::kPrimFloat:
3755 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003756 locations->SetInAt(0, Location::Any());
3757 locations->SetInAt(1, Location::Any());
3758 locations->SetOut(Location::RequiresFpuRegister());
3759 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003760 break;
3761 }
3762
3763 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003764 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003765 }
3766}
3767
3768void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3769 Primitive::Type type = rem->GetResultType();
3770 switch (type) {
3771 case Primitive::kPrimInt:
3772 case Primitive::kPrimLong: {
3773 GenerateDivRemIntegral(rem);
3774 break;
3775 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003776 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003777 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003778 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003779 break;
3780 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003781 default:
3782 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3783 }
3784}
3785
Calin Juravled0d48522014-11-04 16:40:20 +00003786void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003787 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3788 ? LocationSummary::kCallOnSlowPath
3789 : LocationSummary::kNoCall;
3790 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003791 locations->SetInAt(0, Location::Any());
3792 if (instruction->HasUses()) {
3793 locations->SetOut(Location::SameAsFirstInput());
3794 }
3795}
3796
3797void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003798 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003799 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3800 codegen_->AddSlowPath(slow_path);
3801
3802 LocationSummary* locations = instruction->GetLocations();
3803 Location value = locations->InAt(0);
3804
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003805 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003806 case Primitive::kPrimByte:
3807 case Primitive::kPrimChar:
3808 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003809 case Primitive::kPrimInt: {
3810 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003811 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003812 __ j(kEqual, slow_path->GetEntryLabel());
3813 } else if (value.IsStackSlot()) {
3814 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3815 __ j(kEqual, slow_path->GetEntryLabel());
3816 } else {
3817 DCHECK(value.IsConstant()) << value;
3818 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3819 __ jmp(slow_path->GetEntryLabel());
3820 }
3821 }
3822 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003823 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003824 case Primitive::kPrimLong: {
3825 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003826 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003827 __ j(kEqual, slow_path->GetEntryLabel());
3828 } else if (value.IsDoubleStackSlot()) {
3829 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3830 __ j(kEqual, slow_path->GetEntryLabel());
3831 } else {
3832 DCHECK(value.IsConstant()) << value;
3833 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3834 __ jmp(slow_path->GetEntryLabel());
3835 }
3836 }
3837 break;
3838 }
3839 default:
3840 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003841 }
Calin Juravled0d48522014-11-04 16:40:20 +00003842}
3843
Calin Juravle9aec02f2014-11-18 23:06:35 +00003844void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3845 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3846
3847 LocationSummary* locations =
3848 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3849
3850 switch (op->GetResultType()) {
3851 case Primitive::kPrimInt:
3852 case Primitive::kPrimLong: {
3853 locations->SetInAt(0, Location::RequiresRegister());
3854 // The shift count needs to be in CL.
3855 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3856 locations->SetOut(Location::SameAsFirstInput());
3857 break;
3858 }
3859 default:
3860 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3861 }
3862}
3863
3864void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3865 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3866
3867 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003869 Location second = locations->InAt(1);
3870
3871 switch (op->GetResultType()) {
3872 case Primitive::kPrimInt: {
3873 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003874 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003875 if (op->IsShl()) {
3876 __ shll(first_reg, second_reg);
3877 } else if (op->IsShr()) {
3878 __ sarl(first_reg, second_reg);
3879 } else {
3880 __ shrl(first_reg, second_reg);
3881 }
3882 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003883 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003884 if (op->IsShl()) {
3885 __ shll(first_reg, imm);
3886 } else if (op->IsShr()) {
3887 __ sarl(first_reg, imm);
3888 } else {
3889 __ shrl(first_reg, imm);
3890 }
3891 }
3892 break;
3893 }
3894 case Primitive::kPrimLong: {
3895 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003896 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003897 if (op->IsShl()) {
3898 __ shlq(first_reg, second_reg);
3899 } else if (op->IsShr()) {
3900 __ sarq(first_reg, second_reg);
3901 } else {
3902 __ shrq(first_reg, second_reg);
3903 }
3904 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003905 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003906 if (op->IsShl()) {
3907 __ shlq(first_reg, imm);
3908 } else if (op->IsShr()) {
3909 __ sarq(first_reg, imm);
3910 } else {
3911 __ shrq(first_reg, imm);
3912 }
3913 }
3914 break;
3915 }
3916 default:
3917 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003918 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003919 }
3920}
3921
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003922void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3923 LocationSummary* locations =
3924 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3925
3926 switch (ror->GetResultType()) {
3927 case Primitive::kPrimInt:
3928 case Primitive::kPrimLong: {
3929 locations->SetInAt(0, Location::RequiresRegister());
3930 // The shift count needs to be in CL (unless it is a constant).
3931 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3932 locations->SetOut(Location::SameAsFirstInput());
3933 break;
3934 }
3935 default:
3936 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3937 UNREACHABLE();
3938 }
3939}
3940
3941void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3942 LocationSummary* locations = ror->GetLocations();
3943 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3944 Location second = locations->InAt(1);
3945
3946 switch (ror->GetResultType()) {
3947 case Primitive::kPrimInt:
3948 if (second.IsRegister()) {
3949 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3950 __ rorl(first_reg, second_reg);
3951 } else {
3952 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3953 __ rorl(first_reg, imm);
3954 }
3955 break;
3956 case Primitive::kPrimLong:
3957 if (second.IsRegister()) {
3958 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3959 __ rorq(first_reg, second_reg);
3960 } else {
3961 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
3962 __ rorq(first_reg, imm);
3963 }
3964 break;
3965 default:
3966 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3967 UNREACHABLE();
3968 }
3969}
3970
Calin Juravle9aec02f2014-11-18 23:06:35 +00003971void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3972 HandleShift(shl);
3973}
3974
3975void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3976 HandleShift(shl);
3977}
3978
3979void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3980 HandleShift(shr);
3981}
3982
3983void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3984 HandleShift(shr);
3985}
3986
3987void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3988 HandleShift(ushr);
3989}
3990
3991void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3992 HandleShift(ushr);
3993}
3994
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003995void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003996 LocationSummary* locations =
3997 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003998 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003999 if (instruction->IsStringAlloc()) {
4000 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4001 } else {
4002 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4003 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4004 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004005 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004006}
4007
4008void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004009 // Note: if heap poisoning is enabled, the entry point takes cares
4010 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004011 if (instruction->IsStringAlloc()) {
4012 // String is allocated through StringFactory. Call NewEmptyString entry point.
4013 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
4014 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
4015 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4016 __ call(Address(temp, code_offset.SizeValue()));
4017 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4018 } else {
4019 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4020 instruction,
4021 instruction->GetDexPc(),
4022 nullptr);
4023 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4024 DCHECK(!codegen_->IsLeafMethod());
4025 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004026}
4027
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004028void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4029 LocationSummary* locations =
4030 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4031 InvokeRuntimeCallingConvention calling_convention;
4032 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004033 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004034 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004035 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004036}
4037
4038void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
4039 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004040 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
4041 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004042 // Note: if heap poisoning is enabled, the entry point takes cares
4043 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004044 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4045 instruction,
4046 instruction->GetDexPc(),
4047 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004048 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004049
4050 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004051}
4052
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004053void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004054 LocationSummary* locations =
4055 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004056 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4057 if (location.IsStackSlot()) {
4058 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4059 } else if (location.IsDoubleStackSlot()) {
4060 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4061 }
4062 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004063}
4064
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004065void InstructionCodeGeneratorX86_64::VisitParameterValue(
4066 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004067 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004068}
4069
4070void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4071 LocationSummary* locations =
4072 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4073 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4074}
4075
4076void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4077 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4078 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004079}
4080
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004081void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4082 LocationSummary* locations =
4083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4084 locations->SetInAt(0, Location::RequiresRegister());
4085 locations->SetOut(Location::RequiresRegister());
4086}
4087
4088void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4089 LocationSummary* locations = instruction->GetLocations();
4090 uint32_t method_offset = 0;
4091 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
4092 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4093 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
4094 } else {
4095 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4096 instruction->GetIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
4097 }
4098 __ movq(locations->Out().AsRegister<CpuRegister>(),
4099 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
4100}
4101
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004102void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004103 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004104 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004105 locations->SetInAt(0, Location::RequiresRegister());
4106 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004107}
4108
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004109void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4110 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004111 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4112 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004113 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004114 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004115 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004116 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004117 break;
4118
4119 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004120 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004121 break;
4122
4123 default:
4124 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4125 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004126}
4127
David Brazdil66d126e2015-04-03 16:02:44 +01004128void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4129 LocationSummary* locations =
4130 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4131 locations->SetInAt(0, Location::RequiresRegister());
4132 locations->SetOut(Location::SameAsFirstInput());
4133}
4134
4135void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004136 LocationSummary* locations = bool_not->GetLocations();
4137 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4138 locations->Out().AsRegister<CpuRegister>().AsRegister());
4139 Location out = locations->Out();
4140 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4141}
4142
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004143void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004144 LocationSummary* locations =
4145 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004146 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4147 locations->SetInAt(i, Location::Any());
4148 }
4149 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004150}
4151
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004152void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004153 LOG(FATAL) << "Unimplemented";
4154}
4155
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004156void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004157 /*
4158 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004159 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004160 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4161 */
4162 switch (kind) {
4163 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004164 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004165 break;
4166 }
4167 case MemBarrierKind::kAnyStore:
4168 case MemBarrierKind::kLoadAny:
4169 case MemBarrierKind::kStoreStore: {
4170 // nop
4171 break;
4172 }
4173 default:
4174 LOG(FATAL) << "Unexpected memory barier " << kind;
4175 }
4176}
4177
4178void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4179 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4180
Roland Levillain0d5a2812015-11-13 10:07:31 +00004181 bool object_field_get_with_read_barrier =
4182 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004183 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004184 new (GetGraph()->GetArena()) LocationSummary(instruction,
4185 object_field_get_with_read_barrier ?
4186 LocationSummary::kCallOnSlowPath :
4187 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004188 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004189 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4190 locations->SetOut(Location::RequiresFpuRegister());
4191 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004192 // The output overlaps for an object field get when read barriers
4193 // are enabled: we do not want the move to overwrite the object's
4194 // location, as we need it to emit the read barrier.
4195 locations->SetOut(
4196 Location::RequiresRegister(),
4197 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004198 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004199 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4200 // We need a temporary register for the read barrier marking slow
4201 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4202 locations->AddTemp(Location::RequiresRegister());
4203 }
Calin Juravle52c48962014-12-16 17:02:57 +00004204}
4205
4206void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4207 const FieldInfo& field_info) {
4208 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4209
4210 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004211 Location base_loc = locations->InAt(0);
4212 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004213 Location out = locations->Out();
4214 bool is_volatile = field_info.IsVolatile();
4215 Primitive::Type field_type = field_info.GetFieldType();
4216 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4217
4218 switch (field_type) {
4219 case Primitive::kPrimBoolean: {
4220 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4221 break;
4222 }
4223
4224 case Primitive::kPrimByte: {
4225 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4226 break;
4227 }
4228
4229 case Primitive::kPrimShort: {
4230 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4231 break;
4232 }
4233
4234 case Primitive::kPrimChar: {
4235 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4236 break;
4237 }
4238
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004239 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004240 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4241 break;
4242 }
4243
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004244 case Primitive::kPrimNot: {
4245 // /* HeapReference<Object> */ out = *(base + offset)
4246 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4247 Location temp_loc = locations->GetTemp(0);
4248 // Note that a potential implicit null check is handled in this
4249 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4250 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4251 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4252 if (is_volatile) {
4253 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4254 }
4255 } else {
4256 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4257 codegen_->MaybeRecordImplicitNullCheck(instruction);
4258 if (is_volatile) {
4259 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4260 }
4261 // If read barriers are enabled, emit read barriers other than
4262 // Baker's using a slow path (and also unpoison the loaded
4263 // reference, if heap poisoning is enabled).
4264 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4265 }
4266 break;
4267 }
4268
Calin Juravle52c48962014-12-16 17:02:57 +00004269 case Primitive::kPrimLong: {
4270 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4271 break;
4272 }
4273
4274 case Primitive::kPrimFloat: {
4275 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4276 break;
4277 }
4278
4279 case Primitive::kPrimDouble: {
4280 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4281 break;
4282 }
4283
4284 case Primitive::kPrimVoid:
4285 LOG(FATAL) << "Unreachable type " << field_type;
4286 UNREACHABLE();
4287 }
4288
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004289 if (field_type == Primitive::kPrimNot) {
4290 // Potential implicit null checks, in the case of reference
4291 // fields, are handled in the previous switch statement.
4292 } else {
4293 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004294 }
Roland Levillain4d027112015-07-01 15:41:14 +01004295
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004296 if (is_volatile) {
4297 if (field_type == Primitive::kPrimNot) {
4298 // Memory barriers, in the case of references, are also handled
4299 // in the previous switch statement.
4300 } else {
4301 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4302 }
Roland Levillain4d027112015-07-01 15:41:14 +01004303 }
Calin Juravle52c48962014-12-16 17:02:57 +00004304}
4305
4306void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4307 const FieldInfo& field_info) {
4308 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4309
4310 LocationSummary* locations =
4311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004312 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004313 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004314 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004315 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004316
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004317 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004318 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004319 if (is_volatile) {
4320 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4321 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4322 } else {
4323 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4324 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004325 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004326 if (is_volatile) {
4327 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4328 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4329 } else {
4330 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4331 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004332 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004333 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004334 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004335 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004336 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004337 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4338 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004339 locations->AddTemp(Location::RequiresRegister());
4340 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341}
4342
Calin Juravle52c48962014-12-16 17:02:57 +00004343void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004344 const FieldInfo& field_info,
4345 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004346 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4347
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004348 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004349 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4350 Location value = locations->InAt(1);
4351 bool is_volatile = field_info.IsVolatile();
4352 Primitive::Type field_type = field_info.GetFieldType();
4353 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4354
4355 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004356 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004357 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004358
Mark Mendellea5af682015-10-22 17:35:49 -04004359 bool maybe_record_implicit_null_check_done = false;
4360
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004361 switch (field_type) {
4362 case Primitive::kPrimBoolean:
4363 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004364 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004365 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004366 __ movb(Address(base, offset), Immediate(v));
4367 } else {
4368 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4369 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004370 break;
4371 }
4372
4373 case Primitive::kPrimShort:
4374 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004375 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004376 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004377 __ movw(Address(base, offset), Immediate(v));
4378 } else {
4379 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4380 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004381 break;
4382 }
4383
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004384 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004385 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004386 if (value.IsConstant()) {
4387 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004388 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4389 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4390 // Note: if heap poisoning is enabled, no need to poison
4391 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004392 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004393 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004394 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4395 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4396 __ movl(temp, value.AsRegister<CpuRegister>());
4397 __ PoisonHeapReference(temp);
4398 __ movl(Address(base, offset), temp);
4399 } else {
4400 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4401 }
Mark Mendell40741f32015-04-20 22:10:34 -04004402 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004403 break;
4404 }
4405
4406 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004407 if (value.IsConstant()) {
4408 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004409 codegen_->MoveInt64ToAddress(Address(base, offset),
4410 Address(base, offset + sizeof(int32_t)),
4411 v,
4412 instruction);
4413 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004414 } else {
4415 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4416 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004417 break;
4418 }
4419
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004420 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004421 if (value.IsConstant()) {
4422 int32_t v =
4423 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4424 __ movl(Address(base, offset), Immediate(v));
4425 } else {
4426 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4427 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004428 break;
4429 }
4430
4431 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004432 if (value.IsConstant()) {
4433 int64_t v =
4434 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4435 codegen_->MoveInt64ToAddress(Address(base, offset),
4436 Address(base, offset + sizeof(int32_t)),
4437 v,
4438 instruction);
4439 maybe_record_implicit_null_check_done = true;
4440 } else {
4441 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4442 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004443 break;
4444 }
4445
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004446 case Primitive::kPrimVoid:
4447 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004448 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004449 }
Calin Juravle52c48962014-12-16 17:02:57 +00004450
Mark Mendellea5af682015-10-22 17:35:49 -04004451 if (!maybe_record_implicit_null_check_done) {
4452 codegen_->MaybeRecordImplicitNullCheck(instruction);
4453 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004454
4455 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4456 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4457 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004458 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004459 }
4460
Calin Juravle52c48962014-12-16 17:02:57 +00004461 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004462 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004463 }
4464}
4465
4466void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4467 HandleFieldSet(instruction, instruction->GetFieldInfo());
4468}
4469
4470void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004471 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004472}
4473
4474void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004475 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476}
4477
4478void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004479 HandleFieldGet(instruction, instruction->GetFieldInfo());
4480}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004481
Calin Juravle52c48962014-12-16 17:02:57 +00004482void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4483 HandleFieldGet(instruction);
4484}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004485
Calin Juravle52c48962014-12-16 17:02:57 +00004486void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4487 HandleFieldGet(instruction, instruction->GetFieldInfo());
4488}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489
Calin Juravle52c48962014-12-16 17:02:57 +00004490void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4491 HandleFieldSet(instruction, instruction->GetFieldInfo());
4492}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004493
Calin Juravle52c48962014-12-16 17:02:57 +00004494void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004495 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004496}
4497
Calin Juravlee460d1d2015-09-29 04:52:17 +01004498void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4499 HUnresolvedInstanceFieldGet* instruction) {
4500 FieldAccessCallingConventionX86_64 calling_convention;
4501 codegen_->CreateUnresolvedFieldLocationSummary(
4502 instruction, instruction->GetFieldType(), calling_convention);
4503}
4504
4505void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4506 HUnresolvedInstanceFieldGet* instruction) {
4507 FieldAccessCallingConventionX86_64 calling_convention;
4508 codegen_->GenerateUnresolvedFieldAccess(instruction,
4509 instruction->GetFieldType(),
4510 instruction->GetFieldIndex(),
4511 instruction->GetDexPc(),
4512 calling_convention);
4513}
4514
4515void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4516 HUnresolvedInstanceFieldSet* instruction) {
4517 FieldAccessCallingConventionX86_64 calling_convention;
4518 codegen_->CreateUnresolvedFieldLocationSummary(
4519 instruction, instruction->GetFieldType(), calling_convention);
4520}
4521
4522void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4523 HUnresolvedInstanceFieldSet* instruction) {
4524 FieldAccessCallingConventionX86_64 calling_convention;
4525 codegen_->GenerateUnresolvedFieldAccess(instruction,
4526 instruction->GetFieldType(),
4527 instruction->GetFieldIndex(),
4528 instruction->GetDexPc(),
4529 calling_convention);
4530}
4531
4532void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4533 HUnresolvedStaticFieldGet* instruction) {
4534 FieldAccessCallingConventionX86_64 calling_convention;
4535 codegen_->CreateUnresolvedFieldLocationSummary(
4536 instruction, instruction->GetFieldType(), calling_convention);
4537}
4538
4539void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4540 HUnresolvedStaticFieldGet* instruction) {
4541 FieldAccessCallingConventionX86_64 calling_convention;
4542 codegen_->GenerateUnresolvedFieldAccess(instruction,
4543 instruction->GetFieldType(),
4544 instruction->GetFieldIndex(),
4545 instruction->GetDexPc(),
4546 calling_convention);
4547}
4548
4549void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4550 HUnresolvedStaticFieldSet* instruction) {
4551 FieldAccessCallingConventionX86_64 calling_convention;
4552 codegen_->CreateUnresolvedFieldLocationSummary(
4553 instruction, instruction->GetFieldType(), calling_convention);
4554}
4555
4556void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4557 HUnresolvedStaticFieldSet* instruction) {
4558 FieldAccessCallingConventionX86_64 calling_convention;
4559 codegen_->GenerateUnresolvedFieldAccess(instruction,
4560 instruction->GetFieldType(),
4561 instruction->GetFieldIndex(),
4562 instruction->GetDexPc(),
4563 calling_convention);
4564}
4565
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004566void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004567 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4568 ? LocationSummary::kCallOnSlowPath
4569 : LocationSummary::kNoCall;
4570 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4571 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004572 ? Location::RequiresRegister()
4573 : Location::Any();
4574 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004575 if (instruction->HasUses()) {
4576 locations->SetOut(Location::SameAsFirstInput());
4577 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004578}
4579
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004580void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004581 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4582 return;
4583 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004584 LocationSummary* locations = instruction->GetLocations();
4585 Location obj = locations->InAt(0);
4586
4587 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4588 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4589}
4590
4591void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004592 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004593 codegen_->AddSlowPath(slow_path);
4594
4595 LocationSummary* locations = instruction->GetLocations();
4596 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004597
4598 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004599 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004600 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004601 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004602 } else {
4603 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004604 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004605 __ jmp(slow_path->GetEntryLabel());
4606 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607 }
4608 __ j(kEqual, slow_path->GetEntryLabel());
4609}
4610
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004611void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004612 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004613 GenerateImplicitNullCheck(instruction);
4614 } else {
4615 GenerateExplicitNullCheck(instruction);
4616 }
4617}
4618
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004619void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004620 bool object_array_get_with_read_barrier =
4621 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004622 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004623 new (GetGraph()->GetArena()) LocationSummary(instruction,
4624 object_array_get_with_read_barrier ?
4625 LocationSummary::kCallOnSlowPath :
4626 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004627 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004628 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004629 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4630 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4631 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004632 // The output overlaps for an object array get when read barriers
4633 // are enabled: we do not want the move to overwrite the array's
4634 // location, as we need it to emit the read barrier.
4635 locations->SetOut(
4636 Location::RequiresRegister(),
4637 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004638 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004639 // We need a temporary register for the read barrier marking slow
4640 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4641 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4642 locations->AddTemp(Location::RequiresRegister());
4643 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004644}
4645
4646void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4647 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004648 Location obj_loc = locations->InAt(0);
4649 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004650 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004651 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004652
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004653 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004654 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004655 case Primitive::kPrimBoolean: {
4656 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004657 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 if (index.IsConstant()) {
4659 __ movzxb(out, Address(obj,
4660 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4661 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004662 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004663 }
4664 break;
4665 }
4666
4667 case Primitive::kPrimByte: {
4668 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004669 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004670 if (index.IsConstant()) {
4671 __ movsxb(out, Address(obj,
4672 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4673 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004674 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004675 }
4676 break;
4677 }
4678
4679 case Primitive::kPrimShort: {
4680 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004681 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004682 if (index.IsConstant()) {
4683 __ movsxw(out, Address(obj,
4684 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4685 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004686 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004687 }
4688 break;
4689 }
4690
4691 case Primitive::kPrimChar: {
4692 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004693 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004694 if (index.IsConstant()) {
4695 __ movzxw(out, Address(obj,
4696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4697 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004698 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004699 }
4700 break;
4701 }
4702
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004703 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004705 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706 if (index.IsConstant()) {
4707 __ movl(out, Address(obj,
4708 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4709 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004710 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 }
4712 break;
4713 }
4714
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004715 case Primitive::kPrimNot: {
4716 static_assert(
4717 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4718 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4719 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4720 // /* HeapReference<Object> */ out =
4721 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4722 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4723 Location temp = locations->GetTemp(0);
4724 // Note that a potential implicit null check is handled in this
4725 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4726 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4727 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4728 } else {
4729 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4730 if (index.IsConstant()) {
4731 uint32_t offset =
4732 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4733 __ movl(out, Address(obj, offset));
4734 codegen_->MaybeRecordImplicitNullCheck(instruction);
4735 // If read barriers are enabled, emit read barriers other than
4736 // Baker's using a slow path (and also unpoison the loaded
4737 // reference, if heap poisoning is enabled).
4738 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4739 } else {
4740 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4741 codegen_->MaybeRecordImplicitNullCheck(instruction);
4742 // If read barriers are enabled, emit read barriers other than
4743 // Baker's using a slow path (and also unpoison the loaded
4744 // reference, if heap poisoning is enabled).
4745 codegen_->MaybeGenerateReadBarrierSlow(
4746 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4747 }
4748 }
4749 break;
4750 }
4751
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004752 case Primitive::kPrimLong: {
4753 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004754 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004755 if (index.IsConstant()) {
4756 __ movq(out, Address(obj,
4757 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4758 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004759 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004760 }
4761 break;
4762 }
4763
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004764 case Primitive::kPrimFloat: {
4765 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004766 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004767 if (index.IsConstant()) {
4768 __ movss(out, Address(obj,
4769 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4770 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004771 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004772 }
4773 break;
4774 }
4775
4776 case Primitive::kPrimDouble: {
4777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004778 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004779 if (index.IsConstant()) {
4780 __ movsd(out, Address(obj,
4781 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4782 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004783 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004784 }
4785 break;
4786 }
4787
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004788 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004789 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004790 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004791 }
Roland Levillain4d027112015-07-01 15:41:14 +01004792
4793 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004794 // Potential implicit null checks, in the case of reference
4795 // arrays, are handled in the previous switch statement.
4796 } else {
4797 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004798 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004799}
4800
4801void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004802 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004803
4804 bool needs_write_barrier =
4805 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004806 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004807 bool object_array_set_with_read_barrier =
4808 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004809
Nicolas Geoffray39468442014-09-02 15:17:15 +01004810 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004811 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004812 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004813 LocationSummary::kCallOnSlowPath :
4814 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004815
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004816 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004817 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4818 if (Primitive::IsFloatingPointType(value_type)) {
4819 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004820 } else {
4821 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4822 }
4823
4824 if (needs_write_barrier) {
4825 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004826
4827 // This first temporary register is possibly used for heap
4828 // reference poisoning and/or read barrier emission too.
4829 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004830 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004831 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004832}
4833
4834void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4835 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004836 Location array_loc = locations->InAt(0);
4837 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004838 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004839 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004840 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004841 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004842 bool needs_write_barrier =
4843 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004844 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4845 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4846 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004847
4848 switch (value_type) {
4849 case Primitive::kPrimBoolean:
4850 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004851 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4852 Address address = index.IsConstant()
4853 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4854 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4855 if (value.IsRegister()) {
4856 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004857 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004858 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004859 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004860 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004861 break;
4862 }
4863
4864 case Primitive::kPrimShort:
4865 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004866 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4867 Address address = index.IsConstant()
4868 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4869 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4870 if (value.IsRegister()) {
4871 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004872 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004873 DCHECK(value.IsConstant()) << value;
4874 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004875 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004876 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877 break;
4878 }
4879
4880 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004881 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4882 Address address = index.IsConstant()
4883 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4884 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004885
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004886 if (!value.IsRegister()) {
4887 // Just setting null.
4888 DCHECK(instruction->InputAt(2)->IsNullConstant());
4889 DCHECK(value.IsConstant()) << value;
4890 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004891 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004892 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004893 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004894 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004895 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004896
4897 DCHECK(needs_write_barrier);
4898 CpuRegister register_value = value.AsRegister<CpuRegister>();
4899 NearLabel done, not_null, do_put;
4900 SlowPathCode* slow_path = nullptr;
4901 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004902 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004903 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4904 codegen_->AddSlowPath(slow_path);
4905 if (instruction->GetValueCanBeNull()) {
4906 __ testl(register_value, register_value);
4907 __ j(kNotEqual, &not_null);
4908 __ movl(address, Immediate(0));
4909 codegen_->MaybeRecordImplicitNullCheck(instruction);
4910 __ jmp(&done);
4911 __ Bind(&not_null);
4912 }
4913
Roland Levillain0d5a2812015-11-13 10:07:31 +00004914 if (kEmitCompilerReadBarrier) {
4915 // When read barriers are enabled, the type checking
4916 // instrumentation requires two read barriers:
4917 //
4918 // __ movl(temp2, temp);
4919 // // /* HeapReference<Class> */ temp = temp->component_type_
4920 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004921 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004922 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4923 //
4924 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4925 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004926 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004927 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4928 //
4929 // __ cmpl(temp, temp2);
4930 //
4931 // However, the second read barrier may trash `temp`, as it
4932 // is a temporary register, and as such would not be saved
4933 // along with live registers before calling the runtime (nor
4934 // restored afterwards). So in this case, we bail out and
4935 // delegate the work to the array set slow path.
4936 //
4937 // TODO: Extend the register allocator to support a new
4938 // "(locally) live temp" location so as to avoid always
4939 // going into the slow path when read barriers are enabled.
4940 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004941 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004942 // /* HeapReference<Class> */ temp = array->klass_
4943 __ movl(temp, Address(array, class_offset));
4944 codegen_->MaybeRecordImplicitNullCheck(instruction);
4945 __ MaybeUnpoisonHeapReference(temp);
4946
4947 // /* HeapReference<Class> */ temp = temp->component_type_
4948 __ movl(temp, Address(temp, component_offset));
4949 // If heap poisoning is enabled, no need to unpoison `temp`
4950 // nor the object reference in `register_value->klass`, as
4951 // we are comparing two poisoned references.
4952 __ cmpl(temp, Address(register_value, class_offset));
4953
4954 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4955 __ j(kEqual, &do_put);
4956 // If heap poisoning is enabled, the `temp` reference has
4957 // not been unpoisoned yet; unpoison it now.
4958 __ MaybeUnpoisonHeapReference(temp);
4959
4960 // /* HeapReference<Class> */ temp = temp->super_class_
4961 __ movl(temp, Address(temp, super_offset));
4962 // If heap poisoning is enabled, no need to unpoison
4963 // `temp`, as we are comparing against null below.
4964 __ testl(temp, temp);
4965 __ j(kNotEqual, slow_path->GetEntryLabel());
4966 __ Bind(&do_put);
4967 } else {
4968 __ j(kNotEqual, slow_path->GetEntryLabel());
4969 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004970 }
4971 }
4972
4973 if (kPoisonHeapReferences) {
4974 __ movl(temp, register_value);
4975 __ PoisonHeapReference(temp);
4976 __ movl(address, temp);
4977 } else {
4978 __ movl(address, register_value);
4979 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004980 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004981 codegen_->MaybeRecordImplicitNullCheck(instruction);
4982 }
4983
4984 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4985 codegen_->MarkGCCard(
4986 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4987 __ Bind(&done);
4988
4989 if (slow_path != nullptr) {
4990 __ Bind(slow_path->GetExitLabel());
4991 }
4992
4993 break;
4994 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004995
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004996 case Primitive::kPrimInt: {
4997 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4998 Address address = index.IsConstant()
4999 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5000 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
5001 if (value.IsRegister()) {
5002 __ movl(address, value.AsRegister<CpuRegister>());
5003 } else {
5004 DCHECK(value.IsConstant()) << value;
5005 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5006 __ movl(address, Immediate(v));
5007 }
5008 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009 break;
5010 }
5011
5012 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005013 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
5014 Address address = index.IsConstant()
5015 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5016 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
5017 if (value.IsRegister()) {
5018 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005019 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005020 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005021 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04005022 Address address_high = index.IsConstant()
5023 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5024 offset + sizeof(int32_t))
5025 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5026 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005027 }
5028 break;
5029 }
5030
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005031 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005032 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5033 Address address = index.IsConstant()
5034 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5035 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005036 if (value.IsFpuRegister()) {
5037 __ movss(address, value.AsFpuRegister<XmmRegister>());
5038 } else {
5039 DCHECK(value.IsConstant());
5040 int32_t v =
5041 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5042 __ movl(address, Immediate(v));
5043 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005044 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005045 break;
5046 }
5047
5048 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005049 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5050 Address address = index.IsConstant()
5051 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5052 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005053 if (value.IsFpuRegister()) {
5054 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5055 codegen_->MaybeRecordImplicitNullCheck(instruction);
5056 } else {
5057 int64_t v =
5058 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5059 Address address_high = index.IsConstant()
5060 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5061 offset + sizeof(int32_t))
5062 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5063 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5064 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005065 break;
5066 }
5067
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068 case Primitive::kPrimVoid:
5069 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005070 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071 }
5072}
5073
5074void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005075 LocationSummary* locations =
5076 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005077 locations->SetInAt(0, Location::RequiresRegister());
5078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005079}
5080
5081void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
5082 LocationSummary* locations = instruction->GetLocations();
5083 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005084 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5085 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005087 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088}
5089
5090void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005091 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5092 ? LocationSummary::kCallOnSlowPath
5093 : LocationSummary::kNoCall;
5094 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005095 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005096 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005097 if (instruction->HasUses()) {
5098 locations->SetOut(Location::SameAsFirstInput());
5099 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100}
5101
5102void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5103 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005104 Location index_loc = locations->InAt(0);
5105 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005106 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005107 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108
Mark Mendell99dbd682015-04-22 16:18:52 -04005109 if (length_loc.IsConstant()) {
5110 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5111 if (index_loc.IsConstant()) {
5112 // BCE will remove the bounds check if we are guarenteed to pass.
5113 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5114 if (index < 0 || index >= length) {
5115 codegen_->AddSlowPath(slow_path);
5116 __ jmp(slow_path->GetEntryLabel());
5117 } else {
5118 // Some optimization after BCE may have generated this, and we should not
5119 // generate a bounds check if it is a valid range.
5120 }
5121 return;
5122 }
5123
5124 // We have to reverse the jump condition because the length is the constant.
5125 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5126 __ cmpl(index_reg, Immediate(length));
5127 codegen_->AddSlowPath(slow_path);
5128 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005129 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005130 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5131 if (index_loc.IsConstant()) {
5132 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5133 __ cmpl(length, Immediate(value));
5134 } else {
5135 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5136 }
5137 codegen_->AddSlowPath(slow_path);
5138 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005139 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005140}
5141
5142void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5143 CpuRegister card,
5144 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005145 CpuRegister value,
5146 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005147 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005148 if (value_can_be_null) {
5149 __ testl(value, value);
5150 __ j(kEqual, &is_null);
5151 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005152 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5153 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154 __ movq(temp, object);
5155 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005156 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005157 if (value_can_be_null) {
5158 __ Bind(&is_null);
5159 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160}
5161
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005162void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
5163 temp->SetLocations(nullptr);
5164}
5165
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005166void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005167 // Nothing to do, this is driven by the code generator.
5168}
5169
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005170void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005171 LOG(FATAL) << "Unimplemented";
5172}
5173
5174void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005175 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5176}
5177
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005178void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5179 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5180}
5181
5182void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005183 HBasicBlock* block = instruction->GetBlock();
5184 if (block->GetLoopInformation() != nullptr) {
5185 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5186 // The back edge will generate the suspend check.
5187 return;
5188 }
5189 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5190 // The goto will generate the suspend check.
5191 return;
5192 }
5193 GenerateSuspendCheck(instruction, nullptr);
5194}
5195
5196void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5197 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005198 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005199 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5200 if (slow_path == nullptr) {
5201 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5202 instruction->SetSlowPath(slow_path);
5203 codegen_->AddSlowPath(slow_path);
5204 if (successor != nullptr) {
5205 DCHECK(successor->IsLoopHeader());
5206 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5207 }
5208 } else {
5209 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5210 }
5211
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005212 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5213 /* no_rip */ true),
5214 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005215 if (successor == nullptr) {
5216 __ j(kNotEqual, slow_path->GetEntryLabel());
5217 __ Bind(slow_path->GetReturnLabel());
5218 } else {
5219 __ j(kEqual, codegen_->GetLabelOf(successor));
5220 __ jmp(slow_path->GetEntryLabel());
5221 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005222}
5223
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005224X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5225 return codegen_->GetAssembler();
5226}
5227
5228void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005229 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005230 Location source = move->GetSource();
5231 Location destination = move->GetDestination();
5232
5233 if (source.IsRegister()) {
5234 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005235 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005236 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005237 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005238 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005239 } else {
5240 DCHECK(destination.IsDoubleStackSlot());
5241 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005242 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005243 }
5244 } else if (source.IsStackSlot()) {
5245 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005246 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005247 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005248 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005249 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005250 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005251 } else {
5252 DCHECK(destination.IsStackSlot());
5253 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5254 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5255 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005256 } else if (source.IsDoubleStackSlot()) {
5257 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005258 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005259 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005260 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005261 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5262 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005263 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005264 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005265 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5266 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5267 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005268 } else if (source.IsConstant()) {
5269 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005270 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5271 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005272 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005273 if (value == 0) {
5274 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5275 } else {
5276 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5277 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005278 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005279 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005280 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005281 }
5282 } else if (constant->IsLongConstant()) {
5283 int64_t value = constant->AsLongConstant()->GetValue();
5284 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005285 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005286 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005287 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005288 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005289 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005290 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005291 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005292 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005293 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005294 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005295 } else {
5296 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005297 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5299 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005300 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005301 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005302 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005303 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005304 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005305 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005306 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005307 } else {
5308 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005309 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005310 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005311 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005312 } else if (source.IsFpuRegister()) {
5313 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005314 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005315 } else if (destination.IsStackSlot()) {
5316 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005317 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005318 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005319 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005320 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005321 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005322 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005323 }
5324}
5325
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005326void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005327 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005328 __ movl(Address(CpuRegister(RSP), mem), reg);
5329 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005330}
5331
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005332void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005333 ScratchRegisterScope ensure_scratch(
5334 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5335
5336 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5337 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5338 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5339 Address(CpuRegister(RSP), mem2 + stack_offset));
5340 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5341 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5342 CpuRegister(ensure_scratch.GetRegister()));
5343}
5344
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005345void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5346 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5347 __ movq(Address(CpuRegister(RSP), mem), reg);
5348 __ movq(reg, CpuRegister(TMP));
5349}
5350
5351void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5352 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005353 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005354
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005355 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5356 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5357 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5358 Address(CpuRegister(RSP), mem2 + stack_offset));
5359 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5360 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5361 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005362}
5363
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005364void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5365 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5366 __ movss(Address(CpuRegister(RSP), mem), reg);
5367 __ movd(reg, CpuRegister(TMP));
5368}
5369
5370void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5371 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5372 __ movsd(Address(CpuRegister(RSP), mem), reg);
5373 __ movd(reg, CpuRegister(TMP));
5374}
5375
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005376void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005377 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005378 Location source = move->GetSource();
5379 Location destination = move->GetDestination();
5380
5381 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005382 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005383 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005384 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005385 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005386 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005387 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005388 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5389 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005390 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005391 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005392 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005393 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5394 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005395 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005396 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5397 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5398 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005399 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005400 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005401 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005402 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005403 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005404 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005405 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005406 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005407 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005408 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005409 }
5410}
5411
5412
5413void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5414 __ pushq(CpuRegister(reg));
5415}
5416
5417
5418void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5419 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005420}
5421
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005422void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005423 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005424 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5425 Immediate(mirror::Class::kStatusInitialized));
5426 __ j(kLess, slow_path->GetEntryLabel());
5427 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005428 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005429}
5430
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005431void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005432 InvokeRuntimeCallingConvention calling_convention;
5433 CodeGenerator::CreateLoadClassLocationSummary(
5434 cls,
5435 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005436 Location::RegisterLocation(RAX),
5437 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005438}
5439
5440void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005441 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005442 if (cls->NeedsAccessCheck()) {
5443 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5444 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5445 cls,
5446 cls->GetDexPc(),
5447 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005448 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005449 return;
5450 }
5451
Roland Levillain0d5a2812015-11-13 10:07:31 +00005452 Location out_loc = locations->Out();
5453 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005454 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005455
Calin Juravle580b6092015-10-06 17:35:58 +01005456 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005457 DCHECK(!cls->CanCallRuntime());
5458 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005459 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5460 GenerateGcRootFieldLoad(
5461 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005462 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005463 // /* GcRoot<mirror::Class>[] */ out =
5464 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5465 __ movq(out, Address(current_method,
5466 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005467 // /* GcRoot<mirror::Class> */ out = out[type_index]
5468 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01005469
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005470 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5471 DCHECK(cls->CanCallRuntime());
5472 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5473 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5474 codegen_->AddSlowPath(slow_path);
5475 if (!cls->IsInDexCache()) {
5476 __ testl(out, out);
5477 __ j(kEqual, slow_path->GetEntryLabel());
5478 }
5479 if (cls->MustGenerateClinitCheck()) {
5480 GenerateClassInitializationCheck(slow_path, out);
5481 } else {
5482 __ Bind(slow_path->GetExitLabel());
5483 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005484 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005485 }
5486}
5487
5488void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5489 LocationSummary* locations =
5490 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5491 locations->SetInAt(0, Location::RequiresRegister());
5492 if (check->HasUses()) {
5493 locations->SetOut(Location::SameAsFirstInput());
5494 }
5495}
5496
5497void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005498 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005499 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005500 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005501 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005502 GenerateClassInitializationCheck(slow_path,
5503 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005504}
5505
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005506void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005507 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5508 ? LocationSummary::kCallOnSlowPath
5509 : LocationSummary::kNoCall;
5510 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005511 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005512 locations->SetOut(Location::RequiresRegister());
5513}
5514
5515void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005516 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005517 Location out_loc = locations->Out();
5518 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005519 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005520
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005521 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5522 GenerateGcRootFieldLoad(
5523 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005524 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5525 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005526 // /* GcRoot<mirror::String> */ out = out[string_index]
5527 GenerateGcRootFieldLoad(
5528 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005529
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005530 if (!load->IsInDexCache()) {
5531 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5532 codegen_->AddSlowPath(slow_path);
5533 __ testl(out, out);
5534 __ j(kEqual, slow_path->GetEntryLabel());
5535 __ Bind(slow_path->GetExitLabel());
5536 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005537}
5538
David Brazdilcb1c0552015-08-04 16:22:25 +01005539static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005540 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5541 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005542}
5543
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005544void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5545 LocationSummary* locations =
5546 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5547 locations->SetOut(Location::RequiresRegister());
5548}
5549
5550void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005551 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5552}
5553
5554void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5555 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5556}
5557
5558void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5559 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005560}
5561
5562void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5563 LocationSummary* locations =
5564 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5565 InvokeRuntimeCallingConvention calling_convention;
5566 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5567}
5568
5569void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005570 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5571 instruction,
5572 instruction->GetDexPc(),
5573 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005574 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005575}
5576
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005577static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5578 return kEmitCompilerReadBarrier &&
5579 (kUseBakerReadBarrier ||
5580 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5581 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5582 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5583}
5584
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005585void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005586 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005587 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5588 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005589 case TypeCheckKind::kExactCheck:
5590 case TypeCheckKind::kAbstractClassCheck:
5591 case TypeCheckKind::kClassHierarchyCheck:
5592 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005593 call_kind =
5594 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005595 break;
5596 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005597 case TypeCheckKind::kUnresolvedCheck:
5598 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005599 call_kind = LocationSummary::kCallOnSlowPath;
5600 break;
5601 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005602
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005604 locations->SetInAt(0, Location::RequiresRegister());
5605 locations->SetInAt(1, Location::Any());
5606 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5607 locations->SetOut(Location::RequiresRegister());
5608 // When read barriers are enabled, we need a temporary register for
5609 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005610 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005611 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005612 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005613}
5614
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005615void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005616 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005617 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005618 Location obj_loc = locations->InAt(0);
5619 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005620 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005621 Location out_loc = locations->Out();
5622 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005623 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005624 locations->GetTemp(0) :
5625 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005626 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005627 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5628 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5629 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005630 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005631 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005632
5633 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005634 // Avoid null check if we know obj is not null.
5635 if (instruction->MustDoNullCheck()) {
5636 __ testl(obj, obj);
5637 __ j(kEqual, &zero);
5638 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005639
Roland Levillain0d5a2812015-11-13 10:07:31 +00005640 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005641 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005642
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005643 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 case TypeCheckKind::kExactCheck: {
5645 if (cls.IsRegister()) {
5646 __ cmpl(out, cls.AsRegister<CpuRegister>());
5647 } else {
5648 DCHECK(cls.IsStackSlot()) << cls;
5649 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5650 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005651 if (zero.IsLinked()) {
5652 // Classes must be equal for the instanceof to succeed.
5653 __ j(kNotEqual, &zero);
5654 __ movl(out, Immediate(1));
5655 __ jmp(&done);
5656 } else {
5657 __ setcc(kEqual, out);
5658 // setcc only sets the low byte.
5659 __ andl(out, Immediate(1));
5660 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005661 break;
5662 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005663
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005664 case TypeCheckKind::kAbstractClassCheck: {
5665 // If the class is abstract, we eagerly fetch the super class of the
5666 // object to avoid doing a comparison we know will fail.
5667 NearLabel loop, success;
5668 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005669 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005670 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005671 __ testl(out, out);
5672 // If `out` is null, we use it for the result, and jump to `done`.
5673 __ j(kEqual, &done);
5674 if (cls.IsRegister()) {
5675 __ cmpl(out, cls.AsRegister<CpuRegister>());
5676 } else {
5677 DCHECK(cls.IsStackSlot()) << cls;
5678 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5679 }
5680 __ j(kNotEqual, &loop);
5681 __ movl(out, Immediate(1));
5682 if (zero.IsLinked()) {
5683 __ jmp(&done);
5684 }
5685 break;
5686 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005687
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005688 case TypeCheckKind::kClassHierarchyCheck: {
5689 // Walk over the class hierarchy to find a match.
5690 NearLabel loop, success;
5691 __ Bind(&loop);
5692 if (cls.IsRegister()) {
5693 __ cmpl(out, cls.AsRegister<CpuRegister>());
5694 } else {
5695 DCHECK(cls.IsStackSlot()) << cls;
5696 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5697 }
5698 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005699 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005700 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005701 __ testl(out, out);
5702 __ j(kNotEqual, &loop);
5703 // If `out` is null, we use it for the result, and jump to `done`.
5704 __ jmp(&done);
5705 __ Bind(&success);
5706 __ movl(out, Immediate(1));
5707 if (zero.IsLinked()) {
5708 __ jmp(&done);
5709 }
5710 break;
5711 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005712
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005713 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005714 // Do an exact check.
5715 NearLabel exact_check;
5716 if (cls.IsRegister()) {
5717 __ cmpl(out, cls.AsRegister<CpuRegister>());
5718 } else {
5719 DCHECK(cls.IsStackSlot()) << cls;
5720 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5721 }
5722 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005723 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005724 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005725 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005726 __ testl(out, out);
5727 // If `out` is null, we use it for the result, and jump to `done`.
5728 __ j(kEqual, &done);
5729 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5730 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005731 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005732 __ movl(out, Immediate(1));
5733 __ jmp(&done);
5734 break;
5735 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005736
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005737 case TypeCheckKind::kArrayCheck: {
5738 if (cls.IsRegister()) {
5739 __ cmpl(out, cls.AsRegister<CpuRegister>());
5740 } else {
5741 DCHECK(cls.IsStackSlot()) << cls;
5742 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5743 }
5744 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005745 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5746 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005747 codegen_->AddSlowPath(slow_path);
5748 __ j(kNotEqual, slow_path->GetEntryLabel());
5749 __ movl(out, Immediate(1));
5750 if (zero.IsLinked()) {
5751 __ jmp(&done);
5752 }
5753 break;
5754 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005755
Calin Juravle98893e12015-10-02 21:05:03 +01005756 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005757 case TypeCheckKind::kInterfaceCheck: {
5758 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005759 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005760 // cases.
5761 //
5762 // We cannot directly call the InstanceofNonTrivial runtime
5763 // entry point without resorting to a type checking slow path
5764 // here (i.e. by calling InvokeRuntime directly), as it would
5765 // require to assign fixed registers for the inputs of this
5766 // HInstanceOf instruction (following the runtime calling
5767 // convention), which might be cluttered by the potential first
5768 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005769 //
5770 // TODO: Introduce a new runtime entry point taking the object
5771 // to test (instead of its class) as argument, and let it deal
5772 // with the read barrier issues. This will let us refactor this
5773 // case of the `switch` code as it was previously (with a direct
5774 // call to the runtime not using a type checking slow path).
5775 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005776 DCHECK(locations->OnlyCallsOnSlowPath());
5777 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5778 /* is_fatal */ false);
5779 codegen_->AddSlowPath(slow_path);
5780 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005781 if (zero.IsLinked()) {
5782 __ jmp(&done);
5783 }
5784 break;
5785 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005786 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005787
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005788 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005789 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005790 __ xorl(out, out);
5791 }
5792
5793 if (done.IsLinked()) {
5794 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005795 }
5796
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005797 if (slow_path != nullptr) {
5798 __ Bind(slow_path->GetExitLabel());
5799 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005800}
5801
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005802void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005803 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5804 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005805 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5806 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005807 case TypeCheckKind::kExactCheck:
5808 case TypeCheckKind::kAbstractClassCheck:
5809 case TypeCheckKind::kClassHierarchyCheck:
5810 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005811 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5812 LocationSummary::kCallOnSlowPath :
5813 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005814 break;
5815 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005816 case TypeCheckKind::kUnresolvedCheck:
5817 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005818 call_kind = LocationSummary::kCallOnSlowPath;
5819 break;
5820 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005821 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5822 locations->SetInAt(0, Location::RequiresRegister());
5823 locations->SetInAt(1, Location::Any());
5824 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5825 locations->AddTemp(Location::RequiresRegister());
5826 // When read barriers are enabled, we need an additional temporary
5827 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005828 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005830 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005831}
5832
5833void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005834 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005835 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005836 Location obj_loc = locations->InAt(0);
5837 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005838 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839 Location temp_loc = locations->GetTemp(0);
5840 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005841 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005842 locations->GetTemp(1) :
5843 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5845 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5846 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5847 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005848
Roland Levillain0d5a2812015-11-13 10:07:31 +00005849 bool is_type_check_slow_path_fatal =
5850 (type_check_kind == TypeCheckKind::kExactCheck ||
5851 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5852 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5853 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5854 !instruction->CanThrowIntoCatchBlock();
5855 SlowPathCode* type_check_slow_path =
5856 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5857 is_type_check_slow_path_fatal);
5858 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005859
Mark Mendell152408f2015-12-31 12:28:50 -05005860 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005861 // Avoid null check if we know obj is not null.
5862 if (instruction->MustDoNullCheck()) {
5863 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005864 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005865 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005866
Roland Levillain0d5a2812015-11-13 10:07:31 +00005867 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005868 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005869
Roland Levillain0d5a2812015-11-13 10:07:31 +00005870 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005871 case TypeCheckKind::kExactCheck:
5872 case TypeCheckKind::kArrayCheck: {
5873 if (cls.IsRegister()) {
5874 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5875 } else {
5876 DCHECK(cls.IsStackSlot()) << cls;
5877 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5878 }
5879 // Jump to slow path for throwing the exception or doing a
5880 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005881 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 break;
5883 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005884
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 case TypeCheckKind::kAbstractClassCheck: {
5886 // If the class is abstract, we eagerly fetch the super class of the
5887 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005888 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005890 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005891 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005892
5893 // If the class reference currently in `temp` is not null, jump
5894 // to the `compare_classes` label to compare it with the checked
5895 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005896 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005897 __ j(kNotEqual, &compare_classes);
5898 // Otherwise, jump to the slow path to throw the exception.
5899 //
5900 // But before, move back the object's class into `temp` before
5901 // going into the slow path, as it has been overwritten in the
5902 // meantime.
5903 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005904 GenerateReferenceLoadTwoRegisters(
5905 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005906 __ jmp(type_check_slow_path->GetEntryLabel());
5907
5908 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005909 if (cls.IsRegister()) {
5910 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5911 } else {
5912 DCHECK(cls.IsStackSlot()) << cls;
5913 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5914 }
5915 __ j(kNotEqual, &loop);
5916 break;
5917 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005918
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005919 case TypeCheckKind::kClassHierarchyCheck: {
5920 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005921 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005922 __ Bind(&loop);
5923 if (cls.IsRegister()) {
5924 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5925 } else {
5926 DCHECK(cls.IsStackSlot()) << cls;
5927 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5928 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005929 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005930
Roland Levillain0d5a2812015-11-13 10:07:31 +00005931 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005932 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005933
5934 // If the class reference currently in `temp` is not null, jump
5935 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005936 __ testl(temp, temp);
5937 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005938 // Otherwise, jump to the slow path to throw the exception.
5939 //
5940 // But before, move back the object's class into `temp` before
5941 // going into the slow path, as it has been overwritten in the
5942 // meantime.
5943 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005944 GenerateReferenceLoadTwoRegisters(
5945 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005946 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005947 break;
5948 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005949
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005950 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005951 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005952 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005953 if (cls.IsRegister()) {
5954 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5955 } else {
5956 DCHECK(cls.IsStackSlot()) << cls;
5957 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5958 }
5959 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005960
5961 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005962 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005963 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005964
5965 // If the component type is not null (i.e. the object is indeed
5966 // an array), jump to label `check_non_primitive_component_type`
5967 // to further check that this component type is not a primitive
5968 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005969 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005970 __ j(kNotEqual, &check_non_primitive_component_type);
5971 // Otherwise, jump to the slow path to throw the exception.
5972 //
5973 // But before, move back the object's class into `temp` before
5974 // going into the slow path, as it has been overwritten in the
5975 // meantime.
5976 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005977 GenerateReferenceLoadTwoRegisters(
5978 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005979 __ jmp(type_check_slow_path->GetEntryLabel());
5980
5981 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005982 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005983 __ j(kEqual, &done);
5984 // Same comment as above regarding `temp` and the slow path.
5985 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005986 GenerateReferenceLoadTwoRegisters(
5987 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005988 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005989 break;
5990 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005991
Calin Juravle98893e12015-10-02 21:05:03 +01005992 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005993 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005994 // We always go into the type check slow path for the unresolved
5995 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005996 //
5997 // We cannot directly call the CheckCast runtime entry point
5998 // without resorting to a type checking slow path here (i.e. by
5999 // calling InvokeRuntime directly), as it would require to
6000 // assign fixed registers for the inputs of this HInstanceOf
6001 // instruction (following the runtime calling convention), which
6002 // might be cluttered by the potential first read barrier
6003 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006004 //
6005 // TODO: Introduce a new runtime entry point taking the object
6006 // to test (instead of its class) as argument, and let it deal
6007 // with the read barrier issues. This will let us refactor this
6008 // case of the `switch` code as it was previously (with a direct
6009 // call to the runtime not using a type checking slow path).
6010 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006011 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006012 break;
6013 }
6014 __ Bind(&done);
6015
Roland Levillain0d5a2812015-11-13 10:07:31 +00006016 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006017}
6018
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006019void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6020 LocationSummary* locations =
6021 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6022 InvokeRuntimeCallingConvention calling_convention;
6023 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6024}
6025
6026void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006027 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6028 : QUICK_ENTRY_POINT(pUnlockObject),
6029 instruction,
6030 instruction->GetDexPc(),
6031 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006032 if (instruction->IsEnter()) {
6033 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6034 } else {
6035 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6036 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006037}
6038
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006039void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6040void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6041void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6042
6043void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6044 LocationSummary* locations =
6045 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6046 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6047 || instruction->GetResultType() == Primitive::kPrimLong);
6048 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006049 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006050 locations->SetOut(Location::SameAsFirstInput());
6051}
6052
6053void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6054 HandleBitwiseOperation(instruction);
6055}
6056
6057void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6058 HandleBitwiseOperation(instruction);
6059}
6060
6061void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6062 HandleBitwiseOperation(instruction);
6063}
6064
6065void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6066 LocationSummary* locations = instruction->GetLocations();
6067 Location first = locations->InAt(0);
6068 Location second = locations->InAt(1);
6069 DCHECK(first.Equals(locations->Out()));
6070
6071 if (instruction->GetResultType() == Primitive::kPrimInt) {
6072 if (second.IsRegister()) {
6073 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006074 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006075 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006076 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006077 } else {
6078 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006079 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006080 }
6081 } else if (second.IsConstant()) {
6082 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6083 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006084 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006085 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006086 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006087 } else {
6088 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006089 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006090 }
6091 } else {
6092 Address address(CpuRegister(RSP), second.GetStackIndex());
6093 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006094 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006095 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006096 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006097 } else {
6098 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006099 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006100 }
6101 }
6102 } else {
6103 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006104 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6105 bool second_is_constant = false;
6106 int64_t value = 0;
6107 if (second.IsConstant()) {
6108 second_is_constant = true;
6109 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006110 }
Mark Mendell40741f32015-04-20 22:10:34 -04006111 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006112
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006113 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006114 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006115 if (is_int32_value) {
6116 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6117 } else {
6118 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6119 }
6120 } else if (second.IsDoubleStackSlot()) {
6121 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006122 } else {
6123 __ andq(first_reg, second.AsRegister<CpuRegister>());
6124 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006125 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006126 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006127 if (is_int32_value) {
6128 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6129 } else {
6130 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6131 }
6132 } else if (second.IsDoubleStackSlot()) {
6133 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006134 } else {
6135 __ orq(first_reg, second.AsRegister<CpuRegister>());
6136 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006137 } else {
6138 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006139 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006140 if (is_int32_value) {
6141 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6142 } else {
6143 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6144 }
6145 } else if (second.IsDoubleStackSlot()) {
6146 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006147 } else {
6148 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6149 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006150 }
6151 }
6152}
6153
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006154void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6155 Location out,
6156 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006157 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006158 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6159 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006160 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006161 if (kUseBakerReadBarrier) {
6162 // Load with fast path based Baker's read barrier.
6163 // /* HeapReference<Object> */ out = *(out + offset)
6164 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006165 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006166 } else {
6167 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006168 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006169 // in the following move operation, as we will need it for the
6170 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006171 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006172 // /* HeapReference<Object> */ out = *(out + offset)
6173 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006174 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006175 }
6176 } else {
6177 // Plain load with no read barrier.
6178 // /* HeapReference<Object> */ out = *(out + offset)
6179 __ movl(out_reg, Address(out_reg, offset));
6180 __ MaybeUnpoisonHeapReference(out_reg);
6181 }
6182}
6183
6184void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6185 Location out,
6186 Location obj,
6187 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006188 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006189 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6190 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6191 if (kEmitCompilerReadBarrier) {
6192 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006193 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006194 // Load with fast path based Baker's read barrier.
6195 // /* HeapReference<Object> */ out = *(obj + offset)
6196 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006197 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006198 } else {
6199 // Load with slow path based read barrier.
6200 // /* HeapReference<Object> */ out = *(obj + offset)
6201 __ movl(out_reg, Address(obj_reg, offset));
6202 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6203 }
6204 } else {
6205 // Plain load with no read barrier.
6206 // /* HeapReference<Object> */ out = *(obj + offset)
6207 __ movl(out_reg, Address(obj_reg, offset));
6208 __ MaybeUnpoisonHeapReference(out_reg);
6209 }
6210}
6211
6212void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6213 Location root,
6214 CpuRegister obj,
6215 uint32_t offset) {
6216 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6217 if (kEmitCompilerReadBarrier) {
6218 if (kUseBakerReadBarrier) {
6219 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6220 // Baker's read barrier are used:
6221 //
6222 // root = obj.field;
6223 // if (Thread::Current()->GetIsGcMarking()) {
6224 // root = ReadBarrier::Mark(root)
6225 // }
6226
6227 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6228 __ movl(root_reg, Address(obj, offset));
6229 static_assert(
6230 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6231 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6232 "have different sizes.");
6233 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6234 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6235 "have different sizes.");
6236
6237 // Slow path used to mark the GC root `root`.
6238 SlowPathCode* slow_path =
6239 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6240 codegen_->AddSlowPath(slow_path);
6241
6242 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6243 /* no_rip */ true),
6244 Immediate(0));
6245 __ j(kNotEqual, slow_path->GetEntryLabel());
6246 __ Bind(slow_path->GetExitLabel());
6247 } else {
6248 // GC root loaded through a slow path for read barriers other
6249 // than Baker's.
6250 // /* GcRoot<mirror::Object>* */ root = obj + offset
6251 __ leaq(root_reg, Address(obj, offset));
6252 // /* mirror::Object* */ root = root->Read()
6253 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6254 }
6255 } else {
6256 // Plain GC root load with no read barrier.
6257 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6258 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006259 // Note that GC roots are not affected by heap poisoning, thus we
6260 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006261 }
6262}
6263
6264void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6265 Location ref,
6266 CpuRegister obj,
6267 uint32_t offset,
6268 Location temp,
6269 bool needs_null_check) {
6270 DCHECK(kEmitCompilerReadBarrier);
6271 DCHECK(kUseBakerReadBarrier);
6272
6273 // /* HeapReference<Object> */ ref = *(obj + offset)
6274 Address src(obj, offset);
6275 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6276}
6277
6278void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6279 Location ref,
6280 CpuRegister obj,
6281 uint32_t data_offset,
6282 Location index,
6283 Location temp,
6284 bool needs_null_check) {
6285 DCHECK(kEmitCompilerReadBarrier);
6286 DCHECK(kUseBakerReadBarrier);
6287
6288 // /* HeapReference<Object> */ ref =
6289 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6290 Address src = index.IsConstant() ?
6291 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6292 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6293 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6294}
6295
6296void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6297 Location ref,
6298 CpuRegister obj,
6299 const Address& src,
6300 Location temp,
6301 bool needs_null_check) {
6302 DCHECK(kEmitCompilerReadBarrier);
6303 DCHECK(kUseBakerReadBarrier);
6304
6305 // In slow path based read barriers, the read barrier call is
6306 // inserted after the original load. However, in fast path based
6307 // Baker's read barriers, we need to perform the load of
6308 // mirror::Object::monitor_ *before* the original reference load.
6309 // This load-load ordering is required by the read barrier.
6310 // The fast path/slow path (for Baker's algorithm) should look like:
6311 //
6312 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6313 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6314 // HeapReference<Object> ref = *src; // Original reference load.
6315 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6316 // if (is_gray) {
6317 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6318 // }
6319 //
6320 // Note: the original implementation in ReadBarrier::Barrier is
6321 // slightly more complex as:
6322 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006323 // the high-bits of rb_state, which are expected to be all zeroes
6324 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6325 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006326 // - it performs additional checks that we do not do here for
6327 // performance reasons.
6328
6329 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6330 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6331 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6332
6333 // /* int32_t */ monitor = obj->monitor_
6334 __ movl(temp_reg, Address(obj, monitor_offset));
6335 if (needs_null_check) {
6336 MaybeRecordImplicitNullCheck(instruction);
6337 }
6338 // /* LockWord */ lock_word = LockWord(monitor)
6339 static_assert(sizeof(LockWord) == sizeof(int32_t),
6340 "art::LockWord and int32_t have different sizes.");
6341 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6342 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6343 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6344 static_assert(
6345 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6346 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6347
6348 // Load fence to prevent load-load reordering.
6349 // Note that this is a no-op, thanks to the x86-64 memory model.
6350 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6351
6352 // The actual reference load.
6353 // /* HeapReference<Object> */ ref = *src
6354 __ movl(ref_reg, src);
6355
6356 // Object* ref = ref_addr->AsMirrorPtr()
6357 __ MaybeUnpoisonHeapReference(ref_reg);
6358
6359 // Slow path used to mark the object `ref` when it is gray.
6360 SlowPathCode* slow_path =
6361 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6362 AddSlowPath(slow_path);
6363
6364 // if (rb_state == ReadBarrier::gray_ptr_)
6365 // ref = ReadBarrier::Mark(ref);
6366 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6367 __ j(kEqual, slow_path->GetEntryLabel());
6368 __ Bind(slow_path->GetExitLabel());
6369}
6370
6371void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6372 Location out,
6373 Location ref,
6374 Location obj,
6375 uint32_t offset,
6376 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006377 DCHECK(kEmitCompilerReadBarrier);
6378
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006379 // Insert a slow path based read barrier *after* the reference load.
6380 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006381 // If heap poisoning is enabled, the unpoisoning of the loaded
6382 // reference will be carried out by the runtime within the slow
6383 // path.
6384 //
6385 // Note that `ref` currently does not get unpoisoned (when heap
6386 // poisoning is enabled), which is alright as the `ref` argument is
6387 // not used by the artReadBarrierSlow entry point.
6388 //
6389 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6390 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6391 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6392 AddSlowPath(slow_path);
6393
Roland Levillain0d5a2812015-11-13 10:07:31 +00006394 __ jmp(slow_path->GetEntryLabel());
6395 __ Bind(slow_path->GetExitLabel());
6396}
6397
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006398void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6399 Location out,
6400 Location ref,
6401 Location obj,
6402 uint32_t offset,
6403 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006404 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006405 // Baker's read barriers shall be handled by the fast path
6406 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6407 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006408 // If heap poisoning is enabled, unpoisoning will be taken care of
6409 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006410 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006411 } else if (kPoisonHeapReferences) {
6412 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6413 }
6414}
6415
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006416void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6417 Location out,
6418 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006419 DCHECK(kEmitCompilerReadBarrier);
6420
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006421 // Insert a slow path based read barrier *after* the GC root load.
6422 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423 // Note that GC roots are not affected by heap poisoning, so we do
6424 // not need to do anything special for this here.
6425 SlowPathCode* slow_path =
6426 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6427 AddSlowPath(slow_path);
6428
Roland Levillain0d5a2812015-11-13 10:07:31 +00006429 __ jmp(slow_path->GetEntryLabel());
6430 __ Bind(slow_path->GetExitLabel());
6431}
6432
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006433void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006434 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006435 LOG(FATAL) << "Unreachable";
6436}
6437
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006438void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006439 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006440 LOG(FATAL) << "Unreachable";
6441}
6442
Mark Mendellfe57faa2015-09-18 09:26:15 -04006443// Simple implementation of packed switch - generate cascaded compare/jumps.
6444void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6445 LocationSummary* locations =
6446 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6447 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006448 locations->AddTemp(Location::RequiresRegister());
6449 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006450}
6451
6452void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6453 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006454 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006455 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006456 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6457 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6458 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006459 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6460
6461 // Should we generate smaller inline compare/jumps?
6462 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6463 // Figure out the correct compare values and jump conditions.
6464 // Handle the first compare/branch as a special case because it might
6465 // jump to the default case.
6466 DCHECK_GT(num_entries, 2u);
6467 Condition first_condition;
6468 uint32_t index;
6469 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6470 if (lower_bound != 0) {
6471 first_condition = kLess;
6472 __ cmpl(value_reg_in, Immediate(lower_bound));
6473 __ j(first_condition, codegen_->GetLabelOf(default_block));
6474 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6475
6476 index = 1;
6477 } else {
6478 // Handle all the compare/jumps below.
6479 first_condition = kBelow;
6480 index = 0;
6481 }
6482
6483 // Handle the rest of the compare/jumps.
6484 for (; index + 1 < num_entries; index += 2) {
6485 int32_t compare_to_value = lower_bound + index + 1;
6486 __ cmpl(value_reg_in, Immediate(compare_to_value));
6487 // Jump to successors[index] if value < case_value[index].
6488 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6489 // Jump to successors[index + 1] if value == case_value[index + 1].
6490 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6491 }
6492
6493 if (index != num_entries) {
6494 // There are an odd number of entries. Handle the last one.
6495 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006496 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006497 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6498 }
6499
6500 // And the default for any other value.
6501 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6502 __ jmp(codegen_->GetLabelOf(default_block));
6503 }
6504 return;
6505 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006506
6507 // Remove the bias, if needed.
6508 Register value_reg_out = value_reg_in.AsRegister();
6509 if (lower_bound != 0) {
6510 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6511 value_reg_out = temp_reg.AsRegister();
6512 }
6513 CpuRegister value_reg(value_reg_out);
6514
6515 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006516 __ cmpl(value_reg, Immediate(num_entries - 1));
6517 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006518
Mark Mendell9c86b482015-09-18 13:36:07 -04006519 // We are in the range of the table.
6520 // Load the address of the jump table in the constant area.
6521 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006522
Mark Mendell9c86b482015-09-18 13:36:07 -04006523 // Load the (signed) offset from the jump table.
6524 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6525
6526 // Add the offset to the address of the table base.
6527 __ addq(temp_reg, base_reg);
6528
6529 // And jump.
6530 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006531}
6532
Aart Bikc5d47542016-01-27 17:00:35 -08006533void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6534 if (value == 0) {
6535 __ xorl(dest, dest);
6536 } else {
6537 __ movl(dest, Immediate(value));
6538 }
6539}
6540
Mark Mendell92e83bf2015-05-07 11:25:03 -04006541void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6542 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006543 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006544 __ xorl(dest, dest);
6545 } else if (value > 0 && IsInt<32>(value)) {
6546 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
6547 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6548 } else {
6549 __ movq(dest, Immediate(value));
6550 }
6551}
6552
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006553void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6554 if (value == 0) {
6555 __ xorps(dest, dest);
6556 } else {
6557 __ movss(dest, LiteralInt32Address(value));
6558 }
6559}
6560
6561void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6562 if (value == 0) {
6563 __ xorpd(dest, dest);
6564 } else {
6565 __ movsd(dest, LiteralInt64Address(value));
6566 }
6567}
6568
6569void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6570 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6571}
6572
6573void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6574 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6575}
6576
Mark Mendellcfa410b2015-05-25 16:02:44 -04006577void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6578 DCHECK(dest.IsDoubleStackSlot());
6579 if (IsInt<32>(value)) {
6580 // Can move directly as an int32 constant.
6581 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6582 Immediate(static_cast<int32_t>(value)));
6583 } else {
6584 Load64BitValue(CpuRegister(TMP), value);
6585 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6586 }
6587}
6588
Mark Mendell9c86b482015-09-18 13:36:07 -04006589/**
6590 * Class to handle late fixup of offsets into constant area.
6591 */
6592class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6593 public:
6594 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6595 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6596
6597 protected:
6598 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6599
6600 CodeGeneratorX86_64* codegen_;
6601
6602 private:
6603 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6604 // Patch the correct offset for the instruction. We use the address of the
6605 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6606 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6607 int32_t relative_position = constant_offset - pos;
6608
6609 // Patch in the right value.
6610 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6611 }
6612
6613 // Location in constant area that the fixup refers to.
6614 size_t offset_into_constant_area_;
6615};
6616
6617/**
6618 t * Class to handle late fixup of offsets to a jump table that will be created in the
6619 * constant area.
6620 */
6621class JumpTableRIPFixup : public RIPFixup {
6622 public:
6623 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6624 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6625
6626 void CreateJumpTable() {
6627 X86_64Assembler* assembler = codegen_->GetAssembler();
6628
6629 // Ensure that the reference to the jump table has the correct offset.
6630 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6631 SetOffset(offset_in_constant_table);
6632
6633 // Compute the offset from the start of the function to this jump table.
6634 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6635
6636 // Populate the jump table with the correct values for the jump table.
6637 int32_t num_entries = switch_instr_->GetNumEntries();
6638 HBasicBlock* block = switch_instr_->GetBlock();
6639 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6640 // The value that we want is the target offset - the position of the table.
6641 for (int32_t i = 0; i < num_entries; i++) {
6642 HBasicBlock* b = successors[i];
6643 Label* l = codegen_->GetLabelOf(b);
6644 DCHECK(l->IsBound());
6645 int32_t offset_to_block = l->Position() - current_table_offset;
6646 assembler->AppendInt32(offset_to_block);
6647 }
6648 }
6649
6650 private:
6651 const HPackedSwitch* switch_instr_;
6652};
6653
Mark Mendellf55c3e02015-03-26 21:07:46 -04006654void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6655 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006656 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006657 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6658 // 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 -04006659 assembler->Align(4, 0);
6660 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006661
6662 // Populate any jump tables.
6663 for (auto jump_table : fixups_to_jump_tables_) {
6664 jump_table->CreateJumpTable();
6665 }
6666
6667 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006668 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006669 }
6670
6671 // And finish up.
6672 CodeGenerator::Finalize(allocator);
6673}
6674
Mark Mendellf55c3e02015-03-26 21:07:46 -04006675Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6676 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6677 return Address::RIP(fixup);
6678}
6679
6680Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6681 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6682 return Address::RIP(fixup);
6683}
6684
6685Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6686 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6687 return Address::RIP(fixup);
6688}
6689
6690Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6691 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6692 return Address::RIP(fixup);
6693}
6694
Andreas Gampe85b62f22015-09-09 13:15:38 -07006695// TODO: trg as memory.
6696void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6697 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006698 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006699 return;
6700 }
6701
6702 DCHECK_NE(type, Primitive::kPrimVoid);
6703
6704 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6705 if (trg.Equals(return_loc)) {
6706 return;
6707 }
6708
6709 // Let the parallel move resolver take care of all of this.
6710 HParallelMove parallel_move(GetGraph()->GetArena());
6711 parallel_move.AddMove(return_loc, trg, type, nullptr);
6712 GetMoveResolver()->EmitNativeCode(&parallel_move);
6713}
6714
Mark Mendell9c86b482015-09-18 13:36:07 -04006715Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6716 // Create a fixup to be used to create and address the jump table.
6717 JumpTableRIPFixup* table_fixup =
6718 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6719
6720 // We have to populate the jump tables.
6721 fixups_to_jump_tables_.push_back(table_fixup);
6722 return Address::RIP(table_fixup);
6723}
6724
Mark Mendellea5af682015-10-22 17:35:49 -04006725void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6726 const Address& addr_high,
6727 int64_t v,
6728 HInstruction* instruction) {
6729 if (IsInt<32>(v)) {
6730 int32_t v_32 = v;
6731 __ movq(addr_low, Immediate(v_32));
6732 MaybeRecordImplicitNullCheck(instruction);
6733 } else {
6734 // Didn't fit in a register. Do it in pieces.
6735 int32_t low_v = Low32Bits(v);
6736 int32_t high_v = High32Bits(v);
6737 __ movl(addr_low, Immediate(low_v));
6738 MaybeRecordImplicitNullCheck(instruction);
6739 __ movl(addr_high, Immediate(high_v));
6740 }
6741}
6742
Roland Levillain4d027112015-07-01 15:41:14 +01006743#undef __
6744
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006745} // namespace x86_64
6746} // namespace art