blob: 67954887699f8adda31198e0344b0ad7155d12c3 [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()) {
1129 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001130 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001131 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001132 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001133 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001134 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001135 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001136 } else {
1137 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001138 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001139 Address(CpuRegister(RSP), source.GetStackIndex()));
1140 }
1141 } else if (destination.IsFpuRegister()) {
1142 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001143 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001144 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001145 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001146 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001147 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001148 Address(CpuRegister(RSP), source.GetStackIndex()));
1149 } else {
1150 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001151 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001152 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001153 }
1154 } else if (destination.IsStackSlot()) {
1155 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001156 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001157 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 } else if (source.IsFpuRegister()) {
1159 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001160 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001161 } else if (source.IsConstant()) {
1162 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001163 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001164 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001165 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001166 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001167 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1168 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001169 }
1170 } else {
1171 DCHECK(destination.IsDoubleStackSlot());
1172 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001173 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001174 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001175 } else if (source.IsFpuRegister()) {
1176 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001177 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001178 } else if (source.IsConstant()) {
1179 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001180 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001181 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001182 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001183 } else {
1184 DCHECK(constant->IsLongConstant());
1185 value = constant->AsLongConstant()->GetValue();
1186 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001187 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001188 } else {
1189 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001190 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1191 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001192 }
1193 }
1194}
1195
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001196void CodeGeneratorX86_64::Move(HInstruction* instruction,
1197 Location location,
1198 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001199 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001200 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -07001201 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001202 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001203 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001204 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001205 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001206 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1207 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001208 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001209 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001210 } else if (location.IsStackSlot()) {
1211 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
1212 } else {
1213 DCHECK(location.IsConstant());
1214 DCHECK_EQ(location.GetConstant(), const_to_move);
1215 }
1216 } else if (const_to_move->IsLongConstant()) {
1217 int64_t value = const_to_move->AsLongConstant()->GetValue();
1218 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04001219 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001220 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -04001221 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001222 } else {
1223 DCHECK(location.IsConstant());
1224 DCHECK_EQ(location.GetConstant(), const_to_move);
1225 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001226 }
Roland Levillain476df552014-10-09 17:51:36 +01001227 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 switch (instruction->GetType()) {
1229 case Primitive::kPrimBoolean:
1230 case Primitive::kPrimByte:
1231 case Primitive::kPrimChar:
1232 case Primitive::kPrimShort:
1233 case Primitive::kPrimInt:
1234 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001236 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
1237 break;
1238
1239 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +00001241 Move(location,
1242 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001243 break;
1244
1245 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001246 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001247 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001248 } else if (instruction->IsTemporary()) {
1249 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
1250 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001251 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001252 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001253 switch (instruction->GetType()) {
1254 case Primitive::kPrimBoolean:
1255 case Primitive::kPrimByte:
1256 case Primitive::kPrimChar:
1257 case Primitive::kPrimShort:
1258 case Primitive::kPrimInt:
1259 case Primitive::kPrimNot:
1260 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001261 case Primitive::kPrimFloat:
1262 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001263 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264 break;
1265
1266 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001267 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001268 }
1269 }
1270}
1271
Calin Juravle175dc732015-08-25 15:42:32 +01001272void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1273 DCHECK(location.IsRegister());
1274 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1275}
1276
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277void CodeGeneratorX86_64::MoveLocation(
1278 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1279 Move(dst, src);
1280}
1281
1282void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1283 if (location.IsRegister()) {
1284 locations->AddTemp(location);
1285 } else {
1286 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1287 }
1288}
1289
David Brazdilfc6a86a2015-06-26 10:33:45 +00001290void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001291 DCHECK(!successor->IsExitBlock());
1292
1293 HBasicBlock* block = got->GetBlock();
1294 HInstruction* previous = got->GetPrevious();
1295
1296 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001297 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001298 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1299 return;
1300 }
1301
1302 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1303 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1304 }
1305 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001306 __ jmp(codegen_->GetLabelOf(successor));
1307 }
1308}
1309
David Brazdilfc6a86a2015-06-26 10:33:45 +00001310void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1311 got->SetLocations(nullptr);
1312}
1313
1314void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1315 HandleGoto(got, got->GetSuccessor());
1316}
1317
1318void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1319 try_boundary->SetLocations(nullptr);
1320}
1321
1322void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1323 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1324 if (!successor->IsExitBlock()) {
1325 HandleGoto(try_boundary, successor);
1326 }
1327}
1328
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001329void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1330 exit->SetLocations(nullptr);
1331}
1332
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001333void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001334}
1335
Mark Mendell152408f2015-12-31 12:28:50 -05001336template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001337void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001338 LabelType* true_label,
1339 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001340 if (cond->IsFPConditionTrueIfNaN()) {
1341 __ j(kUnordered, true_label);
1342 } else if (cond->IsFPConditionFalseIfNaN()) {
1343 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001344 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001345 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001346}
1347
Mark Mendell152408f2015-12-31 12:28:50 -05001348template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001349void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001350 LabelType* true_target_in,
1351 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001352 // Generated branching requires both targets to be explicit. If either of the
1353 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001354 LabelType fallthrough_target;
1355 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1356 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001357
Mark Mendellc4701932015-04-10 13:18:51 -04001358 LocationSummary* locations = condition->GetLocations();
1359 Location left = locations->InAt(0);
1360 Location right = locations->InAt(1);
1361
Mark Mendellc4701932015-04-10 13:18:51 -04001362 Primitive::Type type = condition->InputAt(0)->GetType();
1363 switch (type) {
1364 case Primitive::kPrimLong: {
1365 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1366 if (right.IsConstant()) {
1367 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1368 if (IsInt<32>(value)) {
1369 if (value == 0) {
1370 __ testq(left_reg, left_reg);
1371 } else {
1372 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1373 }
1374 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001375 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001376 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1377 }
1378 } else if (right.IsDoubleStackSlot()) {
1379 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1380 } else {
1381 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1382 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001383 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001384 break;
1385 }
1386 case Primitive::kPrimFloat: {
1387 if (right.IsFpuRegister()) {
1388 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1389 } else if (right.IsConstant()) {
1390 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1391 codegen_->LiteralFloatAddress(
1392 right.GetConstant()->AsFloatConstant()->GetValue()));
1393 } else {
1394 DCHECK(right.IsStackSlot());
1395 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1396 Address(CpuRegister(RSP), right.GetStackIndex()));
1397 }
1398 GenerateFPJumps(condition, true_target, false_target);
1399 break;
1400 }
1401 case Primitive::kPrimDouble: {
1402 if (right.IsFpuRegister()) {
1403 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1404 } else if (right.IsConstant()) {
1405 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1406 codegen_->LiteralDoubleAddress(
1407 right.GetConstant()->AsDoubleConstant()->GetValue()));
1408 } else {
1409 DCHECK(right.IsDoubleStackSlot());
1410 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1411 Address(CpuRegister(RSP), right.GetStackIndex()));
1412 }
1413 GenerateFPJumps(condition, true_target, false_target);
1414 break;
1415 }
1416 default:
1417 LOG(FATAL) << "Unexpected condition type " << type;
1418 }
1419
David Brazdil0debae72015-11-12 18:37:00 +00001420 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001421 __ jmp(false_target);
1422 }
David Brazdil0debae72015-11-12 18:37:00 +00001423
1424 if (fallthrough_target.IsLinked()) {
1425 __ Bind(&fallthrough_target);
1426 }
Mark Mendellc4701932015-04-10 13:18:51 -04001427}
1428
David Brazdil0debae72015-11-12 18:37:00 +00001429static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1430 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1431 // are set only strictly before `branch`. We can't use the eflags on long
1432 // conditions if they are materialized due to the complex branching.
1433 return cond->IsCondition() &&
1434 cond->GetNext() == branch &&
1435 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1436}
1437
Mark Mendell152408f2015-12-31 12:28:50 -05001438template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001439void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001440 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001441 LabelType* true_target,
1442 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001443 HInstruction* cond = instruction->InputAt(condition_input_index);
1444
1445 if (true_target == nullptr && false_target == nullptr) {
1446 // Nothing to do. The code always falls through.
1447 return;
1448 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001449 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001450 if (cond->AsIntConstant()->IsOne()) {
1451 if (true_target != nullptr) {
1452 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001453 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001454 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001455 DCHECK(cond->AsIntConstant()->IsZero());
1456 if (false_target != nullptr) {
1457 __ jmp(false_target);
1458 }
1459 }
1460 return;
1461 }
1462
1463 // The following code generates these patterns:
1464 // (1) true_target == nullptr && false_target != nullptr
1465 // - opposite condition true => branch to false_target
1466 // (2) true_target != nullptr && false_target == nullptr
1467 // - condition true => branch to true_target
1468 // (3) true_target != nullptr && false_target != nullptr
1469 // - condition true => branch to true_target
1470 // - branch to false_target
1471 if (IsBooleanValueOrMaterializedCondition(cond)) {
1472 if (AreEflagsSetFrom(cond, instruction)) {
1473 if (true_target == nullptr) {
1474 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1475 } else {
1476 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1477 }
1478 } else {
1479 // Materialized condition, compare against 0.
1480 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1481 if (lhs.IsRegister()) {
1482 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1483 } else {
1484 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1485 }
1486 if (true_target == nullptr) {
1487 __ j(kEqual, false_target);
1488 } else {
1489 __ j(kNotEqual, true_target);
1490 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001491 }
1492 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001493 // Condition has not been materialized, use its inputs as the
1494 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001495 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001496
David Brazdil0debae72015-11-12 18:37:00 +00001497 // If this is a long or FP comparison that has been folded into
1498 // the HCondition, generate the comparison directly.
1499 Primitive::Type type = condition->InputAt(0)->GetType();
1500 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1501 GenerateCompareTestAndBranch(condition, true_target, false_target);
1502 return;
1503 }
1504
1505 Location lhs = condition->GetLocations()->InAt(0);
1506 Location rhs = condition->GetLocations()->InAt(1);
1507 if (rhs.IsRegister()) {
1508 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1509 } else if (rhs.IsConstant()) {
1510 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1511 if (constant == 0) {
1512 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001513 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001514 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001515 }
1516 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001517 __ cmpl(lhs.AsRegister<CpuRegister>(),
1518 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1519 }
1520 if (true_target == nullptr) {
1521 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1522 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001523 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001524 }
Dave Allison20dfc792014-06-16 20:44:29 -07001525 }
David Brazdil0debae72015-11-12 18:37:00 +00001526
1527 // If neither branch falls through (case 3), the conditional branch to `true_target`
1528 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1529 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001530 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001531 }
1532}
1533
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001534void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1536 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001537 locations->SetInAt(0, Location::Any());
1538 }
1539}
1540
1541void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001542 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1543 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1544 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1545 nullptr : codegen_->GetLabelOf(true_successor);
1546 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1547 nullptr : codegen_->GetLabelOf(false_successor);
1548 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001549}
1550
1551void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1552 LocationSummary* locations = new (GetGraph()->GetArena())
1553 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001554 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001555 locations->SetInAt(0, Location::Any());
1556 }
1557}
1558
1559void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001560 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001561 GenerateTestAndBranch<Label>(deoptimize,
1562 /* condition_input_index */ 0,
1563 slow_path->GetEntryLabel(),
1564 /* false_target */ nullptr);
1565}
1566
1567void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1569 if (Primitive::IsFloatingPointType(select->GetType())) {
1570 locations->SetInAt(0, Location::RequiresFpuRegister());
1571 locations->SetInAt(1, Location::RequiresFpuRegister());
1572 } else {
1573 locations->SetInAt(0, Location::RequiresRegister());
1574 locations->SetInAt(1, Location::RequiresRegister());
1575 }
1576 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1577 locations->SetInAt(2, Location::RequiresRegister());
1578 }
1579 locations->SetOut(Location::SameAsFirstInput());
1580}
1581
1582void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1583 LocationSummary* locations = select->GetLocations();
1584 NearLabel false_target;
1585 GenerateTestAndBranch<NearLabel>(select,
1586 /* condition_input_index */ 2,
1587 /* true_target */ nullptr,
1588 &false_target);
1589 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1590 __ Bind(&false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001591}
1592
David Srbecky0cf44932015-12-09 14:09:59 +00001593void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1594 new (GetGraph()->GetArena()) LocationSummary(info);
1595}
1596
1597void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001598 if (codegen_->HasStackMapAtCurrentPc()) {
1599 // Ensure that we do not collide with the stack map of the previous instruction.
1600 __ nop();
1601 }
David Srbecky0cf44932015-12-09 14:09:59 +00001602 codegen_->RecordPcInfo(info, info->GetDexPc());
1603}
1604
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001605void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1606 local->SetLocations(nullptr);
1607}
1608
1609void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1610 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1611}
1612
1613void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1614 local->SetLocations(nullptr);
1615}
1616
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001617void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001618 // Nothing to do, this is driven by the code generator.
1619}
1620
1621void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001622 LocationSummary* locations =
1623 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001624 switch (store->InputAt(1)->GetType()) {
1625 case Primitive::kPrimBoolean:
1626 case Primitive::kPrimByte:
1627 case Primitive::kPrimChar:
1628 case Primitive::kPrimShort:
1629 case Primitive::kPrimInt:
1630 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001631 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001632 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1633 break;
1634
1635 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001636 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001637 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1638 break;
1639
1640 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001641 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001642 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001643}
1644
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001645void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001646}
1647
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001648void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001649 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001650 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001651 // Handle the long/FP comparisons made in instruction simplification.
1652 switch (cond->InputAt(0)->GetType()) {
1653 case Primitive::kPrimLong:
1654 locations->SetInAt(0, Location::RequiresRegister());
1655 locations->SetInAt(1, Location::Any());
1656 break;
1657 case Primitive::kPrimFloat:
1658 case Primitive::kPrimDouble:
1659 locations->SetInAt(0, Location::RequiresFpuRegister());
1660 locations->SetInAt(1, Location::Any());
1661 break;
1662 default:
1663 locations->SetInAt(0, Location::RequiresRegister());
1664 locations->SetInAt(1, Location::Any());
1665 break;
1666 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001667 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001668 locations->SetOut(Location::RequiresRegister());
1669 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001670}
1671
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001672void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001673 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001674 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001675 }
Mark Mendellc4701932015-04-10 13:18:51 -04001676
1677 LocationSummary* locations = cond->GetLocations();
1678 Location lhs = locations->InAt(0);
1679 Location rhs = locations->InAt(1);
1680 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001681 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001682
1683 switch (cond->InputAt(0)->GetType()) {
1684 default:
1685 // Integer case.
1686
1687 // Clear output register: setcc only sets the low byte.
1688 __ xorl(reg, reg);
1689
1690 if (rhs.IsRegister()) {
1691 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1692 } else if (rhs.IsConstant()) {
1693 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1694 if (constant == 0) {
1695 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1696 } else {
1697 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1698 }
1699 } else {
1700 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1701 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001702 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001703 return;
1704 case Primitive::kPrimLong:
1705 // Clear output register: setcc only sets the low byte.
1706 __ xorl(reg, reg);
1707
1708 if (rhs.IsRegister()) {
1709 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1710 } else if (rhs.IsConstant()) {
1711 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1712 if (IsInt<32>(value)) {
1713 if (value == 0) {
1714 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1715 } else {
1716 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1717 }
1718 } else {
1719 // Value won't fit in an int.
1720 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1721 }
1722 } else {
1723 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1724 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001725 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001726 return;
1727 case Primitive::kPrimFloat: {
1728 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1729 if (rhs.IsConstant()) {
1730 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1731 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1732 } else if (rhs.IsStackSlot()) {
1733 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1734 } else {
1735 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1736 }
1737 GenerateFPJumps(cond, &true_label, &false_label);
1738 break;
1739 }
1740 case Primitive::kPrimDouble: {
1741 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1742 if (rhs.IsConstant()) {
1743 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1744 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1745 } else if (rhs.IsDoubleStackSlot()) {
1746 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1747 } else {
1748 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1749 }
1750 GenerateFPJumps(cond, &true_label, &false_label);
1751 break;
1752 }
1753 }
1754
1755 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001756 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001757
Roland Levillain4fa13f62015-07-06 18:11:54 +01001758 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001759 __ Bind(&false_label);
1760 __ xorl(reg, reg);
1761 __ jmp(&done_label);
1762
Roland Levillain4fa13f62015-07-06 18:11:54 +01001763 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001764 __ Bind(&true_label);
1765 __ movl(reg, Immediate(1));
1766 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001767}
1768
1769void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001771}
1772
1773void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001775}
1776
1777void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001779}
1780
1781void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001783}
1784
1785void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001787}
1788
1789void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001791}
1792
1793void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001795}
1796
1797void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001799}
1800
1801void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001803}
1804
1805void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001807}
1808
1809void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001810 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001811}
1812
1813void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001815}
1816
Aart Bike9f37602015-10-09 11:15:55 -07001817void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001818 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001819}
1820
1821void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001822 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001823}
1824
1825void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001826 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001827}
1828
1829void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001830 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001831}
1832
1833void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001834 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001835}
1836
1837void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001838 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001839}
1840
1841void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001842 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001843}
1844
1845void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001846 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001847}
1848
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001849void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001850 LocationSummary* locations =
1851 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001852 switch (compare->InputAt(0)->GetType()) {
1853 case Primitive::kPrimLong: {
1854 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001855 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001856 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1857 break;
1858 }
1859 case Primitive::kPrimFloat:
1860 case Primitive::kPrimDouble: {
1861 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001862 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001863 locations->SetOut(Location::RequiresRegister());
1864 break;
1865 }
1866 default:
1867 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1868 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001869}
1870
1871void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001872 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001873 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001874 Location left = locations->InAt(0);
1875 Location right = locations->InAt(1);
1876
Mark Mendell0c9497d2015-08-21 09:30:05 -04001877 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001878 Primitive::Type type = compare->InputAt(0)->GetType();
1879 switch (type) {
1880 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001881 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1882 if (right.IsConstant()) {
1883 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001884 if (IsInt<32>(value)) {
1885 if (value == 0) {
1886 __ testq(left_reg, left_reg);
1887 } else {
1888 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1889 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001890 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001891 // Value won't fit in an int.
1892 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001893 }
Mark Mendell40741f32015-04-20 22:10:34 -04001894 } else if (right.IsDoubleStackSlot()) {
1895 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001896 } else {
1897 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1898 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001899 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001900 }
1901 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001902 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1903 if (right.IsConstant()) {
1904 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1905 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1906 } else if (right.IsStackSlot()) {
1907 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1908 } else {
1909 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1910 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001911 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1912 break;
1913 }
1914 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001915 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1916 if (right.IsConstant()) {
1917 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1918 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1919 } else if (right.IsDoubleStackSlot()) {
1920 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1921 } else {
1922 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1923 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001924 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1925 break;
1926 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001927 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001928 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001929 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001930 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001931 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001932 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001933
Calin Juravle91debbc2014-11-26 19:01:09 +00001934 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001935 __ movl(out, Immediate(1));
1936 __ jmp(&done);
1937
1938 __ Bind(&less);
1939 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001940
1941 __ Bind(&done);
1942}
1943
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001944void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001945 LocationSummary* locations =
1946 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001947 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001948}
1949
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001950void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001951 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001952}
1953
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001954void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1955 LocationSummary* locations =
1956 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1957 locations->SetOut(Location::ConstantLocation(constant));
1958}
1959
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001960void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001961 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001962}
1963
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001964void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001965 LocationSummary* locations =
1966 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001967 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001968}
1969
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001970void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001971 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001972}
1973
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001974void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1975 LocationSummary* locations =
1976 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1977 locations->SetOut(Location::ConstantLocation(constant));
1978}
1979
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001980void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001981 // Will be generated at use site.
1982}
1983
1984void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1985 LocationSummary* locations =
1986 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1987 locations->SetOut(Location::ConstantLocation(constant));
1988}
1989
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001990void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1991 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001992 // Will be generated at use site.
1993}
1994
Calin Juravle27df7582015-04-17 19:12:31 +01001995void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1996 memory_barrier->SetLocations(nullptr);
1997}
1998
1999void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002000 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002001}
2002
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002003void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2004 ret->SetLocations(nullptr);
2005}
2006
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002007void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002008 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002009}
2010
2011void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002012 LocationSummary* locations =
2013 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002014 switch (ret->InputAt(0)->GetType()) {
2015 case Primitive::kPrimBoolean:
2016 case Primitive::kPrimByte:
2017 case Primitive::kPrimChar:
2018 case Primitive::kPrimShort:
2019 case Primitive::kPrimInt:
2020 case Primitive::kPrimNot:
2021 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002022 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002023 break;
2024
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002025 case Primitive::kPrimFloat:
2026 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002027 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002028 break;
2029
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002030 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002031 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002032 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002033}
2034
2035void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2036 if (kIsDebugBuild) {
2037 switch (ret->InputAt(0)->GetType()) {
2038 case Primitive::kPrimBoolean:
2039 case Primitive::kPrimByte:
2040 case Primitive::kPrimChar:
2041 case Primitive::kPrimShort:
2042 case Primitive::kPrimInt:
2043 case Primitive::kPrimNot:
2044 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002046 break;
2047
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002048 case Primitive::kPrimFloat:
2049 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002051 XMM0);
2052 break;
2053
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002054 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002055 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002056 }
2057 }
2058 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002059}
2060
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002061Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2062 switch (type) {
2063 case Primitive::kPrimBoolean:
2064 case Primitive::kPrimByte:
2065 case Primitive::kPrimChar:
2066 case Primitive::kPrimShort:
2067 case Primitive::kPrimInt:
2068 case Primitive::kPrimNot:
2069 case Primitive::kPrimLong:
2070 return Location::RegisterLocation(RAX);
2071
2072 case Primitive::kPrimVoid:
2073 return Location::NoLocation();
2074
2075 case Primitive::kPrimDouble:
2076 case Primitive::kPrimFloat:
2077 return Location::FpuRegisterLocation(XMM0);
2078 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002079
2080 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002081}
2082
2083Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2084 return Location::RegisterLocation(kMethodRegisterArgument);
2085}
2086
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002087Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002088 switch (type) {
2089 case Primitive::kPrimBoolean:
2090 case Primitive::kPrimByte:
2091 case Primitive::kPrimChar:
2092 case Primitive::kPrimShort:
2093 case Primitive::kPrimInt:
2094 case Primitive::kPrimNot: {
2095 uint32_t index = gp_index_++;
2096 stack_index_++;
2097 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002098 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002099 } else {
2100 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2101 }
2102 }
2103
2104 case Primitive::kPrimLong: {
2105 uint32_t index = gp_index_;
2106 stack_index_ += 2;
2107 if (index < calling_convention.GetNumberOfRegisters()) {
2108 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002109 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002110 } else {
2111 gp_index_ += 2;
2112 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2113 }
2114 }
2115
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002116 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002117 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002118 stack_index_++;
2119 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002120 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121 } else {
2122 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2123 }
2124 }
2125
2126 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002127 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002128 stack_index_ += 2;
2129 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002130 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002131 } else {
2132 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2133 }
2134 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002135
2136 case Primitive::kPrimVoid:
2137 LOG(FATAL) << "Unexpected parameter type " << type;
2138 break;
2139 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002140 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002141}
2142
Calin Juravle175dc732015-08-25 15:42:32 +01002143void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2144 // The trampoline uses the same calling convention as dex calling conventions,
2145 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2146 // the method_idx.
2147 HandleInvoke(invoke);
2148}
2149
2150void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2151 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2152}
2153
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002154void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002155 // Explicit clinit checks triggered by static invokes must have been pruned by
2156 // art::PrepareForRegisterAllocation.
2157 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002158
Mark Mendellfb8d2792015-03-31 22:16:59 -04002159 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002160 if (intrinsic.TryDispatch(invoke)) {
2161 return;
2162 }
2163
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002164 HandleInvoke(invoke);
2165}
2166
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002167static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2168 if (invoke->GetLocations()->Intrinsified()) {
2169 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2170 intrinsic.Dispatch(invoke);
2171 return true;
2172 }
2173 return false;
2174}
2175
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002176void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002177 // Explicit clinit checks triggered by static invokes must have been pruned by
2178 // art::PrepareForRegisterAllocation.
2179 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002180
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002181 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2182 return;
2183 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002184
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002185 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002186 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002187 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002188 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002189}
2190
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002191void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002192 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002193 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002194}
2195
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002196void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002197 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002198 if (intrinsic.TryDispatch(invoke)) {
2199 return;
2200 }
2201
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002202 HandleInvoke(invoke);
2203}
2204
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002205void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002206 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2207 return;
2208 }
2209
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002210 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002211 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002212 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002213}
2214
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002215void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2216 HandleInvoke(invoke);
2217 // Add the hidden argument.
2218 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2219}
2220
2221void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2222 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002223 LocationSummary* locations = invoke->GetLocations();
2224 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2225 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002226 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2227 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002228 Location receiver = locations->InAt(0);
2229 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2230
Roland Levillain0d5a2812015-11-13 10:07:31 +00002231 // Set the hidden argument. This is safe to do this here, as RAX
2232 // won't be modified thereafter, before the `call` instruction.
2233 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002234 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002235
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002236 if (receiver.IsStackSlot()) {
2237 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002238 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002239 __ movl(temp, Address(temp, class_offset));
2240 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002241 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002242 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002243 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002244 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002245 // Instead of simply (possibly) unpoisoning `temp` here, we should
2246 // emit a read barrier for the previous class reference load.
2247 // However this is not required in practice, as this is an
2248 // intermediate/temporary reference and because the current
2249 // concurrent copying collector keeps the from-space memory
2250 // intact/accessible until the end of the marking phase (the
2251 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002252 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002253 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002254 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002255 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002256 __ call(Address(temp,
2257 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258
2259 DCHECK(!codegen_->IsLeafMethod());
2260 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2261}
2262
Roland Levillain88cb1752014-10-20 16:36:47 +01002263void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2264 LocationSummary* locations =
2265 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2266 switch (neg->GetResultType()) {
2267 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002268 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002269 locations->SetInAt(0, Location::RequiresRegister());
2270 locations->SetOut(Location::SameAsFirstInput());
2271 break;
2272
Roland Levillain88cb1752014-10-20 16:36:47 +01002273 case Primitive::kPrimFloat:
2274 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002275 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002276 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002277 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002278 break;
2279
2280 default:
2281 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2282 }
2283}
2284
2285void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2286 LocationSummary* locations = neg->GetLocations();
2287 Location out = locations->Out();
2288 Location in = locations->InAt(0);
2289 switch (neg->GetResultType()) {
2290 case Primitive::kPrimInt:
2291 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002292 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002293 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002294 break;
2295
2296 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002297 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002298 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002299 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002300 break;
2301
Roland Levillain5368c212014-11-27 15:03:41 +00002302 case Primitive::kPrimFloat: {
2303 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002304 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002305 // Implement float negation with an exclusive or with value
2306 // 0x80000000 (mask for bit 31, representing the sign of a
2307 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002308 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002309 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002310 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002311 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002312
Roland Levillain5368c212014-11-27 15:03:41 +00002313 case Primitive::kPrimDouble: {
2314 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002315 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002316 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002317 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002318 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002319 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002320 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002321 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002322 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002323
2324 default:
2325 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2326 }
2327}
2328
Roland Levillaindff1f282014-11-05 14:15:05 +00002329void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2330 LocationSummary* locations =
2331 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2332 Primitive::Type result_type = conversion->GetResultType();
2333 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002334 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002335
David Brazdilb2bd1c52015-03-25 11:17:37 +00002336 // The Java language does not allow treating boolean as an integral type but
2337 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002338
Roland Levillaindff1f282014-11-05 14:15:05 +00002339 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002340 case Primitive::kPrimByte:
2341 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002342 case Primitive::kPrimBoolean:
2343 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002344 case Primitive::kPrimShort:
2345 case Primitive::kPrimInt:
2346 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002347 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002348 locations->SetInAt(0, Location::Any());
2349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2350 break;
2351
2352 default:
2353 LOG(FATAL) << "Unexpected type conversion from " << input_type
2354 << " to " << result_type;
2355 }
2356 break;
2357
Roland Levillain01a8d712014-11-14 16:27:39 +00002358 case Primitive::kPrimShort:
2359 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002360 case Primitive::kPrimBoolean:
2361 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002362 case Primitive::kPrimByte:
2363 case Primitive::kPrimInt:
2364 case Primitive::kPrimChar:
2365 // Processing a Dex `int-to-short' instruction.
2366 locations->SetInAt(0, Location::Any());
2367 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2368 break;
2369
2370 default:
2371 LOG(FATAL) << "Unexpected type conversion from " << input_type
2372 << " to " << result_type;
2373 }
2374 break;
2375
Roland Levillain946e1432014-11-11 17:35:19 +00002376 case Primitive::kPrimInt:
2377 switch (input_type) {
2378 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002379 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002380 locations->SetInAt(0, Location::Any());
2381 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2382 break;
2383
2384 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002385 // Processing a Dex `float-to-int' instruction.
2386 locations->SetInAt(0, Location::RequiresFpuRegister());
2387 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002388 break;
2389
Roland Levillain946e1432014-11-11 17:35:19 +00002390 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002391 // Processing a Dex `double-to-int' instruction.
2392 locations->SetInAt(0, Location::RequiresFpuRegister());
2393 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002394 break;
2395
2396 default:
2397 LOG(FATAL) << "Unexpected type conversion from " << input_type
2398 << " to " << result_type;
2399 }
2400 break;
2401
Roland Levillaindff1f282014-11-05 14:15:05 +00002402 case Primitive::kPrimLong:
2403 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002404 case Primitive::kPrimBoolean:
2405 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002406 case Primitive::kPrimByte:
2407 case Primitive::kPrimShort:
2408 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002409 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002410 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002411 // TODO: We would benefit from a (to-be-implemented)
2412 // Location::RegisterOrStackSlot requirement for this input.
2413 locations->SetInAt(0, Location::RequiresRegister());
2414 locations->SetOut(Location::RequiresRegister());
2415 break;
2416
2417 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002418 // Processing a Dex `float-to-long' instruction.
2419 locations->SetInAt(0, Location::RequiresFpuRegister());
2420 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002421 break;
2422
Roland Levillaindff1f282014-11-05 14:15:05 +00002423 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002424 // Processing a Dex `double-to-long' instruction.
2425 locations->SetInAt(0, Location::RequiresFpuRegister());
2426 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002427 break;
2428
2429 default:
2430 LOG(FATAL) << "Unexpected type conversion from " << input_type
2431 << " to " << result_type;
2432 }
2433 break;
2434
Roland Levillain981e4542014-11-14 11:47:14 +00002435 case Primitive::kPrimChar:
2436 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002437 case Primitive::kPrimBoolean:
2438 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002439 case Primitive::kPrimByte:
2440 case Primitive::kPrimShort:
2441 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002442 // Processing a Dex `int-to-char' instruction.
2443 locations->SetInAt(0, Location::Any());
2444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2445 break;
2446
2447 default:
2448 LOG(FATAL) << "Unexpected type conversion from " << input_type
2449 << " to " << result_type;
2450 }
2451 break;
2452
Roland Levillaindff1f282014-11-05 14:15:05 +00002453 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002454 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002455 case Primitive::kPrimBoolean:
2456 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002457 case Primitive::kPrimByte:
2458 case Primitive::kPrimShort:
2459 case Primitive::kPrimInt:
2460 case Primitive::kPrimChar:
2461 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002462 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002463 locations->SetOut(Location::RequiresFpuRegister());
2464 break;
2465
2466 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002467 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002468 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002469 locations->SetOut(Location::RequiresFpuRegister());
2470 break;
2471
Roland Levillaincff13742014-11-17 14:32:17 +00002472 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002473 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002474 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002475 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002476 break;
2477
2478 default:
2479 LOG(FATAL) << "Unexpected type conversion from " << input_type
2480 << " to " << result_type;
2481 };
2482 break;
2483
Roland Levillaindff1f282014-11-05 14:15:05 +00002484 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002485 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002486 case Primitive::kPrimBoolean:
2487 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002488 case Primitive::kPrimByte:
2489 case Primitive::kPrimShort:
2490 case Primitive::kPrimInt:
2491 case Primitive::kPrimChar:
2492 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002493 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002494 locations->SetOut(Location::RequiresFpuRegister());
2495 break;
2496
2497 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002498 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002499 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002500 locations->SetOut(Location::RequiresFpuRegister());
2501 break;
2502
Roland Levillaincff13742014-11-17 14:32:17 +00002503 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002504 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002505 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002506 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002507 break;
2508
2509 default:
2510 LOG(FATAL) << "Unexpected type conversion from " << input_type
2511 << " to " << result_type;
2512 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002513 break;
2514
2515 default:
2516 LOG(FATAL) << "Unexpected type conversion from " << input_type
2517 << " to " << result_type;
2518 }
2519}
2520
2521void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2522 LocationSummary* locations = conversion->GetLocations();
2523 Location out = locations->Out();
2524 Location in = locations->InAt(0);
2525 Primitive::Type result_type = conversion->GetResultType();
2526 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002527 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002528 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002529 case Primitive::kPrimByte:
2530 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002531 case Primitive::kPrimBoolean:
2532 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002533 case Primitive::kPrimShort:
2534 case Primitive::kPrimInt:
2535 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002536 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002537 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002538 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002539 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002540 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002541 Address(CpuRegister(RSP), in.GetStackIndex()));
2542 } else {
2543 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002544 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002545 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2546 }
2547 break;
2548
2549 default:
2550 LOG(FATAL) << "Unexpected type conversion from " << input_type
2551 << " to " << result_type;
2552 }
2553 break;
2554
Roland Levillain01a8d712014-11-14 16:27:39 +00002555 case Primitive::kPrimShort:
2556 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002557 case Primitive::kPrimBoolean:
2558 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002559 case Primitive::kPrimByte:
2560 case Primitive::kPrimInt:
2561 case Primitive::kPrimChar:
2562 // Processing a Dex `int-to-short' instruction.
2563 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002564 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002565 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002566 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002567 Address(CpuRegister(RSP), in.GetStackIndex()));
2568 } else {
2569 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002570 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002571 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2572 }
2573 break;
2574
2575 default:
2576 LOG(FATAL) << "Unexpected type conversion from " << input_type
2577 << " to " << result_type;
2578 }
2579 break;
2580
Roland Levillain946e1432014-11-11 17:35:19 +00002581 case Primitive::kPrimInt:
2582 switch (input_type) {
2583 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002584 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002585 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002586 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002587 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002588 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002589 Address(CpuRegister(RSP), in.GetStackIndex()));
2590 } else {
2591 DCHECK(in.IsConstant());
2592 DCHECK(in.GetConstant()->IsLongConstant());
2593 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002595 }
2596 break;
2597
Roland Levillain3f8f9362014-12-02 17:45:01 +00002598 case Primitive::kPrimFloat: {
2599 // Processing a Dex `float-to-int' instruction.
2600 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2601 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002602 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002603
2604 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002605 // if input >= (float)INT_MAX goto done
2606 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002607 __ j(kAboveEqual, &done);
2608 // if input == NaN goto nan
2609 __ j(kUnordered, &nan);
2610 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002611 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002612 __ jmp(&done);
2613 __ Bind(&nan);
2614 // output = 0
2615 __ xorl(output, output);
2616 __ Bind(&done);
2617 break;
2618 }
2619
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002620 case Primitive::kPrimDouble: {
2621 // Processing a Dex `double-to-int' instruction.
2622 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2623 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002624 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002625
2626 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002627 // if input >= (double)INT_MAX goto done
2628 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002629 __ j(kAboveEqual, &done);
2630 // if input == NaN goto nan
2631 __ j(kUnordered, &nan);
2632 // output = double-to-int-truncate(input)
2633 __ cvttsd2si(output, input);
2634 __ jmp(&done);
2635 __ Bind(&nan);
2636 // output = 0
2637 __ xorl(output, output);
2638 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002639 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002640 }
Roland Levillain946e1432014-11-11 17:35:19 +00002641
2642 default:
2643 LOG(FATAL) << "Unexpected type conversion from " << input_type
2644 << " to " << result_type;
2645 }
2646 break;
2647
Roland Levillaindff1f282014-11-05 14:15:05 +00002648 case Primitive::kPrimLong:
2649 switch (input_type) {
2650 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002651 case Primitive::kPrimBoolean:
2652 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002653 case Primitive::kPrimByte:
2654 case Primitive::kPrimShort:
2655 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002656 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002657 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002658 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002659 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002660 break;
2661
Roland Levillain624279f2014-12-04 11:54:28 +00002662 case Primitive::kPrimFloat: {
2663 // Processing a Dex `float-to-long' instruction.
2664 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2665 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002666 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002667
Mark Mendell92e83bf2015-05-07 11:25:03 -04002668 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002669 // if input >= (float)LONG_MAX goto done
2670 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002671 __ j(kAboveEqual, &done);
2672 // if input == NaN goto nan
2673 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002674 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002675 __ cvttss2si(output, input, true);
2676 __ jmp(&done);
2677 __ Bind(&nan);
2678 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002679 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002680 __ Bind(&done);
2681 break;
2682 }
2683
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002684 case Primitive::kPrimDouble: {
2685 // Processing a Dex `double-to-long' instruction.
2686 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2687 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002688 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002689
Mark Mendell92e83bf2015-05-07 11:25:03 -04002690 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002691 // if input >= (double)LONG_MAX goto done
2692 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002693 __ j(kAboveEqual, &done);
2694 // if input == NaN goto nan
2695 __ j(kUnordered, &nan);
2696 // output = double-to-long-truncate(input)
2697 __ cvttsd2si(output, input, true);
2698 __ jmp(&done);
2699 __ Bind(&nan);
2700 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002701 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002702 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002703 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002704 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002705
2706 default:
2707 LOG(FATAL) << "Unexpected type conversion from " << input_type
2708 << " to " << result_type;
2709 }
2710 break;
2711
Roland Levillain981e4542014-11-14 11:47:14 +00002712 case Primitive::kPrimChar:
2713 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002714 case Primitive::kPrimBoolean:
2715 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002716 case Primitive::kPrimByte:
2717 case Primitive::kPrimShort:
2718 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002719 // Processing a Dex `int-to-char' instruction.
2720 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002721 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002722 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002724 Address(CpuRegister(RSP), in.GetStackIndex()));
2725 } else {
2726 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002727 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002728 Immediate(static_cast<uint16_t>(
2729 in.GetConstant()->AsIntConstant()->GetValue())));
Roland Levillain981e4542014-11-14 11:47:14 +00002730 }
2731 break;
2732
2733 default:
2734 LOG(FATAL) << "Unexpected type conversion from " << input_type
2735 << " to " << result_type;
2736 }
2737 break;
2738
Roland Levillaindff1f282014-11-05 14:15:05 +00002739 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002740 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002741 case Primitive::kPrimBoolean:
2742 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002743 case Primitive::kPrimByte:
2744 case Primitive::kPrimShort:
2745 case Primitive::kPrimInt:
2746 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002747 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002748 if (in.IsRegister()) {
2749 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2750 } else if (in.IsConstant()) {
2751 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2752 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2753 if (v == 0) {
2754 __ xorps(dest, dest);
2755 } else {
2756 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2757 }
2758 } else {
2759 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2760 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2761 }
Roland Levillaincff13742014-11-17 14:32:17 +00002762 break;
2763
2764 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002765 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002766 if (in.IsRegister()) {
2767 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2768 } else if (in.IsConstant()) {
2769 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2770 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2771 if (v == 0) {
2772 __ xorps(dest, dest);
2773 } else {
2774 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2775 }
2776 } else {
2777 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2778 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2779 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002780 break;
2781
Roland Levillaincff13742014-11-17 14:32:17 +00002782 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002783 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002784 if (in.IsFpuRegister()) {
2785 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2786 } else if (in.IsConstant()) {
2787 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2788 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2789 if (bit_cast<int64_t, double>(v) == 0) {
2790 __ xorps(dest, dest);
2791 } else {
2792 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2793 }
2794 } else {
2795 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2796 Address(CpuRegister(RSP), in.GetStackIndex()));
2797 }
Roland Levillaincff13742014-11-17 14:32:17 +00002798 break;
2799
2800 default:
2801 LOG(FATAL) << "Unexpected type conversion from " << input_type
2802 << " to " << result_type;
2803 };
2804 break;
2805
Roland Levillaindff1f282014-11-05 14:15:05 +00002806 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002807 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002808 case Primitive::kPrimBoolean:
2809 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002810 case Primitive::kPrimByte:
2811 case Primitive::kPrimShort:
2812 case Primitive::kPrimInt:
2813 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002814 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002815 if (in.IsRegister()) {
2816 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2817 } else if (in.IsConstant()) {
2818 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2819 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2820 if (v == 0) {
2821 __ xorpd(dest, dest);
2822 } else {
2823 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2824 }
2825 } else {
2826 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2827 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2828 }
Roland Levillaincff13742014-11-17 14:32:17 +00002829 break;
2830
2831 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002832 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002833 if (in.IsRegister()) {
2834 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2835 } else if (in.IsConstant()) {
2836 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2837 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2838 if (v == 0) {
2839 __ xorpd(dest, dest);
2840 } else {
2841 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2842 }
2843 } else {
2844 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2845 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2846 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002847 break;
2848
Roland Levillaincff13742014-11-17 14:32:17 +00002849 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002850 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002851 if (in.IsFpuRegister()) {
2852 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2853 } else if (in.IsConstant()) {
2854 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2855 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2856 if (bit_cast<int32_t, float>(v) == 0) {
2857 __ xorpd(dest, dest);
2858 } else {
2859 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2860 }
2861 } else {
2862 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2863 Address(CpuRegister(RSP), in.GetStackIndex()));
2864 }
Roland Levillaincff13742014-11-17 14:32:17 +00002865 break;
2866
2867 default:
2868 LOG(FATAL) << "Unexpected type conversion from " << input_type
2869 << " to " << result_type;
2870 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002871 break;
2872
2873 default:
2874 LOG(FATAL) << "Unexpected type conversion from " << input_type
2875 << " to " << result_type;
2876 }
2877}
2878
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002879void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002880 LocationSummary* locations =
2881 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002882 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002883 case Primitive::kPrimInt: {
2884 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002885 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002887 break;
2888 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002889
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002890 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002891 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002892 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002893 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002894 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002895 break;
2896 }
2897
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002898 case Primitive::kPrimDouble:
2899 case Primitive::kPrimFloat: {
2900 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002901 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002902 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002903 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002904 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002905
2906 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002907 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002908 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002909}
2910
2911void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2912 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002913 Location first = locations->InAt(0);
2914 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002915 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002916
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002917 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002918 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002919 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002920 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2921 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002922 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2923 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002924 } else {
2925 __ leal(out.AsRegister<CpuRegister>(), Address(
2926 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2927 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002928 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002929 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2930 __ addl(out.AsRegister<CpuRegister>(),
2931 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2932 } else {
2933 __ leal(out.AsRegister<CpuRegister>(), Address(
2934 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2935 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002936 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002937 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002938 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002939 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002940 break;
2941 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002942
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002943 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002944 if (second.IsRegister()) {
2945 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2946 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002947 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2948 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002949 } else {
2950 __ leaq(out.AsRegister<CpuRegister>(), Address(
2951 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2952 }
2953 } else {
2954 DCHECK(second.IsConstant());
2955 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2956 int32_t int32_value = Low32Bits(value);
2957 DCHECK_EQ(int32_value, value);
2958 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2959 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2960 } else {
2961 __ leaq(out.AsRegister<CpuRegister>(), Address(
2962 first.AsRegister<CpuRegister>(), int32_value));
2963 }
2964 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002965 break;
2966 }
2967
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002969 if (second.IsFpuRegister()) {
2970 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2971 } else if (second.IsConstant()) {
2972 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002973 codegen_->LiteralFloatAddress(
2974 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002975 } else {
2976 DCHECK(second.IsStackSlot());
2977 __ addss(first.AsFpuRegister<XmmRegister>(),
2978 Address(CpuRegister(RSP), second.GetStackIndex()));
2979 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002980 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002981 }
2982
2983 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002984 if (second.IsFpuRegister()) {
2985 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2986 } else if (second.IsConstant()) {
2987 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002988 codegen_->LiteralDoubleAddress(
2989 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002990 } else {
2991 DCHECK(second.IsDoubleStackSlot());
2992 __ addsd(first.AsFpuRegister<XmmRegister>(),
2993 Address(CpuRegister(RSP), second.GetStackIndex()));
2994 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002995 break;
2996 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002997
2998 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002999 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003000 }
3001}
3002
3003void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003004 LocationSummary* locations =
3005 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003006 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003007 case Primitive::kPrimInt: {
3008 locations->SetInAt(0, Location::RequiresRegister());
3009 locations->SetInAt(1, Location::Any());
3010 locations->SetOut(Location::SameAsFirstInput());
3011 break;
3012 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003013 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003014 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003015 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003016 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003017 break;
3018 }
Calin Juravle11351682014-10-23 15:38:15 +01003019 case Primitive::kPrimFloat:
3020 case Primitive::kPrimDouble: {
3021 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003022 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003023 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003024 break;
Calin Juravle11351682014-10-23 15:38:15 +01003025 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003026 default:
Calin Juravle11351682014-10-23 15:38:15 +01003027 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003028 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003029}
3030
3031void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3032 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003033 Location first = locations->InAt(0);
3034 Location second = locations->InAt(1);
3035 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003036 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003037 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003038 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003039 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003040 } else if (second.IsConstant()) {
3041 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003042 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003043 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003044 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003045 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003046 break;
3047 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003048 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003049 if (second.IsConstant()) {
3050 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3051 DCHECK(IsInt<32>(value));
3052 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3053 } else {
3054 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3055 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003056 break;
3057 }
3058
Calin Juravle11351682014-10-23 15:38:15 +01003059 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003060 if (second.IsFpuRegister()) {
3061 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3062 } else if (second.IsConstant()) {
3063 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003064 codegen_->LiteralFloatAddress(
3065 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003066 } else {
3067 DCHECK(second.IsStackSlot());
3068 __ subss(first.AsFpuRegister<XmmRegister>(),
3069 Address(CpuRegister(RSP), second.GetStackIndex()));
3070 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003071 break;
Calin Juravle11351682014-10-23 15:38:15 +01003072 }
3073
3074 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003075 if (second.IsFpuRegister()) {
3076 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3077 } else if (second.IsConstant()) {
3078 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003079 codegen_->LiteralDoubleAddress(
3080 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003081 } else {
3082 DCHECK(second.IsDoubleStackSlot());
3083 __ subsd(first.AsFpuRegister<XmmRegister>(),
3084 Address(CpuRegister(RSP), second.GetStackIndex()));
3085 }
Calin Juravle11351682014-10-23 15:38:15 +01003086 break;
3087 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003088
3089 default:
Calin Juravle11351682014-10-23 15:38:15 +01003090 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003091 }
3092}
3093
Calin Juravle34bacdf2014-10-07 20:23:36 +01003094void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3095 LocationSummary* locations =
3096 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3097 switch (mul->GetResultType()) {
3098 case Primitive::kPrimInt: {
3099 locations->SetInAt(0, Location::RequiresRegister());
3100 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003101 if (mul->InputAt(1)->IsIntConstant()) {
3102 // Can use 3 operand multiply.
3103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3104 } else {
3105 locations->SetOut(Location::SameAsFirstInput());
3106 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003107 break;
3108 }
3109 case Primitive::kPrimLong: {
3110 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003111 locations->SetInAt(1, Location::Any());
3112 if (mul->InputAt(1)->IsLongConstant() &&
3113 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003114 // Can use 3 operand multiply.
3115 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3116 } else {
3117 locations->SetOut(Location::SameAsFirstInput());
3118 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003119 break;
3120 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003121 case Primitive::kPrimFloat:
3122 case Primitive::kPrimDouble: {
3123 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003124 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003125 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003126 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003127 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003128
3129 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003130 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003131 }
3132}
3133
3134void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3135 LocationSummary* locations = mul->GetLocations();
3136 Location first = locations->InAt(0);
3137 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003138 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003139 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003140 case Primitive::kPrimInt:
3141 // The constant may have ended up in a register, so test explicitly to avoid
3142 // problems where the output may not be the same as the first operand.
3143 if (mul->InputAt(1)->IsIntConstant()) {
3144 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3145 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3146 } else if (second.IsRegister()) {
3147 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003148 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003149 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003150 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003151 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003152 __ imull(first.AsRegister<CpuRegister>(),
3153 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003154 }
3155 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003156 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003157 // The constant may have ended up in a register, so test explicitly to avoid
3158 // problems where the output may not be the same as the first operand.
3159 if (mul->InputAt(1)->IsLongConstant()) {
3160 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3161 if (IsInt<32>(value)) {
3162 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3163 Immediate(static_cast<int32_t>(value)));
3164 } else {
3165 // Have to use the constant area.
3166 DCHECK(first.Equals(out));
3167 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3168 }
3169 } else if (second.IsRegister()) {
3170 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003171 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003172 } else {
3173 DCHECK(second.IsDoubleStackSlot());
3174 DCHECK(first.Equals(out));
3175 __ imulq(first.AsRegister<CpuRegister>(),
3176 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003177 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003178 break;
3179 }
3180
Calin Juravleb5bfa962014-10-21 18:02:24 +01003181 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003182 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003183 if (second.IsFpuRegister()) {
3184 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3185 } else if (second.IsConstant()) {
3186 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003187 codegen_->LiteralFloatAddress(
3188 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003189 } else {
3190 DCHECK(second.IsStackSlot());
3191 __ mulss(first.AsFpuRegister<XmmRegister>(),
3192 Address(CpuRegister(RSP), second.GetStackIndex()));
3193 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003194 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003195 }
3196
3197 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003198 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003199 if (second.IsFpuRegister()) {
3200 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3201 } else if (second.IsConstant()) {
3202 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003203 codegen_->LiteralDoubleAddress(
3204 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003205 } else {
3206 DCHECK(second.IsDoubleStackSlot());
3207 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3208 Address(CpuRegister(RSP), second.GetStackIndex()));
3209 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003210 break;
3211 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003212
3213 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003214 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215 }
3216}
3217
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003218void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3219 uint32_t stack_adjustment, bool is_float) {
3220 if (source.IsStackSlot()) {
3221 DCHECK(is_float);
3222 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3223 } else if (source.IsDoubleStackSlot()) {
3224 DCHECK(!is_float);
3225 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3226 } else {
3227 // Write the value to the temporary location on the stack and load to FP stack.
3228 if (is_float) {
3229 Location stack_temp = Location::StackSlot(temp_offset);
3230 codegen_->Move(stack_temp, source);
3231 __ flds(Address(CpuRegister(RSP), temp_offset));
3232 } else {
3233 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3234 codegen_->Move(stack_temp, source);
3235 __ fldl(Address(CpuRegister(RSP), temp_offset));
3236 }
3237 }
3238}
3239
3240void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3241 Primitive::Type type = rem->GetResultType();
3242 bool is_float = type == Primitive::kPrimFloat;
3243 size_t elem_size = Primitive::ComponentSize(type);
3244 LocationSummary* locations = rem->GetLocations();
3245 Location first = locations->InAt(0);
3246 Location second = locations->InAt(1);
3247 Location out = locations->Out();
3248
3249 // Create stack space for 2 elements.
3250 // TODO: enhance register allocator to ask for stack temporaries.
3251 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3252
3253 // Load the values to the FP stack in reverse order, using temporaries if needed.
3254 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3255 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3256
3257 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003258 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003259 __ Bind(&retry);
3260 __ fprem();
3261
3262 // Move FP status to AX.
3263 __ fstsw();
3264
3265 // And see if the argument reduction is complete. This is signaled by the
3266 // C2 FPU flag bit set to 0.
3267 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3268 __ j(kNotEqual, &retry);
3269
3270 // We have settled on the final value. Retrieve it into an XMM register.
3271 // Store FP top of stack to real stack.
3272 if (is_float) {
3273 __ fsts(Address(CpuRegister(RSP), 0));
3274 } else {
3275 __ fstl(Address(CpuRegister(RSP), 0));
3276 }
3277
3278 // Pop the 2 items from the FP stack.
3279 __ fucompp();
3280
3281 // Load the value from the stack into an XMM register.
3282 DCHECK(out.IsFpuRegister()) << out;
3283 if (is_float) {
3284 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3285 } else {
3286 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3287 }
3288
3289 // And remove the temporary stack space we allocated.
3290 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3291}
3292
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003293void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3294 DCHECK(instruction->IsDiv() || instruction->IsRem());
3295
3296 LocationSummary* locations = instruction->GetLocations();
3297 Location second = locations->InAt(1);
3298 DCHECK(second.IsConstant());
3299
3300 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3301 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003302 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003303
3304 DCHECK(imm == 1 || imm == -1);
3305
3306 switch (instruction->GetResultType()) {
3307 case Primitive::kPrimInt: {
3308 if (instruction->IsRem()) {
3309 __ xorl(output_register, output_register);
3310 } else {
3311 __ movl(output_register, input_register);
3312 if (imm == -1) {
3313 __ negl(output_register);
3314 }
3315 }
3316 break;
3317 }
3318
3319 case Primitive::kPrimLong: {
3320 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003321 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003322 } else {
3323 __ movq(output_register, input_register);
3324 if (imm == -1) {
3325 __ negq(output_register);
3326 }
3327 }
3328 break;
3329 }
3330
3331 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003332 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003333 }
3334}
3335
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003336void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003337 LocationSummary* locations = instruction->GetLocations();
3338 Location second = locations->InAt(1);
3339
3340 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3341 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3342
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003343 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003344 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3345 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003346
3347 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3348
3349 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003350 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003351 __ testl(numerator, numerator);
3352 __ cmov(kGreaterEqual, tmp, numerator);
3353 int shift = CTZ(imm);
3354 __ sarl(tmp, Immediate(shift));
3355
3356 if (imm < 0) {
3357 __ negl(tmp);
3358 }
3359
3360 __ movl(output_register, tmp);
3361 } else {
3362 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3363 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3364
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003365 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003366 __ addq(rdx, numerator);
3367 __ testq(numerator, numerator);
3368 __ cmov(kGreaterEqual, rdx, numerator);
3369 int shift = CTZ(imm);
3370 __ sarq(rdx, Immediate(shift));
3371
3372 if (imm < 0) {
3373 __ negq(rdx);
3374 }
3375
3376 __ movq(output_register, rdx);
3377 }
3378}
3379
3380void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3381 DCHECK(instruction->IsDiv() || instruction->IsRem());
3382
3383 LocationSummary* locations = instruction->GetLocations();
3384 Location second = locations->InAt(1);
3385
3386 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3387 : locations->GetTemp(0).AsRegister<CpuRegister>();
3388 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3389 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3390 : locations->Out().AsRegister<CpuRegister>();
3391 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3392
3393 DCHECK_EQ(RAX, eax.AsRegister());
3394 DCHECK_EQ(RDX, edx.AsRegister());
3395 if (instruction->IsDiv()) {
3396 DCHECK_EQ(RAX, out.AsRegister());
3397 } else {
3398 DCHECK_EQ(RDX, out.AsRegister());
3399 }
3400
3401 int64_t magic;
3402 int shift;
3403
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003404 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003405 if (instruction->GetResultType() == Primitive::kPrimInt) {
3406 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3407
3408 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3409
3410 __ movl(numerator, eax);
3411
Mark Mendell0c9497d2015-08-21 09:30:05 -04003412 NearLabel no_div;
3413 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003414 __ testl(eax, eax);
3415 __ j(kNotEqual, &no_div);
3416
3417 __ xorl(out, out);
3418 __ jmp(&end);
3419
3420 __ Bind(&no_div);
3421
3422 __ movl(eax, Immediate(magic));
3423 __ imull(numerator);
3424
3425 if (imm > 0 && magic < 0) {
3426 __ addl(edx, numerator);
3427 } else if (imm < 0 && magic > 0) {
3428 __ subl(edx, numerator);
3429 }
3430
3431 if (shift != 0) {
3432 __ sarl(edx, Immediate(shift));
3433 }
3434
3435 __ movl(eax, edx);
3436 __ shrl(edx, Immediate(31));
3437 __ addl(edx, eax);
3438
3439 if (instruction->IsRem()) {
3440 __ movl(eax, numerator);
3441 __ imull(edx, Immediate(imm));
3442 __ subl(eax, edx);
3443 __ movl(edx, eax);
3444 } else {
3445 __ movl(eax, edx);
3446 }
3447 __ Bind(&end);
3448 } else {
3449 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3450
3451 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3452
3453 CpuRegister rax = eax;
3454 CpuRegister rdx = edx;
3455
3456 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3457
3458 // Save the numerator.
3459 __ movq(numerator, rax);
3460
3461 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003462 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003463
3464 // RDX:RAX = magic * numerator
3465 __ imulq(numerator);
3466
3467 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003468 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003469 __ addq(rdx, numerator);
3470 } else if (imm < 0 && magic > 0) {
3471 // RDX -= numerator
3472 __ subq(rdx, numerator);
3473 }
3474
3475 // Shift if needed.
3476 if (shift != 0) {
3477 __ sarq(rdx, Immediate(shift));
3478 }
3479
3480 // RDX += 1 if RDX < 0
3481 __ movq(rax, rdx);
3482 __ shrq(rdx, Immediate(63));
3483 __ addq(rdx, rax);
3484
3485 if (instruction->IsRem()) {
3486 __ movq(rax, numerator);
3487
3488 if (IsInt<32>(imm)) {
3489 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3490 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003491 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003492 }
3493
3494 __ subq(rax, rdx);
3495 __ movq(rdx, rax);
3496 } else {
3497 __ movq(rax, rdx);
3498 }
3499 }
3500}
3501
Calin Juravlebacfec32014-11-14 15:54:36 +00003502void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3503 DCHECK(instruction->IsDiv() || instruction->IsRem());
3504 Primitive::Type type = instruction->GetResultType();
3505 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3506
3507 bool is_div = instruction->IsDiv();
3508 LocationSummary* locations = instruction->GetLocations();
3509
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3511 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003512
Roland Levillain271ab9c2014-11-27 15:23:57 +00003513 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003514 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003515
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003516 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003517 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003518
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003519 if (imm == 0) {
3520 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3521 } else if (imm == 1 || imm == -1) {
3522 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003523 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003524 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 } else {
3526 DCHECK(imm <= -2 || imm >= 2);
3527 GenerateDivRemWithAnyConstant(instruction);
3528 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003529 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003530 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003531 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3532 out.AsRegister(), type, is_div);
3533 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003534
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003535 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3536 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3537 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3538 // so it's safe to just use negl instead of more complex comparisons.
3539 if (type == Primitive::kPrimInt) {
3540 __ cmpl(second_reg, Immediate(-1));
3541 __ j(kEqual, slow_path->GetEntryLabel());
3542 // edx:eax <- sign-extended of eax
3543 __ cdq();
3544 // eax = quotient, edx = remainder
3545 __ idivl(second_reg);
3546 } else {
3547 __ cmpq(second_reg, Immediate(-1));
3548 __ j(kEqual, slow_path->GetEntryLabel());
3549 // rdx:rax <- sign-extended of rax
3550 __ cqo();
3551 // rax = quotient, rdx = remainder
3552 __ idivq(second_reg);
3553 }
3554 __ Bind(slow_path->GetExitLabel());
3555 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003556}
3557
Calin Juravle7c4954d2014-10-28 16:57:40 +00003558void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3559 LocationSummary* locations =
3560 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3561 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003562 case Primitive::kPrimInt:
3563 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003564 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003565 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003566 locations->SetOut(Location::SameAsFirstInput());
3567 // Intel uses edx:eax as the dividend.
3568 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003569 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3570 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3571 // output and request another temp.
3572 if (div->InputAt(1)->IsConstant()) {
3573 locations->AddTemp(Location::RequiresRegister());
3574 }
Calin Juravled0d48522014-11-04 16:40:20 +00003575 break;
3576 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003577
Calin Juravle7c4954d2014-10-28 16:57:40 +00003578 case Primitive::kPrimFloat:
3579 case Primitive::kPrimDouble: {
3580 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003581 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003582 locations->SetOut(Location::SameAsFirstInput());
3583 break;
3584 }
3585
3586 default:
3587 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3588 }
3589}
3590
3591void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3592 LocationSummary* locations = div->GetLocations();
3593 Location first = locations->InAt(0);
3594 Location second = locations->InAt(1);
3595 DCHECK(first.Equals(locations->Out()));
3596
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003597 Primitive::Type type = div->GetResultType();
3598 switch (type) {
3599 case Primitive::kPrimInt:
3600 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003601 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003602 break;
3603 }
3604
Calin Juravle7c4954d2014-10-28 16:57:40 +00003605 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003606 if (second.IsFpuRegister()) {
3607 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3608 } else if (second.IsConstant()) {
3609 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003610 codegen_->LiteralFloatAddress(
3611 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003612 } else {
3613 DCHECK(second.IsStackSlot());
3614 __ divss(first.AsFpuRegister<XmmRegister>(),
3615 Address(CpuRegister(RSP), second.GetStackIndex()));
3616 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003617 break;
3618 }
3619
3620 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003621 if (second.IsFpuRegister()) {
3622 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3623 } else if (second.IsConstant()) {
3624 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003625 codegen_->LiteralDoubleAddress(
3626 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003627 } else {
3628 DCHECK(second.IsDoubleStackSlot());
3629 __ divsd(first.AsFpuRegister<XmmRegister>(),
3630 Address(CpuRegister(RSP), second.GetStackIndex()));
3631 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003632 break;
3633 }
3634
3635 default:
3636 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3637 }
3638}
3639
Calin Juravlebacfec32014-11-14 15:54:36 +00003640void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003641 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003642 LocationSummary* locations =
3643 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003644
3645 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003646 case Primitive::kPrimInt:
3647 case Primitive::kPrimLong: {
3648 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003649 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003650 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3651 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003652 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3653 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3654 // output and request another temp.
3655 if (rem->InputAt(1)->IsConstant()) {
3656 locations->AddTemp(Location::RequiresRegister());
3657 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003658 break;
3659 }
3660
3661 case Primitive::kPrimFloat:
3662 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003663 locations->SetInAt(0, Location::Any());
3664 locations->SetInAt(1, Location::Any());
3665 locations->SetOut(Location::RequiresFpuRegister());
3666 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003667 break;
3668 }
3669
3670 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003671 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003672 }
3673}
3674
3675void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3676 Primitive::Type type = rem->GetResultType();
3677 switch (type) {
3678 case Primitive::kPrimInt:
3679 case Primitive::kPrimLong: {
3680 GenerateDivRemIntegral(rem);
3681 break;
3682 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003683 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003684 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003685 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003686 break;
3687 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003688 default:
3689 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3690 }
3691}
3692
Calin Juravled0d48522014-11-04 16:40:20 +00003693void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003694 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3695 ? LocationSummary::kCallOnSlowPath
3696 : LocationSummary::kNoCall;
3697 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003698 locations->SetInAt(0, Location::Any());
3699 if (instruction->HasUses()) {
3700 locations->SetOut(Location::SameAsFirstInput());
3701 }
3702}
3703
3704void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003705 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003706 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3707 codegen_->AddSlowPath(slow_path);
3708
3709 LocationSummary* locations = instruction->GetLocations();
3710 Location value = locations->InAt(0);
3711
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003712 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003713 case Primitive::kPrimByte:
3714 case Primitive::kPrimChar:
3715 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003716 case Primitive::kPrimInt: {
3717 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003718 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003719 __ j(kEqual, slow_path->GetEntryLabel());
3720 } else if (value.IsStackSlot()) {
3721 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3722 __ j(kEqual, slow_path->GetEntryLabel());
3723 } else {
3724 DCHECK(value.IsConstant()) << value;
3725 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3726 __ jmp(slow_path->GetEntryLabel());
3727 }
3728 }
3729 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003730 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003731 case Primitive::kPrimLong: {
3732 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003733 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003734 __ j(kEqual, slow_path->GetEntryLabel());
3735 } else if (value.IsDoubleStackSlot()) {
3736 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3737 __ j(kEqual, slow_path->GetEntryLabel());
3738 } else {
3739 DCHECK(value.IsConstant()) << value;
3740 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3741 __ jmp(slow_path->GetEntryLabel());
3742 }
3743 }
3744 break;
3745 }
3746 default:
3747 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003748 }
Calin Juravled0d48522014-11-04 16:40:20 +00003749}
3750
Calin Juravle9aec02f2014-11-18 23:06:35 +00003751void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3752 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3753
3754 LocationSummary* locations =
3755 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3756
3757 switch (op->GetResultType()) {
3758 case Primitive::kPrimInt:
3759 case Primitive::kPrimLong: {
3760 locations->SetInAt(0, Location::RequiresRegister());
3761 // The shift count needs to be in CL.
3762 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3763 locations->SetOut(Location::SameAsFirstInput());
3764 break;
3765 }
3766 default:
3767 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3768 }
3769}
3770
3771void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3772 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3773
3774 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003775 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003776 Location second = locations->InAt(1);
3777
3778 switch (op->GetResultType()) {
3779 case Primitive::kPrimInt: {
3780 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003781 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003782 if (op->IsShl()) {
3783 __ shll(first_reg, second_reg);
3784 } else if (op->IsShr()) {
3785 __ sarl(first_reg, second_reg);
3786 } else {
3787 __ shrl(first_reg, second_reg);
3788 }
3789 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003790 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003791 if (op->IsShl()) {
3792 __ shll(first_reg, imm);
3793 } else if (op->IsShr()) {
3794 __ sarl(first_reg, imm);
3795 } else {
3796 __ shrl(first_reg, imm);
3797 }
3798 }
3799 break;
3800 }
3801 case Primitive::kPrimLong: {
3802 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003803 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003804 if (op->IsShl()) {
3805 __ shlq(first_reg, second_reg);
3806 } else if (op->IsShr()) {
3807 __ sarq(first_reg, second_reg);
3808 } else {
3809 __ shrq(first_reg, second_reg);
3810 }
3811 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003812 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003813 if (op->IsShl()) {
3814 __ shlq(first_reg, imm);
3815 } else if (op->IsShr()) {
3816 __ sarq(first_reg, imm);
3817 } else {
3818 __ shrq(first_reg, imm);
3819 }
3820 }
3821 break;
3822 }
3823 default:
3824 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003825 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003826 }
3827}
3828
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003829void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3830 LocationSummary* locations =
3831 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3832
3833 switch (ror->GetResultType()) {
3834 case Primitive::kPrimInt:
3835 case Primitive::kPrimLong: {
3836 locations->SetInAt(0, Location::RequiresRegister());
3837 // The shift count needs to be in CL (unless it is a constant).
3838 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3839 locations->SetOut(Location::SameAsFirstInput());
3840 break;
3841 }
3842 default:
3843 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3844 UNREACHABLE();
3845 }
3846}
3847
3848void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3849 LocationSummary* locations = ror->GetLocations();
3850 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3851 Location second = locations->InAt(1);
3852
3853 switch (ror->GetResultType()) {
3854 case Primitive::kPrimInt:
3855 if (second.IsRegister()) {
3856 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3857 __ rorl(first_reg, second_reg);
3858 } else {
3859 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3860 __ rorl(first_reg, imm);
3861 }
3862 break;
3863 case Primitive::kPrimLong:
3864 if (second.IsRegister()) {
3865 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3866 __ rorq(first_reg, second_reg);
3867 } else {
3868 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
3869 __ rorq(first_reg, imm);
3870 }
3871 break;
3872 default:
3873 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3874 UNREACHABLE();
3875 }
3876}
3877
Calin Juravle9aec02f2014-11-18 23:06:35 +00003878void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3879 HandleShift(shl);
3880}
3881
3882void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3883 HandleShift(shl);
3884}
3885
3886void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3887 HandleShift(shr);
3888}
3889
3890void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3891 HandleShift(shr);
3892}
3893
3894void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3895 HandleShift(ushr);
3896}
3897
3898void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3899 HandleShift(ushr);
3900}
3901
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003902void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003903 LocationSummary* locations =
3904 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003905 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003906 if (instruction->IsStringAlloc()) {
3907 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3908 } else {
3909 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3910 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3911 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003912 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003913}
3914
3915void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003916 // Note: if heap poisoning is enabled, the entry point takes cares
3917 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003918 if (instruction->IsStringAlloc()) {
3919 // String is allocated through StringFactory. Call NewEmptyString entry point.
3920 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3921 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3922 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3923 __ call(Address(temp, code_offset.SizeValue()));
3924 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3925 } else {
3926 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3927 instruction,
3928 instruction->GetDexPc(),
3929 nullptr);
3930 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3931 DCHECK(!codegen_->IsLeafMethod());
3932 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003933}
3934
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003935void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3936 LocationSummary* locations =
3937 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3938 InvokeRuntimeCallingConvention calling_convention;
3939 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003940 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003941 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003942 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003943}
3944
3945void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3946 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003947 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3948 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003949 // Note: if heap poisoning is enabled, the entry point takes cares
3950 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003951 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3952 instruction,
3953 instruction->GetDexPc(),
3954 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003955 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003956
3957 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003958}
3959
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003960void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003961 LocationSummary* locations =
3962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003963 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3964 if (location.IsStackSlot()) {
3965 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3966 } else if (location.IsDoubleStackSlot()) {
3967 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3968 }
3969 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003970}
3971
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003972void InstructionCodeGeneratorX86_64::VisitParameterValue(
3973 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003974 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003975}
3976
3977void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3978 LocationSummary* locations =
3979 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3980 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3981}
3982
3983void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3984 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3985 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003986}
3987
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003988void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3989 LocationSummary* locations =
3990 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3991 locations->SetInAt(0, Location::RequiresRegister());
3992 locations->SetOut(Location::RequiresRegister());
3993}
3994
3995void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3996 LocationSummary* locations = instruction->GetLocations();
3997 uint32_t method_offset = 0;
3998 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
3999 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4000 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
4001 } else {
4002 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4003 instruction->GetIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
4004 }
4005 __ movq(locations->Out().AsRegister<CpuRegister>(),
4006 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
4007}
4008
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004009void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004010 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004011 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004012 locations->SetInAt(0, Location::RequiresRegister());
4013 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004014}
4015
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004016void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4017 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004018 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4019 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004020 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004021 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004022 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004023 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004024 break;
4025
4026 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004027 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004028 break;
4029
4030 default:
4031 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4032 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004033}
4034
David Brazdil66d126e2015-04-03 16:02:44 +01004035void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4036 LocationSummary* locations =
4037 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4038 locations->SetInAt(0, Location::RequiresRegister());
4039 locations->SetOut(Location::SameAsFirstInput());
4040}
4041
4042void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004043 LocationSummary* locations = bool_not->GetLocations();
4044 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4045 locations->Out().AsRegister<CpuRegister>().AsRegister());
4046 Location out = locations->Out();
4047 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4048}
4049
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004050void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004051 LocationSummary* locations =
4052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004053 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4054 locations->SetInAt(i, Location::Any());
4055 }
4056 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004057}
4058
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004059void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004060 LOG(FATAL) << "Unimplemented";
4061}
4062
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004063void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004064 /*
4065 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004066 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004067 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4068 */
4069 switch (kind) {
4070 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004071 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004072 break;
4073 }
4074 case MemBarrierKind::kAnyStore:
4075 case MemBarrierKind::kLoadAny:
4076 case MemBarrierKind::kStoreStore: {
4077 // nop
4078 break;
4079 }
4080 default:
4081 LOG(FATAL) << "Unexpected memory barier " << kind;
4082 }
4083}
4084
4085void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4086 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4087
Roland Levillain0d5a2812015-11-13 10:07:31 +00004088 bool object_field_get_with_read_barrier =
4089 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004090 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004091 new (GetGraph()->GetArena()) LocationSummary(instruction,
4092 object_field_get_with_read_barrier ?
4093 LocationSummary::kCallOnSlowPath :
4094 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004095 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004096 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4097 locations->SetOut(Location::RequiresFpuRegister());
4098 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004099 // The output overlaps for an object field get when read barriers
4100 // are enabled: we do not want the move to overwrite the object's
4101 // location, as we need it to emit the read barrier.
4102 locations->SetOut(
4103 Location::RequiresRegister(),
4104 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004105 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004106 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4107 // We need a temporary register for the read barrier marking slow
4108 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4109 locations->AddTemp(Location::RequiresRegister());
4110 }
Calin Juravle52c48962014-12-16 17:02:57 +00004111}
4112
4113void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4114 const FieldInfo& field_info) {
4115 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4116
4117 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004118 Location base_loc = locations->InAt(0);
4119 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004120 Location out = locations->Out();
4121 bool is_volatile = field_info.IsVolatile();
4122 Primitive::Type field_type = field_info.GetFieldType();
4123 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4124
4125 switch (field_type) {
4126 case Primitive::kPrimBoolean: {
4127 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4128 break;
4129 }
4130
4131 case Primitive::kPrimByte: {
4132 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4133 break;
4134 }
4135
4136 case Primitive::kPrimShort: {
4137 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4138 break;
4139 }
4140
4141 case Primitive::kPrimChar: {
4142 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4143 break;
4144 }
4145
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004146 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004147 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4148 break;
4149 }
4150
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004151 case Primitive::kPrimNot: {
4152 // /* HeapReference<Object> */ out = *(base + offset)
4153 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4154 Location temp_loc = locations->GetTemp(0);
4155 // Note that a potential implicit null check is handled in this
4156 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4157 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4158 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4159 if (is_volatile) {
4160 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4161 }
4162 } else {
4163 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4164 codegen_->MaybeRecordImplicitNullCheck(instruction);
4165 if (is_volatile) {
4166 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4167 }
4168 // If read barriers are enabled, emit read barriers other than
4169 // Baker's using a slow path (and also unpoison the loaded
4170 // reference, if heap poisoning is enabled).
4171 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4172 }
4173 break;
4174 }
4175
Calin Juravle52c48962014-12-16 17:02:57 +00004176 case Primitive::kPrimLong: {
4177 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4178 break;
4179 }
4180
4181 case Primitive::kPrimFloat: {
4182 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4183 break;
4184 }
4185
4186 case Primitive::kPrimDouble: {
4187 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4188 break;
4189 }
4190
4191 case Primitive::kPrimVoid:
4192 LOG(FATAL) << "Unreachable type " << field_type;
4193 UNREACHABLE();
4194 }
4195
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004196 if (field_type == Primitive::kPrimNot) {
4197 // Potential implicit null checks, in the case of reference
4198 // fields, are handled in the previous switch statement.
4199 } else {
4200 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004201 }
Roland Levillain4d027112015-07-01 15:41:14 +01004202
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004203 if (is_volatile) {
4204 if (field_type == Primitive::kPrimNot) {
4205 // Memory barriers, in the case of references, are also handled
4206 // in the previous switch statement.
4207 } else {
4208 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4209 }
Roland Levillain4d027112015-07-01 15:41:14 +01004210 }
Calin Juravle52c48962014-12-16 17:02:57 +00004211}
4212
4213void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4214 const FieldInfo& field_info) {
4215 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4216
4217 LocationSummary* locations =
4218 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004219 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004220 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004221 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004222 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004223
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004224 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004225 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004226 if (is_volatile) {
4227 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4228 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4229 } else {
4230 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4231 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004232 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004233 if (is_volatile) {
4234 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4235 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4236 } else {
4237 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4238 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004239 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004240 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004241 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004242 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004243 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004244 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4245 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004246 locations->AddTemp(Location::RequiresRegister());
4247 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004248}
4249
Calin Juravle52c48962014-12-16 17:02:57 +00004250void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004251 const FieldInfo& field_info,
4252 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004253 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4254
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004255 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004256 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4257 Location value = locations->InAt(1);
4258 bool is_volatile = field_info.IsVolatile();
4259 Primitive::Type field_type = field_info.GetFieldType();
4260 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4261
4262 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004263 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004264 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004265
Mark Mendellea5af682015-10-22 17:35:49 -04004266 bool maybe_record_implicit_null_check_done = false;
4267
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004268 switch (field_type) {
4269 case Primitive::kPrimBoolean:
4270 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004271 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004272 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004273 __ movb(Address(base, offset), Immediate(v));
4274 } else {
4275 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4276 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277 break;
4278 }
4279
4280 case Primitive::kPrimShort:
4281 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004282 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004283 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004284 __ movw(Address(base, offset), Immediate(v));
4285 } else {
4286 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4287 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004288 break;
4289 }
4290
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004291 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004292 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004293 if (value.IsConstant()) {
4294 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004295 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4296 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4297 // Note: if heap poisoning is enabled, no need to poison
4298 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004299 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004300 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004301 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4302 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4303 __ movl(temp, value.AsRegister<CpuRegister>());
4304 __ PoisonHeapReference(temp);
4305 __ movl(Address(base, offset), temp);
4306 } else {
4307 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4308 }
Mark Mendell40741f32015-04-20 22:10:34 -04004309 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004310 break;
4311 }
4312
4313 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004314 if (value.IsConstant()) {
4315 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004316 codegen_->MoveInt64ToAddress(Address(base, offset),
4317 Address(base, offset + sizeof(int32_t)),
4318 v,
4319 instruction);
4320 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004321 } else {
4322 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4323 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004324 break;
4325 }
4326
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004327 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004328 if (value.IsConstant()) {
4329 int32_t v =
4330 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4331 __ movl(Address(base, offset), Immediate(v));
4332 } else {
4333 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4334 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004335 break;
4336 }
4337
4338 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004339 if (value.IsConstant()) {
4340 int64_t v =
4341 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4342 codegen_->MoveInt64ToAddress(Address(base, offset),
4343 Address(base, offset + sizeof(int32_t)),
4344 v,
4345 instruction);
4346 maybe_record_implicit_null_check_done = true;
4347 } else {
4348 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4349 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004350 break;
4351 }
4352
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004353 case Primitive::kPrimVoid:
4354 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004355 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004356 }
Calin Juravle52c48962014-12-16 17:02:57 +00004357
Mark Mendellea5af682015-10-22 17:35:49 -04004358 if (!maybe_record_implicit_null_check_done) {
4359 codegen_->MaybeRecordImplicitNullCheck(instruction);
4360 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004361
4362 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4363 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4364 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004365 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004366 }
4367
Calin Juravle52c48962014-12-16 17:02:57 +00004368 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004369 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004370 }
4371}
4372
4373void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4374 HandleFieldSet(instruction, instruction->GetFieldInfo());
4375}
4376
4377void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004378 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004379}
4380
4381void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004382 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004383}
4384
4385void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004386 HandleFieldGet(instruction, instruction->GetFieldInfo());
4387}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004388
Calin Juravle52c48962014-12-16 17:02:57 +00004389void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4390 HandleFieldGet(instruction);
4391}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004392
Calin Juravle52c48962014-12-16 17:02:57 +00004393void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4394 HandleFieldGet(instruction, instruction->GetFieldInfo());
4395}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004396
Calin Juravle52c48962014-12-16 17:02:57 +00004397void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4398 HandleFieldSet(instruction, instruction->GetFieldInfo());
4399}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400
Calin Juravle52c48962014-12-16 17:02:57 +00004401void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004402 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004403}
4404
Calin Juravlee460d1d2015-09-29 04:52:17 +01004405void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4406 HUnresolvedInstanceFieldGet* instruction) {
4407 FieldAccessCallingConventionX86_64 calling_convention;
4408 codegen_->CreateUnresolvedFieldLocationSummary(
4409 instruction, instruction->GetFieldType(), calling_convention);
4410}
4411
4412void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4413 HUnresolvedInstanceFieldGet* instruction) {
4414 FieldAccessCallingConventionX86_64 calling_convention;
4415 codegen_->GenerateUnresolvedFieldAccess(instruction,
4416 instruction->GetFieldType(),
4417 instruction->GetFieldIndex(),
4418 instruction->GetDexPc(),
4419 calling_convention);
4420}
4421
4422void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4423 HUnresolvedInstanceFieldSet* instruction) {
4424 FieldAccessCallingConventionX86_64 calling_convention;
4425 codegen_->CreateUnresolvedFieldLocationSummary(
4426 instruction, instruction->GetFieldType(), calling_convention);
4427}
4428
4429void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4430 HUnresolvedInstanceFieldSet* instruction) {
4431 FieldAccessCallingConventionX86_64 calling_convention;
4432 codegen_->GenerateUnresolvedFieldAccess(instruction,
4433 instruction->GetFieldType(),
4434 instruction->GetFieldIndex(),
4435 instruction->GetDexPc(),
4436 calling_convention);
4437}
4438
4439void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4440 HUnresolvedStaticFieldGet* instruction) {
4441 FieldAccessCallingConventionX86_64 calling_convention;
4442 codegen_->CreateUnresolvedFieldLocationSummary(
4443 instruction, instruction->GetFieldType(), calling_convention);
4444}
4445
4446void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4447 HUnresolvedStaticFieldGet* instruction) {
4448 FieldAccessCallingConventionX86_64 calling_convention;
4449 codegen_->GenerateUnresolvedFieldAccess(instruction,
4450 instruction->GetFieldType(),
4451 instruction->GetFieldIndex(),
4452 instruction->GetDexPc(),
4453 calling_convention);
4454}
4455
4456void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4457 HUnresolvedStaticFieldSet* instruction) {
4458 FieldAccessCallingConventionX86_64 calling_convention;
4459 codegen_->CreateUnresolvedFieldLocationSummary(
4460 instruction, instruction->GetFieldType(), calling_convention);
4461}
4462
4463void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4464 HUnresolvedStaticFieldSet* instruction) {
4465 FieldAccessCallingConventionX86_64 calling_convention;
4466 codegen_->GenerateUnresolvedFieldAccess(instruction,
4467 instruction->GetFieldType(),
4468 instruction->GetFieldIndex(),
4469 instruction->GetDexPc(),
4470 calling_convention);
4471}
4472
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004473void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004474 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4475 ? LocationSummary::kCallOnSlowPath
4476 : LocationSummary::kNoCall;
4477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4478 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004479 ? Location::RequiresRegister()
4480 : Location::Any();
4481 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004482 if (instruction->HasUses()) {
4483 locations->SetOut(Location::SameAsFirstInput());
4484 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004485}
4486
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004487void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004488 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4489 return;
4490 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004491 LocationSummary* locations = instruction->GetLocations();
4492 Location obj = locations->InAt(0);
4493
4494 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4495 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4496}
4497
4498void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004499 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004500 codegen_->AddSlowPath(slow_path);
4501
4502 LocationSummary* locations = instruction->GetLocations();
4503 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504
4505 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004506 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004507 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004508 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004509 } else {
4510 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004511 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004512 __ jmp(slow_path->GetEntryLabel());
4513 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004514 }
4515 __ j(kEqual, slow_path->GetEntryLabel());
4516}
4517
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004518void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004519 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004520 GenerateImplicitNullCheck(instruction);
4521 } else {
4522 GenerateExplicitNullCheck(instruction);
4523 }
4524}
4525
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004526void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004527 bool object_array_get_with_read_barrier =
4528 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004529 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004530 new (GetGraph()->GetArena()) LocationSummary(instruction,
4531 object_array_get_with_read_barrier ?
4532 LocationSummary::kCallOnSlowPath :
4533 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004534 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004535 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004536 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4537 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4538 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004539 // The output overlaps for an object array get when read barriers
4540 // are enabled: we do not want the move to overwrite the array's
4541 // location, as we need it to emit the read barrier.
4542 locations->SetOut(
4543 Location::RequiresRegister(),
4544 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004545 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004546 // We need a temporary register for the read barrier marking slow
4547 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4548 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4549 locations->AddTemp(Location::RequiresRegister());
4550 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004551}
4552
4553void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4554 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004555 Location obj_loc = locations->InAt(0);
4556 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004557 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004558 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004559
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004560 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004561 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004562 case Primitive::kPrimBoolean: {
4563 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004564 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004565 if (index.IsConstant()) {
4566 __ movzxb(out, Address(obj,
4567 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4568 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004569 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004570 }
4571 break;
4572 }
4573
4574 case Primitive::kPrimByte: {
4575 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004576 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577 if (index.IsConstant()) {
4578 __ movsxb(out, Address(obj,
4579 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4580 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004581 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004582 }
4583 break;
4584 }
4585
4586 case Primitive::kPrimShort: {
4587 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004588 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 if (index.IsConstant()) {
4590 __ movsxw(out, Address(obj,
4591 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4592 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004593 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594 }
4595 break;
4596 }
4597
4598 case Primitive::kPrimChar: {
4599 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004600 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601 if (index.IsConstant()) {
4602 __ movzxw(out, Address(obj,
4603 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4604 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004605 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004606 }
4607 break;
4608 }
4609
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004610 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004612 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004613 if (index.IsConstant()) {
4614 __ movl(out, Address(obj,
4615 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4616 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004617 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004618 }
4619 break;
4620 }
4621
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004622 case Primitive::kPrimNot: {
4623 static_assert(
4624 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4625 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4626 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4627 // /* HeapReference<Object> */ out =
4628 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4629 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4630 Location temp = locations->GetTemp(0);
4631 // Note that a potential implicit null check is handled in this
4632 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4633 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4634 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4635 } else {
4636 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4637 if (index.IsConstant()) {
4638 uint32_t offset =
4639 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4640 __ movl(out, Address(obj, offset));
4641 codegen_->MaybeRecordImplicitNullCheck(instruction);
4642 // If read barriers are enabled, emit read barriers other than
4643 // Baker's using a slow path (and also unpoison the loaded
4644 // reference, if heap poisoning is enabled).
4645 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4646 } else {
4647 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4648 codegen_->MaybeRecordImplicitNullCheck(instruction);
4649 // If read barriers are enabled, emit read barriers other than
4650 // Baker's using a slow path (and also unpoison the loaded
4651 // reference, if heap poisoning is enabled).
4652 codegen_->MaybeGenerateReadBarrierSlow(
4653 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4654 }
4655 }
4656 break;
4657 }
4658
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004659 case Primitive::kPrimLong: {
4660 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004661 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004662 if (index.IsConstant()) {
4663 __ movq(out, Address(obj,
4664 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4665 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004666 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667 }
4668 break;
4669 }
4670
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004671 case Primitive::kPrimFloat: {
4672 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004673 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004674 if (index.IsConstant()) {
4675 __ movss(out, Address(obj,
4676 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4677 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004678 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004679 }
4680 break;
4681 }
4682
4683 case Primitive::kPrimDouble: {
4684 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004685 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004686 if (index.IsConstant()) {
4687 __ movsd(out, Address(obj,
4688 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4689 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004690 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004691 }
4692 break;
4693 }
4694
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004695 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004696 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004697 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004698 }
Roland Levillain4d027112015-07-01 15:41:14 +01004699
4700 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004701 // Potential implicit null checks, in the case of reference
4702 // arrays, are handled in the previous switch statement.
4703 } else {
4704 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004705 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706}
4707
4708void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004709 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004710
4711 bool needs_write_barrier =
4712 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004713 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004714 bool object_array_set_with_read_barrier =
4715 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004716
Nicolas Geoffray39468442014-09-02 15:17:15 +01004717 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004718 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004719 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004720 LocationSummary::kCallOnSlowPath :
4721 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004722
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004723 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004724 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4725 if (Primitive::IsFloatingPointType(value_type)) {
4726 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004727 } else {
4728 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4729 }
4730
4731 if (needs_write_barrier) {
4732 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004733
4734 // This first temporary register is possibly used for heap
4735 // reference poisoning and/or read barrier emission too.
4736 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004737 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739}
4740
4741void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4742 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004743 Location array_loc = locations->InAt(0);
4744 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004745 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004746 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004747 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004748 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004749 bool needs_write_barrier =
4750 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004751 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4752 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4753 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004754
4755 switch (value_type) {
4756 case Primitive::kPrimBoolean:
4757 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004758 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4759 Address address = index.IsConstant()
4760 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4761 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4762 if (value.IsRegister()) {
4763 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004764 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004765 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004766 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768 break;
4769 }
4770
4771 case Primitive::kPrimShort:
4772 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4774 Address address = index.IsConstant()
4775 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4776 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4777 if (value.IsRegister()) {
4778 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004779 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004780 DCHECK(value.IsConstant()) << value;
4781 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004782 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004783 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004784 break;
4785 }
4786
4787 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004788 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4789 Address address = index.IsConstant()
4790 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4791 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004792
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004793 if (!value.IsRegister()) {
4794 // Just setting null.
4795 DCHECK(instruction->InputAt(2)->IsNullConstant());
4796 DCHECK(value.IsConstant()) << value;
4797 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004798 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004800 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004801 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004802 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004803
4804 DCHECK(needs_write_barrier);
4805 CpuRegister register_value = value.AsRegister<CpuRegister>();
4806 NearLabel done, not_null, do_put;
4807 SlowPathCode* slow_path = nullptr;
4808 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004809 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004810 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4811 codegen_->AddSlowPath(slow_path);
4812 if (instruction->GetValueCanBeNull()) {
4813 __ testl(register_value, register_value);
4814 __ j(kNotEqual, &not_null);
4815 __ movl(address, Immediate(0));
4816 codegen_->MaybeRecordImplicitNullCheck(instruction);
4817 __ jmp(&done);
4818 __ Bind(&not_null);
4819 }
4820
Roland Levillain0d5a2812015-11-13 10:07:31 +00004821 if (kEmitCompilerReadBarrier) {
4822 // When read barriers are enabled, the type checking
4823 // instrumentation requires two read barriers:
4824 //
4825 // __ movl(temp2, temp);
4826 // // /* HeapReference<Class> */ temp = temp->component_type_
4827 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004828 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004829 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4830 //
4831 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4832 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004833 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004834 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4835 //
4836 // __ cmpl(temp, temp2);
4837 //
4838 // However, the second read barrier may trash `temp`, as it
4839 // is a temporary register, and as such would not be saved
4840 // along with live registers before calling the runtime (nor
4841 // restored afterwards). So in this case, we bail out and
4842 // delegate the work to the array set slow path.
4843 //
4844 // TODO: Extend the register allocator to support a new
4845 // "(locally) live temp" location so as to avoid always
4846 // going into the slow path when read barriers are enabled.
4847 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004848 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004849 // /* HeapReference<Class> */ temp = array->klass_
4850 __ movl(temp, Address(array, class_offset));
4851 codegen_->MaybeRecordImplicitNullCheck(instruction);
4852 __ MaybeUnpoisonHeapReference(temp);
4853
4854 // /* HeapReference<Class> */ temp = temp->component_type_
4855 __ movl(temp, Address(temp, component_offset));
4856 // If heap poisoning is enabled, no need to unpoison `temp`
4857 // nor the object reference in `register_value->klass`, as
4858 // we are comparing two poisoned references.
4859 __ cmpl(temp, Address(register_value, class_offset));
4860
4861 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4862 __ j(kEqual, &do_put);
4863 // If heap poisoning is enabled, the `temp` reference has
4864 // not been unpoisoned yet; unpoison it now.
4865 __ MaybeUnpoisonHeapReference(temp);
4866
4867 // /* HeapReference<Class> */ temp = temp->super_class_
4868 __ movl(temp, Address(temp, super_offset));
4869 // If heap poisoning is enabled, no need to unpoison
4870 // `temp`, as we are comparing against null below.
4871 __ testl(temp, temp);
4872 __ j(kNotEqual, slow_path->GetEntryLabel());
4873 __ Bind(&do_put);
4874 } else {
4875 __ j(kNotEqual, slow_path->GetEntryLabel());
4876 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004877 }
4878 }
4879
4880 if (kPoisonHeapReferences) {
4881 __ movl(temp, register_value);
4882 __ PoisonHeapReference(temp);
4883 __ movl(address, temp);
4884 } else {
4885 __ movl(address, register_value);
4886 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004887 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004888 codegen_->MaybeRecordImplicitNullCheck(instruction);
4889 }
4890
4891 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4892 codegen_->MarkGCCard(
4893 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4894 __ Bind(&done);
4895
4896 if (slow_path != nullptr) {
4897 __ Bind(slow_path->GetExitLabel());
4898 }
4899
4900 break;
4901 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004902
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004903 case Primitive::kPrimInt: {
4904 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4905 Address address = index.IsConstant()
4906 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4907 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4908 if (value.IsRegister()) {
4909 __ movl(address, value.AsRegister<CpuRegister>());
4910 } else {
4911 DCHECK(value.IsConstant()) << value;
4912 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4913 __ movl(address, Immediate(v));
4914 }
4915 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004916 break;
4917 }
4918
4919 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004920 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4921 Address address = index.IsConstant()
4922 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4923 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4924 if (value.IsRegister()) {
4925 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004926 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004927 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004928 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004929 Address address_high = index.IsConstant()
4930 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4931 offset + sizeof(int32_t))
4932 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4933 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934 }
4935 break;
4936 }
4937
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004938 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004939 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4940 Address address = index.IsConstant()
4941 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4942 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004943 if (value.IsFpuRegister()) {
4944 __ movss(address, value.AsFpuRegister<XmmRegister>());
4945 } else {
4946 DCHECK(value.IsConstant());
4947 int32_t v =
4948 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4949 __ movl(address, Immediate(v));
4950 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004951 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004952 break;
4953 }
4954
4955 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004956 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4957 Address address = index.IsConstant()
4958 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4959 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004960 if (value.IsFpuRegister()) {
4961 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4962 codegen_->MaybeRecordImplicitNullCheck(instruction);
4963 } else {
4964 int64_t v =
4965 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4966 Address address_high = index.IsConstant()
4967 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4968 offset + sizeof(int32_t))
4969 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4970 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4971 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004972 break;
4973 }
4974
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004975 case Primitive::kPrimVoid:
4976 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004977 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004978 }
4979}
4980
4981void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004982 LocationSummary* locations =
4983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004984 locations->SetInAt(0, Location::RequiresRegister());
4985 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004986}
4987
4988void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4989 LocationSummary* locations = instruction->GetLocations();
4990 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004991 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4992 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004993 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004994 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995}
4996
4997void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004998 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4999 ? LocationSummary::kCallOnSlowPath
5000 : LocationSummary::kNoCall;
5001 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005002 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005003 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005004 if (instruction->HasUses()) {
5005 locations->SetOut(Location::SameAsFirstInput());
5006 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005007}
5008
5009void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5010 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005011 Location index_loc = locations->InAt(0);
5012 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005013 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005014 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005015
Mark Mendell99dbd682015-04-22 16:18:52 -04005016 if (length_loc.IsConstant()) {
5017 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5018 if (index_loc.IsConstant()) {
5019 // BCE will remove the bounds check if we are guarenteed to pass.
5020 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5021 if (index < 0 || index >= length) {
5022 codegen_->AddSlowPath(slow_path);
5023 __ jmp(slow_path->GetEntryLabel());
5024 } else {
5025 // Some optimization after BCE may have generated this, and we should not
5026 // generate a bounds check if it is a valid range.
5027 }
5028 return;
5029 }
5030
5031 // We have to reverse the jump condition because the length is the constant.
5032 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5033 __ cmpl(index_reg, Immediate(length));
5034 codegen_->AddSlowPath(slow_path);
5035 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005036 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005037 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5038 if (index_loc.IsConstant()) {
5039 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5040 __ cmpl(length, Immediate(value));
5041 } else {
5042 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5043 }
5044 codegen_->AddSlowPath(slow_path);
5045 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005046 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005047}
5048
5049void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5050 CpuRegister card,
5051 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005052 CpuRegister value,
5053 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005054 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005055 if (value_can_be_null) {
5056 __ testl(value, value);
5057 __ j(kEqual, &is_null);
5058 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005059 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5060 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061 __ movq(temp, object);
5062 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005063 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005064 if (value_can_be_null) {
5065 __ Bind(&is_null);
5066 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005067}
5068
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005069void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
5070 temp->SetLocations(nullptr);
5071}
5072
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005073void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005074 // Nothing to do, this is driven by the code generator.
5075}
5076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005077void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005078 LOG(FATAL) << "Unimplemented";
5079}
5080
5081void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005082 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5083}
5084
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005085void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5086 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5087}
5088
5089void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005090 HBasicBlock* block = instruction->GetBlock();
5091 if (block->GetLoopInformation() != nullptr) {
5092 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5093 // The back edge will generate the suspend check.
5094 return;
5095 }
5096 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5097 // The goto will generate the suspend check.
5098 return;
5099 }
5100 GenerateSuspendCheck(instruction, nullptr);
5101}
5102
5103void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5104 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005105 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005106 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5107 if (slow_path == nullptr) {
5108 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5109 instruction->SetSlowPath(slow_path);
5110 codegen_->AddSlowPath(slow_path);
5111 if (successor != nullptr) {
5112 DCHECK(successor->IsLoopHeader());
5113 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5114 }
5115 } else {
5116 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5117 }
5118
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005119 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5120 /* no_rip */ true),
5121 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005122 if (successor == nullptr) {
5123 __ j(kNotEqual, slow_path->GetEntryLabel());
5124 __ Bind(slow_path->GetReturnLabel());
5125 } else {
5126 __ j(kEqual, codegen_->GetLabelOf(successor));
5127 __ jmp(slow_path->GetEntryLabel());
5128 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005129}
5130
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005131X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5132 return codegen_->GetAssembler();
5133}
5134
5135void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005136 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005137 Location source = move->GetSource();
5138 Location destination = move->GetDestination();
5139
5140 if (source.IsRegister()) {
5141 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005142 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005143 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005144 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005145 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005146 } else {
5147 DCHECK(destination.IsDoubleStackSlot());
5148 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005149 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005150 }
5151 } else if (source.IsStackSlot()) {
5152 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005153 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005154 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005155 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005156 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005157 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005158 } else {
5159 DCHECK(destination.IsStackSlot());
5160 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5161 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5162 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005163 } else if (source.IsDoubleStackSlot()) {
5164 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005165 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005166 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005167 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005168 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5169 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005170 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005171 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005172 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5173 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5174 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005175 } else if (source.IsConstant()) {
5176 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005177 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5178 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005179 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005180 if (value == 0) {
5181 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5182 } else {
5183 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5184 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005185 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005186 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005187 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005188 }
5189 } else if (constant->IsLongConstant()) {
5190 int64_t value = constant->AsLongConstant()->GetValue();
5191 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005192 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005193 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005194 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005195 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005196 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005197 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005198 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005199 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005200 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005201 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5202 if (value == 0) {
5203 // easy FP 0.0.
5204 __ xorps(dest, dest);
5205 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005206 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005207 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005208 } else {
5209 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04005210 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005211 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5212 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005213 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005214 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005215 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005216 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005217 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005218 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5219 if (value == 0) {
5220 __ xorpd(dest, dest);
5221 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005222 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005223 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005224 } else {
5225 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005226 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005227 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005228 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005229 } else if (source.IsFpuRegister()) {
5230 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005231 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005232 } else if (destination.IsStackSlot()) {
5233 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005234 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005235 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005236 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005237 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005238 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005239 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005240 }
5241}
5242
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005243void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005244 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005245 __ movl(Address(CpuRegister(RSP), mem), reg);
5246 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005247}
5248
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005249void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005250 ScratchRegisterScope ensure_scratch(
5251 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5252
5253 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5254 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5255 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5256 Address(CpuRegister(RSP), mem2 + stack_offset));
5257 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5258 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5259 CpuRegister(ensure_scratch.GetRegister()));
5260}
5261
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005262void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5263 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5264 __ movq(Address(CpuRegister(RSP), mem), reg);
5265 __ movq(reg, CpuRegister(TMP));
5266}
5267
5268void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5269 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005270 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005271
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005272 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5273 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5274 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5275 Address(CpuRegister(RSP), mem2 + stack_offset));
5276 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5277 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5278 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005279}
5280
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005281void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5282 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5283 __ movss(Address(CpuRegister(RSP), mem), reg);
5284 __ movd(reg, CpuRegister(TMP));
5285}
5286
5287void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5288 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5289 __ movsd(Address(CpuRegister(RSP), mem), reg);
5290 __ movd(reg, CpuRegister(TMP));
5291}
5292
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005293void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005294 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005295 Location source = move->GetSource();
5296 Location destination = move->GetDestination();
5297
5298 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005299 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005300 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005301 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005302 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005303 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005304 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005305 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5306 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005307 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005308 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005309 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005310 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5311 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005312 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005313 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5314 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5315 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005316 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005317 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005318 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005319 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005320 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005321 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005322 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005323 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005324 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005325 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005326 }
5327}
5328
5329
5330void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5331 __ pushq(CpuRegister(reg));
5332}
5333
5334
5335void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5336 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005337}
5338
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005339void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005340 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005341 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5342 Immediate(mirror::Class::kStatusInitialized));
5343 __ j(kLess, slow_path->GetEntryLabel());
5344 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005345 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005346}
5347
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005348void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005349 InvokeRuntimeCallingConvention calling_convention;
5350 CodeGenerator::CreateLoadClassLocationSummary(
5351 cls,
5352 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005353 Location::RegisterLocation(RAX),
5354 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005355}
5356
5357void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005358 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005359 if (cls->NeedsAccessCheck()) {
5360 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5361 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5362 cls,
5363 cls->GetDexPc(),
5364 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005365 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005366 return;
5367 }
5368
Roland Levillain0d5a2812015-11-13 10:07:31 +00005369 Location out_loc = locations->Out();
5370 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005371 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005372
Calin Juravle580b6092015-10-06 17:35:58 +01005373 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005374 DCHECK(!cls->CanCallRuntime());
5375 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005376 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5377 GenerateGcRootFieldLoad(
5378 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005379 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005380 // /* GcRoot<mirror::Class>[] */ out =
5381 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5382 __ movq(out, Address(current_method,
5383 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005384 // /* GcRoot<mirror::Class> */ out = out[type_index]
5385 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01005386
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005387 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5388 DCHECK(cls->CanCallRuntime());
5389 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5390 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5391 codegen_->AddSlowPath(slow_path);
5392 if (!cls->IsInDexCache()) {
5393 __ testl(out, out);
5394 __ j(kEqual, slow_path->GetEntryLabel());
5395 }
5396 if (cls->MustGenerateClinitCheck()) {
5397 GenerateClassInitializationCheck(slow_path, out);
5398 } else {
5399 __ Bind(slow_path->GetExitLabel());
5400 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005401 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005402 }
5403}
5404
5405void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5406 LocationSummary* locations =
5407 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5408 locations->SetInAt(0, Location::RequiresRegister());
5409 if (check->HasUses()) {
5410 locations->SetOut(Location::SameAsFirstInput());
5411 }
5412}
5413
5414void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005415 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005416 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005417 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005418 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005419 GenerateClassInitializationCheck(slow_path,
5420 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421}
5422
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005423void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005424 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5425 ? LocationSummary::kCallOnSlowPath
5426 : LocationSummary::kNoCall;
5427 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005428 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005429 locations->SetOut(Location::RequiresRegister());
5430}
5431
5432void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005433 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005434 Location out_loc = locations->Out();
5435 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005436 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005437
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005438 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5439 GenerateGcRootFieldLoad(
5440 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005441 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5442 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005443 // /* GcRoot<mirror::String> */ out = out[string_index]
5444 GenerateGcRootFieldLoad(
5445 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005446
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005447 if (!load->IsInDexCache()) {
5448 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5449 codegen_->AddSlowPath(slow_path);
5450 __ testl(out, out);
5451 __ j(kEqual, slow_path->GetEntryLabel());
5452 __ Bind(slow_path->GetExitLabel());
5453 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005454}
5455
David Brazdilcb1c0552015-08-04 16:22:25 +01005456static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005457 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5458 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005459}
5460
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005461void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5462 LocationSummary* locations =
5463 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5464 locations->SetOut(Location::RequiresRegister());
5465}
5466
5467void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005468 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5469}
5470
5471void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5472 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5473}
5474
5475void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5476 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005477}
5478
5479void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5480 LocationSummary* locations =
5481 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5482 InvokeRuntimeCallingConvention calling_convention;
5483 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5484}
5485
5486void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005487 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5488 instruction,
5489 instruction->GetDexPc(),
5490 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005491 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005492}
5493
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005494static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5495 return kEmitCompilerReadBarrier &&
5496 (kUseBakerReadBarrier ||
5497 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5498 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5499 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5500}
5501
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005502void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005503 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005504 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5505 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005506 case TypeCheckKind::kExactCheck:
5507 case TypeCheckKind::kAbstractClassCheck:
5508 case TypeCheckKind::kClassHierarchyCheck:
5509 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005510 call_kind =
5511 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005512 break;
5513 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005514 case TypeCheckKind::kUnresolvedCheck:
5515 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005516 call_kind = LocationSummary::kCallOnSlowPath;
5517 break;
5518 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005519
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005521 locations->SetInAt(0, Location::RequiresRegister());
5522 locations->SetInAt(1, Location::Any());
5523 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5524 locations->SetOut(Location::RequiresRegister());
5525 // When read barriers are enabled, we need a temporary register for
5526 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005527 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005528 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005529 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005530}
5531
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005532void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005533 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005534 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005535 Location obj_loc = locations->InAt(0);
5536 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005537 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005538 Location out_loc = locations->Out();
5539 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005540 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005541 locations->GetTemp(0) :
5542 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005543 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005544 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5545 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5546 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005547 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005548 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005549
5550 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005551 // Avoid null check if we know obj is not null.
5552 if (instruction->MustDoNullCheck()) {
5553 __ testl(obj, obj);
5554 __ j(kEqual, &zero);
5555 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005556
Roland Levillain0d5a2812015-11-13 10:07:31 +00005557 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005558 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005559
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005560 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005561 case TypeCheckKind::kExactCheck: {
5562 if (cls.IsRegister()) {
5563 __ cmpl(out, cls.AsRegister<CpuRegister>());
5564 } else {
5565 DCHECK(cls.IsStackSlot()) << cls;
5566 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5567 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005568 if (zero.IsLinked()) {
5569 // Classes must be equal for the instanceof to succeed.
5570 __ j(kNotEqual, &zero);
5571 __ movl(out, Immediate(1));
5572 __ jmp(&done);
5573 } else {
5574 __ setcc(kEqual, out);
5575 // setcc only sets the low byte.
5576 __ andl(out, Immediate(1));
5577 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005578 break;
5579 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005580
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005581 case TypeCheckKind::kAbstractClassCheck: {
5582 // If the class is abstract, we eagerly fetch the super class of the
5583 // object to avoid doing a comparison we know will fail.
5584 NearLabel loop, success;
5585 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005586 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005587 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005588 __ testl(out, out);
5589 // If `out` is null, we use it for the result, and jump to `done`.
5590 __ j(kEqual, &done);
5591 if (cls.IsRegister()) {
5592 __ cmpl(out, cls.AsRegister<CpuRegister>());
5593 } else {
5594 DCHECK(cls.IsStackSlot()) << cls;
5595 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5596 }
5597 __ j(kNotEqual, &loop);
5598 __ movl(out, Immediate(1));
5599 if (zero.IsLinked()) {
5600 __ jmp(&done);
5601 }
5602 break;
5603 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005604
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005605 case TypeCheckKind::kClassHierarchyCheck: {
5606 // Walk over the class hierarchy to find a match.
5607 NearLabel loop, success;
5608 __ Bind(&loop);
5609 if (cls.IsRegister()) {
5610 __ cmpl(out, cls.AsRegister<CpuRegister>());
5611 } else {
5612 DCHECK(cls.IsStackSlot()) << cls;
5613 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5614 }
5615 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005616 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005617 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005618 __ testl(out, out);
5619 __ j(kNotEqual, &loop);
5620 // If `out` is null, we use it for the result, and jump to `done`.
5621 __ jmp(&done);
5622 __ Bind(&success);
5623 __ movl(out, Immediate(1));
5624 if (zero.IsLinked()) {
5625 __ jmp(&done);
5626 }
5627 break;
5628 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005629
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005630 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005631 // Do an exact check.
5632 NearLabel exact_check;
5633 if (cls.IsRegister()) {
5634 __ cmpl(out, cls.AsRegister<CpuRegister>());
5635 } else {
5636 DCHECK(cls.IsStackSlot()) << cls;
5637 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5638 }
5639 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005640 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005641 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005642 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005643 __ testl(out, out);
5644 // If `out` is null, we use it for the result, and jump to `done`.
5645 __ j(kEqual, &done);
5646 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5647 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005648 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005649 __ movl(out, Immediate(1));
5650 __ jmp(&done);
5651 break;
5652 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005653
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005654 case TypeCheckKind::kArrayCheck: {
5655 if (cls.IsRegister()) {
5656 __ cmpl(out, cls.AsRegister<CpuRegister>());
5657 } else {
5658 DCHECK(cls.IsStackSlot()) << cls;
5659 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5660 }
5661 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005662 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5663 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005664 codegen_->AddSlowPath(slow_path);
5665 __ j(kNotEqual, slow_path->GetEntryLabel());
5666 __ movl(out, Immediate(1));
5667 if (zero.IsLinked()) {
5668 __ jmp(&done);
5669 }
5670 break;
5671 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005672
Calin Juravle98893e12015-10-02 21:05:03 +01005673 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005674 case TypeCheckKind::kInterfaceCheck: {
5675 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005676 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005677 // cases.
5678 //
5679 // We cannot directly call the InstanceofNonTrivial runtime
5680 // entry point without resorting to a type checking slow path
5681 // here (i.e. by calling InvokeRuntime directly), as it would
5682 // require to assign fixed registers for the inputs of this
5683 // HInstanceOf instruction (following the runtime calling
5684 // convention), which might be cluttered by the potential first
5685 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005686 //
5687 // TODO: Introduce a new runtime entry point taking the object
5688 // to test (instead of its class) as argument, and let it deal
5689 // with the read barrier issues. This will let us refactor this
5690 // case of the `switch` code as it was previously (with a direct
5691 // call to the runtime not using a type checking slow path).
5692 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005693 DCHECK(locations->OnlyCallsOnSlowPath());
5694 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5695 /* is_fatal */ false);
5696 codegen_->AddSlowPath(slow_path);
5697 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005698 if (zero.IsLinked()) {
5699 __ jmp(&done);
5700 }
5701 break;
5702 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005703 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005704
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005705 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005706 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005707 __ xorl(out, out);
5708 }
5709
5710 if (done.IsLinked()) {
5711 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005712 }
5713
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005714 if (slow_path != nullptr) {
5715 __ Bind(slow_path->GetExitLabel());
5716 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005717}
5718
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005719void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005720 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5721 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005722 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5723 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005724 case TypeCheckKind::kExactCheck:
5725 case TypeCheckKind::kAbstractClassCheck:
5726 case TypeCheckKind::kClassHierarchyCheck:
5727 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005728 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5729 LocationSummary::kCallOnSlowPath :
5730 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005731 break;
5732 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005733 case TypeCheckKind::kUnresolvedCheck:
5734 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005735 call_kind = LocationSummary::kCallOnSlowPath;
5736 break;
5737 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005738 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5739 locations->SetInAt(0, Location::RequiresRegister());
5740 locations->SetInAt(1, Location::Any());
5741 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5742 locations->AddTemp(Location::RequiresRegister());
5743 // When read barriers are enabled, we need an additional temporary
5744 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005745 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005746 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005747 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005748}
5749
5750void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005751 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005752 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005753 Location obj_loc = locations->InAt(0);
5754 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005755 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005756 Location temp_loc = locations->GetTemp(0);
5757 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005758 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005759 locations->GetTemp(1) :
5760 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005761 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5762 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5763 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5764 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005765
Roland Levillain0d5a2812015-11-13 10:07:31 +00005766 bool is_type_check_slow_path_fatal =
5767 (type_check_kind == TypeCheckKind::kExactCheck ||
5768 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5769 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5770 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5771 !instruction->CanThrowIntoCatchBlock();
5772 SlowPathCode* type_check_slow_path =
5773 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5774 is_type_check_slow_path_fatal);
5775 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005776
Mark Mendell152408f2015-12-31 12:28:50 -05005777 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005778 // Avoid null check if we know obj is not null.
5779 if (instruction->MustDoNullCheck()) {
5780 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005781 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005782 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005783
Roland Levillain0d5a2812015-11-13 10:07:31 +00005784 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005785 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005786
Roland Levillain0d5a2812015-11-13 10:07:31 +00005787 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005788 case TypeCheckKind::kExactCheck:
5789 case TypeCheckKind::kArrayCheck: {
5790 if (cls.IsRegister()) {
5791 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5792 } else {
5793 DCHECK(cls.IsStackSlot()) << cls;
5794 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5795 }
5796 // Jump to slow path for throwing the exception or doing a
5797 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005798 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005799 break;
5800 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005801
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005802 case TypeCheckKind::kAbstractClassCheck: {
5803 // If the class is abstract, we eagerly fetch the super class of the
5804 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005805 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005806 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005807 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005808 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005809
5810 // If the class reference currently in `temp` is not null, jump
5811 // to the `compare_classes` label to compare it with the checked
5812 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005813 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005814 __ j(kNotEqual, &compare_classes);
5815 // Otherwise, jump to the slow path to throw the exception.
5816 //
5817 // But before, move back the object's class into `temp` before
5818 // going into the slow path, as it has been overwritten in the
5819 // meantime.
5820 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005821 GenerateReferenceLoadTwoRegisters(
5822 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005823 __ jmp(type_check_slow_path->GetEntryLabel());
5824
5825 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005826 if (cls.IsRegister()) {
5827 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5828 } else {
5829 DCHECK(cls.IsStackSlot()) << cls;
5830 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5831 }
5832 __ j(kNotEqual, &loop);
5833 break;
5834 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005835
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005836 case TypeCheckKind::kClassHierarchyCheck: {
5837 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005838 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005839 __ Bind(&loop);
5840 if (cls.IsRegister()) {
5841 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5842 } else {
5843 DCHECK(cls.IsStackSlot()) << cls;
5844 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5845 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005846 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005847
Roland Levillain0d5a2812015-11-13 10:07:31 +00005848 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005849 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005850
5851 // If the class reference currently in `temp` is not null, jump
5852 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005853 __ testl(temp, temp);
5854 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005855 // Otherwise, jump to the slow path to throw the exception.
5856 //
5857 // But before, move back the object's class into `temp` before
5858 // going into the slow path, as it has been overwritten in the
5859 // meantime.
5860 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005861 GenerateReferenceLoadTwoRegisters(
5862 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005863 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005864 break;
5865 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005866
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005867 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005868 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005869 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005870 if (cls.IsRegister()) {
5871 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5872 } else {
5873 DCHECK(cls.IsStackSlot()) << cls;
5874 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5875 }
5876 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005877
5878 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005879 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005880 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005881
5882 // If the component type is not null (i.e. the object is indeed
5883 // an array), jump to label `check_non_primitive_component_type`
5884 // to further check that this component type is not a primitive
5885 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005886 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005887 __ j(kNotEqual, &check_non_primitive_component_type);
5888 // Otherwise, jump to the slow path to throw the exception.
5889 //
5890 // But before, move back the object's class into `temp` before
5891 // going into the slow path, as it has been overwritten in the
5892 // meantime.
5893 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005894 GenerateReferenceLoadTwoRegisters(
5895 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005896 __ jmp(type_check_slow_path->GetEntryLabel());
5897
5898 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005899 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005900 __ j(kEqual, &done);
5901 // Same comment as above regarding `temp` and the slow path.
5902 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005903 GenerateReferenceLoadTwoRegisters(
5904 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005905 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005906 break;
5907 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005908
Calin Juravle98893e12015-10-02 21:05:03 +01005909 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005911 // We always go into the type check slow path for the unresolved
5912 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005913 //
5914 // We cannot directly call the CheckCast runtime entry point
5915 // without resorting to a type checking slow path here (i.e. by
5916 // calling InvokeRuntime directly), as it would require to
5917 // assign fixed registers for the inputs of this HInstanceOf
5918 // instruction (following the runtime calling convention), which
5919 // might be cluttered by the potential first read barrier
5920 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005921 //
5922 // TODO: Introduce a new runtime entry point taking the object
5923 // to test (instead of its class) as argument, and let it deal
5924 // with the read barrier issues. This will let us refactor this
5925 // case of the `switch` code as it was previously (with a direct
5926 // call to the runtime not using a type checking slow path).
5927 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005928 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929 break;
5930 }
5931 __ Bind(&done);
5932
Roland Levillain0d5a2812015-11-13 10:07:31 +00005933 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005934}
5935
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005936void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5937 LocationSummary* locations =
5938 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5939 InvokeRuntimeCallingConvention calling_convention;
5940 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5941}
5942
5943void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005944 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5945 : QUICK_ENTRY_POINT(pUnlockObject),
5946 instruction,
5947 instruction->GetDexPc(),
5948 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005949 if (instruction->IsEnter()) {
5950 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5951 } else {
5952 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5953 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005954}
5955
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005956void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5957void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5958void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5959
5960void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5961 LocationSummary* locations =
5962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5963 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5964 || instruction->GetResultType() == Primitive::kPrimLong);
5965 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005966 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005967 locations->SetOut(Location::SameAsFirstInput());
5968}
5969
5970void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5971 HandleBitwiseOperation(instruction);
5972}
5973
5974void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5975 HandleBitwiseOperation(instruction);
5976}
5977
5978void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5979 HandleBitwiseOperation(instruction);
5980}
5981
5982void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5983 LocationSummary* locations = instruction->GetLocations();
5984 Location first = locations->InAt(0);
5985 Location second = locations->InAt(1);
5986 DCHECK(first.Equals(locations->Out()));
5987
5988 if (instruction->GetResultType() == Primitive::kPrimInt) {
5989 if (second.IsRegister()) {
5990 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005991 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005992 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005993 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005994 } else {
5995 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005996 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005997 }
5998 } else if (second.IsConstant()) {
5999 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6000 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006001 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006002 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006003 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006004 } else {
6005 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006006 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006007 }
6008 } else {
6009 Address address(CpuRegister(RSP), second.GetStackIndex());
6010 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006011 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006012 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006013 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006014 } else {
6015 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006016 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006017 }
6018 }
6019 } else {
6020 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006021 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6022 bool second_is_constant = false;
6023 int64_t value = 0;
6024 if (second.IsConstant()) {
6025 second_is_constant = true;
6026 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006027 }
Mark Mendell40741f32015-04-20 22:10:34 -04006028 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006029
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006030 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006031 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006032 if (is_int32_value) {
6033 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6034 } else {
6035 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6036 }
6037 } else if (second.IsDoubleStackSlot()) {
6038 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006039 } else {
6040 __ andq(first_reg, second.AsRegister<CpuRegister>());
6041 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006042 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006043 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006044 if (is_int32_value) {
6045 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6046 } else {
6047 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6048 }
6049 } else if (second.IsDoubleStackSlot()) {
6050 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006051 } else {
6052 __ orq(first_reg, second.AsRegister<CpuRegister>());
6053 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006054 } else {
6055 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006056 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006057 if (is_int32_value) {
6058 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6059 } else {
6060 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6061 }
6062 } else if (second.IsDoubleStackSlot()) {
6063 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006064 } else {
6065 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6066 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006067 }
6068 }
6069}
6070
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006071void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6072 Location out,
6073 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006074 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006075 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6076 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006077 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006078 if (kUseBakerReadBarrier) {
6079 // Load with fast path based Baker's read barrier.
6080 // /* HeapReference<Object> */ out = *(out + offset)
6081 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006082 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006083 } else {
6084 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006085 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006086 // in the following move operation, as we will need it for the
6087 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006088 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006089 // /* HeapReference<Object> */ out = *(out + offset)
6090 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006091 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006092 }
6093 } else {
6094 // Plain load with no read barrier.
6095 // /* HeapReference<Object> */ out = *(out + offset)
6096 __ movl(out_reg, Address(out_reg, offset));
6097 __ MaybeUnpoisonHeapReference(out_reg);
6098 }
6099}
6100
6101void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6102 Location out,
6103 Location obj,
6104 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006105 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006106 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6107 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6108 if (kEmitCompilerReadBarrier) {
6109 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006110 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006111 // Load with fast path based Baker's read barrier.
6112 // /* HeapReference<Object> */ out = *(obj + offset)
6113 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006114 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006115 } else {
6116 // Load with slow path based read barrier.
6117 // /* HeapReference<Object> */ out = *(obj + offset)
6118 __ movl(out_reg, Address(obj_reg, offset));
6119 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6120 }
6121 } else {
6122 // Plain load with no read barrier.
6123 // /* HeapReference<Object> */ out = *(obj + offset)
6124 __ movl(out_reg, Address(obj_reg, offset));
6125 __ MaybeUnpoisonHeapReference(out_reg);
6126 }
6127}
6128
6129void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6130 Location root,
6131 CpuRegister obj,
6132 uint32_t offset) {
6133 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6134 if (kEmitCompilerReadBarrier) {
6135 if (kUseBakerReadBarrier) {
6136 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6137 // Baker's read barrier are used:
6138 //
6139 // root = obj.field;
6140 // if (Thread::Current()->GetIsGcMarking()) {
6141 // root = ReadBarrier::Mark(root)
6142 // }
6143
6144 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6145 __ movl(root_reg, Address(obj, offset));
6146 static_assert(
6147 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6148 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6149 "have different sizes.");
6150 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6151 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6152 "have different sizes.");
6153
6154 // Slow path used to mark the GC root `root`.
6155 SlowPathCode* slow_path =
6156 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6157 codegen_->AddSlowPath(slow_path);
6158
6159 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6160 /* no_rip */ true),
6161 Immediate(0));
6162 __ j(kNotEqual, slow_path->GetEntryLabel());
6163 __ Bind(slow_path->GetExitLabel());
6164 } else {
6165 // GC root loaded through a slow path for read barriers other
6166 // than Baker's.
6167 // /* GcRoot<mirror::Object>* */ root = obj + offset
6168 __ leaq(root_reg, Address(obj, offset));
6169 // /* mirror::Object* */ root = root->Read()
6170 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6171 }
6172 } else {
6173 // Plain GC root load with no read barrier.
6174 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6175 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006176 // Note that GC roots are not affected by heap poisoning, thus we
6177 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006178 }
6179}
6180
6181void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6182 Location ref,
6183 CpuRegister obj,
6184 uint32_t offset,
6185 Location temp,
6186 bool needs_null_check) {
6187 DCHECK(kEmitCompilerReadBarrier);
6188 DCHECK(kUseBakerReadBarrier);
6189
6190 // /* HeapReference<Object> */ ref = *(obj + offset)
6191 Address src(obj, offset);
6192 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6193}
6194
6195void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6196 Location ref,
6197 CpuRegister obj,
6198 uint32_t data_offset,
6199 Location index,
6200 Location temp,
6201 bool needs_null_check) {
6202 DCHECK(kEmitCompilerReadBarrier);
6203 DCHECK(kUseBakerReadBarrier);
6204
6205 // /* HeapReference<Object> */ ref =
6206 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6207 Address src = index.IsConstant() ?
6208 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6209 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6210 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6211}
6212
6213void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6214 Location ref,
6215 CpuRegister obj,
6216 const Address& src,
6217 Location temp,
6218 bool needs_null_check) {
6219 DCHECK(kEmitCompilerReadBarrier);
6220 DCHECK(kUseBakerReadBarrier);
6221
6222 // In slow path based read barriers, the read barrier call is
6223 // inserted after the original load. However, in fast path based
6224 // Baker's read barriers, we need to perform the load of
6225 // mirror::Object::monitor_ *before* the original reference load.
6226 // This load-load ordering is required by the read barrier.
6227 // The fast path/slow path (for Baker's algorithm) should look like:
6228 //
6229 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6230 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6231 // HeapReference<Object> ref = *src; // Original reference load.
6232 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6233 // if (is_gray) {
6234 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6235 // }
6236 //
6237 // Note: the original implementation in ReadBarrier::Barrier is
6238 // slightly more complex as:
6239 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006240 // the high-bits of rb_state, which are expected to be all zeroes
6241 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6242 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006243 // - it performs additional checks that we do not do here for
6244 // performance reasons.
6245
6246 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6247 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6248 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6249
6250 // /* int32_t */ monitor = obj->monitor_
6251 __ movl(temp_reg, Address(obj, monitor_offset));
6252 if (needs_null_check) {
6253 MaybeRecordImplicitNullCheck(instruction);
6254 }
6255 // /* LockWord */ lock_word = LockWord(monitor)
6256 static_assert(sizeof(LockWord) == sizeof(int32_t),
6257 "art::LockWord and int32_t have different sizes.");
6258 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6259 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6260 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6261 static_assert(
6262 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6263 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6264
6265 // Load fence to prevent load-load reordering.
6266 // Note that this is a no-op, thanks to the x86-64 memory model.
6267 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6268
6269 // The actual reference load.
6270 // /* HeapReference<Object> */ ref = *src
6271 __ movl(ref_reg, src);
6272
6273 // Object* ref = ref_addr->AsMirrorPtr()
6274 __ MaybeUnpoisonHeapReference(ref_reg);
6275
6276 // Slow path used to mark the object `ref` when it is gray.
6277 SlowPathCode* slow_path =
6278 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6279 AddSlowPath(slow_path);
6280
6281 // if (rb_state == ReadBarrier::gray_ptr_)
6282 // ref = ReadBarrier::Mark(ref);
6283 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6284 __ j(kEqual, slow_path->GetEntryLabel());
6285 __ Bind(slow_path->GetExitLabel());
6286}
6287
6288void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6289 Location out,
6290 Location ref,
6291 Location obj,
6292 uint32_t offset,
6293 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006294 DCHECK(kEmitCompilerReadBarrier);
6295
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006296 // Insert a slow path based read barrier *after* the reference load.
6297 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298 // If heap poisoning is enabled, the unpoisoning of the loaded
6299 // reference will be carried out by the runtime within the slow
6300 // path.
6301 //
6302 // Note that `ref` currently does not get unpoisoned (when heap
6303 // poisoning is enabled), which is alright as the `ref` argument is
6304 // not used by the artReadBarrierSlow entry point.
6305 //
6306 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6307 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6308 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6309 AddSlowPath(slow_path);
6310
Roland Levillain0d5a2812015-11-13 10:07:31 +00006311 __ jmp(slow_path->GetEntryLabel());
6312 __ Bind(slow_path->GetExitLabel());
6313}
6314
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006315void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6316 Location out,
6317 Location ref,
6318 Location obj,
6319 uint32_t offset,
6320 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006321 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006322 // Baker's read barriers shall be handled by the fast path
6323 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6324 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006325 // If heap poisoning is enabled, unpoisoning will be taken care of
6326 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006327 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006328 } else if (kPoisonHeapReferences) {
6329 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6330 }
6331}
6332
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006333void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6334 Location out,
6335 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006336 DCHECK(kEmitCompilerReadBarrier);
6337
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006338 // Insert a slow path based read barrier *after* the GC root load.
6339 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006340 // Note that GC roots are not affected by heap poisoning, so we do
6341 // not need to do anything special for this here.
6342 SlowPathCode* slow_path =
6343 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6344 AddSlowPath(slow_path);
6345
Roland Levillain0d5a2812015-11-13 10:07:31 +00006346 __ jmp(slow_path->GetEntryLabel());
6347 __ Bind(slow_path->GetExitLabel());
6348}
6349
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006350void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006351 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006352 LOG(FATAL) << "Unreachable";
6353}
6354
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006355void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006356 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006357 LOG(FATAL) << "Unreachable";
6358}
6359
Mark Mendellfe57faa2015-09-18 09:26:15 -04006360// Simple implementation of packed switch - generate cascaded compare/jumps.
6361void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6362 LocationSummary* locations =
6363 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6364 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006365 locations->AddTemp(Location::RequiresRegister());
6366 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006367}
6368
6369void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6370 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006371 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006372 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006373 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6374 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6375 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006376 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6377
6378 // Should we generate smaller inline compare/jumps?
6379 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6380 // Figure out the correct compare values and jump conditions.
6381 // Handle the first compare/branch as a special case because it might
6382 // jump to the default case.
6383 DCHECK_GT(num_entries, 2u);
6384 Condition first_condition;
6385 uint32_t index;
6386 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6387 if (lower_bound != 0) {
6388 first_condition = kLess;
6389 __ cmpl(value_reg_in, Immediate(lower_bound));
6390 __ j(first_condition, codegen_->GetLabelOf(default_block));
6391 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6392
6393 index = 1;
6394 } else {
6395 // Handle all the compare/jumps below.
6396 first_condition = kBelow;
6397 index = 0;
6398 }
6399
6400 // Handle the rest of the compare/jumps.
6401 for (; index + 1 < num_entries; index += 2) {
6402 int32_t compare_to_value = lower_bound + index + 1;
6403 __ cmpl(value_reg_in, Immediate(compare_to_value));
6404 // Jump to successors[index] if value < case_value[index].
6405 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6406 // Jump to successors[index + 1] if value == case_value[index + 1].
6407 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6408 }
6409
6410 if (index != num_entries) {
6411 // There are an odd number of entries. Handle the last one.
6412 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006413 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006414 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6415 }
6416
6417 // And the default for any other value.
6418 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6419 __ jmp(codegen_->GetLabelOf(default_block));
6420 }
6421 return;
6422 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006423
6424 // Remove the bias, if needed.
6425 Register value_reg_out = value_reg_in.AsRegister();
6426 if (lower_bound != 0) {
6427 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6428 value_reg_out = temp_reg.AsRegister();
6429 }
6430 CpuRegister value_reg(value_reg_out);
6431
6432 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006433 __ cmpl(value_reg, Immediate(num_entries - 1));
6434 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006435
Mark Mendell9c86b482015-09-18 13:36:07 -04006436 // We are in the range of the table.
6437 // Load the address of the jump table in the constant area.
6438 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006439
Mark Mendell9c86b482015-09-18 13:36:07 -04006440 // Load the (signed) offset from the jump table.
6441 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6442
6443 // Add the offset to the address of the table base.
6444 __ addq(temp_reg, base_reg);
6445
6446 // And jump.
6447 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006448}
6449
Aart Bikc5d47542016-01-27 17:00:35 -08006450void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6451 if (value == 0) {
6452 __ xorl(dest, dest);
6453 } else {
6454 __ movl(dest, Immediate(value));
6455 }
6456}
6457
Mark Mendell92e83bf2015-05-07 11:25:03 -04006458void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6459 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006460 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006461 __ xorl(dest, dest);
6462 } else if (value > 0 && IsInt<32>(value)) {
6463 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
6464 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6465 } else {
6466 __ movq(dest, Immediate(value));
6467 }
6468}
6469
Mark Mendellcfa410b2015-05-25 16:02:44 -04006470void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6471 DCHECK(dest.IsDoubleStackSlot());
6472 if (IsInt<32>(value)) {
6473 // Can move directly as an int32 constant.
6474 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6475 Immediate(static_cast<int32_t>(value)));
6476 } else {
6477 Load64BitValue(CpuRegister(TMP), value);
6478 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6479 }
6480}
6481
Mark Mendell9c86b482015-09-18 13:36:07 -04006482/**
6483 * Class to handle late fixup of offsets into constant area.
6484 */
6485class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6486 public:
6487 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6488 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6489
6490 protected:
6491 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6492
6493 CodeGeneratorX86_64* codegen_;
6494
6495 private:
6496 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6497 // Patch the correct offset for the instruction. We use the address of the
6498 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6499 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6500 int32_t relative_position = constant_offset - pos;
6501
6502 // Patch in the right value.
6503 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6504 }
6505
6506 // Location in constant area that the fixup refers to.
6507 size_t offset_into_constant_area_;
6508};
6509
6510/**
6511 t * Class to handle late fixup of offsets to a jump table that will be created in the
6512 * constant area.
6513 */
6514class JumpTableRIPFixup : public RIPFixup {
6515 public:
6516 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6517 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6518
6519 void CreateJumpTable() {
6520 X86_64Assembler* assembler = codegen_->GetAssembler();
6521
6522 // Ensure that the reference to the jump table has the correct offset.
6523 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6524 SetOffset(offset_in_constant_table);
6525
6526 // Compute the offset from the start of the function to this jump table.
6527 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6528
6529 // Populate the jump table with the correct values for the jump table.
6530 int32_t num_entries = switch_instr_->GetNumEntries();
6531 HBasicBlock* block = switch_instr_->GetBlock();
6532 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6533 // The value that we want is the target offset - the position of the table.
6534 for (int32_t i = 0; i < num_entries; i++) {
6535 HBasicBlock* b = successors[i];
6536 Label* l = codegen_->GetLabelOf(b);
6537 DCHECK(l->IsBound());
6538 int32_t offset_to_block = l->Position() - current_table_offset;
6539 assembler->AppendInt32(offset_to_block);
6540 }
6541 }
6542
6543 private:
6544 const HPackedSwitch* switch_instr_;
6545};
6546
Mark Mendellf55c3e02015-03-26 21:07:46 -04006547void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6548 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006549 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006550 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6551 // 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 -04006552 assembler->Align(4, 0);
6553 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006554
6555 // Populate any jump tables.
6556 for (auto jump_table : fixups_to_jump_tables_) {
6557 jump_table->CreateJumpTable();
6558 }
6559
6560 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006561 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006562 }
6563
6564 // And finish up.
6565 CodeGenerator::Finalize(allocator);
6566}
6567
Mark Mendellf55c3e02015-03-26 21:07:46 -04006568Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6569 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6570 return Address::RIP(fixup);
6571}
6572
6573Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6574 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6575 return Address::RIP(fixup);
6576}
6577
6578Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6579 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6580 return Address::RIP(fixup);
6581}
6582
6583Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6584 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6585 return Address::RIP(fixup);
6586}
6587
Andreas Gampe85b62f22015-09-09 13:15:38 -07006588// TODO: trg as memory.
6589void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6590 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006591 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006592 return;
6593 }
6594
6595 DCHECK_NE(type, Primitive::kPrimVoid);
6596
6597 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6598 if (trg.Equals(return_loc)) {
6599 return;
6600 }
6601
6602 // Let the parallel move resolver take care of all of this.
6603 HParallelMove parallel_move(GetGraph()->GetArena());
6604 parallel_move.AddMove(return_loc, trg, type, nullptr);
6605 GetMoveResolver()->EmitNativeCode(&parallel_move);
6606}
6607
Mark Mendell9c86b482015-09-18 13:36:07 -04006608Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6609 // Create a fixup to be used to create and address the jump table.
6610 JumpTableRIPFixup* table_fixup =
6611 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6612
6613 // We have to populate the jump tables.
6614 fixups_to_jump_tables_.push_back(table_fixup);
6615 return Address::RIP(table_fixup);
6616}
6617
Mark Mendellea5af682015-10-22 17:35:49 -04006618void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6619 const Address& addr_high,
6620 int64_t v,
6621 HInstruction* instruction) {
6622 if (IsInt<32>(v)) {
6623 int32_t v_32 = v;
6624 __ movq(addr_low, Immediate(v_32));
6625 MaybeRecordImplicitNullCheck(instruction);
6626 } else {
6627 // Didn't fit in a register. Do it in pieces.
6628 int32_t low_v = Low32Bits(v);
6629 int32_t high_v = High32Bits(v);
6630 __ movl(addr_low, Immediate(low_v));
6631 MaybeRecordImplicitNullCheck(instruction);
6632 __ movl(addr_high, Immediate(high_v));
6633 }
6634}
6635
Roland Levillain4d027112015-07-01 15:41:14 +01006636#undef __
6637
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006638} // namespace x86_64
6639} // namespace art