blob: 4f0f5f0ff835c6286bac67c4a9f6dc20406c0af8 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "intrinsics.h"
25#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "mirror/object_reference.h"
29#include "thread.h"
30#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "utils/x86_64/assembler_x86_64.h"
33#include "utils/x86_64/managed_register_x86_64.h"
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace art {
36
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace x86_64 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000044// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
45// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
46// generates less code/data with a small num_entries.
47static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000049static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Mark Mendell24f2dfa2015-01-14 19:51:45 -050052static constexpr int kC2ConditionMask = 0x400;
53
Roland Levillain62a46b22015-06-01 18:24:13 +010054#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010055#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Andreas Gampe85b62f22015-09-09 13:15:38 -070057class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060
Alexandre Rames2ed20af2015-03-06 13:55:35 +000061 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000062 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000064 if (instruction_->CanThrowIntoCatchBlock()) {
65 // Live registers will be restored in the catch block if caught.
66 SaveLiveRegisters(codegen, instruction_->GetLocations());
67 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000068 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
69 instruction_,
70 instruction_->GetDexPc(),
71 this);
Roland Levillain888d0672015-11-23 18:53:50 +000072 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 }
74
Alexandre Rames8158f282015-08-07 10:26:17 +010075 bool IsFatal() const OVERRIDE { return true; }
76
Alexandre Rames9931f312015-06-19 14:47:01 +010077 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
78
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
82};
83
Andreas Gampe85b62f22015-09-09 13:15:38 -070084class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000085 public:
86 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
87
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000089 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000091 if (instruction_->CanThrowIntoCatchBlock()) {
92 // Live registers will be restored in the catch block if caught.
93 SaveLiveRegisters(codegen, instruction_->GetLocations());
94 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000095 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
96 instruction_,
97 instruction_->GetDexPc(),
98 this);
Roland Levillain888d0672015-11-23 18:53:50 +000099 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000100 }
101
Alexandre Rames8158f282015-08-07 10:26:17 +0100102 bool IsFatal() const OVERRIDE { return true; }
103
Alexandre Rames9931f312015-06-19 14:47:01 +0100104 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
105
Calin Juravled0d48522014-11-04 16:40:20 +0000106 private:
107 HDivZeroCheck* const instruction_;
108 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
109};
110
Andreas Gampe85b62f22015-09-09 13:15:38 -0700111class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100113 DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000115
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000116 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000117 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000118 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000119 if (is_div_) {
120 __ negl(cpu_reg_);
121 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400122 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 }
124
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000125 } else {
126 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 if (is_div_) {
128 __ negq(cpu_reg_);
129 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400130 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000131 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000132 }
Calin Juravled0d48522014-11-04 16:40:20 +0000133 __ jmp(GetExitLabel());
134 }
135
Alexandre Rames9931f312015-06-19 14:47:01 +0100136 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
137
Calin Juravled0d48522014-11-04 16:40:20 +0000138 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000139 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000140 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000141 const bool is_div_;
142 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000143};
144
Andreas Gampe85b62f22015-09-09 13:15:38 -0700145class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100147 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000150 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000151 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000152 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000153 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000154 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
155 instruction_,
156 instruction_->GetDexPc(),
157 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000158 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000159 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 if (successor_ == nullptr) {
161 __ jmp(GetReturnLabel());
162 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000163 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100164 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 }
166
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100167 Label* GetReturnLabel() {
168 DCHECK(successor_ == nullptr);
169 return &return_label_;
170 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100172 HBasicBlock* GetSuccessor() const {
173 return successor_;
174 }
175
Alexandre Rames9931f312015-06-19 14:47:01 +0100176 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
177
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000178 private:
179 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100180 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181 Label return_label_;
182
183 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
184};
185
Andreas Gampe85b62f22015-09-09 13:15:38 -0700186class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100188 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
189 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000191 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100192 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000193 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000195 if (instruction_->CanThrowIntoCatchBlock()) {
196 // Live registers will be restored in the catch block if caught.
197 SaveLiveRegisters(codegen, instruction_->GetLocations());
198 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000199 // We're moving two locations to locations that could overlap, so we need a parallel
200 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100201 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000202 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100203 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000204 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100205 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100206 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100207 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
208 Primitive::kPrimInt);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000209 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
210 instruction_,
211 instruction_->GetDexPc(),
212 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000213 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100214 }
215
Alexandre Rames8158f282015-08-07 10:26:17 +0100216 bool IsFatal() const OVERRIDE { return true; }
217
Alexandre Rames9931f312015-06-19 14:47:01 +0100218 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
219
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100220 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100221 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100222
223 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
224};
225
Andreas Gampe85b62f22015-09-09 13:15:38 -0700226class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100227 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 LoadClassSlowPathX86_64(HLoadClass* cls,
229 HInstruction* at,
230 uint32_t dex_pc,
231 bool do_clinit)
232 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
233 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
234 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000236 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000238 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000241 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000242
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100243 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000245 x86_64_codegen->InvokeRuntime(do_clinit_ ?
246 QUICK_ENTRY_POINT(pInitializeStaticStorage) :
247 QUICK_ENTRY_POINT(pInitializeType),
248 at_,
249 dex_pc_,
250 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000251 if (do_clinit_) {
252 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
253 } else {
254 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
255 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100256
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 if (out.IsValid()) {
260 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000261 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 }
263
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000264 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100265 __ jmp(GetExitLabel());
266 }
267
Alexandre Rames9931f312015-06-19 14:47:01 +0100268 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
269
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100270 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 // The class this slow path will load.
272 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100273
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 // The instruction where this slow path is happening.
275 // (Might be the load class or an initialization check).
276 HInstruction* const at_;
277
278 // The dex PC of `at_`.
279 const uint32_t dex_pc_;
280
281 // Whether to initialize the class.
282 const bool do_clinit_;
283
284 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100285};
286
Andreas Gampe85b62f22015-09-09 13:15:38 -0700287class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000288 public:
289 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
290
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000291 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000292 LocationSummary* locations = instruction_->GetLocations();
293 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
294
Roland Levillain0d5a2812015-11-13 10:07:31 +0000295 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000296 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000297 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000298
299 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800300 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000301 Immediate(instruction_->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000302 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
303 instruction_,
304 instruction_->GetDexPc(),
305 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000306 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000307 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000308 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000309 __ jmp(GetExitLabel());
310 }
311
Alexandre Rames9931f312015-06-19 14:47:01 +0100312 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
313
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000314 private:
315 HLoadString* const instruction_;
316
317 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
318};
319
Andreas Gampe85b62f22015-09-09 13:15:38 -0700320class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000322 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
323 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000325 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100327 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
328 : locations->Out();
329 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 DCHECK(instruction_->IsCheckCast()
331 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
Roland Levillain0d5a2812015-11-13 10:07:31 +0000333 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000336 if (!is_fatal_) {
337 SaveLiveRegisters(codegen, locations);
338 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339
340 // We're moving two locations to locations that could overlap, so we need a parallel
341 // move resolver.
342 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000343 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100344 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000345 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100346 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100348 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
349 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000350
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000351 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000352 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
353 instruction_,
354 dex_pc,
355 this);
356 CheckEntrypointTypes<
357 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000358 } else {
359 DCHECK(instruction_->IsCheckCast());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000360 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
361 instruction_,
362 dex_pc,
363 this);
364 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000365 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000366
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000367 if (!is_fatal_) {
368 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000369 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 RestoreLiveRegisters(codegen, locations);
373 __ jmp(GetExitLabel());
374 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000375 }
376
Alexandre Rames9931f312015-06-19 14:47:01 +0100377 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
378
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000379 bool IsFatal() const OVERRIDE { return is_fatal_; }
380
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000381 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000382 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000383 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000384
385 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
386};
387
Andreas Gampe85b62f22015-09-09 13:15:38 -0700388class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700389 public:
Aart Bik42249c32016-01-07 15:33:50 -0800390 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 : instruction_(instruction) {}
392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000394 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395 __ Bind(GetEntryLabel());
396 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000397 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
Aart Bik42249c32016-01-07 15:33:50 -0800398 instruction_,
399 instruction_->GetDexPc(),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000400 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000401 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700402 }
403
Alexandre Rames9931f312015-06-19 14:47:01 +0100404 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
405
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700406 private:
Aart Bik42249c32016-01-07 15:33:50 -0800407 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700408 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
409};
410
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100411class ArraySetSlowPathX86_64 : public SlowPathCode {
412 public:
413 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : instruction_(instruction) {}
414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
416 LocationSummary* locations = instruction_->GetLocations();
417 __ Bind(GetEntryLabel());
418 SaveLiveRegisters(codegen, locations);
419
420 InvokeRuntimeCallingConvention calling_convention;
421 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
422 parallel_move.AddMove(
423 locations->InAt(0),
424 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
425 Primitive::kPrimNot,
426 nullptr);
427 parallel_move.AddMove(
428 locations->InAt(1),
429 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
430 Primitive::kPrimInt,
431 nullptr);
432 parallel_move.AddMove(
433 locations->InAt(2),
434 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
435 Primitive::kPrimNot,
436 nullptr);
437 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
438
Roland Levillain0d5a2812015-11-13 10:07:31 +0000439 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
440 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
441 instruction_,
442 instruction_->GetDexPc(),
443 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000444 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100445 RestoreLiveRegisters(codegen, locations);
446 __ jmp(GetExitLabel());
447 }
448
449 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
450
451 private:
452 HInstruction* const instruction_;
453
454 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
455};
456
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000457// Slow path marking an object during a read barrier.
458class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
459 public:
460 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location out, Location obj)
461 : instruction_(instruction), out_(out), obj_(obj) {
462 DCHECK(kEmitCompilerReadBarrier);
463 }
464
465 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
466
467 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
468 LocationSummary* locations = instruction_->GetLocations();
469 Register reg_out = out_.AsRegister<Register>();
470 DCHECK(locations->CanCall());
471 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
472 DCHECK(instruction_->IsInstanceFieldGet() ||
473 instruction_->IsStaticFieldGet() ||
474 instruction_->IsArrayGet() ||
475 instruction_->IsLoadClass() ||
476 instruction_->IsLoadString() ||
477 instruction_->IsInstanceOf() ||
478 instruction_->IsCheckCast())
479 << "Unexpected instruction in read barrier marking slow path: "
480 << instruction_->DebugName();
481
482 __ Bind(GetEntryLabel());
483 SaveLiveRegisters(codegen, locations);
484
485 InvokeRuntimeCallingConvention calling_convention;
486 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
487 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
488 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
489 instruction_,
490 instruction_->GetDexPc(),
491 this);
492 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
493 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
494
495 RestoreLiveRegisters(codegen, locations);
496 __ jmp(GetExitLabel());
497 }
498
499 private:
500 HInstruction* const instruction_;
501 const Location out_;
502 const Location obj_;
503
504 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
505};
506
Roland Levillain0d5a2812015-11-13 10:07:31 +0000507// Slow path generating a read barrier for a heap reference.
508class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
509 public:
510 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
511 Location out,
512 Location ref,
513 Location obj,
514 uint32_t offset,
515 Location index)
516 : instruction_(instruction),
517 out_(out),
518 ref_(ref),
519 obj_(obj),
520 offset_(offset),
521 index_(index) {
522 DCHECK(kEmitCompilerReadBarrier);
523 // If `obj` is equal to `out` or `ref`, it means the initial
524 // object has been overwritten by (or after) the heap object
525 // reference load to be instrumented, e.g.:
526 //
527 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000528 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000529 //
530 // In that case, we have lost the information about the original
531 // object, and the emitted read barrier cannot work properly.
532 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
533 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
534}
535
536 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
537 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
538 LocationSummary* locations = instruction_->GetLocations();
539 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
540 DCHECK(locations->CanCall());
541 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
542 DCHECK(!instruction_->IsInvoke() ||
543 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000544 instruction_->GetLocations()->Intrinsified()))
545 << "Unexpected instruction in read barrier for heap reference slow path: "
546 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000547
548 __ Bind(GetEntryLabel());
549 SaveLiveRegisters(codegen, locations);
550
551 // We may have to change the index's value, but as `index_` is a
552 // constant member (like other "inputs" of this slow path),
553 // introduce a copy of it, `index`.
554 Location index = index_;
555 if (index_.IsValid()) {
556 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
557 if (instruction_->IsArrayGet()) {
558 // Compute real offset and store it in index_.
559 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
560 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
561 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
562 // We are about to change the value of `index_reg` (see the
563 // calls to art::x86_64::X86_64Assembler::shll and
564 // art::x86_64::X86_64Assembler::AddImmediate below), but it
565 // has not been saved by the previous call to
566 // art::SlowPathCode::SaveLiveRegisters, as it is a
567 // callee-save register --
568 // art::SlowPathCode::SaveLiveRegisters does not consider
569 // callee-save registers, as it has been designed with the
570 // assumption that callee-save registers are supposed to be
571 // handled by the called function. So, as a callee-save
572 // register, `index_reg` _would_ eventually be saved onto
573 // the stack, but it would be too late: we would have
574 // changed its value earlier. Therefore, we manually save
575 // it here into another freely available register,
576 // `free_reg`, chosen of course among the caller-save
577 // registers (as a callee-save `free_reg` register would
578 // exhibit the same problem).
579 //
580 // Note we could have requested a temporary register from
581 // the register allocator instead; but we prefer not to, as
582 // this is a slow path, and we know we can find a
583 // caller-save register that is available.
584 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
585 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
586 index_reg = free_reg;
587 index = Location::RegisterLocation(index_reg);
588 } else {
589 // The initial register stored in `index_` has already been
590 // saved in the call to art::SlowPathCode::SaveLiveRegisters
591 // (as it is not a callee-save register), so we can freely
592 // use it.
593 }
594 // Shifting the index value contained in `index_reg` by the
595 // scale factor (2) cannot overflow in practice, as the
596 // runtime is unable to allocate object arrays with a size
597 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
598 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
599 static_assert(
600 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
601 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
602 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
603 } else {
604 DCHECK(instruction_->IsInvoke());
605 DCHECK(instruction_->GetLocations()->Intrinsified());
606 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
607 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
608 << instruction_->AsInvoke()->GetIntrinsic();
609 DCHECK_EQ(offset_, 0U);
610 DCHECK(index_.IsRegister());
611 }
612 }
613
614 // We're moving two or three locations to locations that could
615 // overlap, so we need a parallel move resolver.
616 InvokeRuntimeCallingConvention calling_convention;
617 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
618 parallel_move.AddMove(ref_,
619 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
620 Primitive::kPrimNot,
621 nullptr);
622 parallel_move.AddMove(obj_,
623 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
624 Primitive::kPrimNot,
625 nullptr);
626 if (index.IsValid()) {
627 parallel_move.AddMove(index,
628 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
629 Primitive::kPrimInt,
630 nullptr);
631 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
632 } else {
633 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
634 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
635 }
636 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
637 instruction_,
638 instruction_->GetDexPc(),
639 this);
640 CheckEntrypointTypes<
641 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
642 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
643
644 RestoreLiveRegisters(codegen, locations);
645 __ jmp(GetExitLabel());
646 }
647
648 const char* GetDescription() const OVERRIDE {
649 return "ReadBarrierForHeapReferenceSlowPathX86_64";
650 }
651
652 private:
653 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
654 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
655 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
656 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
657 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
658 return static_cast<CpuRegister>(i);
659 }
660 }
661 // We shall never fail to find a free caller-save register, as
662 // there are more than two core caller-save registers on x86-64
663 // (meaning it is possible to find one which is different from
664 // `ref` and `obj`).
665 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
666 LOG(FATAL) << "Could not find a free caller-save register";
667 UNREACHABLE();
668 }
669
670 HInstruction* const instruction_;
671 const Location out_;
672 const Location ref_;
673 const Location obj_;
674 const uint32_t offset_;
675 // An additional location containing an index to an array.
676 // Only used for HArrayGet and the UnsafeGetObject &
677 // UnsafeGetObjectVolatile intrinsics.
678 const Location index_;
679
680 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
681};
682
683// Slow path generating a read barrier for a GC root.
684class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
685 public:
686 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000687 : instruction_(instruction), out_(out), root_(root) {
688 DCHECK(kEmitCompilerReadBarrier);
689 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000690
691 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
692 LocationSummary* locations = instruction_->GetLocations();
693 DCHECK(locations->CanCall());
694 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000695 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
696 << "Unexpected instruction in read barrier for GC root slow path: "
697 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000698
699 __ Bind(GetEntryLabel());
700 SaveLiveRegisters(codegen, locations);
701
702 InvokeRuntimeCallingConvention calling_convention;
703 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
704 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
705 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
706 instruction_,
707 instruction_->GetDexPc(),
708 this);
709 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
710 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
711
712 RestoreLiveRegisters(codegen, locations);
713 __ jmp(GetExitLabel());
714 }
715
716 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
717
718 private:
719 HInstruction* const instruction_;
720 const Location out_;
721 const Location root_;
722
723 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
724};
725
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100726#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100727#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100728
Roland Levillain4fa13f62015-07-06 18:11:54 +0100729inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700730 switch (cond) {
731 case kCondEQ: return kEqual;
732 case kCondNE: return kNotEqual;
733 case kCondLT: return kLess;
734 case kCondLE: return kLessEqual;
735 case kCondGT: return kGreater;
736 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700737 case kCondB: return kBelow;
738 case kCondBE: return kBelowEqual;
739 case kCondA: return kAbove;
740 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700741 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100742 LOG(FATAL) << "Unreachable";
743 UNREACHABLE();
744}
745
Aart Bike9f37602015-10-09 11:15:55 -0700746// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100747inline Condition X86_64FPCondition(IfCondition cond) {
748 switch (cond) {
749 case kCondEQ: return kEqual;
750 case kCondNE: return kNotEqual;
751 case kCondLT: return kBelow;
752 case kCondLE: return kBelowEqual;
753 case kCondGT: return kAbove;
754 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700755 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100756 };
757 LOG(FATAL) << "Unreachable";
758 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700759}
760
Vladimir Markodc151b22015-10-15 18:02:30 +0100761HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
762 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
763 MethodReference target_method ATTRIBUTE_UNUSED) {
764 switch (desired_dispatch_info.code_ptr_location) {
765 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
766 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
767 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
768 return HInvokeStaticOrDirect::DispatchInfo {
769 desired_dispatch_info.method_load_kind,
770 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
771 desired_dispatch_info.method_load_data,
772 0u
773 };
774 default:
775 return desired_dispatch_info;
776 }
777}
778
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800779void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100780 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800781 // All registers are assumed to be correctly set up.
782
Vladimir Marko58155012015-08-19 12:49:41 +0000783 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
784 switch (invoke->GetMethodLoadKind()) {
785 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
786 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000787 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000788 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000789 break;
790 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000791 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000792 break;
793 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
794 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
795 break;
796 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
797 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
798 method_patches_.emplace_back(invoke->GetTargetMethod());
799 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
800 break;
801 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000802 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
803 invoke->GetDexCacheArrayOffset());
Vladimir Marko58155012015-08-19 12:49:41 +0000804 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000805 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko58155012015-08-19 12:49:41 +0000806 // Bind the label at the end of the "movl" insn.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000807 __ Bind(&pc_relative_dex_cache_patches_.back().label);
Vladimir Marko58155012015-08-19 12:49:41 +0000808 break;
809 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000810 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000811 Register method_reg;
812 CpuRegister reg = temp.AsRegister<CpuRegister>();
813 if (current_method.IsRegister()) {
814 method_reg = current_method.AsRegister<Register>();
815 } else {
816 DCHECK(invoke->GetLocations()->Intrinsified());
817 DCHECK(!current_method.IsValid());
818 method_reg = reg.AsRegister();
819 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
820 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000821 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100822 __ movq(reg,
823 Address(CpuRegister(method_reg),
824 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000825 // temp = temp[index_in_cache]
826 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
827 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
828 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100829 }
Vladimir Marko58155012015-08-19 12:49:41 +0000830 }
831
832 switch (invoke->GetCodePtrLocation()) {
833 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
834 __ call(&frame_entry_label_);
835 break;
836 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
837 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
838 Label* label = &relative_call_patches_.back().label;
839 __ call(label); // Bind to the patch label, override at link time.
840 __ Bind(label); // Bind the label at the end of the "call" insn.
841 break;
842 }
843 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
844 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100845 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
846 LOG(FATAL) << "Unsupported";
847 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000848 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
849 // (callee_method + offset_of_quick_compiled_code)()
850 __ call(Address(callee_method.AsRegister<CpuRegister>(),
851 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
852 kX86_64WordSize).SizeValue()));
853 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000854 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800855
856 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800857}
858
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000859void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
860 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
861 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
862 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000863
864 // Use the calling convention instead of the location of the receiver, as
865 // intrinsics may have put the receiver in a different register. In the intrinsics
866 // slow path, the arguments have been moved to the right place, so here we are
867 // guaranteed that the receiver is the first register of the calling convention.
868 InvokeDexCallingConvention calling_convention;
869 Register receiver = calling_convention.GetRegisterAt(0);
870
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000871 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000872 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000873 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000874 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000875 // Instead of simply (possibly) unpoisoning `temp` here, we should
876 // emit a read barrier for the previous class reference load.
877 // However this is not required in practice, as this is an
878 // intermediate/temporary reference and because the current
879 // concurrent copying collector keeps the from-space memory
880 // intact/accessible until the end of the marking phase (the
881 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000882 __ MaybeUnpoisonHeapReference(temp);
883 // temp = temp->GetMethodAt(method_offset);
884 __ movq(temp, Address(temp, method_offset));
885 // call temp->GetEntryPoint();
886 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
887 kX86_64WordSize).SizeValue()));
888}
889
Vladimir Marko58155012015-08-19 12:49:41 +0000890void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
891 DCHECK(linker_patches->empty());
892 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000893 method_patches_.size() +
894 relative_call_patches_.size() +
895 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000896 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000897 // The label points to the end of the "movl" insn but the literal offset for method
898 // patch needs to point to the embedded constant which occupies the last 4 bytes.
899 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000900 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000901 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000902 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
903 info.target_method.dex_file,
904 info.target_method.dex_method_index));
905 }
906 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000907 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000908 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
909 info.target_method.dex_file,
910 info.target_method.dex_method_index));
911 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000912 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
913 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000914 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
915 &info.target_dex_file,
916 info.label.Position(),
917 info.element_offset));
918 }
919}
920
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100921void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100922 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923}
924
925void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100926 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927}
928
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100929size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
930 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
931 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100932}
933
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100934size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
935 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
936 return kX86_64WordSize;
937}
938
939size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
940 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
941 return kX86_64WordSize;
942}
943
944size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
945 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
946 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100947}
948
Calin Juravle175dc732015-08-25 15:42:32 +0100949void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
950 HInstruction* instruction,
951 uint32_t dex_pc,
952 SlowPathCode* slow_path) {
953 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
954 instruction,
955 dex_pc,
956 slow_path);
957}
958
959void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100960 HInstruction* instruction,
961 uint32_t dex_pc,
962 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100963 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000964 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100965 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100966}
967
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000968static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000969// Use a fake return address register to mimic Quick.
970static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400971CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000972 const X86_64InstructionSetFeatures& isa_features,
973 const CompilerOptions& compiler_options,
974 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000975 : CodeGenerator(graph,
976 kNumberOfCpuRegisters,
977 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000978 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000979 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
980 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000981 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000982 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
983 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100984 compiler_options,
985 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100986 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100987 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000988 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400989 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400990 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000991 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100992 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
993 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000994 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -0400995 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000996 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
997}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100998
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100999InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1000 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001001 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001002 assembler_(codegen->GetAssembler()),
1003 codegen_(codegen) {}
1004
David Brazdil58282f42016-01-14 12:45:10 +00001005void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001006 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001007 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001008
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001009 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001010 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001011}
1012
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001013static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001014 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001015}
David Srbecky9d8606d2015-04-12 09:35:32 +01001016
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001017static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001018 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001019}
1020
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001021void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001022 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001023 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001024 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001025 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001026 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001027
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001028 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001029 __ testq(CpuRegister(RAX), Address(
1030 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001031 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001032 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001033
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001034 if (HasEmptyFrame()) {
1035 return;
1036 }
1037
Nicolas Geoffray98893962015-01-21 12:32:32 +00001038 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001039 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001040 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001041 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001042 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1043 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001044 }
1045 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001046
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001047 int adjust = GetFrameSize() - GetCoreSpillSize();
1048 __ subq(CpuRegister(RSP), Immediate(adjust));
1049 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001050 uint32_t xmm_spill_location = GetFpuSpillStart();
1051 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001052
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001053 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1054 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001055 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1056 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1057 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001058 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001059 }
1060
Mathieu Chartiere401d142015-04-22 13:56:20 -07001061 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001062 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001063}
1064
1065void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001066 __ cfi().RememberState();
1067 if (!HasEmptyFrame()) {
1068 uint32_t xmm_spill_location = GetFpuSpillStart();
1069 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1070 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1071 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1072 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1073 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1074 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1075 }
1076 }
1077
1078 int adjust = GetFrameSize() - GetCoreSpillSize();
1079 __ addq(CpuRegister(RSP), Immediate(adjust));
1080 __ cfi().AdjustCFAOffset(-adjust);
1081
1082 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1083 Register reg = kCoreCalleeSaves[i];
1084 if (allocated_registers_.ContainsCoreRegister(reg)) {
1085 __ popq(CpuRegister(reg));
1086 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1087 __ cfi().Restore(DWARFReg(reg));
1088 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001089 }
1090 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001091 __ ret();
1092 __ cfi().RestoreState();
1093 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001094}
1095
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001096void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1097 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098}
1099
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001100Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
1101 switch (load->GetType()) {
1102 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001104 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001105
1106 case Primitive::kPrimInt:
1107 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001109 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001110
1111 case Primitive::kPrimBoolean:
1112 case Primitive::kPrimByte:
1113 case Primitive::kPrimChar:
1114 case Primitive::kPrimShort:
1115 case Primitive::kPrimVoid:
1116 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -07001117 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001118 }
1119
1120 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -07001121 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001122}
1123
1124void CodeGeneratorX86_64::Move(Location destination, Location source) {
1125 if (source.Equals(destination)) {
1126 return;
1127 }
1128 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001129 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001130 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001131 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001133 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001134 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001135 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1136 } else if (source.IsConstant()) {
1137 HConstant* constant = source.GetConstant();
1138 if (constant->IsLongConstant()) {
1139 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1140 } else {
1141 Load32BitValue(dest, GetInt32ValueOf(constant));
1142 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001143 } else {
1144 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001145 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001146 }
1147 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001148 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001149 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001150 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001151 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001152 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1153 } else if (source.IsConstant()) {
1154 HConstant* constant = source.GetConstant();
1155 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1156 if (constant->IsFloatConstant()) {
1157 Load32BitValue(dest, static_cast<int32_t>(value));
1158 } else {
1159 Load64BitValue(dest, value);
1160 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001161 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001162 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001163 } else {
1164 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001165 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001166 }
1167 } else if (destination.IsStackSlot()) {
1168 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001169 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001170 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 } else if (source.IsFpuRegister()) {
1172 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001173 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001174 } else if (source.IsConstant()) {
1175 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001176 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001177 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001178 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001179 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001180 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1181 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001182 }
1183 } else {
1184 DCHECK(destination.IsDoubleStackSlot());
1185 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001186 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001187 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001188 } else if (source.IsFpuRegister()) {
1189 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001190 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001191 } else if (source.IsConstant()) {
1192 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001193 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001194 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001195 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001196 } else {
1197 DCHECK(constant->IsLongConstant());
1198 value = constant->AsLongConstant()->GetValue();
1199 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001200 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001201 } else {
1202 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001203 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1204 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001205 }
1206 }
1207}
1208
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001209void CodeGeneratorX86_64::Move(HInstruction* instruction,
1210 Location location,
1211 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001212 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001213 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -07001214 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001215 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001216 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001217 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001218 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001219 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1220 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001221 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001222 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001223 } else if (location.IsStackSlot()) {
1224 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
1225 } else {
1226 DCHECK(location.IsConstant());
1227 DCHECK_EQ(location.GetConstant(), const_to_move);
1228 }
1229 } else if (const_to_move->IsLongConstant()) {
1230 int64_t value = const_to_move->AsLongConstant()->GetValue();
1231 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04001232 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001233 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -04001234 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001235 } else {
1236 DCHECK(location.IsConstant());
1237 DCHECK_EQ(location.GetConstant(), const_to_move);
1238 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001239 }
Roland Levillain476df552014-10-09 17:51:36 +01001240 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 switch (instruction->GetType()) {
1242 case Primitive::kPrimBoolean:
1243 case Primitive::kPrimByte:
1244 case Primitive::kPrimChar:
1245 case Primitive::kPrimShort:
1246 case Primitive::kPrimInt:
1247 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001249 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
1250 break;
1251
1252 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001253 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +00001254 Move(location,
1255 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001256 break;
1257
1258 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001259 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001261 } else if (instruction->IsTemporary()) {
1262 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
1263 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001265 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001266 switch (instruction->GetType()) {
1267 case Primitive::kPrimBoolean:
1268 case Primitive::kPrimByte:
1269 case Primitive::kPrimChar:
1270 case Primitive::kPrimShort:
1271 case Primitive::kPrimInt:
1272 case Primitive::kPrimNot:
1273 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001274 case Primitive::kPrimFloat:
1275 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001276 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001277 break;
1278
1279 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001280 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001281 }
1282 }
1283}
1284
Calin Juravle175dc732015-08-25 15:42:32 +01001285void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1286 DCHECK(location.IsRegister());
1287 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1288}
1289
Calin Juravlee460d1d2015-09-29 04:52:17 +01001290void CodeGeneratorX86_64::MoveLocation(
1291 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1292 Move(dst, src);
1293}
1294
1295void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1296 if (location.IsRegister()) {
1297 locations->AddTemp(location);
1298 } else {
1299 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1300 }
1301}
1302
David Brazdilfc6a86a2015-06-26 10:33:45 +00001303void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001304 DCHECK(!successor->IsExitBlock());
1305
1306 HBasicBlock* block = got->GetBlock();
1307 HInstruction* previous = got->GetPrevious();
1308
1309 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001310 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001311 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1312 return;
1313 }
1314
1315 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1316 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1317 }
1318 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001319 __ jmp(codegen_->GetLabelOf(successor));
1320 }
1321}
1322
David Brazdilfc6a86a2015-06-26 10:33:45 +00001323void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1324 got->SetLocations(nullptr);
1325}
1326
1327void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1328 HandleGoto(got, got->GetSuccessor());
1329}
1330
1331void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1332 try_boundary->SetLocations(nullptr);
1333}
1334
1335void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1336 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1337 if (!successor->IsExitBlock()) {
1338 HandleGoto(try_boundary, successor);
1339 }
1340}
1341
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001342void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1343 exit->SetLocations(nullptr);
1344}
1345
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001346void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001347}
1348
Mark Mendell152408f2015-12-31 12:28:50 -05001349template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001350void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001351 LabelType* true_label,
1352 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001353 if (cond->IsFPConditionTrueIfNaN()) {
1354 __ j(kUnordered, true_label);
1355 } else if (cond->IsFPConditionFalseIfNaN()) {
1356 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001357 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001358 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001359}
1360
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001361void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001362 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001363
Mark Mendellc4701932015-04-10 13:18:51 -04001364 Location left = locations->InAt(0);
1365 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001366 Primitive::Type type = condition->InputAt(0)->GetType();
1367 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001368 case Primitive::kPrimBoolean:
1369 case Primitive::kPrimByte:
1370 case Primitive::kPrimChar:
1371 case Primitive::kPrimShort:
1372 case Primitive::kPrimInt:
1373 case Primitive::kPrimNot: {
1374 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1375 if (right.IsConstant()) {
1376 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1377 if (value == 0) {
1378 __ testl(left_reg, left_reg);
1379 } else {
1380 __ cmpl(left_reg, Immediate(value));
1381 }
1382 } else if (right.IsStackSlot()) {
1383 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1384 } else {
1385 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1386 }
1387 break;
1388 }
Mark Mendellc4701932015-04-10 13:18:51 -04001389 case Primitive::kPrimLong: {
1390 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1391 if (right.IsConstant()) {
1392 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001393 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001394 } else if (right.IsDoubleStackSlot()) {
1395 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1396 } else {
1397 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1398 }
Mark Mendellc4701932015-04-10 13:18:51 -04001399 break;
1400 }
1401 case Primitive::kPrimFloat: {
1402 if (right.IsFpuRegister()) {
1403 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1404 } else if (right.IsConstant()) {
1405 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1406 codegen_->LiteralFloatAddress(
1407 right.GetConstant()->AsFloatConstant()->GetValue()));
1408 } else {
1409 DCHECK(right.IsStackSlot());
1410 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1411 Address(CpuRegister(RSP), right.GetStackIndex()));
1412 }
Mark Mendellc4701932015-04-10 13:18:51 -04001413 break;
1414 }
1415 case Primitive::kPrimDouble: {
1416 if (right.IsFpuRegister()) {
1417 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1418 } else if (right.IsConstant()) {
1419 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1420 codegen_->LiteralDoubleAddress(
1421 right.GetConstant()->AsDoubleConstant()->GetValue()));
1422 } else {
1423 DCHECK(right.IsDoubleStackSlot());
1424 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1425 Address(CpuRegister(RSP), right.GetStackIndex()));
1426 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001427 break;
1428 }
1429 default:
1430 LOG(FATAL) << "Unexpected condition type " << type;
1431 }
1432}
1433
1434template<class LabelType>
1435void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1436 LabelType* true_target_in,
1437 LabelType* false_target_in) {
1438 // Generated branching requires both targets to be explicit. If either of the
1439 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1440 LabelType fallthrough_target;
1441 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1442 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1443
1444 // Generate the comparison to set the CC.
1445 GenerateCompareTest(condition);
1446
1447 // Now generate the correct jump(s).
1448 Primitive::Type type = condition->InputAt(0)->GetType();
1449 switch (type) {
1450 case Primitive::kPrimLong: {
1451 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1452 break;
1453 }
1454 case Primitive::kPrimFloat: {
1455 GenerateFPJumps(condition, true_target, false_target);
1456 break;
1457 }
1458 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001459 GenerateFPJumps(condition, true_target, false_target);
1460 break;
1461 }
1462 default:
1463 LOG(FATAL) << "Unexpected condition type " << type;
1464 }
1465
David Brazdil0debae72015-11-12 18:37:00 +00001466 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001467 __ jmp(false_target);
1468 }
David Brazdil0debae72015-11-12 18:37:00 +00001469
1470 if (fallthrough_target.IsLinked()) {
1471 __ Bind(&fallthrough_target);
1472 }
Mark Mendellc4701932015-04-10 13:18:51 -04001473}
1474
David Brazdil0debae72015-11-12 18:37:00 +00001475static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1476 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1477 // are set only strictly before `branch`. We can't use the eflags on long
1478 // conditions if they are materialized due to the complex branching.
1479 return cond->IsCondition() &&
1480 cond->GetNext() == branch &&
1481 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1482}
1483
Mark Mendell152408f2015-12-31 12:28:50 -05001484template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001485void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001486 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001487 LabelType* true_target,
1488 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001489 HInstruction* cond = instruction->InputAt(condition_input_index);
1490
1491 if (true_target == nullptr && false_target == nullptr) {
1492 // Nothing to do. The code always falls through.
1493 return;
1494 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001495 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001496 if (cond->AsIntConstant()->IsOne()) {
1497 if (true_target != nullptr) {
1498 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001499 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001500 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001501 DCHECK(cond->AsIntConstant()->IsZero());
1502 if (false_target != nullptr) {
1503 __ jmp(false_target);
1504 }
1505 }
1506 return;
1507 }
1508
1509 // The following code generates these patterns:
1510 // (1) true_target == nullptr && false_target != nullptr
1511 // - opposite condition true => branch to false_target
1512 // (2) true_target != nullptr && false_target == nullptr
1513 // - condition true => branch to true_target
1514 // (3) true_target != nullptr && false_target != nullptr
1515 // - condition true => branch to true_target
1516 // - branch to false_target
1517 if (IsBooleanValueOrMaterializedCondition(cond)) {
1518 if (AreEflagsSetFrom(cond, instruction)) {
1519 if (true_target == nullptr) {
1520 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1521 } else {
1522 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1523 }
1524 } else {
1525 // Materialized condition, compare against 0.
1526 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1527 if (lhs.IsRegister()) {
1528 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1529 } else {
1530 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1531 }
1532 if (true_target == nullptr) {
1533 __ j(kEqual, false_target);
1534 } else {
1535 __ j(kNotEqual, true_target);
1536 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001537 }
1538 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001539 // Condition has not been materialized, use its inputs as the
1540 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001541 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001542
David Brazdil0debae72015-11-12 18:37:00 +00001543 // If this is a long or FP comparison that has been folded into
1544 // the HCondition, generate the comparison directly.
1545 Primitive::Type type = condition->InputAt(0)->GetType();
1546 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1547 GenerateCompareTestAndBranch(condition, true_target, false_target);
1548 return;
1549 }
1550
1551 Location lhs = condition->GetLocations()->InAt(0);
1552 Location rhs = condition->GetLocations()->InAt(1);
1553 if (rhs.IsRegister()) {
1554 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1555 } else if (rhs.IsConstant()) {
1556 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001557 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001558 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001559 __ cmpl(lhs.AsRegister<CpuRegister>(),
1560 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1561 }
1562 if (true_target == nullptr) {
1563 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1564 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001565 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001566 }
Dave Allison20dfc792014-06-16 20:44:29 -07001567 }
David Brazdil0debae72015-11-12 18:37:00 +00001568
1569 // If neither branch falls through (case 3), the conditional branch to `true_target`
1570 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1571 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001572 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001573 }
1574}
1575
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001576void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1578 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001579 locations->SetInAt(0, Location::Any());
1580 }
1581}
1582
1583void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001584 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1585 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1586 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1587 nullptr : codegen_->GetLabelOf(true_successor);
1588 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1589 nullptr : codegen_->GetLabelOf(false_successor);
1590 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001591}
1592
1593void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1594 LocationSummary* locations = new (GetGraph()->GetArena())
1595 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001596 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001597 locations->SetInAt(0, Location::Any());
1598 }
1599}
1600
1601void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001602 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001603 GenerateTestAndBranch<Label>(deoptimize,
1604 /* condition_input_index */ 0,
1605 slow_path->GetEntryLabel(),
1606 /* false_target */ nullptr);
1607}
1608
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001609static bool SelectCanUseCMOV(HSelect* select) {
1610 // There are no conditional move instructions for XMMs.
1611 if (Primitive::IsFloatingPointType(select->GetType())) {
1612 return false;
1613 }
1614
1615 // A FP condition doesn't generate the single CC that we need.
1616 HInstruction* condition = select->GetCondition();
1617 if (condition->IsCondition() &&
1618 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1619 return false;
1620 }
1621
1622 // We can generate a CMOV for this Select.
1623 return true;
1624}
1625
David Brazdil74eb1b22015-12-14 11:44:01 +00001626void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1627 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1628 if (Primitive::IsFloatingPointType(select->GetType())) {
1629 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001630 // Since we can't use CMOV, there is no need to force 'true' into a register.
1631 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001632 } else {
1633 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001634 if (SelectCanUseCMOV(select)) {
1635 locations->SetInAt(1, Location::RequiresRegister());
1636 } else {
1637 // Since we can't use CMOV, there is no need to force 'true' into a register.
1638 locations->SetInAt(1, Location::Any());
1639 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001640 }
1641 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1642 locations->SetInAt(2, Location::RequiresRegister());
1643 }
1644 locations->SetOut(Location::SameAsFirstInput());
1645}
1646
1647void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1648 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001649 if (SelectCanUseCMOV(select)) {
1650 // If both the condition and the source types are integer, we can generate
1651 // a CMOV to implement Select.
1652 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
1653 CpuRegister value_true = locations->InAt(1).AsRegister<CpuRegister>();
1654 DCHECK(locations->InAt(0).Equals(locations->Out()));
1655
1656 HInstruction* select_condition = select->GetCondition();
1657 Condition cond = kNotEqual;
1658
1659 // Figure out how to test the 'condition'.
1660 if (select_condition->IsCondition()) {
1661 HCondition* condition = select_condition->AsCondition();
1662 if (!condition->IsEmittedAtUseSite()) {
1663 // This was a previously materialized condition.
1664 // Can we use the existing condition code?
1665 if (AreEflagsSetFrom(condition, select)) {
1666 // Materialization was the previous instruction. Condition codes are right.
1667 cond = X86_64IntegerCondition(condition->GetCondition());
1668 } else {
1669 // No, we have to recreate the condition code.
1670 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1671 __ testl(cond_reg, cond_reg);
1672 }
1673 } else {
1674 GenerateCompareTest(condition);
1675 cond = X86_64IntegerCondition(condition->GetCondition());
1676 }
1677 } else {
1678 // Must be a boolean condition, which needs to be compared to 0.
1679 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1680 __ testl(cond_reg, cond_reg);
1681 }
1682
1683 // If the condition is true, overwrite the output, which already contains false.
1684 // Generate the correct sized CMOV.
1685 __ cmov(cond, value_false, value_true, select->GetType() == Primitive::kPrimLong);
1686 } else {
1687 NearLabel false_target;
1688 GenerateTestAndBranch<NearLabel>(select,
1689 /* condition_input_index */ 2,
1690 /* true_target */ nullptr,
1691 &false_target);
1692 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1693 __ Bind(&false_target);
1694 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001695}
1696
David Srbecky0cf44932015-12-09 14:09:59 +00001697void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1698 new (GetGraph()->GetArena()) LocationSummary(info);
1699}
1700
1701void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001702 if (codegen_->HasStackMapAtCurrentPc()) {
1703 // Ensure that we do not collide with the stack map of the previous instruction.
1704 __ nop();
1705 }
David Srbecky0cf44932015-12-09 14:09:59 +00001706 codegen_->RecordPcInfo(info, info->GetDexPc());
1707}
1708
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001709void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1710 local->SetLocations(nullptr);
1711}
1712
1713void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1714 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1715}
1716
1717void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1718 local->SetLocations(nullptr);
1719}
1720
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001721void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001722 // Nothing to do, this is driven by the code generator.
1723}
1724
1725void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001726 LocationSummary* locations =
1727 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001728 switch (store->InputAt(1)->GetType()) {
1729 case Primitive::kPrimBoolean:
1730 case Primitive::kPrimByte:
1731 case Primitive::kPrimChar:
1732 case Primitive::kPrimShort:
1733 case Primitive::kPrimInt:
1734 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001735 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001736 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1737 break;
1738
1739 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001740 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001741 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1742 break;
1743
1744 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001745 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001746 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001747}
1748
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001749void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001750}
1751
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001752void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001753 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001754 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001755 // Handle the long/FP comparisons made in instruction simplification.
1756 switch (cond->InputAt(0)->GetType()) {
1757 case Primitive::kPrimLong:
1758 locations->SetInAt(0, Location::RequiresRegister());
1759 locations->SetInAt(1, Location::Any());
1760 break;
1761 case Primitive::kPrimFloat:
1762 case Primitive::kPrimDouble:
1763 locations->SetInAt(0, Location::RequiresFpuRegister());
1764 locations->SetInAt(1, Location::Any());
1765 break;
1766 default:
1767 locations->SetInAt(0, Location::RequiresRegister());
1768 locations->SetInAt(1, Location::Any());
1769 break;
1770 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001771 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001772 locations->SetOut(Location::RequiresRegister());
1773 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001774}
1775
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001777 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001778 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001779 }
Mark Mendellc4701932015-04-10 13:18:51 -04001780
1781 LocationSummary* locations = cond->GetLocations();
1782 Location lhs = locations->InAt(0);
1783 Location rhs = locations->InAt(1);
1784 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001785 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001786
1787 switch (cond->InputAt(0)->GetType()) {
1788 default:
1789 // Integer case.
1790
1791 // Clear output register: setcc only sets the low byte.
1792 __ xorl(reg, reg);
1793
1794 if (rhs.IsRegister()) {
1795 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1796 } else if (rhs.IsConstant()) {
1797 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001798 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001799 } else {
1800 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1801 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001802 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001803 return;
1804 case Primitive::kPrimLong:
1805 // Clear output register: setcc only sets the low byte.
1806 __ xorl(reg, reg);
1807
1808 if (rhs.IsRegister()) {
1809 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1810 } else if (rhs.IsConstant()) {
1811 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001812 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001813 } else {
1814 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1815 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001816 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001817 return;
1818 case Primitive::kPrimFloat: {
1819 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1820 if (rhs.IsConstant()) {
1821 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1822 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1823 } else if (rhs.IsStackSlot()) {
1824 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1825 } else {
1826 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1827 }
1828 GenerateFPJumps(cond, &true_label, &false_label);
1829 break;
1830 }
1831 case Primitive::kPrimDouble: {
1832 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1833 if (rhs.IsConstant()) {
1834 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1835 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1836 } else if (rhs.IsDoubleStackSlot()) {
1837 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1838 } else {
1839 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1840 }
1841 GenerateFPJumps(cond, &true_label, &false_label);
1842 break;
1843 }
1844 }
1845
1846 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001847 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001848
Roland Levillain4fa13f62015-07-06 18:11:54 +01001849 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001850 __ Bind(&false_label);
1851 __ xorl(reg, reg);
1852 __ jmp(&done_label);
1853
Roland Levillain4fa13f62015-07-06 18:11:54 +01001854 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001855 __ Bind(&true_label);
1856 __ movl(reg, Immediate(1));
1857 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001858}
1859
1860void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001861 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001862}
1863
1864void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001865 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001866}
1867
1868void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001869 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001870}
1871
1872void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001873 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001874}
1875
1876void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001877 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001878}
1879
1880void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001881 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001882}
1883
1884void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001885 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001886}
1887
1888void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001889 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001890}
1891
1892void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001893 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001894}
1895
1896void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001897 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001898}
1899
1900void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001901 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001902}
1903
1904void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001905 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001906}
1907
Aart Bike9f37602015-10-09 11:15:55 -07001908void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001909 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001910}
1911
1912void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001913 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001914}
1915
1916void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001917 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001918}
1919
1920void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001921 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001922}
1923
1924void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001925 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001926}
1927
1928void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001929 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001930}
1931
1932void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001933 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001934}
1935
1936void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001937 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001938}
1939
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001940void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001941 LocationSummary* locations =
1942 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001943 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08001944 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001945 case Primitive::kPrimLong: {
1946 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001947 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001948 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1949 break;
1950 }
1951 case Primitive::kPrimFloat:
1952 case Primitive::kPrimDouble: {
1953 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001954 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001955 locations->SetOut(Location::RequiresRegister());
1956 break;
1957 }
1958 default:
1959 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1960 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001961}
1962
1963void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001964 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001965 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001966 Location left = locations->InAt(0);
1967 Location right = locations->InAt(1);
1968
Mark Mendell0c9497d2015-08-21 09:30:05 -04001969 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001970 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001971 Condition less_cond = kLess;
1972
Calin Juravleddb7df22014-11-25 20:56:51 +00001973 switch (type) {
Aart Bika19616e2016-02-01 18:57:58 -08001974 case Primitive::kPrimInt: {
1975 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1976 if (right.IsConstant()) {
1977 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1978 codegen_->Compare32BitValue(left_reg, value);
1979 } else if (right.IsStackSlot()) {
1980 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1981 } else {
1982 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1983 }
1984 break;
1985 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001986 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001987 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1988 if (right.IsConstant()) {
1989 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001990 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001991 } else if (right.IsDoubleStackSlot()) {
1992 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001993 } else {
1994 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1995 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001996 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001997 }
1998 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001999 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2000 if (right.IsConstant()) {
2001 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2002 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2003 } else if (right.IsStackSlot()) {
2004 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2005 } else {
2006 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2007 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002008 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002009 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002010 break;
2011 }
2012 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04002013 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2014 if (right.IsConstant()) {
2015 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2016 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2017 } else if (right.IsDoubleStackSlot()) {
2018 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2019 } else {
2020 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2021 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002022 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002023 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002024 break;
2025 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002026 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002027 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002028 }
Aart Bika19616e2016-02-01 18:57:58 -08002029
Calin Juravleddb7df22014-11-25 20:56:51 +00002030 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002031 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002032 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002033
Calin Juravle91debbc2014-11-26 19:01:09 +00002034 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002035 __ movl(out, Immediate(1));
2036 __ jmp(&done);
2037
2038 __ Bind(&less);
2039 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002040
2041 __ Bind(&done);
2042}
2043
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002044void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002045 LocationSummary* locations =
2046 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002047 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002048}
2049
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002050void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002051 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002052}
2053
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002054void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2055 LocationSummary* locations =
2056 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2057 locations->SetOut(Location::ConstantLocation(constant));
2058}
2059
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002060void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002061 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002062}
2063
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002064void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002065 LocationSummary* locations =
2066 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002067 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002068}
2069
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002070void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002071 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002072}
2073
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002074void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2075 LocationSummary* locations =
2076 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2077 locations->SetOut(Location::ConstantLocation(constant));
2078}
2079
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002080void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002081 // Will be generated at use site.
2082}
2083
2084void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2085 LocationSummary* locations =
2086 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2087 locations->SetOut(Location::ConstantLocation(constant));
2088}
2089
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002090void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2091 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002092 // Will be generated at use site.
2093}
2094
Calin Juravle27df7582015-04-17 19:12:31 +01002095void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2096 memory_barrier->SetLocations(nullptr);
2097}
2098
2099void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002100 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002101}
2102
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002103void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2104 ret->SetLocations(nullptr);
2105}
2106
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002107void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002108 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002109}
2110
2111void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002112 LocationSummary* locations =
2113 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002114 switch (ret->InputAt(0)->GetType()) {
2115 case Primitive::kPrimBoolean:
2116 case Primitive::kPrimByte:
2117 case Primitive::kPrimChar:
2118 case Primitive::kPrimShort:
2119 case Primitive::kPrimInt:
2120 case Primitive::kPrimNot:
2121 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002122 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002123 break;
2124
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002125 case Primitive::kPrimFloat:
2126 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002127 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002128 break;
2129
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002130 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002131 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002132 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002133}
2134
2135void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2136 if (kIsDebugBuild) {
2137 switch (ret->InputAt(0)->GetType()) {
2138 case Primitive::kPrimBoolean:
2139 case Primitive::kPrimByte:
2140 case Primitive::kPrimChar:
2141 case Primitive::kPrimShort:
2142 case Primitive::kPrimInt:
2143 case Primitive::kPrimNot:
2144 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002146 break;
2147
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002148 case Primitive::kPrimFloat:
2149 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002150 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002151 XMM0);
2152 break;
2153
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002154 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002155 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002156 }
2157 }
2158 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002159}
2160
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002161Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2162 switch (type) {
2163 case Primitive::kPrimBoolean:
2164 case Primitive::kPrimByte:
2165 case Primitive::kPrimChar:
2166 case Primitive::kPrimShort:
2167 case Primitive::kPrimInt:
2168 case Primitive::kPrimNot:
2169 case Primitive::kPrimLong:
2170 return Location::RegisterLocation(RAX);
2171
2172 case Primitive::kPrimVoid:
2173 return Location::NoLocation();
2174
2175 case Primitive::kPrimDouble:
2176 case Primitive::kPrimFloat:
2177 return Location::FpuRegisterLocation(XMM0);
2178 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002179
2180 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002181}
2182
2183Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2184 return Location::RegisterLocation(kMethodRegisterArgument);
2185}
2186
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002187Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002188 switch (type) {
2189 case Primitive::kPrimBoolean:
2190 case Primitive::kPrimByte:
2191 case Primitive::kPrimChar:
2192 case Primitive::kPrimShort:
2193 case Primitive::kPrimInt:
2194 case Primitive::kPrimNot: {
2195 uint32_t index = gp_index_++;
2196 stack_index_++;
2197 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002198 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002199 } else {
2200 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2201 }
2202 }
2203
2204 case Primitive::kPrimLong: {
2205 uint32_t index = gp_index_;
2206 stack_index_ += 2;
2207 if (index < calling_convention.GetNumberOfRegisters()) {
2208 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002209 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002210 } else {
2211 gp_index_ += 2;
2212 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2213 }
2214 }
2215
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002216 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002217 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002218 stack_index_++;
2219 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002220 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002221 } else {
2222 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2223 }
2224 }
2225
2226 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002227 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002228 stack_index_ += 2;
2229 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002230 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002231 } else {
2232 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2233 }
2234 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002235
2236 case Primitive::kPrimVoid:
2237 LOG(FATAL) << "Unexpected parameter type " << type;
2238 break;
2239 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002240 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002241}
2242
Calin Juravle175dc732015-08-25 15:42:32 +01002243void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2244 // The trampoline uses the same calling convention as dex calling conventions,
2245 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2246 // the method_idx.
2247 HandleInvoke(invoke);
2248}
2249
2250void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2251 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2252}
2253
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002254void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002255 // Explicit clinit checks triggered by static invokes must have been pruned by
2256 // art::PrepareForRegisterAllocation.
2257 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002258
Mark Mendellfb8d2792015-03-31 22:16:59 -04002259 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002260 if (intrinsic.TryDispatch(invoke)) {
2261 return;
2262 }
2263
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002264 HandleInvoke(invoke);
2265}
2266
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002267static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2268 if (invoke->GetLocations()->Intrinsified()) {
2269 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2270 intrinsic.Dispatch(invoke);
2271 return true;
2272 }
2273 return false;
2274}
2275
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002276void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002277 // Explicit clinit checks triggered by static invokes must have been pruned by
2278 // art::PrepareForRegisterAllocation.
2279 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002280
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002281 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2282 return;
2283 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002284
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002285 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002286 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002287 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002288 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002289}
2290
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002291void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002292 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002293 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002294}
2295
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002296void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002297 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002298 if (intrinsic.TryDispatch(invoke)) {
2299 return;
2300 }
2301
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002302 HandleInvoke(invoke);
2303}
2304
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002305void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002306 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2307 return;
2308 }
2309
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002310 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002311 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002312 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002313}
2314
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002315void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2316 HandleInvoke(invoke);
2317 // Add the hidden argument.
2318 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2319}
2320
2321void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2322 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002323 LocationSummary* locations = invoke->GetLocations();
2324 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2325 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002326 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2327 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002328 Location receiver = locations->InAt(0);
2329 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2330
Roland Levillain0d5a2812015-11-13 10:07:31 +00002331 // Set the hidden argument. This is safe to do this here, as RAX
2332 // won't be modified thereafter, before the `call` instruction.
2333 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002334 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002335
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002336 if (receiver.IsStackSlot()) {
2337 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002338 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002339 __ movl(temp, Address(temp, class_offset));
2340 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002341 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002342 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002343 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002344 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002345 // Instead of simply (possibly) unpoisoning `temp` here, we should
2346 // emit a read barrier for the previous class reference load.
2347 // However this is not required in practice, as this is an
2348 // intermediate/temporary reference and because the current
2349 // concurrent copying collector keeps the from-space memory
2350 // intact/accessible until the end of the marking phase (the
2351 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002352 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002353 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002354 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002355 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002356 __ call(Address(temp,
2357 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002358
2359 DCHECK(!codegen_->IsLeafMethod());
2360 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2361}
2362
Roland Levillain88cb1752014-10-20 16:36:47 +01002363void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2364 LocationSummary* locations =
2365 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2366 switch (neg->GetResultType()) {
2367 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002368 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002369 locations->SetInAt(0, Location::RequiresRegister());
2370 locations->SetOut(Location::SameAsFirstInput());
2371 break;
2372
Roland Levillain88cb1752014-10-20 16:36:47 +01002373 case Primitive::kPrimFloat:
2374 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002375 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002376 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002377 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002378 break;
2379
2380 default:
2381 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2382 }
2383}
2384
2385void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2386 LocationSummary* locations = neg->GetLocations();
2387 Location out = locations->Out();
2388 Location in = locations->InAt(0);
2389 switch (neg->GetResultType()) {
2390 case Primitive::kPrimInt:
2391 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002392 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002393 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002394 break;
2395
2396 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002397 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002398 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002399 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002400 break;
2401
Roland Levillain5368c212014-11-27 15:03:41 +00002402 case Primitive::kPrimFloat: {
2403 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002404 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002405 // Implement float negation with an exclusive or with value
2406 // 0x80000000 (mask for bit 31, representing the sign of a
2407 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002408 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002409 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002410 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002411 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002412
Roland Levillain5368c212014-11-27 15:03:41 +00002413 case Primitive::kPrimDouble: {
2414 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002415 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002416 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002417 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002418 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002419 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002420 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002421 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002422 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002423
2424 default:
2425 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2426 }
2427}
2428
Roland Levillaindff1f282014-11-05 14:15:05 +00002429void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2430 LocationSummary* locations =
2431 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2432 Primitive::Type result_type = conversion->GetResultType();
2433 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002434 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002435
David Brazdilb2bd1c52015-03-25 11:17:37 +00002436 // The Java language does not allow treating boolean as an integral type but
2437 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002438
Roland Levillaindff1f282014-11-05 14:15:05 +00002439 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002440 case Primitive::kPrimByte:
2441 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002442 case Primitive::kPrimBoolean:
2443 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002444 case Primitive::kPrimShort:
2445 case Primitive::kPrimInt:
2446 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002447 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002448 locations->SetInAt(0, Location::Any());
2449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2450 break;
2451
2452 default:
2453 LOG(FATAL) << "Unexpected type conversion from " << input_type
2454 << " to " << result_type;
2455 }
2456 break;
2457
Roland Levillain01a8d712014-11-14 16:27:39 +00002458 case Primitive::kPrimShort:
2459 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002460 case Primitive::kPrimBoolean:
2461 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002462 case Primitive::kPrimByte:
2463 case Primitive::kPrimInt:
2464 case Primitive::kPrimChar:
2465 // Processing a Dex `int-to-short' instruction.
2466 locations->SetInAt(0, Location::Any());
2467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2468 break;
2469
2470 default:
2471 LOG(FATAL) << "Unexpected type conversion from " << input_type
2472 << " to " << result_type;
2473 }
2474 break;
2475
Roland Levillain946e1432014-11-11 17:35:19 +00002476 case Primitive::kPrimInt:
2477 switch (input_type) {
2478 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002479 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002480 locations->SetInAt(0, Location::Any());
2481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2482 break;
2483
2484 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002485 // Processing a Dex `float-to-int' instruction.
2486 locations->SetInAt(0, Location::RequiresFpuRegister());
2487 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002488 break;
2489
Roland Levillain946e1432014-11-11 17:35:19 +00002490 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002491 // Processing a Dex `double-to-int' instruction.
2492 locations->SetInAt(0, Location::RequiresFpuRegister());
2493 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002494 break;
2495
2496 default:
2497 LOG(FATAL) << "Unexpected type conversion from " << input_type
2498 << " to " << result_type;
2499 }
2500 break;
2501
Roland Levillaindff1f282014-11-05 14:15:05 +00002502 case Primitive::kPrimLong:
2503 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002504 case Primitive::kPrimBoolean:
2505 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 case Primitive::kPrimByte:
2507 case Primitive::kPrimShort:
2508 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002509 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002510 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002511 // TODO: We would benefit from a (to-be-implemented)
2512 // Location::RegisterOrStackSlot requirement for this input.
2513 locations->SetInAt(0, Location::RequiresRegister());
2514 locations->SetOut(Location::RequiresRegister());
2515 break;
2516
2517 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002518 // Processing a Dex `float-to-long' instruction.
2519 locations->SetInAt(0, Location::RequiresFpuRegister());
2520 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002521 break;
2522
Roland Levillaindff1f282014-11-05 14:15:05 +00002523 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002524 // Processing a Dex `double-to-long' instruction.
2525 locations->SetInAt(0, Location::RequiresFpuRegister());
2526 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 break;
2528
2529 default:
2530 LOG(FATAL) << "Unexpected type conversion from " << input_type
2531 << " to " << result_type;
2532 }
2533 break;
2534
Roland Levillain981e4542014-11-14 11:47:14 +00002535 case Primitive::kPrimChar:
2536 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002537 case Primitive::kPrimBoolean:
2538 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002539 case Primitive::kPrimByte:
2540 case Primitive::kPrimShort:
2541 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002542 // Processing a Dex `int-to-char' instruction.
2543 locations->SetInAt(0, Location::Any());
2544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2545 break;
2546
2547 default:
2548 LOG(FATAL) << "Unexpected type conversion from " << input_type
2549 << " to " << result_type;
2550 }
2551 break;
2552
Roland Levillaindff1f282014-11-05 14:15:05 +00002553 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002554 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002555 case Primitive::kPrimBoolean:
2556 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002557 case Primitive::kPrimByte:
2558 case Primitive::kPrimShort:
2559 case Primitive::kPrimInt:
2560 case Primitive::kPrimChar:
2561 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002562 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002563 locations->SetOut(Location::RequiresFpuRegister());
2564 break;
2565
2566 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002567 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002568 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002569 locations->SetOut(Location::RequiresFpuRegister());
2570 break;
2571
Roland Levillaincff13742014-11-17 14:32:17 +00002572 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002573 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002574 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002575 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002576 break;
2577
2578 default:
2579 LOG(FATAL) << "Unexpected type conversion from " << input_type
2580 << " to " << result_type;
2581 };
2582 break;
2583
Roland Levillaindff1f282014-11-05 14:15:05 +00002584 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002585 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002586 case Primitive::kPrimBoolean:
2587 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002588 case Primitive::kPrimByte:
2589 case Primitive::kPrimShort:
2590 case Primitive::kPrimInt:
2591 case Primitive::kPrimChar:
2592 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002593 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002594 locations->SetOut(Location::RequiresFpuRegister());
2595 break;
2596
2597 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002598 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002599 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002600 locations->SetOut(Location::RequiresFpuRegister());
2601 break;
2602
Roland Levillaincff13742014-11-17 14:32:17 +00002603 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002604 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002605 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002606 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002607 break;
2608
2609 default:
2610 LOG(FATAL) << "Unexpected type conversion from " << input_type
2611 << " to " << result_type;
2612 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002613 break;
2614
2615 default:
2616 LOG(FATAL) << "Unexpected type conversion from " << input_type
2617 << " to " << result_type;
2618 }
2619}
2620
2621void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2622 LocationSummary* locations = conversion->GetLocations();
2623 Location out = locations->Out();
2624 Location in = locations->InAt(0);
2625 Primitive::Type result_type = conversion->GetResultType();
2626 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002627 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002628 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002629 case Primitive::kPrimByte:
2630 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002631 case Primitive::kPrimBoolean:
2632 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002633 case Primitive::kPrimShort:
2634 case Primitive::kPrimInt:
2635 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002636 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002637 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002638 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002639 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002641 Address(CpuRegister(RSP), in.GetStackIndex()));
2642 } else {
2643 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002645 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2646 }
2647 break;
2648
2649 default:
2650 LOG(FATAL) << "Unexpected type conversion from " << input_type
2651 << " to " << result_type;
2652 }
2653 break;
2654
Roland Levillain01a8d712014-11-14 16:27:39 +00002655 case Primitive::kPrimShort:
2656 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002657 case Primitive::kPrimBoolean:
2658 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002659 case Primitive::kPrimByte:
2660 case Primitive::kPrimInt:
2661 case Primitive::kPrimChar:
2662 // Processing a Dex `int-to-short' instruction.
2663 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002665 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002666 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002667 Address(CpuRegister(RSP), in.GetStackIndex()));
2668 } else {
2669 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002671 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2672 }
2673 break;
2674
2675 default:
2676 LOG(FATAL) << "Unexpected type conversion from " << input_type
2677 << " to " << result_type;
2678 }
2679 break;
2680
Roland Levillain946e1432014-11-11 17:35:19 +00002681 case Primitive::kPrimInt:
2682 switch (input_type) {
2683 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002684 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002685 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002687 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002689 Address(CpuRegister(RSP), in.GetStackIndex()));
2690 } else {
2691 DCHECK(in.IsConstant());
2692 DCHECK(in.GetConstant()->IsLongConstant());
2693 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002694 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002695 }
2696 break;
2697
Roland Levillain3f8f9362014-12-02 17:45:01 +00002698 case Primitive::kPrimFloat: {
2699 // Processing a Dex `float-to-int' instruction.
2700 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2701 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002702 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002703
2704 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002705 // if input >= (float)INT_MAX goto done
2706 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002707 __ j(kAboveEqual, &done);
2708 // if input == NaN goto nan
2709 __ j(kUnordered, &nan);
2710 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002711 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002712 __ jmp(&done);
2713 __ Bind(&nan);
2714 // output = 0
2715 __ xorl(output, output);
2716 __ Bind(&done);
2717 break;
2718 }
2719
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002720 case Primitive::kPrimDouble: {
2721 // Processing a Dex `double-to-int' instruction.
2722 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2723 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002724 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002725
2726 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002727 // if input >= (double)INT_MAX goto done
2728 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002729 __ j(kAboveEqual, &done);
2730 // if input == NaN goto nan
2731 __ j(kUnordered, &nan);
2732 // output = double-to-int-truncate(input)
2733 __ cvttsd2si(output, input);
2734 __ jmp(&done);
2735 __ Bind(&nan);
2736 // output = 0
2737 __ xorl(output, output);
2738 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002739 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002740 }
Roland Levillain946e1432014-11-11 17:35:19 +00002741
2742 default:
2743 LOG(FATAL) << "Unexpected type conversion from " << input_type
2744 << " to " << result_type;
2745 }
2746 break;
2747
Roland Levillaindff1f282014-11-05 14:15:05 +00002748 case Primitive::kPrimLong:
2749 switch (input_type) {
2750 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002751 case Primitive::kPrimBoolean:
2752 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002753 case Primitive::kPrimByte:
2754 case Primitive::kPrimShort:
2755 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002756 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002757 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002758 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002759 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002760 break;
2761
Roland Levillain624279f2014-12-04 11:54:28 +00002762 case Primitive::kPrimFloat: {
2763 // Processing a Dex `float-to-long' instruction.
2764 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2765 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002766 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002767
Mark Mendell92e83bf2015-05-07 11:25:03 -04002768 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002769 // if input >= (float)LONG_MAX goto done
2770 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002771 __ j(kAboveEqual, &done);
2772 // if input == NaN goto nan
2773 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002774 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002775 __ cvttss2si(output, input, true);
2776 __ jmp(&done);
2777 __ Bind(&nan);
2778 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002779 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002780 __ Bind(&done);
2781 break;
2782 }
2783
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002784 case Primitive::kPrimDouble: {
2785 // Processing a Dex `double-to-long' instruction.
2786 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2787 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002788 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002789
Mark Mendell92e83bf2015-05-07 11:25:03 -04002790 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002791 // if input >= (double)LONG_MAX goto done
2792 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002793 __ j(kAboveEqual, &done);
2794 // if input == NaN goto nan
2795 __ j(kUnordered, &nan);
2796 // output = double-to-long-truncate(input)
2797 __ cvttsd2si(output, input, true);
2798 __ jmp(&done);
2799 __ Bind(&nan);
2800 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002801 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002802 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002803 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002804 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002805
2806 default:
2807 LOG(FATAL) << "Unexpected type conversion from " << input_type
2808 << " to " << result_type;
2809 }
2810 break;
2811
Roland Levillain981e4542014-11-14 11:47:14 +00002812 case Primitive::kPrimChar:
2813 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002814 case Primitive::kPrimBoolean:
2815 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002816 case Primitive::kPrimByte:
2817 case Primitive::kPrimShort:
2818 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002819 // Processing a Dex `int-to-char' instruction.
2820 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002821 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002822 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002823 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002824 Address(CpuRegister(RSP), in.GetStackIndex()));
2825 } else {
2826 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002827 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002828 Immediate(static_cast<uint16_t>(
2829 in.GetConstant()->AsIntConstant()->GetValue())));
Roland Levillain981e4542014-11-14 11:47:14 +00002830 }
2831 break;
2832
2833 default:
2834 LOG(FATAL) << "Unexpected type conversion from " << input_type
2835 << " to " << result_type;
2836 }
2837 break;
2838
Roland Levillaindff1f282014-11-05 14:15:05 +00002839 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002840 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002841 case Primitive::kPrimBoolean:
2842 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002843 case Primitive::kPrimByte:
2844 case Primitive::kPrimShort:
2845 case Primitive::kPrimInt:
2846 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002847 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002848 if (in.IsRegister()) {
2849 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2850 } else if (in.IsConstant()) {
2851 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2852 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002853 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002854 } else {
2855 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2856 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2857 }
Roland Levillaincff13742014-11-17 14:32:17 +00002858 break;
2859
2860 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002861 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002862 if (in.IsRegister()) {
2863 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2864 } else if (in.IsConstant()) {
2865 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2866 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002867 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002868 } else {
2869 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2870 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2871 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002872 break;
2873
Roland Levillaincff13742014-11-17 14:32:17 +00002874 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002875 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002876 if (in.IsFpuRegister()) {
2877 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2878 } else if (in.IsConstant()) {
2879 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2880 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002881 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002882 } else {
2883 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2884 Address(CpuRegister(RSP), in.GetStackIndex()));
2885 }
Roland Levillaincff13742014-11-17 14:32:17 +00002886 break;
2887
2888 default:
2889 LOG(FATAL) << "Unexpected type conversion from " << input_type
2890 << " to " << result_type;
2891 };
2892 break;
2893
Roland Levillaindff1f282014-11-05 14:15:05 +00002894 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002895 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002896 case Primitive::kPrimBoolean:
2897 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002898 case Primitive::kPrimByte:
2899 case Primitive::kPrimShort:
2900 case Primitive::kPrimInt:
2901 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002902 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002903 if (in.IsRegister()) {
2904 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2905 } else if (in.IsConstant()) {
2906 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2907 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002908 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002909 } else {
2910 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2911 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2912 }
Roland Levillaincff13742014-11-17 14:32:17 +00002913 break;
2914
2915 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002916 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002917 if (in.IsRegister()) {
2918 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2919 } else if (in.IsConstant()) {
2920 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2921 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002922 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002923 } else {
2924 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2925 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2926 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002927 break;
2928
Roland Levillaincff13742014-11-17 14:32:17 +00002929 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002930 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002931 if (in.IsFpuRegister()) {
2932 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2933 } else if (in.IsConstant()) {
2934 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2935 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002936 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002937 } else {
2938 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2939 Address(CpuRegister(RSP), in.GetStackIndex()));
2940 }
Roland Levillaincff13742014-11-17 14:32:17 +00002941 break;
2942
2943 default:
2944 LOG(FATAL) << "Unexpected type conversion from " << input_type
2945 << " to " << result_type;
2946 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002947 break;
2948
2949 default:
2950 LOG(FATAL) << "Unexpected type conversion from " << input_type
2951 << " to " << result_type;
2952 }
2953}
2954
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002955void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002956 LocationSummary* locations =
2957 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002958 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002959 case Primitive::kPrimInt: {
2960 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002961 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002963 break;
2964 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002965
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002966 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002967 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002968 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002969 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002970 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002971 break;
2972 }
2973
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 case Primitive::kPrimDouble:
2975 case Primitive::kPrimFloat: {
2976 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002977 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002978 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002979 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002980 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002981
2982 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002983 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002984 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002985}
2986
2987void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2988 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002989 Location first = locations->InAt(0);
2990 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002991 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002992
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002993 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002994 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002995 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002996 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2997 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002998 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2999 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003000 } else {
3001 __ leal(out.AsRegister<CpuRegister>(), Address(
3002 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3003 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003005 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3006 __ addl(out.AsRegister<CpuRegister>(),
3007 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3008 } else {
3009 __ leal(out.AsRegister<CpuRegister>(), Address(
3010 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3011 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003012 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003013 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003014 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003015 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003016 break;
3017 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003018
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003019 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003020 if (second.IsRegister()) {
3021 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3022 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003023 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3024 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003025 } else {
3026 __ leaq(out.AsRegister<CpuRegister>(), Address(
3027 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3028 }
3029 } else {
3030 DCHECK(second.IsConstant());
3031 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3032 int32_t int32_value = Low32Bits(value);
3033 DCHECK_EQ(int32_value, value);
3034 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3035 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3036 } else {
3037 __ leaq(out.AsRegister<CpuRegister>(), Address(
3038 first.AsRegister<CpuRegister>(), int32_value));
3039 }
3040 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003041 break;
3042 }
3043
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003044 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003045 if (second.IsFpuRegister()) {
3046 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3047 } else if (second.IsConstant()) {
3048 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003049 codegen_->LiteralFloatAddress(
3050 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003051 } else {
3052 DCHECK(second.IsStackSlot());
3053 __ addss(first.AsFpuRegister<XmmRegister>(),
3054 Address(CpuRegister(RSP), second.GetStackIndex()));
3055 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003056 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003057 }
3058
3059 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003060 if (second.IsFpuRegister()) {
3061 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3062 } else if (second.IsConstant()) {
3063 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003064 codegen_->LiteralDoubleAddress(
3065 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003066 } else {
3067 DCHECK(second.IsDoubleStackSlot());
3068 __ addsd(first.AsFpuRegister<XmmRegister>(),
3069 Address(CpuRegister(RSP), second.GetStackIndex()));
3070 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003071 break;
3072 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003073
3074 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003075 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003076 }
3077}
3078
3079void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003080 LocationSummary* locations =
3081 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003082 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003083 case Primitive::kPrimInt: {
3084 locations->SetInAt(0, Location::RequiresRegister());
3085 locations->SetInAt(1, Location::Any());
3086 locations->SetOut(Location::SameAsFirstInput());
3087 break;
3088 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003089 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003090 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003091 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003092 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003093 break;
3094 }
Calin Juravle11351682014-10-23 15:38:15 +01003095 case Primitive::kPrimFloat:
3096 case Primitive::kPrimDouble: {
3097 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003098 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003099 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003100 break;
Calin Juravle11351682014-10-23 15:38:15 +01003101 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003102 default:
Calin Juravle11351682014-10-23 15:38:15 +01003103 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003104 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003105}
3106
3107void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3108 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003109 Location first = locations->InAt(0);
3110 Location second = locations->InAt(1);
3111 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003112 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003113 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003114 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003115 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003116 } else if (second.IsConstant()) {
3117 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003118 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003119 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003120 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003121 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003122 break;
3123 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003124 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003125 if (second.IsConstant()) {
3126 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3127 DCHECK(IsInt<32>(value));
3128 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3129 } else {
3130 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3131 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003132 break;
3133 }
3134
Calin Juravle11351682014-10-23 15:38:15 +01003135 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003136 if (second.IsFpuRegister()) {
3137 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3138 } else if (second.IsConstant()) {
3139 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003140 codegen_->LiteralFloatAddress(
3141 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003142 } else {
3143 DCHECK(second.IsStackSlot());
3144 __ subss(first.AsFpuRegister<XmmRegister>(),
3145 Address(CpuRegister(RSP), second.GetStackIndex()));
3146 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003147 break;
Calin Juravle11351682014-10-23 15:38:15 +01003148 }
3149
3150 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003151 if (second.IsFpuRegister()) {
3152 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3153 } else if (second.IsConstant()) {
3154 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003155 codegen_->LiteralDoubleAddress(
3156 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003157 } else {
3158 DCHECK(second.IsDoubleStackSlot());
3159 __ subsd(first.AsFpuRegister<XmmRegister>(),
3160 Address(CpuRegister(RSP), second.GetStackIndex()));
3161 }
Calin Juravle11351682014-10-23 15:38:15 +01003162 break;
3163 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003164
3165 default:
Calin Juravle11351682014-10-23 15:38:15 +01003166 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003167 }
3168}
3169
Calin Juravle34bacdf2014-10-07 20:23:36 +01003170void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3171 LocationSummary* locations =
3172 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3173 switch (mul->GetResultType()) {
3174 case Primitive::kPrimInt: {
3175 locations->SetInAt(0, Location::RequiresRegister());
3176 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003177 if (mul->InputAt(1)->IsIntConstant()) {
3178 // Can use 3 operand multiply.
3179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3180 } else {
3181 locations->SetOut(Location::SameAsFirstInput());
3182 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003183 break;
3184 }
3185 case Primitive::kPrimLong: {
3186 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003187 locations->SetInAt(1, Location::Any());
3188 if (mul->InputAt(1)->IsLongConstant() &&
3189 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003190 // Can use 3 operand multiply.
3191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3192 } else {
3193 locations->SetOut(Location::SameAsFirstInput());
3194 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195 break;
3196 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003197 case Primitive::kPrimFloat:
3198 case Primitive::kPrimDouble: {
3199 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003200 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003201 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003203 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204
3205 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003206 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207 }
3208}
3209
3210void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3211 LocationSummary* locations = mul->GetLocations();
3212 Location first = locations->InAt(0);
3213 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003214 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003216 case Primitive::kPrimInt:
3217 // The constant may have ended up in a register, so test explicitly to avoid
3218 // problems where the output may not be the same as the first operand.
3219 if (mul->InputAt(1)->IsIntConstant()) {
3220 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3221 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3222 } else if (second.IsRegister()) {
3223 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003224 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003226 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003227 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003228 __ imull(first.AsRegister<CpuRegister>(),
3229 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003230 }
3231 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003232 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003233 // The constant may have ended up in a register, so test explicitly to avoid
3234 // problems where the output may not be the same as the first operand.
3235 if (mul->InputAt(1)->IsLongConstant()) {
3236 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3237 if (IsInt<32>(value)) {
3238 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3239 Immediate(static_cast<int32_t>(value)));
3240 } else {
3241 // Have to use the constant area.
3242 DCHECK(first.Equals(out));
3243 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3244 }
3245 } else if (second.IsRegister()) {
3246 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003247 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003248 } else {
3249 DCHECK(second.IsDoubleStackSlot());
3250 DCHECK(first.Equals(out));
3251 __ imulq(first.AsRegister<CpuRegister>(),
3252 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003253 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003254 break;
3255 }
3256
Calin Juravleb5bfa962014-10-21 18:02:24 +01003257 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003258 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003259 if (second.IsFpuRegister()) {
3260 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3261 } else if (second.IsConstant()) {
3262 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003263 codegen_->LiteralFloatAddress(
3264 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003265 } else {
3266 DCHECK(second.IsStackSlot());
3267 __ mulss(first.AsFpuRegister<XmmRegister>(),
3268 Address(CpuRegister(RSP), second.GetStackIndex()));
3269 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003270 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003271 }
3272
3273 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003274 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003275 if (second.IsFpuRegister()) {
3276 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3277 } else if (second.IsConstant()) {
3278 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003279 codegen_->LiteralDoubleAddress(
3280 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003281 } else {
3282 DCHECK(second.IsDoubleStackSlot());
3283 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3284 Address(CpuRegister(RSP), second.GetStackIndex()));
3285 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003286 break;
3287 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003288
3289 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003290 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003291 }
3292}
3293
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003294void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3295 uint32_t stack_adjustment, bool is_float) {
3296 if (source.IsStackSlot()) {
3297 DCHECK(is_float);
3298 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3299 } else if (source.IsDoubleStackSlot()) {
3300 DCHECK(!is_float);
3301 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3302 } else {
3303 // Write the value to the temporary location on the stack and load to FP stack.
3304 if (is_float) {
3305 Location stack_temp = Location::StackSlot(temp_offset);
3306 codegen_->Move(stack_temp, source);
3307 __ flds(Address(CpuRegister(RSP), temp_offset));
3308 } else {
3309 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3310 codegen_->Move(stack_temp, source);
3311 __ fldl(Address(CpuRegister(RSP), temp_offset));
3312 }
3313 }
3314}
3315
3316void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3317 Primitive::Type type = rem->GetResultType();
3318 bool is_float = type == Primitive::kPrimFloat;
3319 size_t elem_size = Primitive::ComponentSize(type);
3320 LocationSummary* locations = rem->GetLocations();
3321 Location first = locations->InAt(0);
3322 Location second = locations->InAt(1);
3323 Location out = locations->Out();
3324
3325 // Create stack space for 2 elements.
3326 // TODO: enhance register allocator to ask for stack temporaries.
3327 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3328
3329 // Load the values to the FP stack in reverse order, using temporaries if needed.
3330 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3331 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3332
3333 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003334 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003335 __ Bind(&retry);
3336 __ fprem();
3337
3338 // Move FP status to AX.
3339 __ fstsw();
3340
3341 // And see if the argument reduction is complete. This is signaled by the
3342 // C2 FPU flag bit set to 0.
3343 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3344 __ j(kNotEqual, &retry);
3345
3346 // We have settled on the final value. Retrieve it into an XMM register.
3347 // Store FP top of stack to real stack.
3348 if (is_float) {
3349 __ fsts(Address(CpuRegister(RSP), 0));
3350 } else {
3351 __ fstl(Address(CpuRegister(RSP), 0));
3352 }
3353
3354 // Pop the 2 items from the FP stack.
3355 __ fucompp();
3356
3357 // Load the value from the stack into an XMM register.
3358 DCHECK(out.IsFpuRegister()) << out;
3359 if (is_float) {
3360 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3361 } else {
3362 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3363 }
3364
3365 // And remove the temporary stack space we allocated.
3366 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3367}
3368
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003369void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3370 DCHECK(instruction->IsDiv() || instruction->IsRem());
3371
3372 LocationSummary* locations = instruction->GetLocations();
3373 Location second = locations->InAt(1);
3374 DCHECK(second.IsConstant());
3375
3376 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3377 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003378 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003379
3380 DCHECK(imm == 1 || imm == -1);
3381
3382 switch (instruction->GetResultType()) {
3383 case Primitive::kPrimInt: {
3384 if (instruction->IsRem()) {
3385 __ xorl(output_register, output_register);
3386 } else {
3387 __ movl(output_register, input_register);
3388 if (imm == -1) {
3389 __ negl(output_register);
3390 }
3391 }
3392 break;
3393 }
3394
3395 case Primitive::kPrimLong: {
3396 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003397 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003398 } else {
3399 __ movq(output_register, input_register);
3400 if (imm == -1) {
3401 __ negq(output_register);
3402 }
3403 }
3404 break;
3405 }
3406
3407 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003408 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003409 }
3410}
3411
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003412void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003413 LocationSummary* locations = instruction->GetLocations();
3414 Location second = locations->InAt(1);
3415
3416 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3417 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3418
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003419 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003420 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3421 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003422
3423 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3424
3425 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003426 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427 __ testl(numerator, numerator);
3428 __ cmov(kGreaterEqual, tmp, numerator);
3429 int shift = CTZ(imm);
3430 __ sarl(tmp, Immediate(shift));
3431
3432 if (imm < 0) {
3433 __ negl(tmp);
3434 }
3435
3436 __ movl(output_register, tmp);
3437 } else {
3438 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3439 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3440
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003441 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003442 __ addq(rdx, numerator);
3443 __ testq(numerator, numerator);
3444 __ cmov(kGreaterEqual, rdx, numerator);
3445 int shift = CTZ(imm);
3446 __ sarq(rdx, Immediate(shift));
3447
3448 if (imm < 0) {
3449 __ negq(rdx);
3450 }
3451
3452 __ movq(output_register, rdx);
3453 }
3454}
3455
3456void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3457 DCHECK(instruction->IsDiv() || instruction->IsRem());
3458
3459 LocationSummary* locations = instruction->GetLocations();
3460 Location second = locations->InAt(1);
3461
3462 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3463 : locations->GetTemp(0).AsRegister<CpuRegister>();
3464 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3465 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3466 : locations->Out().AsRegister<CpuRegister>();
3467 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3468
3469 DCHECK_EQ(RAX, eax.AsRegister());
3470 DCHECK_EQ(RDX, edx.AsRegister());
3471 if (instruction->IsDiv()) {
3472 DCHECK_EQ(RAX, out.AsRegister());
3473 } else {
3474 DCHECK_EQ(RDX, out.AsRegister());
3475 }
3476
3477 int64_t magic;
3478 int shift;
3479
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003480 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003481 if (instruction->GetResultType() == Primitive::kPrimInt) {
3482 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3483
3484 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3485
3486 __ movl(numerator, eax);
3487
Mark Mendell0c9497d2015-08-21 09:30:05 -04003488 NearLabel no_div;
3489 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 __ testl(eax, eax);
3491 __ j(kNotEqual, &no_div);
3492
3493 __ xorl(out, out);
3494 __ jmp(&end);
3495
3496 __ Bind(&no_div);
3497
3498 __ movl(eax, Immediate(magic));
3499 __ imull(numerator);
3500
3501 if (imm > 0 && magic < 0) {
3502 __ addl(edx, numerator);
3503 } else if (imm < 0 && magic > 0) {
3504 __ subl(edx, numerator);
3505 }
3506
3507 if (shift != 0) {
3508 __ sarl(edx, Immediate(shift));
3509 }
3510
3511 __ movl(eax, edx);
3512 __ shrl(edx, Immediate(31));
3513 __ addl(edx, eax);
3514
3515 if (instruction->IsRem()) {
3516 __ movl(eax, numerator);
3517 __ imull(edx, Immediate(imm));
3518 __ subl(eax, edx);
3519 __ movl(edx, eax);
3520 } else {
3521 __ movl(eax, edx);
3522 }
3523 __ Bind(&end);
3524 } else {
3525 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3526
3527 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3528
3529 CpuRegister rax = eax;
3530 CpuRegister rdx = edx;
3531
3532 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3533
3534 // Save the numerator.
3535 __ movq(numerator, rax);
3536
3537 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003538 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003539
3540 // RDX:RAX = magic * numerator
3541 __ imulq(numerator);
3542
3543 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003544 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003545 __ addq(rdx, numerator);
3546 } else if (imm < 0 && magic > 0) {
3547 // RDX -= numerator
3548 __ subq(rdx, numerator);
3549 }
3550
3551 // Shift if needed.
3552 if (shift != 0) {
3553 __ sarq(rdx, Immediate(shift));
3554 }
3555
3556 // RDX += 1 if RDX < 0
3557 __ movq(rax, rdx);
3558 __ shrq(rdx, Immediate(63));
3559 __ addq(rdx, rax);
3560
3561 if (instruction->IsRem()) {
3562 __ movq(rax, numerator);
3563
3564 if (IsInt<32>(imm)) {
3565 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3566 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003567 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003568 }
3569
3570 __ subq(rax, rdx);
3571 __ movq(rdx, rax);
3572 } else {
3573 __ movq(rax, rdx);
3574 }
3575 }
3576}
3577
Calin Juravlebacfec32014-11-14 15:54:36 +00003578void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3579 DCHECK(instruction->IsDiv() || instruction->IsRem());
3580 Primitive::Type type = instruction->GetResultType();
3581 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3582
3583 bool is_div = instruction->IsDiv();
3584 LocationSummary* locations = instruction->GetLocations();
3585
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003586 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3587 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003588
Roland Levillain271ab9c2014-11-27 15:23:57 +00003589 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003591
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003592 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003593 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003594
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003595 if (imm == 0) {
3596 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3597 } else if (imm == 1 || imm == -1) {
3598 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003599 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003600 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003601 } else {
3602 DCHECK(imm <= -2 || imm >= 2);
3603 GenerateDivRemWithAnyConstant(instruction);
3604 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003605 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003606 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003607 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3608 out.AsRegister(), type, is_div);
3609 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003610
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003611 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3612 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3613 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3614 // so it's safe to just use negl instead of more complex comparisons.
3615 if (type == Primitive::kPrimInt) {
3616 __ cmpl(second_reg, Immediate(-1));
3617 __ j(kEqual, slow_path->GetEntryLabel());
3618 // edx:eax <- sign-extended of eax
3619 __ cdq();
3620 // eax = quotient, edx = remainder
3621 __ idivl(second_reg);
3622 } else {
3623 __ cmpq(second_reg, Immediate(-1));
3624 __ j(kEqual, slow_path->GetEntryLabel());
3625 // rdx:rax <- sign-extended of rax
3626 __ cqo();
3627 // rax = quotient, rdx = remainder
3628 __ idivq(second_reg);
3629 }
3630 __ Bind(slow_path->GetExitLabel());
3631 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003632}
3633
Calin Juravle7c4954d2014-10-28 16:57:40 +00003634void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3635 LocationSummary* locations =
3636 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3637 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003638 case Primitive::kPrimInt:
3639 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003640 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003641 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003642 locations->SetOut(Location::SameAsFirstInput());
3643 // Intel uses edx:eax as the dividend.
3644 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003645 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3646 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3647 // output and request another temp.
3648 if (div->InputAt(1)->IsConstant()) {
3649 locations->AddTemp(Location::RequiresRegister());
3650 }
Calin Juravled0d48522014-11-04 16:40:20 +00003651 break;
3652 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003653
Calin Juravle7c4954d2014-10-28 16:57:40 +00003654 case Primitive::kPrimFloat:
3655 case Primitive::kPrimDouble: {
3656 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003657 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003658 locations->SetOut(Location::SameAsFirstInput());
3659 break;
3660 }
3661
3662 default:
3663 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3664 }
3665}
3666
3667void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3668 LocationSummary* locations = div->GetLocations();
3669 Location first = locations->InAt(0);
3670 Location second = locations->InAt(1);
3671 DCHECK(first.Equals(locations->Out()));
3672
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003673 Primitive::Type type = div->GetResultType();
3674 switch (type) {
3675 case Primitive::kPrimInt:
3676 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003677 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003678 break;
3679 }
3680
Calin Juravle7c4954d2014-10-28 16:57:40 +00003681 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003682 if (second.IsFpuRegister()) {
3683 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3684 } else if (second.IsConstant()) {
3685 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003686 codegen_->LiteralFloatAddress(
3687 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003688 } else {
3689 DCHECK(second.IsStackSlot());
3690 __ divss(first.AsFpuRegister<XmmRegister>(),
3691 Address(CpuRegister(RSP), second.GetStackIndex()));
3692 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003693 break;
3694 }
3695
3696 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003697 if (second.IsFpuRegister()) {
3698 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3699 } else if (second.IsConstant()) {
3700 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003701 codegen_->LiteralDoubleAddress(
3702 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003703 } else {
3704 DCHECK(second.IsDoubleStackSlot());
3705 __ divsd(first.AsFpuRegister<XmmRegister>(),
3706 Address(CpuRegister(RSP), second.GetStackIndex()));
3707 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003708 break;
3709 }
3710
3711 default:
3712 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3713 }
3714}
3715
Calin Juravlebacfec32014-11-14 15:54:36 +00003716void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003717 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003718 LocationSummary* locations =
3719 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003720
3721 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003722 case Primitive::kPrimInt:
3723 case Primitive::kPrimLong: {
3724 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003725 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003726 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3727 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003728 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3729 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3730 // output and request another temp.
3731 if (rem->InputAt(1)->IsConstant()) {
3732 locations->AddTemp(Location::RequiresRegister());
3733 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003734 break;
3735 }
3736
3737 case Primitive::kPrimFloat:
3738 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003739 locations->SetInAt(0, Location::Any());
3740 locations->SetInAt(1, Location::Any());
3741 locations->SetOut(Location::RequiresFpuRegister());
3742 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003743 break;
3744 }
3745
3746 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003747 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003748 }
3749}
3750
3751void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3752 Primitive::Type type = rem->GetResultType();
3753 switch (type) {
3754 case Primitive::kPrimInt:
3755 case Primitive::kPrimLong: {
3756 GenerateDivRemIntegral(rem);
3757 break;
3758 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003759 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003760 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003761 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003762 break;
3763 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003764 default:
3765 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3766 }
3767}
3768
Calin Juravled0d48522014-11-04 16:40:20 +00003769void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003770 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3771 ? LocationSummary::kCallOnSlowPath
3772 : LocationSummary::kNoCall;
3773 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003774 locations->SetInAt(0, Location::Any());
3775 if (instruction->HasUses()) {
3776 locations->SetOut(Location::SameAsFirstInput());
3777 }
3778}
3779
3780void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003781 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003782 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3783 codegen_->AddSlowPath(slow_path);
3784
3785 LocationSummary* locations = instruction->GetLocations();
3786 Location value = locations->InAt(0);
3787
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003788 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003789 case Primitive::kPrimByte:
3790 case Primitive::kPrimChar:
3791 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003792 case Primitive::kPrimInt: {
3793 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003794 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003795 __ j(kEqual, slow_path->GetEntryLabel());
3796 } else if (value.IsStackSlot()) {
3797 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3798 __ j(kEqual, slow_path->GetEntryLabel());
3799 } else {
3800 DCHECK(value.IsConstant()) << value;
3801 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3802 __ jmp(slow_path->GetEntryLabel());
3803 }
3804 }
3805 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003806 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003807 case Primitive::kPrimLong: {
3808 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003809 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003810 __ j(kEqual, slow_path->GetEntryLabel());
3811 } else if (value.IsDoubleStackSlot()) {
3812 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3813 __ j(kEqual, slow_path->GetEntryLabel());
3814 } else {
3815 DCHECK(value.IsConstant()) << value;
3816 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3817 __ jmp(slow_path->GetEntryLabel());
3818 }
3819 }
3820 break;
3821 }
3822 default:
3823 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003824 }
Calin Juravled0d48522014-11-04 16:40:20 +00003825}
3826
Calin Juravle9aec02f2014-11-18 23:06:35 +00003827void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3828 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3829
3830 LocationSummary* locations =
3831 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3832
3833 switch (op->GetResultType()) {
3834 case Primitive::kPrimInt:
3835 case Primitive::kPrimLong: {
3836 locations->SetInAt(0, Location::RequiresRegister());
3837 // The shift count needs to be in CL.
3838 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3839 locations->SetOut(Location::SameAsFirstInput());
3840 break;
3841 }
3842 default:
3843 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3844 }
3845}
3846
3847void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3848 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3849
3850 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003851 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003852 Location second = locations->InAt(1);
3853
3854 switch (op->GetResultType()) {
3855 case Primitive::kPrimInt: {
3856 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003857 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003858 if (op->IsShl()) {
3859 __ shll(first_reg, second_reg);
3860 } else if (op->IsShr()) {
3861 __ sarl(first_reg, second_reg);
3862 } else {
3863 __ shrl(first_reg, second_reg);
3864 }
3865 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003866 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003867 if (op->IsShl()) {
3868 __ shll(first_reg, imm);
3869 } else if (op->IsShr()) {
3870 __ sarl(first_reg, imm);
3871 } else {
3872 __ shrl(first_reg, imm);
3873 }
3874 }
3875 break;
3876 }
3877 case Primitive::kPrimLong: {
3878 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003879 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003880 if (op->IsShl()) {
3881 __ shlq(first_reg, second_reg);
3882 } else if (op->IsShr()) {
3883 __ sarq(first_reg, second_reg);
3884 } else {
3885 __ shrq(first_reg, second_reg);
3886 }
3887 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003888 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003889 if (op->IsShl()) {
3890 __ shlq(first_reg, imm);
3891 } else if (op->IsShr()) {
3892 __ sarq(first_reg, imm);
3893 } else {
3894 __ shrq(first_reg, imm);
3895 }
3896 }
3897 break;
3898 }
3899 default:
3900 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003901 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003902 }
3903}
3904
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003905void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3906 LocationSummary* locations =
3907 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3908
3909 switch (ror->GetResultType()) {
3910 case Primitive::kPrimInt:
3911 case Primitive::kPrimLong: {
3912 locations->SetInAt(0, Location::RequiresRegister());
3913 // The shift count needs to be in CL (unless it is a constant).
3914 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3915 locations->SetOut(Location::SameAsFirstInput());
3916 break;
3917 }
3918 default:
3919 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3920 UNREACHABLE();
3921 }
3922}
3923
3924void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3925 LocationSummary* locations = ror->GetLocations();
3926 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3927 Location second = locations->InAt(1);
3928
3929 switch (ror->GetResultType()) {
3930 case Primitive::kPrimInt:
3931 if (second.IsRegister()) {
3932 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3933 __ rorl(first_reg, second_reg);
3934 } else {
3935 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3936 __ rorl(first_reg, imm);
3937 }
3938 break;
3939 case Primitive::kPrimLong:
3940 if (second.IsRegister()) {
3941 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3942 __ rorq(first_reg, second_reg);
3943 } else {
3944 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
3945 __ rorq(first_reg, imm);
3946 }
3947 break;
3948 default:
3949 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3950 UNREACHABLE();
3951 }
3952}
3953
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3955 HandleShift(shl);
3956}
3957
3958void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3959 HandleShift(shl);
3960}
3961
3962void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3963 HandleShift(shr);
3964}
3965
3966void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3967 HandleShift(shr);
3968}
3969
3970void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3971 HandleShift(ushr);
3972}
3973
3974void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3975 HandleShift(ushr);
3976}
3977
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003978void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003979 LocationSummary* locations =
3980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003981 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003982 if (instruction->IsStringAlloc()) {
3983 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3984 } else {
3985 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3986 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3987 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003988 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003989}
3990
3991void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003992 // Note: if heap poisoning is enabled, the entry point takes cares
3993 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003994 if (instruction->IsStringAlloc()) {
3995 // String is allocated through StringFactory. Call NewEmptyString entry point.
3996 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3997 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3998 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3999 __ call(Address(temp, code_offset.SizeValue()));
4000 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4001 } else {
4002 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4003 instruction,
4004 instruction->GetDexPc(),
4005 nullptr);
4006 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4007 DCHECK(!codegen_->IsLeafMethod());
4008 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004009}
4010
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004011void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4012 LocationSummary* locations =
4013 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4014 InvokeRuntimeCallingConvention calling_convention;
4015 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004016 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004017 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004018 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004019}
4020
4021void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
4022 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004023 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
4024 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004025 // Note: if heap poisoning is enabled, the entry point takes cares
4026 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004027 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4028 instruction,
4029 instruction->GetDexPc(),
4030 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004031 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004032
4033 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004034}
4035
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004036void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004037 LocationSummary* locations =
4038 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004039 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4040 if (location.IsStackSlot()) {
4041 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4042 } else if (location.IsDoubleStackSlot()) {
4043 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4044 }
4045 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004046}
4047
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004048void InstructionCodeGeneratorX86_64::VisitParameterValue(
4049 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004050 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004051}
4052
4053void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4054 LocationSummary* locations =
4055 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4056 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4057}
4058
4059void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4060 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4061 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004062}
4063
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004064void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4065 LocationSummary* locations =
4066 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4067 locations->SetInAt(0, Location::RequiresRegister());
4068 locations->SetOut(Location::RequiresRegister());
4069}
4070
4071void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4072 LocationSummary* locations = instruction->GetLocations();
4073 uint32_t method_offset = 0;
4074 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
4075 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4076 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
4077 } else {
4078 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4079 instruction->GetIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
4080 }
4081 __ movq(locations->Out().AsRegister<CpuRegister>(),
4082 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
4083}
4084
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004085void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004086 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004087 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004088 locations->SetInAt(0, Location::RequiresRegister());
4089 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004090}
4091
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004092void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4093 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004094 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4095 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004096 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004097 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004098 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004099 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004100 break;
4101
4102 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004103 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004104 break;
4105
4106 default:
4107 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4108 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004109}
4110
David Brazdil66d126e2015-04-03 16:02:44 +01004111void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4112 LocationSummary* locations =
4113 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4114 locations->SetInAt(0, Location::RequiresRegister());
4115 locations->SetOut(Location::SameAsFirstInput());
4116}
4117
4118void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004119 LocationSummary* locations = bool_not->GetLocations();
4120 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4121 locations->Out().AsRegister<CpuRegister>().AsRegister());
4122 Location out = locations->Out();
4123 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4124}
4125
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004126void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004127 LocationSummary* locations =
4128 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004129 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4130 locations->SetInAt(i, Location::Any());
4131 }
4132 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004133}
4134
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004135void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004136 LOG(FATAL) << "Unimplemented";
4137}
4138
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004139void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004140 /*
4141 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004142 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004143 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4144 */
4145 switch (kind) {
4146 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004147 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004148 break;
4149 }
4150 case MemBarrierKind::kAnyStore:
4151 case MemBarrierKind::kLoadAny:
4152 case MemBarrierKind::kStoreStore: {
4153 // nop
4154 break;
4155 }
4156 default:
4157 LOG(FATAL) << "Unexpected memory barier " << kind;
4158 }
4159}
4160
4161void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4162 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4163
Roland Levillain0d5a2812015-11-13 10:07:31 +00004164 bool object_field_get_with_read_barrier =
4165 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004166 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004167 new (GetGraph()->GetArena()) LocationSummary(instruction,
4168 object_field_get_with_read_barrier ?
4169 LocationSummary::kCallOnSlowPath :
4170 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004171 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004172 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4173 locations->SetOut(Location::RequiresFpuRegister());
4174 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004175 // The output overlaps for an object field get when read barriers
4176 // are enabled: we do not want the move to overwrite the object's
4177 // location, as we need it to emit the read barrier.
4178 locations->SetOut(
4179 Location::RequiresRegister(),
4180 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004181 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004182 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4183 // We need a temporary register for the read barrier marking slow
4184 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4185 locations->AddTemp(Location::RequiresRegister());
4186 }
Calin Juravle52c48962014-12-16 17:02:57 +00004187}
4188
4189void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4190 const FieldInfo& field_info) {
4191 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4192
4193 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004194 Location base_loc = locations->InAt(0);
4195 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004196 Location out = locations->Out();
4197 bool is_volatile = field_info.IsVolatile();
4198 Primitive::Type field_type = field_info.GetFieldType();
4199 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4200
4201 switch (field_type) {
4202 case Primitive::kPrimBoolean: {
4203 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4204 break;
4205 }
4206
4207 case Primitive::kPrimByte: {
4208 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4209 break;
4210 }
4211
4212 case Primitive::kPrimShort: {
4213 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4214 break;
4215 }
4216
4217 case Primitive::kPrimChar: {
4218 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4219 break;
4220 }
4221
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004222 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004223 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4224 break;
4225 }
4226
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004227 case Primitive::kPrimNot: {
4228 // /* HeapReference<Object> */ out = *(base + offset)
4229 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4230 Location temp_loc = locations->GetTemp(0);
4231 // Note that a potential implicit null check is handled in this
4232 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4233 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4234 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4235 if (is_volatile) {
4236 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4237 }
4238 } else {
4239 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4240 codegen_->MaybeRecordImplicitNullCheck(instruction);
4241 if (is_volatile) {
4242 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4243 }
4244 // If read barriers are enabled, emit read barriers other than
4245 // Baker's using a slow path (and also unpoison the loaded
4246 // reference, if heap poisoning is enabled).
4247 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4248 }
4249 break;
4250 }
4251
Calin Juravle52c48962014-12-16 17:02:57 +00004252 case Primitive::kPrimLong: {
4253 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4254 break;
4255 }
4256
4257 case Primitive::kPrimFloat: {
4258 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4259 break;
4260 }
4261
4262 case Primitive::kPrimDouble: {
4263 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4264 break;
4265 }
4266
4267 case Primitive::kPrimVoid:
4268 LOG(FATAL) << "Unreachable type " << field_type;
4269 UNREACHABLE();
4270 }
4271
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004272 if (field_type == Primitive::kPrimNot) {
4273 // Potential implicit null checks, in the case of reference
4274 // fields, are handled in the previous switch statement.
4275 } else {
4276 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004277 }
Roland Levillain4d027112015-07-01 15:41:14 +01004278
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004279 if (is_volatile) {
4280 if (field_type == Primitive::kPrimNot) {
4281 // Memory barriers, in the case of references, are also handled
4282 // in the previous switch statement.
4283 } else {
4284 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4285 }
Roland Levillain4d027112015-07-01 15:41:14 +01004286 }
Calin Juravle52c48962014-12-16 17:02:57 +00004287}
4288
4289void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4290 const FieldInfo& field_info) {
4291 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4292
4293 LocationSummary* locations =
4294 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004295 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004296 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004297 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004298 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004299
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004300 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004301 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004302 if (is_volatile) {
4303 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4304 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4305 } else {
4306 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4307 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004308 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004309 if (is_volatile) {
4310 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4311 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4312 } else {
4313 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4314 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004315 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004316 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004317 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004318 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004319 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004320 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4321 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004322 locations->AddTemp(Location::RequiresRegister());
4323 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004324}
4325
Calin Juravle52c48962014-12-16 17:02:57 +00004326void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004327 const FieldInfo& field_info,
4328 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004329 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4330
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004331 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004332 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4333 Location value = locations->InAt(1);
4334 bool is_volatile = field_info.IsVolatile();
4335 Primitive::Type field_type = field_info.GetFieldType();
4336 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4337
4338 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004339 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004340 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341
Mark Mendellea5af682015-10-22 17:35:49 -04004342 bool maybe_record_implicit_null_check_done = false;
4343
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004344 switch (field_type) {
4345 case Primitive::kPrimBoolean:
4346 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004347 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004348 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004349 __ movb(Address(base, offset), Immediate(v));
4350 } else {
4351 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4352 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004353 break;
4354 }
4355
4356 case Primitive::kPrimShort:
4357 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004358 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004359 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004360 __ movw(Address(base, offset), Immediate(v));
4361 } else {
4362 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4363 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004364 break;
4365 }
4366
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004367 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004368 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004369 if (value.IsConstant()) {
4370 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004371 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4372 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4373 // Note: if heap poisoning is enabled, no need to poison
4374 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004375 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004376 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004377 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4378 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4379 __ movl(temp, value.AsRegister<CpuRegister>());
4380 __ PoisonHeapReference(temp);
4381 __ movl(Address(base, offset), temp);
4382 } else {
4383 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4384 }
Mark Mendell40741f32015-04-20 22:10:34 -04004385 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004386 break;
4387 }
4388
4389 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004390 if (value.IsConstant()) {
4391 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004392 codegen_->MoveInt64ToAddress(Address(base, offset),
4393 Address(base, offset + sizeof(int32_t)),
4394 v,
4395 instruction);
4396 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004397 } else {
4398 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4399 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400 break;
4401 }
4402
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004403 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004404 if (value.IsConstant()) {
4405 int32_t v =
4406 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4407 __ movl(Address(base, offset), Immediate(v));
4408 } else {
4409 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4410 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004411 break;
4412 }
4413
4414 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004415 if (value.IsConstant()) {
4416 int64_t v =
4417 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4418 codegen_->MoveInt64ToAddress(Address(base, offset),
4419 Address(base, offset + sizeof(int32_t)),
4420 v,
4421 instruction);
4422 maybe_record_implicit_null_check_done = true;
4423 } else {
4424 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4425 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004426 break;
4427 }
4428
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429 case Primitive::kPrimVoid:
4430 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004431 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004432 }
Calin Juravle52c48962014-12-16 17:02:57 +00004433
Mark Mendellea5af682015-10-22 17:35:49 -04004434 if (!maybe_record_implicit_null_check_done) {
4435 codegen_->MaybeRecordImplicitNullCheck(instruction);
4436 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004437
4438 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4439 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4440 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004441 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004442 }
4443
Calin Juravle52c48962014-12-16 17:02:57 +00004444 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004445 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004446 }
4447}
4448
4449void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4450 HandleFieldSet(instruction, instruction->GetFieldInfo());
4451}
4452
4453void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004454 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004455}
4456
4457void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004458 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004459}
4460
4461void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004462 HandleFieldGet(instruction, instruction->GetFieldInfo());
4463}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004464
Calin Juravle52c48962014-12-16 17:02:57 +00004465void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4466 HandleFieldGet(instruction);
4467}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004468
Calin Juravle52c48962014-12-16 17:02:57 +00004469void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4470 HandleFieldGet(instruction, instruction->GetFieldInfo());
4471}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004472
Calin Juravle52c48962014-12-16 17:02:57 +00004473void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4474 HandleFieldSet(instruction, instruction->GetFieldInfo());
4475}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476
Calin Juravle52c48962014-12-16 17:02:57 +00004477void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004478 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004479}
4480
Calin Juravlee460d1d2015-09-29 04:52:17 +01004481void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4482 HUnresolvedInstanceFieldGet* instruction) {
4483 FieldAccessCallingConventionX86_64 calling_convention;
4484 codegen_->CreateUnresolvedFieldLocationSummary(
4485 instruction, instruction->GetFieldType(), calling_convention);
4486}
4487
4488void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4489 HUnresolvedInstanceFieldGet* instruction) {
4490 FieldAccessCallingConventionX86_64 calling_convention;
4491 codegen_->GenerateUnresolvedFieldAccess(instruction,
4492 instruction->GetFieldType(),
4493 instruction->GetFieldIndex(),
4494 instruction->GetDexPc(),
4495 calling_convention);
4496}
4497
4498void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4499 HUnresolvedInstanceFieldSet* instruction) {
4500 FieldAccessCallingConventionX86_64 calling_convention;
4501 codegen_->CreateUnresolvedFieldLocationSummary(
4502 instruction, instruction->GetFieldType(), calling_convention);
4503}
4504
4505void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4506 HUnresolvedInstanceFieldSet* instruction) {
4507 FieldAccessCallingConventionX86_64 calling_convention;
4508 codegen_->GenerateUnresolvedFieldAccess(instruction,
4509 instruction->GetFieldType(),
4510 instruction->GetFieldIndex(),
4511 instruction->GetDexPc(),
4512 calling_convention);
4513}
4514
4515void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4516 HUnresolvedStaticFieldGet* instruction) {
4517 FieldAccessCallingConventionX86_64 calling_convention;
4518 codegen_->CreateUnresolvedFieldLocationSummary(
4519 instruction, instruction->GetFieldType(), calling_convention);
4520}
4521
4522void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4523 HUnresolvedStaticFieldGet* instruction) {
4524 FieldAccessCallingConventionX86_64 calling_convention;
4525 codegen_->GenerateUnresolvedFieldAccess(instruction,
4526 instruction->GetFieldType(),
4527 instruction->GetFieldIndex(),
4528 instruction->GetDexPc(),
4529 calling_convention);
4530}
4531
4532void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4533 HUnresolvedStaticFieldSet* instruction) {
4534 FieldAccessCallingConventionX86_64 calling_convention;
4535 codegen_->CreateUnresolvedFieldLocationSummary(
4536 instruction, instruction->GetFieldType(), calling_convention);
4537}
4538
4539void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4540 HUnresolvedStaticFieldSet* instruction) {
4541 FieldAccessCallingConventionX86_64 calling_convention;
4542 codegen_->GenerateUnresolvedFieldAccess(instruction,
4543 instruction->GetFieldType(),
4544 instruction->GetFieldIndex(),
4545 instruction->GetDexPc(),
4546 calling_convention);
4547}
4548
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004550 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4551 ? LocationSummary::kCallOnSlowPath
4552 : LocationSummary::kNoCall;
4553 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4554 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004555 ? Location::RequiresRegister()
4556 : Location::Any();
4557 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004558 if (instruction->HasUses()) {
4559 locations->SetOut(Location::SameAsFirstInput());
4560 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004561}
4562
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004563void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004564 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4565 return;
4566 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004567 LocationSummary* locations = instruction->GetLocations();
4568 Location obj = locations->InAt(0);
4569
4570 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4571 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4572}
4573
4574void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004575 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004576 codegen_->AddSlowPath(slow_path);
4577
4578 LocationSummary* locations = instruction->GetLocations();
4579 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004580
4581 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004582 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004583 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004584 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004585 } else {
4586 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004587 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004588 __ jmp(slow_path->GetEntryLabel());
4589 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004590 }
4591 __ j(kEqual, slow_path->GetEntryLabel());
4592}
4593
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004594void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004595 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004596 GenerateImplicitNullCheck(instruction);
4597 } else {
4598 GenerateExplicitNullCheck(instruction);
4599 }
4600}
4601
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004602void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004603 bool object_array_get_with_read_barrier =
4604 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004605 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004606 new (GetGraph()->GetArena()) LocationSummary(instruction,
4607 object_array_get_with_read_barrier ?
4608 LocationSummary::kCallOnSlowPath :
4609 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004610 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004611 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004612 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4613 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4614 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004615 // The output overlaps for an object array get when read barriers
4616 // are enabled: we do not want the move to overwrite the array's
4617 // location, as we need it to emit the read barrier.
4618 locations->SetOut(
4619 Location::RequiresRegister(),
4620 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004621 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004622 // We need a temporary register for the read barrier marking slow
4623 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4624 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4625 locations->AddTemp(Location::RequiresRegister());
4626 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627}
4628
4629void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4630 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004631 Location obj_loc = locations->InAt(0);
4632 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004633 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004634 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004635
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004636 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004637 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004638 case Primitive::kPrimBoolean: {
4639 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004640 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004641 if (index.IsConstant()) {
4642 __ movzxb(out, Address(obj,
4643 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4644 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004645 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004646 }
4647 break;
4648 }
4649
4650 case Primitive::kPrimByte: {
4651 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004652 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004653 if (index.IsConstant()) {
4654 __ movsxb(out, Address(obj,
4655 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4656 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004657 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 }
4659 break;
4660 }
4661
4662 case Primitive::kPrimShort: {
4663 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004664 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004665 if (index.IsConstant()) {
4666 __ movsxw(out, Address(obj,
4667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4668 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004669 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004670 }
4671 break;
4672 }
4673
4674 case Primitive::kPrimChar: {
4675 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004676 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004677 if (index.IsConstant()) {
4678 __ movzxw(out, Address(obj,
4679 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4680 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004681 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004682 }
4683 break;
4684 }
4685
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004686 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004687 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004688 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004689 if (index.IsConstant()) {
4690 __ movl(out, Address(obj,
4691 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4692 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004693 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004694 }
4695 break;
4696 }
4697
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004698 case Primitive::kPrimNot: {
4699 static_assert(
4700 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4701 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4702 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4703 // /* HeapReference<Object> */ out =
4704 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4705 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4706 Location temp = locations->GetTemp(0);
4707 // Note that a potential implicit null check is handled in this
4708 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4709 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4710 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4711 } else {
4712 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4713 if (index.IsConstant()) {
4714 uint32_t offset =
4715 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4716 __ movl(out, Address(obj, offset));
4717 codegen_->MaybeRecordImplicitNullCheck(instruction);
4718 // If read barriers are enabled, emit read barriers other than
4719 // Baker's using a slow path (and also unpoison the loaded
4720 // reference, if heap poisoning is enabled).
4721 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4722 } else {
4723 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4724 codegen_->MaybeRecordImplicitNullCheck(instruction);
4725 // If read barriers are enabled, emit read barriers other than
4726 // Baker's using a slow path (and also unpoison the loaded
4727 // reference, if heap poisoning is enabled).
4728 codegen_->MaybeGenerateReadBarrierSlow(
4729 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4730 }
4731 }
4732 break;
4733 }
4734
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004735 case Primitive::kPrimLong: {
4736 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004737 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738 if (index.IsConstant()) {
4739 __ movq(out, Address(obj,
4740 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4741 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004742 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004743 }
4744 break;
4745 }
4746
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004747 case Primitive::kPrimFloat: {
4748 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004749 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004750 if (index.IsConstant()) {
4751 __ movss(out, Address(obj,
4752 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4753 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004754 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004755 }
4756 break;
4757 }
4758
4759 case Primitive::kPrimDouble: {
4760 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004761 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004762 if (index.IsConstant()) {
4763 __ movsd(out, Address(obj,
4764 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4765 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004766 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004767 }
4768 break;
4769 }
4770
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004771 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004772 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004773 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004774 }
Roland Levillain4d027112015-07-01 15:41:14 +01004775
4776 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004777 // Potential implicit null checks, in the case of reference
4778 // arrays, are handled in the previous switch statement.
4779 } else {
4780 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004781 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004782}
4783
4784void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004785 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004786
4787 bool needs_write_barrier =
4788 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004789 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004790 bool object_array_set_with_read_barrier =
4791 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004792
Nicolas Geoffray39468442014-09-02 15:17:15 +01004793 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004794 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004795 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004796 LocationSummary::kCallOnSlowPath :
4797 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004798
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004800 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4801 if (Primitive::IsFloatingPointType(value_type)) {
4802 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004803 } else {
4804 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4805 }
4806
4807 if (needs_write_barrier) {
4808 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004809
4810 // This first temporary register is possibly used for heap
4811 // reference poisoning and/or read barrier emission too.
4812 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004813 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004814 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004815}
4816
4817void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4818 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004819 Location array_loc = locations->InAt(0);
4820 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004821 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004822 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004823 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004824 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004825 bool needs_write_barrier =
4826 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004827 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4828 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4829 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004830
4831 switch (value_type) {
4832 case Primitive::kPrimBoolean:
4833 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004834 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4835 Address address = index.IsConstant()
4836 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4837 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4838 if (value.IsRegister()) {
4839 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004840 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004841 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004843 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844 break;
4845 }
4846
4847 case Primitive::kPrimShort:
4848 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004849 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4850 Address address = index.IsConstant()
4851 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4852 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4853 if (value.IsRegister()) {
4854 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004855 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004856 DCHECK(value.IsConstant()) << value;
4857 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004858 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004859 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004860 break;
4861 }
4862
4863 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004864 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4865 Address address = index.IsConstant()
4866 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4867 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004868
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004869 if (!value.IsRegister()) {
4870 // Just setting null.
4871 DCHECK(instruction->InputAt(2)->IsNullConstant());
4872 DCHECK(value.IsConstant()) << value;
4873 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004874 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004875 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004876 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004877 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004878 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004879
4880 DCHECK(needs_write_barrier);
4881 CpuRegister register_value = value.AsRegister<CpuRegister>();
4882 NearLabel done, not_null, do_put;
4883 SlowPathCode* slow_path = nullptr;
4884 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004885 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004886 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4887 codegen_->AddSlowPath(slow_path);
4888 if (instruction->GetValueCanBeNull()) {
4889 __ testl(register_value, register_value);
4890 __ j(kNotEqual, &not_null);
4891 __ movl(address, Immediate(0));
4892 codegen_->MaybeRecordImplicitNullCheck(instruction);
4893 __ jmp(&done);
4894 __ Bind(&not_null);
4895 }
4896
Roland Levillain0d5a2812015-11-13 10:07:31 +00004897 if (kEmitCompilerReadBarrier) {
4898 // When read barriers are enabled, the type checking
4899 // instrumentation requires two read barriers:
4900 //
4901 // __ movl(temp2, temp);
4902 // // /* HeapReference<Class> */ temp = temp->component_type_
4903 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004904 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004905 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4906 //
4907 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4908 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004909 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004910 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4911 //
4912 // __ cmpl(temp, temp2);
4913 //
4914 // However, the second read barrier may trash `temp`, as it
4915 // is a temporary register, and as such would not be saved
4916 // along with live registers before calling the runtime (nor
4917 // restored afterwards). So in this case, we bail out and
4918 // delegate the work to the array set slow path.
4919 //
4920 // TODO: Extend the register allocator to support a new
4921 // "(locally) live temp" location so as to avoid always
4922 // going into the slow path when read barriers are enabled.
4923 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004924 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004925 // /* HeapReference<Class> */ temp = array->klass_
4926 __ movl(temp, Address(array, class_offset));
4927 codegen_->MaybeRecordImplicitNullCheck(instruction);
4928 __ MaybeUnpoisonHeapReference(temp);
4929
4930 // /* HeapReference<Class> */ temp = temp->component_type_
4931 __ movl(temp, Address(temp, component_offset));
4932 // If heap poisoning is enabled, no need to unpoison `temp`
4933 // nor the object reference in `register_value->klass`, as
4934 // we are comparing two poisoned references.
4935 __ cmpl(temp, Address(register_value, class_offset));
4936
4937 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4938 __ j(kEqual, &do_put);
4939 // If heap poisoning is enabled, the `temp` reference has
4940 // not been unpoisoned yet; unpoison it now.
4941 __ MaybeUnpoisonHeapReference(temp);
4942
4943 // /* HeapReference<Class> */ temp = temp->super_class_
4944 __ movl(temp, Address(temp, super_offset));
4945 // If heap poisoning is enabled, no need to unpoison
4946 // `temp`, as we are comparing against null below.
4947 __ testl(temp, temp);
4948 __ j(kNotEqual, slow_path->GetEntryLabel());
4949 __ Bind(&do_put);
4950 } else {
4951 __ j(kNotEqual, slow_path->GetEntryLabel());
4952 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004953 }
4954 }
4955
4956 if (kPoisonHeapReferences) {
4957 __ movl(temp, register_value);
4958 __ PoisonHeapReference(temp);
4959 __ movl(address, temp);
4960 } else {
4961 __ movl(address, register_value);
4962 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004963 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004964 codegen_->MaybeRecordImplicitNullCheck(instruction);
4965 }
4966
4967 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4968 codegen_->MarkGCCard(
4969 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4970 __ Bind(&done);
4971
4972 if (slow_path != nullptr) {
4973 __ Bind(slow_path->GetExitLabel());
4974 }
4975
4976 break;
4977 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004978
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004979 case Primitive::kPrimInt: {
4980 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4981 Address address = index.IsConstant()
4982 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4983 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4984 if (value.IsRegister()) {
4985 __ movl(address, value.AsRegister<CpuRegister>());
4986 } else {
4987 DCHECK(value.IsConstant()) << value;
4988 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4989 __ movl(address, Immediate(v));
4990 }
4991 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004992 break;
4993 }
4994
4995 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004996 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4997 Address address = index.IsConstant()
4998 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4999 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
5000 if (value.IsRegister()) {
5001 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005002 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005003 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005004 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04005005 Address address_high = index.IsConstant()
5006 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5007 offset + sizeof(int32_t))
5008 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5009 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005010 }
5011 break;
5012 }
5013
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005014 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005015 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5016 Address address = index.IsConstant()
5017 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5018 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005019 if (value.IsFpuRegister()) {
5020 __ movss(address, value.AsFpuRegister<XmmRegister>());
5021 } else {
5022 DCHECK(value.IsConstant());
5023 int32_t v =
5024 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5025 __ movl(address, Immediate(v));
5026 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005027 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005028 break;
5029 }
5030
5031 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005032 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5033 Address address = index.IsConstant()
5034 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5035 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005036 if (value.IsFpuRegister()) {
5037 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5038 codegen_->MaybeRecordImplicitNullCheck(instruction);
5039 } else {
5040 int64_t v =
5041 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5042 Address address_high = index.IsConstant()
5043 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5044 offset + sizeof(int32_t))
5045 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
5046 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5047 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005048 break;
5049 }
5050
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051 case Primitive::kPrimVoid:
5052 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005053 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005054 }
5055}
5056
5057void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005058 LocationSummary* locations =
5059 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005060 locations->SetInAt(0, Location::RequiresRegister());
5061 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005062}
5063
5064void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
5065 LocationSummary* locations = instruction->GetLocations();
5066 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005067 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5068 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005070 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071}
5072
5073void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005074 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5075 ? LocationSummary::kCallOnSlowPath
5076 : LocationSummary::kNoCall;
5077 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005078 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005079 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005080 if (instruction->HasUses()) {
5081 locations->SetOut(Location::SameAsFirstInput());
5082 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083}
5084
5085void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5086 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005087 Location index_loc = locations->InAt(0);
5088 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005089 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005090 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005091
Mark Mendell99dbd682015-04-22 16:18:52 -04005092 if (length_loc.IsConstant()) {
5093 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5094 if (index_loc.IsConstant()) {
5095 // BCE will remove the bounds check if we are guarenteed to pass.
5096 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5097 if (index < 0 || index >= length) {
5098 codegen_->AddSlowPath(slow_path);
5099 __ jmp(slow_path->GetEntryLabel());
5100 } else {
5101 // Some optimization after BCE may have generated this, and we should not
5102 // generate a bounds check if it is a valid range.
5103 }
5104 return;
5105 }
5106
5107 // We have to reverse the jump condition because the length is the constant.
5108 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5109 __ cmpl(index_reg, Immediate(length));
5110 codegen_->AddSlowPath(slow_path);
5111 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005112 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005113 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5114 if (index_loc.IsConstant()) {
5115 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5116 __ cmpl(length, Immediate(value));
5117 } else {
5118 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5119 }
5120 codegen_->AddSlowPath(slow_path);
5121 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005122 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005123}
5124
5125void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5126 CpuRegister card,
5127 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005128 CpuRegister value,
5129 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005130 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005131 if (value_can_be_null) {
5132 __ testl(value, value);
5133 __ j(kEqual, &is_null);
5134 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005135 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5136 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 __ movq(temp, object);
5138 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005139 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005140 if (value_can_be_null) {
5141 __ Bind(&is_null);
5142 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143}
5144
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005145void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
5146 temp->SetLocations(nullptr);
5147}
5148
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005149void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005150 // Nothing to do, this is driven by the code generator.
5151}
5152
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005153void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005154 LOG(FATAL) << "Unimplemented";
5155}
5156
5157void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005158 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5159}
5160
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005161void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5162 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5163}
5164
5165void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005166 HBasicBlock* block = instruction->GetBlock();
5167 if (block->GetLoopInformation() != nullptr) {
5168 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5169 // The back edge will generate the suspend check.
5170 return;
5171 }
5172 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5173 // The goto will generate the suspend check.
5174 return;
5175 }
5176 GenerateSuspendCheck(instruction, nullptr);
5177}
5178
5179void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5180 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005181 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005182 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5183 if (slow_path == nullptr) {
5184 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5185 instruction->SetSlowPath(slow_path);
5186 codegen_->AddSlowPath(slow_path);
5187 if (successor != nullptr) {
5188 DCHECK(successor->IsLoopHeader());
5189 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5190 }
5191 } else {
5192 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5193 }
5194
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005195 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5196 /* no_rip */ true),
5197 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005198 if (successor == nullptr) {
5199 __ j(kNotEqual, slow_path->GetEntryLabel());
5200 __ Bind(slow_path->GetReturnLabel());
5201 } else {
5202 __ j(kEqual, codegen_->GetLabelOf(successor));
5203 __ jmp(slow_path->GetEntryLabel());
5204 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005205}
5206
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005207X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5208 return codegen_->GetAssembler();
5209}
5210
5211void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005212 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005213 Location source = move->GetSource();
5214 Location destination = move->GetDestination();
5215
5216 if (source.IsRegister()) {
5217 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005218 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005219 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005220 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005221 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005222 } else {
5223 DCHECK(destination.IsDoubleStackSlot());
5224 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005225 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005226 }
5227 } else if (source.IsStackSlot()) {
5228 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005229 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005230 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005231 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005232 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005233 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005234 } else {
5235 DCHECK(destination.IsStackSlot());
5236 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5237 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5238 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005239 } else if (source.IsDoubleStackSlot()) {
5240 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005241 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005242 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005243 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005244 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5245 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005246 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005247 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005248 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5249 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5250 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005251 } else if (source.IsConstant()) {
5252 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005253 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5254 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005255 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005256 if (value == 0) {
5257 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5258 } else {
5259 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5260 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005261 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005262 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005263 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005264 }
5265 } else if (constant->IsLongConstant()) {
5266 int64_t value = constant->AsLongConstant()->GetValue();
5267 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005268 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005269 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005270 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005271 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005272 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005273 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005274 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005275 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005276 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005277 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005278 } else {
5279 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005280 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005281 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5282 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005283 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005284 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005285 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005286 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005287 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005288 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005289 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005290 } else {
5291 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005292 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005293 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005294 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005295 } else if (source.IsFpuRegister()) {
5296 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005297 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 } else if (destination.IsStackSlot()) {
5299 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005300 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005301 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005302 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005303 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005304 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005305 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005306 }
5307}
5308
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005309void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005310 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005311 __ movl(Address(CpuRegister(RSP), mem), reg);
5312 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005313}
5314
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005315void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005316 ScratchRegisterScope ensure_scratch(
5317 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5318
5319 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5320 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5321 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5322 Address(CpuRegister(RSP), mem2 + stack_offset));
5323 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5324 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5325 CpuRegister(ensure_scratch.GetRegister()));
5326}
5327
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005328void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5329 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5330 __ movq(Address(CpuRegister(RSP), mem), reg);
5331 __ movq(reg, CpuRegister(TMP));
5332}
5333
5334void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5335 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005336 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005337
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005338 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5339 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5340 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5341 Address(CpuRegister(RSP), mem2 + stack_offset));
5342 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5343 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5344 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005345}
5346
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005347void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5348 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5349 __ movss(Address(CpuRegister(RSP), mem), reg);
5350 __ movd(reg, CpuRegister(TMP));
5351}
5352
5353void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5354 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5355 __ movsd(Address(CpuRegister(RSP), mem), reg);
5356 __ movd(reg, CpuRegister(TMP));
5357}
5358
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005359void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005360 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005361 Location source = move->GetSource();
5362 Location destination = move->GetDestination();
5363
5364 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005365 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005366 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005367 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005368 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005369 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005370 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005371 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5372 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005373 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005374 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005375 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005376 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5377 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005378 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005379 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5380 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5381 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005382 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005383 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005384 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005385 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005386 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005387 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005388 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005389 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005390 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005391 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005392 }
5393}
5394
5395
5396void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5397 __ pushq(CpuRegister(reg));
5398}
5399
5400
5401void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5402 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005403}
5404
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005405void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005406 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005407 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5408 Immediate(mirror::Class::kStatusInitialized));
5409 __ j(kLess, slow_path->GetEntryLabel());
5410 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005411 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005412}
5413
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005414void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005415 InvokeRuntimeCallingConvention calling_convention;
5416 CodeGenerator::CreateLoadClassLocationSummary(
5417 cls,
5418 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005419 Location::RegisterLocation(RAX),
5420 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421}
5422
5423void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005424 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005425 if (cls->NeedsAccessCheck()) {
5426 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5427 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5428 cls,
5429 cls->GetDexPc(),
5430 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005431 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005432 return;
5433 }
5434
Roland Levillain0d5a2812015-11-13 10:07:31 +00005435 Location out_loc = locations->Out();
5436 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005437 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005438
Calin Juravle580b6092015-10-06 17:35:58 +01005439 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005440 DCHECK(!cls->CanCallRuntime());
5441 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005442 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5443 GenerateGcRootFieldLoad(
5444 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005445 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005446 // /* GcRoot<mirror::Class>[] */ out =
5447 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5448 __ movq(out, Address(current_method,
5449 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005450 // /* GcRoot<mirror::Class> */ out = out[type_index]
5451 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01005452
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005453 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5454 DCHECK(cls->CanCallRuntime());
5455 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5456 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5457 codegen_->AddSlowPath(slow_path);
5458 if (!cls->IsInDexCache()) {
5459 __ testl(out, out);
5460 __ j(kEqual, slow_path->GetEntryLabel());
5461 }
5462 if (cls->MustGenerateClinitCheck()) {
5463 GenerateClassInitializationCheck(slow_path, out);
5464 } else {
5465 __ Bind(slow_path->GetExitLabel());
5466 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005467 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005468 }
5469}
5470
5471void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5472 LocationSummary* locations =
5473 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5474 locations->SetInAt(0, Location::RequiresRegister());
5475 if (check->HasUses()) {
5476 locations->SetOut(Location::SameAsFirstInput());
5477 }
5478}
5479
5480void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005481 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005482 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005483 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005484 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005485 GenerateClassInitializationCheck(slow_path,
5486 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005487}
5488
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005489void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005490 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5491 ? LocationSummary::kCallOnSlowPath
5492 : LocationSummary::kNoCall;
5493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005494 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005495 locations->SetOut(Location::RequiresRegister());
5496}
5497
5498void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005499 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005500 Location out_loc = locations->Out();
5501 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005502 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005503
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005504 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5505 GenerateGcRootFieldLoad(
5506 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005507 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5508 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005509 // /* GcRoot<mirror::String> */ out = out[string_index]
5510 GenerateGcRootFieldLoad(
5511 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005512
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005513 if (!load->IsInDexCache()) {
5514 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5515 codegen_->AddSlowPath(slow_path);
5516 __ testl(out, out);
5517 __ j(kEqual, slow_path->GetEntryLabel());
5518 __ Bind(slow_path->GetExitLabel());
5519 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005520}
5521
David Brazdilcb1c0552015-08-04 16:22:25 +01005522static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005523 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5524 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005525}
5526
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005527void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5528 LocationSummary* locations =
5529 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5530 locations->SetOut(Location::RequiresRegister());
5531}
5532
5533void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005534 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5535}
5536
5537void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5538 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5539}
5540
5541void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5542 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005543}
5544
5545void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5546 LocationSummary* locations =
5547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5548 InvokeRuntimeCallingConvention calling_convention;
5549 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5550}
5551
5552void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005553 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5554 instruction,
5555 instruction->GetDexPc(),
5556 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005557 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005558}
5559
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005560static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5561 return kEmitCompilerReadBarrier &&
5562 (kUseBakerReadBarrier ||
5563 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5564 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5565 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5566}
5567
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005568void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005569 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005570 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5571 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005572 case TypeCheckKind::kExactCheck:
5573 case TypeCheckKind::kAbstractClassCheck:
5574 case TypeCheckKind::kClassHierarchyCheck:
5575 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005576 call_kind =
5577 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005578 break;
5579 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005580 case TypeCheckKind::kUnresolvedCheck:
5581 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005582 call_kind = LocationSummary::kCallOnSlowPath;
5583 break;
5584 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005585
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005587 locations->SetInAt(0, Location::RequiresRegister());
5588 locations->SetInAt(1, Location::Any());
5589 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5590 locations->SetOut(Location::RequiresRegister());
5591 // When read barriers are enabled, we need a temporary register for
5592 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005593 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005594 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005595 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005596}
5597
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005598void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005599 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005600 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005601 Location obj_loc = locations->InAt(0);
5602 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005603 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005604 Location out_loc = locations->Out();
5605 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005606 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005607 locations->GetTemp(0) :
5608 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005609 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005610 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5611 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5612 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005613 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005614 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005615
5616 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005617 // Avoid null check if we know obj is not null.
5618 if (instruction->MustDoNullCheck()) {
5619 __ testl(obj, obj);
5620 __ j(kEqual, &zero);
5621 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005622
Roland Levillain0d5a2812015-11-13 10:07:31 +00005623 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005624 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005625
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005626 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005627 case TypeCheckKind::kExactCheck: {
5628 if (cls.IsRegister()) {
5629 __ cmpl(out, cls.AsRegister<CpuRegister>());
5630 } else {
5631 DCHECK(cls.IsStackSlot()) << cls;
5632 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5633 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005634 if (zero.IsLinked()) {
5635 // Classes must be equal for the instanceof to succeed.
5636 __ j(kNotEqual, &zero);
5637 __ movl(out, Immediate(1));
5638 __ jmp(&done);
5639 } else {
5640 __ setcc(kEqual, out);
5641 // setcc only sets the low byte.
5642 __ andl(out, Immediate(1));
5643 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 break;
5645 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005646
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005647 case TypeCheckKind::kAbstractClassCheck: {
5648 // If the class is abstract, we eagerly fetch the super class of the
5649 // object to avoid doing a comparison we know will fail.
5650 NearLabel loop, success;
5651 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005652 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005653 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005654 __ testl(out, out);
5655 // If `out` is null, we use it for the result, and jump to `done`.
5656 __ j(kEqual, &done);
5657 if (cls.IsRegister()) {
5658 __ cmpl(out, cls.AsRegister<CpuRegister>());
5659 } else {
5660 DCHECK(cls.IsStackSlot()) << cls;
5661 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5662 }
5663 __ j(kNotEqual, &loop);
5664 __ movl(out, Immediate(1));
5665 if (zero.IsLinked()) {
5666 __ jmp(&done);
5667 }
5668 break;
5669 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005670
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005671 case TypeCheckKind::kClassHierarchyCheck: {
5672 // Walk over the class hierarchy to find a match.
5673 NearLabel loop, success;
5674 __ Bind(&loop);
5675 if (cls.IsRegister()) {
5676 __ cmpl(out, cls.AsRegister<CpuRegister>());
5677 } else {
5678 DCHECK(cls.IsStackSlot()) << cls;
5679 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5680 }
5681 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005682 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005683 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005684 __ testl(out, out);
5685 __ j(kNotEqual, &loop);
5686 // If `out` is null, we use it for the result, and jump to `done`.
5687 __ jmp(&done);
5688 __ Bind(&success);
5689 __ movl(out, Immediate(1));
5690 if (zero.IsLinked()) {
5691 __ jmp(&done);
5692 }
5693 break;
5694 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005695
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005696 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005697 // Do an exact check.
5698 NearLabel exact_check;
5699 if (cls.IsRegister()) {
5700 __ cmpl(out, cls.AsRegister<CpuRegister>());
5701 } else {
5702 DCHECK(cls.IsStackSlot()) << cls;
5703 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5704 }
5705 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005706 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005707 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005708 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005709 __ testl(out, out);
5710 // If `out` is null, we use it for the result, and jump to `done`.
5711 __ j(kEqual, &done);
5712 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5713 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005714 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005715 __ movl(out, Immediate(1));
5716 __ jmp(&done);
5717 break;
5718 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005719
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005720 case TypeCheckKind::kArrayCheck: {
5721 if (cls.IsRegister()) {
5722 __ cmpl(out, cls.AsRegister<CpuRegister>());
5723 } else {
5724 DCHECK(cls.IsStackSlot()) << cls;
5725 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5726 }
5727 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005728 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5729 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005730 codegen_->AddSlowPath(slow_path);
5731 __ j(kNotEqual, slow_path->GetEntryLabel());
5732 __ movl(out, Immediate(1));
5733 if (zero.IsLinked()) {
5734 __ jmp(&done);
5735 }
5736 break;
5737 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005738
Calin Juravle98893e12015-10-02 21:05:03 +01005739 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005740 case TypeCheckKind::kInterfaceCheck: {
5741 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005742 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005743 // cases.
5744 //
5745 // We cannot directly call the InstanceofNonTrivial runtime
5746 // entry point without resorting to a type checking slow path
5747 // here (i.e. by calling InvokeRuntime directly), as it would
5748 // require to assign fixed registers for the inputs of this
5749 // HInstanceOf instruction (following the runtime calling
5750 // convention), which might be cluttered by the potential first
5751 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005752 //
5753 // TODO: Introduce a new runtime entry point taking the object
5754 // to test (instead of its class) as argument, and let it deal
5755 // with the read barrier issues. This will let us refactor this
5756 // case of the `switch` code as it was previously (with a direct
5757 // call to the runtime not using a type checking slow path).
5758 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005759 DCHECK(locations->OnlyCallsOnSlowPath());
5760 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5761 /* is_fatal */ false);
5762 codegen_->AddSlowPath(slow_path);
5763 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 if (zero.IsLinked()) {
5765 __ jmp(&done);
5766 }
5767 break;
5768 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005769 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005770
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005771 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005772 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005773 __ xorl(out, out);
5774 }
5775
5776 if (done.IsLinked()) {
5777 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005778 }
5779
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005780 if (slow_path != nullptr) {
5781 __ Bind(slow_path->GetExitLabel());
5782 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005783}
5784
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005785void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005786 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5787 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005788 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5789 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005790 case TypeCheckKind::kExactCheck:
5791 case TypeCheckKind::kAbstractClassCheck:
5792 case TypeCheckKind::kClassHierarchyCheck:
5793 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005794 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5795 LocationSummary::kCallOnSlowPath :
5796 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005797 break;
5798 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005799 case TypeCheckKind::kUnresolvedCheck:
5800 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005801 call_kind = LocationSummary::kCallOnSlowPath;
5802 break;
5803 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005804 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5805 locations->SetInAt(0, Location::RequiresRegister());
5806 locations->SetInAt(1, Location::Any());
5807 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5808 locations->AddTemp(Location::RequiresRegister());
5809 // When read barriers are enabled, we need an additional temporary
5810 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005811 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005812 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005813 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005814}
5815
5816void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005817 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005818 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005819 Location obj_loc = locations->InAt(0);
5820 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005821 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005822 Location temp_loc = locations->GetTemp(0);
5823 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005824 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005825 locations->GetTemp(1) :
5826 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005827 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5828 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5829 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5830 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831
Roland Levillain0d5a2812015-11-13 10:07:31 +00005832 bool is_type_check_slow_path_fatal =
5833 (type_check_kind == TypeCheckKind::kExactCheck ||
5834 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5835 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5836 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5837 !instruction->CanThrowIntoCatchBlock();
5838 SlowPathCode* type_check_slow_path =
5839 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5840 is_type_check_slow_path_fatal);
5841 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005842
Roland Levillain0d5a2812015-11-13 10:07:31 +00005843 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 case TypeCheckKind::kExactCheck:
5845 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005846 NearLabel done;
5847 // Avoid null check if we know obj is not null.
5848 if (instruction->MustDoNullCheck()) {
5849 __ testl(obj, obj);
5850 __ j(kEqual, &done);
5851 }
5852
5853 // /* HeapReference<Class> */ temp = obj->klass_
5854 GenerateReferenceLoadTwoRegisters(
5855 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5856
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005857 if (cls.IsRegister()) {
5858 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5859 } else {
5860 DCHECK(cls.IsStackSlot()) << cls;
5861 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5862 }
5863 // Jump to slow path for throwing the exception or doing a
5864 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005865 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005866 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005867 break;
5868 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005869
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005870 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005871 NearLabel done;
5872 // Avoid null check if we know obj is not null.
5873 if (instruction->MustDoNullCheck()) {
5874 __ testl(obj, obj);
5875 __ j(kEqual, &done);
5876 }
5877
5878 // /* HeapReference<Class> */ temp = obj->klass_
5879 GenerateReferenceLoadTwoRegisters(
5880 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5881
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 // If the class is abstract, we eagerly fetch the super class of the
5883 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005884 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005886 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005887 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005888
5889 // If the class reference currently in `temp` is not null, jump
5890 // to the `compare_classes` label to compare it with the checked
5891 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005892 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005893 __ j(kNotEqual, &compare_classes);
5894 // Otherwise, jump to the slow path to throw the exception.
5895 //
5896 // But before, move back the object's class into `temp` before
5897 // going into the slow path, as it has been overwritten in the
5898 // meantime.
5899 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005900 GenerateReferenceLoadTwoRegisters(
5901 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005902 __ jmp(type_check_slow_path->GetEntryLabel());
5903
5904 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005905 if (cls.IsRegister()) {
5906 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5907 } else {
5908 DCHECK(cls.IsStackSlot()) << cls;
5909 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5910 }
5911 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00005912 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005913 break;
5914 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005915
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005917 NearLabel done;
5918 // Avoid null check if we know obj is not null.
5919 if (instruction->MustDoNullCheck()) {
5920 __ testl(obj, obj);
5921 __ j(kEqual, &done);
5922 }
5923
5924 // /* HeapReference<Class> */ temp = obj->klass_
5925 GenerateReferenceLoadTwoRegisters(
5926 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5927
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005928 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005929 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930 __ Bind(&loop);
5931 if (cls.IsRegister()) {
5932 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5933 } else {
5934 DCHECK(cls.IsStackSlot()) << cls;
5935 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5936 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005937 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005938
Roland Levillain0d5a2812015-11-13 10:07:31 +00005939 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005940 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005941
5942 // If the class reference currently in `temp` is not null, jump
5943 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005944 __ testl(temp, temp);
5945 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005946 // Otherwise, jump to the slow path to throw the exception.
5947 //
5948 // But before, move back the object's class into `temp` before
5949 // going into the slow path, as it has been overwritten in the
5950 // meantime.
5951 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005952 GenerateReferenceLoadTwoRegisters(
5953 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005954 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005955 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005956 break;
5957 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005958
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005960 // We cannot use a NearLabel here, as its range might be too
5961 // short in some cases when read barriers are enabled. This has
5962 // been observed for instance when the code emitted for this
5963 // case uses high x86-64 registers (R8-R15).
5964 Label done;
5965 // Avoid null check if we know obj is not null.
5966 if (instruction->MustDoNullCheck()) {
5967 __ testl(obj, obj);
5968 __ j(kEqual, &done);
5969 }
5970
5971 // /* HeapReference<Class> */ temp = obj->klass_
5972 GenerateReferenceLoadTwoRegisters(
5973 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5974
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005975 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005976 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005977 if (cls.IsRegister()) {
5978 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5979 } else {
5980 DCHECK(cls.IsStackSlot()) << cls;
5981 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5982 }
5983 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005984
5985 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005987 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005988
5989 // If the component type is not null (i.e. the object is indeed
5990 // an array), jump to label `check_non_primitive_component_type`
5991 // to further check that this component type is not a primitive
5992 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005993 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994 __ j(kNotEqual, &check_non_primitive_component_type);
5995 // Otherwise, jump to the slow path to throw the exception.
5996 //
5997 // But before, move back the object's class into `temp` before
5998 // going into the slow path, as it has been overwritten in the
5999 // meantime.
6000 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006001 GenerateReferenceLoadTwoRegisters(
6002 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006003 __ jmp(type_check_slow_path->GetEntryLabel());
6004
6005 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006006 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006007 __ j(kEqual, &done);
6008 // Same comment as above regarding `temp` and the slow path.
6009 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006010 GenerateReferenceLoadTwoRegisters(
6011 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006013 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006014 break;
6015 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006016
Calin Juravle98893e12015-10-02 21:05:03 +01006017 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006018 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006019 NearLabel done;
6020 // Avoid null check if we know obj is not null.
6021 if (instruction->MustDoNullCheck()) {
6022 __ testl(obj, obj);
6023 __ j(kEqual, &done);
6024 }
6025
6026 // /* HeapReference<Class> */ temp = obj->klass_
6027 GenerateReferenceLoadTwoRegisters(
6028 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6029
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006030 // We always go into the type check slow path for the unresolved
6031 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006032 //
6033 // We cannot directly call the CheckCast runtime entry point
6034 // without resorting to a type checking slow path here (i.e. by
6035 // calling InvokeRuntime directly), as it would require to
6036 // assign fixed registers for the inputs of this HInstanceOf
6037 // instruction (following the runtime calling convention), which
6038 // might be cluttered by the potential first read barrier
6039 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006040 //
6041 // TODO: Introduce a new runtime entry point taking the object
6042 // to test (instead of its class) as argument, and let it deal
6043 // with the read barrier issues. This will let us refactor this
6044 // case of the `switch` code as it was previously (with a direct
6045 // call to the runtime not using a type checking slow path).
6046 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006047 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006048 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006049 break;
6050 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006051
Roland Levillain0d5a2812015-11-13 10:07:31 +00006052 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006053}
6054
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006055void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6056 LocationSummary* locations =
6057 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6058 InvokeRuntimeCallingConvention calling_convention;
6059 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6060}
6061
6062void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006063 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6064 : QUICK_ENTRY_POINT(pUnlockObject),
6065 instruction,
6066 instruction->GetDexPc(),
6067 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006068 if (instruction->IsEnter()) {
6069 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6070 } else {
6071 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6072 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006073}
6074
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006075void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6076void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6077void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6078
6079void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6080 LocationSummary* locations =
6081 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6082 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6083 || instruction->GetResultType() == Primitive::kPrimLong);
6084 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006085 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006086 locations->SetOut(Location::SameAsFirstInput());
6087}
6088
6089void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6090 HandleBitwiseOperation(instruction);
6091}
6092
6093void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6094 HandleBitwiseOperation(instruction);
6095}
6096
6097void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6098 HandleBitwiseOperation(instruction);
6099}
6100
6101void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6102 LocationSummary* locations = instruction->GetLocations();
6103 Location first = locations->InAt(0);
6104 Location second = locations->InAt(1);
6105 DCHECK(first.Equals(locations->Out()));
6106
6107 if (instruction->GetResultType() == Primitive::kPrimInt) {
6108 if (second.IsRegister()) {
6109 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006110 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006111 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006112 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006113 } else {
6114 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006115 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006116 }
6117 } else if (second.IsConstant()) {
6118 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6119 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006120 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006121 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006122 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006123 } else {
6124 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006125 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006126 }
6127 } else {
6128 Address address(CpuRegister(RSP), second.GetStackIndex());
6129 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006130 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006131 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006132 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006133 } else {
6134 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006135 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006136 }
6137 }
6138 } else {
6139 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006140 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6141 bool second_is_constant = false;
6142 int64_t value = 0;
6143 if (second.IsConstant()) {
6144 second_is_constant = true;
6145 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006146 }
Mark Mendell40741f32015-04-20 22:10:34 -04006147 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006148
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006149 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006150 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006151 if (is_int32_value) {
6152 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6153 } else {
6154 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6155 }
6156 } else if (second.IsDoubleStackSlot()) {
6157 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006158 } else {
6159 __ andq(first_reg, second.AsRegister<CpuRegister>());
6160 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006161 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006162 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006163 if (is_int32_value) {
6164 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6165 } else {
6166 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6167 }
6168 } else if (second.IsDoubleStackSlot()) {
6169 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006170 } else {
6171 __ orq(first_reg, second.AsRegister<CpuRegister>());
6172 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006173 } else {
6174 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006175 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006176 if (is_int32_value) {
6177 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6178 } else {
6179 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6180 }
6181 } else if (second.IsDoubleStackSlot()) {
6182 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006183 } else {
6184 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6185 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006186 }
6187 }
6188}
6189
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006190void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6191 Location out,
6192 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006193 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006194 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6195 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006196 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006197 if (kUseBakerReadBarrier) {
6198 // Load with fast path based Baker's read barrier.
6199 // /* HeapReference<Object> */ out = *(out + offset)
6200 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006201 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006202 } else {
6203 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006204 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006205 // in the following move operation, as we will need it for the
6206 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006207 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006208 // /* HeapReference<Object> */ out = *(out + offset)
6209 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006210 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006211 }
6212 } else {
6213 // Plain load with no read barrier.
6214 // /* HeapReference<Object> */ out = *(out + offset)
6215 __ movl(out_reg, Address(out_reg, offset));
6216 __ MaybeUnpoisonHeapReference(out_reg);
6217 }
6218}
6219
6220void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6221 Location out,
6222 Location obj,
6223 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006224 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006225 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6226 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6227 if (kEmitCompilerReadBarrier) {
6228 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006229 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006230 // Load with fast path based Baker's read barrier.
6231 // /* HeapReference<Object> */ out = *(obj + offset)
6232 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006233 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006234 } else {
6235 // Load with slow path based read barrier.
6236 // /* HeapReference<Object> */ out = *(obj + offset)
6237 __ movl(out_reg, Address(obj_reg, offset));
6238 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6239 }
6240 } else {
6241 // Plain load with no read barrier.
6242 // /* HeapReference<Object> */ out = *(obj + offset)
6243 __ movl(out_reg, Address(obj_reg, offset));
6244 __ MaybeUnpoisonHeapReference(out_reg);
6245 }
6246}
6247
6248void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6249 Location root,
6250 CpuRegister obj,
6251 uint32_t offset) {
6252 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6253 if (kEmitCompilerReadBarrier) {
6254 if (kUseBakerReadBarrier) {
6255 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6256 // Baker's read barrier are used:
6257 //
6258 // root = obj.field;
6259 // if (Thread::Current()->GetIsGcMarking()) {
6260 // root = ReadBarrier::Mark(root)
6261 // }
6262
6263 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6264 __ movl(root_reg, Address(obj, offset));
6265 static_assert(
6266 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6267 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6268 "have different sizes.");
6269 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6270 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6271 "have different sizes.");
6272
6273 // Slow path used to mark the GC root `root`.
6274 SlowPathCode* slow_path =
6275 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6276 codegen_->AddSlowPath(slow_path);
6277
6278 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6279 /* no_rip */ true),
6280 Immediate(0));
6281 __ j(kNotEqual, slow_path->GetEntryLabel());
6282 __ Bind(slow_path->GetExitLabel());
6283 } else {
6284 // GC root loaded through a slow path for read barriers other
6285 // than Baker's.
6286 // /* GcRoot<mirror::Object>* */ root = obj + offset
6287 __ leaq(root_reg, Address(obj, offset));
6288 // /* mirror::Object* */ root = root->Read()
6289 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6290 }
6291 } else {
6292 // Plain GC root load with no read barrier.
6293 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6294 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006295 // Note that GC roots are not affected by heap poisoning, thus we
6296 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006297 }
6298}
6299
6300void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6301 Location ref,
6302 CpuRegister obj,
6303 uint32_t offset,
6304 Location temp,
6305 bool needs_null_check) {
6306 DCHECK(kEmitCompilerReadBarrier);
6307 DCHECK(kUseBakerReadBarrier);
6308
6309 // /* HeapReference<Object> */ ref = *(obj + offset)
6310 Address src(obj, offset);
6311 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6312}
6313
6314void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6315 Location ref,
6316 CpuRegister obj,
6317 uint32_t data_offset,
6318 Location index,
6319 Location temp,
6320 bool needs_null_check) {
6321 DCHECK(kEmitCompilerReadBarrier);
6322 DCHECK(kUseBakerReadBarrier);
6323
6324 // /* HeapReference<Object> */ ref =
6325 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6326 Address src = index.IsConstant() ?
6327 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6328 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6329 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6330}
6331
6332void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6333 Location ref,
6334 CpuRegister obj,
6335 const Address& src,
6336 Location temp,
6337 bool needs_null_check) {
6338 DCHECK(kEmitCompilerReadBarrier);
6339 DCHECK(kUseBakerReadBarrier);
6340
6341 // In slow path based read barriers, the read barrier call is
6342 // inserted after the original load. However, in fast path based
6343 // Baker's read barriers, we need to perform the load of
6344 // mirror::Object::monitor_ *before* the original reference load.
6345 // This load-load ordering is required by the read barrier.
6346 // The fast path/slow path (for Baker's algorithm) should look like:
6347 //
6348 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6349 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6350 // HeapReference<Object> ref = *src; // Original reference load.
6351 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6352 // if (is_gray) {
6353 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6354 // }
6355 //
6356 // Note: the original implementation in ReadBarrier::Barrier is
6357 // slightly more complex as:
6358 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006359 // the high-bits of rb_state, which are expected to be all zeroes
6360 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6361 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006362 // - it performs additional checks that we do not do here for
6363 // performance reasons.
6364
6365 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6366 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6367 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6368
6369 // /* int32_t */ monitor = obj->monitor_
6370 __ movl(temp_reg, Address(obj, monitor_offset));
6371 if (needs_null_check) {
6372 MaybeRecordImplicitNullCheck(instruction);
6373 }
6374 // /* LockWord */ lock_word = LockWord(monitor)
6375 static_assert(sizeof(LockWord) == sizeof(int32_t),
6376 "art::LockWord and int32_t have different sizes.");
6377 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6378 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6379 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6380 static_assert(
6381 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6382 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6383
6384 // Load fence to prevent load-load reordering.
6385 // Note that this is a no-op, thanks to the x86-64 memory model.
6386 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6387
6388 // The actual reference load.
6389 // /* HeapReference<Object> */ ref = *src
6390 __ movl(ref_reg, src);
6391
6392 // Object* ref = ref_addr->AsMirrorPtr()
6393 __ MaybeUnpoisonHeapReference(ref_reg);
6394
6395 // Slow path used to mark the object `ref` when it is gray.
6396 SlowPathCode* slow_path =
6397 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6398 AddSlowPath(slow_path);
6399
6400 // if (rb_state == ReadBarrier::gray_ptr_)
6401 // ref = ReadBarrier::Mark(ref);
6402 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6403 __ j(kEqual, slow_path->GetEntryLabel());
6404 __ Bind(slow_path->GetExitLabel());
6405}
6406
6407void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6408 Location out,
6409 Location ref,
6410 Location obj,
6411 uint32_t offset,
6412 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413 DCHECK(kEmitCompilerReadBarrier);
6414
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006415 // Insert a slow path based read barrier *after* the reference load.
6416 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006417 // If heap poisoning is enabled, the unpoisoning of the loaded
6418 // reference will be carried out by the runtime within the slow
6419 // path.
6420 //
6421 // Note that `ref` currently does not get unpoisoned (when heap
6422 // poisoning is enabled), which is alright as the `ref` argument is
6423 // not used by the artReadBarrierSlow entry point.
6424 //
6425 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6426 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6427 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6428 AddSlowPath(slow_path);
6429
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 __ jmp(slow_path->GetEntryLabel());
6431 __ Bind(slow_path->GetExitLabel());
6432}
6433
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006434void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6435 Location out,
6436 Location ref,
6437 Location obj,
6438 uint32_t offset,
6439 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006440 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006441 // Baker's read barriers shall be handled by the fast path
6442 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6443 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006444 // If heap poisoning is enabled, unpoisoning will be taken care of
6445 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006446 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006447 } else if (kPoisonHeapReferences) {
6448 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6449 }
6450}
6451
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006452void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6453 Location out,
6454 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455 DCHECK(kEmitCompilerReadBarrier);
6456
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006457 // Insert a slow path based read barrier *after* the GC root load.
6458 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006459 // Note that GC roots are not affected by heap poisoning, so we do
6460 // not need to do anything special for this here.
6461 SlowPathCode* slow_path =
6462 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6463 AddSlowPath(slow_path);
6464
Roland Levillain0d5a2812015-11-13 10:07:31 +00006465 __ jmp(slow_path->GetEntryLabel());
6466 __ Bind(slow_path->GetExitLabel());
6467}
6468
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006469void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006470 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006471 LOG(FATAL) << "Unreachable";
6472}
6473
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006474void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006475 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006476 LOG(FATAL) << "Unreachable";
6477}
6478
Mark Mendellfe57faa2015-09-18 09:26:15 -04006479// Simple implementation of packed switch - generate cascaded compare/jumps.
6480void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6481 LocationSummary* locations =
6482 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6483 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006484 locations->AddTemp(Location::RequiresRegister());
6485 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006486}
6487
6488void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6489 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006490 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006491 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006492 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6493 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6494 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006495 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6496
6497 // Should we generate smaller inline compare/jumps?
6498 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6499 // Figure out the correct compare values and jump conditions.
6500 // Handle the first compare/branch as a special case because it might
6501 // jump to the default case.
6502 DCHECK_GT(num_entries, 2u);
6503 Condition first_condition;
6504 uint32_t index;
6505 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6506 if (lower_bound != 0) {
6507 first_condition = kLess;
6508 __ cmpl(value_reg_in, Immediate(lower_bound));
6509 __ j(first_condition, codegen_->GetLabelOf(default_block));
6510 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6511
6512 index = 1;
6513 } else {
6514 // Handle all the compare/jumps below.
6515 first_condition = kBelow;
6516 index = 0;
6517 }
6518
6519 // Handle the rest of the compare/jumps.
6520 for (; index + 1 < num_entries; index += 2) {
6521 int32_t compare_to_value = lower_bound + index + 1;
6522 __ cmpl(value_reg_in, Immediate(compare_to_value));
6523 // Jump to successors[index] if value < case_value[index].
6524 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6525 // Jump to successors[index + 1] if value == case_value[index + 1].
6526 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6527 }
6528
6529 if (index != num_entries) {
6530 // There are an odd number of entries. Handle the last one.
6531 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006532 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006533 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6534 }
6535
6536 // And the default for any other value.
6537 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6538 __ jmp(codegen_->GetLabelOf(default_block));
6539 }
6540 return;
6541 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006542
6543 // Remove the bias, if needed.
6544 Register value_reg_out = value_reg_in.AsRegister();
6545 if (lower_bound != 0) {
6546 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6547 value_reg_out = temp_reg.AsRegister();
6548 }
6549 CpuRegister value_reg(value_reg_out);
6550
6551 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006552 __ cmpl(value_reg, Immediate(num_entries - 1));
6553 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006554
Mark Mendell9c86b482015-09-18 13:36:07 -04006555 // We are in the range of the table.
6556 // Load the address of the jump table in the constant area.
6557 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006558
Mark Mendell9c86b482015-09-18 13:36:07 -04006559 // Load the (signed) offset from the jump table.
6560 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6561
6562 // Add the offset to the address of the table base.
6563 __ addq(temp_reg, base_reg);
6564
6565 // And jump.
6566 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006567}
6568
Aart Bikc5d47542016-01-27 17:00:35 -08006569void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6570 if (value == 0) {
6571 __ xorl(dest, dest);
6572 } else {
6573 __ movl(dest, Immediate(value));
6574 }
6575}
6576
Mark Mendell92e83bf2015-05-07 11:25:03 -04006577void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6578 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006579 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006580 __ xorl(dest, dest);
6581 } else if (value > 0 && IsInt<32>(value)) {
6582 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
6583 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6584 } else {
6585 __ movq(dest, Immediate(value));
6586 }
6587}
6588
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006589void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6590 if (value == 0) {
6591 __ xorps(dest, dest);
6592 } else {
6593 __ movss(dest, LiteralInt32Address(value));
6594 }
6595}
6596
6597void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6598 if (value == 0) {
6599 __ xorpd(dest, dest);
6600 } else {
6601 __ movsd(dest, LiteralInt64Address(value));
6602 }
6603}
6604
6605void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6606 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6607}
6608
6609void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6610 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6611}
6612
Aart Bika19616e2016-02-01 18:57:58 -08006613void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6614 if (value == 0) {
6615 __ testl(dest, dest);
6616 } else {
6617 __ cmpl(dest, Immediate(value));
6618 }
6619}
6620
6621void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6622 if (IsInt<32>(value)) {
6623 if (value == 0) {
6624 __ testq(dest, dest);
6625 } else {
6626 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6627 }
6628 } else {
6629 // Value won't fit in an int.
6630 __ cmpq(dest, LiteralInt64Address(value));
6631 }
6632}
6633
Mark Mendellcfa410b2015-05-25 16:02:44 -04006634void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6635 DCHECK(dest.IsDoubleStackSlot());
6636 if (IsInt<32>(value)) {
6637 // Can move directly as an int32 constant.
6638 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6639 Immediate(static_cast<int32_t>(value)));
6640 } else {
6641 Load64BitValue(CpuRegister(TMP), value);
6642 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6643 }
6644}
6645
Mark Mendell9c86b482015-09-18 13:36:07 -04006646/**
6647 * Class to handle late fixup of offsets into constant area.
6648 */
6649class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6650 public:
6651 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6652 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6653
6654 protected:
6655 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6656
6657 CodeGeneratorX86_64* codegen_;
6658
6659 private:
6660 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6661 // Patch the correct offset for the instruction. We use the address of the
6662 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6663 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6664 int32_t relative_position = constant_offset - pos;
6665
6666 // Patch in the right value.
6667 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6668 }
6669
6670 // Location in constant area that the fixup refers to.
6671 size_t offset_into_constant_area_;
6672};
6673
6674/**
6675 t * Class to handle late fixup of offsets to a jump table that will be created in the
6676 * constant area.
6677 */
6678class JumpTableRIPFixup : public RIPFixup {
6679 public:
6680 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6681 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6682
6683 void CreateJumpTable() {
6684 X86_64Assembler* assembler = codegen_->GetAssembler();
6685
6686 // Ensure that the reference to the jump table has the correct offset.
6687 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6688 SetOffset(offset_in_constant_table);
6689
6690 // Compute the offset from the start of the function to this jump table.
6691 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6692
6693 // Populate the jump table with the correct values for the jump table.
6694 int32_t num_entries = switch_instr_->GetNumEntries();
6695 HBasicBlock* block = switch_instr_->GetBlock();
6696 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6697 // The value that we want is the target offset - the position of the table.
6698 for (int32_t i = 0; i < num_entries; i++) {
6699 HBasicBlock* b = successors[i];
6700 Label* l = codegen_->GetLabelOf(b);
6701 DCHECK(l->IsBound());
6702 int32_t offset_to_block = l->Position() - current_table_offset;
6703 assembler->AppendInt32(offset_to_block);
6704 }
6705 }
6706
6707 private:
6708 const HPackedSwitch* switch_instr_;
6709};
6710
Mark Mendellf55c3e02015-03-26 21:07:46 -04006711void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6712 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006713 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006714 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6715 // 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 -04006716 assembler->Align(4, 0);
6717 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006718
6719 // Populate any jump tables.
6720 for (auto jump_table : fixups_to_jump_tables_) {
6721 jump_table->CreateJumpTable();
6722 }
6723
6724 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006725 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006726 }
6727
6728 // And finish up.
6729 CodeGenerator::Finalize(allocator);
6730}
6731
Mark Mendellf55c3e02015-03-26 21:07:46 -04006732Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6733 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6734 return Address::RIP(fixup);
6735}
6736
6737Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6738 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6739 return Address::RIP(fixup);
6740}
6741
6742Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6743 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6744 return Address::RIP(fixup);
6745}
6746
6747Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6748 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6749 return Address::RIP(fixup);
6750}
6751
Andreas Gampe85b62f22015-09-09 13:15:38 -07006752// TODO: trg as memory.
6753void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6754 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006755 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006756 return;
6757 }
6758
6759 DCHECK_NE(type, Primitive::kPrimVoid);
6760
6761 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6762 if (trg.Equals(return_loc)) {
6763 return;
6764 }
6765
6766 // Let the parallel move resolver take care of all of this.
6767 HParallelMove parallel_move(GetGraph()->GetArena());
6768 parallel_move.AddMove(return_loc, trg, type, nullptr);
6769 GetMoveResolver()->EmitNativeCode(&parallel_move);
6770}
6771
Mark Mendell9c86b482015-09-18 13:36:07 -04006772Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6773 // Create a fixup to be used to create and address the jump table.
6774 JumpTableRIPFixup* table_fixup =
6775 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6776
6777 // We have to populate the jump tables.
6778 fixups_to_jump_tables_.push_back(table_fixup);
6779 return Address::RIP(table_fixup);
6780}
6781
Mark Mendellea5af682015-10-22 17:35:49 -04006782void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6783 const Address& addr_high,
6784 int64_t v,
6785 HInstruction* instruction) {
6786 if (IsInt<32>(v)) {
6787 int32_t v_32 = v;
6788 __ movq(addr_low, Immediate(v_32));
6789 MaybeRecordImplicitNullCheck(instruction);
6790 } else {
6791 // Didn't fit in a register. Do it in pieces.
6792 int32_t low_v = Low32Bits(v);
6793 int32_t high_v = High32Bits(v);
6794 __ movl(addr_low, Immediate(low_v));
6795 MaybeRecordImplicitNullCheck(instruction);
6796 __ movl(addr_high, Immediate(high_v));
6797 }
6798}
6799
Roland Levillain4d027112015-07-01 15:41:14 +01006800#undef __
6801
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006802} // namespace x86_64
6803} // namespace art