blob: 126c83b17e2c3778361655a69149b3020d111dc7 [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) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001611 if (codegen_->HasStackMapAtCurrentPc()) {
1612 // Ensure that we do not collide with the stack map of the previous instruction.
1613 __ nop();
1614 }
David Srbecky0cf44932015-12-09 14:09:59 +00001615 codegen_->RecordPcInfo(info, info->GetDexPc());
1616}
1617
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001618void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1619 local->SetLocations(nullptr);
1620}
1621
1622void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1623 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1624}
1625
1626void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1627 local->SetLocations(nullptr);
1628}
1629
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001630void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001631 // Nothing to do, this is driven by the code generator.
1632}
1633
1634void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001635 LocationSummary* locations =
1636 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001637 switch (store->InputAt(1)->GetType()) {
1638 case Primitive::kPrimBoolean:
1639 case Primitive::kPrimByte:
1640 case Primitive::kPrimChar:
1641 case Primitive::kPrimShort:
1642 case Primitive::kPrimInt:
1643 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001644 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001645 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1646 break;
1647
1648 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001649 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001650 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1651 break;
1652
1653 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001654 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001655 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001656}
1657
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001658void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001659}
1660
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001661void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001662 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001663 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001664 // Handle the long/FP comparisons made in instruction simplification.
1665 switch (cond->InputAt(0)->GetType()) {
1666 case Primitive::kPrimLong:
1667 locations->SetInAt(0, Location::RequiresRegister());
1668 locations->SetInAt(1, Location::Any());
1669 break;
1670 case Primitive::kPrimFloat:
1671 case Primitive::kPrimDouble:
1672 locations->SetInAt(0, Location::RequiresFpuRegister());
1673 locations->SetInAt(1, Location::Any());
1674 break;
1675 default:
1676 locations->SetInAt(0, Location::RequiresRegister());
1677 locations->SetInAt(1, Location::Any());
1678 break;
1679 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001680 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001681 locations->SetOut(Location::RequiresRegister());
1682 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001683}
1684
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001685void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001686 if (!cond->NeedsMaterialization()) {
1687 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001688 }
Mark Mendellc4701932015-04-10 13:18:51 -04001689
1690 LocationSummary* locations = cond->GetLocations();
1691 Location lhs = locations->InAt(0);
1692 Location rhs = locations->InAt(1);
1693 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001694 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001695
1696 switch (cond->InputAt(0)->GetType()) {
1697 default:
1698 // Integer case.
1699
1700 // Clear output register: setcc only sets the low byte.
1701 __ xorl(reg, reg);
1702
1703 if (rhs.IsRegister()) {
1704 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1705 } else if (rhs.IsConstant()) {
1706 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1707 if (constant == 0) {
1708 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1709 } else {
1710 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1711 }
1712 } else {
1713 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1714 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001715 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001716 return;
1717 case Primitive::kPrimLong:
1718 // Clear output register: setcc only sets the low byte.
1719 __ xorl(reg, reg);
1720
1721 if (rhs.IsRegister()) {
1722 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1723 } else if (rhs.IsConstant()) {
1724 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1725 if (IsInt<32>(value)) {
1726 if (value == 0) {
1727 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1728 } else {
1729 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1730 }
1731 } else {
1732 // Value won't fit in an int.
1733 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1734 }
1735 } else {
1736 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1737 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001738 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001739 return;
1740 case Primitive::kPrimFloat: {
1741 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1742 if (rhs.IsConstant()) {
1743 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1744 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1745 } else if (rhs.IsStackSlot()) {
1746 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1747 } else {
1748 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1749 }
1750 GenerateFPJumps(cond, &true_label, &false_label);
1751 break;
1752 }
1753 case Primitive::kPrimDouble: {
1754 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1755 if (rhs.IsConstant()) {
1756 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1757 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1758 } else if (rhs.IsDoubleStackSlot()) {
1759 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1760 } else {
1761 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1762 }
1763 GenerateFPJumps(cond, &true_label, &false_label);
1764 break;
1765 }
1766 }
1767
1768 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001769 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001770
Roland Levillain4fa13f62015-07-06 18:11:54 +01001771 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001772 __ Bind(&false_label);
1773 __ xorl(reg, reg);
1774 __ jmp(&done_label);
1775
Roland Levillain4fa13f62015-07-06 18:11:54 +01001776 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001777 __ Bind(&true_label);
1778 __ movl(reg, Immediate(1));
1779 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001780}
1781
1782void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001784}
1785
1786void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001788}
1789
1790void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001791 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001792}
1793
1794void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001795 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001796}
1797
1798void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001799 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001800}
1801
1802void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001803 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001804}
1805
1806void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001807 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001808}
1809
1810void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001811 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001812}
1813
1814void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001815 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001816}
1817
1818void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001819 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001820}
1821
1822void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001824}
1825
1826void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001827 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001828}
1829
Aart Bike9f37602015-10-09 11:15:55 -07001830void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001831 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001832}
1833
1834void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001835 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001836}
1837
1838void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001839 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001840}
1841
1842void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001843 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001844}
1845
1846void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001847 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001848}
1849
1850void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001851 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001852}
1853
1854void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001855 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001856}
1857
1858void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001859 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001860}
1861
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001862void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001863 LocationSummary* locations =
1864 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001865 switch (compare->InputAt(0)->GetType()) {
1866 case Primitive::kPrimLong: {
1867 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001868 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001869 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1870 break;
1871 }
1872 case Primitive::kPrimFloat:
1873 case Primitive::kPrimDouble: {
1874 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001875 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001876 locations->SetOut(Location::RequiresRegister());
1877 break;
1878 }
1879 default:
1880 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1881 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001882}
1883
1884void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001885 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001886 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001887 Location left = locations->InAt(0);
1888 Location right = locations->InAt(1);
1889
Mark Mendell0c9497d2015-08-21 09:30:05 -04001890 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001891 Primitive::Type type = compare->InputAt(0)->GetType();
1892 switch (type) {
1893 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001894 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1895 if (right.IsConstant()) {
1896 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001897 if (IsInt<32>(value)) {
1898 if (value == 0) {
1899 __ testq(left_reg, left_reg);
1900 } else {
1901 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1902 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001903 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001904 // Value won't fit in an int.
1905 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001906 }
Mark Mendell40741f32015-04-20 22:10:34 -04001907 } else if (right.IsDoubleStackSlot()) {
1908 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001909 } else {
1910 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1911 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001912 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001913 }
1914 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001915 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1916 if (right.IsConstant()) {
1917 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1918 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1919 } else if (right.IsStackSlot()) {
1920 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1921 } else {
1922 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1923 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001924 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1925 break;
1926 }
1927 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001928 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1929 if (right.IsConstant()) {
1930 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1931 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1932 } else if (right.IsDoubleStackSlot()) {
1933 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1934 } else {
1935 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1936 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001937 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1938 break;
1939 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001940 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001941 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001942 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001943 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001944 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001945 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001946
Calin Juravle91debbc2014-11-26 19:01:09 +00001947 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001948 __ movl(out, Immediate(1));
1949 __ jmp(&done);
1950
1951 __ Bind(&less);
1952 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001953
1954 __ Bind(&done);
1955}
1956
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001957void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001958 LocationSummary* locations =
1959 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001960 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001961}
1962
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001963void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001964 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001965}
1966
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001967void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1968 LocationSummary* locations =
1969 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1970 locations->SetOut(Location::ConstantLocation(constant));
1971}
1972
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001973void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001974 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001975}
1976
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001977void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001978 LocationSummary* locations =
1979 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001980 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001981}
1982
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001983void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001984 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001985}
1986
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001987void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1988 LocationSummary* locations =
1989 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1990 locations->SetOut(Location::ConstantLocation(constant));
1991}
1992
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001993void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001994 // Will be generated at use site.
1995}
1996
1997void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1998 LocationSummary* locations =
1999 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2000 locations->SetOut(Location::ConstantLocation(constant));
2001}
2002
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002003void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2004 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002005 // Will be generated at use site.
2006}
2007
Calin Juravle27df7582015-04-17 19:12:31 +01002008void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2009 memory_barrier->SetLocations(nullptr);
2010}
2011
2012void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002013 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002014}
2015
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002016void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2017 ret->SetLocations(nullptr);
2018}
2019
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002020void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002021 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002022}
2023
2024void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002025 LocationSummary* locations =
2026 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002027 switch (ret->InputAt(0)->GetType()) {
2028 case Primitive::kPrimBoolean:
2029 case Primitive::kPrimByte:
2030 case Primitive::kPrimChar:
2031 case Primitive::kPrimShort:
2032 case Primitive::kPrimInt:
2033 case Primitive::kPrimNot:
2034 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002035 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002036 break;
2037
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002038 case Primitive::kPrimFloat:
2039 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002040 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002041 break;
2042
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002043 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002044 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002045 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002046}
2047
2048void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2049 if (kIsDebugBuild) {
2050 switch (ret->InputAt(0)->GetType()) {
2051 case Primitive::kPrimBoolean:
2052 case Primitive::kPrimByte:
2053 case Primitive::kPrimChar:
2054 case Primitive::kPrimShort:
2055 case Primitive::kPrimInt:
2056 case Primitive::kPrimNot:
2057 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002058 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002059 break;
2060
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002061 case Primitive::kPrimFloat:
2062 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002063 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002064 XMM0);
2065 break;
2066
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002067 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002068 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002069 }
2070 }
2071 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002072}
2073
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002074Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2075 switch (type) {
2076 case Primitive::kPrimBoolean:
2077 case Primitive::kPrimByte:
2078 case Primitive::kPrimChar:
2079 case Primitive::kPrimShort:
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimNot:
2082 case Primitive::kPrimLong:
2083 return Location::RegisterLocation(RAX);
2084
2085 case Primitive::kPrimVoid:
2086 return Location::NoLocation();
2087
2088 case Primitive::kPrimDouble:
2089 case Primitive::kPrimFloat:
2090 return Location::FpuRegisterLocation(XMM0);
2091 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002092
2093 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002094}
2095
2096Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2097 return Location::RegisterLocation(kMethodRegisterArgument);
2098}
2099
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002100Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002101 switch (type) {
2102 case Primitive::kPrimBoolean:
2103 case Primitive::kPrimByte:
2104 case Primitive::kPrimChar:
2105 case Primitive::kPrimShort:
2106 case Primitive::kPrimInt:
2107 case Primitive::kPrimNot: {
2108 uint32_t index = gp_index_++;
2109 stack_index_++;
2110 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002111 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002112 } else {
2113 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2114 }
2115 }
2116
2117 case Primitive::kPrimLong: {
2118 uint32_t index = gp_index_;
2119 stack_index_ += 2;
2120 if (index < calling_convention.GetNumberOfRegisters()) {
2121 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002122 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002123 } else {
2124 gp_index_ += 2;
2125 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2126 }
2127 }
2128
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002129 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002130 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002131 stack_index_++;
2132 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002133 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002134 } else {
2135 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2136 }
2137 }
2138
2139 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002140 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002141 stack_index_ += 2;
2142 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002143 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002144 } else {
2145 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2146 }
2147 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002148
2149 case Primitive::kPrimVoid:
2150 LOG(FATAL) << "Unexpected parameter type " << type;
2151 break;
2152 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002153 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002154}
2155
Calin Juravle175dc732015-08-25 15:42:32 +01002156void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2157 // The trampoline uses the same calling convention as dex calling conventions,
2158 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2159 // the method_idx.
2160 HandleInvoke(invoke);
2161}
2162
2163void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2164 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2165}
2166
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002167void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002168 // When we do not run baseline, explicit clinit checks triggered by static
2169 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2170 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002171
Mark Mendellfb8d2792015-03-31 22:16:59 -04002172 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002173 if (intrinsic.TryDispatch(invoke)) {
2174 return;
2175 }
2176
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002177 HandleInvoke(invoke);
2178}
2179
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002180static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2181 if (invoke->GetLocations()->Intrinsified()) {
2182 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2183 intrinsic.Dispatch(invoke);
2184 return true;
2185 }
2186 return false;
2187}
2188
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002189void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002190 // When we do not run baseline, explicit clinit checks triggered by static
2191 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2192 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002193
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002194 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2195 return;
2196 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002197
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002198 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002199 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002200 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002201 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002202}
2203
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002204void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002205 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002206 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002207}
2208
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002209void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002210 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002211 if (intrinsic.TryDispatch(invoke)) {
2212 return;
2213 }
2214
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002215 HandleInvoke(invoke);
2216}
2217
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002218void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002219 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2220 return;
2221 }
2222
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002223 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002224 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002225 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002226}
2227
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002228void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2229 HandleInvoke(invoke);
2230 // Add the hidden argument.
2231 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2232}
2233
2234void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2235 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002236 LocationSummary* locations = invoke->GetLocations();
2237 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2238 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002239 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2240 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002241 Location receiver = locations->InAt(0);
2242 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2243
Roland Levillain0d5a2812015-11-13 10:07:31 +00002244 // Set the hidden argument. This is safe to do this here, as RAX
2245 // won't be modified thereafter, before the `call` instruction.
2246 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002247 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002248
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002249 if (receiver.IsStackSlot()) {
2250 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002251 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002252 __ movl(temp, Address(temp, class_offset));
2253 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002254 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002255 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002257 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002258 // Instead of simply (possibly) unpoisoning `temp` here, we should
2259 // emit a read barrier for the previous class reference load.
2260 // However this is not required in practice, as this is an
2261 // intermediate/temporary reference and because the current
2262 // concurrent copying collector keeps the from-space memory
2263 // intact/accessible until the end of the marking phase (the
2264 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002265 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002266 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002267 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002268 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002269 __ call(Address(temp,
2270 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002271
2272 DCHECK(!codegen_->IsLeafMethod());
2273 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2274}
2275
Roland Levillain88cb1752014-10-20 16:36:47 +01002276void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2277 LocationSummary* locations =
2278 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2279 switch (neg->GetResultType()) {
2280 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002281 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002282 locations->SetInAt(0, Location::RequiresRegister());
2283 locations->SetOut(Location::SameAsFirstInput());
2284 break;
2285
Roland Levillain88cb1752014-10-20 16:36:47 +01002286 case Primitive::kPrimFloat:
2287 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002288 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002289 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002290 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002291 break;
2292
2293 default:
2294 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2295 }
2296}
2297
2298void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2299 LocationSummary* locations = neg->GetLocations();
2300 Location out = locations->Out();
2301 Location in = locations->InAt(0);
2302 switch (neg->GetResultType()) {
2303 case Primitive::kPrimInt:
2304 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002305 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002306 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002307 break;
2308
2309 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002310 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002311 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002312 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002313 break;
2314
Roland Levillain5368c212014-11-27 15:03:41 +00002315 case Primitive::kPrimFloat: {
2316 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002317 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002318 // Implement float negation with an exclusive or with value
2319 // 0x80000000 (mask for bit 31, representing the sign of a
2320 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002321 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002322 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002323 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002324 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002325
Roland Levillain5368c212014-11-27 15:03:41 +00002326 case Primitive::kPrimDouble: {
2327 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002328 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002329 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002330 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002331 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002332 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002333 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002334 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002335 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002336
2337 default:
2338 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2339 }
2340}
2341
Roland Levillaindff1f282014-11-05 14:15:05 +00002342void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2343 LocationSummary* locations =
2344 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2345 Primitive::Type result_type = conversion->GetResultType();
2346 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002347 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002348
David Brazdilb2bd1c52015-03-25 11:17:37 +00002349 // The Java language does not allow treating boolean as an integral type but
2350 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002351
Roland Levillaindff1f282014-11-05 14:15:05 +00002352 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002353 case Primitive::kPrimByte:
2354 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002355 case Primitive::kPrimBoolean:
2356 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002357 case Primitive::kPrimShort:
2358 case Primitive::kPrimInt:
2359 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002360 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002361 locations->SetInAt(0, Location::Any());
2362 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2363 break;
2364
2365 default:
2366 LOG(FATAL) << "Unexpected type conversion from " << input_type
2367 << " to " << result_type;
2368 }
2369 break;
2370
Roland Levillain01a8d712014-11-14 16:27:39 +00002371 case Primitive::kPrimShort:
2372 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002373 case Primitive::kPrimBoolean:
2374 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002375 case Primitive::kPrimByte:
2376 case Primitive::kPrimInt:
2377 case Primitive::kPrimChar:
2378 // Processing a Dex `int-to-short' instruction.
2379 locations->SetInAt(0, Location::Any());
2380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2381 break;
2382
2383 default:
2384 LOG(FATAL) << "Unexpected type conversion from " << input_type
2385 << " to " << result_type;
2386 }
2387 break;
2388
Roland Levillain946e1432014-11-11 17:35:19 +00002389 case Primitive::kPrimInt:
2390 switch (input_type) {
2391 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002392 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002393 locations->SetInAt(0, Location::Any());
2394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2395 break;
2396
2397 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002398 // Processing a Dex `float-to-int' instruction.
2399 locations->SetInAt(0, Location::RequiresFpuRegister());
2400 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002401 break;
2402
Roland Levillain946e1432014-11-11 17:35:19 +00002403 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002404 // Processing a Dex `double-to-int' instruction.
2405 locations->SetInAt(0, Location::RequiresFpuRegister());
2406 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002407 break;
2408
2409 default:
2410 LOG(FATAL) << "Unexpected type conversion from " << input_type
2411 << " to " << result_type;
2412 }
2413 break;
2414
Roland Levillaindff1f282014-11-05 14:15:05 +00002415 case Primitive::kPrimLong:
2416 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002417 case Primitive::kPrimBoolean:
2418 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002419 case Primitive::kPrimByte:
2420 case Primitive::kPrimShort:
2421 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002422 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002423 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002424 // TODO: We would benefit from a (to-be-implemented)
2425 // Location::RegisterOrStackSlot requirement for this input.
2426 locations->SetInAt(0, Location::RequiresRegister());
2427 locations->SetOut(Location::RequiresRegister());
2428 break;
2429
2430 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002431 // Processing a Dex `float-to-long' instruction.
2432 locations->SetInAt(0, Location::RequiresFpuRegister());
2433 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002434 break;
2435
Roland Levillaindff1f282014-11-05 14:15:05 +00002436 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002437 // Processing a Dex `double-to-long' instruction.
2438 locations->SetInAt(0, Location::RequiresFpuRegister());
2439 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002440 break;
2441
2442 default:
2443 LOG(FATAL) << "Unexpected type conversion from " << input_type
2444 << " to " << result_type;
2445 }
2446 break;
2447
Roland Levillain981e4542014-11-14 11:47:14 +00002448 case Primitive::kPrimChar:
2449 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002450 case Primitive::kPrimBoolean:
2451 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002452 case Primitive::kPrimByte:
2453 case Primitive::kPrimShort:
2454 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002455 // Processing a Dex `int-to-char' instruction.
2456 locations->SetInAt(0, Location::Any());
2457 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2458 break;
2459
2460 default:
2461 LOG(FATAL) << "Unexpected type conversion from " << input_type
2462 << " to " << result_type;
2463 }
2464 break;
2465
Roland Levillaindff1f282014-11-05 14:15:05 +00002466 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002467 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002468 case Primitive::kPrimBoolean:
2469 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002470 case Primitive::kPrimByte:
2471 case Primitive::kPrimShort:
2472 case Primitive::kPrimInt:
2473 case Primitive::kPrimChar:
2474 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002475 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002476 locations->SetOut(Location::RequiresFpuRegister());
2477 break;
2478
2479 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002480 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002481 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002482 locations->SetOut(Location::RequiresFpuRegister());
2483 break;
2484
Roland Levillaincff13742014-11-17 14:32:17 +00002485 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002486 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002487 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002488 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002489 break;
2490
2491 default:
2492 LOG(FATAL) << "Unexpected type conversion from " << input_type
2493 << " to " << result_type;
2494 };
2495 break;
2496
Roland Levillaindff1f282014-11-05 14:15:05 +00002497 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002498 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002499 case Primitive::kPrimBoolean:
2500 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002501 case Primitive::kPrimByte:
2502 case Primitive::kPrimShort:
2503 case Primitive::kPrimInt:
2504 case Primitive::kPrimChar:
2505 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002506 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002507 locations->SetOut(Location::RequiresFpuRegister());
2508 break;
2509
2510 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002511 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002512 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002513 locations->SetOut(Location::RequiresFpuRegister());
2514 break;
2515
Roland Levillaincff13742014-11-17 14:32:17 +00002516 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002517 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002518 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002519 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002520 break;
2521
2522 default:
2523 LOG(FATAL) << "Unexpected type conversion from " << input_type
2524 << " to " << result_type;
2525 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002526 break;
2527
2528 default:
2529 LOG(FATAL) << "Unexpected type conversion from " << input_type
2530 << " to " << result_type;
2531 }
2532}
2533
2534void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2535 LocationSummary* locations = conversion->GetLocations();
2536 Location out = locations->Out();
2537 Location in = locations->InAt(0);
2538 Primitive::Type result_type = conversion->GetResultType();
2539 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002540 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002541 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002542 case Primitive::kPrimByte:
2543 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002544 case Primitive::kPrimBoolean:
2545 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002546 case Primitive::kPrimShort:
2547 case Primitive::kPrimInt:
2548 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002549 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002550 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002551 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002552 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002553 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002554 Address(CpuRegister(RSP), in.GetStackIndex()));
2555 } else {
2556 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002557 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002558 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2559 }
2560 break;
2561
2562 default:
2563 LOG(FATAL) << "Unexpected type conversion from " << input_type
2564 << " to " << result_type;
2565 }
2566 break;
2567
Roland Levillain01a8d712014-11-14 16:27:39 +00002568 case Primitive::kPrimShort:
2569 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002570 case Primitive::kPrimBoolean:
2571 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002572 case Primitive::kPrimByte:
2573 case Primitive::kPrimInt:
2574 case Primitive::kPrimChar:
2575 // Processing a Dex `int-to-short' instruction.
2576 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002577 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002578 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002579 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002580 Address(CpuRegister(RSP), in.GetStackIndex()));
2581 } else {
2582 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002583 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002584 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2585 }
2586 break;
2587
2588 default:
2589 LOG(FATAL) << "Unexpected type conversion from " << input_type
2590 << " to " << result_type;
2591 }
2592 break;
2593
Roland Levillain946e1432014-11-11 17:35:19 +00002594 case Primitive::kPrimInt:
2595 switch (input_type) {
2596 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002597 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002598 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002600 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002601 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002602 Address(CpuRegister(RSP), in.GetStackIndex()));
2603 } else {
2604 DCHECK(in.IsConstant());
2605 DCHECK(in.GetConstant()->IsLongConstant());
2606 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002607 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002608 }
2609 break;
2610
Roland Levillain3f8f9362014-12-02 17:45:01 +00002611 case Primitive::kPrimFloat: {
2612 // Processing a Dex `float-to-int' instruction.
2613 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2614 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002615 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002616
2617 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002618 // if input >= (float)INT_MAX goto done
2619 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002620 __ j(kAboveEqual, &done);
2621 // if input == NaN goto nan
2622 __ j(kUnordered, &nan);
2623 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002624 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002625 __ jmp(&done);
2626 __ Bind(&nan);
2627 // output = 0
2628 __ xorl(output, output);
2629 __ Bind(&done);
2630 break;
2631 }
2632
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002633 case Primitive::kPrimDouble: {
2634 // Processing a Dex `double-to-int' instruction.
2635 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2636 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002637 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002638
2639 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002640 // if input >= (double)INT_MAX goto done
2641 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002642 __ j(kAboveEqual, &done);
2643 // if input == NaN goto nan
2644 __ j(kUnordered, &nan);
2645 // output = double-to-int-truncate(input)
2646 __ cvttsd2si(output, input);
2647 __ jmp(&done);
2648 __ Bind(&nan);
2649 // output = 0
2650 __ xorl(output, output);
2651 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002652 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002653 }
Roland Levillain946e1432014-11-11 17:35:19 +00002654
2655 default:
2656 LOG(FATAL) << "Unexpected type conversion from " << input_type
2657 << " to " << result_type;
2658 }
2659 break;
2660
Roland Levillaindff1f282014-11-05 14:15:05 +00002661 case Primitive::kPrimLong:
2662 switch (input_type) {
2663 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002664 case Primitive::kPrimBoolean:
2665 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002666 case Primitive::kPrimByte:
2667 case Primitive::kPrimShort:
2668 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002669 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002670 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002671 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002672 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002673 break;
2674
Roland Levillain624279f2014-12-04 11:54:28 +00002675 case Primitive::kPrimFloat: {
2676 // Processing a Dex `float-to-long' instruction.
2677 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2678 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002679 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002680
Mark Mendell92e83bf2015-05-07 11:25:03 -04002681 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002682 // if input >= (float)LONG_MAX goto done
2683 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002684 __ j(kAboveEqual, &done);
2685 // if input == NaN goto nan
2686 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002687 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002688 __ cvttss2si(output, input, true);
2689 __ jmp(&done);
2690 __ Bind(&nan);
2691 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002692 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002693 __ Bind(&done);
2694 break;
2695 }
2696
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002697 case Primitive::kPrimDouble: {
2698 // Processing a Dex `double-to-long' instruction.
2699 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2700 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002701 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002702
Mark Mendell92e83bf2015-05-07 11:25:03 -04002703 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002704 // if input >= (double)LONG_MAX goto done
2705 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002706 __ j(kAboveEqual, &done);
2707 // if input == NaN goto nan
2708 __ j(kUnordered, &nan);
2709 // output = double-to-long-truncate(input)
2710 __ cvttsd2si(output, input, true);
2711 __ jmp(&done);
2712 __ Bind(&nan);
2713 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002714 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002715 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002716 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002717 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002718
2719 default:
2720 LOG(FATAL) << "Unexpected type conversion from " << input_type
2721 << " to " << result_type;
2722 }
2723 break;
2724
Roland Levillain981e4542014-11-14 11:47:14 +00002725 case Primitive::kPrimChar:
2726 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002727 case Primitive::kPrimBoolean:
2728 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002729 case Primitive::kPrimByte:
2730 case Primitive::kPrimShort:
2731 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002732 // Processing a Dex `int-to-char' instruction.
2733 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002735 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002736 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002737 Address(CpuRegister(RSP), in.GetStackIndex()));
2738 } else {
2739 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002740 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002741 Immediate(static_cast<uint16_t>(
2742 in.GetConstant()->AsIntConstant()->GetValue())));
Roland Levillain981e4542014-11-14 11:47:14 +00002743 }
2744 break;
2745
2746 default:
2747 LOG(FATAL) << "Unexpected type conversion from " << input_type
2748 << " to " << result_type;
2749 }
2750 break;
2751
Roland Levillaindff1f282014-11-05 14:15:05 +00002752 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002753 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002754 case Primitive::kPrimBoolean:
2755 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002756 case Primitive::kPrimByte:
2757 case Primitive::kPrimShort:
2758 case Primitive::kPrimInt:
2759 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002760 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002761 if (in.IsRegister()) {
2762 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2763 } else if (in.IsConstant()) {
2764 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2765 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2766 if (v == 0) {
2767 __ xorps(dest, dest);
2768 } else {
2769 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2770 }
2771 } else {
2772 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2773 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2774 }
Roland Levillaincff13742014-11-17 14:32:17 +00002775 break;
2776
2777 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002778 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002779 if (in.IsRegister()) {
2780 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2781 } else if (in.IsConstant()) {
2782 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2783 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2784 if (v == 0) {
2785 __ xorps(dest, dest);
2786 } else {
2787 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2788 }
2789 } else {
2790 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2791 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2792 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002793 break;
2794
Roland Levillaincff13742014-11-17 14:32:17 +00002795 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002796 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002797 if (in.IsFpuRegister()) {
2798 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2799 } else if (in.IsConstant()) {
2800 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2801 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2802 if (bit_cast<int64_t, double>(v) == 0) {
2803 __ xorps(dest, dest);
2804 } else {
2805 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2806 }
2807 } else {
2808 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2809 Address(CpuRegister(RSP), in.GetStackIndex()));
2810 }
Roland Levillaincff13742014-11-17 14:32:17 +00002811 break;
2812
2813 default:
2814 LOG(FATAL) << "Unexpected type conversion from " << input_type
2815 << " to " << result_type;
2816 };
2817 break;
2818
Roland Levillaindff1f282014-11-05 14:15:05 +00002819 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002820 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002821 case Primitive::kPrimBoolean:
2822 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002823 case Primitive::kPrimByte:
2824 case Primitive::kPrimShort:
2825 case Primitive::kPrimInt:
2826 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002827 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002828 if (in.IsRegister()) {
2829 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2830 } else if (in.IsConstant()) {
2831 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2832 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2833 if (v == 0) {
2834 __ xorpd(dest, dest);
2835 } else {
2836 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2837 }
2838 } else {
2839 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2840 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2841 }
Roland Levillaincff13742014-11-17 14:32:17 +00002842 break;
2843
2844 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002845 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002846 if (in.IsRegister()) {
2847 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2848 } else if (in.IsConstant()) {
2849 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2850 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2851 if (v == 0) {
2852 __ xorpd(dest, dest);
2853 } else {
2854 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2855 }
2856 } else {
2857 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2858 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2859 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002860 break;
2861
Roland Levillaincff13742014-11-17 14:32:17 +00002862 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002863 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002864 if (in.IsFpuRegister()) {
2865 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2866 } else if (in.IsConstant()) {
2867 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2868 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2869 if (bit_cast<int32_t, float>(v) == 0) {
2870 __ xorpd(dest, dest);
2871 } else {
2872 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2873 }
2874 } else {
2875 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2876 Address(CpuRegister(RSP), in.GetStackIndex()));
2877 }
Roland Levillaincff13742014-11-17 14:32:17 +00002878 break;
2879
2880 default:
2881 LOG(FATAL) << "Unexpected type conversion from " << input_type
2882 << " to " << result_type;
2883 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002884 break;
2885
2886 default:
2887 LOG(FATAL) << "Unexpected type conversion from " << input_type
2888 << " to " << result_type;
2889 }
2890}
2891
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002892void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002893 LocationSummary* locations =
2894 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002895 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002896 case Primitive::kPrimInt: {
2897 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002898 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2899 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002900 break;
2901 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002902
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002903 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002904 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002905 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002906 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002907 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002908 break;
2909 }
2910
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002911 case Primitive::kPrimDouble:
2912 case Primitive::kPrimFloat: {
2913 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002914 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002915 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002916 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002917 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002918
2919 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002920 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002921 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002922}
2923
2924void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2925 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002926 Location first = locations->InAt(0);
2927 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002928 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002929
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002930 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002931 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002932 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002933 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2934 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002935 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2936 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002937 } else {
2938 __ leal(out.AsRegister<CpuRegister>(), Address(
2939 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2940 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002941 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002942 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2943 __ addl(out.AsRegister<CpuRegister>(),
2944 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2945 } else {
2946 __ leal(out.AsRegister<CpuRegister>(), Address(
2947 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2948 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002949 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002950 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002951 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002952 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002953 break;
2954 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002955
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002956 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002957 if (second.IsRegister()) {
2958 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2959 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002960 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2961 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002962 } else {
2963 __ leaq(out.AsRegister<CpuRegister>(), Address(
2964 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2965 }
2966 } else {
2967 DCHECK(second.IsConstant());
2968 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2969 int32_t int32_value = Low32Bits(value);
2970 DCHECK_EQ(int32_value, value);
2971 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2972 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2973 } else {
2974 __ leaq(out.AsRegister<CpuRegister>(), Address(
2975 first.AsRegister<CpuRegister>(), int32_value));
2976 }
2977 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002978 break;
2979 }
2980
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002981 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002982 if (second.IsFpuRegister()) {
2983 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2984 } else if (second.IsConstant()) {
2985 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002986 codegen_->LiteralFloatAddress(
2987 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002988 } else {
2989 DCHECK(second.IsStackSlot());
2990 __ addss(first.AsFpuRegister<XmmRegister>(),
2991 Address(CpuRegister(RSP), second.GetStackIndex()));
2992 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002993 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002994 }
2995
2996 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002997 if (second.IsFpuRegister()) {
2998 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2999 } else if (second.IsConstant()) {
3000 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003001 codegen_->LiteralDoubleAddress(
3002 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003003 } else {
3004 DCHECK(second.IsDoubleStackSlot());
3005 __ addsd(first.AsFpuRegister<XmmRegister>(),
3006 Address(CpuRegister(RSP), second.GetStackIndex()));
3007 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003008 break;
3009 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003010
3011 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003012 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003013 }
3014}
3015
3016void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003017 LocationSummary* locations =
3018 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003019 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003020 case Primitive::kPrimInt: {
3021 locations->SetInAt(0, Location::RequiresRegister());
3022 locations->SetInAt(1, Location::Any());
3023 locations->SetOut(Location::SameAsFirstInput());
3024 break;
3025 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003026 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003027 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003028 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003029 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003030 break;
3031 }
Calin Juravle11351682014-10-23 15:38:15 +01003032 case Primitive::kPrimFloat:
3033 case Primitive::kPrimDouble: {
3034 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003035 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003036 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003037 break;
Calin Juravle11351682014-10-23 15:38:15 +01003038 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003039 default:
Calin Juravle11351682014-10-23 15:38:15 +01003040 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003041 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003042}
3043
3044void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3045 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003046 Location first = locations->InAt(0);
3047 Location second = locations->InAt(1);
3048 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003049 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003050 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003051 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003052 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003053 } else if (second.IsConstant()) {
3054 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003055 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003056 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003058 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003059 break;
3060 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003061 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003062 if (second.IsConstant()) {
3063 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3064 DCHECK(IsInt<32>(value));
3065 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3066 } else {
3067 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3068 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003069 break;
3070 }
3071
Calin Juravle11351682014-10-23 15:38:15 +01003072 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003073 if (second.IsFpuRegister()) {
3074 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3075 } else if (second.IsConstant()) {
3076 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003077 codegen_->LiteralFloatAddress(
3078 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003079 } else {
3080 DCHECK(second.IsStackSlot());
3081 __ subss(first.AsFpuRegister<XmmRegister>(),
3082 Address(CpuRegister(RSP), second.GetStackIndex()));
3083 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003084 break;
Calin Juravle11351682014-10-23 15:38:15 +01003085 }
3086
3087 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003088 if (second.IsFpuRegister()) {
3089 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3090 } else if (second.IsConstant()) {
3091 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003092 codegen_->LiteralDoubleAddress(
3093 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003094 } else {
3095 DCHECK(second.IsDoubleStackSlot());
3096 __ subsd(first.AsFpuRegister<XmmRegister>(),
3097 Address(CpuRegister(RSP), second.GetStackIndex()));
3098 }
Calin Juravle11351682014-10-23 15:38:15 +01003099 break;
3100 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003101
3102 default:
Calin Juravle11351682014-10-23 15:38:15 +01003103 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003104 }
3105}
3106
Calin Juravle34bacdf2014-10-07 20:23:36 +01003107void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3108 LocationSummary* locations =
3109 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3110 switch (mul->GetResultType()) {
3111 case Primitive::kPrimInt: {
3112 locations->SetInAt(0, Location::RequiresRegister());
3113 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003114 if (mul->InputAt(1)->IsIntConstant()) {
3115 // Can use 3 operand multiply.
3116 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3117 } else {
3118 locations->SetOut(Location::SameAsFirstInput());
3119 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003120 break;
3121 }
3122 case Primitive::kPrimLong: {
3123 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003124 locations->SetInAt(1, Location::Any());
3125 if (mul->InputAt(1)->IsLongConstant() &&
3126 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003127 // Can use 3 operand multiply.
3128 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3129 } else {
3130 locations->SetOut(Location::SameAsFirstInput());
3131 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003132 break;
3133 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003134 case Primitive::kPrimFloat:
3135 case Primitive::kPrimDouble: {
3136 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003137 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003138 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003139 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003140 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003141
3142 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003143 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003144 }
3145}
3146
3147void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3148 LocationSummary* locations = mul->GetLocations();
3149 Location first = locations->InAt(0);
3150 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003151 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003152 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003153 case Primitive::kPrimInt:
3154 // The constant may have ended up in a register, so test explicitly to avoid
3155 // problems where the output may not be the same as the first operand.
3156 if (mul->InputAt(1)->IsIntConstant()) {
3157 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3158 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3159 } else if (second.IsRegister()) {
3160 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003161 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003162 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003163 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003164 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003165 __ imull(first.AsRegister<CpuRegister>(),
3166 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003167 }
3168 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003169 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003170 // The constant may have ended up in a register, so test explicitly to avoid
3171 // problems where the output may not be the same as the first operand.
3172 if (mul->InputAt(1)->IsLongConstant()) {
3173 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3174 if (IsInt<32>(value)) {
3175 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3176 Immediate(static_cast<int32_t>(value)));
3177 } else {
3178 // Have to use the constant area.
3179 DCHECK(first.Equals(out));
3180 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3181 }
3182 } else if (second.IsRegister()) {
3183 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003184 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003185 } else {
3186 DCHECK(second.IsDoubleStackSlot());
3187 DCHECK(first.Equals(out));
3188 __ imulq(first.AsRegister<CpuRegister>(),
3189 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003190 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191 break;
3192 }
3193
Calin Juravleb5bfa962014-10-21 18:02:24 +01003194 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003195 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003196 if (second.IsFpuRegister()) {
3197 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3198 } else if (second.IsConstant()) {
3199 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003200 codegen_->LiteralFloatAddress(
3201 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003202 } else {
3203 DCHECK(second.IsStackSlot());
3204 __ mulss(first.AsFpuRegister<XmmRegister>(),
3205 Address(CpuRegister(RSP), second.GetStackIndex()));
3206 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003208 }
3209
3210 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003211 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003212 if (second.IsFpuRegister()) {
3213 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3214 } else if (second.IsConstant()) {
3215 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003216 codegen_->LiteralDoubleAddress(
3217 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003218 } else {
3219 DCHECK(second.IsDoubleStackSlot());
3220 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3221 Address(CpuRegister(RSP), second.GetStackIndex()));
3222 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003223 break;
3224 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225
3226 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003227 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003228 }
3229}
3230
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003231void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3232 uint32_t stack_adjustment, bool is_float) {
3233 if (source.IsStackSlot()) {
3234 DCHECK(is_float);
3235 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3236 } else if (source.IsDoubleStackSlot()) {
3237 DCHECK(!is_float);
3238 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3239 } else {
3240 // Write the value to the temporary location on the stack and load to FP stack.
3241 if (is_float) {
3242 Location stack_temp = Location::StackSlot(temp_offset);
3243 codegen_->Move(stack_temp, source);
3244 __ flds(Address(CpuRegister(RSP), temp_offset));
3245 } else {
3246 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3247 codegen_->Move(stack_temp, source);
3248 __ fldl(Address(CpuRegister(RSP), temp_offset));
3249 }
3250 }
3251}
3252
3253void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3254 Primitive::Type type = rem->GetResultType();
3255 bool is_float = type == Primitive::kPrimFloat;
3256 size_t elem_size = Primitive::ComponentSize(type);
3257 LocationSummary* locations = rem->GetLocations();
3258 Location first = locations->InAt(0);
3259 Location second = locations->InAt(1);
3260 Location out = locations->Out();
3261
3262 // Create stack space for 2 elements.
3263 // TODO: enhance register allocator to ask for stack temporaries.
3264 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3265
3266 // Load the values to the FP stack in reverse order, using temporaries if needed.
3267 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3268 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3269
3270 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003271 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003272 __ Bind(&retry);
3273 __ fprem();
3274
3275 // Move FP status to AX.
3276 __ fstsw();
3277
3278 // And see if the argument reduction is complete. This is signaled by the
3279 // C2 FPU flag bit set to 0.
3280 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3281 __ j(kNotEqual, &retry);
3282
3283 // We have settled on the final value. Retrieve it into an XMM register.
3284 // Store FP top of stack to real stack.
3285 if (is_float) {
3286 __ fsts(Address(CpuRegister(RSP), 0));
3287 } else {
3288 __ fstl(Address(CpuRegister(RSP), 0));
3289 }
3290
3291 // Pop the 2 items from the FP stack.
3292 __ fucompp();
3293
3294 // Load the value from the stack into an XMM register.
3295 DCHECK(out.IsFpuRegister()) << out;
3296 if (is_float) {
3297 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3298 } else {
3299 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3300 }
3301
3302 // And remove the temporary stack space we allocated.
3303 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3304}
3305
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003306void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3307 DCHECK(instruction->IsDiv() || instruction->IsRem());
3308
3309 LocationSummary* locations = instruction->GetLocations();
3310 Location second = locations->InAt(1);
3311 DCHECK(second.IsConstant());
3312
3313 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3314 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003315 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003316
3317 DCHECK(imm == 1 || imm == -1);
3318
3319 switch (instruction->GetResultType()) {
3320 case Primitive::kPrimInt: {
3321 if (instruction->IsRem()) {
3322 __ xorl(output_register, output_register);
3323 } else {
3324 __ movl(output_register, input_register);
3325 if (imm == -1) {
3326 __ negl(output_register);
3327 }
3328 }
3329 break;
3330 }
3331
3332 case Primitive::kPrimLong: {
3333 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003334 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003335 } else {
3336 __ movq(output_register, input_register);
3337 if (imm == -1) {
3338 __ negq(output_register);
3339 }
3340 }
3341 break;
3342 }
3343
3344 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003345 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003346 }
3347}
3348
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003349void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003350 LocationSummary* locations = instruction->GetLocations();
3351 Location second = locations->InAt(1);
3352
3353 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3354 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3355
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003356 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003357
3358 DCHECK(IsPowerOfTwo(std::abs(imm)));
3359
3360 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3361
3362 if (instruction->GetResultType() == Primitive::kPrimInt) {
3363 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
3364 __ testl(numerator, numerator);
3365 __ cmov(kGreaterEqual, tmp, numerator);
3366 int shift = CTZ(imm);
3367 __ sarl(tmp, Immediate(shift));
3368
3369 if (imm < 0) {
3370 __ negl(tmp);
3371 }
3372
3373 __ movl(output_register, tmp);
3374 } else {
3375 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3376 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3377
Mark Mendell92e83bf2015-05-07 11:25:03 -04003378 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003379 __ addq(rdx, numerator);
3380 __ testq(numerator, numerator);
3381 __ cmov(kGreaterEqual, rdx, numerator);
3382 int shift = CTZ(imm);
3383 __ sarq(rdx, Immediate(shift));
3384
3385 if (imm < 0) {
3386 __ negq(rdx);
3387 }
3388
3389 __ movq(output_register, rdx);
3390 }
3391}
3392
3393void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3394 DCHECK(instruction->IsDiv() || instruction->IsRem());
3395
3396 LocationSummary* locations = instruction->GetLocations();
3397 Location second = locations->InAt(1);
3398
3399 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3400 : locations->GetTemp(0).AsRegister<CpuRegister>();
3401 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3402 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3403 : locations->Out().AsRegister<CpuRegister>();
3404 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3405
3406 DCHECK_EQ(RAX, eax.AsRegister());
3407 DCHECK_EQ(RDX, edx.AsRegister());
3408 if (instruction->IsDiv()) {
3409 DCHECK_EQ(RAX, out.AsRegister());
3410 } else {
3411 DCHECK_EQ(RDX, out.AsRegister());
3412 }
3413
3414 int64_t magic;
3415 int shift;
3416
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003417 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003418 if (instruction->GetResultType() == Primitive::kPrimInt) {
3419 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3420
3421 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3422
3423 __ movl(numerator, eax);
3424
Mark Mendell0c9497d2015-08-21 09:30:05 -04003425 NearLabel no_div;
3426 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427 __ testl(eax, eax);
3428 __ j(kNotEqual, &no_div);
3429
3430 __ xorl(out, out);
3431 __ jmp(&end);
3432
3433 __ Bind(&no_div);
3434
3435 __ movl(eax, Immediate(magic));
3436 __ imull(numerator);
3437
3438 if (imm > 0 && magic < 0) {
3439 __ addl(edx, numerator);
3440 } else if (imm < 0 && magic > 0) {
3441 __ subl(edx, numerator);
3442 }
3443
3444 if (shift != 0) {
3445 __ sarl(edx, Immediate(shift));
3446 }
3447
3448 __ movl(eax, edx);
3449 __ shrl(edx, Immediate(31));
3450 __ addl(edx, eax);
3451
3452 if (instruction->IsRem()) {
3453 __ movl(eax, numerator);
3454 __ imull(edx, Immediate(imm));
3455 __ subl(eax, edx);
3456 __ movl(edx, eax);
3457 } else {
3458 __ movl(eax, edx);
3459 }
3460 __ Bind(&end);
3461 } else {
3462 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3463
3464 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3465
3466 CpuRegister rax = eax;
3467 CpuRegister rdx = edx;
3468
3469 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3470
3471 // Save the numerator.
3472 __ movq(numerator, rax);
3473
3474 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003475 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003476
3477 // RDX:RAX = magic * numerator
3478 __ imulq(numerator);
3479
3480 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003481 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003482 __ addq(rdx, numerator);
3483 } else if (imm < 0 && magic > 0) {
3484 // RDX -= numerator
3485 __ subq(rdx, numerator);
3486 }
3487
3488 // Shift if needed.
3489 if (shift != 0) {
3490 __ sarq(rdx, Immediate(shift));
3491 }
3492
3493 // RDX += 1 if RDX < 0
3494 __ movq(rax, rdx);
3495 __ shrq(rdx, Immediate(63));
3496 __ addq(rdx, rax);
3497
3498 if (instruction->IsRem()) {
3499 __ movq(rax, numerator);
3500
3501 if (IsInt<32>(imm)) {
3502 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3503 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003504 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003505 }
3506
3507 __ subq(rax, rdx);
3508 __ movq(rdx, rax);
3509 } else {
3510 __ movq(rax, rdx);
3511 }
3512 }
3513}
3514
Calin Juravlebacfec32014-11-14 15:54:36 +00003515void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3516 DCHECK(instruction->IsDiv() || instruction->IsRem());
3517 Primitive::Type type = instruction->GetResultType();
3518 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3519
3520 bool is_div = instruction->IsDiv();
3521 LocationSummary* locations = instruction->GetLocations();
3522
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003523 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3524 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003525
Roland Levillain271ab9c2014-11-27 15:23:57 +00003526 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003527 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003528
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003529 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003530 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003531
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003532 if (imm == 0) {
3533 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3534 } else if (imm == 1 || imm == -1) {
3535 DivRemOneOrMinusOne(instruction);
3536 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003537 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003538 } else {
3539 DCHECK(imm <= -2 || imm >= 2);
3540 GenerateDivRemWithAnyConstant(instruction);
3541 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003542 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003543 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3545 out.AsRegister(), type, is_div);
3546 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003547
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003548 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3549 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3550 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3551 // so it's safe to just use negl instead of more complex comparisons.
3552 if (type == Primitive::kPrimInt) {
3553 __ cmpl(second_reg, Immediate(-1));
3554 __ j(kEqual, slow_path->GetEntryLabel());
3555 // edx:eax <- sign-extended of eax
3556 __ cdq();
3557 // eax = quotient, edx = remainder
3558 __ idivl(second_reg);
3559 } else {
3560 __ cmpq(second_reg, Immediate(-1));
3561 __ j(kEqual, slow_path->GetEntryLabel());
3562 // rdx:rax <- sign-extended of rax
3563 __ cqo();
3564 // rax = quotient, rdx = remainder
3565 __ idivq(second_reg);
3566 }
3567 __ Bind(slow_path->GetExitLabel());
3568 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003569}
3570
Calin Juravle7c4954d2014-10-28 16:57:40 +00003571void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3572 LocationSummary* locations =
3573 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3574 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003575 case Primitive::kPrimInt:
3576 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003577 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003578 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003579 locations->SetOut(Location::SameAsFirstInput());
3580 // Intel uses edx:eax as the dividend.
3581 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003582 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3583 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3584 // output and request another temp.
3585 if (div->InputAt(1)->IsConstant()) {
3586 locations->AddTemp(Location::RequiresRegister());
3587 }
Calin Juravled0d48522014-11-04 16:40:20 +00003588 break;
3589 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003590
Calin Juravle7c4954d2014-10-28 16:57:40 +00003591 case Primitive::kPrimFloat:
3592 case Primitive::kPrimDouble: {
3593 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003594 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003595 locations->SetOut(Location::SameAsFirstInput());
3596 break;
3597 }
3598
3599 default:
3600 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3601 }
3602}
3603
3604void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3605 LocationSummary* locations = div->GetLocations();
3606 Location first = locations->InAt(0);
3607 Location second = locations->InAt(1);
3608 DCHECK(first.Equals(locations->Out()));
3609
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003610 Primitive::Type type = div->GetResultType();
3611 switch (type) {
3612 case Primitive::kPrimInt:
3613 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003614 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003615 break;
3616 }
3617
Calin Juravle7c4954d2014-10-28 16:57:40 +00003618 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003619 if (second.IsFpuRegister()) {
3620 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3621 } else if (second.IsConstant()) {
3622 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003623 codegen_->LiteralFloatAddress(
3624 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003625 } else {
3626 DCHECK(second.IsStackSlot());
3627 __ divss(first.AsFpuRegister<XmmRegister>(),
3628 Address(CpuRegister(RSP), second.GetStackIndex()));
3629 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003630 break;
3631 }
3632
3633 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003634 if (second.IsFpuRegister()) {
3635 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3636 } else if (second.IsConstant()) {
3637 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003638 codegen_->LiteralDoubleAddress(
3639 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003640 } else {
3641 DCHECK(second.IsDoubleStackSlot());
3642 __ divsd(first.AsFpuRegister<XmmRegister>(),
3643 Address(CpuRegister(RSP), second.GetStackIndex()));
3644 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003645 break;
3646 }
3647
3648 default:
3649 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3650 }
3651}
3652
Calin Juravlebacfec32014-11-14 15:54:36 +00003653void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003654 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003655 LocationSummary* locations =
3656 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003657
3658 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003659 case Primitive::kPrimInt:
3660 case Primitive::kPrimLong: {
3661 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003662 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003663 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3664 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003665 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3666 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3667 // output and request another temp.
3668 if (rem->InputAt(1)->IsConstant()) {
3669 locations->AddTemp(Location::RequiresRegister());
3670 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003671 break;
3672 }
3673
3674 case Primitive::kPrimFloat:
3675 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003676 locations->SetInAt(0, Location::Any());
3677 locations->SetInAt(1, Location::Any());
3678 locations->SetOut(Location::RequiresFpuRegister());
3679 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003680 break;
3681 }
3682
3683 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003684 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003685 }
3686}
3687
3688void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3689 Primitive::Type type = rem->GetResultType();
3690 switch (type) {
3691 case Primitive::kPrimInt:
3692 case Primitive::kPrimLong: {
3693 GenerateDivRemIntegral(rem);
3694 break;
3695 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003696 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003697 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003698 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003699 break;
3700 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003701 default:
3702 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3703 }
3704}
3705
Calin Juravled0d48522014-11-04 16:40:20 +00003706void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003707 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3708 ? LocationSummary::kCallOnSlowPath
3709 : LocationSummary::kNoCall;
3710 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003711 locations->SetInAt(0, Location::Any());
3712 if (instruction->HasUses()) {
3713 locations->SetOut(Location::SameAsFirstInput());
3714 }
3715}
3716
3717void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003718 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003719 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3720 codegen_->AddSlowPath(slow_path);
3721
3722 LocationSummary* locations = instruction->GetLocations();
3723 Location value = locations->InAt(0);
3724
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003725 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003726 case Primitive::kPrimByte:
3727 case Primitive::kPrimChar:
3728 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003729 case Primitive::kPrimInt: {
3730 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003731 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003732 __ j(kEqual, slow_path->GetEntryLabel());
3733 } else if (value.IsStackSlot()) {
3734 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3735 __ j(kEqual, slow_path->GetEntryLabel());
3736 } else {
3737 DCHECK(value.IsConstant()) << value;
3738 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3739 __ jmp(slow_path->GetEntryLabel());
3740 }
3741 }
3742 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003743 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003744 case Primitive::kPrimLong: {
3745 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003746 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003747 __ j(kEqual, slow_path->GetEntryLabel());
3748 } else if (value.IsDoubleStackSlot()) {
3749 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3750 __ j(kEqual, slow_path->GetEntryLabel());
3751 } else {
3752 DCHECK(value.IsConstant()) << value;
3753 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3754 __ jmp(slow_path->GetEntryLabel());
3755 }
3756 }
3757 break;
3758 }
3759 default:
3760 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003761 }
Calin Juravled0d48522014-11-04 16:40:20 +00003762}
3763
Calin Juravle9aec02f2014-11-18 23:06:35 +00003764void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3765 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3766
3767 LocationSummary* locations =
3768 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3769
3770 switch (op->GetResultType()) {
3771 case Primitive::kPrimInt:
3772 case Primitive::kPrimLong: {
3773 locations->SetInAt(0, Location::RequiresRegister());
3774 // The shift count needs to be in CL.
3775 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3776 locations->SetOut(Location::SameAsFirstInput());
3777 break;
3778 }
3779 default:
3780 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3781 }
3782}
3783
3784void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3785 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3786
3787 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003788 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003789 Location second = locations->InAt(1);
3790
3791 switch (op->GetResultType()) {
3792 case Primitive::kPrimInt: {
3793 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003794 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003795 if (op->IsShl()) {
3796 __ shll(first_reg, second_reg);
3797 } else if (op->IsShr()) {
3798 __ sarl(first_reg, second_reg);
3799 } else {
3800 __ shrl(first_reg, second_reg);
3801 }
3802 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003803 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003804 if (op->IsShl()) {
3805 __ shll(first_reg, imm);
3806 } else if (op->IsShr()) {
3807 __ sarl(first_reg, imm);
3808 } else {
3809 __ shrl(first_reg, imm);
3810 }
3811 }
3812 break;
3813 }
3814 case Primitive::kPrimLong: {
3815 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003816 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003817 if (op->IsShl()) {
3818 __ shlq(first_reg, second_reg);
3819 } else if (op->IsShr()) {
3820 __ sarq(first_reg, second_reg);
3821 } else {
3822 __ shrq(first_reg, second_reg);
3823 }
3824 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003825 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003826 if (op->IsShl()) {
3827 __ shlq(first_reg, imm);
3828 } else if (op->IsShr()) {
3829 __ sarq(first_reg, imm);
3830 } else {
3831 __ shrq(first_reg, imm);
3832 }
3833 }
3834 break;
3835 }
3836 default:
3837 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003838 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003839 }
3840}
3841
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003842void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3843 LocationSummary* locations =
3844 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3845
3846 switch (ror->GetResultType()) {
3847 case Primitive::kPrimInt:
3848 case Primitive::kPrimLong: {
3849 locations->SetInAt(0, Location::RequiresRegister());
3850 // The shift count needs to be in CL (unless it is a constant).
3851 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3852 locations->SetOut(Location::SameAsFirstInput());
3853 break;
3854 }
3855 default:
3856 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3857 UNREACHABLE();
3858 }
3859}
3860
3861void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3862 LocationSummary* locations = ror->GetLocations();
3863 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3864 Location second = locations->InAt(1);
3865
3866 switch (ror->GetResultType()) {
3867 case Primitive::kPrimInt:
3868 if (second.IsRegister()) {
3869 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3870 __ rorl(first_reg, second_reg);
3871 } else {
3872 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3873 __ rorl(first_reg, imm);
3874 }
3875 break;
3876 case Primitive::kPrimLong:
3877 if (second.IsRegister()) {
3878 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3879 __ rorq(first_reg, second_reg);
3880 } else {
3881 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
3882 __ rorq(first_reg, imm);
3883 }
3884 break;
3885 default:
3886 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3887 UNREACHABLE();
3888 }
3889}
3890
Calin Juravle9aec02f2014-11-18 23:06:35 +00003891void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3892 HandleShift(shl);
3893}
3894
3895void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3896 HandleShift(shl);
3897}
3898
3899void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3900 HandleShift(shr);
3901}
3902
3903void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3904 HandleShift(shr);
3905}
3906
3907void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3908 HandleShift(ushr);
3909}
3910
3911void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3912 HandleShift(ushr);
3913}
3914
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003915void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003916 LocationSummary* locations =
3917 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003918 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003919 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3920 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003921 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003922}
3923
3924void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003925 // Note: if heap poisoning is enabled, the entry point takes cares
3926 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003927 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3928 instruction,
3929 instruction->GetDexPc(),
3930 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003931 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003932
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003933 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003934}
3935
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003936void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3937 LocationSummary* locations =
3938 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3939 InvokeRuntimeCallingConvention calling_convention;
3940 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003941 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003942 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003943 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003944}
3945
3946void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3947 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003948 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3949 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003950 // Note: if heap poisoning is enabled, the entry point takes cares
3951 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003952 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3953 instruction,
3954 instruction->GetDexPc(),
3955 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003956 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003957
3958 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003959}
3960
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003961void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003962 LocationSummary* locations =
3963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003964 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3965 if (location.IsStackSlot()) {
3966 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3967 } else if (location.IsDoubleStackSlot()) {
3968 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3969 }
3970 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003971}
3972
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003973void InstructionCodeGeneratorX86_64::VisitParameterValue(
3974 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003975 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003976}
3977
3978void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3979 LocationSummary* locations =
3980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3981 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3982}
3983
3984void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3985 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3986 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003987}
3988
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003989void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003990 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003991 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003992 locations->SetInAt(0, Location::RequiresRegister());
3993 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003994}
3995
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003996void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3997 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003998 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3999 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004000 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004001 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004002 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004003 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004004 break;
4005
4006 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004007 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004008 break;
4009
4010 default:
4011 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4012 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004013}
4014
David Brazdil66d126e2015-04-03 16:02:44 +01004015void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4016 LocationSummary* locations =
4017 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4018 locations->SetInAt(0, Location::RequiresRegister());
4019 locations->SetOut(Location::SameAsFirstInput());
4020}
4021
4022void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004023 LocationSummary* locations = bool_not->GetLocations();
4024 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4025 locations->Out().AsRegister<CpuRegister>().AsRegister());
4026 Location out = locations->Out();
4027 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4028}
4029
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004030void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004031 LocationSummary* locations =
4032 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004033 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4034 locations->SetInAt(i, Location::Any());
4035 }
4036 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004037}
4038
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004039void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004040 LOG(FATAL) << "Unimplemented";
4041}
4042
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004043void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004044 /*
4045 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004046 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004047 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4048 */
4049 switch (kind) {
4050 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004051 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004052 break;
4053 }
4054 case MemBarrierKind::kAnyStore:
4055 case MemBarrierKind::kLoadAny:
4056 case MemBarrierKind::kStoreStore: {
4057 // nop
4058 break;
4059 }
4060 default:
4061 LOG(FATAL) << "Unexpected memory barier " << kind;
4062 }
4063}
4064
4065void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4066 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4067
Roland Levillain0d5a2812015-11-13 10:07:31 +00004068 bool object_field_get_with_read_barrier =
4069 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004070 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004071 new (GetGraph()->GetArena()) LocationSummary(instruction,
4072 object_field_get_with_read_barrier ?
4073 LocationSummary::kCallOnSlowPath :
4074 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004075 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004076 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4077 locations->SetOut(Location::RequiresFpuRegister());
4078 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004079 // The output overlaps for an object field get when read barriers
4080 // are enabled: we do not want the move to overwrite the object's
4081 // location, as we need it to emit the read barrier.
4082 locations->SetOut(
4083 Location::RequiresRegister(),
4084 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004085 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004086 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4087 // We need a temporary register for the read barrier marking slow
4088 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4089 locations->AddTemp(Location::RequiresRegister());
4090 }
Calin Juravle52c48962014-12-16 17:02:57 +00004091}
4092
4093void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4094 const FieldInfo& field_info) {
4095 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4096
4097 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004098 Location base_loc = locations->InAt(0);
4099 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004100 Location out = locations->Out();
4101 bool is_volatile = field_info.IsVolatile();
4102 Primitive::Type field_type = field_info.GetFieldType();
4103 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4104
4105 switch (field_type) {
4106 case Primitive::kPrimBoolean: {
4107 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4108 break;
4109 }
4110
4111 case Primitive::kPrimByte: {
4112 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4113 break;
4114 }
4115
4116 case Primitive::kPrimShort: {
4117 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4118 break;
4119 }
4120
4121 case Primitive::kPrimChar: {
4122 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4123 break;
4124 }
4125
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004126 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004127 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4128 break;
4129 }
4130
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004131 case Primitive::kPrimNot: {
4132 // /* HeapReference<Object> */ out = *(base + offset)
4133 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4134 Location temp_loc = locations->GetTemp(0);
4135 // Note that a potential implicit null check is handled in this
4136 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4137 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4138 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4139 if (is_volatile) {
4140 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4141 }
4142 } else {
4143 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4144 codegen_->MaybeRecordImplicitNullCheck(instruction);
4145 if (is_volatile) {
4146 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4147 }
4148 // If read barriers are enabled, emit read barriers other than
4149 // Baker's using a slow path (and also unpoison the loaded
4150 // reference, if heap poisoning is enabled).
4151 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4152 }
4153 break;
4154 }
4155
Calin Juravle52c48962014-12-16 17:02:57 +00004156 case Primitive::kPrimLong: {
4157 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4158 break;
4159 }
4160
4161 case Primitive::kPrimFloat: {
4162 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4163 break;
4164 }
4165
4166 case Primitive::kPrimDouble: {
4167 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4168 break;
4169 }
4170
4171 case Primitive::kPrimVoid:
4172 LOG(FATAL) << "Unreachable type " << field_type;
4173 UNREACHABLE();
4174 }
4175
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004176 if (field_type == Primitive::kPrimNot) {
4177 // Potential implicit null checks, in the case of reference
4178 // fields, are handled in the previous switch statement.
4179 } else {
4180 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004181 }
Roland Levillain4d027112015-07-01 15:41:14 +01004182
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004183 if (is_volatile) {
4184 if (field_type == Primitive::kPrimNot) {
4185 // Memory barriers, in the case of references, are also handled
4186 // in the previous switch statement.
4187 } else {
4188 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4189 }
Roland Levillain4d027112015-07-01 15:41:14 +01004190 }
Calin Juravle52c48962014-12-16 17:02:57 +00004191}
4192
4193void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4194 const FieldInfo& field_info) {
4195 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4196
4197 LocationSummary* locations =
4198 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004199 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004200 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004201 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004202 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004203
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004204 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004205 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004206 if (is_volatile) {
4207 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4208 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4209 } else {
4210 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4211 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004212 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004213 if (is_volatile) {
4214 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4215 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4216 } else {
4217 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4218 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004219 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004220 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004221 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004222 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004223 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004224 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4225 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004226 locations->AddTemp(Location::RequiresRegister());
4227 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004228}
4229
Calin Juravle52c48962014-12-16 17:02:57 +00004230void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004231 const FieldInfo& field_info,
4232 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004233 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4234
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004235 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004236 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4237 Location value = locations->InAt(1);
4238 bool is_volatile = field_info.IsVolatile();
4239 Primitive::Type field_type = field_info.GetFieldType();
4240 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4241
4242 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004243 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004244 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004245
Mark Mendellea5af682015-10-22 17:35:49 -04004246 bool maybe_record_implicit_null_check_done = false;
4247
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004248 switch (field_type) {
4249 case Primitive::kPrimBoolean:
4250 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004251 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004252 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004253 __ movb(Address(base, offset), Immediate(v));
4254 } else {
4255 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4256 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004257 break;
4258 }
4259
4260 case Primitive::kPrimShort:
4261 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004262 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004263 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004264 __ movw(Address(base, offset), Immediate(v));
4265 } else {
4266 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4267 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004268 break;
4269 }
4270
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004271 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004272 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004273 if (value.IsConstant()) {
4274 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004275 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4276 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4277 // Note: if heap poisoning is enabled, no need to poison
4278 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004279 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004280 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004281 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4282 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4283 __ movl(temp, value.AsRegister<CpuRegister>());
4284 __ PoisonHeapReference(temp);
4285 __ movl(Address(base, offset), temp);
4286 } else {
4287 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4288 }
Mark Mendell40741f32015-04-20 22:10:34 -04004289 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004290 break;
4291 }
4292
4293 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004294 if (value.IsConstant()) {
4295 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004296 codegen_->MoveInt64ToAddress(Address(base, offset),
4297 Address(base, offset + sizeof(int32_t)),
4298 v,
4299 instruction);
4300 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004301 } else {
4302 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4303 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004304 break;
4305 }
4306
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004307 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004308 if (value.IsConstant()) {
4309 int32_t v =
4310 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4311 __ movl(Address(base, offset), Immediate(v));
4312 } else {
4313 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4314 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004315 break;
4316 }
4317
4318 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004319 if (value.IsConstant()) {
4320 int64_t v =
4321 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4322 codegen_->MoveInt64ToAddress(Address(base, offset),
4323 Address(base, offset + sizeof(int32_t)),
4324 v,
4325 instruction);
4326 maybe_record_implicit_null_check_done = true;
4327 } else {
4328 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4329 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004330 break;
4331 }
4332
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004333 case Primitive::kPrimVoid:
4334 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004335 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004336 }
Calin Juravle52c48962014-12-16 17:02:57 +00004337
Mark Mendellea5af682015-10-22 17:35:49 -04004338 if (!maybe_record_implicit_null_check_done) {
4339 codegen_->MaybeRecordImplicitNullCheck(instruction);
4340 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004341
4342 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4343 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4344 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004345 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004346 }
4347
Calin Juravle52c48962014-12-16 17:02:57 +00004348 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004349 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004350 }
4351}
4352
4353void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4354 HandleFieldSet(instruction, instruction->GetFieldInfo());
4355}
4356
4357void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004358 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004359}
4360
4361void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004362 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363}
4364
4365void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004366 HandleFieldGet(instruction, instruction->GetFieldInfo());
4367}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004368
Calin Juravle52c48962014-12-16 17:02:57 +00004369void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4370 HandleFieldGet(instruction);
4371}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372
Calin Juravle52c48962014-12-16 17:02:57 +00004373void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4374 HandleFieldGet(instruction, instruction->GetFieldInfo());
4375}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004376
Calin Juravle52c48962014-12-16 17:02:57 +00004377void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4378 HandleFieldSet(instruction, instruction->GetFieldInfo());
4379}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380
Calin Juravle52c48962014-12-16 17:02:57 +00004381void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004382 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004383}
4384
Calin Juravlee460d1d2015-09-29 04:52:17 +01004385void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4386 HUnresolvedInstanceFieldGet* instruction) {
4387 FieldAccessCallingConventionX86_64 calling_convention;
4388 codegen_->CreateUnresolvedFieldLocationSummary(
4389 instruction, instruction->GetFieldType(), calling_convention);
4390}
4391
4392void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4393 HUnresolvedInstanceFieldGet* instruction) {
4394 FieldAccessCallingConventionX86_64 calling_convention;
4395 codegen_->GenerateUnresolvedFieldAccess(instruction,
4396 instruction->GetFieldType(),
4397 instruction->GetFieldIndex(),
4398 instruction->GetDexPc(),
4399 calling_convention);
4400}
4401
4402void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4403 HUnresolvedInstanceFieldSet* instruction) {
4404 FieldAccessCallingConventionX86_64 calling_convention;
4405 codegen_->CreateUnresolvedFieldLocationSummary(
4406 instruction, instruction->GetFieldType(), calling_convention);
4407}
4408
4409void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4410 HUnresolvedInstanceFieldSet* instruction) {
4411 FieldAccessCallingConventionX86_64 calling_convention;
4412 codegen_->GenerateUnresolvedFieldAccess(instruction,
4413 instruction->GetFieldType(),
4414 instruction->GetFieldIndex(),
4415 instruction->GetDexPc(),
4416 calling_convention);
4417}
4418
4419void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4420 HUnresolvedStaticFieldGet* instruction) {
4421 FieldAccessCallingConventionX86_64 calling_convention;
4422 codegen_->CreateUnresolvedFieldLocationSummary(
4423 instruction, instruction->GetFieldType(), calling_convention);
4424}
4425
4426void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4427 HUnresolvedStaticFieldGet* instruction) {
4428 FieldAccessCallingConventionX86_64 calling_convention;
4429 codegen_->GenerateUnresolvedFieldAccess(instruction,
4430 instruction->GetFieldType(),
4431 instruction->GetFieldIndex(),
4432 instruction->GetDexPc(),
4433 calling_convention);
4434}
4435
4436void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4437 HUnresolvedStaticFieldSet* instruction) {
4438 FieldAccessCallingConventionX86_64 calling_convention;
4439 codegen_->CreateUnresolvedFieldLocationSummary(
4440 instruction, instruction->GetFieldType(), calling_convention);
4441}
4442
4443void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4444 HUnresolvedStaticFieldSet* instruction) {
4445 FieldAccessCallingConventionX86_64 calling_convention;
4446 codegen_->GenerateUnresolvedFieldAccess(instruction,
4447 instruction->GetFieldType(),
4448 instruction->GetFieldIndex(),
4449 instruction->GetDexPc(),
4450 calling_convention);
4451}
4452
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004453void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004454 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4455 ? LocationSummary::kCallOnSlowPath
4456 : LocationSummary::kNoCall;
4457 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4458 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004459 ? Location::RequiresRegister()
4460 : Location::Any();
4461 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004462 if (instruction->HasUses()) {
4463 locations->SetOut(Location::SameAsFirstInput());
4464 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004465}
4466
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004467void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004468 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4469 return;
4470 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004471 LocationSummary* locations = instruction->GetLocations();
4472 Location obj = locations->InAt(0);
4473
4474 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4475 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4476}
4477
4478void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004479 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004480 codegen_->AddSlowPath(slow_path);
4481
4482 LocationSummary* locations = instruction->GetLocations();
4483 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484
4485 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004486 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004487 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004488 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004489 } else {
4490 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004491 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004492 __ jmp(slow_path->GetEntryLabel());
4493 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004494 }
4495 __ j(kEqual, slow_path->GetEntryLabel());
4496}
4497
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004498void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004499 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004500 GenerateImplicitNullCheck(instruction);
4501 } else {
4502 GenerateExplicitNullCheck(instruction);
4503 }
4504}
4505
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004506void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004507 bool object_array_get_with_read_barrier =
4508 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004509 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004510 new (GetGraph()->GetArena()) LocationSummary(instruction,
4511 object_array_get_with_read_barrier ?
4512 LocationSummary::kCallOnSlowPath :
4513 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004514 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004515 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004516 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4517 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4518 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004519 // The output overlaps for an object array get when read barriers
4520 // are enabled: we do not want the move to overwrite the array's
4521 // location, as we need it to emit the read barrier.
4522 locations->SetOut(
4523 Location::RequiresRegister(),
4524 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004525 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004526 // We need a temporary register for the read barrier marking slow
4527 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4528 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4529 locations->AddTemp(Location::RequiresRegister());
4530 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004531}
4532
4533void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4534 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004535 Location obj_loc = locations->InAt(0);
4536 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004537 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004538 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004539
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004540 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004541 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004542 case Primitive::kPrimBoolean: {
4543 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004544 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004545 if (index.IsConstant()) {
4546 __ movzxb(out, Address(obj,
4547 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4548 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004549 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550 }
4551 break;
4552 }
4553
4554 case Primitive::kPrimByte: {
4555 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004556 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004557 if (index.IsConstant()) {
4558 __ movsxb(out, Address(obj,
4559 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4560 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004561 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004562 }
4563 break;
4564 }
4565
4566 case Primitive::kPrimShort: {
4567 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004568 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004569 if (index.IsConstant()) {
4570 __ movsxw(out, Address(obj,
4571 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4572 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004573 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004574 }
4575 break;
4576 }
4577
4578 case Primitive::kPrimChar: {
4579 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004580 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004581 if (index.IsConstant()) {
4582 __ movzxw(out, Address(obj,
4583 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4584 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004585 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004586 }
4587 break;
4588 }
4589
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004590 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004592 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004593 if (index.IsConstant()) {
4594 __ movl(out, Address(obj,
4595 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4596 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004597 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598 }
4599 break;
4600 }
4601
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004602 case Primitive::kPrimNot: {
4603 static_assert(
4604 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4605 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4606 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4607 // /* HeapReference<Object> */ out =
4608 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4609 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4610 Location temp = locations->GetTemp(0);
4611 // Note that a potential implicit null check is handled in this
4612 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4613 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4614 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4615 } else {
4616 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4617 if (index.IsConstant()) {
4618 uint32_t offset =
4619 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4620 __ movl(out, Address(obj, offset));
4621 codegen_->MaybeRecordImplicitNullCheck(instruction);
4622 // If read barriers are enabled, emit read barriers other than
4623 // Baker's using a slow path (and also unpoison the loaded
4624 // reference, if heap poisoning is enabled).
4625 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4626 } else {
4627 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4628 codegen_->MaybeRecordImplicitNullCheck(instruction);
4629 // If read barriers are enabled, emit read barriers other than
4630 // Baker's using a slow path (and also unpoison the loaded
4631 // reference, if heap poisoning is enabled).
4632 codegen_->MaybeGenerateReadBarrierSlow(
4633 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4634 }
4635 }
4636 break;
4637 }
4638
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004639 case Primitive::kPrimLong: {
4640 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004641 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004642 if (index.IsConstant()) {
4643 __ movq(out, Address(obj,
4644 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4645 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004646 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004647 }
4648 break;
4649 }
4650
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004651 case Primitive::kPrimFloat: {
4652 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004653 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004654 if (index.IsConstant()) {
4655 __ movss(out, Address(obj,
4656 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4657 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004658 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004659 }
4660 break;
4661 }
4662
4663 case Primitive::kPrimDouble: {
4664 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004665 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004666 if (index.IsConstant()) {
4667 __ movsd(out, Address(obj,
4668 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4669 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004670 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004671 }
4672 break;
4673 }
4674
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004675 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004676 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004677 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004678 }
Roland Levillain4d027112015-07-01 15:41:14 +01004679
4680 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004681 // Potential implicit null checks, in the case of reference
4682 // arrays, are handled in the previous switch statement.
4683 } else {
4684 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004685 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004686}
4687
4688void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004689 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004690
4691 bool needs_write_barrier =
4692 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004693 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004694 bool object_array_set_with_read_barrier =
4695 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004696
Nicolas Geoffray39468442014-09-02 15:17:15 +01004697 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004698 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004699 (may_need_runtime_call || object_array_set_with_read_barrier) ?
4700 LocationSummary::kCallOnSlowPath :
4701 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004702
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004703 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004704 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4705 if (Primitive::IsFloatingPointType(value_type)) {
4706 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004707 } else {
4708 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4709 }
4710
4711 if (needs_write_barrier) {
4712 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004713
4714 // This first temporary register is possibly used for heap
4715 // reference poisoning and/or read barrier emission too.
4716 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004717 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004719}
4720
4721void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4722 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004723 Location array_loc = locations->InAt(0);
4724 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004725 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004726 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004727 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004728 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004729 bool needs_write_barrier =
4730 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004731 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4732 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4733 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004734
4735 switch (value_type) {
4736 case Primitive::kPrimBoolean:
4737 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004738 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4739 Address address = index.IsConstant()
4740 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4741 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4742 if (value.IsRegister()) {
4743 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004744 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004745 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004746 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004747 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004748 break;
4749 }
4750
4751 case Primitive::kPrimShort:
4752 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004753 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4754 Address address = index.IsConstant()
4755 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4756 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4757 if (value.IsRegister()) {
4758 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004759 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004760 DCHECK(value.IsConstant()) << value;
4761 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004762 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004763 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004764 break;
4765 }
4766
4767 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004768 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4769 Address address = index.IsConstant()
4770 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4771 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004772
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 if (!value.IsRegister()) {
4774 // Just setting null.
4775 DCHECK(instruction->InputAt(2)->IsNullConstant());
4776 DCHECK(value.IsConstant()) << value;
4777 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004778 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004779 DCHECK(!needs_write_barrier);
4780 DCHECK(!may_need_runtime_call);
4781 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004782 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004783
4784 DCHECK(needs_write_barrier);
4785 CpuRegister register_value = value.AsRegister<CpuRegister>();
4786 NearLabel done, not_null, do_put;
4787 SlowPathCode* slow_path = nullptr;
4788 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4789 if (may_need_runtime_call) {
4790 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4791 codegen_->AddSlowPath(slow_path);
4792 if (instruction->GetValueCanBeNull()) {
4793 __ testl(register_value, register_value);
4794 __ j(kNotEqual, &not_null);
4795 __ movl(address, Immediate(0));
4796 codegen_->MaybeRecordImplicitNullCheck(instruction);
4797 __ jmp(&done);
4798 __ Bind(&not_null);
4799 }
4800
Roland Levillain0d5a2812015-11-13 10:07:31 +00004801 if (kEmitCompilerReadBarrier) {
4802 // When read barriers are enabled, the type checking
4803 // instrumentation requires two read barriers:
4804 //
4805 // __ movl(temp2, temp);
4806 // // /* HeapReference<Class> */ temp = temp->component_type_
4807 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004808 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004809 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4810 //
4811 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4812 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004813 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004814 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4815 //
4816 // __ cmpl(temp, temp2);
4817 //
4818 // However, the second read barrier may trash `temp`, as it
4819 // is a temporary register, and as such would not be saved
4820 // along with live registers before calling the runtime (nor
4821 // restored afterwards). So in this case, we bail out and
4822 // delegate the work to the array set slow path.
4823 //
4824 // TODO: Extend the register allocator to support a new
4825 // "(locally) live temp" location so as to avoid always
4826 // going into the slow path when read barriers are enabled.
4827 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004828 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004829 // /* HeapReference<Class> */ temp = array->klass_
4830 __ movl(temp, Address(array, class_offset));
4831 codegen_->MaybeRecordImplicitNullCheck(instruction);
4832 __ MaybeUnpoisonHeapReference(temp);
4833
4834 // /* HeapReference<Class> */ temp = temp->component_type_
4835 __ movl(temp, Address(temp, component_offset));
4836 // If heap poisoning is enabled, no need to unpoison `temp`
4837 // nor the object reference in `register_value->klass`, as
4838 // we are comparing two poisoned references.
4839 __ cmpl(temp, Address(register_value, class_offset));
4840
4841 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4842 __ j(kEqual, &do_put);
4843 // If heap poisoning is enabled, the `temp` reference has
4844 // not been unpoisoned yet; unpoison it now.
4845 __ MaybeUnpoisonHeapReference(temp);
4846
4847 // /* HeapReference<Class> */ temp = temp->super_class_
4848 __ movl(temp, Address(temp, super_offset));
4849 // If heap poisoning is enabled, no need to unpoison
4850 // `temp`, as we are comparing against null below.
4851 __ testl(temp, temp);
4852 __ j(kNotEqual, slow_path->GetEntryLabel());
4853 __ Bind(&do_put);
4854 } else {
4855 __ j(kNotEqual, slow_path->GetEntryLabel());
4856 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004857 }
4858 }
4859
4860 if (kPoisonHeapReferences) {
4861 __ movl(temp, register_value);
4862 __ PoisonHeapReference(temp);
4863 __ movl(address, temp);
4864 } else {
4865 __ movl(address, register_value);
4866 }
4867 if (!may_need_runtime_call) {
4868 codegen_->MaybeRecordImplicitNullCheck(instruction);
4869 }
4870
4871 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4872 codegen_->MarkGCCard(
4873 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4874 __ Bind(&done);
4875
4876 if (slow_path != nullptr) {
4877 __ Bind(slow_path->GetExitLabel());
4878 }
4879
4880 break;
4881 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004882
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004883 case Primitive::kPrimInt: {
4884 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4885 Address address = index.IsConstant()
4886 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4887 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4888 if (value.IsRegister()) {
4889 __ movl(address, value.AsRegister<CpuRegister>());
4890 } else {
4891 DCHECK(value.IsConstant()) << value;
4892 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4893 __ movl(address, Immediate(v));
4894 }
4895 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004896 break;
4897 }
4898
4899 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004900 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4901 Address address = index.IsConstant()
4902 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4903 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4904 if (value.IsRegister()) {
4905 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004906 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004907 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004908 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004909 Address address_high = index.IsConstant()
4910 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4911 offset + sizeof(int32_t))
4912 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4913 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004914 }
4915 break;
4916 }
4917
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004918 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004919 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4920 Address address = index.IsConstant()
4921 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4922 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004923 if (value.IsFpuRegister()) {
4924 __ movss(address, value.AsFpuRegister<XmmRegister>());
4925 } else {
4926 DCHECK(value.IsConstant());
4927 int32_t v =
4928 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4929 __ movl(address, Immediate(v));
4930 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004931 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004932 break;
4933 }
4934
4935 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004936 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4937 Address address = index.IsConstant()
4938 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4939 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004940 if (value.IsFpuRegister()) {
4941 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4942 codegen_->MaybeRecordImplicitNullCheck(instruction);
4943 } else {
4944 int64_t v =
4945 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4946 Address address_high = index.IsConstant()
4947 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4948 offset + sizeof(int32_t))
4949 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4950 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4951 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004952 break;
4953 }
4954
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004955 case Primitive::kPrimVoid:
4956 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004957 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004958 }
4959}
4960
4961void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004962 LocationSummary* locations =
4963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004964 locations->SetInAt(0, Location::RequiresRegister());
4965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004966}
4967
4968void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4969 LocationSummary* locations = instruction->GetLocations();
4970 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004971 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4972 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004973 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004974 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004975}
4976
4977void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004978 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4979 ? LocationSummary::kCallOnSlowPath
4980 : LocationSummary::kNoCall;
4981 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004982 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004983 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004984 if (instruction->HasUses()) {
4985 locations->SetOut(Location::SameAsFirstInput());
4986 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004987}
4988
4989void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4990 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004991 Location index_loc = locations->InAt(0);
4992 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004993 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004994 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995
Mark Mendell99dbd682015-04-22 16:18:52 -04004996 if (length_loc.IsConstant()) {
4997 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4998 if (index_loc.IsConstant()) {
4999 // BCE will remove the bounds check if we are guarenteed to pass.
5000 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5001 if (index < 0 || index >= length) {
5002 codegen_->AddSlowPath(slow_path);
5003 __ jmp(slow_path->GetEntryLabel());
5004 } else {
5005 // Some optimization after BCE may have generated this, and we should not
5006 // generate a bounds check if it is a valid range.
5007 }
5008 return;
5009 }
5010
5011 // We have to reverse the jump condition because the length is the constant.
5012 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5013 __ cmpl(index_reg, Immediate(length));
5014 codegen_->AddSlowPath(slow_path);
5015 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005016 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005017 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5018 if (index_loc.IsConstant()) {
5019 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5020 __ cmpl(length, Immediate(value));
5021 } else {
5022 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5023 }
5024 codegen_->AddSlowPath(slow_path);
5025 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005026 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005027}
5028
5029void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5030 CpuRegister card,
5031 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005032 CpuRegister value,
5033 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005034 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005035 if (value_can_be_null) {
5036 __ testl(value, value);
5037 __ j(kEqual, &is_null);
5038 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005039 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5040 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041 __ movq(temp, object);
5042 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005043 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005044 if (value_can_be_null) {
5045 __ Bind(&is_null);
5046 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005047}
5048
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005049void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
5050 temp->SetLocations(nullptr);
5051}
5052
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005053void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005054 // Nothing to do, this is driven by the code generator.
5055}
5056
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005057void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005058 LOG(FATAL) << "Unimplemented";
5059}
5060
5061void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005062 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5063}
5064
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005065void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5066 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5067}
5068
5069void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005070 HBasicBlock* block = instruction->GetBlock();
5071 if (block->GetLoopInformation() != nullptr) {
5072 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5073 // The back edge will generate the suspend check.
5074 return;
5075 }
5076 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5077 // The goto will generate the suspend check.
5078 return;
5079 }
5080 GenerateSuspendCheck(instruction, nullptr);
5081}
5082
5083void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5084 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005085 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005086 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5087 if (slow_path == nullptr) {
5088 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5089 instruction->SetSlowPath(slow_path);
5090 codegen_->AddSlowPath(slow_path);
5091 if (successor != nullptr) {
5092 DCHECK(successor->IsLoopHeader());
5093 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5094 }
5095 } else {
5096 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5097 }
5098
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005099 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5100 /* no_rip */ true),
5101 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005102 if (successor == nullptr) {
5103 __ j(kNotEqual, slow_path->GetEntryLabel());
5104 __ Bind(slow_path->GetReturnLabel());
5105 } else {
5106 __ j(kEqual, codegen_->GetLabelOf(successor));
5107 __ jmp(slow_path->GetEntryLabel());
5108 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005109}
5110
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005111X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5112 return codegen_->GetAssembler();
5113}
5114
5115void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005116 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005117 Location source = move->GetSource();
5118 Location destination = move->GetDestination();
5119
5120 if (source.IsRegister()) {
5121 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005122 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005123 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005124 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005125 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005126 } else {
5127 DCHECK(destination.IsDoubleStackSlot());
5128 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005129 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005130 }
5131 } else if (source.IsStackSlot()) {
5132 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005133 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005134 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005135 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005136 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005137 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005138 } else {
5139 DCHECK(destination.IsStackSlot());
5140 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5141 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5142 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005143 } else if (source.IsDoubleStackSlot()) {
5144 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005145 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005146 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005147 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005148 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5149 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005150 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005151 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005152 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5153 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5154 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005155 } else if (source.IsConstant()) {
5156 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005157 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5158 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005159 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005160 if (value == 0) {
5161 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5162 } else {
5163 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5164 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005165 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005166 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005167 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005168 }
5169 } else if (constant->IsLongConstant()) {
5170 int64_t value = constant->AsLongConstant()->GetValue();
5171 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005172 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005173 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005174 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005175 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005176 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005177 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005178 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005179 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005180 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005181 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5182 if (value == 0) {
5183 // easy FP 0.0.
5184 __ xorps(dest, dest);
5185 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005186 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005187 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005188 } else {
5189 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04005190 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005191 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5192 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005193 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005194 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005195 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005196 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005197 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005198 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5199 if (value == 0) {
5200 __ xorpd(dest, dest);
5201 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005202 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005203 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005204 } else {
5205 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005206 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005207 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005208 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005209 } else if (source.IsFpuRegister()) {
5210 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005211 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005212 } else if (destination.IsStackSlot()) {
5213 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005214 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005215 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005216 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005217 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005218 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005219 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005220 }
5221}
5222
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005223void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005224 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005225 __ movl(Address(CpuRegister(RSP), mem), reg);
5226 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005227}
5228
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005229void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005230 ScratchRegisterScope ensure_scratch(
5231 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5232
5233 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5234 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5235 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5236 Address(CpuRegister(RSP), mem2 + stack_offset));
5237 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5238 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5239 CpuRegister(ensure_scratch.GetRegister()));
5240}
5241
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005242void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5243 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5244 __ movq(Address(CpuRegister(RSP), mem), reg);
5245 __ movq(reg, CpuRegister(TMP));
5246}
5247
5248void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5249 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005250 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005251
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005252 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5253 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5254 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5255 Address(CpuRegister(RSP), mem2 + stack_offset));
5256 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5257 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5258 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005259}
5260
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005261void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5262 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5263 __ movss(Address(CpuRegister(RSP), mem), reg);
5264 __ movd(reg, CpuRegister(TMP));
5265}
5266
5267void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5268 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5269 __ movsd(Address(CpuRegister(RSP), mem), reg);
5270 __ movd(reg, CpuRegister(TMP));
5271}
5272
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005273void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005274 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005275 Location source = move->GetSource();
5276 Location destination = move->GetDestination();
5277
5278 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005279 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005280 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005281 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005282 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005283 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005284 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005285 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5286 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005287 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005288 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005289 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005290 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5291 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005292 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005293 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5294 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5295 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005296 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005297 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005299 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005300 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005301 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005302 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005303 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005304 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005305 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005306 }
5307}
5308
5309
5310void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5311 __ pushq(CpuRegister(reg));
5312}
5313
5314
5315void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5316 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005317}
5318
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005319void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005320 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005321 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5322 Immediate(mirror::Class::kStatusInitialized));
5323 __ j(kLess, slow_path->GetEntryLabel());
5324 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005325 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005326}
5327
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005328void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005329 InvokeRuntimeCallingConvention calling_convention;
5330 CodeGenerator::CreateLoadClassLocationSummary(
5331 cls,
5332 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005333 Location::RegisterLocation(RAX),
5334 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005335}
5336
5337void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005338 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005339 if (cls->NeedsAccessCheck()) {
5340 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5341 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5342 cls,
5343 cls->GetDexPc(),
5344 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005345 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005346 return;
5347 }
5348
Roland Levillain0d5a2812015-11-13 10:07:31 +00005349 Location out_loc = locations->Out();
5350 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005351 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005352
Calin Juravle580b6092015-10-06 17:35:58 +01005353 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005354 DCHECK(!cls->CanCallRuntime());
5355 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005356 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5357 GenerateGcRootFieldLoad(
5358 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005359 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005360 // /* GcRoot<mirror::Class>[] */ out =
5361 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5362 __ movq(out, Address(current_method,
5363 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005364 // /* GcRoot<mirror::Class> */ out = out[type_index]
5365 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01005366
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005367 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5368 DCHECK(cls->CanCallRuntime());
5369 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5370 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5371 codegen_->AddSlowPath(slow_path);
5372 if (!cls->IsInDexCache()) {
5373 __ testl(out, out);
5374 __ j(kEqual, slow_path->GetEntryLabel());
5375 }
5376 if (cls->MustGenerateClinitCheck()) {
5377 GenerateClassInitializationCheck(slow_path, out);
5378 } else {
5379 __ Bind(slow_path->GetExitLabel());
5380 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005381 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005382 }
5383}
5384
5385void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5386 LocationSummary* locations =
5387 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5388 locations->SetInAt(0, Location::RequiresRegister());
5389 if (check->HasUses()) {
5390 locations->SetOut(Location::SameAsFirstInput());
5391 }
5392}
5393
5394void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005395 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005396 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005397 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005398 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005399 GenerateClassInitializationCheck(slow_path,
5400 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005401}
5402
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005403void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005404 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5405 ? LocationSummary::kCallOnSlowPath
5406 : LocationSummary::kNoCall;
5407 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005408 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005409 locations->SetOut(Location::RequiresRegister());
5410}
5411
5412void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005413 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005414 Location out_loc = locations->Out();
5415 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005416 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005417
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005418 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5419 GenerateGcRootFieldLoad(
5420 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005421 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5422 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005423 // /* GcRoot<mirror::String> */ out = out[string_index]
5424 GenerateGcRootFieldLoad(
5425 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005426
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005427 if (!load->IsInDexCache()) {
5428 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5429 codegen_->AddSlowPath(slow_path);
5430 __ testl(out, out);
5431 __ j(kEqual, slow_path->GetEntryLabel());
5432 __ Bind(slow_path->GetExitLabel());
5433 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005434}
5435
David Brazdilcb1c0552015-08-04 16:22:25 +01005436static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005437 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5438 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005439}
5440
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005441void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5442 LocationSummary* locations =
5443 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5444 locations->SetOut(Location::RequiresRegister());
5445}
5446
5447void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005448 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5449}
5450
5451void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5452 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5453}
5454
5455void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5456 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005457}
5458
5459void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5460 LocationSummary* locations =
5461 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5462 InvokeRuntimeCallingConvention calling_convention;
5463 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5464}
5465
5466void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005467 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5468 instruction,
5469 instruction->GetDexPc(),
5470 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005471 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005472}
5473
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005474static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5475 return kEmitCompilerReadBarrier &&
5476 (kUseBakerReadBarrier ||
5477 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5478 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5479 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5480}
5481
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005482void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005483 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005484 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5485 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005486 case TypeCheckKind::kExactCheck:
5487 case TypeCheckKind::kAbstractClassCheck:
5488 case TypeCheckKind::kClassHierarchyCheck:
5489 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005490 call_kind =
5491 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005492 break;
5493 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005494 case TypeCheckKind::kUnresolvedCheck:
5495 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005496 call_kind = LocationSummary::kCallOnSlowPath;
5497 break;
5498 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005499
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005501 locations->SetInAt(0, Location::RequiresRegister());
5502 locations->SetInAt(1, Location::Any());
5503 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5504 locations->SetOut(Location::RequiresRegister());
5505 // When read barriers are enabled, we need a temporary register for
5506 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005507 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005508 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005509 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005510}
5511
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005512void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005513 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005514 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005515 Location obj_loc = locations->InAt(0);
5516 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005517 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005518 Location out_loc = locations->Out();
5519 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005520 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5521 locations->GetTemp(0) :
5522 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005523 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005524 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5525 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5526 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005527 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005528 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005529
5530 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005531 // Avoid null check if we know obj is not null.
5532 if (instruction->MustDoNullCheck()) {
5533 __ testl(obj, obj);
5534 __ j(kEqual, &zero);
5535 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005536
Roland Levillain0d5a2812015-11-13 10:07:31 +00005537 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005538 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005539
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005540 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005541 case TypeCheckKind::kExactCheck: {
5542 if (cls.IsRegister()) {
5543 __ cmpl(out, cls.AsRegister<CpuRegister>());
5544 } else {
5545 DCHECK(cls.IsStackSlot()) << cls;
5546 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5547 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005548 if (zero.IsLinked()) {
5549 // Classes must be equal for the instanceof to succeed.
5550 __ j(kNotEqual, &zero);
5551 __ movl(out, Immediate(1));
5552 __ jmp(&done);
5553 } else {
5554 __ setcc(kEqual, out);
5555 // setcc only sets the low byte.
5556 __ andl(out, Immediate(1));
5557 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005558 break;
5559 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005560
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005561 case TypeCheckKind::kAbstractClassCheck: {
5562 // If the class is abstract, we eagerly fetch the super class of the
5563 // object to avoid doing a comparison we know will fail.
5564 NearLabel loop, success;
5565 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005566 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005567 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005568 __ testl(out, out);
5569 // If `out` is null, we use it for the result, and jump to `done`.
5570 __ j(kEqual, &done);
5571 if (cls.IsRegister()) {
5572 __ cmpl(out, cls.AsRegister<CpuRegister>());
5573 } else {
5574 DCHECK(cls.IsStackSlot()) << cls;
5575 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5576 }
5577 __ j(kNotEqual, &loop);
5578 __ movl(out, Immediate(1));
5579 if (zero.IsLinked()) {
5580 __ jmp(&done);
5581 }
5582 break;
5583 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005584
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005585 case TypeCheckKind::kClassHierarchyCheck: {
5586 // Walk over the class hierarchy to find a match.
5587 NearLabel loop, success;
5588 __ Bind(&loop);
5589 if (cls.IsRegister()) {
5590 __ cmpl(out, cls.AsRegister<CpuRegister>());
5591 } else {
5592 DCHECK(cls.IsStackSlot()) << cls;
5593 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5594 }
5595 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005596 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005597 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005598 __ testl(out, out);
5599 __ j(kNotEqual, &loop);
5600 // If `out` is null, we use it for the result, and jump to `done`.
5601 __ jmp(&done);
5602 __ Bind(&success);
5603 __ movl(out, Immediate(1));
5604 if (zero.IsLinked()) {
5605 __ jmp(&done);
5606 }
5607 break;
5608 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005609
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005610 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005611 // Do an exact check.
5612 NearLabel exact_check;
5613 if (cls.IsRegister()) {
5614 __ cmpl(out, cls.AsRegister<CpuRegister>());
5615 } else {
5616 DCHECK(cls.IsStackSlot()) << cls;
5617 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5618 }
5619 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005620 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005621 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005622 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005623 __ testl(out, out);
5624 // If `out` is null, we use it for the result, and jump to `done`.
5625 __ j(kEqual, &done);
5626 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5627 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005628 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005629 __ movl(out, Immediate(1));
5630 __ jmp(&done);
5631 break;
5632 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005633
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005634 case TypeCheckKind::kArrayCheck: {
5635 if (cls.IsRegister()) {
5636 __ cmpl(out, cls.AsRegister<CpuRegister>());
5637 } else {
5638 DCHECK(cls.IsStackSlot()) << cls;
5639 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5640 }
5641 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005642 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5643 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 codegen_->AddSlowPath(slow_path);
5645 __ j(kNotEqual, slow_path->GetEntryLabel());
5646 __ movl(out, Immediate(1));
5647 if (zero.IsLinked()) {
5648 __ jmp(&done);
5649 }
5650 break;
5651 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005652
Calin Juravle98893e12015-10-02 21:05:03 +01005653 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005654 case TypeCheckKind::kInterfaceCheck: {
5655 // Note that we indeed only call on slow path, but we always go
5656 // into the slow path for the unresolved & interface check
5657 // cases.
5658 //
5659 // We cannot directly call the InstanceofNonTrivial runtime
5660 // entry point without resorting to a type checking slow path
5661 // here (i.e. by calling InvokeRuntime directly), as it would
5662 // require to assign fixed registers for the inputs of this
5663 // HInstanceOf instruction (following the runtime calling
5664 // convention), which might be cluttered by the potential first
5665 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005666 //
5667 // TODO: Introduce a new runtime entry point taking the object
5668 // to test (instead of its class) as argument, and let it deal
5669 // with the read barrier issues. This will let us refactor this
5670 // case of the `switch` code as it was previously (with a direct
5671 // call to the runtime not using a type checking slow path).
5672 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005673 DCHECK(locations->OnlyCallsOnSlowPath());
5674 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5675 /* is_fatal */ false);
5676 codegen_->AddSlowPath(slow_path);
5677 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005678 if (zero.IsLinked()) {
5679 __ jmp(&done);
5680 }
5681 break;
5682 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005683 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005684
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005685 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005686 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005687 __ xorl(out, out);
5688 }
5689
5690 if (done.IsLinked()) {
5691 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005692 }
5693
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005694 if (slow_path != nullptr) {
5695 __ Bind(slow_path->GetExitLabel());
5696 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005697}
5698
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005699void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005700 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5701 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005702 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5703 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005704 case TypeCheckKind::kExactCheck:
5705 case TypeCheckKind::kAbstractClassCheck:
5706 case TypeCheckKind::kClassHierarchyCheck:
5707 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005708 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5709 LocationSummary::kCallOnSlowPath :
5710 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005711 break;
5712 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005713 case TypeCheckKind::kUnresolvedCheck:
5714 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005715 call_kind = LocationSummary::kCallOnSlowPath;
5716 break;
5717 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5719 locations->SetInAt(0, Location::RequiresRegister());
5720 locations->SetInAt(1, Location::Any());
5721 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5722 locations->AddTemp(Location::RequiresRegister());
5723 // When read barriers are enabled, we need an additional temporary
5724 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005725 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005726 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005728}
5729
5730void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005731 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005732 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005733 Location obj_loc = locations->InAt(0);
5734 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005735 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005736 Location temp_loc = locations->GetTemp(0);
5737 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005738 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5739 locations->GetTemp(1) :
5740 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005741 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5742 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5743 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5744 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005745
Roland Levillain0d5a2812015-11-13 10:07:31 +00005746 bool is_type_check_slow_path_fatal =
5747 (type_check_kind == TypeCheckKind::kExactCheck ||
5748 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5749 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5750 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5751 !instruction->CanThrowIntoCatchBlock();
5752 SlowPathCode* type_check_slow_path =
5753 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5754 is_type_check_slow_path_fatal);
5755 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005756
Mark Mendell152408f2015-12-31 12:28:50 -05005757 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005758 // Avoid null check if we know obj is not null.
5759 if (instruction->MustDoNullCheck()) {
5760 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005761 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005762 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005763
Roland Levillain0d5a2812015-11-13 10:07:31 +00005764 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005765 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005766
Roland Levillain0d5a2812015-11-13 10:07:31 +00005767 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005768 case TypeCheckKind::kExactCheck:
5769 case TypeCheckKind::kArrayCheck: {
5770 if (cls.IsRegister()) {
5771 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5772 } else {
5773 DCHECK(cls.IsStackSlot()) << cls;
5774 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5775 }
5776 // Jump to slow path for throwing the exception or doing a
5777 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005778 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005779 break;
5780 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005781
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005782 case TypeCheckKind::kAbstractClassCheck: {
5783 // If the class is abstract, we eagerly fetch the super class of the
5784 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005785 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005786 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005787 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005788 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005789
5790 // If the class reference currently in `temp` is not null, jump
5791 // to the `compare_classes` label to compare it with the checked
5792 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005793 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005794 __ j(kNotEqual, &compare_classes);
5795 // Otherwise, jump to the slow path to throw the exception.
5796 //
5797 // But before, move back the object's class into `temp` before
5798 // going into the slow path, as it has been overwritten in the
5799 // meantime.
5800 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005801 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005802 __ jmp(type_check_slow_path->GetEntryLabel());
5803
5804 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005805 if (cls.IsRegister()) {
5806 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5807 } else {
5808 DCHECK(cls.IsStackSlot()) << cls;
5809 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5810 }
5811 __ j(kNotEqual, &loop);
5812 break;
5813 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005814
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005815 case TypeCheckKind::kClassHierarchyCheck: {
5816 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005817 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005818 __ Bind(&loop);
5819 if (cls.IsRegister()) {
5820 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5821 } else {
5822 DCHECK(cls.IsStackSlot()) << cls;
5823 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5824 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005825 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005826
Roland Levillain0d5a2812015-11-13 10:07:31 +00005827 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005828 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005829
5830 // If the class reference currently in `temp` is not null, jump
5831 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005832 __ testl(temp, temp);
5833 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005834 // Otherwise, jump to the slow path to throw the exception.
5835 //
5836 // But before, move back the object's class into `temp` before
5837 // going into the slow path, as it has been overwritten in the
5838 // meantime.
5839 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005840 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005841 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005842 break;
5843 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005844
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005845 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005846 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005847 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005848 if (cls.IsRegister()) {
5849 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5850 } else {
5851 DCHECK(cls.IsStackSlot()) << cls;
5852 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5853 }
5854 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005855
5856 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005857 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005858 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005859
5860 // If the component type is not null (i.e. the object is indeed
5861 // an array), jump to label `check_non_primitive_component_type`
5862 // to further check that this component type is not a primitive
5863 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005864 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005865 __ j(kNotEqual, &check_non_primitive_component_type);
5866 // Otherwise, jump to the slow path to throw the exception.
5867 //
5868 // But before, move back the object's class into `temp` before
5869 // going into the slow path, as it has been overwritten in the
5870 // meantime.
5871 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005872 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005873 __ jmp(type_check_slow_path->GetEntryLabel());
5874
5875 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005876 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005877 __ j(kEqual, &done);
5878 // Same comment as above regarding `temp` and the slow path.
5879 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005880 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005881 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 break;
5883 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005884
Calin Juravle98893e12015-10-02 21:05:03 +01005885 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005886 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005887 // We always go into the type check slow path for the unresolved &
5888 // interface check cases.
5889 //
5890 // We cannot directly call the CheckCast runtime entry point
5891 // without resorting to a type checking slow path here (i.e. by
5892 // calling InvokeRuntime directly), as it would require to
5893 // assign fixed registers for the inputs of this HInstanceOf
5894 // instruction (following the runtime calling convention), which
5895 // might be cluttered by the potential first read barrier
5896 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005897 //
5898 // TODO: Introduce a new runtime entry point taking the object
5899 // to test (instead of its class) as argument, and let it deal
5900 // with the read barrier issues. This will let us refactor this
5901 // case of the `switch` code as it was previously (with a direct
5902 // call to the runtime not using a type checking slow path).
5903 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005904 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005905 break;
5906 }
5907 __ Bind(&done);
5908
Roland Levillain0d5a2812015-11-13 10:07:31 +00005909 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005910}
5911
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005912void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5913 LocationSummary* locations =
5914 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5915 InvokeRuntimeCallingConvention calling_convention;
5916 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5917}
5918
5919void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005920 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5921 : QUICK_ENTRY_POINT(pUnlockObject),
5922 instruction,
5923 instruction->GetDexPc(),
5924 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005925 if (instruction->IsEnter()) {
5926 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5927 } else {
5928 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5929 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005930}
5931
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005932void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5933void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5934void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5935
5936void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5937 LocationSummary* locations =
5938 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5939 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5940 || instruction->GetResultType() == Primitive::kPrimLong);
5941 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005942 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005943 locations->SetOut(Location::SameAsFirstInput());
5944}
5945
5946void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5947 HandleBitwiseOperation(instruction);
5948}
5949
5950void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5951 HandleBitwiseOperation(instruction);
5952}
5953
5954void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5955 HandleBitwiseOperation(instruction);
5956}
5957
5958void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5959 LocationSummary* locations = instruction->GetLocations();
5960 Location first = locations->InAt(0);
5961 Location second = locations->InAt(1);
5962 DCHECK(first.Equals(locations->Out()));
5963
5964 if (instruction->GetResultType() == Primitive::kPrimInt) {
5965 if (second.IsRegister()) {
5966 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005967 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005968 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005969 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005970 } else {
5971 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005972 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005973 }
5974 } else if (second.IsConstant()) {
5975 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5976 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005977 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005978 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005979 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005980 } else {
5981 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005982 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005983 }
5984 } else {
5985 Address address(CpuRegister(RSP), second.GetStackIndex());
5986 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005987 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005988 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005989 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005990 } else {
5991 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005992 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005993 }
5994 }
5995 } else {
5996 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005997 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5998 bool second_is_constant = false;
5999 int64_t value = 0;
6000 if (second.IsConstant()) {
6001 second_is_constant = true;
6002 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006003 }
Mark Mendell40741f32015-04-20 22:10:34 -04006004 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006005
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006006 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006007 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006008 if (is_int32_value) {
6009 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6010 } else {
6011 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6012 }
6013 } else if (second.IsDoubleStackSlot()) {
6014 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006015 } else {
6016 __ andq(first_reg, second.AsRegister<CpuRegister>());
6017 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006018 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006019 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006020 if (is_int32_value) {
6021 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6022 } else {
6023 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6024 }
6025 } else if (second.IsDoubleStackSlot()) {
6026 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006027 } else {
6028 __ orq(first_reg, second.AsRegister<CpuRegister>());
6029 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006030 } else {
6031 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006032 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006033 if (is_int32_value) {
6034 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6035 } else {
6036 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6037 }
6038 } else if (second.IsDoubleStackSlot()) {
6039 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006040 } else {
6041 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6042 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006043 }
6044 }
6045}
6046
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006047void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6048 Location out,
6049 uint32_t offset,
6050 Location temp) {
6051 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6052 if (kEmitCompilerReadBarrier) {
6053 if (kUseBakerReadBarrier) {
6054 // Load with fast path based Baker's read barrier.
6055 // /* HeapReference<Object> */ out = *(out + offset)
6056 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6057 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
6058 } else {
6059 // Load with slow path based read barrier.
6060 // Save the value of `out` into `temp` before overwriting it
6061 // in the following move operation, as we will need it for the
6062 // read barrier below.
6063 __ movl(temp.AsRegister<CpuRegister>(), out_reg);
6064 // /* HeapReference<Object> */ out = *(out + offset)
6065 __ movl(out_reg, Address(out_reg, offset));
6066 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
6067 }
6068 } else {
6069 // Plain load with no read barrier.
6070 // /* HeapReference<Object> */ out = *(out + offset)
6071 __ movl(out_reg, Address(out_reg, offset));
6072 __ MaybeUnpoisonHeapReference(out_reg);
6073 }
6074}
6075
6076void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6077 Location out,
6078 Location obj,
6079 uint32_t offset,
6080 Location temp) {
6081 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6082 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6083 if (kEmitCompilerReadBarrier) {
6084 if (kUseBakerReadBarrier) {
6085 // Load with fast path based Baker's read barrier.
6086 // /* HeapReference<Object> */ out = *(obj + offset)
6087 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6088 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
6089 } else {
6090 // Load with slow path based read barrier.
6091 // /* HeapReference<Object> */ out = *(obj + offset)
6092 __ movl(out_reg, Address(obj_reg, offset));
6093 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6094 }
6095 } else {
6096 // Plain load with no read barrier.
6097 // /* HeapReference<Object> */ out = *(obj + offset)
6098 __ movl(out_reg, Address(obj_reg, offset));
6099 __ MaybeUnpoisonHeapReference(out_reg);
6100 }
6101}
6102
6103void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6104 Location root,
6105 CpuRegister obj,
6106 uint32_t offset) {
6107 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6108 if (kEmitCompilerReadBarrier) {
6109 if (kUseBakerReadBarrier) {
6110 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6111 // Baker's read barrier are used:
6112 //
6113 // root = obj.field;
6114 // if (Thread::Current()->GetIsGcMarking()) {
6115 // root = ReadBarrier::Mark(root)
6116 // }
6117
6118 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6119 __ movl(root_reg, Address(obj, offset));
6120 static_assert(
6121 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6122 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6123 "have different sizes.");
6124 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6125 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6126 "have different sizes.");
6127
6128 // Slow path used to mark the GC root `root`.
6129 SlowPathCode* slow_path =
6130 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6131 codegen_->AddSlowPath(slow_path);
6132
6133 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6134 /* no_rip */ true),
6135 Immediate(0));
6136 __ j(kNotEqual, slow_path->GetEntryLabel());
6137 __ Bind(slow_path->GetExitLabel());
6138 } else {
6139 // GC root loaded through a slow path for read barriers other
6140 // than Baker's.
6141 // /* GcRoot<mirror::Object>* */ root = obj + offset
6142 __ leaq(root_reg, Address(obj, offset));
6143 // /* mirror::Object* */ root = root->Read()
6144 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6145 }
6146 } else {
6147 // Plain GC root load with no read barrier.
6148 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6149 __ movl(root_reg, Address(obj, offset));
6150 }
6151}
6152
6153void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6154 Location ref,
6155 CpuRegister obj,
6156 uint32_t offset,
6157 Location temp,
6158 bool needs_null_check) {
6159 DCHECK(kEmitCompilerReadBarrier);
6160 DCHECK(kUseBakerReadBarrier);
6161
6162 // /* HeapReference<Object> */ ref = *(obj + offset)
6163 Address src(obj, offset);
6164 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6165}
6166
6167void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6168 Location ref,
6169 CpuRegister obj,
6170 uint32_t data_offset,
6171 Location index,
6172 Location temp,
6173 bool needs_null_check) {
6174 DCHECK(kEmitCompilerReadBarrier);
6175 DCHECK(kUseBakerReadBarrier);
6176
6177 // /* HeapReference<Object> */ ref =
6178 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6179 Address src = index.IsConstant() ?
6180 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6181 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6182 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6183}
6184
6185void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6186 Location ref,
6187 CpuRegister obj,
6188 const Address& src,
6189 Location temp,
6190 bool needs_null_check) {
6191 DCHECK(kEmitCompilerReadBarrier);
6192 DCHECK(kUseBakerReadBarrier);
6193
6194 // In slow path based read barriers, the read barrier call is
6195 // inserted after the original load. However, in fast path based
6196 // Baker's read barriers, we need to perform the load of
6197 // mirror::Object::monitor_ *before* the original reference load.
6198 // This load-load ordering is required by the read barrier.
6199 // The fast path/slow path (for Baker's algorithm) should look like:
6200 //
6201 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6202 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6203 // HeapReference<Object> ref = *src; // Original reference load.
6204 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6205 // if (is_gray) {
6206 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6207 // }
6208 //
6209 // Note: the original implementation in ReadBarrier::Barrier is
6210 // slightly more complex as:
6211 // - it implements the load-load fence using a data dependency on
6212 // the high-bits of rb_state, which are expected to be all zeroes;
6213 // - it performs additional checks that we do not do here for
6214 // performance reasons.
6215
6216 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6217 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6218 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6219
6220 // /* int32_t */ monitor = obj->monitor_
6221 __ movl(temp_reg, Address(obj, monitor_offset));
6222 if (needs_null_check) {
6223 MaybeRecordImplicitNullCheck(instruction);
6224 }
6225 // /* LockWord */ lock_word = LockWord(monitor)
6226 static_assert(sizeof(LockWord) == sizeof(int32_t),
6227 "art::LockWord and int32_t have different sizes.");
6228 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6229 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6230 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6231 static_assert(
6232 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6233 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6234
6235 // Load fence to prevent load-load reordering.
6236 // Note that this is a no-op, thanks to the x86-64 memory model.
6237 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6238
6239 // The actual reference load.
6240 // /* HeapReference<Object> */ ref = *src
6241 __ movl(ref_reg, src);
6242
6243 // Object* ref = ref_addr->AsMirrorPtr()
6244 __ MaybeUnpoisonHeapReference(ref_reg);
6245
6246 // Slow path used to mark the object `ref` when it is gray.
6247 SlowPathCode* slow_path =
6248 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6249 AddSlowPath(slow_path);
6250
6251 // if (rb_state == ReadBarrier::gray_ptr_)
6252 // ref = ReadBarrier::Mark(ref);
6253 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6254 __ j(kEqual, slow_path->GetEntryLabel());
6255 __ Bind(slow_path->GetExitLabel());
6256}
6257
6258void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6259 Location out,
6260 Location ref,
6261 Location obj,
6262 uint32_t offset,
6263 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006264 DCHECK(kEmitCompilerReadBarrier);
6265
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006266 // Insert a slow path based read barrier *after* the reference load.
6267 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006268 // If heap poisoning is enabled, the unpoisoning of the loaded
6269 // reference will be carried out by the runtime within the slow
6270 // path.
6271 //
6272 // Note that `ref` currently does not get unpoisoned (when heap
6273 // poisoning is enabled), which is alright as the `ref` argument is
6274 // not used by the artReadBarrierSlow entry point.
6275 //
6276 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6277 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6278 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6279 AddSlowPath(slow_path);
6280
Roland Levillain0d5a2812015-11-13 10:07:31 +00006281 __ jmp(slow_path->GetEntryLabel());
6282 __ Bind(slow_path->GetExitLabel());
6283}
6284
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006285void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6286 Location out,
6287 Location ref,
6288 Location obj,
6289 uint32_t offset,
6290 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006291 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006292 // Baker's read barriers shall be handled by the fast path
6293 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6294 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006295 // If heap poisoning is enabled, unpoisoning will be taken care of
6296 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006297 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298 } else if (kPoisonHeapReferences) {
6299 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6300 }
6301}
6302
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006303void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6304 Location out,
6305 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006306 DCHECK(kEmitCompilerReadBarrier);
6307
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006308 // Insert a slow path based read barrier *after* the GC root load.
6309 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006310 // Note that GC roots are not affected by heap poisoning, so we do
6311 // not need to do anything special for this here.
6312 SlowPathCode* slow_path =
6313 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6314 AddSlowPath(slow_path);
6315
Roland Levillain0d5a2812015-11-13 10:07:31 +00006316 __ jmp(slow_path->GetEntryLabel());
6317 __ Bind(slow_path->GetExitLabel());
6318}
6319
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006320void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006321 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006322 LOG(FATAL) << "Unreachable";
6323}
6324
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006325void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006326 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006327 LOG(FATAL) << "Unreachable";
6328}
6329
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006330void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
6331 DCHECK(codegen_->IsBaseline());
6332 LocationSummary* locations =
6333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6334 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6335}
6336
6337void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6338 DCHECK(codegen_->IsBaseline());
6339 // Will be generated at use site.
6340}
6341
Mark Mendellfe57faa2015-09-18 09:26:15 -04006342// Simple implementation of packed switch - generate cascaded compare/jumps.
6343void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6344 LocationSummary* locations =
6345 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6346 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006347 locations->AddTemp(Location::RequiresRegister());
6348 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006349}
6350
6351void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6352 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006353 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006354 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006355 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6356 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6357 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006358 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6359
6360 // Should we generate smaller inline compare/jumps?
6361 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6362 // Figure out the correct compare values and jump conditions.
6363 // Handle the first compare/branch as a special case because it might
6364 // jump to the default case.
6365 DCHECK_GT(num_entries, 2u);
6366 Condition first_condition;
6367 uint32_t index;
6368 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6369 if (lower_bound != 0) {
6370 first_condition = kLess;
6371 __ cmpl(value_reg_in, Immediate(lower_bound));
6372 __ j(first_condition, codegen_->GetLabelOf(default_block));
6373 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6374
6375 index = 1;
6376 } else {
6377 // Handle all the compare/jumps below.
6378 first_condition = kBelow;
6379 index = 0;
6380 }
6381
6382 // Handle the rest of the compare/jumps.
6383 for (; index + 1 < num_entries; index += 2) {
6384 int32_t compare_to_value = lower_bound + index + 1;
6385 __ cmpl(value_reg_in, Immediate(compare_to_value));
6386 // Jump to successors[index] if value < case_value[index].
6387 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6388 // Jump to successors[index + 1] if value == case_value[index + 1].
6389 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6390 }
6391
6392 if (index != num_entries) {
6393 // There are an odd number of entries. Handle the last one.
6394 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006395 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006396 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6397 }
6398
6399 // And the default for any other value.
6400 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6401 __ jmp(codegen_->GetLabelOf(default_block));
6402 }
6403 return;
6404 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006405
6406 // Remove the bias, if needed.
6407 Register value_reg_out = value_reg_in.AsRegister();
6408 if (lower_bound != 0) {
6409 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6410 value_reg_out = temp_reg.AsRegister();
6411 }
6412 CpuRegister value_reg(value_reg_out);
6413
6414 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006415 __ cmpl(value_reg, Immediate(num_entries - 1));
6416 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006417
Mark Mendell9c86b482015-09-18 13:36:07 -04006418 // We are in the range of the table.
6419 // Load the address of the jump table in the constant area.
6420 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006421
Mark Mendell9c86b482015-09-18 13:36:07 -04006422 // Load the (signed) offset from the jump table.
6423 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6424
6425 // Add the offset to the address of the table base.
6426 __ addq(temp_reg, base_reg);
6427
6428 // And jump.
6429 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006430}
6431
Mark Mendell92e83bf2015-05-07 11:25:03 -04006432void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6433 if (value == 0) {
6434 __ xorl(dest, dest);
6435 } else if (value > 0 && IsInt<32>(value)) {
6436 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
6437 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6438 } else {
6439 __ movq(dest, Immediate(value));
6440 }
6441}
6442
Mark Mendellcfa410b2015-05-25 16:02:44 -04006443void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6444 DCHECK(dest.IsDoubleStackSlot());
6445 if (IsInt<32>(value)) {
6446 // Can move directly as an int32 constant.
6447 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6448 Immediate(static_cast<int32_t>(value)));
6449 } else {
6450 Load64BitValue(CpuRegister(TMP), value);
6451 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6452 }
6453}
6454
Mark Mendell9c86b482015-09-18 13:36:07 -04006455/**
6456 * Class to handle late fixup of offsets into constant area.
6457 */
6458class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6459 public:
6460 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6461 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6462
6463 protected:
6464 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6465
6466 CodeGeneratorX86_64* codegen_;
6467
6468 private:
6469 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6470 // Patch the correct offset for the instruction. We use the address of the
6471 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6472 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6473 int32_t relative_position = constant_offset - pos;
6474
6475 // Patch in the right value.
6476 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6477 }
6478
6479 // Location in constant area that the fixup refers to.
6480 size_t offset_into_constant_area_;
6481};
6482
6483/**
6484 t * Class to handle late fixup of offsets to a jump table that will be created in the
6485 * constant area.
6486 */
6487class JumpTableRIPFixup : public RIPFixup {
6488 public:
6489 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6490 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6491
6492 void CreateJumpTable() {
6493 X86_64Assembler* assembler = codegen_->GetAssembler();
6494
6495 // Ensure that the reference to the jump table has the correct offset.
6496 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6497 SetOffset(offset_in_constant_table);
6498
6499 // Compute the offset from the start of the function to this jump table.
6500 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6501
6502 // Populate the jump table with the correct values for the jump table.
6503 int32_t num_entries = switch_instr_->GetNumEntries();
6504 HBasicBlock* block = switch_instr_->GetBlock();
6505 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6506 // The value that we want is the target offset - the position of the table.
6507 for (int32_t i = 0; i < num_entries; i++) {
6508 HBasicBlock* b = successors[i];
6509 Label* l = codegen_->GetLabelOf(b);
6510 DCHECK(l->IsBound());
6511 int32_t offset_to_block = l->Position() - current_table_offset;
6512 assembler->AppendInt32(offset_to_block);
6513 }
6514 }
6515
6516 private:
6517 const HPackedSwitch* switch_instr_;
6518};
6519
Mark Mendellf55c3e02015-03-26 21:07:46 -04006520void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6521 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006522 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006523 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6524 // 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 -04006525 assembler->Align(4, 0);
6526 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006527
6528 // Populate any jump tables.
6529 for (auto jump_table : fixups_to_jump_tables_) {
6530 jump_table->CreateJumpTable();
6531 }
6532
6533 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006534 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006535 }
6536
6537 // And finish up.
6538 CodeGenerator::Finalize(allocator);
6539}
6540
Mark Mendellf55c3e02015-03-26 21:07:46 -04006541Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6542 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6543 return Address::RIP(fixup);
6544}
6545
6546Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6547 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6548 return Address::RIP(fixup);
6549}
6550
6551Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6552 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6553 return Address::RIP(fixup);
6554}
6555
6556Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6557 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6558 return Address::RIP(fixup);
6559}
6560
Andreas Gampe85b62f22015-09-09 13:15:38 -07006561// TODO: trg as memory.
6562void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6563 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006564 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006565 return;
6566 }
6567
6568 DCHECK_NE(type, Primitive::kPrimVoid);
6569
6570 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6571 if (trg.Equals(return_loc)) {
6572 return;
6573 }
6574
6575 // Let the parallel move resolver take care of all of this.
6576 HParallelMove parallel_move(GetGraph()->GetArena());
6577 parallel_move.AddMove(return_loc, trg, type, nullptr);
6578 GetMoveResolver()->EmitNativeCode(&parallel_move);
6579}
6580
Mark Mendell9c86b482015-09-18 13:36:07 -04006581Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6582 // Create a fixup to be used to create and address the jump table.
6583 JumpTableRIPFixup* table_fixup =
6584 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6585
6586 // We have to populate the jump tables.
6587 fixups_to_jump_tables_.push_back(table_fixup);
6588 return Address::RIP(table_fixup);
6589}
6590
Mark Mendellea5af682015-10-22 17:35:49 -04006591void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6592 const Address& addr_high,
6593 int64_t v,
6594 HInstruction* instruction) {
6595 if (IsInt<32>(v)) {
6596 int32_t v_32 = v;
6597 __ movq(addr_low, Immediate(v_32));
6598 MaybeRecordImplicitNullCheck(instruction);
6599 } else {
6600 // Didn't fit in a register. Do it in pieces.
6601 int32_t low_v = Low32Bits(v);
6602 int32_t high_v = High32Bits(v);
6603 __ movl(addr_low, Immediate(low_v));
6604 MaybeRecordImplicitNullCheck(instruction);
6605 __ movl(addr_high, Immediate(high_v));
6606 }
6607}
6608
Roland Levillain4d027112015-07-01 15:41:14 +01006609#undef __
6610
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006611} // namespace x86_64
6612} // namespace art