blob: 76a4ce2e930198d0d42de4e130d03f20a486246f [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 Mendell152408f2015-12-31 12:28:50 -05001373template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001374void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001375 LabelType* true_label,
1376 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001377 if (cond->IsFPConditionTrueIfNaN()) {
1378 __ j(kUnordered, true_label);
1379 } else if (cond->IsFPConditionFalseIfNaN()) {
1380 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001381 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001382 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001383}
1384
Mark Mendell152408f2015-12-31 12:28:50 -05001385template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001386void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001387 LabelType* true_target_in,
1388 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001389 // Generated branching requires both targets to be explicit. If either of the
1390 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001391 LabelType fallthrough_target;
1392 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1393 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001394
Mark Mendellc4701932015-04-10 13:18:51 -04001395 LocationSummary* locations = condition->GetLocations();
1396 Location left = locations->InAt(0);
1397 Location right = locations->InAt(1);
1398
Mark Mendellc4701932015-04-10 13:18:51 -04001399 Primitive::Type type = condition->InputAt(0)->GetType();
1400 switch (type) {
1401 case Primitive::kPrimLong: {
1402 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1403 if (right.IsConstant()) {
1404 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1405 if (IsInt<32>(value)) {
1406 if (value == 0) {
1407 __ testq(left_reg, left_reg);
1408 } else {
1409 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1410 }
1411 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001412 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001413 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1414 }
1415 } else if (right.IsDoubleStackSlot()) {
1416 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1417 } else {
1418 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1419 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001420 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001421 break;
1422 }
1423 case Primitive::kPrimFloat: {
1424 if (right.IsFpuRegister()) {
1425 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1426 } else if (right.IsConstant()) {
1427 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1428 codegen_->LiteralFloatAddress(
1429 right.GetConstant()->AsFloatConstant()->GetValue()));
1430 } else {
1431 DCHECK(right.IsStackSlot());
1432 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1433 Address(CpuRegister(RSP), right.GetStackIndex()));
1434 }
1435 GenerateFPJumps(condition, true_target, false_target);
1436 break;
1437 }
1438 case Primitive::kPrimDouble: {
1439 if (right.IsFpuRegister()) {
1440 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1441 } else if (right.IsConstant()) {
1442 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1443 codegen_->LiteralDoubleAddress(
1444 right.GetConstant()->AsDoubleConstant()->GetValue()));
1445 } else {
1446 DCHECK(right.IsDoubleStackSlot());
1447 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1448 Address(CpuRegister(RSP), right.GetStackIndex()));
1449 }
1450 GenerateFPJumps(condition, true_target, false_target);
1451 break;
1452 }
1453 default:
1454 LOG(FATAL) << "Unexpected condition type " << type;
1455 }
1456
David Brazdil0debae72015-11-12 18:37:00 +00001457 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001458 __ jmp(false_target);
1459 }
David Brazdil0debae72015-11-12 18:37:00 +00001460
1461 if (fallthrough_target.IsLinked()) {
1462 __ Bind(&fallthrough_target);
1463 }
Mark Mendellc4701932015-04-10 13:18:51 -04001464}
1465
David Brazdil0debae72015-11-12 18:37:00 +00001466static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1467 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1468 // are set only strictly before `branch`. We can't use the eflags on long
1469 // conditions if they are materialized due to the complex branching.
1470 return cond->IsCondition() &&
1471 cond->GetNext() == branch &&
1472 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1473}
1474
Mark Mendell152408f2015-12-31 12:28:50 -05001475template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001476void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001477 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001478 LabelType* true_target,
1479 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001480 HInstruction* cond = instruction->InputAt(condition_input_index);
1481
1482 if (true_target == nullptr && false_target == nullptr) {
1483 // Nothing to do. The code always falls through.
1484 return;
1485 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001486 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001487 if (cond->AsIntConstant()->IsOne()) {
1488 if (true_target != nullptr) {
1489 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001490 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001491 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001492 DCHECK(cond->AsIntConstant()->IsZero());
1493 if (false_target != nullptr) {
1494 __ jmp(false_target);
1495 }
1496 }
1497 return;
1498 }
1499
1500 // The following code generates these patterns:
1501 // (1) true_target == nullptr && false_target != nullptr
1502 // - opposite condition true => branch to false_target
1503 // (2) true_target != nullptr && false_target == nullptr
1504 // - condition true => branch to true_target
1505 // (3) true_target != nullptr && false_target != nullptr
1506 // - condition true => branch to true_target
1507 // - branch to false_target
1508 if (IsBooleanValueOrMaterializedCondition(cond)) {
1509 if (AreEflagsSetFrom(cond, instruction)) {
1510 if (true_target == nullptr) {
1511 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1512 } else {
1513 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1514 }
1515 } else {
1516 // Materialized condition, compare against 0.
1517 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1518 if (lhs.IsRegister()) {
1519 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1520 } else {
1521 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1522 }
1523 if (true_target == nullptr) {
1524 __ j(kEqual, false_target);
1525 } else {
1526 __ j(kNotEqual, true_target);
1527 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001528 }
1529 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001530 // Condition has not been materialized, use its inputs as the
1531 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001532 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001533
David Brazdil0debae72015-11-12 18:37:00 +00001534 // If this is a long or FP comparison that has been folded into
1535 // the HCondition, generate the comparison directly.
1536 Primitive::Type type = condition->InputAt(0)->GetType();
1537 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1538 GenerateCompareTestAndBranch(condition, true_target, false_target);
1539 return;
1540 }
1541
1542 Location lhs = condition->GetLocations()->InAt(0);
1543 Location rhs = condition->GetLocations()->InAt(1);
1544 if (rhs.IsRegister()) {
1545 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1546 } else if (rhs.IsConstant()) {
1547 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1548 if (constant == 0) {
1549 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001550 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001551 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001552 }
1553 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001554 __ cmpl(lhs.AsRegister<CpuRegister>(),
1555 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1556 }
1557 if (true_target == nullptr) {
1558 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1559 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001560 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001561 }
Dave Allison20dfc792014-06-16 20:44:29 -07001562 }
David Brazdil0debae72015-11-12 18:37:00 +00001563
1564 // If neither branch falls through (case 3), the conditional branch to `true_target`
1565 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1566 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001567 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001568 }
1569}
1570
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001571void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001572 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1573 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001574 locations->SetInAt(0, Location::Any());
1575 }
1576}
1577
1578void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001579 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1580 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1581 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1582 nullptr : codegen_->GetLabelOf(true_successor);
1583 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1584 nullptr : codegen_->GetLabelOf(false_successor);
1585 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001586}
1587
1588void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1589 LocationSummary* locations = new (GetGraph()->GetArena())
1590 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001591 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001592 locations->SetInAt(0, Location::Any());
1593 }
1594}
1595
1596void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001597 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001598 DeoptimizationSlowPathX86_64(deoptimize);
1599 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001600 GenerateTestAndBranch(deoptimize,
1601 /* condition_input_index */ 0,
1602 slow_path->GetEntryLabel(),
Mark Mendell152408f2015-12-31 12:28:50 -05001603 /* false_target */ static_cast<Label*>(nullptr));
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001604}
1605
David Srbecky0cf44932015-12-09 14:09:59 +00001606void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1607 new (GetGraph()->GetArena()) LocationSummary(info);
1608}
1609
1610void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1611 codegen_->RecordPcInfo(info, info->GetDexPc());
1612}
1613
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001614void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1615 local->SetLocations(nullptr);
1616}
1617
1618void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1619 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1620}
1621
1622void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1623 local->SetLocations(nullptr);
1624}
1625
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001626void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001627 // Nothing to do, this is driven by the code generator.
1628}
1629
1630void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001631 LocationSummary* locations =
1632 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001633 switch (store->InputAt(1)->GetType()) {
1634 case Primitive::kPrimBoolean:
1635 case Primitive::kPrimByte:
1636 case Primitive::kPrimChar:
1637 case Primitive::kPrimShort:
1638 case Primitive::kPrimInt:
1639 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001640 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001641 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1642 break;
1643
1644 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001645 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001646 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1647 break;
1648
1649 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001650 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001651 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001652}
1653
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001654void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001655}
1656
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001657void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001658 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001659 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001660 // Handle the long/FP comparisons made in instruction simplification.
1661 switch (cond->InputAt(0)->GetType()) {
1662 case Primitive::kPrimLong:
1663 locations->SetInAt(0, Location::RequiresRegister());
1664 locations->SetInAt(1, Location::Any());
1665 break;
1666 case Primitive::kPrimFloat:
1667 case Primitive::kPrimDouble:
1668 locations->SetInAt(0, Location::RequiresFpuRegister());
1669 locations->SetInAt(1, Location::Any());
1670 break;
1671 default:
1672 locations->SetInAt(0, Location::RequiresRegister());
1673 locations->SetInAt(1, Location::Any());
1674 break;
1675 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001676 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001677 locations->SetOut(Location::RequiresRegister());
1678 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001679}
1680
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001681void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001682 if (!cond->NeedsMaterialization()) {
1683 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001684 }
Mark Mendellc4701932015-04-10 13:18:51 -04001685
1686 LocationSummary* locations = cond->GetLocations();
1687 Location lhs = locations->InAt(0);
1688 Location rhs = locations->InAt(1);
1689 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001690 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001691
1692 switch (cond->InputAt(0)->GetType()) {
1693 default:
1694 // Integer case.
1695
1696 // Clear output register: setcc only sets the low byte.
1697 __ xorl(reg, reg);
1698
1699 if (rhs.IsRegister()) {
1700 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1701 } else if (rhs.IsConstant()) {
1702 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1703 if (constant == 0) {
1704 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1705 } else {
1706 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1707 }
1708 } else {
1709 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1710 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001711 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001712 return;
1713 case Primitive::kPrimLong:
1714 // Clear output register: setcc only sets the low byte.
1715 __ xorl(reg, reg);
1716
1717 if (rhs.IsRegister()) {
1718 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1719 } else if (rhs.IsConstant()) {
1720 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1721 if (IsInt<32>(value)) {
1722 if (value == 0) {
1723 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1724 } else {
1725 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1726 }
1727 } else {
1728 // Value won't fit in an int.
1729 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1730 }
1731 } else {
1732 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1733 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001734 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001735 return;
1736 case Primitive::kPrimFloat: {
1737 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1738 if (rhs.IsConstant()) {
1739 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1740 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1741 } else if (rhs.IsStackSlot()) {
1742 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1743 } else {
1744 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1745 }
1746 GenerateFPJumps(cond, &true_label, &false_label);
1747 break;
1748 }
1749 case Primitive::kPrimDouble: {
1750 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1751 if (rhs.IsConstant()) {
1752 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1753 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1754 } else if (rhs.IsDoubleStackSlot()) {
1755 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1756 } else {
1757 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1758 }
1759 GenerateFPJumps(cond, &true_label, &false_label);
1760 break;
1761 }
1762 }
1763
1764 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001765 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001766
Roland Levillain4fa13f62015-07-06 18:11:54 +01001767 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001768 __ Bind(&false_label);
1769 __ xorl(reg, reg);
1770 __ jmp(&done_label);
1771
Roland Levillain4fa13f62015-07-06 18:11:54 +01001772 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001773 __ Bind(&true_label);
1774 __ movl(reg, Immediate(1));
1775 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001776}
1777
1778void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001779 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001780}
1781
1782void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001784}
1785
1786void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001788}
1789
1790void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001791 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001792}
1793
1794void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001795 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001796}
1797
1798void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001799 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001800}
1801
1802void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001803 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001804}
1805
1806void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001807 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001808}
1809
1810void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001811 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001812}
1813
1814void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001815 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001816}
1817
1818void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001819 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001820}
1821
1822void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001824}
1825
Aart Bike9f37602015-10-09 11:15:55 -07001826void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001827 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001828}
1829
1830void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001831 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001832}
1833
1834void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001835 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001836}
1837
1838void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001839 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001840}
1841
1842void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001843 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001844}
1845
1846void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001847 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001848}
1849
1850void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001851 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001852}
1853
1854void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001855 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001856}
1857
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001858void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001859 LocationSummary* locations =
1860 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001861 switch (compare->InputAt(0)->GetType()) {
1862 case Primitive::kPrimLong: {
1863 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001864 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1866 break;
1867 }
1868 case Primitive::kPrimFloat:
1869 case Primitive::kPrimDouble: {
1870 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001871 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001872 locations->SetOut(Location::RequiresRegister());
1873 break;
1874 }
1875 default:
1876 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1877 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001878}
1879
1880void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001881 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001882 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001883 Location left = locations->InAt(0);
1884 Location right = locations->InAt(1);
1885
Mark Mendell0c9497d2015-08-21 09:30:05 -04001886 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001887 Primitive::Type type = compare->InputAt(0)->GetType();
1888 switch (type) {
1889 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001890 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1891 if (right.IsConstant()) {
1892 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001893 if (IsInt<32>(value)) {
1894 if (value == 0) {
1895 __ testq(left_reg, left_reg);
1896 } else {
1897 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1898 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001899 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001900 // Value won't fit in an int.
1901 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001902 }
Mark Mendell40741f32015-04-20 22:10:34 -04001903 } else if (right.IsDoubleStackSlot()) {
1904 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001905 } else {
1906 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1907 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001908 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001909 }
1910 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001911 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1912 if (right.IsConstant()) {
1913 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1914 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1915 } else if (right.IsStackSlot()) {
1916 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1917 } else {
1918 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1919 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001920 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1921 break;
1922 }
1923 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001924 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1925 if (right.IsConstant()) {
1926 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1927 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1928 } else if (right.IsDoubleStackSlot()) {
1929 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1930 } else {
1931 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1932 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001933 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1934 break;
1935 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001936 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001937 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001938 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001939 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001940 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001941 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001942
Calin Juravle91debbc2014-11-26 19:01:09 +00001943 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001944 __ movl(out, Immediate(1));
1945 __ jmp(&done);
1946
1947 __ Bind(&less);
1948 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001949
1950 __ Bind(&done);
1951}
1952
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001953void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001954 LocationSummary* locations =
1955 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001956 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001957}
1958
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001959void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001960 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001961}
1962
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001963void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1964 LocationSummary* locations =
1965 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1966 locations->SetOut(Location::ConstantLocation(constant));
1967}
1968
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001969void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001970 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001971}
1972
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001973void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001974 LocationSummary* locations =
1975 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001976 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001977}
1978
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001979void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001980 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001981}
1982
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001983void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1984 LocationSummary* locations =
1985 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1986 locations->SetOut(Location::ConstantLocation(constant));
1987}
1988
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001989void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001990 // Will be generated at use site.
1991}
1992
1993void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1994 LocationSummary* locations =
1995 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1996 locations->SetOut(Location::ConstantLocation(constant));
1997}
1998
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001999void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2000 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002001 // Will be generated at use site.
2002}
2003
Calin Juravle27df7582015-04-17 19:12:31 +01002004void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2005 memory_barrier->SetLocations(nullptr);
2006}
2007
2008void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002009 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002010}
2011
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002012void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2013 ret->SetLocations(nullptr);
2014}
2015
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002016void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002017 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002018}
2019
2020void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002023 switch (ret->InputAt(0)->GetType()) {
2024 case Primitive::kPrimBoolean:
2025 case Primitive::kPrimByte:
2026 case Primitive::kPrimChar:
2027 case Primitive::kPrimShort:
2028 case Primitive::kPrimInt:
2029 case Primitive::kPrimNot:
2030 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002031 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002032 break;
2033
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002034 case Primitive::kPrimFloat:
2035 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002036 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002037 break;
2038
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002039 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002040 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002041 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002042}
2043
2044void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2045 if (kIsDebugBuild) {
2046 switch (ret->InputAt(0)->GetType()) {
2047 case Primitive::kPrimBoolean:
2048 case Primitive::kPrimByte:
2049 case Primitive::kPrimChar:
2050 case Primitive::kPrimShort:
2051 case Primitive::kPrimInt:
2052 case Primitive::kPrimNot:
2053 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002054 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002055 break;
2056
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002057 case Primitive::kPrimFloat:
2058 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002059 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002060 XMM0);
2061 break;
2062
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002063 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002064 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002065 }
2066 }
2067 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002068}
2069
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002070Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2071 switch (type) {
2072 case Primitive::kPrimBoolean:
2073 case Primitive::kPrimByte:
2074 case Primitive::kPrimChar:
2075 case Primitive::kPrimShort:
2076 case Primitive::kPrimInt:
2077 case Primitive::kPrimNot:
2078 case Primitive::kPrimLong:
2079 return Location::RegisterLocation(RAX);
2080
2081 case Primitive::kPrimVoid:
2082 return Location::NoLocation();
2083
2084 case Primitive::kPrimDouble:
2085 case Primitive::kPrimFloat:
2086 return Location::FpuRegisterLocation(XMM0);
2087 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002088
2089 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002090}
2091
2092Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2093 return Location::RegisterLocation(kMethodRegisterArgument);
2094}
2095
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002096Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002097 switch (type) {
2098 case Primitive::kPrimBoolean:
2099 case Primitive::kPrimByte:
2100 case Primitive::kPrimChar:
2101 case Primitive::kPrimShort:
2102 case Primitive::kPrimInt:
2103 case Primitive::kPrimNot: {
2104 uint32_t index = gp_index_++;
2105 stack_index_++;
2106 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002107 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002108 } else {
2109 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2110 }
2111 }
2112
2113 case Primitive::kPrimLong: {
2114 uint32_t index = gp_index_;
2115 stack_index_ += 2;
2116 if (index < calling_convention.GetNumberOfRegisters()) {
2117 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002118 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002119 } else {
2120 gp_index_ += 2;
2121 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2122 }
2123 }
2124
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002125 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002126 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002127 stack_index_++;
2128 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002129 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002130 } else {
2131 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2132 }
2133 }
2134
2135 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002136 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002137 stack_index_ += 2;
2138 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002139 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002140 } else {
2141 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2142 }
2143 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002144
2145 case Primitive::kPrimVoid:
2146 LOG(FATAL) << "Unexpected parameter type " << type;
2147 break;
2148 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002149 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002150}
2151
Calin Juravle175dc732015-08-25 15:42:32 +01002152void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2153 // The trampoline uses the same calling convention as dex calling conventions,
2154 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2155 // the method_idx.
2156 HandleInvoke(invoke);
2157}
2158
2159void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2160 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2161}
2162
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002163void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002164 // When we do not run baseline, explicit clinit checks triggered by static
2165 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2166 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002167
Mark Mendellfb8d2792015-03-31 22:16:59 -04002168 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002169 if (intrinsic.TryDispatch(invoke)) {
2170 return;
2171 }
2172
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002173 HandleInvoke(invoke);
2174}
2175
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002176static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2177 if (invoke->GetLocations()->Intrinsified()) {
2178 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2179 intrinsic.Dispatch(invoke);
2180 return true;
2181 }
2182 return false;
2183}
2184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002185void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002186 // When we do not run baseline, explicit clinit checks triggered by static
2187 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2188 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002189
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002190 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2191 return;
2192 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002193
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002194 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002195 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002196 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002197 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002198}
2199
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002200void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002201 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002202 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002203}
2204
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002205void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002206 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002207 if (intrinsic.TryDispatch(invoke)) {
2208 return;
2209 }
2210
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002211 HandleInvoke(invoke);
2212}
2213
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002214void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002215 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2216 return;
2217 }
2218
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002219 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002220 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002221 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002222}
2223
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002224void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2225 HandleInvoke(invoke);
2226 // Add the hidden argument.
2227 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2228}
2229
2230void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2231 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002232 LocationSummary* locations = invoke->GetLocations();
2233 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2234 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002235 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2236 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002237 Location receiver = locations->InAt(0);
2238 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2239
Roland Levillain0d5a2812015-11-13 10:07:31 +00002240 // Set the hidden argument. This is safe to do this here, as RAX
2241 // won't be modified thereafter, before the `call` instruction.
2242 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002243 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002244
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002245 if (receiver.IsStackSlot()) {
2246 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002247 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002248 __ movl(temp, Address(temp, class_offset));
2249 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002250 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002251 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002252 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002253 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002254 // Instead of simply (possibly) unpoisoning `temp` here, we should
2255 // emit a read barrier for the previous class reference load.
2256 // However this is not required in practice, as this is an
2257 // intermediate/temporary reference and because the current
2258 // concurrent copying collector keeps the from-space memory
2259 // intact/accessible until the end of the marking phase (the
2260 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002261 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002262 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002263 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 __ call(Address(temp,
2266 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002267
2268 DCHECK(!codegen_->IsLeafMethod());
2269 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2270}
2271
Roland Levillain88cb1752014-10-20 16:36:47 +01002272void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2273 LocationSummary* locations =
2274 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2275 switch (neg->GetResultType()) {
2276 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002277 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002278 locations->SetInAt(0, Location::RequiresRegister());
2279 locations->SetOut(Location::SameAsFirstInput());
2280 break;
2281
Roland Levillain88cb1752014-10-20 16:36:47 +01002282 case Primitive::kPrimFloat:
2283 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002284 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002285 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002286 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002287 break;
2288
2289 default:
2290 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2291 }
2292}
2293
2294void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2295 LocationSummary* locations = neg->GetLocations();
2296 Location out = locations->Out();
2297 Location in = locations->InAt(0);
2298 switch (neg->GetResultType()) {
2299 case Primitive::kPrimInt:
2300 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002301 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002303 break;
2304
2305 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002306 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002307 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002308 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002309 break;
2310
Roland Levillain5368c212014-11-27 15:03:41 +00002311 case Primitive::kPrimFloat: {
2312 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002313 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002314 // Implement float negation with an exclusive or with value
2315 // 0x80000000 (mask for bit 31, representing the sign of a
2316 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002317 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002318 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002319 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002320 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002321
Roland Levillain5368c212014-11-27 15:03:41 +00002322 case Primitive::kPrimDouble: {
2323 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002324 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002325 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002326 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002327 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002328 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002329 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002330 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002331 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002332
2333 default:
2334 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2335 }
2336}
2337
Roland Levillaindff1f282014-11-05 14:15:05 +00002338void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2339 LocationSummary* locations =
2340 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2341 Primitive::Type result_type = conversion->GetResultType();
2342 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002343 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002344
David Brazdilb2bd1c52015-03-25 11:17:37 +00002345 // The Java language does not allow treating boolean as an integral type but
2346 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002347
Roland Levillaindff1f282014-11-05 14:15:05 +00002348 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002349 case Primitive::kPrimByte:
2350 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002351 case Primitive::kPrimBoolean:
2352 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002353 case Primitive::kPrimShort:
2354 case Primitive::kPrimInt:
2355 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002356 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002357 locations->SetInAt(0, Location::Any());
2358 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2359 break;
2360
2361 default:
2362 LOG(FATAL) << "Unexpected type conversion from " << input_type
2363 << " to " << result_type;
2364 }
2365 break;
2366
Roland Levillain01a8d712014-11-14 16:27:39 +00002367 case Primitive::kPrimShort:
2368 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002369 case Primitive::kPrimBoolean:
2370 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002371 case Primitive::kPrimByte:
2372 case Primitive::kPrimInt:
2373 case Primitive::kPrimChar:
2374 // Processing a Dex `int-to-short' instruction.
2375 locations->SetInAt(0, Location::Any());
2376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2377 break;
2378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 }
2383 break;
2384
Roland Levillain946e1432014-11-11 17:35:19 +00002385 case Primitive::kPrimInt:
2386 switch (input_type) {
2387 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002388 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002389 locations->SetInAt(0, Location::Any());
2390 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2391 break;
2392
2393 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002394 // Processing a Dex `float-to-int' instruction.
2395 locations->SetInAt(0, Location::RequiresFpuRegister());
2396 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002397 break;
2398
Roland Levillain946e1432014-11-11 17:35:19 +00002399 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002400 // Processing a Dex `double-to-int' instruction.
2401 locations->SetInAt(0, Location::RequiresFpuRegister());
2402 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002403 break;
2404
2405 default:
2406 LOG(FATAL) << "Unexpected type conversion from " << input_type
2407 << " to " << result_type;
2408 }
2409 break;
2410
Roland Levillaindff1f282014-11-05 14:15:05 +00002411 case Primitive::kPrimLong:
2412 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002413 case Primitive::kPrimBoolean:
2414 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002415 case Primitive::kPrimByte:
2416 case Primitive::kPrimShort:
2417 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002418 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002419 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002420 // TODO: We would benefit from a (to-be-implemented)
2421 // Location::RegisterOrStackSlot requirement for this input.
2422 locations->SetInAt(0, Location::RequiresRegister());
2423 locations->SetOut(Location::RequiresRegister());
2424 break;
2425
2426 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002427 // Processing a Dex `float-to-long' instruction.
2428 locations->SetInAt(0, Location::RequiresFpuRegister());
2429 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002430 break;
2431
Roland Levillaindff1f282014-11-05 14:15:05 +00002432 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002433 // Processing a Dex `double-to-long' instruction.
2434 locations->SetInAt(0, Location::RequiresFpuRegister());
2435 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002436 break;
2437
2438 default:
2439 LOG(FATAL) << "Unexpected type conversion from " << input_type
2440 << " to " << result_type;
2441 }
2442 break;
2443
Roland Levillain981e4542014-11-14 11:47:14 +00002444 case Primitive::kPrimChar:
2445 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002446 case Primitive::kPrimBoolean:
2447 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002448 case Primitive::kPrimByte:
2449 case Primitive::kPrimShort:
2450 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002451 // Processing a Dex `int-to-char' instruction.
2452 locations->SetInAt(0, Location::Any());
2453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2454 break;
2455
2456 default:
2457 LOG(FATAL) << "Unexpected type conversion from " << input_type
2458 << " to " << result_type;
2459 }
2460 break;
2461
Roland Levillaindff1f282014-11-05 14:15:05 +00002462 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002463 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002464 case Primitive::kPrimBoolean:
2465 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002466 case Primitive::kPrimByte:
2467 case Primitive::kPrimShort:
2468 case Primitive::kPrimInt:
2469 case Primitive::kPrimChar:
2470 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002471 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002472 locations->SetOut(Location::RequiresFpuRegister());
2473 break;
2474
2475 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002476 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002477 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002478 locations->SetOut(Location::RequiresFpuRegister());
2479 break;
2480
Roland Levillaincff13742014-11-17 14:32:17 +00002481 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002482 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002483 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002484 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002485 break;
2486
2487 default:
2488 LOG(FATAL) << "Unexpected type conversion from " << input_type
2489 << " to " << result_type;
2490 };
2491 break;
2492
Roland Levillaindff1f282014-11-05 14:15:05 +00002493 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002494 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002495 case Primitive::kPrimBoolean:
2496 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002497 case Primitive::kPrimByte:
2498 case Primitive::kPrimShort:
2499 case Primitive::kPrimInt:
2500 case Primitive::kPrimChar:
2501 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002502 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002503 locations->SetOut(Location::RequiresFpuRegister());
2504 break;
2505
2506 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002507 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002508 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002509 locations->SetOut(Location::RequiresFpuRegister());
2510 break;
2511
Roland Levillaincff13742014-11-17 14:32:17 +00002512 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002513 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002514 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002515 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002516 break;
2517
2518 default:
2519 LOG(FATAL) << "Unexpected type conversion from " << input_type
2520 << " to " << result_type;
2521 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002522 break;
2523
2524 default:
2525 LOG(FATAL) << "Unexpected type conversion from " << input_type
2526 << " to " << result_type;
2527 }
2528}
2529
2530void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2531 LocationSummary* locations = conversion->GetLocations();
2532 Location out = locations->Out();
2533 Location in = locations->InAt(0);
2534 Primitive::Type result_type = conversion->GetResultType();
2535 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002536 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002537 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002538 case Primitive::kPrimByte:
2539 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002540 case Primitive::kPrimBoolean:
2541 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002542 case Primitive::kPrimShort:
2543 case Primitive::kPrimInt:
2544 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002545 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002546 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002547 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002548 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002549 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002550 Address(CpuRegister(RSP), in.GetStackIndex()));
2551 } else {
2552 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002553 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002554 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2555 }
2556 break;
2557
2558 default:
2559 LOG(FATAL) << "Unexpected type conversion from " << input_type
2560 << " to " << result_type;
2561 }
2562 break;
2563
Roland Levillain01a8d712014-11-14 16:27:39 +00002564 case Primitive::kPrimShort:
2565 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002566 case Primitive::kPrimBoolean:
2567 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002568 case Primitive::kPrimByte:
2569 case Primitive::kPrimInt:
2570 case Primitive::kPrimChar:
2571 // Processing a Dex `int-to-short' instruction.
2572 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002573 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002574 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002575 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002576 Address(CpuRegister(RSP), in.GetStackIndex()));
2577 } else {
2578 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002579 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002580 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2581 }
2582 break;
2583
2584 default:
2585 LOG(FATAL) << "Unexpected type conversion from " << input_type
2586 << " to " << result_type;
2587 }
2588 break;
2589
Roland Levillain946e1432014-11-11 17:35:19 +00002590 case Primitive::kPrimInt:
2591 switch (input_type) {
2592 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002593 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002594 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002595 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002596 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002598 Address(CpuRegister(RSP), in.GetStackIndex()));
2599 } else {
2600 DCHECK(in.IsConstant());
2601 DCHECK(in.GetConstant()->IsLongConstant());
2602 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002604 }
2605 break;
2606
Roland Levillain3f8f9362014-12-02 17:45:01 +00002607 case Primitive::kPrimFloat: {
2608 // Processing a Dex `float-to-int' instruction.
2609 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2610 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002611 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002612
2613 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002614 // if input >= (float)INT_MAX goto done
2615 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002616 __ j(kAboveEqual, &done);
2617 // if input == NaN goto nan
2618 __ j(kUnordered, &nan);
2619 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002620 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002621 __ jmp(&done);
2622 __ Bind(&nan);
2623 // output = 0
2624 __ xorl(output, output);
2625 __ Bind(&done);
2626 break;
2627 }
2628
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002629 case Primitive::kPrimDouble: {
2630 // Processing a Dex `double-to-int' instruction.
2631 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2632 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002633 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002634
2635 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002636 // if input >= (double)INT_MAX goto done
2637 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002638 __ j(kAboveEqual, &done);
2639 // if input == NaN goto nan
2640 __ j(kUnordered, &nan);
2641 // output = double-to-int-truncate(input)
2642 __ cvttsd2si(output, input);
2643 __ jmp(&done);
2644 __ Bind(&nan);
2645 // output = 0
2646 __ xorl(output, output);
2647 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002648 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002649 }
Roland Levillain946e1432014-11-11 17:35:19 +00002650
2651 default:
2652 LOG(FATAL) << "Unexpected type conversion from " << input_type
2653 << " to " << result_type;
2654 }
2655 break;
2656
Roland Levillaindff1f282014-11-05 14:15:05 +00002657 case Primitive::kPrimLong:
2658 switch (input_type) {
2659 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002660 case Primitive::kPrimBoolean:
2661 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002662 case Primitive::kPrimByte:
2663 case Primitive::kPrimShort:
2664 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002665 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002666 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002667 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002668 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002669 break;
2670
Roland Levillain624279f2014-12-04 11:54:28 +00002671 case Primitive::kPrimFloat: {
2672 // Processing a Dex `float-to-long' instruction.
2673 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2674 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002675 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002676
Mark Mendell92e83bf2015-05-07 11:25:03 -04002677 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002678 // if input >= (float)LONG_MAX goto done
2679 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002680 __ j(kAboveEqual, &done);
2681 // if input == NaN goto nan
2682 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002683 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002684 __ cvttss2si(output, input, true);
2685 __ jmp(&done);
2686 __ Bind(&nan);
2687 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002688 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002689 __ Bind(&done);
2690 break;
2691 }
2692
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002693 case Primitive::kPrimDouble: {
2694 // Processing a Dex `double-to-long' instruction.
2695 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2696 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002697 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002698
Mark Mendell92e83bf2015-05-07 11:25:03 -04002699 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002700 // if input >= (double)LONG_MAX goto done
2701 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002702 __ j(kAboveEqual, &done);
2703 // if input == NaN goto nan
2704 __ j(kUnordered, &nan);
2705 // output = double-to-long-truncate(input)
2706 __ cvttsd2si(output, input, true);
2707 __ jmp(&done);
2708 __ Bind(&nan);
2709 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002710 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002711 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002712 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002713 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002714
2715 default:
2716 LOG(FATAL) << "Unexpected type conversion from " << input_type
2717 << " to " << result_type;
2718 }
2719 break;
2720
Roland Levillain981e4542014-11-14 11:47:14 +00002721 case Primitive::kPrimChar:
2722 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002723 case Primitive::kPrimBoolean:
2724 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002725 case Primitive::kPrimByte:
2726 case Primitive::kPrimShort:
2727 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002728 // Processing a Dex `int-to-char' instruction.
2729 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002730 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002731 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002732 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002733 Address(CpuRegister(RSP), in.GetStackIndex()));
2734 } else {
2735 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002736 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002737 Immediate(static_cast<uint16_t>(
2738 in.GetConstant()->AsIntConstant()->GetValue())));
Roland Levillain981e4542014-11-14 11:47:14 +00002739 }
2740 break;
2741
2742 default:
2743 LOG(FATAL) << "Unexpected type conversion from " << input_type
2744 << " to " << result_type;
2745 }
2746 break;
2747
Roland Levillaindff1f282014-11-05 14:15:05 +00002748 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002749 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002750 case Primitive::kPrimBoolean:
2751 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002752 case Primitive::kPrimByte:
2753 case Primitive::kPrimShort:
2754 case Primitive::kPrimInt:
2755 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002756 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002757 if (in.IsRegister()) {
2758 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2759 } else if (in.IsConstant()) {
2760 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2761 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2762 if (v == 0) {
2763 __ xorps(dest, dest);
2764 } else {
2765 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2766 }
2767 } else {
2768 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2769 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2770 }
Roland Levillaincff13742014-11-17 14:32:17 +00002771 break;
2772
2773 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002774 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002775 if (in.IsRegister()) {
2776 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2777 } else if (in.IsConstant()) {
2778 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2779 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2780 if (v == 0) {
2781 __ xorps(dest, dest);
2782 } else {
2783 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2784 }
2785 } else {
2786 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2787 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2788 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002789 break;
2790
Roland Levillaincff13742014-11-17 14:32:17 +00002791 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002792 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002793 if (in.IsFpuRegister()) {
2794 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2795 } else if (in.IsConstant()) {
2796 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2797 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2798 if (bit_cast<int64_t, double>(v) == 0) {
2799 __ xorps(dest, dest);
2800 } else {
2801 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2802 }
2803 } else {
2804 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2805 Address(CpuRegister(RSP), in.GetStackIndex()));
2806 }
Roland Levillaincff13742014-11-17 14:32:17 +00002807 break;
2808
2809 default:
2810 LOG(FATAL) << "Unexpected type conversion from " << input_type
2811 << " to " << result_type;
2812 };
2813 break;
2814
Roland Levillaindff1f282014-11-05 14:15:05 +00002815 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002816 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002817 case Primitive::kPrimBoolean:
2818 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002819 case Primitive::kPrimByte:
2820 case Primitive::kPrimShort:
2821 case Primitive::kPrimInt:
2822 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002823 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002824 if (in.IsRegister()) {
2825 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2826 } else if (in.IsConstant()) {
2827 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2828 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2829 if (v == 0) {
2830 __ xorpd(dest, dest);
2831 } else {
2832 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2833 }
2834 } else {
2835 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2836 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2837 }
Roland Levillaincff13742014-11-17 14:32:17 +00002838 break;
2839
2840 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002841 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002842 if (in.IsRegister()) {
2843 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2844 } else if (in.IsConstant()) {
2845 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2846 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2847 if (v == 0) {
2848 __ xorpd(dest, dest);
2849 } else {
2850 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2851 }
2852 } else {
2853 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2854 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2855 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002856 break;
2857
Roland Levillaincff13742014-11-17 14:32:17 +00002858 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002859 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002860 if (in.IsFpuRegister()) {
2861 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2862 } else if (in.IsConstant()) {
2863 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2864 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2865 if (bit_cast<int32_t, float>(v) == 0) {
2866 __ xorpd(dest, dest);
2867 } else {
2868 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2869 }
2870 } else {
2871 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2872 Address(CpuRegister(RSP), in.GetStackIndex()));
2873 }
Roland Levillaincff13742014-11-17 14:32:17 +00002874 break;
2875
2876 default:
2877 LOG(FATAL) << "Unexpected type conversion from " << input_type
2878 << " to " << result_type;
2879 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002880 break;
2881
2882 default:
2883 LOG(FATAL) << "Unexpected type conversion from " << input_type
2884 << " to " << result_type;
2885 }
2886}
2887
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002888void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002889 LocationSummary* locations =
2890 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002891 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002892 case Primitive::kPrimInt: {
2893 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002894 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002896 break;
2897 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002898
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002899 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002900 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002901 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002902 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002903 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002904 break;
2905 }
2906
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002907 case Primitive::kPrimDouble:
2908 case Primitive::kPrimFloat: {
2909 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002910 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002911 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002912 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002913 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002914
2915 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002916 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002917 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002918}
2919
2920void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2921 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002922 Location first = locations->InAt(0);
2923 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002924 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002925
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002926 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002927 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002928 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002929 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2930 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002931 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2932 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002933 } else {
2934 __ leal(out.AsRegister<CpuRegister>(), Address(
2935 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2936 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002937 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002938 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2939 __ addl(out.AsRegister<CpuRegister>(),
2940 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2941 } else {
2942 __ leal(out.AsRegister<CpuRegister>(), Address(
2943 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2944 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002945 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002946 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002947 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002948 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002949 break;
2950 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002951
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002952 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002953 if (second.IsRegister()) {
2954 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2955 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002956 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2957 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002958 } else {
2959 __ leaq(out.AsRegister<CpuRegister>(), Address(
2960 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2961 }
2962 } else {
2963 DCHECK(second.IsConstant());
2964 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2965 int32_t int32_value = Low32Bits(value);
2966 DCHECK_EQ(int32_value, value);
2967 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2968 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2969 } else {
2970 __ leaq(out.AsRegister<CpuRegister>(), Address(
2971 first.AsRegister<CpuRegister>(), int32_value));
2972 }
2973 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002974 break;
2975 }
2976
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002977 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002978 if (second.IsFpuRegister()) {
2979 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2980 } else if (second.IsConstant()) {
2981 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002982 codegen_->LiteralFloatAddress(
2983 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002984 } else {
2985 DCHECK(second.IsStackSlot());
2986 __ addss(first.AsFpuRegister<XmmRegister>(),
2987 Address(CpuRegister(RSP), second.GetStackIndex()));
2988 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002989 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002990 }
2991
2992 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002993 if (second.IsFpuRegister()) {
2994 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2995 } else if (second.IsConstant()) {
2996 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002997 codegen_->LiteralDoubleAddress(
2998 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002999 } else {
3000 DCHECK(second.IsDoubleStackSlot());
3001 __ addsd(first.AsFpuRegister<XmmRegister>(),
3002 Address(CpuRegister(RSP), second.GetStackIndex()));
3003 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 break;
3005 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003006
3007 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003008 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003009 }
3010}
3011
3012void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003013 LocationSummary* locations =
3014 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003015 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003016 case Primitive::kPrimInt: {
3017 locations->SetInAt(0, Location::RequiresRegister());
3018 locations->SetInAt(1, Location::Any());
3019 locations->SetOut(Location::SameAsFirstInput());
3020 break;
3021 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003022 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003023 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003024 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003025 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003026 break;
3027 }
Calin Juravle11351682014-10-23 15:38:15 +01003028 case Primitive::kPrimFloat:
3029 case Primitive::kPrimDouble: {
3030 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003031 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003032 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003033 break;
Calin Juravle11351682014-10-23 15:38:15 +01003034 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035 default:
Calin Juravle11351682014-10-23 15:38:15 +01003036 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003037 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003038}
3039
3040void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3041 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003042 Location first = locations->InAt(0);
3043 Location second = locations->InAt(1);
3044 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003045 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003046 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003047 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003048 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003049 } else if (second.IsConstant()) {
3050 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003052 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003053 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003054 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003055 break;
3056 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003057 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003058 if (second.IsConstant()) {
3059 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3060 DCHECK(IsInt<32>(value));
3061 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3062 } else {
3063 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3064 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003065 break;
3066 }
3067
Calin Juravle11351682014-10-23 15:38:15 +01003068 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003069 if (second.IsFpuRegister()) {
3070 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3071 } else if (second.IsConstant()) {
3072 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003073 codegen_->LiteralFloatAddress(
3074 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003075 } else {
3076 DCHECK(second.IsStackSlot());
3077 __ subss(first.AsFpuRegister<XmmRegister>(),
3078 Address(CpuRegister(RSP), second.GetStackIndex()));
3079 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003080 break;
Calin Juravle11351682014-10-23 15:38:15 +01003081 }
3082
3083 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003084 if (second.IsFpuRegister()) {
3085 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3086 } else if (second.IsConstant()) {
3087 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003088 codegen_->LiteralDoubleAddress(
3089 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003090 } else {
3091 DCHECK(second.IsDoubleStackSlot());
3092 __ subsd(first.AsFpuRegister<XmmRegister>(),
3093 Address(CpuRegister(RSP), second.GetStackIndex()));
3094 }
Calin Juravle11351682014-10-23 15:38:15 +01003095 break;
3096 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003097
3098 default:
Calin Juravle11351682014-10-23 15:38:15 +01003099 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003100 }
3101}
3102
Calin Juravle34bacdf2014-10-07 20:23:36 +01003103void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3104 LocationSummary* locations =
3105 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3106 switch (mul->GetResultType()) {
3107 case Primitive::kPrimInt: {
3108 locations->SetInAt(0, Location::RequiresRegister());
3109 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003110 if (mul->InputAt(1)->IsIntConstant()) {
3111 // Can use 3 operand multiply.
3112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3113 } else {
3114 locations->SetOut(Location::SameAsFirstInput());
3115 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003116 break;
3117 }
3118 case Primitive::kPrimLong: {
3119 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003120 locations->SetInAt(1, Location::Any());
3121 if (mul->InputAt(1)->IsLongConstant() &&
3122 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003123 // Can use 3 operand multiply.
3124 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3125 } else {
3126 locations->SetOut(Location::SameAsFirstInput());
3127 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003128 break;
3129 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003130 case Primitive::kPrimFloat:
3131 case Primitive::kPrimDouble: {
3132 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003133 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003134 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003135 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003136 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003137
3138 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003139 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003140 }
3141}
3142
3143void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3144 LocationSummary* locations = mul->GetLocations();
3145 Location first = locations->InAt(0);
3146 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003147 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003148 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003149 case Primitive::kPrimInt:
3150 // The constant may have ended up in a register, so test explicitly to avoid
3151 // problems where the output may not be the same as the first operand.
3152 if (mul->InputAt(1)->IsIntConstant()) {
3153 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3154 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3155 } else if (second.IsRegister()) {
3156 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003157 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003158 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003159 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003161 __ imull(first.AsRegister<CpuRegister>(),
3162 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003163 }
3164 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003166 // The constant may have ended up in a register, so test explicitly to avoid
3167 // problems where the output may not be the same as the first operand.
3168 if (mul->InputAt(1)->IsLongConstant()) {
3169 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3170 if (IsInt<32>(value)) {
3171 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3172 Immediate(static_cast<int32_t>(value)));
3173 } else {
3174 // Have to use the constant area.
3175 DCHECK(first.Equals(out));
3176 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3177 }
3178 } else if (second.IsRegister()) {
3179 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003180 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003181 } else {
3182 DCHECK(second.IsDoubleStackSlot());
3183 DCHECK(first.Equals(out));
3184 __ imulq(first.AsRegister<CpuRegister>(),
3185 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003186 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187 break;
3188 }
3189
Calin Juravleb5bfa962014-10-21 18:02:24 +01003190 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003191 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003192 if (second.IsFpuRegister()) {
3193 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3194 } else if (second.IsConstant()) {
3195 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003196 codegen_->LiteralFloatAddress(
3197 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003198 } else {
3199 DCHECK(second.IsStackSlot());
3200 __ mulss(first.AsFpuRegister<XmmRegister>(),
3201 Address(CpuRegister(RSP), second.GetStackIndex()));
3202 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003203 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003204 }
3205
3206 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003207 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003208 if (second.IsFpuRegister()) {
3209 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3210 } else if (second.IsConstant()) {
3211 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003212 codegen_->LiteralDoubleAddress(
3213 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003214 } else {
3215 DCHECK(second.IsDoubleStackSlot());
3216 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3217 Address(CpuRegister(RSP), second.GetStackIndex()));
3218 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003219 break;
3220 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003221
3222 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003223 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003224 }
3225}
3226
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003227void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3228 uint32_t stack_adjustment, bool is_float) {
3229 if (source.IsStackSlot()) {
3230 DCHECK(is_float);
3231 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3232 } else if (source.IsDoubleStackSlot()) {
3233 DCHECK(!is_float);
3234 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3235 } else {
3236 // Write the value to the temporary location on the stack and load to FP stack.
3237 if (is_float) {
3238 Location stack_temp = Location::StackSlot(temp_offset);
3239 codegen_->Move(stack_temp, source);
3240 __ flds(Address(CpuRegister(RSP), temp_offset));
3241 } else {
3242 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3243 codegen_->Move(stack_temp, source);
3244 __ fldl(Address(CpuRegister(RSP), temp_offset));
3245 }
3246 }
3247}
3248
3249void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3250 Primitive::Type type = rem->GetResultType();
3251 bool is_float = type == Primitive::kPrimFloat;
3252 size_t elem_size = Primitive::ComponentSize(type);
3253 LocationSummary* locations = rem->GetLocations();
3254 Location first = locations->InAt(0);
3255 Location second = locations->InAt(1);
3256 Location out = locations->Out();
3257
3258 // Create stack space for 2 elements.
3259 // TODO: enhance register allocator to ask for stack temporaries.
3260 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3261
3262 // Load the values to the FP stack in reverse order, using temporaries if needed.
3263 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3264 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3265
3266 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003267 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003268 __ Bind(&retry);
3269 __ fprem();
3270
3271 // Move FP status to AX.
3272 __ fstsw();
3273
3274 // And see if the argument reduction is complete. This is signaled by the
3275 // C2 FPU flag bit set to 0.
3276 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3277 __ j(kNotEqual, &retry);
3278
3279 // We have settled on the final value. Retrieve it into an XMM register.
3280 // Store FP top of stack to real stack.
3281 if (is_float) {
3282 __ fsts(Address(CpuRegister(RSP), 0));
3283 } else {
3284 __ fstl(Address(CpuRegister(RSP), 0));
3285 }
3286
3287 // Pop the 2 items from the FP stack.
3288 __ fucompp();
3289
3290 // Load the value from the stack into an XMM register.
3291 DCHECK(out.IsFpuRegister()) << out;
3292 if (is_float) {
3293 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3294 } else {
3295 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3296 }
3297
3298 // And remove the temporary stack space we allocated.
3299 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3300}
3301
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003302void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3303 DCHECK(instruction->IsDiv() || instruction->IsRem());
3304
3305 LocationSummary* locations = instruction->GetLocations();
3306 Location second = locations->InAt(1);
3307 DCHECK(second.IsConstant());
3308
3309 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3310 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003311 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003312
3313 DCHECK(imm == 1 || imm == -1);
3314
3315 switch (instruction->GetResultType()) {
3316 case Primitive::kPrimInt: {
3317 if (instruction->IsRem()) {
3318 __ xorl(output_register, output_register);
3319 } else {
3320 __ movl(output_register, input_register);
3321 if (imm == -1) {
3322 __ negl(output_register);
3323 }
3324 }
3325 break;
3326 }
3327
3328 case Primitive::kPrimLong: {
3329 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003330 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003331 } else {
3332 __ movq(output_register, input_register);
3333 if (imm == -1) {
3334 __ negq(output_register);
3335 }
3336 }
3337 break;
3338 }
3339
3340 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003341 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003342 }
3343}
3344
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003345void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003346 LocationSummary* locations = instruction->GetLocations();
3347 Location second = locations->InAt(1);
3348
3349 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3350 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3351
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003352 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003353
3354 DCHECK(IsPowerOfTwo(std::abs(imm)));
3355
3356 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3357
3358 if (instruction->GetResultType() == Primitive::kPrimInt) {
3359 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
3360 __ testl(numerator, numerator);
3361 __ cmov(kGreaterEqual, tmp, numerator);
3362 int shift = CTZ(imm);
3363 __ sarl(tmp, Immediate(shift));
3364
3365 if (imm < 0) {
3366 __ negl(tmp);
3367 }
3368
3369 __ movl(output_register, tmp);
3370 } else {
3371 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3372 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3373
Mark Mendell92e83bf2015-05-07 11:25:03 -04003374 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003375 __ addq(rdx, numerator);
3376 __ testq(numerator, numerator);
3377 __ cmov(kGreaterEqual, rdx, numerator);
3378 int shift = CTZ(imm);
3379 __ sarq(rdx, Immediate(shift));
3380
3381 if (imm < 0) {
3382 __ negq(rdx);
3383 }
3384
3385 __ movq(output_register, rdx);
3386 }
3387}
3388
3389void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3390 DCHECK(instruction->IsDiv() || instruction->IsRem());
3391
3392 LocationSummary* locations = instruction->GetLocations();
3393 Location second = locations->InAt(1);
3394
3395 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3396 : locations->GetTemp(0).AsRegister<CpuRegister>();
3397 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3398 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3399 : locations->Out().AsRegister<CpuRegister>();
3400 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3401
3402 DCHECK_EQ(RAX, eax.AsRegister());
3403 DCHECK_EQ(RDX, edx.AsRegister());
3404 if (instruction->IsDiv()) {
3405 DCHECK_EQ(RAX, out.AsRegister());
3406 } else {
3407 DCHECK_EQ(RDX, out.AsRegister());
3408 }
3409
3410 int64_t magic;
3411 int shift;
3412
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003413 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003414 if (instruction->GetResultType() == Primitive::kPrimInt) {
3415 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3416
3417 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3418
3419 __ movl(numerator, eax);
3420
Mark Mendell0c9497d2015-08-21 09:30:05 -04003421 NearLabel no_div;
3422 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003423 __ testl(eax, eax);
3424 __ j(kNotEqual, &no_div);
3425
3426 __ xorl(out, out);
3427 __ jmp(&end);
3428
3429 __ Bind(&no_div);
3430
3431 __ movl(eax, Immediate(magic));
3432 __ imull(numerator);
3433
3434 if (imm > 0 && magic < 0) {
3435 __ addl(edx, numerator);
3436 } else if (imm < 0 && magic > 0) {
3437 __ subl(edx, numerator);
3438 }
3439
3440 if (shift != 0) {
3441 __ sarl(edx, Immediate(shift));
3442 }
3443
3444 __ movl(eax, edx);
3445 __ shrl(edx, Immediate(31));
3446 __ addl(edx, eax);
3447
3448 if (instruction->IsRem()) {
3449 __ movl(eax, numerator);
3450 __ imull(edx, Immediate(imm));
3451 __ subl(eax, edx);
3452 __ movl(edx, eax);
3453 } else {
3454 __ movl(eax, edx);
3455 }
3456 __ Bind(&end);
3457 } else {
3458 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3459
3460 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3461
3462 CpuRegister rax = eax;
3463 CpuRegister rdx = edx;
3464
3465 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3466
3467 // Save the numerator.
3468 __ movq(numerator, rax);
3469
3470 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003471 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003472
3473 // RDX:RAX = magic * numerator
3474 __ imulq(numerator);
3475
3476 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003477 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003478 __ addq(rdx, numerator);
3479 } else if (imm < 0 && magic > 0) {
3480 // RDX -= numerator
3481 __ subq(rdx, numerator);
3482 }
3483
3484 // Shift if needed.
3485 if (shift != 0) {
3486 __ sarq(rdx, Immediate(shift));
3487 }
3488
3489 // RDX += 1 if RDX < 0
3490 __ movq(rax, rdx);
3491 __ shrq(rdx, Immediate(63));
3492 __ addq(rdx, rax);
3493
3494 if (instruction->IsRem()) {
3495 __ movq(rax, numerator);
3496
3497 if (IsInt<32>(imm)) {
3498 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3499 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003500 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501 }
3502
3503 __ subq(rax, rdx);
3504 __ movq(rdx, rax);
3505 } else {
3506 __ movq(rax, rdx);
3507 }
3508 }
3509}
3510
Calin Juravlebacfec32014-11-14 15:54:36 +00003511void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3512 DCHECK(instruction->IsDiv() || instruction->IsRem());
3513 Primitive::Type type = instruction->GetResultType();
3514 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3515
3516 bool is_div = instruction->IsDiv();
3517 LocationSummary* locations = instruction->GetLocations();
3518
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003519 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3520 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003521
Roland Levillain271ab9c2014-11-27 15:23:57 +00003522 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003523 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003524
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003526 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003527
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003528 if (imm == 0) {
3529 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3530 } else if (imm == 1 || imm == -1) {
3531 DivRemOneOrMinusOne(instruction);
3532 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003533 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003534 } else {
3535 DCHECK(imm <= -2 || imm >= 2);
3536 GenerateDivRemWithAnyConstant(instruction);
3537 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003538 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003539 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3541 out.AsRegister(), type, is_div);
3542 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003543
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3545 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3546 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3547 // so it's safe to just use negl instead of more complex comparisons.
3548 if (type == Primitive::kPrimInt) {
3549 __ cmpl(second_reg, Immediate(-1));
3550 __ j(kEqual, slow_path->GetEntryLabel());
3551 // edx:eax <- sign-extended of eax
3552 __ cdq();
3553 // eax = quotient, edx = remainder
3554 __ idivl(second_reg);
3555 } else {
3556 __ cmpq(second_reg, Immediate(-1));
3557 __ j(kEqual, slow_path->GetEntryLabel());
3558 // rdx:rax <- sign-extended of rax
3559 __ cqo();
3560 // rax = quotient, rdx = remainder
3561 __ idivq(second_reg);
3562 }
3563 __ Bind(slow_path->GetExitLabel());
3564 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003565}
3566
Calin Juravle7c4954d2014-10-28 16:57:40 +00003567void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3568 LocationSummary* locations =
3569 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3570 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003571 case Primitive::kPrimInt:
3572 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003573 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003574 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003575 locations->SetOut(Location::SameAsFirstInput());
3576 // Intel uses edx:eax as the dividend.
3577 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003578 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3579 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3580 // output and request another temp.
3581 if (div->InputAt(1)->IsConstant()) {
3582 locations->AddTemp(Location::RequiresRegister());
3583 }
Calin Juravled0d48522014-11-04 16:40:20 +00003584 break;
3585 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003586
Calin Juravle7c4954d2014-10-28 16:57:40 +00003587 case Primitive::kPrimFloat:
3588 case Primitive::kPrimDouble: {
3589 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003590 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003591 locations->SetOut(Location::SameAsFirstInput());
3592 break;
3593 }
3594
3595 default:
3596 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3597 }
3598}
3599
3600void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3601 LocationSummary* locations = div->GetLocations();
3602 Location first = locations->InAt(0);
3603 Location second = locations->InAt(1);
3604 DCHECK(first.Equals(locations->Out()));
3605
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003606 Primitive::Type type = div->GetResultType();
3607 switch (type) {
3608 case Primitive::kPrimInt:
3609 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003610 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003611 break;
3612 }
3613
Calin Juravle7c4954d2014-10-28 16:57:40 +00003614 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003615 if (second.IsFpuRegister()) {
3616 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3617 } else if (second.IsConstant()) {
3618 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003619 codegen_->LiteralFloatAddress(
3620 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003621 } else {
3622 DCHECK(second.IsStackSlot());
3623 __ divss(first.AsFpuRegister<XmmRegister>(),
3624 Address(CpuRegister(RSP), second.GetStackIndex()));
3625 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003626 break;
3627 }
3628
3629 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003630 if (second.IsFpuRegister()) {
3631 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3632 } else if (second.IsConstant()) {
3633 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003634 codegen_->LiteralDoubleAddress(
3635 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003636 } else {
3637 DCHECK(second.IsDoubleStackSlot());
3638 __ divsd(first.AsFpuRegister<XmmRegister>(),
3639 Address(CpuRegister(RSP), second.GetStackIndex()));
3640 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003641 break;
3642 }
3643
3644 default:
3645 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3646 }
3647}
3648
Calin Juravlebacfec32014-11-14 15:54:36 +00003649void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003650 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003651 LocationSummary* locations =
3652 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003653
3654 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003655 case Primitive::kPrimInt:
3656 case Primitive::kPrimLong: {
3657 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003658 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003659 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3660 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003661 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3662 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3663 // output and request another temp.
3664 if (rem->InputAt(1)->IsConstant()) {
3665 locations->AddTemp(Location::RequiresRegister());
3666 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003667 break;
3668 }
3669
3670 case Primitive::kPrimFloat:
3671 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003672 locations->SetInAt(0, Location::Any());
3673 locations->SetInAt(1, Location::Any());
3674 locations->SetOut(Location::RequiresFpuRegister());
3675 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003676 break;
3677 }
3678
3679 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003680 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003681 }
3682}
3683
3684void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3685 Primitive::Type type = rem->GetResultType();
3686 switch (type) {
3687 case Primitive::kPrimInt:
3688 case Primitive::kPrimLong: {
3689 GenerateDivRemIntegral(rem);
3690 break;
3691 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003692 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003693 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003694 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003695 break;
3696 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003697 default:
3698 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3699 }
3700}
3701
Calin Juravled0d48522014-11-04 16:40:20 +00003702void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003703 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3704 ? LocationSummary::kCallOnSlowPath
3705 : LocationSummary::kNoCall;
3706 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003707 locations->SetInAt(0, Location::Any());
3708 if (instruction->HasUses()) {
3709 locations->SetOut(Location::SameAsFirstInput());
3710 }
3711}
3712
3713void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003714 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003715 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3716 codegen_->AddSlowPath(slow_path);
3717
3718 LocationSummary* locations = instruction->GetLocations();
3719 Location value = locations->InAt(0);
3720
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003721 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003722 case Primitive::kPrimByte:
3723 case Primitive::kPrimChar:
3724 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003725 case Primitive::kPrimInt: {
3726 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003727 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003728 __ j(kEqual, slow_path->GetEntryLabel());
3729 } else if (value.IsStackSlot()) {
3730 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3731 __ j(kEqual, slow_path->GetEntryLabel());
3732 } else {
3733 DCHECK(value.IsConstant()) << value;
3734 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3735 __ jmp(slow_path->GetEntryLabel());
3736 }
3737 }
3738 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003739 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003740 case Primitive::kPrimLong: {
3741 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003742 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003743 __ j(kEqual, slow_path->GetEntryLabel());
3744 } else if (value.IsDoubleStackSlot()) {
3745 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3746 __ j(kEqual, slow_path->GetEntryLabel());
3747 } else {
3748 DCHECK(value.IsConstant()) << value;
3749 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3750 __ jmp(slow_path->GetEntryLabel());
3751 }
3752 }
3753 break;
3754 }
3755 default:
3756 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003757 }
Calin Juravled0d48522014-11-04 16:40:20 +00003758}
3759
Calin Juravle9aec02f2014-11-18 23:06:35 +00003760void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3761 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3762
3763 LocationSummary* locations =
3764 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3765
3766 switch (op->GetResultType()) {
3767 case Primitive::kPrimInt:
3768 case Primitive::kPrimLong: {
3769 locations->SetInAt(0, Location::RequiresRegister());
3770 // The shift count needs to be in CL.
3771 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3772 locations->SetOut(Location::SameAsFirstInput());
3773 break;
3774 }
3775 default:
3776 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3777 }
3778}
3779
3780void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3781 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3782
3783 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003784 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003785 Location second = locations->InAt(1);
3786
3787 switch (op->GetResultType()) {
3788 case Primitive::kPrimInt: {
3789 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003790 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003791 if (op->IsShl()) {
3792 __ shll(first_reg, second_reg);
3793 } else if (op->IsShr()) {
3794 __ sarl(first_reg, second_reg);
3795 } else {
3796 __ shrl(first_reg, second_reg);
3797 }
3798 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003799 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003800 if (op->IsShl()) {
3801 __ shll(first_reg, imm);
3802 } else if (op->IsShr()) {
3803 __ sarl(first_reg, imm);
3804 } else {
3805 __ shrl(first_reg, imm);
3806 }
3807 }
3808 break;
3809 }
3810 case Primitive::kPrimLong: {
3811 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003812 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003813 if (op->IsShl()) {
3814 __ shlq(first_reg, second_reg);
3815 } else if (op->IsShr()) {
3816 __ sarq(first_reg, second_reg);
3817 } else {
3818 __ shrq(first_reg, second_reg);
3819 }
3820 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003821 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003822 if (op->IsShl()) {
3823 __ shlq(first_reg, imm);
3824 } else if (op->IsShr()) {
3825 __ sarq(first_reg, imm);
3826 } else {
3827 __ shrq(first_reg, imm);
3828 }
3829 }
3830 break;
3831 }
3832 default:
3833 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003834 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003835 }
3836}
3837
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003838void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3839 LocationSummary* locations =
3840 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3841
3842 switch (ror->GetResultType()) {
3843 case Primitive::kPrimInt:
3844 case Primitive::kPrimLong: {
3845 locations->SetInAt(0, Location::RequiresRegister());
3846 // The shift count needs to be in CL (unless it is a constant).
3847 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3848 locations->SetOut(Location::SameAsFirstInput());
3849 break;
3850 }
3851 default:
3852 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3853 UNREACHABLE();
3854 }
3855}
3856
3857void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3858 LocationSummary* locations = ror->GetLocations();
3859 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3860 Location second = locations->InAt(1);
3861
3862 switch (ror->GetResultType()) {
3863 case Primitive::kPrimInt:
3864 if (second.IsRegister()) {
3865 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3866 __ rorl(first_reg, second_reg);
3867 } else {
3868 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3869 __ rorl(first_reg, imm);
3870 }
3871 break;
3872 case Primitive::kPrimLong:
3873 if (second.IsRegister()) {
3874 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3875 __ rorq(first_reg, second_reg);
3876 } else {
3877 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
3878 __ rorq(first_reg, imm);
3879 }
3880 break;
3881 default:
3882 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3883 UNREACHABLE();
3884 }
3885}
3886
Calin Juravle9aec02f2014-11-18 23:06:35 +00003887void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3888 HandleShift(shl);
3889}
3890
3891void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3892 HandleShift(shl);
3893}
3894
3895void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3896 HandleShift(shr);
3897}
3898
3899void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3900 HandleShift(shr);
3901}
3902
3903void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3904 HandleShift(ushr);
3905}
3906
3907void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3908 HandleShift(ushr);
3909}
3910
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003911void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003912 LocationSummary* locations =
3913 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003914 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003915 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3916 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003917 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003918}
3919
3920void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003921 // Note: if heap poisoning is enabled, the entry point takes cares
3922 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003923 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3924 instruction,
3925 instruction->GetDexPc(),
3926 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003927 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003928
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003929 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003930}
3931
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003932void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3933 LocationSummary* locations =
3934 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3935 InvokeRuntimeCallingConvention calling_convention;
3936 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003937 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003938 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003939 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003940}
3941
3942void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3943 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003944 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3945 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003946 // Note: if heap poisoning is enabled, the entry point takes cares
3947 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003948 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3949 instruction,
3950 instruction->GetDexPc(),
3951 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003952 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003953
3954 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003955}
3956
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003957void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003958 LocationSummary* locations =
3959 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003960 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3961 if (location.IsStackSlot()) {
3962 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3963 } else if (location.IsDoubleStackSlot()) {
3964 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3965 }
3966 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003967}
3968
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003969void InstructionCodeGeneratorX86_64::VisitParameterValue(
3970 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003971 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003972}
3973
3974void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3975 LocationSummary* locations =
3976 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3977 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3978}
3979
3980void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3981 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3982 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003983}
3984
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003985void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003986 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003987 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003988 locations->SetInAt(0, Location::RequiresRegister());
3989 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003990}
3991
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003992void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3993 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003994 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3995 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003996 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003997 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003998 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003999 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004000 break;
4001
4002 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004003 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004004 break;
4005
4006 default:
4007 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4008 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004009}
4010
David Brazdil66d126e2015-04-03 16:02:44 +01004011void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4012 LocationSummary* locations =
4013 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4014 locations->SetInAt(0, Location::RequiresRegister());
4015 locations->SetOut(Location::SameAsFirstInput());
4016}
4017
4018void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004019 LocationSummary* locations = bool_not->GetLocations();
4020 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4021 locations->Out().AsRegister<CpuRegister>().AsRegister());
4022 Location out = locations->Out();
4023 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4024}
4025
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004026void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004027 LocationSummary* locations =
4028 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004029 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4030 locations->SetInAt(i, Location::Any());
4031 }
4032 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004033}
4034
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004035void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004036 LOG(FATAL) << "Unimplemented";
4037}
4038
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004039void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004040 /*
4041 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004042 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004043 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4044 */
4045 switch (kind) {
4046 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004047 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004048 break;
4049 }
4050 case MemBarrierKind::kAnyStore:
4051 case MemBarrierKind::kLoadAny:
4052 case MemBarrierKind::kStoreStore: {
4053 // nop
4054 break;
4055 }
4056 default:
4057 LOG(FATAL) << "Unexpected memory barier " << kind;
4058 }
4059}
4060
4061void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4062 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4063
Roland Levillain0d5a2812015-11-13 10:07:31 +00004064 bool object_field_get_with_read_barrier =
4065 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004066 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004067 new (GetGraph()->GetArena()) LocationSummary(instruction,
4068 object_field_get_with_read_barrier ?
4069 LocationSummary::kCallOnSlowPath :
4070 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004071 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004072 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4073 locations->SetOut(Location::RequiresFpuRegister());
4074 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004075 // The output overlaps for an object field get when read barriers
4076 // are enabled: we do not want the move to overwrite the object's
4077 // location, as we need it to emit the read barrier.
4078 locations->SetOut(
4079 Location::RequiresRegister(),
4080 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004081 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004082 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4083 // We need a temporary register for the read barrier marking slow
4084 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4085 locations->AddTemp(Location::RequiresRegister());
4086 }
Calin Juravle52c48962014-12-16 17:02:57 +00004087}
4088
4089void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4090 const FieldInfo& field_info) {
4091 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4092
4093 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004094 Location base_loc = locations->InAt(0);
4095 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004096 Location out = locations->Out();
4097 bool is_volatile = field_info.IsVolatile();
4098 Primitive::Type field_type = field_info.GetFieldType();
4099 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4100
4101 switch (field_type) {
4102 case Primitive::kPrimBoolean: {
4103 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4104 break;
4105 }
4106
4107 case Primitive::kPrimByte: {
4108 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4109 break;
4110 }
4111
4112 case Primitive::kPrimShort: {
4113 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4114 break;
4115 }
4116
4117 case Primitive::kPrimChar: {
4118 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4119 break;
4120 }
4121
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004122 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004123 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4124 break;
4125 }
4126
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004127 case Primitive::kPrimNot: {
4128 // /* HeapReference<Object> */ out = *(base + offset)
4129 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4130 Location temp_loc = locations->GetTemp(0);
4131 // Note that a potential implicit null check is handled in this
4132 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4133 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4134 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4135 if (is_volatile) {
4136 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4137 }
4138 } else {
4139 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4140 codegen_->MaybeRecordImplicitNullCheck(instruction);
4141 if (is_volatile) {
4142 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4143 }
4144 // If read barriers are enabled, emit read barriers other than
4145 // Baker's using a slow path (and also unpoison the loaded
4146 // reference, if heap poisoning is enabled).
4147 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4148 }
4149 break;
4150 }
4151
Calin Juravle52c48962014-12-16 17:02:57 +00004152 case Primitive::kPrimLong: {
4153 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4154 break;
4155 }
4156
4157 case Primitive::kPrimFloat: {
4158 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4159 break;
4160 }
4161
4162 case Primitive::kPrimDouble: {
4163 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4164 break;
4165 }
4166
4167 case Primitive::kPrimVoid:
4168 LOG(FATAL) << "Unreachable type " << field_type;
4169 UNREACHABLE();
4170 }
4171
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004172 if (field_type == Primitive::kPrimNot) {
4173 // Potential implicit null checks, in the case of reference
4174 // fields, are handled in the previous switch statement.
4175 } else {
4176 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004177 }
Roland Levillain4d027112015-07-01 15:41:14 +01004178
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004179 if (is_volatile) {
4180 if (field_type == Primitive::kPrimNot) {
4181 // Memory barriers, in the case of references, are also handled
4182 // in the previous switch statement.
4183 } else {
4184 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4185 }
Roland Levillain4d027112015-07-01 15:41:14 +01004186 }
Calin Juravle52c48962014-12-16 17:02:57 +00004187}
4188
4189void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4190 const FieldInfo& field_info) {
4191 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4192
4193 LocationSummary* locations =
4194 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004195 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004196 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004197 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004198 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004199
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004200 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004201 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004202 if (is_volatile) {
4203 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4204 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4205 } else {
4206 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4207 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004208 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004209 if (is_volatile) {
4210 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4211 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4212 } else {
4213 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4214 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004215 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004216 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004217 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004218 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004219 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004220 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4221 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004222 locations->AddTemp(Location::RequiresRegister());
4223 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004224}
4225
Calin Juravle52c48962014-12-16 17:02:57 +00004226void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004227 const FieldInfo& field_info,
4228 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004229 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4230
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004231 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004232 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4233 Location value = locations->InAt(1);
4234 bool is_volatile = field_info.IsVolatile();
4235 Primitive::Type field_type = field_info.GetFieldType();
4236 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4237
4238 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004239 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004240 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004241
Mark Mendellea5af682015-10-22 17:35:49 -04004242 bool maybe_record_implicit_null_check_done = false;
4243
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004244 switch (field_type) {
4245 case Primitive::kPrimBoolean:
4246 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004247 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004248 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004249 __ movb(Address(base, offset), Immediate(v));
4250 } else {
4251 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4252 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253 break;
4254 }
4255
4256 case Primitive::kPrimShort:
4257 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004258 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004259 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004260 __ movw(Address(base, offset), Immediate(v));
4261 } else {
4262 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4263 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004264 break;
4265 }
4266
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004267 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004268 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004269 if (value.IsConstant()) {
4270 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004271 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4272 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4273 // Note: if heap poisoning is enabled, no need to poison
4274 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004275 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004276 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004277 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4278 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4279 __ movl(temp, value.AsRegister<CpuRegister>());
4280 __ PoisonHeapReference(temp);
4281 __ movl(Address(base, offset), temp);
4282 } else {
4283 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4284 }
Mark Mendell40741f32015-04-20 22:10:34 -04004285 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004286 break;
4287 }
4288
4289 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004290 if (value.IsConstant()) {
4291 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004292 codegen_->MoveInt64ToAddress(Address(base, offset),
4293 Address(base, offset + sizeof(int32_t)),
4294 v,
4295 instruction);
4296 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004297 } else {
4298 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4299 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300 break;
4301 }
4302
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004303 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004304 if (value.IsConstant()) {
4305 int32_t v =
4306 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4307 __ movl(Address(base, offset), Immediate(v));
4308 } else {
4309 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4310 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004311 break;
4312 }
4313
4314 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004315 if (value.IsConstant()) {
4316 int64_t v =
4317 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4318 codegen_->MoveInt64ToAddress(Address(base, offset),
4319 Address(base, offset + sizeof(int32_t)),
4320 v,
4321 instruction);
4322 maybe_record_implicit_null_check_done = true;
4323 } else {
4324 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4325 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004326 break;
4327 }
4328
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004329 case Primitive::kPrimVoid:
4330 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004331 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004332 }
Calin Juravle52c48962014-12-16 17:02:57 +00004333
Mark Mendellea5af682015-10-22 17:35:49 -04004334 if (!maybe_record_implicit_null_check_done) {
4335 codegen_->MaybeRecordImplicitNullCheck(instruction);
4336 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004337
4338 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4339 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4340 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004341 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004342 }
4343
Calin Juravle52c48962014-12-16 17:02:57 +00004344 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004345 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004346 }
4347}
4348
4349void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4350 HandleFieldSet(instruction, instruction->GetFieldInfo());
4351}
4352
4353void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004354 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004355}
4356
4357void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004358 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004359}
4360
4361void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004362 HandleFieldGet(instruction, instruction->GetFieldInfo());
4363}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004364
Calin Juravle52c48962014-12-16 17:02:57 +00004365void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4366 HandleFieldGet(instruction);
4367}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004368
Calin Juravle52c48962014-12-16 17:02:57 +00004369void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4370 HandleFieldGet(instruction, instruction->GetFieldInfo());
4371}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372
Calin Juravle52c48962014-12-16 17:02:57 +00004373void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4374 HandleFieldSet(instruction, instruction->GetFieldInfo());
4375}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004376
Calin Juravle52c48962014-12-16 17:02:57 +00004377void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004378 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004379}
4380
Calin Juravlee460d1d2015-09-29 04:52:17 +01004381void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4382 HUnresolvedInstanceFieldGet* instruction) {
4383 FieldAccessCallingConventionX86_64 calling_convention;
4384 codegen_->CreateUnresolvedFieldLocationSummary(
4385 instruction, instruction->GetFieldType(), calling_convention);
4386}
4387
4388void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4389 HUnresolvedInstanceFieldGet* instruction) {
4390 FieldAccessCallingConventionX86_64 calling_convention;
4391 codegen_->GenerateUnresolvedFieldAccess(instruction,
4392 instruction->GetFieldType(),
4393 instruction->GetFieldIndex(),
4394 instruction->GetDexPc(),
4395 calling_convention);
4396}
4397
4398void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4399 HUnresolvedInstanceFieldSet* instruction) {
4400 FieldAccessCallingConventionX86_64 calling_convention;
4401 codegen_->CreateUnresolvedFieldLocationSummary(
4402 instruction, instruction->GetFieldType(), calling_convention);
4403}
4404
4405void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4406 HUnresolvedInstanceFieldSet* instruction) {
4407 FieldAccessCallingConventionX86_64 calling_convention;
4408 codegen_->GenerateUnresolvedFieldAccess(instruction,
4409 instruction->GetFieldType(),
4410 instruction->GetFieldIndex(),
4411 instruction->GetDexPc(),
4412 calling_convention);
4413}
4414
4415void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4416 HUnresolvedStaticFieldGet* instruction) {
4417 FieldAccessCallingConventionX86_64 calling_convention;
4418 codegen_->CreateUnresolvedFieldLocationSummary(
4419 instruction, instruction->GetFieldType(), calling_convention);
4420}
4421
4422void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4423 HUnresolvedStaticFieldGet* instruction) {
4424 FieldAccessCallingConventionX86_64 calling_convention;
4425 codegen_->GenerateUnresolvedFieldAccess(instruction,
4426 instruction->GetFieldType(),
4427 instruction->GetFieldIndex(),
4428 instruction->GetDexPc(),
4429 calling_convention);
4430}
4431
4432void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4433 HUnresolvedStaticFieldSet* instruction) {
4434 FieldAccessCallingConventionX86_64 calling_convention;
4435 codegen_->CreateUnresolvedFieldLocationSummary(
4436 instruction, instruction->GetFieldType(), calling_convention);
4437}
4438
4439void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4440 HUnresolvedStaticFieldSet* instruction) {
4441 FieldAccessCallingConventionX86_64 calling_convention;
4442 codegen_->GenerateUnresolvedFieldAccess(instruction,
4443 instruction->GetFieldType(),
4444 instruction->GetFieldIndex(),
4445 instruction->GetDexPc(),
4446 calling_convention);
4447}
4448
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004449void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004450 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4451 ? LocationSummary::kCallOnSlowPath
4452 : LocationSummary::kNoCall;
4453 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4454 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004455 ? Location::RequiresRegister()
4456 : Location::Any();
4457 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004458 if (instruction->HasUses()) {
4459 locations->SetOut(Location::SameAsFirstInput());
4460 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004461}
4462
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004463void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004464 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4465 return;
4466 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004467 LocationSummary* locations = instruction->GetLocations();
4468 Location obj = locations->InAt(0);
4469
4470 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4471 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4472}
4473
4474void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004475 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476 codegen_->AddSlowPath(slow_path);
4477
4478 LocationSummary* locations = instruction->GetLocations();
4479 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004480
4481 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004482 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004483 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004485 } else {
4486 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004487 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004488 __ jmp(slow_path->GetEntryLabel());
4489 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004490 }
4491 __ j(kEqual, slow_path->GetEntryLabel());
4492}
4493
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004494void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004495 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004496 GenerateImplicitNullCheck(instruction);
4497 } else {
4498 GenerateExplicitNullCheck(instruction);
4499 }
4500}
4501
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004502void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004503 bool object_array_get_with_read_barrier =
4504 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004505 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004506 new (GetGraph()->GetArena()) LocationSummary(instruction,
4507 object_array_get_with_read_barrier ?
4508 LocationSummary::kCallOnSlowPath :
4509 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004510 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004511 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004512 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4513 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4514 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004515 // The output overlaps for an object array get when read barriers
4516 // are enabled: we do not want the move to overwrite the array's
4517 // location, as we need it to emit the read barrier.
4518 locations->SetOut(
4519 Location::RequiresRegister(),
4520 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004521 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004522 // We need a temporary register for the read barrier marking slow
4523 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4524 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4525 locations->AddTemp(Location::RequiresRegister());
4526 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004527}
4528
4529void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4530 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004531 Location obj_loc = locations->InAt(0);
4532 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004533 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004534 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004536 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004537 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538 case Primitive::kPrimBoolean: {
4539 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004540 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004541 if (index.IsConstant()) {
4542 __ movzxb(out, Address(obj,
4543 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4544 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004545 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004546 }
4547 break;
4548 }
4549
4550 case Primitive::kPrimByte: {
4551 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004552 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004553 if (index.IsConstant()) {
4554 __ movsxb(out, Address(obj,
4555 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4556 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004557 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004558 }
4559 break;
4560 }
4561
4562 case Primitive::kPrimShort: {
4563 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004564 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004565 if (index.IsConstant()) {
4566 __ movsxw(out, Address(obj,
4567 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4568 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004569 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004570 }
4571 break;
4572 }
4573
4574 case Primitive::kPrimChar: {
4575 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004576 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577 if (index.IsConstant()) {
4578 __ movzxw(out, Address(obj,
4579 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4580 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004581 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004582 }
4583 break;
4584 }
4585
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004586 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004587 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004588 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 if (index.IsConstant()) {
4590 __ movl(out, Address(obj,
4591 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4592 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004593 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594 }
4595 break;
4596 }
4597
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004598 case Primitive::kPrimNot: {
4599 static_assert(
4600 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4601 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4602 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4603 // /* HeapReference<Object> */ out =
4604 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4605 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4606 Location temp = locations->GetTemp(0);
4607 // Note that a potential implicit null check is handled in this
4608 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4609 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4610 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4611 } else {
4612 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4613 if (index.IsConstant()) {
4614 uint32_t offset =
4615 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4616 __ movl(out, Address(obj, offset));
4617 codegen_->MaybeRecordImplicitNullCheck(instruction);
4618 // If read barriers are enabled, emit read barriers other than
4619 // Baker's using a slow path (and also unpoison the loaded
4620 // reference, if heap poisoning is enabled).
4621 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4622 } else {
4623 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4624 codegen_->MaybeRecordImplicitNullCheck(instruction);
4625 // If read barriers are enabled, emit read barriers other than
4626 // Baker's using a slow path (and also unpoison the loaded
4627 // reference, if heap poisoning is enabled).
4628 codegen_->MaybeGenerateReadBarrierSlow(
4629 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4630 }
4631 }
4632 break;
4633 }
4634
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004635 case Primitive::kPrimLong: {
4636 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004637 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004638 if (index.IsConstant()) {
4639 __ movq(out, Address(obj,
4640 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4641 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004642 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004643 }
4644 break;
4645 }
4646
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004647 case Primitive::kPrimFloat: {
4648 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004649 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004650 if (index.IsConstant()) {
4651 __ movss(out, Address(obj,
4652 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4653 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004654 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004655 }
4656 break;
4657 }
4658
4659 case Primitive::kPrimDouble: {
4660 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004661 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004662 if (index.IsConstant()) {
4663 __ movsd(out, Address(obj,
4664 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4665 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004666 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004667 }
4668 break;
4669 }
4670
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004671 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004672 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004673 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004674 }
Roland Levillain4d027112015-07-01 15:41:14 +01004675
4676 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004677 // Potential implicit null checks, in the case of reference
4678 // arrays, are handled in the previous switch statement.
4679 } else {
4680 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004681 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004682}
4683
4684void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004685 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004686
4687 bool needs_write_barrier =
4688 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004689 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004690 bool object_array_set_with_read_barrier =
4691 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004692
Nicolas Geoffray39468442014-09-02 15:17:15 +01004693 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004694 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004695 (may_need_runtime_call || object_array_set_with_read_barrier) ?
4696 LocationSummary::kCallOnSlowPath :
4697 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004698
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004699 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004700 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4701 if (Primitive::IsFloatingPointType(value_type)) {
4702 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004703 } else {
4704 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4705 }
4706
4707 if (needs_write_barrier) {
4708 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004709
4710 // This first temporary register is possibly used for heap
4711 // reference poisoning and/or read barrier emission too.
4712 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004713 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004714 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004715}
4716
4717void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4718 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004719 Location array_loc = locations->InAt(0);
4720 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004721 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004722 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004723 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004724 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004725 bool needs_write_barrier =
4726 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004727 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4728 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4729 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004730
4731 switch (value_type) {
4732 case Primitive::kPrimBoolean:
4733 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004734 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4735 Address address = index.IsConstant()
4736 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4737 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4738 if (value.IsRegister()) {
4739 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004740 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004741 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004742 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004743 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004744 break;
4745 }
4746
4747 case Primitive::kPrimShort:
4748 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004749 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4750 Address address = index.IsConstant()
4751 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4752 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4753 if (value.IsRegister()) {
4754 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004755 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004756 DCHECK(value.IsConstant()) << value;
4757 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004758 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004759 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004760 break;
4761 }
4762
4763 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004764 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4765 Address address = index.IsConstant()
4766 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4767 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004768
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004769 if (!value.IsRegister()) {
4770 // Just setting null.
4771 DCHECK(instruction->InputAt(2)->IsNullConstant());
4772 DCHECK(value.IsConstant()) << value;
4773 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004774 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004775 DCHECK(!needs_write_barrier);
4776 DCHECK(!may_need_runtime_call);
4777 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004778 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004779
4780 DCHECK(needs_write_barrier);
4781 CpuRegister register_value = value.AsRegister<CpuRegister>();
4782 NearLabel done, not_null, do_put;
4783 SlowPathCode* slow_path = nullptr;
4784 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4785 if (may_need_runtime_call) {
4786 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4787 codegen_->AddSlowPath(slow_path);
4788 if (instruction->GetValueCanBeNull()) {
4789 __ testl(register_value, register_value);
4790 __ j(kNotEqual, &not_null);
4791 __ movl(address, Immediate(0));
4792 codegen_->MaybeRecordImplicitNullCheck(instruction);
4793 __ jmp(&done);
4794 __ Bind(&not_null);
4795 }
4796
Roland Levillain0d5a2812015-11-13 10:07:31 +00004797 if (kEmitCompilerReadBarrier) {
4798 // When read barriers are enabled, the type checking
4799 // instrumentation requires two read barriers:
4800 //
4801 // __ movl(temp2, temp);
4802 // // /* HeapReference<Class> */ temp = temp->component_type_
4803 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004804 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004805 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4806 //
4807 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4808 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004809 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004810 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4811 //
4812 // __ cmpl(temp, temp2);
4813 //
4814 // However, the second read barrier may trash `temp`, as it
4815 // is a temporary register, and as such would not be saved
4816 // along with live registers before calling the runtime (nor
4817 // restored afterwards). So in this case, we bail out and
4818 // delegate the work to the array set slow path.
4819 //
4820 // TODO: Extend the register allocator to support a new
4821 // "(locally) live temp" location so as to avoid always
4822 // going into the slow path when read barriers are enabled.
4823 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004824 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004825 // /* HeapReference<Class> */ temp = array->klass_
4826 __ movl(temp, Address(array, class_offset));
4827 codegen_->MaybeRecordImplicitNullCheck(instruction);
4828 __ MaybeUnpoisonHeapReference(temp);
4829
4830 // /* HeapReference<Class> */ temp = temp->component_type_
4831 __ movl(temp, Address(temp, component_offset));
4832 // If heap poisoning is enabled, no need to unpoison `temp`
4833 // nor the object reference in `register_value->klass`, as
4834 // we are comparing two poisoned references.
4835 __ cmpl(temp, Address(register_value, class_offset));
4836
4837 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4838 __ j(kEqual, &do_put);
4839 // If heap poisoning is enabled, the `temp` reference has
4840 // not been unpoisoned yet; unpoison it now.
4841 __ MaybeUnpoisonHeapReference(temp);
4842
4843 // /* HeapReference<Class> */ temp = temp->super_class_
4844 __ movl(temp, Address(temp, super_offset));
4845 // If heap poisoning is enabled, no need to unpoison
4846 // `temp`, as we are comparing against null below.
4847 __ testl(temp, temp);
4848 __ j(kNotEqual, slow_path->GetEntryLabel());
4849 __ Bind(&do_put);
4850 } else {
4851 __ j(kNotEqual, slow_path->GetEntryLabel());
4852 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004853 }
4854 }
4855
4856 if (kPoisonHeapReferences) {
4857 __ movl(temp, register_value);
4858 __ PoisonHeapReference(temp);
4859 __ movl(address, temp);
4860 } else {
4861 __ movl(address, register_value);
4862 }
4863 if (!may_need_runtime_call) {
4864 codegen_->MaybeRecordImplicitNullCheck(instruction);
4865 }
4866
4867 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4868 codegen_->MarkGCCard(
4869 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4870 __ Bind(&done);
4871
4872 if (slow_path != nullptr) {
4873 __ Bind(slow_path->GetExitLabel());
4874 }
4875
4876 break;
4877 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004878
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004879 case Primitive::kPrimInt: {
4880 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4881 Address address = index.IsConstant()
4882 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4883 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4884 if (value.IsRegister()) {
4885 __ movl(address, value.AsRegister<CpuRegister>());
4886 } else {
4887 DCHECK(value.IsConstant()) << value;
4888 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4889 __ movl(address, Immediate(v));
4890 }
4891 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004892 break;
4893 }
4894
4895 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004896 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4897 Address address = index.IsConstant()
4898 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4899 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4900 if (value.IsRegister()) {
4901 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004902 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004903 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004904 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004905 Address address_high = index.IsConstant()
4906 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4907 offset + sizeof(int32_t))
4908 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4909 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004910 }
4911 break;
4912 }
4913
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004914 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004915 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4916 Address address = index.IsConstant()
4917 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4918 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004919 if (value.IsFpuRegister()) {
4920 __ movss(address, value.AsFpuRegister<XmmRegister>());
4921 } else {
4922 DCHECK(value.IsConstant());
4923 int32_t v =
4924 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4925 __ movl(address, Immediate(v));
4926 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004927 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004928 break;
4929 }
4930
4931 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004932 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4933 Address address = index.IsConstant()
4934 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4935 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004936 if (value.IsFpuRegister()) {
4937 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4938 codegen_->MaybeRecordImplicitNullCheck(instruction);
4939 } else {
4940 int64_t v =
4941 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4942 Address address_high = index.IsConstant()
4943 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4944 offset + sizeof(int32_t))
4945 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4946 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4947 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004948 break;
4949 }
4950
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004951 case Primitive::kPrimVoid:
4952 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004953 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004954 }
4955}
4956
4957void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004958 LocationSummary* locations =
4959 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004960 locations->SetInAt(0, Location::RequiresRegister());
4961 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004962}
4963
4964void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4965 LocationSummary* locations = instruction->GetLocations();
4966 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004967 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4968 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004969 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004970 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004971}
4972
4973void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004974 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4975 ? LocationSummary::kCallOnSlowPath
4976 : LocationSummary::kNoCall;
4977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004978 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004979 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004980 if (instruction->HasUses()) {
4981 locations->SetOut(Location::SameAsFirstInput());
4982 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004983}
4984
4985void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4986 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004987 Location index_loc = locations->InAt(0);
4988 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004989 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004990 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004991
Mark Mendell99dbd682015-04-22 16:18:52 -04004992 if (length_loc.IsConstant()) {
4993 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4994 if (index_loc.IsConstant()) {
4995 // BCE will remove the bounds check if we are guarenteed to pass.
4996 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4997 if (index < 0 || index >= length) {
4998 codegen_->AddSlowPath(slow_path);
4999 __ jmp(slow_path->GetEntryLabel());
5000 } else {
5001 // Some optimization after BCE may have generated this, and we should not
5002 // generate a bounds check if it is a valid range.
5003 }
5004 return;
5005 }
5006
5007 // We have to reverse the jump condition because the length is the constant.
5008 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5009 __ cmpl(index_reg, Immediate(length));
5010 codegen_->AddSlowPath(slow_path);
5011 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005012 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005013 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5014 if (index_loc.IsConstant()) {
5015 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5016 __ cmpl(length, Immediate(value));
5017 } else {
5018 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5019 }
5020 codegen_->AddSlowPath(slow_path);
5021 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005022 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005023}
5024
5025void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5026 CpuRegister card,
5027 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005028 CpuRegister value,
5029 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005030 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005031 if (value_can_be_null) {
5032 __ testl(value, value);
5033 __ j(kEqual, &is_null);
5034 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005035 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5036 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037 __ movq(temp, object);
5038 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005039 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005040 if (value_can_be_null) {
5041 __ Bind(&is_null);
5042 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005043}
5044
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005045void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
5046 temp->SetLocations(nullptr);
5047}
5048
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005049void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005050 // Nothing to do, this is driven by the code generator.
5051}
5052
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005053void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005054 LOG(FATAL) << "Unimplemented";
5055}
5056
5057void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005058 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5059}
5060
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005061void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5062 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5063}
5064
5065void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005066 HBasicBlock* block = instruction->GetBlock();
5067 if (block->GetLoopInformation() != nullptr) {
5068 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5069 // The back edge will generate the suspend check.
5070 return;
5071 }
5072 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5073 // The goto will generate the suspend check.
5074 return;
5075 }
5076 GenerateSuspendCheck(instruction, nullptr);
5077}
5078
5079void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5080 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005081 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005082 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5083 if (slow_path == nullptr) {
5084 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5085 instruction->SetSlowPath(slow_path);
5086 codegen_->AddSlowPath(slow_path);
5087 if (successor != nullptr) {
5088 DCHECK(successor->IsLoopHeader());
5089 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5090 }
5091 } else {
5092 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5093 }
5094
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005095 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5096 /* no_rip */ true),
5097 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005098 if (successor == nullptr) {
5099 __ j(kNotEqual, slow_path->GetEntryLabel());
5100 __ Bind(slow_path->GetReturnLabel());
5101 } else {
5102 __ j(kEqual, codegen_->GetLabelOf(successor));
5103 __ jmp(slow_path->GetEntryLabel());
5104 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005105}
5106
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005107X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5108 return codegen_->GetAssembler();
5109}
5110
5111void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005112 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005113 Location source = move->GetSource();
5114 Location destination = move->GetDestination();
5115
5116 if (source.IsRegister()) {
5117 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005118 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005119 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005120 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005121 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005122 } else {
5123 DCHECK(destination.IsDoubleStackSlot());
5124 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005125 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005126 }
5127 } else if (source.IsStackSlot()) {
5128 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005129 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005130 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005131 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005132 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005133 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005134 } else {
5135 DCHECK(destination.IsStackSlot());
5136 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5137 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5138 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005139 } else if (source.IsDoubleStackSlot()) {
5140 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005141 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005142 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005143 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005144 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5145 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005146 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005147 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005148 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5149 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5150 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005151 } else if (source.IsConstant()) {
5152 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005153 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5154 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005155 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005156 if (value == 0) {
5157 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5158 } else {
5159 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5160 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005161 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005162 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005163 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005164 }
5165 } else if (constant->IsLongConstant()) {
5166 int64_t value = constant->AsLongConstant()->GetValue();
5167 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005168 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005169 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005170 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005171 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005172 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005173 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005174 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005175 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005176 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005177 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5178 if (value == 0) {
5179 // easy FP 0.0.
5180 __ xorps(dest, dest);
5181 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005182 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005183 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005184 } else {
5185 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04005186 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005187 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5188 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005189 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005190 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005191 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005192 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005193 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005194 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5195 if (value == 0) {
5196 __ xorpd(dest, dest);
5197 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005198 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005199 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005200 } else {
5201 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005202 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005203 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005204 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005205 } else if (source.IsFpuRegister()) {
5206 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005207 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005208 } else if (destination.IsStackSlot()) {
5209 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005210 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005211 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005212 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005213 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005214 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005215 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005216 }
5217}
5218
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005219void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005220 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005221 __ movl(Address(CpuRegister(RSP), mem), reg);
5222 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005223}
5224
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005225void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005226 ScratchRegisterScope ensure_scratch(
5227 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5228
5229 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5230 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5231 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5232 Address(CpuRegister(RSP), mem2 + stack_offset));
5233 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5234 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5235 CpuRegister(ensure_scratch.GetRegister()));
5236}
5237
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005238void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5239 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5240 __ movq(Address(CpuRegister(RSP), mem), reg);
5241 __ movq(reg, CpuRegister(TMP));
5242}
5243
5244void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5245 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005246 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005247
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005248 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5249 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5250 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5251 Address(CpuRegister(RSP), mem2 + stack_offset));
5252 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5253 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5254 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005255}
5256
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005257void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5258 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5259 __ movss(Address(CpuRegister(RSP), mem), reg);
5260 __ movd(reg, CpuRegister(TMP));
5261}
5262
5263void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5264 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5265 __ movsd(Address(CpuRegister(RSP), mem), reg);
5266 __ movd(reg, CpuRegister(TMP));
5267}
5268
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005269void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005270 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005271 Location source = move->GetSource();
5272 Location destination = move->GetDestination();
5273
5274 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005275 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005276 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005277 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005278 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005279 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005280 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005281 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5282 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005283 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005284 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005285 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005286 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5287 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005288 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005289 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5290 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5291 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005292 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005293 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005294 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005295 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005296 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005297 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005299 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005300 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005301 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005302 }
5303}
5304
5305
5306void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5307 __ pushq(CpuRegister(reg));
5308}
5309
5310
5311void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5312 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005313}
5314
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005315void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005316 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005317 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5318 Immediate(mirror::Class::kStatusInitialized));
5319 __ j(kLess, slow_path->GetEntryLabel());
5320 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005321 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005322}
5323
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005324void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005325 InvokeRuntimeCallingConvention calling_convention;
5326 CodeGenerator::CreateLoadClassLocationSummary(
5327 cls,
5328 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005329 Location::RegisterLocation(RAX),
5330 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005331}
5332
5333void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005334 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005335 if (cls->NeedsAccessCheck()) {
5336 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5337 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5338 cls,
5339 cls->GetDexPc(),
5340 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005341 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005342 return;
5343 }
5344
Roland Levillain0d5a2812015-11-13 10:07:31 +00005345 Location out_loc = locations->Out();
5346 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005347 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005348
Calin Juravle580b6092015-10-06 17:35:58 +01005349 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005350 DCHECK(!cls->CanCallRuntime());
5351 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005352 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5353 GenerateGcRootFieldLoad(
5354 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005355 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005356 // /* GcRoot<mirror::Class>[] */ out =
5357 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5358 __ movq(out, Address(current_method,
5359 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005360 // /* GcRoot<mirror::Class> */ out = out[type_index]
5361 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01005362
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005363 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5364 DCHECK(cls->CanCallRuntime());
5365 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5366 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5367 codegen_->AddSlowPath(slow_path);
5368 if (!cls->IsInDexCache()) {
5369 __ testl(out, out);
5370 __ j(kEqual, slow_path->GetEntryLabel());
5371 }
5372 if (cls->MustGenerateClinitCheck()) {
5373 GenerateClassInitializationCheck(slow_path, out);
5374 } else {
5375 __ Bind(slow_path->GetExitLabel());
5376 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005377 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005378 }
5379}
5380
5381void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5382 LocationSummary* locations =
5383 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5384 locations->SetInAt(0, Location::RequiresRegister());
5385 if (check->HasUses()) {
5386 locations->SetOut(Location::SameAsFirstInput());
5387 }
5388}
5389
5390void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005391 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005392 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005393 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005394 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005395 GenerateClassInitializationCheck(slow_path,
5396 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005397}
5398
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005399void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005400 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5401 ? LocationSummary::kCallOnSlowPath
5402 : LocationSummary::kNoCall;
5403 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005404 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005405 locations->SetOut(Location::RequiresRegister());
5406}
5407
5408void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005409 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005410 Location out_loc = locations->Out();
5411 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005412 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005413
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005414 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5415 GenerateGcRootFieldLoad(
5416 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005417 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5418 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005419 // /* GcRoot<mirror::String> */ out = out[string_index]
5420 GenerateGcRootFieldLoad(
5421 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005422
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005423 if (!load->IsInDexCache()) {
5424 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5425 codegen_->AddSlowPath(slow_path);
5426 __ testl(out, out);
5427 __ j(kEqual, slow_path->GetEntryLabel());
5428 __ Bind(slow_path->GetExitLabel());
5429 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005430}
5431
David Brazdilcb1c0552015-08-04 16:22:25 +01005432static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005433 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5434 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005435}
5436
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005437void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5438 LocationSummary* locations =
5439 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5440 locations->SetOut(Location::RequiresRegister());
5441}
5442
5443void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005444 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5445}
5446
5447void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5448 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5449}
5450
5451void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5452 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005453}
5454
5455void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5456 LocationSummary* locations =
5457 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5458 InvokeRuntimeCallingConvention calling_convention;
5459 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5460}
5461
5462void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005463 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5464 instruction,
5465 instruction->GetDexPc(),
5466 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005467 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005468}
5469
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005470static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5471 return kEmitCompilerReadBarrier &&
5472 (kUseBakerReadBarrier ||
5473 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5474 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5475 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5476}
5477
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005478void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005479 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005480 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5481 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005482 case TypeCheckKind::kExactCheck:
5483 case TypeCheckKind::kAbstractClassCheck:
5484 case TypeCheckKind::kClassHierarchyCheck:
5485 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005486 call_kind =
5487 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005488 break;
5489 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005490 case TypeCheckKind::kUnresolvedCheck:
5491 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005492 call_kind = LocationSummary::kCallOnSlowPath;
5493 break;
5494 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005495
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005497 locations->SetInAt(0, Location::RequiresRegister());
5498 locations->SetInAt(1, Location::Any());
5499 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5500 locations->SetOut(Location::RequiresRegister());
5501 // When read barriers are enabled, we need a temporary register for
5502 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005503 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005504 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005505 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005506}
5507
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005508void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005509 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005510 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005511 Location obj_loc = locations->InAt(0);
5512 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005513 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005514 Location out_loc = locations->Out();
5515 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005516 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5517 locations->GetTemp(0) :
5518 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005519 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005520 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5521 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5522 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005523 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005524 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005525
5526 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005527 // Avoid null check if we know obj is not null.
5528 if (instruction->MustDoNullCheck()) {
5529 __ testl(obj, obj);
5530 __ j(kEqual, &zero);
5531 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005532
Roland Levillain0d5a2812015-11-13 10:07:31 +00005533 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005534 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005535
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005536 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005537 case TypeCheckKind::kExactCheck: {
5538 if (cls.IsRegister()) {
5539 __ cmpl(out, cls.AsRegister<CpuRegister>());
5540 } else {
5541 DCHECK(cls.IsStackSlot()) << cls;
5542 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5543 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005544 if (zero.IsLinked()) {
5545 // Classes must be equal for the instanceof to succeed.
5546 __ j(kNotEqual, &zero);
5547 __ movl(out, Immediate(1));
5548 __ jmp(&done);
5549 } else {
5550 __ setcc(kEqual, out);
5551 // setcc only sets the low byte.
5552 __ andl(out, Immediate(1));
5553 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005554 break;
5555 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005556
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005557 case TypeCheckKind::kAbstractClassCheck: {
5558 // If the class is abstract, we eagerly fetch the super class of the
5559 // object to avoid doing a comparison we know will fail.
5560 NearLabel loop, success;
5561 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005562 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005563 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005564 __ testl(out, out);
5565 // If `out` is null, we use it for the result, and jump to `done`.
5566 __ j(kEqual, &done);
5567 if (cls.IsRegister()) {
5568 __ cmpl(out, cls.AsRegister<CpuRegister>());
5569 } else {
5570 DCHECK(cls.IsStackSlot()) << cls;
5571 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5572 }
5573 __ j(kNotEqual, &loop);
5574 __ movl(out, Immediate(1));
5575 if (zero.IsLinked()) {
5576 __ jmp(&done);
5577 }
5578 break;
5579 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005580
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005581 case TypeCheckKind::kClassHierarchyCheck: {
5582 // Walk over the class hierarchy to find a match.
5583 NearLabel loop, success;
5584 __ Bind(&loop);
5585 if (cls.IsRegister()) {
5586 __ cmpl(out, cls.AsRegister<CpuRegister>());
5587 } else {
5588 DCHECK(cls.IsStackSlot()) << cls;
5589 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5590 }
5591 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005592 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005593 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005594 __ testl(out, out);
5595 __ j(kNotEqual, &loop);
5596 // If `out` is null, we use it for the result, and jump to `done`.
5597 __ jmp(&done);
5598 __ Bind(&success);
5599 __ movl(out, Immediate(1));
5600 if (zero.IsLinked()) {
5601 __ jmp(&done);
5602 }
5603 break;
5604 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005605
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005606 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005607 // Do an exact check.
5608 NearLabel exact_check;
5609 if (cls.IsRegister()) {
5610 __ cmpl(out, cls.AsRegister<CpuRegister>());
5611 } else {
5612 DCHECK(cls.IsStackSlot()) << cls;
5613 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5614 }
5615 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005616 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005617 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005618 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005619 __ testl(out, out);
5620 // If `out` is null, we use it for the result, and jump to `done`.
5621 __ j(kEqual, &done);
5622 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5623 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005624 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005625 __ movl(out, Immediate(1));
5626 __ jmp(&done);
5627 break;
5628 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005629
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005630 case TypeCheckKind::kArrayCheck: {
5631 if (cls.IsRegister()) {
5632 __ cmpl(out, cls.AsRegister<CpuRegister>());
5633 } else {
5634 DCHECK(cls.IsStackSlot()) << cls;
5635 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5636 }
5637 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005638 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5639 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005640 codegen_->AddSlowPath(slow_path);
5641 __ j(kNotEqual, slow_path->GetEntryLabel());
5642 __ movl(out, Immediate(1));
5643 if (zero.IsLinked()) {
5644 __ jmp(&done);
5645 }
5646 break;
5647 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005648
Calin Juravle98893e12015-10-02 21:05:03 +01005649 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005650 case TypeCheckKind::kInterfaceCheck: {
5651 // Note that we indeed only call on slow path, but we always go
5652 // into the slow path for the unresolved & interface check
5653 // cases.
5654 //
5655 // We cannot directly call the InstanceofNonTrivial runtime
5656 // entry point without resorting to a type checking slow path
5657 // here (i.e. by calling InvokeRuntime directly), as it would
5658 // require to assign fixed registers for the inputs of this
5659 // HInstanceOf instruction (following the runtime calling
5660 // convention), which might be cluttered by the potential first
5661 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005662 //
5663 // TODO: Introduce a new runtime entry point taking the object
5664 // to test (instead of its class) as argument, and let it deal
5665 // with the read barrier issues. This will let us refactor this
5666 // case of the `switch` code as it was previously (with a direct
5667 // call to the runtime not using a type checking slow path).
5668 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005669 DCHECK(locations->OnlyCallsOnSlowPath());
5670 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5671 /* is_fatal */ false);
5672 codegen_->AddSlowPath(slow_path);
5673 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005674 if (zero.IsLinked()) {
5675 __ jmp(&done);
5676 }
5677 break;
5678 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005679 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005680
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005681 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005682 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005683 __ xorl(out, out);
5684 }
5685
5686 if (done.IsLinked()) {
5687 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005688 }
5689
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005690 if (slow_path != nullptr) {
5691 __ Bind(slow_path->GetExitLabel());
5692 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005693}
5694
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005695void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005696 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5697 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005698 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5699 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005700 case TypeCheckKind::kExactCheck:
5701 case TypeCheckKind::kAbstractClassCheck:
5702 case TypeCheckKind::kClassHierarchyCheck:
5703 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005704 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5705 LocationSummary::kCallOnSlowPath :
5706 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005707 break;
5708 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005709 case TypeCheckKind::kUnresolvedCheck:
5710 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005711 call_kind = LocationSummary::kCallOnSlowPath;
5712 break;
5713 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005714 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5715 locations->SetInAt(0, Location::RequiresRegister());
5716 locations->SetInAt(1, Location::Any());
5717 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5718 locations->AddTemp(Location::RequiresRegister());
5719 // When read barriers are enabled, we need an additional temporary
5720 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005721 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005722 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005723 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005724}
5725
5726void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005727 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005728 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005729 Location obj_loc = locations->InAt(0);
5730 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005731 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005732 Location temp_loc = locations->GetTemp(0);
5733 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005734 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5735 locations->GetTemp(1) :
5736 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005737 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5738 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5739 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5740 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005741
Roland Levillain0d5a2812015-11-13 10:07:31 +00005742 bool is_type_check_slow_path_fatal =
5743 (type_check_kind == TypeCheckKind::kExactCheck ||
5744 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5745 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5746 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5747 !instruction->CanThrowIntoCatchBlock();
5748 SlowPathCode* type_check_slow_path =
5749 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5750 is_type_check_slow_path_fatal);
5751 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005752
Mark Mendell152408f2015-12-31 12:28:50 -05005753 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005754 // Avoid null check if we know obj is not null.
5755 if (instruction->MustDoNullCheck()) {
5756 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005757 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005758 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005759
Roland Levillain0d5a2812015-11-13 10:07:31 +00005760 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005761 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005762
Roland Levillain0d5a2812015-11-13 10:07:31 +00005763 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 case TypeCheckKind::kExactCheck:
5765 case TypeCheckKind::kArrayCheck: {
5766 if (cls.IsRegister()) {
5767 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5768 } else {
5769 DCHECK(cls.IsStackSlot()) << cls;
5770 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5771 }
5772 // Jump to slow path for throwing the exception or doing a
5773 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005774 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005775 break;
5776 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005777
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005778 case TypeCheckKind::kAbstractClassCheck: {
5779 // If the class is abstract, we eagerly fetch the super class of the
5780 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005781 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005782 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005783 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005784 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005785
5786 // If the class reference currently in `temp` is not null, jump
5787 // to the `compare_classes` label to compare it with the checked
5788 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005789 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005790 __ j(kNotEqual, &compare_classes);
5791 // Otherwise, jump to the slow path to throw the exception.
5792 //
5793 // But before, move back the object's class into `temp` before
5794 // going into the slow path, as it has been overwritten in the
5795 // meantime.
5796 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005797 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005798 __ jmp(type_check_slow_path->GetEntryLabel());
5799
5800 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005801 if (cls.IsRegister()) {
5802 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5803 } else {
5804 DCHECK(cls.IsStackSlot()) << cls;
5805 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5806 }
5807 __ j(kNotEqual, &loop);
5808 break;
5809 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005810
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005811 case TypeCheckKind::kClassHierarchyCheck: {
5812 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005813 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005814 __ Bind(&loop);
5815 if (cls.IsRegister()) {
5816 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5817 } else {
5818 DCHECK(cls.IsStackSlot()) << cls;
5819 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5820 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005821 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005822
Roland Levillain0d5a2812015-11-13 10:07:31 +00005823 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005824 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005825
5826 // If the class reference currently in `temp` is not null, jump
5827 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005828 __ testl(temp, temp);
5829 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005830 // Otherwise, jump to the slow path to throw the exception.
5831 //
5832 // But before, move back the object's class into `temp` before
5833 // going into the slow path, as it has been overwritten in the
5834 // meantime.
5835 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005836 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005837 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838 break;
5839 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005840
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005842 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005843 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005844 if (cls.IsRegister()) {
5845 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5846 } else {
5847 DCHECK(cls.IsStackSlot()) << cls;
5848 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5849 }
5850 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005851
5852 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005853 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005854 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005855
5856 // If the component type is not null (i.e. the object is indeed
5857 // an array), jump to label `check_non_primitive_component_type`
5858 // to further check that this component type is not a primitive
5859 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005861 __ j(kNotEqual, &check_non_primitive_component_type);
5862 // Otherwise, jump to the slow path to throw the exception.
5863 //
5864 // But before, move back the object's class into `temp` before
5865 // going into the slow path, as it has been overwritten in the
5866 // meantime.
5867 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005868 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005869 __ jmp(type_check_slow_path->GetEntryLabel());
5870
5871 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005872 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005873 __ j(kEqual, &done);
5874 // Same comment as above regarding `temp` and the slow path.
5875 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005876 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005877 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005878 break;
5879 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005880
Calin Juravle98893e12015-10-02 21:05:03 +01005881 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005883 // We always go into the type check slow path for the unresolved &
5884 // interface check cases.
5885 //
5886 // We cannot directly call the CheckCast runtime entry point
5887 // without resorting to a type checking slow path here (i.e. by
5888 // calling InvokeRuntime directly), as it would require to
5889 // assign fixed registers for the inputs of this HInstanceOf
5890 // instruction (following the runtime calling convention), which
5891 // might be cluttered by the potential first read barrier
5892 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005893 //
5894 // TODO: Introduce a new runtime entry point taking the object
5895 // to test (instead of its class) as argument, and let it deal
5896 // with the read barrier issues. This will let us refactor this
5897 // case of the `switch` code as it was previously (with a direct
5898 // call to the runtime not using a type checking slow path).
5899 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005900 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005901 break;
5902 }
5903 __ Bind(&done);
5904
Roland Levillain0d5a2812015-11-13 10:07:31 +00005905 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005906}
5907
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005908void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5909 LocationSummary* locations =
5910 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5911 InvokeRuntimeCallingConvention calling_convention;
5912 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5913}
5914
5915void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005916 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5917 : QUICK_ENTRY_POINT(pUnlockObject),
5918 instruction,
5919 instruction->GetDexPc(),
5920 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005921 if (instruction->IsEnter()) {
5922 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5923 } else {
5924 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5925 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005926}
5927
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005928void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5929void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5930void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5931
5932void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5933 LocationSummary* locations =
5934 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5935 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5936 || instruction->GetResultType() == Primitive::kPrimLong);
5937 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005938 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005939 locations->SetOut(Location::SameAsFirstInput());
5940}
5941
5942void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5943 HandleBitwiseOperation(instruction);
5944}
5945
5946void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5947 HandleBitwiseOperation(instruction);
5948}
5949
5950void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5951 HandleBitwiseOperation(instruction);
5952}
5953
5954void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5955 LocationSummary* locations = instruction->GetLocations();
5956 Location first = locations->InAt(0);
5957 Location second = locations->InAt(1);
5958 DCHECK(first.Equals(locations->Out()));
5959
5960 if (instruction->GetResultType() == Primitive::kPrimInt) {
5961 if (second.IsRegister()) {
5962 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005963 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005964 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005965 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005966 } else {
5967 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005968 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005969 }
5970 } else if (second.IsConstant()) {
5971 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5972 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005973 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005974 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005975 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005976 } else {
5977 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005978 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005979 }
5980 } else {
5981 Address address(CpuRegister(RSP), second.GetStackIndex());
5982 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005983 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005984 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005985 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005986 } else {
5987 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005988 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005989 }
5990 }
5991 } else {
5992 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005993 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5994 bool second_is_constant = false;
5995 int64_t value = 0;
5996 if (second.IsConstant()) {
5997 second_is_constant = true;
5998 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005999 }
Mark Mendell40741f32015-04-20 22:10:34 -04006000 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006001
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006002 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006003 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006004 if (is_int32_value) {
6005 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6006 } else {
6007 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6008 }
6009 } else if (second.IsDoubleStackSlot()) {
6010 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006011 } else {
6012 __ andq(first_reg, second.AsRegister<CpuRegister>());
6013 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006014 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006015 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006016 if (is_int32_value) {
6017 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6018 } else {
6019 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6020 }
6021 } else if (second.IsDoubleStackSlot()) {
6022 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006023 } else {
6024 __ orq(first_reg, second.AsRegister<CpuRegister>());
6025 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006026 } else {
6027 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006028 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006029 if (is_int32_value) {
6030 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6031 } else {
6032 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6033 }
6034 } else if (second.IsDoubleStackSlot()) {
6035 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006036 } else {
6037 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6038 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006039 }
6040 }
6041}
6042
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006043void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6044 Location out,
6045 uint32_t offset,
6046 Location temp) {
6047 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6048 if (kEmitCompilerReadBarrier) {
6049 if (kUseBakerReadBarrier) {
6050 // Load with fast path based Baker's read barrier.
6051 // /* HeapReference<Object> */ out = *(out + offset)
6052 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6053 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
6054 } else {
6055 // Load with slow path based read barrier.
6056 // Save the value of `out` into `temp` before overwriting it
6057 // in the following move operation, as we will need it for the
6058 // read barrier below.
6059 __ movl(temp.AsRegister<CpuRegister>(), out_reg);
6060 // /* HeapReference<Object> */ out = *(out + offset)
6061 __ movl(out_reg, Address(out_reg, offset));
6062 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
6063 }
6064 } else {
6065 // Plain load with no read barrier.
6066 // /* HeapReference<Object> */ out = *(out + offset)
6067 __ movl(out_reg, Address(out_reg, offset));
6068 __ MaybeUnpoisonHeapReference(out_reg);
6069 }
6070}
6071
6072void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6073 Location out,
6074 Location obj,
6075 uint32_t offset,
6076 Location temp) {
6077 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6078 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6079 if (kEmitCompilerReadBarrier) {
6080 if (kUseBakerReadBarrier) {
6081 // Load with fast path based Baker's read barrier.
6082 // /* HeapReference<Object> */ out = *(obj + offset)
6083 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6084 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
6085 } else {
6086 // Load with slow path based read barrier.
6087 // /* HeapReference<Object> */ out = *(obj + offset)
6088 __ movl(out_reg, Address(obj_reg, offset));
6089 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6090 }
6091 } else {
6092 // Plain load with no read barrier.
6093 // /* HeapReference<Object> */ out = *(obj + offset)
6094 __ movl(out_reg, Address(obj_reg, offset));
6095 __ MaybeUnpoisonHeapReference(out_reg);
6096 }
6097}
6098
6099void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6100 Location root,
6101 CpuRegister obj,
6102 uint32_t offset) {
6103 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6104 if (kEmitCompilerReadBarrier) {
6105 if (kUseBakerReadBarrier) {
6106 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6107 // Baker's read barrier are used:
6108 //
6109 // root = obj.field;
6110 // if (Thread::Current()->GetIsGcMarking()) {
6111 // root = ReadBarrier::Mark(root)
6112 // }
6113
6114 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6115 __ movl(root_reg, Address(obj, offset));
6116 static_assert(
6117 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6118 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6119 "have different sizes.");
6120 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6121 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6122 "have different sizes.");
6123
6124 // Slow path used to mark the GC root `root`.
6125 SlowPathCode* slow_path =
6126 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6127 codegen_->AddSlowPath(slow_path);
6128
6129 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6130 /* no_rip */ true),
6131 Immediate(0));
6132 __ j(kNotEqual, slow_path->GetEntryLabel());
6133 __ Bind(slow_path->GetExitLabel());
6134 } else {
6135 // GC root loaded through a slow path for read barriers other
6136 // than Baker's.
6137 // /* GcRoot<mirror::Object>* */ root = obj + offset
6138 __ leaq(root_reg, Address(obj, offset));
6139 // /* mirror::Object* */ root = root->Read()
6140 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6141 }
6142 } else {
6143 // Plain GC root load with no read barrier.
6144 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6145 __ movl(root_reg, Address(obj, offset));
6146 }
6147}
6148
6149void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6150 Location ref,
6151 CpuRegister obj,
6152 uint32_t offset,
6153 Location temp,
6154 bool needs_null_check) {
6155 DCHECK(kEmitCompilerReadBarrier);
6156 DCHECK(kUseBakerReadBarrier);
6157
6158 // /* HeapReference<Object> */ ref = *(obj + offset)
6159 Address src(obj, offset);
6160 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6161}
6162
6163void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6164 Location ref,
6165 CpuRegister obj,
6166 uint32_t data_offset,
6167 Location index,
6168 Location temp,
6169 bool needs_null_check) {
6170 DCHECK(kEmitCompilerReadBarrier);
6171 DCHECK(kUseBakerReadBarrier);
6172
6173 // /* HeapReference<Object> */ ref =
6174 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6175 Address src = index.IsConstant() ?
6176 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6177 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6178 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6179}
6180
6181void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6182 Location ref,
6183 CpuRegister obj,
6184 const Address& src,
6185 Location temp,
6186 bool needs_null_check) {
6187 DCHECK(kEmitCompilerReadBarrier);
6188 DCHECK(kUseBakerReadBarrier);
6189
6190 // In slow path based read barriers, the read barrier call is
6191 // inserted after the original load. However, in fast path based
6192 // Baker's read barriers, we need to perform the load of
6193 // mirror::Object::monitor_ *before* the original reference load.
6194 // This load-load ordering is required by the read barrier.
6195 // The fast path/slow path (for Baker's algorithm) should look like:
6196 //
6197 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6198 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6199 // HeapReference<Object> ref = *src; // Original reference load.
6200 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6201 // if (is_gray) {
6202 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6203 // }
6204 //
6205 // Note: the original implementation in ReadBarrier::Barrier is
6206 // slightly more complex as:
6207 // - it implements the load-load fence using a data dependency on
6208 // the high-bits of rb_state, which are expected to be all zeroes;
6209 // - it performs additional checks that we do not do here for
6210 // performance reasons.
6211
6212 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6213 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6214 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6215
6216 // /* int32_t */ monitor = obj->monitor_
6217 __ movl(temp_reg, Address(obj, monitor_offset));
6218 if (needs_null_check) {
6219 MaybeRecordImplicitNullCheck(instruction);
6220 }
6221 // /* LockWord */ lock_word = LockWord(monitor)
6222 static_assert(sizeof(LockWord) == sizeof(int32_t),
6223 "art::LockWord and int32_t have different sizes.");
6224 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6225 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6226 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6227 static_assert(
6228 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6229 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6230
6231 // Load fence to prevent load-load reordering.
6232 // Note that this is a no-op, thanks to the x86-64 memory model.
6233 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6234
6235 // The actual reference load.
6236 // /* HeapReference<Object> */ ref = *src
6237 __ movl(ref_reg, src);
6238
6239 // Object* ref = ref_addr->AsMirrorPtr()
6240 __ MaybeUnpoisonHeapReference(ref_reg);
6241
6242 // Slow path used to mark the object `ref` when it is gray.
6243 SlowPathCode* slow_path =
6244 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6245 AddSlowPath(slow_path);
6246
6247 // if (rb_state == ReadBarrier::gray_ptr_)
6248 // ref = ReadBarrier::Mark(ref);
6249 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6250 __ j(kEqual, slow_path->GetEntryLabel());
6251 __ Bind(slow_path->GetExitLabel());
6252}
6253
6254void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6255 Location out,
6256 Location ref,
6257 Location obj,
6258 uint32_t offset,
6259 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006260 DCHECK(kEmitCompilerReadBarrier);
6261
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006262 // Insert a slow path based read barrier *after* the reference load.
6263 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006264 // If heap poisoning is enabled, the unpoisoning of the loaded
6265 // reference will be carried out by the runtime within the slow
6266 // path.
6267 //
6268 // Note that `ref` currently does not get unpoisoned (when heap
6269 // poisoning is enabled), which is alright as the `ref` argument is
6270 // not used by the artReadBarrierSlow entry point.
6271 //
6272 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6273 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6274 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6275 AddSlowPath(slow_path);
6276
Roland Levillain0d5a2812015-11-13 10:07:31 +00006277 __ jmp(slow_path->GetEntryLabel());
6278 __ Bind(slow_path->GetExitLabel());
6279}
6280
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006281void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6282 Location out,
6283 Location ref,
6284 Location obj,
6285 uint32_t offset,
6286 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006287 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006288 // Baker's read barriers shall be handled by the fast path
6289 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6290 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006291 // If heap poisoning is enabled, unpoisoning will be taken care of
6292 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006293 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006294 } else if (kPoisonHeapReferences) {
6295 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6296 }
6297}
6298
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006299void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6300 Location out,
6301 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 DCHECK(kEmitCompilerReadBarrier);
6303
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006304 // Insert a slow path based read barrier *after* the GC root load.
6305 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006306 // Note that GC roots are not affected by heap poisoning, so we do
6307 // not need to do anything special for this here.
6308 SlowPathCode* slow_path =
6309 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6310 AddSlowPath(slow_path);
6311
Roland Levillain0d5a2812015-11-13 10:07:31 +00006312 __ jmp(slow_path->GetEntryLabel());
6313 __ Bind(slow_path->GetExitLabel());
6314}
6315
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006316void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006317 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006318 LOG(FATAL) << "Unreachable";
6319}
6320
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006321void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006322 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006323 LOG(FATAL) << "Unreachable";
6324}
6325
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006326void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
6327 DCHECK(codegen_->IsBaseline());
6328 LocationSummary* locations =
6329 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6330 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6331}
6332
6333void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6334 DCHECK(codegen_->IsBaseline());
6335 // Will be generated at use site.
6336}
6337
Mark Mendellfe57faa2015-09-18 09:26:15 -04006338// Simple implementation of packed switch - generate cascaded compare/jumps.
6339void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6340 LocationSummary* locations =
6341 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6342 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006343 locations->AddTemp(Location::RequiresRegister());
6344 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006345}
6346
6347void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6348 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006349 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006350 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006351 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6352 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6353 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006354 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6355
6356 // Should we generate smaller inline compare/jumps?
6357 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6358 // Figure out the correct compare values and jump conditions.
6359 // Handle the first compare/branch as a special case because it might
6360 // jump to the default case.
6361 DCHECK_GT(num_entries, 2u);
6362 Condition first_condition;
6363 uint32_t index;
6364 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6365 if (lower_bound != 0) {
6366 first_condition = kLess;
6367 __ cmpl(value_reg_in, Immediate(lower_bound));
6368 __ j(first_condition, codegen_->GetLabelOf(default_block));
6369 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6370
6371 index = 1;
6372 } else {
6373 // Handle all the compare/jumps below.
6374 first_condition = kBelow;
6375 index = 0;
6376 }
6377
6378 // Handle the rest of the compare/jumps.
6379 for (; index + 1 < num_entries; index += 2) {
6380 int32_t compare_to_value = lower_bound + index + 1;
6381 __ cmpl(value_reg_in, Immediate(compare_to_value));
6382 // Jump to successors[index] if value < case_value[index].
6383 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6384 // Jump to successors[index + 1] if value == case_value[index + 1].
6385 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6386 }
6387
6388 if (index != num_entries) {
6389 // There are an odd number of entries. Handle the last one.
6390 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006391 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006392 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6393 }
6394
6395 // And the default for any other value.
6396 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6397 __ jmp(codegen_->GetLabelOf(default_block));
6398 }
6399 return;
6400 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006401
6402 // Remove the bias, if needed.
6403 Register value_reg_out = value_reg_in.AsRegister();
6404 if (lower_bound != 0) {
6405 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6406 value_reg_out = temp_reg.AsRegister();
6407 }
6408 CpuRegister value_reg(value_reg_out);
6409
6410 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006411 __ cmpl(value_reg, Immediate(num_entries - 1));
6412 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006413
Mark Mendell9c86b482015-09-18 13:36:07 -04006414 // We are in the range of the table.
6415 // Load the address of the jump table in the constant area.
6416 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006417
Mark Mendell9c86b482015-09-18 13:36:07 -04006418 // Load the (signed) offset from the jump table.
6419 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6420
6421 // Add the offset to the address of the table base.
6422 __ addq(temp_reg, base_reg);
6423
6424 // And jump.
6425 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006426}
6427
Mark Mendell92e83bf2015-05-07 11:25:03 -04006428void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6429 if (value == 0) {
6430 __ xorl(dest, dest);
6431 } else if (value > 0 && IsInt<32>(value)) {
6432 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
6433 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6434 } else {
6435 __ movq(dest, Immediate(value));
6436 }
6437}
6438
Mark Mendellcfa410b2015-05-25 16:02:44 -04006439void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6440 DCHECK(dest.IsDoubleStackSlot());
6441 if (IsInt<32>(value)) {
6442 // Can move directly as an int32 constant.
6443 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6444 Immediate(static_cast<int32_t>(value)));
6445 } else {
6446 Load64BitValue(CpuRegister(TMP), value);
6447 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6448 }
6449}
6450
Mark Mendell9c86b482015-09-18 13:36:07 -04006451/**
6452 * Class to handle late fixup of offsets into constant area.
6453 */
6454class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6455 public:
6456 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6457 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6458
6459 protected:
6460 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6461
6462 CodeGeneratorX86_64* codegen_;
6463
6464 private:
6465 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6466 // Patch the correct offset for the instruction. We use the address of the
6467 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6468 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6469 int32_t relative_position = constant_offset - pos;
6470
6471 // Patch in the right value.
6472 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6473 }
6474
6475 // Location in constant area that the fixup refers to.
6476 size_t offset_into_constant_area_;
6477};
6478
6479/**
6480 t * Class to handle late fixup of offsets to a jump table that will be created in the
6481 * constant area.
6482 */
6483class JumpTableRIPFixup : public RIPFixup {
6484 public:
6485 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6486 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6487
6488 void CreateJumpTable() {
6489 X86_64Assembler* assembler = codegen_->GetAssembler();
6490
6491 // Ensure that the reference to the jump table has the correct offset.
6492 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6493 SetOffset(offset_in_constant_table);
6494
6495 // Compute the offset from the start of the function to this jump table.
6496 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6497
6498 // Populate the jump table with the correct values for the jump table.
6499 int32_t num_entries = switch_instr_->GetNumEntries();
6500 HBasicBlock* block = switch_instr_->GetBlock();
6501 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6502 // The value that we want is the target offset - the position of the table.
6503 for (int32_t i = 0; i < num_entries; i++) {
6504 HBasicBlock* b = successors[i];
6505 Label* l = codegen_->GetLabelOf(b);
6506 DCHECK(l->IsBound());
6507 int32_t offset_to_block = l->Position() - current_table_offset;
6508 assembler->AppendInt32(offset_to_block);
6509 }
6510 }
6511
6512 private:
6513 const HPackedSwitch* switch_instr_;
6514};
6515
Mark Mendellf55c3e02015-03-26 21:07:46 -04006516void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6517 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006518 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006519 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6520 // 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 -04006521 assembler->Align(4, 0);
6522 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006523
6524 // Populate any jump tables.
6525 for (auto jump_table : fixups_to_jump_tables_) {
6526 jump_table->CreateJumpTable();
6527 }
6528
6529 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006530 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006531 }
6532
6533 // And finish up.
6534 CodeGenerator::Finalize(allocator);
6535}
6536
Mark Mendellf55c3e02015-03-26 21:07:46 -04006537Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6538 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6539 return Address::RIP(fixup);
6540}
6541
6542Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6543 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6544 return Address::RIP(fixup);
6545}
6546
6547Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6548 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6549 return Address::RIP(fixup);
6550}
6551
6552Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6553 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6554 return Address::RIP(fixup);
6555}
6556
Andreas Gampe85b62f22015-09-09 13:15:38 -07006557// TODO: trg as memory.
6558void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6559 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006560 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006561 return;
6562 }
6563
6564 DCHECK_NE(type, Primitive::kPrimVoid);
6565
6566 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6567 if (trg.Equals(return_loc)) {
6568 return;
6569 }
6570
6571 // Let the parallel move resolver take care of all of this.
6572 HParallelMove parallel_move(GetGraph()->GetArena());
6573 parallel_move.AddMove(return_loc, trg, type, nullptr);
6574 GetMoveResolver()->EmitNativeCode(&parallel_move);
6575}
6576
Mark Mendell9c86b482015-09-18 13:36:07 -04006577Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6578 // Create a fixup to be used to create and address the jump table.
6579 JumpTableRIPFixup* table_fixup =
6580 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6581
6582 // We have to populate the jump tables.
6583 fixups_to_jump_tables_.push_back(table_fixup);
6584 return Address::RIP(table_fixup);
6585}
6586
Mark Mendellea5af682015-10-22 17:35:49 -04006587void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6588 const Address& addr_high,
6589 int64_t v,
6590 HInstruction* instruction) {
6591 if (IsInt<32>(v)) {
6592 int32_t v_32 = v;
6593 __ movq(addr_low, Immediate(v_32));
6594 MaybeRecordImplicitNullCheck(instruction);
6595 } else {
6596 // Didn't fit in a register. Do it in pieces.
6597 int32_t low_v = Low32Bits(v);
6598 int32_t high_v = High32Bits(v);
6599 __ movl(addr_low, Immediate(low_v));
6600 MaybeRecordImplicitNullCheck(instruction);
6601 __ movl(addr_high, Immediate(high_v));
6602 }
6603}
6604
Roland Levillain4d027112015-07-01 15:41:14 +01006605#undef __
6606
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006607} // namespace x86_64
6608} // namespace art