blob: c12166e8436fdaa045779df2d7aa3168f49f479a [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:
390 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
391 : 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());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 DCHECK(instruction_->IsDeoptimize());
398 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000399 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
400 deoptimize,
401 deoptimize->GetDexPc(),
402 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000403 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700404 }
405
Alexandre Rames9931f312015-06-19 14:47:01 +0100406 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
407
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700408 private:
409 HInstruction* const instruction_;
410 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
411};
412
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413class ArraySetSlowPathX86_64 : public SlowPathCode {
414 public:
415 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : instruction_(instruction) {}
416
417 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
418 LocationSummary* locations = instruction_->GetLocations();
419 __ Bind(GetEntryLabel());
420 SaveLiveRegisters(codegen, locations);
421
422 InvokeRuntimeCallingConvention calling_convention;
423 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
424 parallel_move.AddMove(
425 locations->InAt(0),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
427 Primitive::kPrimNot,
428 nullptr);
429 parallel_move.AddMove(
430 locations->InAt(1),
431 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
432 Primitive::kPrimInt,
433 nullptr);
434 parallel_move.AddMove(
435 locations->InAt(2),
436 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
437 Primitive::kPrimNot,
438 nullptr);
439 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
440
Roland Levillain0d5a2812015-11-13 10:07:31 +0000441 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
442 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
443 instruction_,
444 instruction_->GetDexPc(),
445 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000446 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100447 RestoreLiveRegisters(codegen, locations);
448 __ jmp(GetExitLabel());
449 }
450
451 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
452
453 private:
454 HInstruction* const instruction_;
455
456 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
457};
458
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000459// Slow path marking an object during a read barrier.
460class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
461 public:
462 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location out, Location obj)
463 : instruction_(instruction), out_(out), obj_(obj) {
464 DCHECK(kEmitCompilerReadBarrier);
465 }
466
467 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
468
469 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
470 LocationSummary* locations = instruction_->GetLocations();
471 Register reg_out = out_.AsRegister<Register>();
472 DCHECK(locations->CanCall());
473 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
474 DCHECK(instruction_->IsInstanceFieldGet() ||
475 instruction_->IsStaticFieldGet() ||
476 instruction_->IsArrayGet() ||
477 instruction_->IsLoadClass() ||
478 instruction_->IsLoadString() ||
479 instruction_->IsInstanceOf() ||
480 instruction_->IsCheckCast())
481 << "Unexpected instruction in read barrier marking slow path: "
482 << instruction_->DebugName();
483
484 __ Bind(GetEntryLabel());
485 SaveLiveRegisters(codegen, locations);
486
487 InvokeRuntimeCallingConvention calling_convention;
488 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
489 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
490 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
491 instruction_,
492 instruction_->GetDexPc(),
493 this);
494 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
495 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
496
497 RestoreLiveRegisters(codegen, locations);
498 __ jmp(GetExitLabel());
499 }
500
501 private:
502 HInstruction* const instruction_;
503 const Location out_;
504 const Location obj_;
505
506 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
507};
508
Roland Levillain0d5a2812015-11-13 10:07:31 +0000509// Slow path generating a read barrier for a heap reference.
510class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
511 public:
512 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
513 Location out,
514 Location ref,
515 Location obj,
516 uint32_t offset,
517 Location index)
518 : instruction_(instruction),
519 out_(out),
520 ref_(ref),
521 obj_(obj),
522 offset_(offset),
523 index_(index) {
524 DCHECK(kEmitCompilerReadBarrier);
525 // If `obj` is equal to `out` or `ref`, it means the initial
526 // object has been overwritten by (or after) the heap object
527 // reference load to be instrumented, e.g.:
528 //
529 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000530 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000531 //
532 // In that case, we have lost the information about the original
533 // object, and the emitted read barrier cannot work properly.
534 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
535 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
536}
537
538 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
539 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
540 LocationSummary* locations = instruction_->GetLocations();
541 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
542 DCHECK(locations->CanCall());
543 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
544 DCHECK(!instruction_->IsInvoke() ||
545 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000546 instruction_->GetLocations()->Intrinsified()))
547 << "Unexpected instruction in read barrier for heap reference slow path: "
548 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000549
550 __ Bind(GetEntryLabel());
551 SaveLiveRegisters(codegen, locations);
552
553 // We may have to change the index's value, but as `index_` is a
554 // constant member (like other "inputs" of this slow path),
555 // introduce a copy of it, `index`.
556 Location index = index_;
557 if (index_.IsValid()) {
558 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
559 if (instruction_->IsArrayGet()) {
560 // Compute real offset and store it in index_.
561 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
562 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
563 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
564 // We are about to change the value of `index_reg` (see the
565 // calls to art::x86_64::X86_64Assembler::shll and
566 // art::x86_64::X86_64Assembler::AddImmediate below), but it
567 // has not been saved by the previous call to
568 // art::SlowPathCode::SaveLiveRegisters, as it is a
569 // callee-save register --
570 // art::SlowPathCode::SaveLiveRegisters does not consider
571 // callee-save registers, as it has been designed with the
572 // assumption that callee-save registers are supposed to be
573 // handled by the called function. So, as a callee-save
574 // register, `index_reg` _would_ eventually be saved onto
575 // the stack, but it would be too late: we would have
576 // changed its value earlier. Therefore, we manually save
577 // it here into another freely available register,
578 // `free_reg`, chosen of course among the caller-save
579 // registers (as a callee-save `free_reg` register would
580 // exhibit the same problem).
581 //
582 // Note we could have requested a temporary register from
583 // the register allocator instead; but we prefer not to, as
584 // this is a slow path, and we know we can find a
585 // caller-save register that is available.
586 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
587 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
588 index_reg = free_reg;
589 index = Location::RegisterLocation(index_reg);
590 } else {
591 // The initial register stored in `index_` has already been
592 // saved in the call to art::SlowPathCode::SaveLiveRegisters
593 // (as it is not a callee-save register), so we can freely
594 // use it.
595 }
596 // Shifting the index value contained in `index_reg` by the
597 // scale factor (2) cannot overflow in practice, as the
598 // runtime is unable to allocate object arrays with a size
599 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
600 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
601 static_assert(
602 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
603 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
604 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
605 } else {
606 DCHECK(instruction_->IsInvoke());
607 DCHECK(instruction_->GetLocations()->Intrinsified());
608 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
609 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
610 << instruction_->AsInvoke()->GetIntrinsic();
611 DCHECK_EQ(offset_, 0U);
612 DCHECK(index_.IsRegister());
613 }
614 }
615
616 // We're moving two or three locations to locations that could
617 // overlap, so we need a parallel move resolver.
618 InvokeRuntimeCallingConvention calling_convention;
619 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
620 parallel_move.AddMove(ref_,
621 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
622 Primitive::kPrimNot,
623 nullptr);
624 parallel_move.AddMove(obj_,
625 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
626 Primitive::kPrimNot,
627 nullptr);
628 if (index.IsValid()) {
629 parallel_move.AddMove(index,
630 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
631 Primitive::kPrimInt,
632 nullptr);
633 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
634 } else {
635 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
636 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
637 }
638 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
639 instruction_,
640 instruction_->GetDexPc(),
641 this);
642 CheckEntrypointTypes<
643 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
644 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
645
646 RestoreLiveRegisters(codegen, locations);
647 __ jmp(GetExitLabel());
648 }
649
650 const char* GetDescription() const OVERRIDE {
651 return "ReadBarrierForHeapReferenceSlowPathX86_64";
652 }
653
654 private:
655 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
656 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
657 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
658 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
659 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
660 return static_cast<CpuRegister>(i);
661 }
662 }
663 // We shall never fail to find a free caller-save register, as
664 // there are more than two core caller-save registers on x86-64
665 // (meaning it is possible to find one which is different from
666 // `ref` and `obj`).
667 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
668 LOG(FATAL) << "Could not find a free caller-save register";
669 UNREACHABLE();
670 }
671
672 HInstruction* const instruction_;
673 const Location out_;
674 const Location ref_;
675 const Location obj_;
676 const uint32_t offset_;
677 // An additional location containing an index to an array.
678 // Only used for HArrayGet and the UnsafeGetObject &
679 // UnsafeGetObjectVolatile intrinsics.
680 const Location index_;
681
682 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
683};
684
685// Slow path generating a read barrier for a GC root.
686class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
687 public:
688 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000689 : instruction_(instruction), out_(out), root_(root) {
690 DCHECK(kEmitCompilerReadBarrier);
691 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000692
693 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
694 LocationSummary* locations = instruction_->GetLocations();
695 DCHECK(locations->CanCall());
696 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000697 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
698 << "Unexpected instruction in read barrier for GC root slow path: "
699 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000700
701 __ Bind(GetEntryLabel());
702 SaveLiveRegisters(codegen, locations);
703
704 InvokeRuntimeCallingConvention calling_convention;
705 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
706 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
707 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
708 instruction_,
709 instruction_->GetDexPc(),
710 this);
711 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
712 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
713
714 RestoreLiveRegisters(codegen, locations);
715 __ jmp(GetExitLabel());
716 }
717
718 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
719
720 private:
721 HInstruction* const instruction_;
722 const Location out_;
723 const Location root_;
724
725 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
726};
727
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100728#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100729#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100730
Roland Levillain4fa13f62015-07-06 18:11:54 +0100731inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700732 switch (cond) {
733 case kCondEQ: return kEqual;
734 case kCondNE: return kNotEqual;
735 case kCondLT: return kLess;
736 case kCondLE: return kLessEqual;
737 case kCondGT: return kGreater;
738 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700739 case kCondB: return kBelow;
740 case kCondBE: return kBelowEqual;
741 case kCondA: return kAbove;
742 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700743 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100744 LOG(FATAL) << "Unreachable";
745 UNREACHABLE();
746}
747
Aart Bike9f37602015-10-09 11:15:55 -0700748// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100749inline Condition X86_64FPCondition(IfCondition cond) {
750 switch (cond) {
751 case kCondEQ: return kEqual;
752 case kCondNE: return kNotEqual;
753 case kCondLT: return kBelow;
754 case kCondLE: return kBelowEqual;
755 case kCondGT: return kAbove;
756 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700757 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100758 };
759 LOG(FATAL) << "Unreachable";
760 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700761}
762
Vladimir Markodc151b22015-10-15 18:02:30 +0100763HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
764 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
765 MethodReference target_method ATTRIBUTE_UNUSED) {
766 switch (desired_dispatch_info.code_ptr_location) {
767 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
768 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
769 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
770 return HInvokeStaticOrDirect::DispatchInfo {
771 desired_dispatch_info.method_load_kind,
772 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
773 desired_dispatch_info.method_load_data,
774 0u
775 };
776 default:
777 return desired_dispatch_info;
778 }
779}
780
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800781void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100782 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800783 // All registers are assumed to be correctly set up.
784
Vladimir Marko58155012015-08-19 12:49:41 +0000785 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
786 switch (invoke->GetMethodLoadKind()) {
787 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
788 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000789 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000790 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000791 break;
792 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000793 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000794 break;
795 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
796 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
797 break;
798 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
799 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
800 method_patches_.emplace_back(invoke->GetTargetMethod());
801 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
802 break;
803 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000804 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
805 invoke->GetDexCacheArrayOffset());
Vladimir Marko58155012015-08-19 12:49:41 +0000806 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000807 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko58155012015-08-19 12:49:41 +0000808 // Bind the label at the end of the "movl" insn.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000809 __ Bind(&pc_relative_dex_cache_patches_.back().label);
Vladimir Marko58155012015-08-19 12:49:41 +0000810 break;
811 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000812 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000813 Register method_reg;
814 CpuRegister reg = temp.AsRegister<CpuRegister>();
815 if (current_method.IsRegister()) {
816 method_reg = current_method.AsRegister<Register>();
817 } else {
818 DCHECK(invoke->GetLocations()->Intrinsified());
819 DCHECK(!current_method.IsValid());
820 method_reg = reg.AsRegister();
821 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
822 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000823 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100824 __ movq(reg,
825 Address(CpuRegister(method_reg),
826 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000827 // temp = temp[index_in_cache]
828 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
829 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
830 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100831 }
Vladimir Marko58155012015-08-19 12:49:41 +0000832 }
833
834 switch (invoke->GetCodePtrLocation()) {
835 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
836 __ call(&frame_entry_label_);
837 break;
838 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
839 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
840 Label* label = &relative_call_patches_.back().label;
841 __ call(label); // Bind to the patch label, override at link time.
842 __ Bind(label); // Bind the label at the end of the "call" insn.
843 break;
844 }
845 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
846 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100847 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
848 LOG(FATAL) << "Unsupported";
849 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000850 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
851 // (callee_method + offset_of_quick_compiled_code)()
852 __ call(Address(callee_method.AsRegister<CpuRegister>(),
853 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
854 kX86_64WordSize).SizeValue()));
855 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000856 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800857
858 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800859}
860
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000861void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
862 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
863 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
864 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000865
866 // Use the calling convention instead of the location of the receiver, as
867 // intrinsics may have put the receiver in a different register. In the intrinsics
868 // slow path, the arguments have been moved to the right place, so here we are
869 // guaranteed that the receiver is the first register of the calling convention.
870 InvokeDexCallingConvention calling_convention;
871 Register receiver = calling_convention.GetRegisterAt(0);
872
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000873 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000874 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000875 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000876 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000877 // Instead of simply (possibly) unpoisoning `temp` here, we should
878 // emit a read barrier for the previous class reference load.
879 // However this is not required in practice, as this is an
880 // intermediate/temporary reference and because the current
881 // concurrent copying collector keeps the from-space memory
882 // intact/accessible until the end of the marking phase (the
883 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000884 __ MaybeUnpoisonHeapReference(temp);
885 // temp = temp->GetMethodAt(method_offset);
886 __ movq(temp, Address(temp, method_offset));
887 // call temp->GetEntryPoint();
888 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
889 kX86_64WordSize).SizeValue()));
890}
891
Vladimir Marko58155012015-08-19 12:49:41 +0000892void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
893 DCHECK(linker_patches->empty());
894 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000895 method_patches_.size() +
896 relative_call_patches_.size() +
897 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000898 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000899 // The label points to the end of the "movl" insn but the literal offset for method
900 // patch needs to point to the embedded constant which occupies the last 4 bytes.
901 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000902 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000903 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000904 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
905 info.target_method.dex_file,
906 info.target_method.dex_method_index));
907 }
908 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000909 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000910 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
911 info.target_method.dex_file,
912 info.target_method.dex_method_index));
913 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000914 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
915 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000916 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
917 &info.target_dex_file,
918 info.label.Position(),
919 info.element_offset));
920 }
921}
922
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100924 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100925}
926
927void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100928 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100929}
930
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100931size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
932 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
933 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100934}
935
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100936size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
937 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
938 return kX86_64WordSize;
939}
940
941size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
942 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
943 return kX86_64WordSize;
944}
945
946size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
947 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
948 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100949}
950
Calin Juravle175dc732015-08-25 15:42:32 +0100951void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
952 HInstruction* instruction,
953 uint32_t dex_pc,
954 SlowPathCode* slow_path) {
955 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
956 instruction,
957 dex_pc,
958 slow_path);
959}
960
961void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100962 HInstruction* instruction,
963 uint32_t dex_pc,
964 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100965 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000966 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100967 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100968}
969
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000970static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000971// Use a fake return address register to mimic Quick.
972static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400973CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000974 const X86_64InstructionSetFeatures& isa_features,
975 const CompilerOptions& compiler_options,
976 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000977 : CodeGenerator(graph,
978 kNumberOfCpuRegisters,
979 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000980 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000981 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
982 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000983 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000984 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
985 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100986 compiler_options,
987 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100988 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100989 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000990 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400991 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400992 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000993 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100994 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
995 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000996 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -0400997 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000998 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
999}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001000
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001001InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1002 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001003 : HGraphVisitor(graph),
1004 assembler_(codegen->GetAssembler()),
1005 codegen_(codegen) {}
1006
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001007Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001008 switch (type) {
1009 case Primitive::kPrimLong:
1010 case Primitive::kPrimByte:
1011 case Primitive::kPrimBoolean:
1012 case Primitive::kPrimChar:
1013 case Primitive::kPrimShort:
1014 case Primitive::kPrimInt:
1015 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001016 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001017 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001018 }
1019
1020 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001021 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001022 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001023 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001024 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001025
1026 case Primitive::kPrimVoid:
1027 LOG(FATAL) << "Unreachable type " << type;
1028 }
1029
Roland Levillain0d5a2812015-11-13 10:07:31 +00001030 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001031}
1032
Nicolas Geoffray98893962015-01-21 12:32:32 +00001033void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001034 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001035 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001036
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001037 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001038 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001039
Nicolas Geoffray98893962015-01-21 12:32:32 +00001040 if (is_baseline) {
1041 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1042 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1043 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001044 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1045 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1046 }
Nicolas Geoffray98893962015-01-21 12:32:32 +00001047 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001048}
1049
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001050static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001051 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001052}
David Srbecky9d8606d2015-04-12 09:35:32 +01001053
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001054static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001055 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056}
1057
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001058void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001059 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001060 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001061 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001062 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001063 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001064
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001065 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001066 __ testq(CpuRegister(RAX), Address(
1067 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001068 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001069 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001070
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001071 if (HasEmptyFrame()) {
1072 return;
1073 }
1074
Nicolas Geoffray98893962015-01-21 12:32:32 +00001075 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001076 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001077 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001078 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001079 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1080 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001081 }
1082 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001083
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001084 int adjust = GetFrameSize() - GetCoreSpillSize();
1085 __ subq(CpuRegister(RSP), Immediate(adjust));
1086 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001087 uint32_t xmm_spill_location = GetFpuSpillStart();
1088 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001089
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001090 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1091 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001092 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1093 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1094 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001095 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001096 }
1097
Mathieu Chartiere401d142015-04-22 13:56:20 -07001098 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001099 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001100}
1101
1102void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001103 __ cfi().RememberState();
1104 if (!HasEmptyFrame()) {
1105 uint32_t xmm_spill_location = GetFpuSpillStart();
1106 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1107 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1108 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1109 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1110 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1111 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1112 }
1113 }
1114
1115 int adjust = GetFrameSize() - GetCoreSpillSize();
1116 __ addq(CpuRegister(RSP), Immediate(adjust));
1117 __ cfi().AdjustCFAOffset(-adjust);
1118
1119 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1120 Register reg = kCoreCalleeSaves[i];
1121 if (allocated_registers_.ContainsCoreRegister(reg)) {
1122 __ popq(CpuRegister(reg));
1123 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1124 __ cfi().Restore(DWARFReg(reg));
1125 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001126 }
1127 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001128 __ ret();
1129 __ cfi().RestoreState();
1130 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001131}
1132
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001133void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1134 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135}
1136
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001137Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
1138 switch (load->GetType()) {
1139 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001140 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001141 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001142
1143 case Primitive::kPrimInt:
1144 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001145 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001146 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001147
1148 case Primitive::kPrimBoolean:
1149 case Primitive::kPrimByte:
1150 case Primitive::kPrimChar:
1151 case Primitive::kPrimShort:
1152 case Primitive::kPrimVoid:
1153 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -07001154 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001155 }
1156
1157 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -07001158 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001159}
1160
1161void CodeGeneratorX86_64::Move(Location destination, Location source) {
1162 if (source.Equals(destination)) {
1163 return;
1164 }
1165 if (destination.IsRegister()) {
1166 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001167 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001169 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001170 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001171 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001172 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001173 } else {
1174 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001175 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001176 Address(CpuRegister(RSP), source.GetStackIndex()));
1177 }
1178 } else if (destination.IsFpuRegister()) {
1179 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001180 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001181 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001182 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001183 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001184 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001185 Address(CpuRegister(RSP), source.GetStackIndex()));
1186 } else {
1187 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001188 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001190 }
1191 } else if (destination.IsStackSlot()) {
1192 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001193 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001194 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001195 } else if (source.IsFpuRegister()) {
1196 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001197 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001198 } else if (source.IsConstant()) {
1199 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001200 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001201 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001202 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001203 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001204 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1205 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001206 }
1207 } else {
1208 DCHECK(destination.IsDoubleStackSlot());
1209 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001211 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001212 } else if (source.IsFpuRegister()) {
1213 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001214 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001215 } else if (source.IsConstant()) {
1216 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001217 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001218 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001219 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001220 } else {
1221 DCHECK(constant->IsLongConstant());
1222 value = constant->AsLongConstant()->GetValue();
1223 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001224 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001225 } else {
1226 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001227 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1228 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001229 }
1230 }
1231}
1232
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001233void CodeGeneratorX86_64::Move(HInstruction* instruction,
1234 Location location,
1235 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001236 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001237 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -07001238 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001239 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001240 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001241 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001242 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001243 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1244 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001245 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001246 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001247 } else if (location.IsStackSlot()) {
1248 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
1249 } else {
1250 DCHECK(location.IsConstant());
1251 DCHECK_EQ(location.GetConstant(), const_to_move);
1252 }
1253 } else if (const_to_move->IsLongConstant()) {
1254 int64_t value = const_to_move->AsLongConstant()->GetValue();
1255 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04001256 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001257 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -04001258 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001259 } else {
1260 DCHECK(location.IsConstant());
1261 DCHECK_EQ(location.GetConstant(), const_to_move);
1262 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001263 }
Roland Levillain476df552014-10-09 17:51:36 +01001264 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001265 switch (instruction->GetType()) {
1266 case Primitive::kPrimBoolean:
1267 case Primitive::kPrimByte:
1268 case Primitive::kPrimChar:
1269 case Primitive::kPrimShort:
1270 case Primitive::kPrimInt:
1271 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001272 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001273 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
1274 break;
1275
1276 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +00001278 Move(location,
1279 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001280 break;
1281
1282 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001283 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001284 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001285 } else if (instruction->IsTemporary()) {
1286 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
1287 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001288 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001289 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001290 switch (instruction->GetType()) {
1291 case Primitive::kPrimBoolean:
1292 case Primitive::kPrimByte:
1293 case Primitive::kPrimChar:
1294 case Primitive::kPrimShort:
1295 case Primitive::kPrimInt:
1296 case Primitive::kPrimNot:
1297 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001298 case Primitive::kPrimFloat:
1299 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001300 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001301 break;
1302
1303 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001304 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001305 }
1306 }
1307}
1308
Calin Juravle175dc732015-08-25 15:42:32 +01001309void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1310 DCHECK(location.IsRegister());
1311 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1312}
1313
Calin Juravlee460d1d2015-09-29 04:52:17 +01001314void CodeGeneratorX86_64::MoveLocation(
1315 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1316 Move(dst, src);
1317}
1318
1319void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1320 if (location.IsRegister()) {
1321 locations->AddTemp(location);
1322 } else {
1323 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1324 }
1325}
1326
David Brazdilfc6a86a2015-06-26 10:33:45 +00001327void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001328 DCHECK(!successor->IsExitBlock());
1329
1330 HBasicBlock* block = got->GetBlock();
1331 HInstruction* previous = got->GetPrevious();
1332
1333 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001334 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001335 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1336 return;
1337 }
1338
1339 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1340 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1341 }
1342 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001343 __ jmp(codegen_->GetLabelOf(successor));
1344 }
1345}
1346
David Brazdilfc6a86a2015-06-26 10:33:45 +00001347void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1348 got->SetLocations(nullptr);
1349}
1350
1351void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1352 HandleGoto(got, got->GetSuccessor());
1353}
1354
1355void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1356 try_boundary->SetLocations(nullptr);
1357}
1358
1359void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1360 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1361 if (!successor->IsExitBlock()) {
1362 HandleGoto(try_boundary, successor);
1363 }
1364}
1365
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001366void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1367 exit->SetLocations(nullptr);
1368}
1369
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001370void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001371}
1372
Mark Mendellc4701932015-04-10 13:18:51 -04001373void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
1374 Label* true_label,
1375 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001376 if (cond->IsFPConditionTrueIfNaN()) {
1377 __ j(kUnordered, true_label);
1378 } else if (cond->IsFPConditionFalseIfNaN()) {
1379 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001380 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001381 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001382}
1383
David Brazdil0debae72015-11-12 18:37:00 +00001384void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1385 Label* true_target_in,
1386 Label* false_target_in) {
1387 // Generated branching requires both targets to be explicit. If either of the
1388 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1389 Label fallthrough_target;
1390 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1391 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1392
Mark Mendellc4701932015-04-10 13:18:51 -04001393 LocationSummary* locations = condition->GetLocations();
1394 Location left = locations->InAt(0);
1395 Location right = locations->InAt(1);
1396
Mark Mendellc4701932015-04-10 13:18:51 -04001397 Primitive::Type type = condition->InputAt(0)->GetType();
1398 switch (type) {
1399 case Primitive::kPrimLong: {
1400 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1401 if (right.IsConstant()) {
1402 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1403 if (IsInt<32>(value)) {
1404 if (value == 0) {
1405 __ testq(left_reg, left_reg);
1406 } else {
1407 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1408 }
1409 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001410 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001411 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1412 }
1413 } else if (right.IsDoubleStackSlot()) {
1414 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1415 } else {
1416 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1417 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001418 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001419 break;
1420 }
1421 case Primitive::kPrimFloat: {
1422 if (right.IsFpuRegister()) {
1423 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1424 } else if (right.IsConstant()) {
1425 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1426 codegen_->LiteralFloatAddress(
1427 right.GetConstant()->AsFloatConstant()->GetValue()));
1428 } else {
1429 DCHECK(right.IsStackSlot());
1430 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1431 Address(CpuRegister(RSP), right.GetStackIndex()));
1432 }
1433 GenerateFPJumps(condition, true_target, false_target);
1434 break;
1435 }
1436 case Primitive::kPrimDouble: {
1437 if (right.IsFpuRegister()) {
1438 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1439 } else if (right.IsConstant()) {
1440 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1441 codegen_->LiteralDoubleAddress(
1442 right.GetConstant()->AsDoubleConstant()->GetValue()));
1443 } else {
1444 DCHECK(right.IsDoubleStackSlot());
1445 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1446 Address(CpuRegister(RSP), right.GetStackIndex()));
1447 }
1448 GenerateFPJumps(condition, true_target, false_target);
1449 break;
1450 }
1451 default:
1452 LOG(FATAL) << "Unexpected condition type " << type;
1453 }
1454
David Brazdil0debae72015-11-12 18:37:00 +00001455 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001456 __ jmp(false_target);
1457 }
David Brazdil0debae72015-11-12 18:37:00 +00001458
1459 if (fallthrough_target.IsLinked()) {
1460 __ Bind(&fallthrough_target);
1461 }
Mark Mendellc4701932015-04-10 13:18:51 -04001462}
1463
David Brazdil0debae72015-11-12 18:37:00 +00001464static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1465 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1466 // are set only strictly before `branch`. We can't use the eflags on long
1467 // conditions if they are materialized due to the complex branching.
1468 return cond->IsCondition() &&
1469 cond->GetNext() == branch &&
1470 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1471}
1472
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001473void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001474 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001475 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001476 Label* false_target) {
1477 HInstruction* cond = instruction->InputAt(condition_input_index);
1478
1479 if (true_target == nullptr && false_target == nullptr) {
1480 // Nothing to do. The code always falls through.
1481 return;
1482 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001483 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001484 if (cond->AsIntConstant()->IsOne()) {
1485 if (true_target != nullptr) {
1486 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001487 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001488 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001489 DCHECK(cond->AsIntConstant()->IsZero());
1490 if (false_target != nullptr) {
1491 __ jmp(false_target);
1492 }
1493 }
1494 return;
1495 }
1496
1497 // The following code generates these patterns:
1498 // (1) true_target == nullptr && false_target != nullptr
1499 // - opposite condition true => branch to false_target
1500 // (2) true_target != nullptr && false_target == nullptr
1501 // - condition true => branch to true_target
1502 // (3) true_target != nullptr && false_target != nullptr
1503 // - condition true => branch to true_target
1504 // - branch to false_target
1505 if (IsBooleanValueOrMaterializedCondition(cond)) {
1506 if (AreEflagsSetFrom(cond, instruction)) {
1507 if (true_target == nullptr) {
1508 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1509 } else {
1510 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1511 }
1512 } else {
1513 // Materialized condition, compare against 0.
1514 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1515 if (lhs.IsRegister()) {
1516 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1517 } else {
1518 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1519 }
1520 if (true_target == nullptr) {
1521 __ j(kEqual, false_target);
1522 } else {
1523 __ j(kNotEqual, true_target);
1524 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001525 }
1526 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001527 // Condition has not been materialized, use its inputs as the
1528 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001529 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001530
David Brazdil0debae72015-11-12 18:37:00 +00001531 // If this is a long or FP comparison that has been folded into
1532 // the HCondition, generate the comparison directly.
1533 Primitive::Type type = condition->InputAt(0)->GetType();
1534 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1535 GenerateCompareTestAndBranch(condition, true_target, false_target);
1536 return;
1537 }
1538
1539 Location lhs = condition->GetLocations()->InAt(0);
1540 Location rhs = condition->GetLocations()->InAt(1);
1541 if (rhs.IsRegister()) {
1542 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1543 } else if (rhs.IsConstant()) {
1544 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1545 if (constant == 0) {
1546 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001547 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001548 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001549 }
1550 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001551 __ cmpl(lhs.AsRegister<CpuRegister>(),
1552 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1553 }
1554 if (true_target == nullptr) {
1555 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1556 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001557 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001558 }
Dave Allison20dfc792014-06-16 20:44:29 -07001559 }
David Brazdil0debae72015-11-12 18:37:00 +00001560
1561 // If neither branch falls through (case 3), the conditional branch to `true_target`
1562 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1563 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001564 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001565 }
1566}
1567
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001568void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1570 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001571 locations->SetInAt(0, Location::Any());
1572 }
1573}
1574
1575void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001576 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1577 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1578 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1579 nullptr : codegen_->GetLabelOf(true_successor);
1580 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1581 nullptr : codegen_->GetLabelOf(false_successor);
1582 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001583}
1584
1585void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1586 LocationSummary* locations = new (GetGraph()->GetArena())
1587 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001588 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001589 locations->SetInAt(0, Location::Any());
1590 }
1591}
1592
1593void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001594 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001595 DeoptimizationSlowPathX86_64(deoptimize);
1596 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001597 GenerateTestAndBranch(deoptimize,
1598 /* condition_input_index */ 0,
1599 slow_path->GetEntryLabel(),
1600 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001601}
1602
David Srbecky0cf44932015-12-09 14:09:59 +00001603void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1604 new (GetGraph()->GetArena()) LocationSummary(info);
1605}
1606
1607void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1608 codegen_->RecordPcInfo(info, info->GetDexPc());
1609}
1610
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001611void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1612 local->SetLocations(nullptr);
1613}
1614
1615void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1616 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1617}
1618
1619void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1620 local->SetLocations(nullptr);
1621}
1622
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001623void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001624 // Nothing to do, this is driven by the code generator.
1625}
1626
1627void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001628 LocationSummary* locations =
1629 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001630 switch (store->InputAt(1)->GetType()) {
1631 case Primitive::kPrimBoolean:
1632 case Primitive::kPrimByte:
1633 case Primitive::kPrimChar:
1634 case Primitive::kPrimShort:
1635 case Primitive::kPrimInt:
1636 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001637 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001638 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1639 break;
1640
1641 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001642 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001643 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1644 break;
1645
1646 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001647 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001648 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001649}
1650
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001651void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001652}
1653
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001654void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001655 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001656 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001657 // Handle the long/FP comparisons made in instruction simplification.
1658 switch (cond->InputAt(0)->GetType()) {
1659 case Primitive::kPrimLong:
1660 locations->SetInAt(0, Location::RequiresRegister());
1661 locations->SetInAt(1, Location::Any());
1662 break;
1663 case Primitive::kPrimFloat:
1664 case Primitive::kPrimDouble:
1665 locations->SetInAt(0, Location::RequiresFpuRegister());
1666 locations->SetInAt(1, Location::Any());
1667 break;
1668 default:
1669 locations->SetInAt(0, Location::RequiresRegister());
1670 locations->SetInAt(1, Location::Any());
1671 break;
1672 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001673 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001674 locations->SetOut(Location::RequiresRegister());
1675 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001676}
1677
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001678void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001679 if (!cond->NeedsMaterialization()) {
1680 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001681 }
Mark Mendellc4701932015-04-10 13:18:51 -04001682
1683 LocationSummary* locations = cond->GetLocations();
1684 Location lhs = locations->InAt(0);
1685 Location rhs = locations->InAt(1);
1686 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1687 Label true_label, false_label;
1688
1689 switch (cond->InputAt(0)->GetType()) {
1690 default:
1691 // Integer case.
1692
1693 // Clear output register: setcc only sets the low byte.
1694 __ xorl(reg, reg);
1695
1696 if (rhs.IsRegister()) {
1697 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1698 } else if (rhs.IsConstant()) {
1699 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1700 if (constant == 0) {
1701 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1702 } else {
1703 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1704 }
1705 } else {
1706 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1707 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001708 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001709 return;
1710 case Primitive::kPrimLong:
1711 // Clear output register: setcc only sets the low byte.
1712 __ xorl(reg, reg);
1713
1714 if (rhs.IsRegister()) {
1715 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1716 } else if (rhs.IsConstant()) {
1717 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1718 if (IsInt<32>(value)) {
1719 if (value == 0) {
1720 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1721 } else {
1722 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1723 }
1724 } else {
1725 // Value won't fit in an int.
1726 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1727 }
1728 } else {
1729 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1730 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001731 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001732 return;
1733 case Primitive::kPrimFloat: {
1734 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1735 if (rhs.IsConstant()) {
1736 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1737 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1738 } else if (rhs.IsStackSlot()) {
1739 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1740 } else {
1741 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1742 }
1743 GenerateFPJumps(cond, &true_label, &false_label);
1744 break;
1745 }
1746 case Primitive::kPrimDouble: {
1747 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1748 if (rhs.IsConstant()) {
1749 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1750 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1751 } else if (rhs.IsDoubleStackSlot()) {
1752 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1753 } else {
1754 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1755 }
1756 GenerateFPJumps(cond, &true_label, &false_label);
1757 break;
1758 }
1759 }
1760
1761 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001762 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001763
Roland Levillain4fa13f62015-07-06 18:11:54 +01001764 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001765 __ Bind(&false_label);
1766 __ xorl(reg, reg);
1767 __ jmp(&done_label);
1768
Roland Levillain4fa13f62015-07-06 18:11:54 +01001769 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001770 __ Bind(&true_label);
1771 __ movl(reg, Immediate(1));
1772 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001773}
1774
1775void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001801}
1802
1803void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001805}
1806
1807void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001809}
1810
1811void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001813}
1814
1815void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001817}
1818
1819void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001821}
1822
Aart Bike9f37602015-10-09 11:15:55 -07001823void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001825}
1826
1827void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
1835void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001837}
1838
1839void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001841}
1842
1843void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001845}
1846
1847void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001848 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001849}
1850
1851void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001852 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001853}
1854
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001855void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001856 LocationSummary* locations =
1857 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001858 switch (compare->InputAt(0)->GetType()) {
1859 case Primitive::kPrimLong: {
1860 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001861 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001862 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1863 break;
1864 }
1865 case Primitive::kPrimFloat:
1866 case Primitive::kPrimDouble: {
1867 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001868 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001869 locations->SetOut(Location::RequiresRegister());
1870 break;
1871 }
1872 default:
1873 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1874 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001875}
1876
1877void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001878 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001879 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001880 Location left = locations->InAt(0);
1881 Location right = locations->InAt(1);
1882
Mark Mendell0c9497d2015-08-21 09:30:05 -04001883 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001884 Primitive::Type type = compare->InputAt(0)->GetType();
1885 switch (type) {
1886 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001887 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1888 if (right.IsConstant()) {
1889 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001890 if (IsInt<32>(value)) {
1891 if (value == 0) {
1892 __ testq(left_reg, left_reg);
1893 } else {
1894 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1895 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001896 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001897 // Value won't fit in an int.
1898 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001899 }
Mark Mendell40741f32015-04-20 22:10:34 -04001900 } else if (right.IsDoubleStackSlot()) {
1901 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001902 } else {
1903 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1904 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001905 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001906 }
1907 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001908 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1909 if (right.IsConstant()) {
1910 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1911 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1912 } else if (right.IsStackSlot()) {
1913 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1914 } else {
1915 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1916 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001917 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1918 break;
1919 }
1920 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001921 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1922 if (right.IsConstant()) {
1923 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1924 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1925 } else if (right.IsDoubleStackSlot()) {
1926 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1927 } else {
1928 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1929 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001930 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1931 break;
1932 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001933 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001934 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001935 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001936 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001937 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001938 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001939
Calin Juravle91debbc2014-11-26 19:01:09 +00001940 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001941 __ movl(out, Immediate(1));
1942 __ jmp(&done);
1943
1944 __ Bind(&less);
1945 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001946
1947 __ Bind(&done);
1948}
1949
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001950void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001951 LocationSummary* locations =
1952 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001953 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001954}
1955
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001956void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001957 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001958}
1959
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001960void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1961 LocationSummary* locations =
1962 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1963 locations->SetOut(Location::ConstantLocation(constant));
1964}
1965
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001966void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001967 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001968}
1969
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001970void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001971 LocationSummary* locations =
1972 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001973 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001974}
1975
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001976void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001977 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001978}
1979
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001980void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1981 LocationSummary* locations =
1982 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1983 locations->SetOut(Location::ConstantLocation(constant));
1984}
1985
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001986void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001987 // Will be generated at use site.
1988}
1989
1990void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1991 LocationSummary* locations =
1992 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1993 locations->SetOut(Location::ConstantLocation(constant));
1994}
1995
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001996void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1997 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001998 // Will be generated at use site.
1999}
2000
Calin Juravle27df7582015-04-17 19:12:31 +01002001void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2002 memory_barrier->SetLocations(nullptr);
2003}
2004
2005void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002006 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002007}
2008
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002009void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2010 ret->SetLocations(nullptr);
2011}
2012
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002013void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002014 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002015}
2016
2017void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002018 LocationSummary* locations =
2019 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002020 switch (ret->InputAt(0)->GetType()) {
2021 case Primitive::kPrimBoolean:
2022 case Primitive::kPrimByte:
2023 case Primitive::kPrimChar:
2024 case Primitive::kPrimShort:
2025 case Primitive::kPrimInt:
2026 case Primitive::kPrimNot:
2027 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002028 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002029 break;
2030
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002031 case Primitive::kPrimFloat:
2032 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002033 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002034 break;
2035
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002036 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002037 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002038 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002039}
2040
2041void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2042 if (kIsDebugBuild) {
2043 switch (ret->InputAt(0)->GetType()) {
2044 case Primitive::kPrimBoolean:
2045 case Primitive::kPrimByte:
2046 case Primitive::kPrimChar:
2047 case Primitive::kPrimShort:
2048 case Primitive::kPrimInt:
2049 case Primitive::kPrimNot:
2050 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002051 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002052 break;
2053
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002054 case Primitive::kPrimFloat:
2055 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002057 XMM0);
2058 break;
2059
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002060 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002061 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002062 }
2063 }
2064 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002065}
2066
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002067Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2068 switch (type) {
2069 case Primitive::kPrimBoolean:
2070 case Primitive::kPrimByte:
2071 case Primitive::kPrimChar:
2072 case Primitive::kPrimShort:
2073 case Primitive::kPrimInt:
2074 case Primitive::kPrimNot:
2075 case Primitive::kPrimLong:
2076 return Location::RegisterLocation(RAX);
2077
2078 case Primitive::kPrimVoid:
2079 return Location::NoLocation();
2080
2081 case Primitive::kPrimDouble:
2082 case Primitive::kPrimFloat:
2083 return Location::FpuRegisterLocation(XMM0);
2084 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002085
2086 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002087}
2088
2089Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2090 return Location::RegisterLocation(kMethodRegisterArgument);
2091}
2092
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002093Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002094 switch (type) {
2095 case Primitive::kPrimBoolean:
2096 case Primitive::kPrimByte:
2097 case Primitive::kPrimChar:
2098 case Primitive::kPrimShort:
2099 case Primitive::kPrimInt:
2100 case Primitive::kPrimNot: {
2101 uint32_t index = gp_index_++;
2102 stack_index_++;
2103 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002104 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002105 } else {
2106 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2107 }
2108 }
2109
2110 case Primitive::kPrimLong: {
2111 uint32_t index = gp_index_;
2112 stack_index_ += 2;
2113 if (index < calling_convention.GetNumberOfRegisters()) {
2114 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002115 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002116 } else {
2117 gp_index_ += 2;
2118 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2119 }
2120 }
2121
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002122 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002123 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002124 stack_index_++;
2125 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002126 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002127 } else {
2128 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2129 }
2130 }
2131
2132 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002133 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002134 stack_index_ += 2;
2135 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002136 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002137 } else {
2138 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2139 }
2140 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002141
2142 case Primitive::kPrimVoid:
2143 LOG(FATAL) << "Unexpected parameter type " << type;
2144 break;
2145 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002146 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002147}
2148
Calin Juravle175dc732015-08-25 15:42:32 +01002149void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2150 // The trampoline uses the same calling convention as dex calling conventions,
2151 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2152 // the method_idx.
2153 HandleInvoke(invoke);
2154}
2155
2156void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2157 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2158}
2159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002160void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002161 // When we do not run baseline, explicit clinit checks triggered by static
2162 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2163 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002164
Mark Mendellfb8d2792015-03-31 22:16:59 -04002165 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002166 if (intrinsic.TryDispatch(invoke)) {
2167 return;
2168 }
2169
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002170 HandleInvoke(invoke);
2171}
2172
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002173static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2174 if (invoke->GetLocations()->Intrinsified()) {
2175 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2176 intrinsic.Dispatch(invoke);
2177 return true;
2178 }
2179 return false;
2180}
2181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002182void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002183 // When we do not run baseline, explicit clinit checks triggered by static
2184 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2185 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002186
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002187 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2188 return;
2189 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002190
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002191 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002192 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002193 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002194 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002195}
2196
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002197void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002198 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002199 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002200}
2201
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002202void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002203 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002204 if (intrinsic.TryDispatch(invoke)) {
2205 return;
2206 }
2207
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002208 HandleInvoke(invoke);
2209}
2210
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002211void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002212 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2213 return;
2214 }
2215
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002216 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002217 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002218 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002219}
2220
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002221void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2222 HandleInvoke(invoke);
2223 // Add the hidden argument.
2224 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2225}
2226
2227void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2228 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002229 LocationSummary* locations = invoke->GetLocations();
2230 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2231 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002232 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2233 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002234 Location receiver = locations->InAt(0);
2235 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2236
Roland Levillain0d5a2812015-11-13 10:07:31 +00002237 // Set the hidden argument. This is safe to do this here, as RAX
2238 // won't be modified thereafter, before the `call` instruction.
2239 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002240 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002241
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002242 if (receiver.IsStackSlot()) {
2243 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002244 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002245 __ movl(temp, Address(temp, class_offset));
2246 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002247 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002248 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002249 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002250 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002251 // Instead of simply (possibly) unpoisoning `temp` here, we should
2252 // emit a read barrier for the previous class reference load.
2253 // However this is not required in practice, as this is an
2254 // intermediate/temporary reference and because the current
2255 // concurrent copying collector keeps the from-space memory
2256 // intact/accessible until the end of the marking phase (the
2257 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002258 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002259 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002260 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002261 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002262 __ call(Address(temp,
2263 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264
2265 DCHECK(!codegen_->IsLeafMethod());
2266 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2267}
2268
Roland Levillain88cb1752014-10-20 16:36:47 +01002269void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2270 LocationSummary* locations =
2271 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2272 switch (neg->GetResultType()) {
2273 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002274 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002275 locations->SetInAt(0, Location::RequiresRegister());
2276 locations->SetOut(Location::SameAsFirstInput());
2277 break;
2278
Roland Levillain88cb1752014-10-20 16:36:47 +01002279 case Primitive::kPrimFloat:
2280 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002281 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002282 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002283 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002284 break;
2285
2286 default:
2287 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2288 }
2289}
2290
2291void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2292 LocationSummary* locations = neg->GetLocations();
2293 Location out = locations->Out();
2294 Location in = locations->InAt(0);
2295 switch (neg->GetResultType()) {
2296 case Primitive::kPrimInt:
2297 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002298 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002299 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002300 break;
2301
2302 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002303 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002304 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002305 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002306 break;
2307
Roland Levillain5368c212014-11-27 15:03:41 +00002308 case Primitive::kPrimFloat: {
2309 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002310 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002311 // Implement float negation with an exclusive or with value
2312 // 0x80000000 (mask for bit 31, representing the sign of a
2313 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002314 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002315 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002316 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002317 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002318
Roland Levillain5368c212014-11-27 15:03:41 +00002319 case Primitive::kPrimDouble: {
2320 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002321 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002322 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002323 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002324 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002325 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002326 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002327 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002328 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002329
2330 default:
2331 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2332 }
2333}
2334
Roland Levillaindff1f282014-11-05 14:15:05 +00002335void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2336 LocationSummary* locations =
2337 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2338 Primitive::Type result_type = conversion->GetResultType();
2339 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002340 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002341
David Brazdilb2bd1c52015-03-25 11:17:37 +00002342 // The Java language does not allow treating boolean as an integral type but
2343 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002344
Roland Levillaindff1f282014-11-05 14:15:05 +00002345 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002346 case Primitive::kPrimByte:
2347 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002348 case Primitive::kPrimBoolean:
2349 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002350 case Primitive::kPrimShort:
2351 case Primitive::kPrimInt:
2352 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002353 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002354 locations->SetInAt(0, Location::Any());
2355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2356 break;
2357
2358 default:
2359 LOG(FATAL) << "Unexpected type conversion from " << input_type
2360 << " to " << result_type;
2361 }
2362 break;
2363
Roland Levillain01a8d712014-11-14 16:27:39 +00002364 case Primitive::kPrimShort:
2365 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002366 case Primitive::kPrimBoolean:
2367 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002368 case Primitive::kPrimByte:
2369 case Primitive::kPrimInt:
2370 case Primitive::kPrimChar:
2371 // Processing a Dex `int-to-short' instruction.
2372 locations->SetInAt(0, Location::Any());
2373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2374 break;
2375
2376 default:
2377 LOG(FATAL) << "Unexpected type conversion from " << input_type
2378 << " to " << result_type;
2379 }
2380 break;
2381
Roland Levillain946e1432014-11-11 17:35:19 +00002382 case Primitive::kPrimInt:
2383 switch (input_type) {
2384 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002385 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002386 locations->SetInAt(0, Location::Any());
2387 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2388 break;
2389
2390 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002391 // Processing a Dex `float-to-int' instruction.
2392 locations->SetInAt(0, Location::RequiresFpuRegister());
2393 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002394 break;
2395
Roland Levillain946e1432014-11-11 17:35:19 +00002396 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002397 // Processing a Dex `double-to-int' instruction.
2398 locations->SetInAt(0, Location::RequiresFpuRegister());
2399 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002400 break;
2401
2402 default:
2403 LOG(FATAL) << "Unexpected type conversion from " << input_type
2404 << " to " << result_type;
2405 }
2406 break;
2407
Roland Levillaindff1f282014-11-05 14:15:05 +00002408 case Primitive::kPrimLong:
2409 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002410 case Primitive::kPrimBoolean:
2411 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002412 case Primitive::kPrimByte:
2413 case Primitive::kPrimShort:
2414 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002415 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002416 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002417 // TODO: We would benefit from a (to-be-implemented)
2418 // Location::RegisterOrStackSlot requirement for this input.
2419 locations->SetInAt(0, Location::RequiresRegister());
2420 locations->SetOut(Location::RequiresRegister());
2421 break;
2422
2423 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002424 // Processing a Dex `float-to-long' instruction.
2425 locations->SetInAt(0, Location::RequiresFpuRegister());
2426 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002427 break;
2428
Roland Levillaindff1f282014-11-05 14:15:05 +00002429 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002430 // Processing a Dex `double-to-long' instruction.
2431 locations->SetInAt(0, Location::RequiresFpuRegister());
2432 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002433 break;
2434
2435 default:
2436 LOG(FATAL) << "Unexpected type conversion from " << input_type
2437 << " to " << result_type;
2438 }
2439 break;
2440
Roland Levillain981e4542014-11-14 11:47:14 +00002441 case Primitive::kPrimChar:
2442 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002443 case Primitive::kPrimBoolean:
2444 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002445 case Primitive::kPrimByte:
2446 case Primitive::kPrimShort:
2447 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002448 // Processing a Dex `int-to-char' instruction.
2449 locations->SetInAt(0, Location::Any());
2450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2451 break;
2452
2453 default:
2454 LOG(FATAL) << "Unexpected type conversion from " << input_type
2455 << " to " << result_type;
2456 }
2457 break;
2458
Roland Levillaindff1f282014-11-05 14:15:05 +00002459 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002460 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002461 case Primitive::kPrimBoolean:
2462 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002463 case Primitive::kPrimByte:
2464 case Primitive::kPrimShort:
2465 case Primitive::kPrimInt:
2466 case Primitive::kPrimChar:
2467 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002468 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002469 locations->SetOut(Location::RequiresFpuRegister());
2470 break;
2471
2472 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002473 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002474 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002475 locations->SetOut(Location::RequiresFpuRegister());
2476 break;
2477
Roland Levillaincff13742014-11-17 14:32:17 +00002478 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002479 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002480 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002481 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002482 break;
2483
2484 default:
2485 LOG(FATAL) << "Unexpected type conversion from " << input_type
2486 << " to " << result_type;
2487 };
2488 break;
2489
Roland Levillaindff1f282014-11-05 14:15:05 +00002490 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002491 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002492 case Primitive::kPrimBoolean:
2493 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002494 case Primitive::kPrimByte:
2495 case Primitive::kPrimShort:
2496 case Primitive::kPrimInt:
2497 case Primitive::kPrimChar:
2498 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002499 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002500 locations->SetOut(Location::RequiresFpuRegister());
2501 break;
2502
2503 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002504 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002505 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002506 locations->SetOut(Location::RequiresFpuRegister());
2507 break;
2508
Roland Levillaincff13742014-11-17 14:32:17 +00002509 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002510 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002511 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002512 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002513 break;
2514
2515 default:
2516 LOG(FATAL) << "Unexpected type conversion from " << input_type
2517 << " to " << result_type;
2518 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002519 break;
2520
2521 default:
2522 LOG(FATAL) << "Unexpected type conversion from " << input_type
2523 << " to " << result_type;
2524 }
2525}
2526
2527void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2528 LocationSummary* locations = conversion->GetLocations();
2529 Location out = locations->Out();
2530 Location in = locations->InAt(0);
2531 Primitive::Type result_type = conversion->GetResultType();
2532 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002533 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002534 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002535 case Primitive::kPrimByte:
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 Levillain51d3fc42014-11-13 14:11:42 +00002539 case Primitive::kPrimShort:
2540 case Primitive::kPrimInt:
2541 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002542 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002543 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002544 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002545 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002546 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002547 Address(CpuRegister(RSP), in.GetStackIndex()));
2548 } else {
2549 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002550 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002551 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2552 }
2553 break;
2554
2555 default:
2556 LOG(FATAL) << "Unexpected type conversion from " << input_type
2557 << " to " << result_type;
2558 }
2559 break;
2560
Roland Levillain01a8d712014-11-14 16:27:39 +00002561 case Primitive::kPrimShort:
2562 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002563 case Primitive::kPrimBoolean:
2564 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002565 case Primitive::kPrimByte:
2566 case Primitive::kPrimInt:
2567 case Primitive::kPrimChar:
2568 // Processing a Dex `int-to-short' instruction.
2569 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002570 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002571 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002572 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002573 Address(CpuRegister(RSP), in.GetStackIndex()));
2574 } else {
2575 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002576 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002577 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2578 }
2579 break;
2580
2581 default:
2582 LOG(FATAL) << "Unexpected type conversion from " << input_type
2583 << " to " << result_type;
2584 }
2585 break;
2586
Roland Levillain946e1432014-11-11 17:35:19 +00002587 case Primitive::kPrimInt:
2588 switch (input_type) {
2589 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002590 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002591 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002593 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002595 Address(CpuRegister(RSP), in.GetStackIndex()));
2596 } else {
2597 DCHECK(in.IsConstant());
2598 DCHECK(in.GetConstant()->IsLongConstant());
2599 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002600 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002601 }
2602 break;
2603
Roland Levillain3f8f9362014-12-02 17:45:01 +00002604 case Primitive::kPrimFloat: {
2605 // Processing a Dex `float-to-int' instruction.
2606 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2607 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002608 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002609
2610 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002611 // if input >= (float)INT_MAX goto done
2612 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002613 __ j(kAboveEqual, &done);
2614 // if input == NaN goto nan
2615 __ j(kUnordered, &nan);
2616 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002617 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002618 __ jmp(&done);
2619 __ Bind(&nan);
2620 // output = 0
2621 __ xorl(output, output);
2622 __ Bind(&done);
2623 break;
2624 }
2625
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002626 case Primitive::kPrimDouble: {
2627 // Processing a Dex `double-to-int' instruction.
2628 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2629 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002630 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002631
2632 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002633 // if input >= (double)INT_MAX goto done
2634 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002635 __ j(kAboveEqual, &done);
2636 // if input == NaN goto nan
2637 __ j(kUnordered, &nan);
2638 // output = double-to-int-truncate(input)
2639 __ cvttsd2si(output, input);
2640 __ jmp(&done);
2641 __ Bind(&nan);
2642 // output = 0
2643 __ xorl(output, output);
2644 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002645 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002646 }
Roland Levillain946e1432014-11-11 17:35:19 +00002647
2648 default:
2649 LOG(FATAL) << "Unexpected type conversion from " << input_type
2650 << " to " << result_type;
2651 }
2652 break;
2653
Roland Levillaindff1f282014-11-05 14:15:05 +00002654 case Primitive::kPrimLong:
2655 switch (input_type) {
2656 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002657 case Primitive::kPrimBoolean:
2658 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002659 case Primitive::kPrimByte:
2660 case Primitive::kPrimShort:
2661 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002662 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002663 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002664 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002665 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002666 break;
2667
Roland Levillain624279f2014-12-04 11:54:28 +00002668 case Primitive::kPrimFloat: {
2669 // Processing a Dex `float-to-long' instruction.
2670 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2671 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002672 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002673
Mark Mendell92e83bf2015-05-07 11:25:03 -04002674 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002675 // if input >= (float)LONG_MAX goto done
2676 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002677 __ j(kAboveEqual, &done);
2678 // if input == NaN goto nan
2679 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002680 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002681 __ cvttss2si(output, input, true);
2682 __ jmp(&done);
2683 __ Bind(&nan);
2684 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002685 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002686 __ Bind(&done);
2687 break;
2688 }
2689
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002690 case Primitive::kPrimDouble: {
2691 // Processing a Dex `double-to-long' instruction.
2692 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2693 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002694 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002695
Mark Mendell92e83bf2015-05-07 11:25:03 -04002696 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002697 // if input >= (double)LONG_MAX goto done
2698 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002699 __ j(kAboveEqual, &done);
2700 // if input == NaN goto nan
2701 __ j(kUnordered, &nan);
2702 // output = double-to-long-truncate(input)
2703 __ cvttsd2si(output, input, true);
2704 __ jmp(&done);
2705 __ Bind(&nan);
2706 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002707 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002708 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002709 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002710 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002711
2712 default:
2713 LOG(FATAL) << "Unexpected type conversion from " << input_type
2714 << " to " << result_type;
2715 }
2716 break;
2717
Roland Levillain981e4542014-11-14 11:47:14 +00002718 case Primitive::kPrimChar:
2719 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002720 case Primitive::kPrimBoolean:
2721 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002722 case Primitive::kPrimByte:
2723 case Primitive::kPrimShort:
2724 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002725 // Processing a Dex `int-to-char' instruction.
2726 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002727 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002728 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002729 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002730 Address(CpuRegister(RSP), in.GetStackIndex()));
2731 } else {
2732 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002733 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002734 Immediate(static_cast<uint16_t>(
2735 in.GetConstant()->AsIntConstant()->GetValue())));
Roland Levillain981e4542014-11-14 11:47:14 +00002736 }
2737 break;
2738
2739 default:
2740 LOG(FATAL) << "Unexpected type conversion from " << input_type
2741 << " to " << result_type;
2742 }
2743 break;
2744
Roland Levillaindff1f282014-11-05 14:15:05 +00002745 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002746 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002747 case Primitive::kPrimBoolean:
2748 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002749 case Primitive::kPrimByte:
2750 case Primitive::kPrimShort:
2751 case Primitive::kPrimInt:
2752 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002753 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002754 if (in.IsRegister()) {
2755 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2756 } else if (in.IsConstant()) {
2757 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2758 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2759 if (v == 0) {
2760 __ xorps(dest, dest);
2761 } else {
2762 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2763 }
2764 } else {
2765 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2766 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2767 }
Roland Levillaincff13742014-11-17 14:32:17 +00002768 break;
2769
2770 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002771 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002772 if (in.IsRegister()) {
2773 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2774 } else if (in.IsConstant()) {
2775 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2776 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2777 if (v == 0) {
2778 __ xorps(dest, dest);
2779 } else {
2780 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2781 }
2782 } else {
2783 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2784 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2785 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002786 break;
2787
Roland Levillaincff13742014-11-17 14:32:17 +00002788 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002789 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002790 if (in.IsFpuRegister()) {
2791 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2792 } else if (in.IsConstant()) {
2793 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2794 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2795 if (bit_cast<int64_t, double>(v) == 0) {
2796 __ xorps(dest, dest);
2797 } else {
2798 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2799 }
2800 } else {
2801 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2802 Address(CpuRegister(RSP), in.GetStackIndex()));
2803 }
Roland Levillaincff13742014-11-17 14:32:17 +00002804 break;
2805
2806 default:
2807 LOG(FATAL) << "Unexpected type conversion from " << input_type
2808 << " to " << result_type;
2809 };
2810 break;
2811
Roland Levillaindff1f282014-11-05 14:15:05 +00002812 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002813 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002814 case Primitive::kPrimBoolean:
2815 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002816 case Primitive::kPrimByte:
2817 case Primitive::kPrimShort:
2818 case Primitive::kPrimInt:
2819 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002820 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002821 if (in.IsRegister()) {
2822 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2823 } else if (in.IsConstant()) {
2824 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2825 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2826 if (v == 0) {
2827 __ xorpd(dest, dest);
2828 } else {
2829 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2830 }
2831 } else {
2832 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2833 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2834 }
Roland Levillaincff13742014-11-17 14:32:17 +00002835 break;
2836
2837 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002838 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002839 if (in.IsRegister()) {
2840 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2841 } else if (in.IsConstant()) {
2842 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2843 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2844 if (v == 0) {
2845 __ xorpd(dest, dest);
2846 } else {
2847 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2848 }
2849 } else {
2850 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2851 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2852 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002853 break;
2854
Roland Levillaincff13742014-11-17 14:32:17 +00002855 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002856 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002857 if (in.IsFpuRegister()) {
2858 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2859 } else if (in.IsConstant()) {
2860 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2861 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2862 if (bit_cast<int32_t, float>(v) == 0) {
2863 __ xorpd(dest, dest);
2864 } else {
2865 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2866 }
2867 } else {
2868 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2869 Address(CpuRegister(RSP), in.GetStackIndex()));
2870 }
Roland Levillaincff13742014-11-17 14:32:17 +00002871 break;
2872
2873 default:
2874 LOG(FATAL) << "Unexpected type conversion from " << input_type
2875 << " to " << result_type;
2876 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002877 break;
2878
2879 default:
2880 LOG(FATAL) << "Unexpected type conversion from " << input_type
2881 << " to " << result_type;
2882 }
2883}
2884
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002885void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002886 LocationSummary* locations =
2887 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002888 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002889 case Primitive::kPrimInt: {
2890 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002891 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2892 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002893 break;
2894 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002895
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002896 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002897 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002898 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002899 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002900 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002901 break;
2902 }
2903
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002904 case Primitive::kPrimDouble:
2905 case Primitive::kPrimFloat: {
2906 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002907 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002908 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002909 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002910 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002911
2912 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002913 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002914 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002915}
2916
2917void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2918 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002919 Location first = locations->InAt(0);
2920 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002921 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002922
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002923 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002924 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002925 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002926 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2927 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002928 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2929 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002930 } else {
2931 __ leal(out.AsRegister<CpuRegister>(), Address(
2932 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2933 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002934 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002935 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2936 __ addl(out.AsRegister<CpuRegister>(),
2937 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2938 } else {
2939 __ leal(out.AsRegister<CpuRegister>(), Address(
2940 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2941 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002942 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002943 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002944 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002945 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002946 break;
2947 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002948
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002949 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002950 if (second.IsRegister()) {
2951 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2952 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002953 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2954 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002955 } else {
2956 __ leaq(out.AsRegister<CpuRegister>(), Address(
2957 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2958 }
2959 } else {
2960 DCHECK(second.IsConstant());
2961 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2962 int32_t int32_value = Low32Bits(value);
2963 DCHECK_EQ(int32_value, value);
2964 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2965 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2966 } else {
2967 __ leaq(out.AsRegister<CpuRegister>(), Address(
2968 first.AsRegister<CpuRegister>(), int32_value));
2969 }
2970 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002971 break;
2972 }
2973
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002975 if (second.IsFpuRegister()) {
2976 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2977 } else if (second.IsConstant()) {
2978 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002979 codegen_->LiteralFloatAddress(
2980 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002981 } else {
2982 DCHECK(second.IsStackSlot());
2983 __ addss(first.AsFpuRegister<XmmRegister>(),
2984 Address(CpuRegister(RSP), second.GetStackIndex()));
2985 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002986 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002987 }
2988
2989 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002990 if (second.IsFpuRegister()) {
2991 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2992 } else if (second.IsConstant()) {
2993 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002994 codegen_->LiteralDoubleAddress(
2995 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002996 } else {
2997 DCHECK(second.IsDoubleStackSlot());
2998 __ addsd(first.AsFpuRegister<XmmRegister>(),
2999 Address(CpuRegister(RSP), second.GetStackIndex()));
3000 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003001 break;
3002 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003003
3004 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003005 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003006 }
3007}
3008
3009void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003010 LocationSummary* locations =
3011 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003012 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003013 case Primitive::kPrimInt: {
3014 locations->SetInAt(0, Location::RequiresRegister());
3015 locations->SetInAt(1, Location::Any());
3016 locations->SetOut(Location::SameAsFirstInput());
3017 break;
3018 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003019 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003020 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003021 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003022 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003023 break;
3024 }
Calin Juravle11351682014-10-23 15:38:15 +01003025 case Primitive::kPrimFloat:
3026 case Primitive::kPrimDouble: {
3027 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003028 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003029 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003030 break;
Calin Juravle11351682014-10-23 15:38:15 +01003031 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003032 default:
Calin Juravle11351682014-10-23 15:38:15 +01003033 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003034 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035}
3036
3037void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3038 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003039 Location first = locations->InAt(0);
3040 Location second = locations->InAt(1);
3041 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003042 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003043 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003044 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003046 } else if (second.IsConstant()) {
3047 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003048 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003049 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003050 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003051 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003052 break;
3053 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003054 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003055 if (second.IsConstant()) {
3056 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3057 DCHECK(IsInt<32>(value));
3058 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3059 } else {
3060 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3061 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003062 break;
3063 }
3064
Calin Juravle11351682014-10-23 15:38:15 +01003065 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003066 if (second.IsFpuRegister()) {
3067 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3068 } else if (second.IsConstant()) {
3069 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003070 codegen_->LiteralFloatAddress(
3071 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003072 } else {
3073 DCHECK(second.IsStackSlot());
3074 __ subss(first.AsFpuRegister<XmmRegister>(),
3075 Address(CpuRegister(RSP), second.GetStackIndex()));
3076 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003077 break;
Calin Juravle11351682014-10-23 15:38:15 +01003078 }
3079
3080 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003081 if (second.IsFpuRegister()) {
3082 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3083 } else if (second.IsConstant()) {
3084 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003085 codegen_->LiteralDoubleAddress(
3086 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003087 } else {
3088 DCHECK(second.IsDoubleStackSlot());
3089 __ subsd(first.AsFpuRegister<XmmRegister>(),
3090 Address(CpuRegister(RSP), second.GetStackIndex()));
3091 }
Calin Juravle11351682014-10-23 15:38:15 +01003092 break;
3093 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003094
3095 default:
Calin Juravle11351682014-10-23 15:38:15 +01003096 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003097 }
3098}
3099
Calin Juravle34bacdf2014-10-07 20:23:36 +01003100void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3101 LocationSummary* locations =
3102 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3103 switch (mul->GetResultType()) {
3104 case Primitive::kPrimInt: {
3105 locations->SetInAt(0, Location::RequiresRegister());
3106 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003107 if (mul->InputAt(1)->IsIntConstant()) {
3108 // Can use 3 operand multiply.
3109 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3110 } else {
3111 locations->SetOut(Location::SameAsFirstInput());
3112 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003113 break;
3114 }
3115 case Primitive::kPrimLong: {
3116 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003117 locations->SetInAt(1, Location::Any());
3118 if (mul->InputAt(1)->IsLongConstant() &&
3119 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003120 // Can use 3 operand multiply.
3121 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3122 } else {
3123 locations->SetOut(Location::SameAsFirstInput());
3124 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003125 break;
3126 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003127 case Primitive::kPrimFloat:
3128 case Primitive::kPrimDouble: {
3129 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003130 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003131 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003132 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003133 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003134
3135 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003136 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003137 }
3138}
3139
3140void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3141 LocationSummary* locations = mul->GetLocations();
3142 Location first = locations->InAt(0);
3143 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003144 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003145 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003146 case Primitive::kPrimInt:
3147 // The constant may have ended up in a register, so test explicitly to avoid
3148 // problems where the output may not be the same as the first operand.
3149 if (mul->InputAt(1)->IsIntConstant()) {
3150 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3151 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3152 } else if (second.IsRegister()) {
3153 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003154 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003155 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003156 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003157 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003158 __ imull(first.AsRegister<CpuRegister>(),
3159 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160 }
3161 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003162 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003163 // The constant may have ended up in a register, so test explicitly to avoid
3164 // problems where the output may not be the same as the first operand.
3165 if (mul->InputAt(1)->IsLongConstant()) {
3166 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3167 if (IsInt<32>(value)) {
3168 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3169 Immediate(static_cast<int32_t>(value)));
3170 } else {
3171 // Have to use the constant area.
3172 DCHECK(first.Equals(out));
3173 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3174 }
3175 } else if (second.IsRegister()) {
3176 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003177 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003178 } else {
3179 DCHECK(second.IsDoubleStackSlot());
3180 DCHECK(first.Equals(out));
3181 __ imulq(first.AsRegister<CpuRegister>(),
3182 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003183 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 break;
3185 }
3186
Calin Juravleb5bfa962014-10-21 18:02:24 +01003187 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003188 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003189 if (second.IsFpuRegister()) {
3190 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3191 } else if (second.IsConstant()) {
3192 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003193 codegen_->LiteralFloatAddress(
3194 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003195 } else {
3196 DCHECK(second.IsStackSlot());
3197 __ mulss(first.AsFpuRegister<XmmRegister>(),
3198 Address(CpuRegister(RSP), second.GetStackIndex()));
3199 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003201 }
3202
3203 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003204 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003205 if (second.IsFpuRegister()) {
3206 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3207 } else if (second.IsConstant()) {
3208 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003209 codegen_->LiteralDoubleAddress(
3210 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003211 } else {
3212 DCHECK(second.IsDoubleStackSlot());
3213 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3214 Address(CpuRegister(RSP), second.GetStackIndex()));
3215 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003216 break;
3217 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003218
3219 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003220 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003221 }
3222}
3223
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003224void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3225 uint32_t stack_adjustment, bool is_float) {
3226 if (source.IsStackSlot()) {
3227 DCHECK(is_float);
3228 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3229 } else if (source.IsDoubleStackSlot()) {
3230 DCHECK(!is_float);
3231 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3232 } else {
3233 // Write the value to the temporary location on the stack and load to FP stack.
3234 if (is_float) {
3235 Location stack_temp = Location::StackSlot(temp_offset);
3236 codegen_->Move(stack_temp, source);
3237 __ flds(Address(CpuRegister(RSP), temp_offset));
3238 } else {
3239 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3240 codegen_->Move(stack_temp, source);
3241 __ fldl(Address(CpuRegister(RSP), temp_offset));
3242 }
3243 }
3244}
3245
3246void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3247 Primitive::Type type = rem->GetResultType();
3248 bool is_float = type == Primitive::kPrimFloat;
3249 size_t elem_size = Primitive::ComponentSize(type);
3250 LocationSummary* locations = rem->GetLocations();
3251 Location first = locations->InAt(0);
3252 Location second = locations->InAt(1);
3253 Location out = locations->Out();
3254
3255 // Create stack space for 2 elements.
3256 // TODO: enhance register allocator to ask for stack temporaries.
3257 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3258
3259 // Load the values to the FP stack in reverse order, using temporaries if needed.
3260 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3261 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3262
3263 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003264 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003265 __ Bind(&retry);
3266 __ fprem();
3267
3268 // Move FP status to AX.
3269 __ fstsw();
3270
3271 // And see if the argument reduction is complete. This is signaled by the
3272 // C2 FPU flag bit set to 0.
3273 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3274 __ j(kNotEqual, &retry);
3275
3276 // We have settled on the final value. Retrieve it into an XMM register.
3277 // Store FP top of stack to real stack.
3278 if (is_float) {
3279 __ fsts(Address(CpuRegister(RSP), 0));
3280 } else {
3281 __ fstl(Address(CpuRegister(RSP), 0));
3282 }
3283
3284 // Pop the 2 items from the FP stack.
3285 __ fucompp();
3286
3287 // Load the value from the stack into an XMM register.
3288 DCHECK(out.IsFpuRegister()) << out;
3289 if (is_float) {
3290 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3291 } else {
3292 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3293 }
3294
3295 // And remove the temporary stack space we allocated.
3296 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3297}
3298
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003299void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3300 DCHECK(instruction->IsDiv() || instruction->IsRem());
3301
3302 LocationSummary* locations = instruction->GetLocations();
3303 Location second = locations->InAt(1);
3304 DCHECK(second.IsConstant());
3305
3306 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3307 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003308 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003309
3310 DCHECK(imm == 1 || imm == -1);
3311
3312 switch (instruction->GetResultType()) {
3313 case Primitive::kPrimInt: {
3314 if (instruction->IsRem()) {
3315 __ xorl(output_register, output_register);
3316 } else {
3317 __ movl(output_register, input_register);
3318 if (imm == -1) {
3319 __ negl(output_register);
3320 }
3321 }
3322 break;
3323 }
3324
3325 case Primitive::kPrimLong: {
3326 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003327 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003328 } else {
3329 __ movq(output_register, input_register);
3330 if (imm == -1) {
3331 __ negq(output_register);
3332 }
3333 }
3334 break;
3335 }
3336
3337 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003338 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003339 }
3340}
3341
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003342void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003343 LocationSummary* locations = instruction->GetLocations();
3344 Location second = locations->InAt(1);
3345
3346 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3347 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3348
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003349 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003350
3351 DCHECK(IsPowerOfTwo(std::abs(imm)));
3352
3353 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3354
3355 if (instruction->GetResultType() == Primitive::kPrimInt) {
3356 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
3357 __ testl(numerator, numerator);
3358 __ cmov(kGreaterEqual, tmp, numerator);
3359 int shift = CTZ(imm);
3360 __ sarl(tmp, Immediate(shift));
3361
3362 if (imm < 0) {
3363 __ negl(tmp);
3364 }
3365
3366 __ movl(output_register, tmp);
3367 } else {
3368 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3369 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3370
Mark Mendell92e83bf2015-05-07 11:25:03 -04003371 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003372 __ addq(rdx, numerator);
3373 __ testq(numerator, numerator);
3374 __ cmov(kGreaterEqual, rdx, numerator);
3375 int shift = CTZ(imm);
3376 __ sarq(rdx, Immediate(shift));
3377
3378 if (imm < 0) {
3379 __ negq(rdx);
3380 }
3381
3382 __ movq(output_register, rdx);
3383 }
3384}
3385
3386void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3387 DCHECK(instruction->IsDiv() || instruction->IsRem());
3388
3389 LocationSummary* locations = instruction->GetLocations();
3390 Location second = locations->InAt(1);
3391
3392 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3393 : locations->GetTemp(0).AsRegister<CpuRegister>();
3394 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3395 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3396 : locations->Out().AsRegister<CpuRegister>();
3397 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3398
3399 DCHECK_EQ(RAX, eax.AsRegister());
3400 DCHECK_EQ(RDX, edx.AsRegister());
3401 if (instruction->IsDiv()) {
3402 DCHECK_EQ(RAX, out.AsRegister());
3403 } else {
3404 DCHECK_EQ(RDX, out.AsRegister());
3405 }
3406
3407 int64_t magic;
3408 int shift;
3409
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003410 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003411 if (instruction->GetResultType() == Primitive::kPrimInt) {
3412 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3413
3414 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3415
3416 __ movl(numerator, eax);
3417
Mark Mendell0c9497d2015-08-21 09:30:05 -04003418 NearLabel no_div;
3419 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003420 __ testl(eax, eax);
3421 __ j(kNotEqual, &no_div);
3422
3423 __ xorl(out, out);
3424 __ jmp(&end);
3425
3426 __ Bind(&no_div);
3427
3428 __ movl(eax, Immediate(magic));
3429 __ imull(numerator);
3430
3431 if (imm > 0 && magic < 0) {
3432 __ addl(edx, numerator);
3433 } else if (imm < 0 && magic > 0) {
3434 __ subl(edx, numerator);
3435 }
3436
3437 if (shift != 0) {
3438 __ sarl(edx, Immediate(shift));
3439 }
3440
3441 __ movl(eax, edx);
3442 __ shrl(edx, Immediate(31));
3443 __ addl(edx, eax);
3444
3445 if (instruction->IsRem()) {
3446 __ movl(eax, numerator);
3447 __ imull(edx, Immediate(imm));
3448 __ subl(eax, edx);
3449 __ movl(edx, eax);
3450 } else {
3451 __ movl(eax, edx);
3452 }
3453 __ Bind(&end);
3454 } else {
3455 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3456
3457 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3458
3459 CpuRegister rax = eax;
3460 CpuRegister rdx = edx;
3461
3462 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3463
3464 // Save the numerator.
3465 __ movq(numerator, rax);
3466
3467 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003468 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003469
3470 // RDX:RAX = magic * numerator
3471 __ imulq(numerator);
3472
3473 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003474 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003475 __ addq(rdx, numerator);
3476 } else if (imm < 0 && magic > 0) {
3477 // RDX -= numerator
3478 __ subq(rdx, numerator);
3479 }
3480
3481 // Shift if needed.
3482 if (shift != 0) {
3483 __ sarq(rdx, Immediate(shift));
3484 }
3485
3486 // RDX += 1 if RDX < 0
3487 __ movq(rax, rdx);
3488 __ shrq(rdx, Immediate(63));
3489 __ addq(rdx, rax);
3490
3491 if (instruction->IsRem()) {
3492 __ movq(rax, numerator);
3493
3494 if (IsInt<32>(imm)) {
3495 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3496 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003497 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 }
3499
3500 __ subq(rax, rdx);
3501 __ movq(rdx, rax);
3502 } else {
3503 __ movq(rax, rdx);
3504 }
3505 }
3506}
3507
Calin Juravlebacfec32014-11-14 15:54:36 +00003508void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3509 DCHECK(instruction->IsDiv() || instruction->IsRem());
3510 Primitive::Type type = instruction->GetResultType();
3511 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3512
3513 bool is_div = instruction->IsDiv();
3514 LocationSummary* locations = instruction->GetLocations();
3515
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003516 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3517 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003518
Roland Levillain271ab9c2014-11-27 15:23:57 +00003519 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003520 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003521
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003522 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003523 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003524
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 if (imm == 0) {
3526 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3527 } else if (imm == 1 || imm == -1) {
3528 DivRemOneOrMinusOne(instruction);
3529 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003530 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003531 } else {
3532 DCHECK(imm <= -2 || imm >= 2);
3533 GenerateDivRemWithAnyConstant(instruction);
3534 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003535 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003536 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003537 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3538 out.AsRegister(), type, is_div);
3539 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003540
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3542 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3543 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3544 // so it's safe to just use negl instead of more complex comparisons.
3545 if (type == Primitive::kPrimInt) {
3546 __ cmpl(second_reg, Immediate(-1));
3547 __ j(kEqual, slow_path->GetEntryLabel());
3548 // edx:eax <- sign-extended of eax
3549 __ cdq();
3550 // eax = quotient, edx = remainder
3551 __ idivl(second_reg);
3552 } else {
3553 __ cmpq(second_reg, Immediate(-1));
3554 __ j(kEqual, slow_path->GetEntryLabel());
3555 // rdx:rax <- sign-extended of rax
3556 __ cqo();
3557 // rax = quotient, rdx = remainder
3558 __ idivq(second_reg);
3559 }
3560 __ Bind(slow_path->GetExitLabel());
3561 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003562}
3563
Calin Juravle7c4954d2014-10-28 16:57:40 +00003564void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3565 LocationSummary* locations =
3566 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3567 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003568 case Primitive::kPrimInt:
3569 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003570 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003571 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003572 locations->SetOut(Location::SameAsFirstInput());
3573 // Intel uses edx:eax as the dividend.
3574 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003575 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3576 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3577 // output and request another temp.
3578 if (div->InputAt(1)->IsConstant()) {
3579 locations->AddTemp(Location::RequiresRegister());
3580 }
Calin Juravled0d48522014-11-04 16:40:20 +00003581 break;
3582 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003583
Calin Juravle7c4954d2014-10-28 16:57:40 +00003584 case Primitive::kPrimFloat:
3585 case Primitive::kPrimDouble: {
3586 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003587 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003588 locations->SetOut(Location::SameAsFirstInput());
3589 break;
3590 }
3591
3592 default:
3593 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3594 }
3595}
3596
3597void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3598 LocationSummary* locations = div->GetLocations();
3599 Location first = locations->InAt(0);
3600 Location second = locations->InAt(1);
3601 DCHECK(first.Equals(locations->Out()));
3602
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003603 Primitive::Type type = div->GetResultType();
3604 switch (type) {
3605 case Primitive::kPrimInt:
3606 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003607 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003608 break;
3609 }
3610
Calin Juravle7c4954d2014-10-28 16:57:40 +00003611 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003612 if (second.IsFpuRegister()) {
3613 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3614 } else if (second.IsConstant()) {
3615 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003616 codegen_->LiteralFloatAddress(
3617 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003618 } else {
3619 DCHECK(second.IsStackSlot());
3620 __ divss(first.AsFpuRegister<XmmRegister>(),
3621 Address(CpuRegister(RSP), second.GetStackIndex()));
3622 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003623 break;
3624 }
3625
3626 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003627 if (second.IsFpuRegister()) {
3628 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3629 } else if (second.IsConstant()) {
3630 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003631 codegen_->LiteralDoubleAddress(
3632 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003633 } else {
3634 DCHECK(second.IsDoubleStackSlot());
3635 __ divsd(first.AsFpuRegister<XmmRegister>(),
3636 Address(CpuRegister(RSP), second.GetStackIndex()));
3637 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003638 break;
3639 }
3640
3641 default:
3642 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3643 }
3644}
3645
Calin Juravlebacfec32014-11-14 15:54:36 +00003646void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003647 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003648 LocationSummary* locations =
3649 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003650
3651 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003652 case Primitive::kPrimInt:
3653 case Primitive::kPrimLong: {
3654 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003655 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003656 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3657 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003658 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3659 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3660 // output and request another temp.
3661 if (rem->InputAt(1)->IsConstant()) {
3662 locations->AddTemp(Location::RequiresRegister());
3663 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003664 break;
3665 }
3666
3667 case Primitive::kPrimFloat:
3668 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003669 locations->SetInAt(0, Location::Any());
3670 locations->SetInAt(1, Location::Any());
3671 locations->SetOut(Location::RequiresFpuRegister());
3672 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003673 break;
3674 }
3675
3676 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003677 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003678 }
3679}
3680
3681void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3682 Primitive::Type type = rem->GetResultType();
3683 switch (type) {
3684 case Primitive::kPrimInt:
3685 case Primitive::kPrimLong: {
3686 GenerateDivRemIntegral(rem);
3687 break;
3688 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003689 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003690 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003691 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003692 break;
3693 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003694 default:
3695 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3696 }
3697}
3698
Calin Juravled0d48522014-11-04 16:40:20 +00003699void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003700 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3701 ? LocationSummary::kCallOnSlowPath
3702 : LocationSummary::kNoCall;
3703 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003704 locations->SetInAt(0, Location::Any());
3705 if (instruction->HasUses()) {
3706 locations->SetOut(Location::SameAsFirstInput());
3707 }
3708}
3709
3710void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003711 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003712 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3713 codegen_->AddSlowPath(slow_path);
3714
3715 LocationSummary* locations = instruction->GetLocations();
3716 Location value = locations->InAt(0);
3717
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003718 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003719 case Primitive::kPrimByte:
3720 case Primitive::kPrimChar:
3721 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003722 case Primitive::kPrimInt: {
3723 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003724 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003725 __ j(kEqual, slow_path->GetEntryLabel());
3726 } else if (value.IsStackSlot()) {
3727 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3728 __ j(kEqual, slow_path->GetEntryLabel());
3729 } else {
3730 DCHECK(value.IsConstant()) << value;
3731 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3732 __ jmp(slow_path->GetEntryLabel());
3733 }
3734 }
3735 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003736 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003737 case Primitive::kPrimLong: {
3738 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003739 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003740 __ j(kEqual, slow_path->GetEntryLabel());
3741 } else if (value.IsDoubleStackSlot()) {
3742 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3743 __ j(kEqual, slow_path->GetEntryLabel());
3744 } else {
3745 DCHECK(value.IsConstant()) << value;
3746 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3747 __ jmp(slow_path->GetEntryLabel());
3748 }
3749 }
3750 break;
3751 }
3752 default:
3753 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003754 }
Calin Juravled0d48522014-11-04 16:40:20 +00003755}
3756
Calin Juravle9aec02f2014-11-18 23:06:35 +00003757void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3758 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3759
3760 LocationSummary* locations =
3761 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3762
3763 switch (op->GetResultType()) {
3764 case Primitive::kPrimInt:
3765 case Primitive::kPrimLong: {
3766 locations->SetInAt(0, Location::RequiresRegister());
3767 // The shift count needs to be in CL.
3768 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3769 locations->SetOut(Location::SameAsFirstInput());
3770 break;
3771 }
3772 default:
3773 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3774 }
3775}
3776
3777void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3778 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3779
3780 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003781 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003782 Location second = locations->InAt(1);
3783
3784 switch (op->GetResultType()) {
3785 case Primitive::kPrimInt: {
3786 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003787 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003788 if (op->IsShl()) {
3789 __ shll(first_reg, second_reg);
3790 } else if (op->IsShr()) {
3791 __ sarl(first_reg, second_reg);
3792 } else {
3793 __ shrl(first_reg, second_reg);
3794 }
3795 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003796 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003797 if (op->IsShl()) {
3798 __ shll(first_reg, imm);
3799 } else if (op->IsShr()) {
3800 __ sarl(first_reg, imm);
3801 } else {
3802 __ shrl(first_reg, imm);
3803 }
3804 }
3805 break;
3806 }
3807 case Primitive::kPrimLong: {
3808 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003809 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003810 if (op->IsShl()) {
3811 __ shlq(first_reg, second_reg);
3812 } else if (op->IsShr()) {
3813 __ sarq(first_reg, second_reg);
3814 } else {
3815 __ shrq(first_reg, second_reg);
3816 }
3817 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003818 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003819 if (op->IsShl()) {
3820 __ shlq(first_reg, imm);
3821 } else if (op->IsShr()) {
3822 __ sarq(first_reg, imm);
3823 } else {
3824 __ shrq(first_reg, imm);
3825 }
3826 }
3827 break;
3828 }
3829 default:
3830 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003831 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003832 }
3833}
3834
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003835void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3836 LocationSummary* locations =
3837 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3838
3839 switch (ror->GetResultType()) {
3840 case Primitive::kPrimInt:
3841 case Primitive::kPrimLong: {
3842 locations->SetInAt(0, Location::RequiresRegister());
3843 // The shift count needs to be in CL (unless it is a constant).
3844 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3845 locations->SetOut(Location::SameAsFirstInput());
3846 break;
3847 }
3848 default:
3849 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3850 UNREACHABLE();
3851 }
3852}
3853
3854void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3855 LocationSummary* locations = ror->GetLocations();
3856 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3857 Location second = locations->InAt(1);
3858
3859 switch (ror->GetResultType()) {
3860 case Primitive::kPrimInt:
3861 if (second.IsRegister()) {
3862 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3863 __ rorl(first_reg, second_reg);
3864 } else {
3865 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3866 __ rorl(first_reg, imm);
3867 }
3868 break;
3869 case Primitive::kPrimLong:
3870 if (second.IsRegister()) {
3871 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3872 __ rorq(first_reg, second_reg);
3873 } else {
3874 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
3875 __ rorq(first_reg, imm);
3876 }
3877 break;
3878 default:
3879 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3880 UNREACHABLE();
3881 }
3882}
3883
Calin Juravle9aec02f2014-11-18 23:06:35 +00003884void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3885 HandleShift(shl);
3886}
3887
3888void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3889 HandleShift(shl);
3890}
3891
3892void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3893 HandleShift(shr);
3894}
3895
3896void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3897 HandleShift(shr);
3898}
3899
3900void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3901 HandleShift(ushr);
3902}
3903
3904void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3905 HandleShift(ushr);
3906}
3907
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003908void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003909 LocationSummary* locations =
3910 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003911 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003912 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3913 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003914 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003915}
3916
3917void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003918 // Note: if heap poisoning is enabled, the entry point takes cares
3919 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003920 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3921 instruction,
3922 instruction->GetDexPc(),
3923 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003924 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003925
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003926 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003927}
3928
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003929void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3930 LocationSummary* locations =
3931 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3932 InvokeRuntimeCallingConvention calling_convention;
3933 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003934 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003935 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003936 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003937}
3938
3939void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3940 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003941 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3942 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003943 // Note: if heap poisoning is enabled, the entry point takes cares
3944 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003945 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3946 instruction,
3947 instruction->GetDexPc(),
3948 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003949 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003950
3951 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003952}
3953
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003954void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003955 LocationSummary* locations =
3956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003957 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3958 if (location.IsStackSlot()) {
3959 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3960 } else if (location.IsDoubleStackSlot()) {
3961 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3962 }
3963 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003964}
3965
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003966void InstructionCodeGeneratorX86_64::VisitParameterValue(
3967 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003968 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003969}
3970
3971void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3972 LocationSummary* locations =
3973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3974 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3975}
3976
3977void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3978 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3979 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003980}
3981
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003982void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003983 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003984 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003985 locations->SetInAt(0, Location::RequiresRegister());
3986 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003987}
3988
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003989void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3990 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003991 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3992 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003993 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003994 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003995 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003996 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003997 break;
3998
3999 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004000 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004001 break;
4002
4003 default:
4004 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4005 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004006}
4007
David Brazdil66d126e2015-04-03 16:02:44 +01004008void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4009 LocationSummary* locations =
4010 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4011 locations->SetInAt(0, Location::RequiresRegister());
4012 locations->SetOut(Location::SameAsFirstInput());
4013}
4014
4015void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004016 LocationSummary* locations = bool_not->GetLocations();
4017 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4018 locations->Out().AsRegister<CpuRegister>().AsRegister());
4019 Location out = locations->Out();
4020 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4021}
4022
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004023void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004024 LocationSummary* locations =
4025 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004026 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4027 locations->SetInAt(i, Location::Any());
4028 }
4029 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004030}
4031
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004032void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004033 LOG(FATAL) << "Unimplemented";
4034}
4035
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004036void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004037 /*
4038 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004039 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004040 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4041 */
4042 switch (kind) {
4043 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004044 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004045 break;
4046 }
4047 case MemBarrierKind::kAnyStore:
4048 case MemBarrierKind::kLoadAny:
4049 case MemBarrierKind::kStoreStore: {
4050 // nop
4051 break;
4052 }
4053 default:
4054 LOG(FATAL) << "Unexpected memory barier " << kind;
4055 }
4056}
4057
4058void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4059 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4060
Roland Levillain0d5a2812015-11-13 10:07:31 +00004061 bool object_field_get_with_read_barrier =
4062 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004063 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004064 new (GetGraph()->GetArena()) LocationSummary(instruction,
4065 object_field_get_with_read_barrier ?
4066 LocationSummary::kCallOnSlowPath :
4067 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004068 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004069 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4070 locations->SetOut(Location::RequiresFpuRegister());
4071 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004072 // The output overlaps for an object field get when read barriers
4073 // are enabled: we do not want the move to overwrite the object's
4074 // location, as we need it to emit the read barrier.
4075 locations->SetOut(
4076 Location::RequiresRegister(),
4077 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004078 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004079 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4080 // We need a temporary register for the read barrier marking slow
4081 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4082 locations->AddTemp(Location::RequiresRegister());
4083 }
Calin Juravle52c48962014-12-16 17:02:57 +00004084}
4085
4086void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4087 const FieldInfo& field_info) {
4088 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4089
4090 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004091 Location base_loc = locations->InAt(0);
4092 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004093 Location out = locations->Out();
4094 bool is_volatile = field_info.IsVolatile();
4095 Primitive::Type field_type = field_info.GetFieldType();
4096 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4097
4098 switch (field_type) {
4099 case Primitive::kPrimBoolean: {
4100 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4101 break;
4102 }
4103
4104 case Primitive::kPrimByte: {
4105 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4106 break;
4107 }
4108
4109 case Primitive::kPrimShort: {
4110 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4111 break;
4112 }
4113
4114 case Primitive::kPrimChar: {
4115 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4116 break;
4117 }
4118
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004119 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004120 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4121 break;
4122 }
4123
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004124 case Primitive::kPrimNot: {
4125 // /* HeapReference<Object> */ out = *(base + offset)
4126 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4127 Location temp_loc = locations->GetTemp(0);
4128 // Note that a potential implicit null check is handled in this
4129 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4130 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4131 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4132 if (is_volatile) {
4133 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4134 }
4135 } else {
4136 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4137 codegen_->MaybeRecordImplicitNullCheck(instruction);
4138 if (is_volatile) {
4139 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4140 }
4141 // If read barriers are enabled, emit read barriers other than
4142 // Baker's using a slow path (and also unpoison the loaded
4143 // reference, if heap poisoning is enabled).
4144 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4145 }
4146 break;
4147 }
4148
Calin Juravle52c48962014-12-16 17:02:57 +00004149 case Primitive::kPrimLong: {
4150 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4151 break;
4152 }
4153
4154 case Primitive::kPrimFloat: {
4155 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4156 break;
4157 }
4158
4159 case Primitive::kPrimDouble: {
4160 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4161 break;
4162 }
4163
4164 case Primitive::kPrimVoid:
4165 LOG(FATAL) << "Unreachable type " << field_type;
4166 UNREACHABLE();
4167 }
4168
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004169 if (field_type == Primitive::kPrimNot) {
4170 // Potential implicit null checks, in the case of reference
4171 // fields, are handled in the previous switch statement.
4172 } else {
4173 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004174 }
Roland Levillain4d027112015-07-01 15:41:14 +01004175
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004176 if (is_volatile) {
4177 if (field_type == Primitive::kPrimNot) {
4178 // Memory barriers, in the case of references, are also handled
4179 // in the previous switch statement.
4180 } else {
4181 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4182 }
Roland Levillain4d027112015-07-01 15:41:14 +01004183 }
Calin Juravle52c48962014-12-16 17:02:57 +00004184}
4185
4186void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4187 const FieldInfo& field_info) {
4188 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4189
4190 LocationSummary* locations =
4191 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004192 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004193 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004194 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004195 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004196
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004197 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004198 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004199 if (is_volatile) {
4200 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4201 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4202 } else {
4203 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4204 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004205 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004206 if (is_volatile) {
4207 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4208 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4209 } else {
4210 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4211 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004212 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004213 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004214 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004215 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004216 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004217 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4218 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004219 locations->AddTemp(Location::RequiresRegister());
4220 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004221}
4222
Calin Juravle52c48962014-12-16 17:02:57 +00004223void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004224 const FieldInfo& field_info,
4225 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004226 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4227
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004228 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004229 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4230 Location value = locations->InAt(1);
4231 bool is_volatile = field_info.IsVolatile();
4232 Primitive::Type field_type = field_info.GetFieldType();
4233 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4234
4235 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004236 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004237 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004238
Mark Mendellea5af682015-10-22 17:35:49 -04004239 bool maybe_record_implicit_null_check_done = false;
4240
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004241 switch (field_type) {
4242 case Primitive::kPrimBoolean:
4243 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004244 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004245 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004246 __ movb(Address(base, offset), Immediate(v));
4247 } else {
4248 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4249 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004250 break;
4251 }
4252
4253 case Primitive::kPrimShort:
4254 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004255 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004256 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004257 __ movw(Address(base, offset), Immediate(v));
4258 } else {
4259 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4260 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004261 break;
4262 }
4263
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004264 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004265 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004266 if (value.IsConstant()) {
4267 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004268 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4269 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4270 // Note: if heap poisoning is enabled, no need to poison
4271 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004272 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004273 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004274 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4275 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4276 __ movl(temp, value.AsRegister<CpuRegister>());
4277 __ PoisonHeapReference(temp);
4278 __ movl(Address(base, offset), temp);
4279 } else {
4280 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4281 }
Mark Mendell40741f32015-04-20 22:10:34 -04004282 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004283 break;
4284 }
4285
4286 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004287 if (value.IsConstant()) {
4288 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004289 codegen_->MoveInt64ToAddress(Address(base, offset),
4290 Address(base, offset + sizeof(int32_t)),
4291 v,
4292 instruction);
4293 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004294 } else {
4295 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4296 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297 break;
4298 }
4299
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004300 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004301 if (value.IsConstant()) {
4302 int32_t v =
4303 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4304 __ movl(Address(base, offset), Immediate(v));
4305 } else {
4306 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4307 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004308 break;
4309 }
4310
4311 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004312 if (value.IsConstant()) {
4313 int64_t v =
4314 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4315 codegen_->MoveInt64ToAddress(Address(base, offset),
4316 Address(base, offset + sizeof(int32_t)),
4317 v,
4318 instruction);
4319 maybe_record_implicit_null_check_done = true;
4320 } else {
4321 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4322 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004323 break;
4324 }
4325
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004326 case Primitive::kPrimVoid:
4327 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004328 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004329 }
Calin Juravle52c48962014-12-16 17:02:57 +00004330
Mark Mendellea5af682015-10-22 17:35:49 -04004331 if (!maybe_record_implicit_null_check_done) {
4332 codegen_->MaybeRecordImplicitNullCheck(instruction);
4333 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004334
4335 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4336 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4337 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004338 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004339 }
4340
Calin Juravle52c48962014-12-16 17:02:57 +00004341 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004342 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004343 }
4344}
4345
4346void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4347 HandleFieldSet(instruction, instruction->GetFieldInfo());
4348}
4349
4350void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004351 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004352}
4353
4354void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004355 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004356}
4357
4358void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004359 HandleFieldGet(instruction, instruction->GetFieldInfo());
4360}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004361
Calin Juravle52c48962014-12-16 17:02:57 +00004362void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4363 HandleFieldGet(instruction);
4364}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004365
Calin Juravle52c48962014-12-16 17:02:57 +00004366void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4367 HandleFieldGet(instruction, instruction->GetFieldInfo());
4368}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004369
Calin Juravle52c48962014-12-16 17:02:57 +00004370void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4371 HandleFieldSet(instruction, instruction->GetFieldInfo());
4372}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004373
Calin Juravle52c48962014-12-16 17:02:57 +00004374void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004375 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004376}
4377
Calin Juravlee460d1d2015-09-29 04:52:17 +01004378void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4379 HUnresolvedInstanceFieldGet* instruction) {
4380 FieldAccessCallingConventionX86_64 calling_convention;
4381 codegen_->CreateUnresolvedFieldLocationSummary(
4382 instruction, instruction->GetFieldType(), calling_convention);
4383}
4384
4385void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4386 HUnresolvedInstanceFieldGet* instruction) {
4387 FieldAccessCallingConventionX86_64 calling_convention;
4388 codegen_->GenerateUnresolvedFieldAccess(instruction,
4389 instruction->GetFieldType(),
4390 instruction->GetFieldIndex(),
4391 instruction->GetDexPc(),
4392 calling_convention);
4393}
4394
4395void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4396 HUnresolvedInstanceFieldSet* instruction) {
4397 FieldAccessCallingConventionX86_64 calling_convention;
4398 codegen_->CreateUnresolvedFieldLocationSummary(
4399 instruction, instruction->GetFieldType(), calling_convention);
4400}
4401
4402void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4403 HUnresolvedInstanceFieldSet* instruction) {
4404 FieldAccessCallingConventionX86_64 calling_convention;
4405 codegen_->GenerateUnresolvedFieldAccess(instruction,
4406 instruction->GetFieldType(),
4407 instruction->GetFieldIndex(),
4408 instruction->GetDexPc(),
4409 calling_convention);
4410}
4411
4412void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4413 HUnresolvedStaticFieldGet* instruction) {
4414 FieldAccessCallingConventionX86_64 calling_convention;
4415 codegen_->CreateUnresolvedFieldLocationSummary(
4416 instruction, instruction->GetFieldType(), calling_convention);
4417}
4418
4419void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4420 HUnresolvedStaticFieldGet* instruction) {
4421 FieldAccessCallingConventionX86_64 calling_convention;
4422 codegen_->GenerateUnresolvedFieldAccess(instruction,
4423 instruction->GetFieldType(),
4424 instruction->GetFieldIndex(),
4425 instruction->GetDexPc(),
4426 calling_convention);
4427}
4428
4429void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4430 HUnresolvedStaticFieldSet* instruction) {
4431 FieldAccessCallingConventionX86_64 calling_convention;
4432 codegen_->CreateUnresolvedFieldLocationSummary(
4433 instruction, instruction->GetFieldType(), calling_convention);
4434}
4435
4436void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4437 HUnresolvedStaticFieldSet* instruction) {
4438 FieldAccessCallingConventionX86_64 calling_convention;
4439 codegen_->GenerateUnresolvedFieldAccess(instruction,
4440 instruction->GetFieldType(),
4441 instruction->GetFieldIndex(),
4442 instruction->GetDexPc(),
4443 calling_convention);
4444}
4445
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004446void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004447 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4448 ? LocationSummary::kCallOnSlowPath
4449 : LocationSummary::kNoCall;
4450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4451 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004452 ? Location::RequiresRegister()
4453 : Location::Any();
4454 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004455 if (instruction->HasUses()) {
4456 locations->SetOut(Location::SameAsFirstInput());
4457 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004458}
4459
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004460void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004461 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4462 return;
4463 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004464 LocationSummary* locations = instruction->GetLocations();
4465 Location obj = locations->InAt(0);
4466
4467 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4468 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4469}
4470
4471void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004472 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004473 codegen_->AddSlowPath(slow_path);
4474
4475 LocationSummary* locations = instruction->GetLocations();
4476 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477
4478 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004479 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004480 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004481 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004482 } else {
4483 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004484 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004485 __ jmp(slow_path->GetEntryLabel());
4486 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004487 }
4488 __ j(kEqual, slow_path->GetEntryLabel());
4489}
4490
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004491void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004492 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004493 GenerateImplicitNullCheck(instruction);
4494 } else {
4495 GenerateExplicitNullCheck(instruction);
4496 }
4497}
4498
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004499void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004500 bool object_array_get_with_read_barrier =
4501 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004502 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004503 new (GetGraph()->GetArena()) LocationSummary(instruction,
4504 object_array_get_with_read_barrier ?
4505 LocationSummary::kCallOnSlowPath :
4506 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004507 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004508 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004509 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4510 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4511 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004512 // The output overlaps for an object array get when read barriers
4513 // are enabled: we do not want the move to overwrite the array's
4514 // location, as we need it to emit the read barrier.
4515 locations->SetOut(
4516 Location::RequiresRegister(),
4517 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004518 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004519 // We need a temporary register for the read barrier marking slow
4520 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4521 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4522 locations->AddTemp(Location::RequiresRegister());
4523 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004524}
4525
4526void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4527 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004528 Location obj_loc = locations->InAt(0);
4529 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004530 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004531 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004532
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004533 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004534 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535 case Primitive::kPrimBoolean: {
4536 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004537 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538 if (index.IsConstant()) {
4539 __ movzxb(out, Address(obj,
4540 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4541 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004542 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004543 }
4544 break;
4545 }
4546
4547 case Primitive::kPrimByte: {
4548 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004549 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550 if (index.IsConstant()) {
4551 __ movsxb(out, Address(obj,
4552 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4553 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004554 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004555 }
4556 break;
4557 }
4558
4559 case Primitive::kPrimShort: {
4560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004561 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004562 if (index.IsConstant()) {
4563 __ movsxw(out, Address(obj,
4564 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4565 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004566 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004567 }
4568 break;
4569 }
4570
4571 case Primitive::kPrimChar: {
4572 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004573 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004574 if (index.IsConstant()) {
4575 __ movzxw(out, Address(obj,
4576 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4577 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004578 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004579 }
4580 break;
4581 }
4582
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004583 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004584 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004585 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004586 if (index.IsConstant()) {
4587 __ movl(out, Address(obj,
4588 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4589 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004590 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591 }
4592 break;
4593 }
4594
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004595 case Primitive::kPrimNot: {
4596 static_assert(
4597 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4598 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4599 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4600 // /* HeapReference<Object> */ out =
4601 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4602 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4603 Location temp = locations->GetTemp(0);
4604 // Note that a potential implicit null check is handled in this
4605 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4606 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4607 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4608 } else {
4609 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4610 if (index.IsConstant()) {
4611 uint32_t offset =
4612 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4613 __ movl(out, Address(obj, offset));
4614 codegen_->MaybeRecordImplicitNullCheck(instruction);
4615 // If read barriers are enabled, emit read barriers other than
4616 // Baker's using a slow path (and also unpoison the loaded
4617 // reference, if heap poisoning is enabled).
4618 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4619 } else {
4620 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4621 codegen_->MaybeRecordImplicitNullCheck(instruction);
4622 // If read barriers are enabled, emit read barriers other than
4623 // Baker's using a slow path (and also unpoison the loaded
4624 // reference, if heap poisoning is enabled).
4625 codegen_->MaybeGenerateReadBarrierSlow(
4626 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4627 }
4628 }
4629 break;
4630 }
4631
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 case Primitive::kPrimLong: {
4633 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004634 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004635 if (index.IsConstant()) {
4636 __ movq(out, Address(obj,
4637 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4638 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004639 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004640 }
4641 break;
4642 }
4643
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004644 case Primitive::kPrimFloat: {
4645 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004646 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004647 if (index.IsConstant()) {
4648 __ movss(out, Address(obj,
4649 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4650 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004651 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004652 }
4653 break;
4654 }
4655
4656 case Primitive::kPrimDouble: {
4657 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004658 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004659 if (index.IsConstant()) {
4660 __ movsd(out, Address(obj,
4661 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4662 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004663 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004664 }
4665 break;
4666 }
4667
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004668 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004669 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004670 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004671 }
Roland Levillain4d027112015-07-01 15:41:14 +01004672
4673 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004674 // Potential implicit null checks, in the case of reference
4675 // arrays, are handled in the previous switch statement.
4676 } else {
4677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004678 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004679}
4680
4681void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004682 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004683
4684 bool needs_write_barrier =
4685 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004686 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004687 bool object_array_set_with_read_barrier =
4688 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004689
Nicolas Geoffray39468442014-09-02 15:17:15 +01004690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004691 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004692 (may_need_runtime_call || object_array_set_with_read_barrier) ?
4693 LocationSummary::kCallOnSlowPath :
4694 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004695
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004696 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004697 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4698 if (Primitive::IsFloatingPointType(value_type)) {
4699 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004700 } else {
4701 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4702 }
4703
4704 if (needs_write_barrier) {
4705 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004706
4707 // This first temporary register is possibly used for heap
4708 // reference poisoning and/or read barrier emission too.
4709 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004710 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004712}
4713
4714void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4715 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004716 Location array_loc = locations->InAt(0);
4717 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004719 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004720 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004721 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004722 bool needs_write_barrier =
4723 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004724 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4725 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4726 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004727
4728 switch (value_type) {
4729 case Primitive::kPrimBoolean:
4730 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004731 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4732 Address address = index.IsConstant()
4733 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4734 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4735 if (value.IsRegister()) {
4736 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004738 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004740 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004741 break;
4742 }
4743
4744 case Primitive::kPrimShort:
4745 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004746 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4747 Address address = index.IsConstant()
4748 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4749 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4750 if (value.IsRegister()) {
4751 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004752 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004753 DCHECK(value.IsConstant()) << value;
4754 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004755 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004756 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004757 break;
4758 }
4759
4760 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004761 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4762 Address address = index.IsConstant()
4763 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4764 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004765
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004766 if (!value.IsRegister()) {
4767 // Just setting null.
4768 DCHECK(instruction->InputAt(2)->IsNullConstant());
4769 DCHECK(value.IsConstant()) << value;
4770 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004771 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004772 DCHECK(!needs_write_barrier);
4773 DCHECK(!may_need_runtime_call);
4774 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004775 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004776
4777 DCHECK(needs_write_barrier);
4778 CpuRegister register_value = value.AsRegister<CpuRegister>();
4779 NearLabel done, not_null, do_put;
4780 SlowPathCode* slow_path = nullptr;
4781 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4782 if (may_need_runtime_call) {
4783 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4784 codegen_->AddSlowPath(slow_path);
4785 if (instruction->GetValueCanBeNull()) {
4786 __ testl(register_value, register_value);
4787 __ j(kNotEqual, &not_null);
4788 __ movl(address, Immediate(0));
4789 codegen_->MaybeRecordImplicitNullCheck(instruction);
4790 __ jmp(&done);
4791 __ Bind(&not_null);
4792 }
4793
Roland Levillain0d5a2812015-11-13 10:07:31 +00004794 if (kEmitCompilerReadBarrier) {
4795 // When read barriers are enabled, the type checking
4796 // instrumentation requires two read barriers:
4797 //
4798 // __ movl(temp2, temp);
4799 // // /* HeapReference<Class> */ temp = temp->component_type_
4800 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004801 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004802 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4803 //
4804 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4805 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004806 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004807 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4808 //
4809 // __ cmpl(temp, temp2);
4810 //
4811 // However, the second read barrier may trash `temp`, as it
4812 // is a temporary register, and as such would not be saved
4813 // along with live registers before calling the runtime (nor
4814 // restored afterwards). So in this case, we bail out and
4815 // delegate the work to the array set slow path.
4816 //
4817 // TODO: Extend the register allocator to support a new
4818 // "(locally) live temp" location so as to avoid always
4819 // going into the slow path when read barriers are enabled.
4820 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004821 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004822 // /* HeapReference<Class> */ temp = array->klass_
4823 __ movl(temp, Address(array, class_offset));
4824 codegen_->MaybeRecordImplicitNullCheck(instruction);
4825 __ MaybeUnpoisonHeapReference(temp);
4826
4827 // /* HeapReference<Class> */ temp = temp->component_type_
4828 __ movl(temp, Address(temp, component_offset));
4829 // If heap poisoning is enabled, no need to unpoison `temp`
4830 // nor the object reference in `register_value->klass`, as
4831 // we are comparing two poisoned references.
4832 __ cmpl(temp, Address(register_value, class_offset));
4833
4834 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4835 __ j(kEqual, &do_put);
4836 // If heap poisoning is enabled, the `temp` reference has
4837 // not been unpoisoned yet; unpoison it now.
4838 __ MaybeUnpoisonHeapReference(temp);
4839
4840 // /* HeapReference<Class> */ temp = temp->super_class_
4841 __ movl(temp, Address(temp, super_offset));
4842 // If heap poisoning is enabled, no need to unpoison
4843 // `temp`, as we are comparing against null below.
4844 __ testl(temp, temp);
4845 __ j(kNotEqual, slow_path->GetEntryLabel());
4846 __ Bind(&do_put);
4847 } else {
4848 __ j(kNotEqual, slow_path->GetEntryLabel());
4849 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004850 }
4851 }
4852
4853 if (kPoisonHeapReferences) {
4854 __ movl(temp, register_value);
4855 __ PoisonHeapReference(temp);
4856 __ movl(address, temp);
4857 } else {
4858 __ movl(address, register_value);
4859 }
4860 if (!may_need_runtime_call) {
4861 codegen_->MaybeRecordImplicitNullCheck(instruction);
4862 }
4863
4864 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4865 codegen_->MarkGCCard(
4866 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4867 __ Bind(&done);
4868
4869 if (slow_path != nullptr) {
4870 __ Bind(slow_path->GetExitLabel());
4871 }
4872
4873 break;
4874 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004875
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004876 case Primitive::kPrimInt: {
4877 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4878 Address address = index.IsConstant()
4879 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4880 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4881 if (value.IsRegister()) {
4882 __ movl(address, value.AsRegister<CpuRegister>());
4883 } else {
4884 DCHECK(value.IsConstant()) << value;
4885 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4886 __ movl(address, Immediate(v));
4887 }
4888 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004889 break;
4890 }
4891
4892 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004893 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4894 Address address = index.IsConstant()
4895 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4896 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4897 if (value.IsRegister()) {
4898 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004899 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004901 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004902 Address address_high = index.IsConstant()
4903 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4904 offset + sizeof(int32_t))
4905 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4906 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004907 }
4908 break;
4909 }
4910
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004911 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004912 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4913 Address address = index.IsConstant()
4914 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4915 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004916 if (value.IsFpuRegister()) {
4917 __ movss(address, value.AsFpuRegister<XmmRegister>());
4918 } else {
4919 DCHECK(value.IsConstant());
4920 int32_t v =
4921 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4922 __ movl(address, Immediate(v));
4923 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004924 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004925 break;
4926 }
4927
4928 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004929 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4930 Address address = index.IsConstant()
4931 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4932 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004933 if (value.IsFpuRegister()) {
4934 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4935 codegen_->MaybeRecordImplicitNullCheck(instruction);
4936 } else {
4937 int64_t v =
4938 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4939 Address address_high = index.IsConstant()
4940 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4941 offset + sizeof(int32_t))
4942 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4943 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4944 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004945 break;
4946 }
4947
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004948 case Primitive::kPrimVoid:
4949 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004950 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004951 }
4952}
4953
4954void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004955 LocationSummary* locations =
4956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004957 locations->SetInAt(0, Location::RequiresRegister());
4958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004959}
4960
4961void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4962 LocationSummary* locations = instruction->GetLocations();
4963 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004964 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4965 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004966 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004967 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004968}
4969
4970void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004971 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4972 ? LocationSummary::kCallOnSlowPath
4973 : LocationSummary::kNoCall;
4974 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004975 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004976 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004977 if (instruction->HasUses()) {
4978 locations->SetOut(Location::SameAsFirstInput());
4979 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004980}
4981
4982void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4983 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004984 Location index_loc = locations->InAt(0);
4985 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004986 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004987 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004988
Mark Mendell99dbd682015-04-22 16:18:52 -04004989 if (length_loc.IsConstant()) {
4990 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4991 if (index_loc.IsConstant()) {
4992 // BCE will remove the bounds check if we are guarenteed to pass.
4993 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4994 if (index < 0 || index >= length) {
4995 codegen_->AddSlowPath(slow_path);
4996 __ jmp(slow_path->GetEntryLabel());
4997 } else {
4998 // Some optimization after BCE may have generated this, and we should not
4999 // generate a bounds check if it is a valid range.
5000 }
5001 return;
5002 }
5003
5004 // We have to reverse the jump condition because the length is the constant.
5005 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5006 __ cmpl(index_reg, Immediate(length));
5007 codegen_->AddSlowPath(slow_path);
5008 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005009 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005010 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5011 if (index_loc.IsConstant()) {
5012 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5013 __ cmpl(length, Immediate(value));
5014 } else {
5015 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5016 }
5017 codegen_->AddSlowPath(slow_path);
5018 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005019 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005020}
5021
5022void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5023 CpuRegister card,
5024 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005025 CpuRegister value,
5026 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005027 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005028 if (value_can_be_null) {
5029 __ testl(value, value);
5030 __ j(kEqual, &is_null);
5031 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005032 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5033 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034 __ movq(temp, object);
5035 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005036 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005037 if (value_can_be_null) {
5038 __ Bind(&is_null);
5039 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040}
5041
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005042void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
5043 temp->SetLocations(nullptr);
5044}
5045
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005046void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005047 // Nothing to do, this is driven by the code generator.
5048}
5049
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005050void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005051 LOG(FATAL) << "Unimplemented";
5052}
5053
5054void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005055 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5056}
5057
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005058void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5059 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5060}
5061
5062void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005063 HBasicBlock* block = instruction->GetBlock();
5064 if (block->GetLoopInformation() != nullptr) {
5065 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5066 // The back edge will generate the suspend check.
5067 return;
5068 }
5069 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5070 // The goto will generate the suspend check.
5071 return;
5072 }
5073 GenerateSuspendCheck(instruction, nullptr);
5074}
5075
5076void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5077 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005078 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005079 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5080 if (slow_path == nullptr) {
5081 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5082 instruction->SetSlowPath(slow_path);
5083 codegen_->AddSlowPath(slow_path);
5084 if (successor != nullptr) {
5085 DCHECK(successor->IsLoopHeader());
5086 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5087 }
5088 } else {
5089 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5090 }
5091
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005092 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5093 /* no_rip */ true),
5094 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005095 if (successor == nullptr) {
5096 __ j(kNotEqual, slow_path->GetEntryLabel());
5097 __ Bind(slow_path->GetReturnLabel());
5098 } else {
5099 __ j(kEqual, codegen_->GetLabelOf(successor));
5100 __ jmp(slow_path->GetEntryLabel());
5101 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005102}
5103
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005104X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5105 return codegen_->GetAssembler();
5106}
5107
5108void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005109 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005110 Location source = move->GetSource();
5111 Location destination = move->GetDestination();
5112
5113 if (source.IsRegister()) {
5114 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005115 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005116 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005117 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005118 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005119 } else {
5120 DCHECK(destination.IsDoubleStackSlot());
5121 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005122 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005123 }
5124 } else if (source.IsStackSlot()) {
5125 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005126 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005127 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005128 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005129 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005130 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005131 } else {
5132 DCHECK(destination.IsStackSlot());
5133 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5134 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5135 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005136 } else if (source.IsDoubleStackSlot()) {
5137 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005138 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005139 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005140 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005141 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5142 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005143 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005144 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005145 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5146 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5147 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005148 } else if (source.IsConstant()) {
5149 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005150 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5151 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005152 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005153 if (value == 0) {
5154 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5155 } else {
5156 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5157 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005158 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005159 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005160 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005161 }
5162 } else if (constant->IsLongConstant()) {
5163 int64_t value = constant->AsLongConstant()->GetValue();
5164 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005165 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005166 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005167 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005168 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005169 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005170 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005171 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005172 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005173 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005174 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5175 if (value == 0) {
5176 // easy FP 0.0.
5177 __ xorps(dest, dest);
5178 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005179 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005180 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005181 } else {
5182 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04005183 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005184 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5185 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005186 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005187 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005188 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005189 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005190 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005191 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5192 if (value == 0) {
5193 __ xorpd(dest, dest);
5194 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005195 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005196 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005197 } else {
5198 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005199 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005200 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005201 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005202 } else if (source.IsFpuRegister()) {
5203 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005204 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005205 } else if (destination.IsStackSlot()) {
5206 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005207 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005208 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005209 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005210 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005211 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005212 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005213 }
5214}
5215
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005216void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005217 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005218 __ movl(Address(CpuRegister(RSP), mem), reg);
5219 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005220}
5221
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005222void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005223 ScratchRegisterScope ensure_scratch(
5224 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5225
5226 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5227 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5228 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5229 Address(CpuRegister(RSP), mem2 + stack_offset));
5230 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5231 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5232 CpuRegister(ensure_scratch.GetRegister()));
5233}
5234
Mark Mendell8a1c7282015-06-29 15:41:28 -04005235void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5236 __ movq(CpuRegister(TMP), reg1);
5237 __ movq(reg1, reg2);
5238 __ movq(reg2, CpuRegister(TMP));
5239}
5240
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005241void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5242 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5243 __ movq(Address(CpuRegister(RSP), mem), reg);
5244 __ movq(reg, CpuRegister(TMP));
5245}
5246
5247void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5248 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005249 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005250
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005251 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5252 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5253 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5254 Address(CpuRegister(RSP), mem2 + stack_offset));
5255 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5256 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5257 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005258}
5259
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005260void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5261 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5262 __ movss(Address(CpuRegister(RSP), mem), reg);
5263 __ movd(reg, CpuRegister(TMP));
5264}
5265
5266void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5267 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5268 __ movsd(Address(CpuRegister(RSP), mem), reg);
5269 __ movd(reg, CpuRegister(TMP));
5270}
5271
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005272void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005273 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005274 Location source = move->GetSource();
5275 Location destination = move->GetDestination();
5276
5277 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005278 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005279 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005280 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005281 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005282 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005283 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005284 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5285 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005286 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005287 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005288 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005289 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5290 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005291 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005292 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5293 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5294 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005295 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005296 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005297 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005298 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005299 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005300 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005301 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005302 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005303 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005304 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005305 }
5306}
5307
5308
5309void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5310 __ pushq(CpuRegister(reg));
5311}
5312
5313
5314void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5315 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005316}
5317
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005318void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005319 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005320 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5321 Immediate(mirror::Class::kStatusInitialized));
5322 __ j(kLess, slow_path->GetEntryLabel());
5323 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005324 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005325}
5326
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005327void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005328 InvokeRuntimeCallingConvention calling_convention;
5329 CodeGenerator::CreateLoadClassLocationSummary(
5330 cls,
5331 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005332 Location::RegisterLocation(RAX),
5333 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005334}
5335
5336void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005337 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005338 if (cls->NeedsAccessCheck()) {
5339 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5340 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5341 cls,
5342 cls->GetDexPc(),
5343 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005344 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005345 return;
5346 }
5347
Roland Levillain0d5a2812015-11-13 10:07:31 +00005348 Location out_loc = locations->Out();
5349 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005350 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005351
Calin Juravle580b6092015-10-06 17:35:58 +01005352 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005353 DCHECK(!cls->CanCallRuntime());
5354 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005355 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5356 GenerateGcRootFieldLoad(
5357 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005358 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005359 // /* GcRoot<mirror::Class>[] */ out =
5360 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5361 __ movq(out, Address(current_method,
5362 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005363 // /* GcRoot<mirror::Class> */ out = out[type_index]
5364 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01005365
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005366 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5367 DCHECK(cls->CanCallRuntime());
5368 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5369 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5370 codegen_->AddSlowPath(slow_path);
5371 if (!cls->IsInDexCache()) {
5372 __ testl(out, out);
5373 __ j(kEqual, slow_path->GetEntryLabel());
5374 }
5375 if (cls->MustGenerateClinitCheck()) {
5376 GenerateClassInitializationCheck(slow_path, out);
5377 } else {
5378 __ Bind(slow_path->GetExitLabel());
5379 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005380 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005381 }
5382}
5383
5384void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5385 LocationSummary* locations =
5386 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5387 locations->SetInAt(0, Location::RequiresRegister());
5388 if (check->HasUses()) {
5389 locations->SetOut(Location::SameAsFirstInput());
5390 }
5391}
5392
5393void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005394 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005395 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005396 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005397 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005398 GenerateClassInitializationCheck(slow_path,
5399 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005400}
5401
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005402void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005403 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5404 ? LocationSummary::kCallOnSlowPath
5405 : LocationSummary::kNoCall;
5406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005407 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005408 locations->SetOut(Location::RequiresRegister());
5409}
5410
5411void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005412 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005413 Location out_loc = locations->Out();
5414 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005415 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005416
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005417 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5418 GenerateGcRootFieldLoad(
5419 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005420 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5421 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005422 // /* GcRoot<mirror::String> */ out = out[string_index]
5423 GenerateGcRootFieldLoad(
5424 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005425
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005426 if (!load->IsInDexCache()) {
5427 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5428 codegen_->AddSlowPath(slow_path);
5429 __ testl(out, out);
5430 __ j(kEqual, slow_path->GetEntryLabel());
5431 __ Bind(slow_path->GetExitLabel());
5432 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005433}
5434
David Brazdilcb1c0552015-08-04 16:22:25 +01005435static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005436 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5437 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005438}
5439
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005440void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5441 LocationSummary* locations =
5442 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5443 locations->SetOut(Location::RequiresRegister());
5444}
5445
5446void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005447 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5448}
5449
5450void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5451 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5452}
5453
5454void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5455 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005456}
5457
5458void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5459 LocationSummary* locations =
5460 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5461 InvokeRuntimeCallingConvention calling_convention;
5462 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5463}
5464
5465void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005466 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5467 instruction,
5468 instruction->GetDexPc(),
5469 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005470 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005471}
5472
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005473static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5474 return kEmitCompilerReadBarrier &&
5475 (kUseBakerReadBarrier ||
5476 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5477 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5478 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5479}
5480
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005481void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005482 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005483 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5484 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005485 case TypeCheckKind::kExactCheck:
5486 case TypeCheckKind::kAbstractClassCheck:
5487 case TypeCheckKind::kClassHierarchyCheck:
5488 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005489 call_kind =
5490 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005491 break;
5492 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005493 case TypeCheckKind::kUnresolvedCheck:
5494 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005495 call_kind = LocationSummary::kCallOnSlowPath;
5496 break;
5497 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005498
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005499 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005500 locations->SetInAt(0, Location::RequiresRegister());
5501 locations->SetInAt(1, Location::Any());
5502 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5503 locations->SetOut(Location::RequiresRegister());
5504 // When read barriers are enabled, we need a temporary register for
5505 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005506 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005507 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005508 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005509}
5510
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005511void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005512 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005513 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005514 Location obj_loc = locations->InAt(0);
5515 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005516 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005517 Location out_loc = locations->Out();
5518 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005519 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5520 locations->GetTemp(0) :
5521 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005522 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005523 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5524 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5525 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005526 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005527 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005528
5529 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005530 // Avoid null check if we know obj is not null.
5531 if (instruction->MustDoNullCheck()) {
5532 __ testl(obj, obj);
5533 __ j(kEqual, &zero);
5534 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005535
Roland Levillain0d5a2812015-11-13 10:07:31 +00005536 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005537 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005538
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005539 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005540 case TypeCheckKind::kExactCheck: {
5541 if (cls.IsRegister()) {
5542 __ cmpl(out, cls.AsRegister<CpuRegister>());
5543 } else {
5544 DCHECK(cls.IsStackSlot()) << cls;
5545 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5546 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005547 if (zero.IsLinked()) {
5548 // Classes must be equal for the instanceof to succeed.
5549 __ j(kNotEqual, &zero);
5550 __ movl(out, Immediate(1));
5551 __ jmp(&done);
5552 } else {
5553 __ setcc(kEqual, out);
5554 // setcc only sets the low byte.
5555 __ andl(out, Immediate(1));
5556 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005557 break;
5558 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005559
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005560 case TypeCheckKind::kAbstractClassCheck: {
5561 // If the class is abstract, we eagerly fetch the super class of the
5562 // object to avoid doing a comparison we know will fail.
5563 NearLabel loop, success;
5564 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005565 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005566 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005567 __ testl(out, out);
5568 // If `out` is null, we use it for the result, and jump to `done`.
5569 __ j(kEqual, &done);
5570 if (cls.IsRegister()) {
5571 __ cmpl(out, cls.AsRegister<CpuRegister>());
5572 } else {
5573 DCHECK(cls.IsStackSlot()) << cls;
5574 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5575 }
5576 __ j(kNotEqual, &loop);
5577 __ movl(out, Immediate(1));
5578 if (zero.IsLinked()) {
5579 __ jmp(&done);
5580 }
5581 break;
5582 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005583
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005584 case TypeCheckKind::kClassHierarchyCheck: {
5585 // Walk over the class hierarchy to find a match.
5586 NearLabel loop, success;
5587 __ Bind(&loop);
5588 if (cls.IsRegister()) {
5589 __ cmpl(out, cls.AsRegister<CpuRegister>());
5590 } else {
5591 DCHECK(cls.IsStackSlot()) << cls;
5592 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5593 }
5594 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005595 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005596 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005597 __ testl(out, out);
5598 __ j(kNotEqual, &loop);
5599 // If `out` is null, we use it for the result, and jump to `done`.
5600 __ jmp(&done);
5601 __ Bind(&success);
5602 __ movl(out, Immediate(1));
5603 if (zero.IsLinked()) {
5604 __ jmp(&done);
5605 }
5606 break;
5607 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005608
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005609 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005610 // Do an exact check.
5611 NearLabel exact_check;
5612 if (cls.IsRegister()) {
5613 __ cmpl(out, cls.AsRegister<CpuRegister>());
5614 } else {
5615 DCHECK(cls.IsStackSlot()) << cls;
5616 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5617 }
5618 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005619 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005620 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005621 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005622 __ testl(out, out);
5623 // If `out` is null, we use it for the result, and jump to `done`.
5624 __ j(kEqual, &done);
5625 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5626 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005627 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005628 __ movl(out, Immediate(1));
5629 __ jmp(&done);
5630 break;
5631 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005632
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005633 case TypeCheckKind::kArrayCheck: {
5634 if (cls.IsRegister()) {
5635 __ cmpl(out, cls.AsRegister<CpuRegister>());
5636 } else {
5637 DCHECK(cls.IsStackSlot()) << cls;
5638 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5639 }
5640 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005641 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5642 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005643 codegen_->AddSlowPath(slow_path);
5644 __ j(kNotEqual, slow_path->GetEntryLabel());
5645 __ movl(out, Immediate(1));
5646 if (zero.IsLinked()) {
5647 __ jmp(&done);
5648 }
5649 break;
5650 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005651
Calin Juravle98893e12015-10-02 21:05:03 +01005652 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005653 case TypeCheckKind::kInterfaceCheck: {
5654 // Note that we indeed only call on slow path, but we always go
5655 // into the slow path for the unresolved & interface check
5656 // cases.
5657 //
5658 // We cannot directly call the InstanceofNonTrivial runtime
5659 // entry point without resorting to a type checking slow path
5660 // here (i.e. by calling InvokeRuntime directly), as it would
5661 // require to assign fixed registers for the inputs of this
5662 // HInstanceOf instruction (following the runtime calling
5663 // convention), which might be cluttered by the potential first
5664 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005665 //
5666 // TODO: Introduce a new runtime entry point taking the object
5667 // to test (instead of its class) as argument, and let it deal
5668 // with the read barrier issues. This will let us refactor this
5669 // case of the `switch` code as it was previously (with a direct
5670 // call to the runtime not using a type checking slow path).
5671 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005672 DCHECK(locations->OnlyCallsOnSlowPath());
5673 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5674 /* is_fatal */ false);
5675 codegen_->AddSlowPath(slow_path);
5676 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005677 if (zero.IsLinked()) {
5678 __ jmp(&done);
5679 }
5680 break;
5681 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005682 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005683
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005684 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005685 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005686 __ xorl(out, out);
5687 }
5688
5689 if (done.IsLinked()) {
5690 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005691 }
5692
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005693 if (slow_path != nullptr) {
5694 __ Bind(slow_path->GetExitLabel());
5695 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005696}
5697
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005698void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005699 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5700 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005701 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5702 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005703 case TypeCheckKind::kExactCheck:
5704 case TypeCheckKind::kAbstractClassCheck:
5705 case TypeCheckKind::kClassHierarchyCheck:
5706 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005707 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5708 LocationSummary::kCallOnSlowPath :
5709 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005710 break;
5711 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005712 case TypeCheckKind::kUnresolvedCheck:
5713 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005714 call_kind = LocationSummary::kCallOnSlowPath;
5715 break;
5716 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005717 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5718 locations->SetInAt(0, Location::RequiresRegister());
5719 locations->SetInAt(1, Location::Any());
5720 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5721 locations->AddTemp(Location::RequiresRegister());
5722 // When read barriers are enabled, we need an additional temporary
5723 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005724 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005725 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005726 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005727}
5728
5729void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005730 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005731 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005732 Location obj_loc = locations->InAt(0);
5733 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005734 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005735 Location temp_loc = locations->GetTemp(0);
5736 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005737 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5738 locations->GetTemp(1) :
5739 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005740 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5741 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5742 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5743 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005744
Roland Levillain0d5a2812015-11-13 10:07:31 +00005745 bool is_type_check_slow_path_fatal =
5746 (type_check_kind == TypeCheckKind::kExactCheck ||
5747 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5748 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5749 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5750 !instruction->CanThrowIntoCatchBlock();
5751 SlowPathCode* type_check_slow_path =
5752 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5753 is_type_check_slow_path_fatal);
5754 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005755
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005756 Label done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005757 // Avoid null check if we know obj is not null.
5758 if (instruction->MustDoNullCheck()) {
5759 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005760 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005761 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005762
Roland Levillain0d5a2812015-11-13 10:07:31 +00005763 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005764 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005765
Roland Levillain0d5a2812015-11-13 10:07:31 +00005766 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005767 case TypeCheckKind::kExactCheck:
5768 case TypeCheckKind::kArrayCheck: {
5769 if (cls.IsRegister()) {
5770 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5771 } else {
5772 DCHECK(cls.IsStackSlot()) << cls;
5773 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5774 }
5775 // Jump to slow path for throwing the exception or doing a
5776 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005777 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005778 break;
5779 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005780
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005781 case TypeCheckKind::kAbstractClassCheck: {
5782 // If the class is abstract, we eagerly fetch the super class of the
5783 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005784 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005785 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005786 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005787 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005788
5789 // If the class reference currently in `temp` is not null, jump
5790 // to the `compare_classes` label to compare it with the checked
5791 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005792 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005793 __ j(kNotEqual, &compare_classes);
5794 // Otherwise, jump to the slow path to throw the exception.
5795 //
5796 // But before, move back the object's class into `temp` before
5797 // going into the slow path, as it has been overwritten in the
5798 // meantime.
5799 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005800 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005801 __ jmp(type_check_slow_path->GetEntryLabel());
5802
5803 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005804 if (cls.IsRegister()) {
5805 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5806 } else {
5807 DCHECK(cls.IsStackSlot()) << cls;
5808 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5809 }
5810 __ j(kNotEqual, &loop);
5811 break;
5812 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005813
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005814 case TypeCheckKind::kClassHierarchyCheck: {
5815 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005816 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005817 __ Bind(&loop);
5818 if (cls.IsRegister()) {
5819 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5820 } else {
5821 DCHECK(cls.IsStackSlot()) << cls;
5822 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5823 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005824 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005825
Roland Levillain0d5a2812015-11-13 10:07:31 +00005826 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005827 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005828
5829 // If the class reference currently in `temp` is not null, jump
5830 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831 __ testl(temp, temp);
5832 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005833 // Otherwise, jump to the slow path to throw the exception.
5834 //
5835 // But before, move back the object's class into `temp` before
5836 // going into the slow path, as it has been overwritten in the
5837 // meantime.
5838 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005839 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005840 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 break;
5842 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005843
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005845 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005846 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005847 if (cls.IsRegister()) {
5848 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5849 } else {
5850 DCHECK(cls.IsStackSlot()) << cls;
5851 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5852 }
5853 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005854
5855 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005856 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005857 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005858
5859 // If the component type is not null (i.e. the object is indeed
5860 // an array), jump to label `check_non_primitive_component_type`
5861 // to further check that this component type is not a primitive
5862 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005863 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005864 __ j(kNotEqual, &check_non_primitive_component_type);
5865 // Otherwise, jump to the slow path to throw the exception.
5866 //
5867 // But before, move back the object's class into `temp` before
5868 // going into the slow path, as it has been overwritten in the
5869 // meantime.
5870 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005871 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005872 __ jmp(type_check_slow_path->GetEntryLabel());
5873
5874 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005875 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005876 __ j(kEqual, &done);
5877 // Same comment as above regarding `temp` and the slow path.
5878 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005879 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005880 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005881 break;
5882 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005883
Calin Juravle98893e12015-10-02 21:05:03 +01005884 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005886 // We always go into the type check slow path for the unresolved &
5887 // interface check cases.
5888 //
5889 // We cannot directly call the CheckCast runtime entry point
5890 // without resorting to a type checking slow path here (i.e. by
5891 // calling InvokeRuntime directly), as it would require to
5892 // assign fixed registers for the inputs of this HInstanceOf
5893 // instruction (following the runtime calling convention), which
5894 // might be cluttered by the potential first read barrier
5895 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005896 //
5897 // TODO: Introduce a new runtime entry point taking the object
5898 // to test (instead of its class) as argument, and let it deal
5899 // with the read barrier issues. This will let us refactor this
5900 // case of the `switch` code as it was previously (with a direct
5901 // call to the runtime not using a type checking slow path).
5902 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005903 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005904 break;
5905 }
5906 __ Bind(&done);
5907
Roland Levillain0d5a2812015-11-13 10:07:31 +00005908 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005909}
5910
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005911void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5912 LocationSummary* locations =
5913 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5914 InvokeRuntimeCallingConvention calling_convention;
5915 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5916}
5917
5918void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005919 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5920 : QUICK_ENTRY_POINT(pUnlockObject),
5921 instruction,
5922 instruction->GetDexPc(),
5923 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005924 if (instruction->IsEnter()) {
5925 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5926 } else {
5927 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5928 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005929}
5930
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005931void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5932void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5933void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5934
5935void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5936 LocationSummary* locations =
5937 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5938 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5939 || instruction->GetResultType() == Primitive::kPrimLong);
5940 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005941 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005942 locations->SetOut(Location::SameAsFirstInput());
5943}
5944
5945void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5946 HandleBitwiseOperation(instruction);
5947}
5948
5949void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5950 HandleBitwiseOperation(instruction);
5951}
5952
5953void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5954 HandleBitwiseOperation(instruction);
5955}
5956
5957void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5958 LocationSummary* locations = instruction->GetLocations();
5959 Location first = locations->InAt(0);
5960 Location second = locations->InAt(1);
5961 DCHECK(first.Equals(locations->Out()));
5962
5963 if (instruction->GetResultType() == Primitive::kPrimInt) {
5964 if (second.IsRegister()) {
5965 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005966 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005967 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005968 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005969 } else {
5970 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005971 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005972 }
5973 } else if (second.IsConstant()) {
5974 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5975 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005976 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005977 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005978 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005979 } else {
5980 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005981 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005982 }
5983 } else {
5984 Address address(CpuRegister(RSP), second.GetStackIndex());
5985 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005986 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005987 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005988 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005989 } else {
5990 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005991 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005992 }
5993 }
5994 } else {
5995 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005996 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5997 bool second_is_constant = false;
5998 int64_t value = 0;
5999 if (second.IsConstant()) {
6000 second_is_constant = true;
6001 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006002 }
Mark Mendell40741f32015-04-20 22:10:34 -04006003 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006004
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006005 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006006 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006007 if (is_int32_value) {
6008 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6009 } else {
6010 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6011 }
6012 } else if (second.IsDoubleStackSlot()) {
6013 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006014 } else {
6015 __ andq(first_reg, second.AsRegister<CpuRegister>());
6016 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006017 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006018 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006019 if (is_int32_value) {
6020 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6021 } else {
6022 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6023 }
6024 } else if (second.IsDoubleStackSlot()) {
6025 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006026 } else {
6027 __ orq(first_reg, second.AsRegister<CpuRegister>());
6028 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006029 } else {
6030 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006031 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006032 if (is_int32_value) {
6033 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6034 } else {
6035 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6036 }
6037 } else if (second.IsDoubleStackSlot()) {
6038 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006039 } else {
6040 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6041 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006042 }
6043 }
6044}
6045
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006046void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6047 Location out,
6048 uint32_t offset,
6049 Location temp) {
6050 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6051 if (kEmitCompilerReadBarrier) {
6052 if (kUseBakerReadBarrier) {
6053 // Load with fast path based Baker's read barrier.
6054 // /* HeapReference<Object> */ out = *(out + offset)
6055 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6056 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
6057 } else {
6058 // Load with slow path based read barrier.
6059 // Save the value of `out` into `temp` before overwriting it
6060 // in the following move operation, as we will need it for the
6061 // read barrier below.
6062 __ movl(temp.AsRegister<CpuRegister>(), out_reg);
6063 // /* HeapReference<Object> */ out = *(out + offset)
6064 __ movl(out_reg, Address(out_reg, offset));
6065 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
6066 }
6067 } else {
6068 // Plain load with no read barrier.
6069 // /* HeapReference<Object> */ out = *(out + offset)
6070 __ movl(out_reg, Address(out_reg, offset));
6071 __ MaybeUnpoisonHeapReference(out_reg);
6072 }
6073}
6074
6075void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6076 Location out,
6077 Location obj,
6078 uint32_t offset,
6079 Location temp) {
6080 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6081 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6082 if (kEmitCompilerReadBarrier) {
6083 if (kUseBakerReadBarrier) {
6084 // Load with fast path based Baker's read barrier.
6085 // /* HeapReference<Object> */ out = *(obj + offset)
6086 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6087 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
6088 } else {
6089 // Load with slow path based read barrier.
6090 // /* HeapReference<Object> */ out = *(obj + offset)
6091 __ movl(out_reg, Address(obj_reg, offset));
6092 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6093 }
6094 } else {
6095 // Plain load with no read barrier.
6096 // /* HeapReference<Object> */ out = *(obj + offset)
6097 __ movl(out_reg, Address(obj_reg, offset));
6098 __ MaybeUnpoisonHeapReference(out_reg);
6099 }
6100}
6101
6102void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6103 Location root,
6104 CpuRegister obj,
6105 uint32_t offset) {
6106 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6107 if (kEmitCompilerReadBarrier) {
6108 if (kUseBakerReadBarrier) {
6109 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6110 // Baker's read barrier are used:
6111 //
6112 // root = obj.field;
6113 // if (Thread::Current()->GetIsGcMarking()) {
6114 // root = ReadBarrier::Mark(root)
6115 // }
6116
6117 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6118 __ movl(root_reg, Address(obj, offset));
6119 static_assert(
6120 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6121 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6122 "have different sizes.");
6123 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6124 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6125 "have different sizes.");
6126
6127 // Slow path used to mark the GC root `root`.
6128 SlowPathCode* slow_path =
6129 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6130 codegen_->AddSlowPath(slow_path);
6131
6132 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6133 /* no_rip */ true),
6134 Immediate(0));
6135 __ j(kNotEqual, slow_path->GetEntryLabel());
6136 __ Bind(slow_path->GetExitLabel());
6137 } else {
6138 // GC root loaded through a slow path for read barriers other
6139 // than Baker's.
6140 // /* GcRoot<mirror::Object>* */ root = obj + offset
6141 __ leaq(root_reg, Address(obj, offset));
6142 // /* mirror::Object* */ root = root->Read()
6143 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6144 }
6145 } else {
6146 // Plain GC root load with no read barrier.
6147 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6148 __ movl(root_reg, Address(obj, offset));
6149 }
6150}
6151
6152void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6153 Location ref,
6154 CpuRegister obj,
6155 uint32_t offset,
6156 Location temp,
6157 bool needs_null_check) {
6158 DCHECK(kEmitCompilerReadBarrier);
6159 DCHECK(kUseBakerReadBarrier);
6160
6161 // /* HeapReference<Object> */ ref = *(obj + offset)
6162 Address src(obj, offset);
6163 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6164}
6165
6166void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6167 Location ref,
6168 CpuRegister obj,
6169 uint32_t data_offset,
6170 Location index,
6171 Location temp,
6172 bool needs_null_check) {
6173 DCHECK(kEmitCompilerReadBarrier);
6174 DCHECK(kUseBakerReadBarrier);
6175
6176 // /* HeapReference<Object> */ ref =
6177 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6178 Address src = index.IsConstant() ?
6179 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6180 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6181 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6182}
6183
6184void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6185 Location ref,
6186 CpuRegister obj,
6187 const Address& src,
6188 Location temp,
6189 bool needs_null_check) {
6190 DCHECK(kEmitCompilerReadBarrier);
6191 DCHECK(kUseBakerReadBarrier);
6192
6193 // In slow path based read barriers, the read barrier call is
6194 // inserted after the original load. However, in fast path based
6195 // Baker's read barriers, we need to perform the load of
6196 // mirror::Object::monitor_ *before* the original reference load.
6197 // This load-load ordering is required by the read barrier.
6198 // The fast path/slow path (for Baker's algorithm) should look like:
6199 //
6200 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6201 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6202 // HeapReference<Object> ref = *src; // Original reference load.
6203 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6204 // if (is_gray) {
6205 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6206 // }
6207 //
6208 // Note: the original implementation in ReadBarrier::Barrier is
6209 // slightly more complex as:
6210 // - it implements the load-load fence using a data dependency on
6211 // the high-bits of rb_state, which are expected to be all zeroes;
6212 // - it performs additional checks that we do not do here for
6213 // performance reasons.
6214
6215 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6216 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6217 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6218
6219 // /* int32_t */ monitor = obj->monitor_
6220 __ movl(temp_reg, Address(obj, monitor_offset));
6221 if (needs_null_check) {
6222 MaybeRecordImplicitNullCheck(instruction);
6223 }
6224 // /* LockWord */ lock_word = LockWord(monitor)
6225 static_assert(sizeof(LockWord) == sizeof(int32_t),
6226 "art::LockWord and int32_t have different sizes.");
6227 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6228 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6229 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6230 static_assert(
6231 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6232 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6233
6234 // Load fence to prevent load-load reordering.
6235 // Note that this is a no-op, thanks to the x86-64 memory model.
6236 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6237
6238 // The actual reference load.
6239 // /* HeapReference<Object> */ ref = *src
6240 __ movl(ref_reg, src);
6241
6242 // Object* ref = ref_addr->AsMirrorPtr()
6243 __ MaybeUnpoisonHeapReference(ref_reg);
6244
6245 // Slow path used to mark the object `ref` when it is gray.
6246 SlowPathCode* slow_path =
6247 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6248 AddSlowPath(slow_path);
6249
6250 // if (rb_state == ReadBarrier::gray_ptr_)
6251 // ref = ReadBarrier::Mark(ref);
6252 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6253 __ j(kEqual, slow_path->GetEntryLabel());
6254 __ Bind(slow_path->GetExitLabel());
6255}
6256
6257void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6258 Location out,
6259 Location ref,
6260 Location obj,
6261 uint32_t offset,
6262 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006263 DCHECK(kEmitCompilerReadBarrier);
6264
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006265 // Insert a slow path based read barrier *after* the reference load.
6266 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006267 // If heap poisoning is enabled, the unpoisoning of the loaded
6268 // reference will be carried out by the runtime within the slow
6269 // path.
6270 //
6271 // Note that `ref` currently does not get unpoisoned (when heap
6272 // poisoning is enabled), which is alright as the `ref` argument is
6273 // not used by the artReadBarrierSlow entry point.
6274 //
6275 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6276 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6277 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6278 AddSlowPath(slow_path);
6279
Roland Levillain0d5a2812015-11-13 10:07:31 +00006280 __ jmp(slow_path->GetEntryLabel());
6281 __ Bind(slow_path->GetExitLabel());
6282}
6283
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006284void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6285 Location out,
6286 Location ref,
6287 Location obj,
6288 uint32_t offset,
6289 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006290 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006291 // Baker's read barriers shall be handled by the fast path
6292 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6293 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006294 // If heap poisoning is enabled, unpoisoning will be taken care of
6295 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006296 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006297 } else if (kPoisonHeapReferences) {
6298 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6299 }
6300}
6301
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006302void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6303 Location out,
6304 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006305 DCHECK(kEmitCompilerReadBarrier);
6306
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006307 // Insert a slow path based read barrier *after* the GC root load.
6308 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006309 // Note that GC roots are not affected by heap poisoning, so we do
6310 // not need to do anything special for this here.
6311 SlowPathCode* slow_path =
6312 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6313 AddSlowPath(slow_path);
6314
Roland Levillain0d5a2812015-11-13 10:07:31 +00006315 __ jmp(slow_path->GetEntryLabel());
6316 __ Bind(slow_path->GetExitLabel());
6317}
6318
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006319void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006320 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006321 LOG(FATAL) << "Unreachable";
6322}
6323
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006324void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006325 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006326 LOG(FATAL) << "Unreachable";
6327}
6328
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006329void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
6330 DCHECK(codegen_->IsBaseline());
6331 LocationSummary* locations =
6332 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6333 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6334}
6335
6336void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6337 DCHECK(codegen_->IsBaseline());
6338 // Will be generated at use site.
6339}
6340
Mark Mendellfe57faa2015-09-18 09:26:15 -04006341// Simple implementation of packed switch - generate cascaded compare/jumps.
6342void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6343 LocationSummary* locations =
6344 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6345 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006346 locations->AddTemp(Location::RequiresRegister());
6347 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006348}
6349
6350void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6351 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006352 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006353 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006354 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6355 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6356 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006357 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6358
6359 // Should we generate smaller inline compare/jumps?
6360 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6361 // Figure out the correct compare values and jump conditions.
6362 // Handle the first compare/branch as a special case because it might
6363 // jump to the default case.
6364 DCHECK_GT(num_entries, 2u);
6365 Condition first_condition;
6366 uint32_t index;
6367 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6368 if (lower_bound != 0) {
6369 first_condition = kLess;
6370 __ cmpl(value_reg_in, Immediate(lower_bound));
6371 __ j(first_condition, codegen_->GetLabelOf(default_block));
6372 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6373
6374 index = 1;
6375 } else {
6376 // Handle all the compare/jumps below.
6377 first_condition = kBelow;
6378 index = 0;
6379 }
6380
6381 // Handle the rest of the compare/jumps.
6382 for (; index + 1 < num_entries; index += 2) {
6383 int32_t compare_to_value = lower_bound + index + 1;
6384 __ cmpl(value_reg_in, Immediate(compare_to_value));
6385 // Jump to successors[index] if value < case_value[index].
6386 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6387 // Jump to successors[index + 1] if value == case_value[index + 1].
6388 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6389 }
6390
6391 if (index != num_entries) {
6392 // There are an odd number of entries. Handle the last one.
6393 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006394 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006395 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6396 }
6397
6398 // And the default for any other value.
6399 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6400 __ jmp(codegen_->GetLabelOf(default_block));
6401 }
6402 return;
6403 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006404
6405 // Remove the bias, if needed.
6406 Register value_reg_out = value_reg_in.AsRegister();
6407 if (lower_bound != 0) {
6408 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6409 value_reg_out = temp_reg.AsRegister();
6410 }
6411 CpuRegister value_reg(value_reg_out);
6412
6413 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006414 __ cmpl(value_reg, Immediate(num_entries - 1));
6415 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006416
Mark Mendell9c86b482015-09-18 13:36:07 -04006417 // We are in the range of the table.
6418 // Load the address of the jump table in the constant area.
6419 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006420
Mark Mendell9c86b482015-09-18 13:36:07 -04006421 // Load the (signed) offset from the jump table.
6422 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6423
6424 // Add the offset to the address of the table base.
6425 __ addq(temp_reg, base_reg);
6426
6427 // And jump.
6428 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006429}
6430
Mark Mendell92e83bf2015-05-07 11:25:03 -04006431void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6432 if (value == 0) {
6433 __ xorl(dest, dest);
6434 } else if (value > 0 && IsInt<32>(value)) {
6435 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
6436 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6437 } else {
6438 __ movq(dest, Immediate(value));
6439 }
6440}
6441
Mark Mendellcfa410b2015-05-25 16:02:44 -04006442void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6443 DCHECK(dest.IsDoubleStackSlot());
6444 if (IsInt<32>(value)) {
6445 // Can move directly as an int32 constant.
6446 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6447 Immediate(static_cast<int32_t>(value)));
6448 } else {
6449 Load64BitValue(CpuRegister(TMP), value);
6450 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6451 }
6452}
6453
Mark Mendell9c86b482015-09-18 13:36:07 -04006454/**
6455 * Class to handle late fixup of offsets into constant area.
6456 */
6457class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6458 public:
6459 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6460 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6461
6462 protected:
6463 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6464
6465 CodeGeneratorX86_64* codegen_;
6466
6467 private:
6468 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6469 // Patch the correct offset for the instruction. We use the address of the
6470 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6471 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6472 int32_t relative_position = constant_offset - pos;
6473
6474 // Patch in the right value.
6475 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6476 }
6477
6478 // Location in constant area that the fixup refers to.
6479 size_t offset_into_constant_area_;
6480};
6481
6482/**
6483 t * Class to handle late fixup of offsets to a jump table that will be created in the
6484 * constant area.
6485 */
6486class JumpTableRIPFixup : public RIPFixup {
6487 public:
6488 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6489 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6490
6491 void CreateJumpTable() {
6492 X86_64Assembler* assembler = codegen_->GetAssembler();
6493
6494 // Ensure that the reference to the jump table has the correct offset.
6495 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6496 SetOffset(offset_in_constant_table);
6497
6498 // Compute the offset from the start of the function to this jump table.
6499 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6500
6501 // Populate the jump table with the correct values for the jump table.
6502 int32_t num_entries = switch_instr_->GetNumEntries();
6503 HBasicBlock* block = switch_instr_->GetBlock();
6504 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6505 // The value that we want is the target offset - the position of the table.
6506 for (int32_t i = 0; i < num_entries; i++) {
6507 HBasicBlock* b = successors[i];
6508 Label* l = codegen_->GetLabelOf(b);
6509 DCHECK(l->IsBound());
6510 int32_t offset_to_block = l->Position() - current_table_offset;
6511 assembler->AppendInt32(offset_to_block);
6512 }
6513 }
6514
6515 private:
6516 const HPackedSwitch* switch_instr_;
6517};
6518
Mark Mendellf55c3e02015-03-26 21:07:46 -04006519void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6520 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006521 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006522 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6523 // 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 -04006524 assembler->Align(4, 0);
6525 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006526
6527 // Populate any jump tables.
6528 for (auto jump_table : fixups_to_jump_tables_) {
6529 jump_table->CreateJumpTable();
6530 }
6531
6532 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006533 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006534 }
6535
6536 // And finish up.
6537 CodeGenerator::Finalize(allocator);
6538}
6539
Mark Mendellf55c3e02015-03-26 21:07:46 -04006540Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6541 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6542 return Address::RIP(fixup);
6543}
6544
6545Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6546 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6547 return Address::RIP(fixup);
6548}
6549
6550Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6551 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6552 return Address::RIP(fixup);
6553}
6554
6555Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6556 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6557 return Address::RIP(fixup);
6558}
6559
Andreas Gampe85b62f22015-09-09 13:15:38 -07006560// TODO: trg as memory.
6561void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6562 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006563 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006564 return;
6565 }
6566
6567 DCHECK_NE(type, Primitive::kPrimVoid);
6568
6569 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6570 if (trg.Equals(return_loc)) {
6571 return;
6572 }
6573
6574 // Let the parallel move resolver take care of all of this.
6575 HParallelMove parallel_move(GetGraph()->GetArena());
6576 parallel_move.AddMove(return_loc, trg, type, nullptr);
6577 GetMoveResolver()->EmitNativeCode(&parallel_move);
6578}
6579
Mark Mendell9c86b482015-09-18 13:36:07 -04006580Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6581 // Create a fixup to be used to create and address the jump table.
6582 JumpTableRIPFixup* table_fixup =
6583 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6584
6585 // We have to populate the jump tables.
6586 fixups_to_jump_tables_.push_back(table_fixup);
6587 return Address::RIP(table_fixup);
6588}
6589
Mark Mendellea5af682015-10-22 17:35:49 -04006590void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6591 const Address& addr_high,
6592 int64_t v,
6593 HInstruction* instruction) {
6594 if (IsInt<32>(v)) {
6595 int32_t v_32 = v;
6596 __ movq(addr_low, Immediate(v_32));
6597 MaybeRecordImplicitNullCheck(instruction);
6598 } else {
6599 // Didn't fit in a register. Do it in pieces.
6600 int32_t low_v = Low32Bits(v);
6601 int32_t high_v = High32Bits(v);
6602 __ movl(addr_low, Immediate(low_v));
6603 MaybeRecordImplicitNullCheck(instruction);
6604 __ movl(addr_high, Immediate(high_v));
6605 }
6606}
6607
Roland Levillain4d027112015-07-01 15:41:14 +01006608#undef __
6609
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006610} // namespace x86_64
6611} // namespace art