blob: 44a51ea6e23e537cd8e9216da2f54e9cb29258d1 [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 Levillain0d5a2812015-11-13 10:07:31 +0000459// Slow path generating a read barrier for a heap reference.
460class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
461 public:
462 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
463 Location out,
464 Location ref,
465 Location obj,
466 uint32_t offset,
467 Location index)
468 : instruction_(instruction),
469 out_(out),
470 ref_(ref),
471 obj_(obj),
472 offset_(offset),
473 index_(index) {
474 DCHECK(kEmitCompilerReadBarrier);
475 // If `obj` is equal to `out` or `ref`, it means the initial
476 // object has been overwritten by (or after) the heap object
477 // reference load to be instrumented, e.g.:
478 //
479 // __ movl(out, Address(out, offset));
480 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
481 //
482 // In that case, we have lost the information about the original
483 // object, and the emitted read barrier cannot work properly.
484 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
485 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
486}
487
488 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
489 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
490 LocationSummary* locations = instruction_->GetLocations();
491 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
492 DCHECK(locations->CanCall());
493 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
494 DCHECK(!instruction_->IsInvoke() ||
495 (instruction_->IsInvokeStaticOrDirect() &&
496 instruction_->GetLocations()->Intrinsified()));
497
498 __ Bind(GetEntryLabel());
499 SaveLiveRegisters(codegen, locations);
500
501 // We may have to change the index's value, but as `index_` is a
502 // constant member (like other "inputs" of this slow path),
503 // introduce a copy of it, `index`.
504 Location index = index_;
505 if (index_.IsValid()) {
506 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
507 if (instruction_->IsArrayGet()) {
508 // Compute real offset and store it in index_.
509 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
510 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
511 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
512 // We are about to change the value of `index_reg` (see the
513 // calls to art::x86_64::X86_64Assembler::shll and
514 // art::x86_64::X86_64Assembler::AddImmediate below), but it
515 // has not been saved by the previous call to
516 // art::SlowPathCode::SaveLiveRegisters, as it is a
517 // callee-save register --
518 // art::SlowPathCode::SaveLiveRegisters does not consider
519 // callee-save registers, as it has been designed with the
520 // assumption that callee-save registers are supposed to be
521 // handled by the called function. So, as a callee-save
522 // register, `index_reg` _would_ eventually be saved onto
523 // the stack, but it would be too late: we would have
524 // changed its value earlier. Therefore, we manually save
525 // it here into another freely available register,
526 // `free_reg`, chosen of course among the caller-save
527 // registers (as a callee-save `free_reg` register would
528 // exhibit the same problem).
529 //
530 // Note we could have requested a temporary register from
531 // the register allocator instead; but we prefer not to, as
532 // this is a slow path, and we know we can find a
533 // caller-save register that is available.
534 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
535 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
536 index_reg = free_reg;
537 index = Location::RegisterLocation(index_reg);
538 } else {
539 // The initial register stored in `index_` has already been
540 // saved in the call to art::SlowPathCode::SaveLiveRegisters
541 // (as it is not a callee-save register), so we can freely
542 // use it.
543 }
544 // Shifting the index value contained in `index_reg` by the
545 // scale factor (2) cannot overflow in practice, as the
546 // runtime is unable to allocate object arrays with a size
547 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
548 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
549 static_assert(
550 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
551 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
552 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
553 } else {
554 DCHECK(instruction_->IsInvoke());
555 DCHECK(instruction_->GetLocations()->Intrinsified());
556 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
557 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
558 << instruction_->AsInvoke()->GetIntrinsic();
559 DCHECK_EQ(offset_, 0U);
560 DCHECK(index_.IsRegister());
561 }
562 }
563
564 // We're moving two or three locations to locations that could
565 // overlap, so we need a parallel move resolver.
566 InvokeRuntimeCallingConvention calling_convention;
567 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
568 parallel_move.AddMove(ref_,
569 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
570 Primitive::kPrimNot,
571 nullptr);
572 parallel_move.AddMove(obj_,
573 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
574 Primitive::kPrimNot,
575 nullptr);
576 if (index.IsValid()) {
577 parallel_move.AddMove(index,
578 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
579 Primitive::kPrimInt,
580 nullptr);
581 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
582 } else {
583 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
584 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
585 }
586 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
587 instruction_,
588 instruction_->GetDexPc(),
589 this);
590 CheckEntrypointTypes<
591 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
592 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
593
594 RestoreLiveRegisters(codegen, locations);
595 __ jmp(GetExitLabel());
596 }
597
598 const char* GetDescription() const OVERRIDE {
599 return "ReadBarrierForHeapReferenceSlowPathX86_64";
600 }
601
602 private:
603 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
604 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
605 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
606 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
607 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
608 return static_cast<CpuRegister>(i);
609 }
610 }
611 // We shall never fail to find a free caller-save register, as
612 // there are more than two core caller-save registers on x86-64
613 // (meaning it is possible to find one which is different from
614 // `ref` and `obj`).
615 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
616 LOG(FATAL) << "Could not find a free caller-save register";
617 UNREACHABLE();
618 }
619
620 HInstruction* const instruction_;
621 const Location out_;
622 const Location ref_;
623 const Location obj_;
624 const uint32_t offset_;
625 // An additional location containing an index to an array.
626 // Only used for HArrayGet and the UnsafeGetObject &
627 // UnsafeGetObjectVolatile intrinsics.
628 const Location index_;
629
630 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
631};
632
633// Slow path generating a read barrier for a GC root.
634class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
635 public:
636 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
637 : instruction_(instruction), out_(out), root_(root) {}
638
639 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
640 LocationSummary* locations = instruction_->GetLocations();
641 DCHECK(locations->CanCall());
642 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
643 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
644
645 __ Bind(GetEntryLabel());
646 SaveLiveRegisters(codegen, locations);
647
648 InvokeRuntimeCallingConvention calling_convention;
649 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
650 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
651 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
652 instruction_,
653 instruction_->GetDexPc(),
654 this);
655 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
656 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
657
658 RestoreLiveRegisters(codegen, locations);
659 __ jmp(GetExitLabel());
660 }
661
662 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
663
664 private:
665 HInstruction* const instruction_;
666 const Location out_;
667 const Location root_;
668
669 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
670};
671
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100672#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100673#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100674
Roland Levillain4fa13f62015-07-06 18:11:54 +0100675inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700676 switch (cond) {
677 case kCondEQ: return kEqual;
678 case kCondNE: return kNotEqual;
679 case kCondLT: return kLess;
680 case kCondLE: return kLessEqual;
681 case kCondGT: return kGreater;
682 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700683 case kCondB: return kBelow;
684 case kCondBE: return kBelowEqual;
685 case kCondA: return kAbove;
686 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700687 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100688 LOG(FATAL) << "Unreachable";
689 UNREACHABLE();
690}
691
Aart Bike9f37602015-10-09 11:15:55 -0700692// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100693inline Condition X86_64FPCondition(IfCondition cond) {
694 switch (cond) {
695 case kCondEQ: return kEqual;
696 case kCondNE: return kNotEqual;
697 case kCondLT: return kBelow;
698 case kCondLE: return kBelowEqual;
699 case kCondGT: return kAbove;
700 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700701 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100702 };
703 LOG(FATAL) << "Unreachable";
704 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700705}
706
Vladimir Markodc151b22015-10-15 18:02:30 +0100707HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
708 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
709 MethodReference target_method ATTRIBUTE_UNUSED) {
710 switch (desired_dispatch_info.code_ptr_location) {
711 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
712 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
713 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
714 return HInvokeStaticOrDirect::DispatchInfo {
715 desired_dispatch_info.method_load_kind,
716 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
717 desired_dispatch_info.method_load_data,
718 0u
719 };
720 default:
721 return desired_dispatch_info;
722 }
723}
724
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800725void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100726 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800727 // All registers are assumed to be correctly set up.
728
Vladimir Marko58155012015-08-19 12:49:41 +0000729 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
730 switch (invoke->GetMethodLoadKind()) {
731 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
732 // temp = thread->string_init_entrypoint
733 __ gs()->movl(temp.AsRegister<CpuRegister>(),
734 Address::Absolute(invoke->GetStringInitOffset(), true));
735 break;
736 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000737 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000738 break;
739 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
740 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
741 break;
742 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
743 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
744 method_patches_.emplace_back(invoke->GetTargetMethod());
745 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
746 break;
747 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000748 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
749 invoke->GetDexCacheArrayOffset());
Vladimir Marko58155012015-08-19 12:49:41 +0000750 __ movq(temp.AsRegister<CpuRegister>(),
751 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
752 // Bind the label at the end of the "movl" insn.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000753 __ Bind(&pc_relative_dex_cache_patches_.back().label);
Vladimir Marko58155012015-08-19 12:49:41 +0000754 break;
755 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000756 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000757 Register method_reg;
758 CpuRegister reg = temp.AsRegister<CpuRegister>();
759 if (current_method.IsRegister()) {
760 method_reg = current_method.AsRegister<Register>();
761 } else {
762 DCHECK(invoke->GetLocations()->Intrinsified());
763 DCHECK(!current_method.IsValid());
764 method_reg = reg.AsRegister();
765 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
766 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000767 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100768 __ movq(reg,
769 Address(CpuRegister(method_reg),
770 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000771 // temp = temp[index_in_cache]
772 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
773 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
774 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100775 }
Vladimir Marko58155012015-08-19 12:49:41 +0000776 }
777
778 switch (invoke->GetCodePtrLocation()) {
779 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
780 __ call(&frame_entry_label_);
781 break;
782 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
783 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
784 Label* label = &relative_call_patches_.back().label;
785 __ call(label); // Bind to the patch label, override at link time.
786 __ Bind(label); // Bind the label at the end of the "call" insn.
787 break;
788 }
789 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
790 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100791 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
792 LOG(FATAL) << "Unsupported";
793 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000794 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
795 // (callee_method + offset_of_quick_compiled_code)()
796 __ call(Address(callee_method.AsRegister<CpuRegister>(),
797 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
798 kX86_64WordSize).SizeValue()));
799 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000800 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800801
802 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800803}
804
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000805void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
806 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
807 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
808 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000809
810 // Use the calling convention instead of the location of the receiver, as
811 // intrinsics may have put the receiver in a different register. In the intrinsics
812 // slow path, the arguments have been moved to the right place, so here we are
813 // guaranteed that the receiver is the first register of the calling convention.
814 InvokeDexCallingConvention calling_convention;
815 Register receiver = calling_convention.GetRegisterAt(0);
816
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000817 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000818 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000819 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000820 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000821 // Instead of simply (possibly) unpoisoning `temp` here, we should
822 // emit a read barrier for the previous class reference load.
823 // However this is not required in practice, as this is an
824 // intermediate/temporary reference and because the current
825 // concurrent copying collector keeps the from-space memory
826 // intact/accessible until the end of the marking phase (the
827 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000828 __ MaybeUnpoisonHeapReference(temp);
829 // temp = temp->GetMethodAt(method_offset);
830 __ movq(temp, Address(temp, method_offset));
831 // call temp->GetEntryPoint();
832 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
833 kX86_64WordSize).SizeValue()));
834}
835
Vladimir Marko58155012015-08-19 12:49:41 +0000836void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
837 DCHECK(linker_patches->empty());
838 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000839 method_patches_.size() +
840 relative_call_patches_.size() +
841 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000842 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000843 // The label points to the end of the "movl" insn but the literal offset for method
844 // patch needs to point to the embedded constant which occupies the last 4 bytes.
845 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000846 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000847 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000848 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
849 info.target_method.dex_file,
850 info.target_method.dex_method_index));
851 }
852 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000853 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000854 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
855 info.target_method.dex_file,
856 info.target_method.dex_method_index));
857 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000858 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
859 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000860 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
861 &info.target_dex_file,
862 info.label.Position(),
863 info.element_offset));
864 }
865}
866
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100867void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100868 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100869}
870
871void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100872 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100873}
874
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100875size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
876 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
877 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100878}
879
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100880size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
881 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
882 return kX86_64WordSize;
883}
884
885size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
886 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
887 return kX86_64WordSize;
888}
889
890size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
891 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
892 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100893}
894
Calin Juravle175dc732015-08-25 15:42:32 +0100895void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
896 HInstruction* instruction,
897 uint32_t dex_pc,
898 SlowPathCode* slow_path) {
899 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
900 instruction,
901 dex_pc,
902 slow_path);
903}
904
905void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100906 HInstruction* instruction,
907 uint32_t dex_pc,
908 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100909 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100910 __ gs()->call(Address::Absolute(entry_point_offset, true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100911 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100912}
913
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000914static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000915// Use a fake return address register to mimic Quick.
916static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400917CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000918 const X86_64InstructionSetFeatures& isa_features,
919 const CompilerOptions& compiler_options,
920 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000921 : CodeGenerator(graph,
922 kNumberOfCpuRegisters,
923 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000924 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000925 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
926 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000927 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000928 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
929 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100930 compiler_options,
931 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100932 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100933 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000934 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400935 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400936 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000937 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100938 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
939 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000940 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -0400941 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000942 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
943}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100944
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100945InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
946 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100947 : HGraphVisitor(graph),
948 assembler_(codegen->GetAssembler()),
949 codegen_(codegen) {}
950
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100951Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100952 switch (type) {
953 case Primitive::kPrimLong:
954 case Primitive::kPrimByte:
955 case Primitive::kPrimBoolean:
956 case Primitive::kPrimChar:
957 case Primitive::kPrimShort:
958 case Primitive::kPrimInt:
959 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100960 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100961 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100962 }
963
964 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100965 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100966 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100967 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100968 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100969
970 case Primitive::kPrimVoid:
971 LOG(FATAL) << "Unreachable type " << type;
972 }
973
Roland Levillain0d5a2812015-11-13 10:07:31 +0000974 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100975}
976
Nicolas Geoffray98893962015-01-21 12:32:32 +0000977void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100978 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100979 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100980
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000981 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100982 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000983
Nicolas Geoffray98893962015-01-21 12:32:32 +0000984 if (is_baseline) {
985 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
986 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
987 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000988 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
989 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
990 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000991 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100992}
993
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100994static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100995 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100996}
David Srbecky9d8606d2015-04-12 09:35:32 +0100997
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100998static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100999 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001000}
1001
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001002void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001003 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001004 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001005 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001006 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001007 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001008
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001009 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001010 __ testq(CpuRegister(RAX), Address(
1011 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001012 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001013 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001014
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001015 if (HasEmptyFrame()) {
1016 return;
1017 }
1018
Nicolas Geoffray98893962015-01-21 12:32:32 +00001019 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001020 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001021 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001022 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001023 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1024 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001025 }
1026 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001027
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001028 int adjust = GetFrameSize() - GetCoreSpillSize();
1029 __ subq(CpuRegister(RSP), Immediate(adjust));
1030 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001031 uint32_t xmm_spill_location = GetFpuSpillStart();
1032 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001033
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001034 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1035 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001036 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1037 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1038 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001039 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001040 }
1041
Mathieu Chartiere401d142015-04-22 13:56:20 -07001042 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001043 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001044}
1045
1046void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001047 __ cfi().RememberState();
1048 if (!HasEmptyFrame()) {
1049 uint32_t xmm_spill_location = GetFpuSpillStart();
1050 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1051 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1052 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1053 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1054 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1055 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1056 }
1057 }
1058
1059 int adjust = GetFrameSize() - GetCoreSpillSize();
1060 __ addq(CpuRegister(RSP), Immediate(adjust));
1061 __ cfi().AdjustCFAOffset(-adjust);
1062
1063 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1064 Register reg = kCoreCalleeSaves[i];
1065 if (allocated_registers_.ContainsCoreRegister(reg)) {
1066 __ popq(CpuRegister(reg));
1067 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1068 __ cfi().Restore(DWARFReg(reg));
1069 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001070 }
1071 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001072 __ ret();
1073 __ cfi().RestoreState();
1074 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001075}
1076
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001077void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1078 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001079}
1080
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
1082 switch (load->GetType()) {
1083 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001085 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001086
1087 case Primitive::kPrimInt:
1088 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001089 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001091
1092 case Primitive::kPrimBoolean:
1093 case Primitive::kPrimByte:
1094 case Primitive::kPrimChar:
1095 case Primitive::kPrimShort:
1096 case Primitive::kPrimVoid:
1097 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -07001098 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001099 }
1100
1101 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -07001102 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001103}
1104
1105void CodeGeneratorX86_64::Move(Location destination, Location source) {
1106 if (source.Equals(destination)) {
1107 return;
1108 }
1109 if (destination.IsRegister()) {
1110 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001111 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001112 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001113 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001114 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001115 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001116 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001117 } else {
1118 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001119 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001120 Address(CpuRegister(RSP), source.GetStackIndex()));
1121 }
1122 } else if (destination.IsFpuRegister()) {
1123 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001124 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001125 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001126 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001127 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001128 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001129 Address(CpuRegister(RSP), source.GetStackIndex()));
1130 } else {
1131 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001132 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001133 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001134 }
1135 } else if (destination.IsStackSlot()) {
1136 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001137 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001138 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001139 } else if (source.IsFpuRegister()) {
1140 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001141 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001142 } else if (source.IsConstant()) {
1143 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001144 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001145 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001146 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001147 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001148 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1149 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001150 }
1151 } else {
1152 DCHECK(destination.IsDoubleStackSlot());
1153 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001154 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001155 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001156 } else if (source.IsFpuRegister()) {
1157 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001158 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001159 } else if (source.IsConstant()) {
1160 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001161 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001162 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001163 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001164 } else {
1165 DCHECK(constant->IsLongConstant());
1166 value = constant->AsLongConstant()->GetValue();
1167 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001168 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001169 } else {
1170 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001171 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1172 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001173 }
1174 }
1175}
1176
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001177void CodeGeneratorX86_64::Move(HInstruction* instruction,
1178 Location location,
1179 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001180 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001181 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -07001182 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001183 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001184 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001185 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001186 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001187 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1188 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001189 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001190 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001191 } else if (location.IsStackSlot()) {
1192 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
1193 } else {
1194 DCHECK(location.IsConstant());
1195 DCHECK_EQ(location.GetConstant(), const_to_move);
1196 }
1197 } else if (const_to_move->IsLongConstant()) {
1198 int64_t value = const_to_move->AsLongConstant()->GetValue();
1199 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04001200 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001201 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -04001202 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001203 } else {
1204 DCHECK(location.IsConstant());
1205 DCHECK_EQ(location.GetConstant(), const_to_move);
1206 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001207 }
Roland Levillain476df552014-10-09 17:51:36 +01001208 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001209 switch (instruction->GetType()) {
1210 case Primitive::kPrimBoolean:
1211 case Primitive::kPrimByte:
1212 case Primitive::kPrimChar:
1213 case Primitive::kPrimShort:
1214 case Primitive::kPrimInt:
1215 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001216 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001217 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
1218 break;
1219
1220 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +00001222 Move(location,
1223 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001224 break;
1225
1226 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001229 } else if (instruction->IsTemporary()) {
1230 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
1231 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001232 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001233 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001234 switch (instruction->GetType()) {
1235 case Primitive::kPrimBoolean:
1236 case Primitive::kPrimByte:
1237 case Primitive::kPrimChar:
1238 case Primitive::kPrimShort:
1239 case Primitive::kPrimInt:
1240 case Primitive::kPrimNot:
1241 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 case Primitive::kPrimFloat:
1243 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001244 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001245 break;
1246
1247 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001249 }
1250 }
1251}
1252
Calin Juravle175dc732015-08-25 15:42:32 +01001253void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1254 DCHECK(location.IsRegister());
1255 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1256}
1257
Calin Juravlee460d1d2015-09-29 04:52:17 +01001258void CodeGeneratorX86_64::MoveLocation(
1259 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1260 Move(dst, src);
1261}
1262
1263void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1264 if (location.IsRegister()) {
1265 locations->AddTemp(location);
1266 } else {
1267 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1268 }
1269}
1270
David Brazdilfc6a86a2015-06-26 10:33:45 +00001271void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001272 DCHECK(!successor->IsExitBlock());
1273
1274 HBasicBlock* block = got->GetBlock();
1275 HInstruction* previous = got->GetPrevious();
1276
1277 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001278 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001279 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1280 return;
1281 }
1282
1283 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1284 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1285 }
1286 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001287 __ jmp(codegen_->GetLabelOf(successor));
1288 }
1289}
1290
David Brazdilfc6a86a2015-06-26 10:33:45 +00001291void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1292 got->SetLocations(nullptr);
1293}
1294
1295void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1296 HandleGoto(got, got->GetSuccessor());
1297}
1298
1299void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1300 try_boundary->SetLocations(nullptr);
1301}
1302
1303void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1304 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1305 if (!successor->IsExitBlock()) {
1306 HandleGoto(try_boundary, successor);
1307 }
1308}
1309
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001310void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1311 exit->SetLocations(nullptr);
1312}
1313
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001314void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001315}
1316
Mark Mendellc4701932015-04-10 13:18:51 -04001317void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
1318 Label* true_label,
1319 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001320 if (cond->IsFPConditionTrueIfNaN()) {
1321 __ j(kUnordered, true_label);
1322 } else if (cond->IsFPConditionFalseIfNaN()) {
1323 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001324 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001325 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001326}
1327
David Brazdil0debae72015-11-12 18:37:00 +00001328void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1329 Label* true_target_in,
1330 Label* false_target_in) {
1331 // Generated branching requires both targets to be explicit. If either of the
1332 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1333 Label fallthrough_target;
1334 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1335 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1336
Mark Mendellc4701932015-04-10 13:18:51 -04001337 LocationSummary* locations = condition->GetLocations();
1338 Location left = locations->InAt(0);
1339 Location right = locations->InAt(1);
1340
Mark Mendellc4701932015-04-10 13:18:51 -04001341 Primitive::Type type = condition->InputAt(0)->GetType();
1342 switch (type) {
1343 case Primitive::kPrimLong: {
1344 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1345 if (right.IsConstant()) {
1346 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1347 if (IsInt<32>(value)) {
1348 if (value == 0) {
1349 __ testq(left_reg, left_reg);
1350 } else {
1351 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1352 }
1353 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001354 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001355 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1356 }
1357 } else if (right.IsDoubleStackSlot()) {
1358 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1359 } else {
1360 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1361 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001362 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001363 break;
1364 }
1365 case Primitive::kPrimFloat: {
1366 if (right.IsFpuRegister()) {
1367 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1368 } else if (right.IsConstant()) {
1369 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1370 codegen_->LiteralFloatAddress(
1371 right.GetConstant()->AsFloatConstant()->GetValue()));
1372 } else {
1373 DCHECK(right.IsStackSlot());
1374 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1375 Address(CpuRegister(RSP), right.GetStackIndex()));
1376 }
1377 GenerateFPJumps(condition, true_target, false_target);
1378 break;
1379 }
1380 case Primitive::kPrimDouble: {
1381 if (right.IsFpuRegister()) {
1382 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1383 } else if (right.IsConstant()) {
1384 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1385 codegen_->LiteralDoubleAddress(
1386 right.GetConstant()->AsDoubleConstant()->GetValue()));
1387 } else {
1388 DCHECK(right.IsDoubleStackSlot());
1389 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1390 Address(CpuRegister(RSP), right.GetStackIndex()));
1391 }
1392 GenerateFPJumps(condition, true_target, false_target);
1393 break;
1394 }
1395 default:
1396 LOG(FATAL) << "Unexpected condition type " << type;
1397 }
1398
David Brazdil0debae72015-11-12 18:37:00 +00001399 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001400 __ jmp(false_target);
1401 }
David Brazdil0debae72015-11-12 18:37:00 +00001402
1403 if (fallthrough_target.IsLinked()) {
1404 __ Bind(&fallthrough_target);
1405 }
Mark Mendellc4701932015-04-10 13:18:51 -04001406}
1407
David Brazdil0debae72015-11-12 18:37:00 +00001408static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1409 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1410 // are set only strictly before `branch`. We can't use the eflags on long
1411 // conditions if they are materialized due to the complex branching.
1412 return cond->IsCondition() &&
1413 cond->GetNext() == branch &&
1414 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1415}
1416
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001417void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001418 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001419 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001420 Label* false_target) {
1421 HInstruction* cond = instruction->InputAt(condition_input_index);
1422
1423 if (true_target == nullptr && false_target == nullptr) {
1424 // Nothing to do. The code always falls through.
1425 return;
1426 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001427 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001428 if (cond->AsIntConstant()->IsOne()) {
1429 if (true_target != nullptr) {
1430 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001431 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001432 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001433 DCHECK(cond->AsIntConstant()->IsZero());
1434 if (false_target != nullptr) {
1435 __ jmp(false_target);
1436 }
1437 }
1438 return;
1439 }
1440
1441 // The following code generates these patterns:
1442 // (1) true_target == nullptr && false_target != nullptr
1443 // - opposite condition true => branch to false_target
1444 // (2) true_target != nullptr && false_target == nullptr
1445 // - condition true => branch to true_target
1446 // (3) true_target != nullptr && false_target != nullptr
1447 // - condition true => branch to true_target
1448 // - branch to false_target
1449 if (IsBooleanValueOrMaterializedCondition(cond)) {
1450 if (AreEflagsSetFrom(cond, instruction)) {
1451 if (true_target == nullptr) {
1452 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1453 } else {
1454 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1455 }
1456 } else {
1457 // Materialized condition, compare against 0.
1458 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1459 if (lhs.IsRegister()) {
1460 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1461 } else {
1462 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1463 }
1464 if (true_target == nullptr) {
1465 __ j(kEqual, false_target);
1466 } else {
1467 __ j(kNotEqual, true_target);
1468 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001469 }
1470 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001471 // Condition has not been materialized, use its inputs as the
1472 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001473 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001474
David Brazdil0debae72015-11-12 18:37:00 +00001475 // If this is a long or FP comparison that has been folded into
1476 // the HCondition, generate the comparison directly.
1477 Primitive::Type type = condition->InputAt(0)->GetType();
1478 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1479 GenerateCompareTestAndBranch(condition, true_target, false_target);
1480 return;
1481 }
1482
1483 Location lhs = condition->GetLocations()->InAt(0);
1484 Location rhs = condition->GetLocations()->InAt(1);
1485 if (rhs.IsRegister()) {
1486 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1487 } else if (rhs.IsConstant()) {
1488 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1489 if (constant == 0) {
1490 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001491 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001492 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001493 }
1494 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001495 __ cmpl(lhs.AsRegister<CpuRegister>(),
1496 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1497 }
1498 if (true_target == nullptr) {
1499 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1500 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001501 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001502 }
Dave Allison20dfc792014-06-16 20:44:29 -07001503 }
David Brazdil0debae72015-11-12 18:37:00 +00001504
1505 // If neither branch falls through (case 3), the conditional branch to `true_target`
1506 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1507 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001508 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001509 }
1510}
1511
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001512void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1514 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001515 locations->SetInAt(0, Location::Any());
1516 }
1517}
1518
1519void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001520 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1521 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1522 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1523 nullptr : codegen_->GetLabelOf(true_successor);
1524 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1525 nullptr : codegen_->GetLabelOf(false_successor);
1526 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001527}
1528
1529void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1530 LocationSummary* locations = new (GetGraph()->GetArena())
1531 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001532 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001533 locations->SetInAt(0, Location::Any());
1534 }
1535}
1536
1537void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001538 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001539 DeoptimizationSlowPathX86_64(deoptimize);
1540 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001541 GenerateTestAndBranch(deoptimize,
1542 /* condition_input_index */ 0,
1543 slow_path->GetEntryLabel(),
1544 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001545}
1546
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001547void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1548 local->SetLocations(nullptr);
1549}
1550
1551void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1552 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1553}
1554
1555void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1556 local->SetLocations(nullptr);
1557}
1558
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001559void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001560 // Nothing to do, this is driven by the code generator.
1561}
1562
1563void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001564 LocationSummary* locations =
1565 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001566 switch (store->InputAt(1)->GetType()) {
1567 case Primitive::kPrimBoolean:
1568 case Primitive::kPrimByte:
1569 case Primitive::kPrimChar:
1570 case Primitive::kPrimShort:
1571 case Primitive::kPrimInt:
1572 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001573 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001574 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1575 break;
1576
1577 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001578 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001579 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1580 break;
1581
1582 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001583 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001584 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001585}
1586
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001587void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001588}
1589
Roland Levillain0d37cd02015-05-27 16:39:19 +01001590void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001591 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001592 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001593 // Handle the long/FP comparisons made in instruction simplification.
1594 switch (cond->InputAt(0)->GetType()) {
1595 case Primitive::kPrimLong:
1596 locations->SetInAt(0, Location::RequiresRegister());
1597 locations->SetInAt(1, Location::Any());
1598 break;
1599 case Primitive::kPrimFloat:
1600 case Primitive::kPrimDouble:
1601 locations->SetInAt(0, Location::RequiresFpuRegister());
1602 locations->SetInAt(1, Location::Any());
1603 break;
1604 default:
1605 locations->SetInAt(0, Location::RequiresRegister());
1606 locations->SetInAt(1, Location::Any());
1607 break;
1608 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001609 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001610 locations->SetOut(Location::RequiresRegister());
1611 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001612}
1613
Roland Levillain0d37cd02015-05-27 16:39:19 +01001614void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001615 if (!cond->NeedsMaterialization()) {
1616 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001617 }
Mark Mendellc4701932015-04-10 13:18:51 -04001618
1619 LocationSummary* locations = cond->GetLocations();
1620 Location lhs = locations->InAt(0);
1621 Location rhs = locations->InAt(1);
1622 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1623 Label true_label, false_label;
1624
1625 switch (cond->InputAt(0)->GetType()) {
1626 default:
1627 // Integer case.
1628
1629 // Clear output register: setcc only sets the low byte.
1630 __ xorl(reg, reg);
1631
1632 if (rhs.IsRegister()) {
1633 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1634 } else if (rhs.IsConstant()) {
1635 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1636 if (constant == 0) {
1637 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1638 } else {
1639 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1640 }
1641 } else {
1642 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1643 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001644 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001645 return;
1646 case Primitive::kPrimLong:
1647 // Clear output register: setcc only sets the low byte.
1648 __ xorl(reg, reg);
1649
1650 if (rhs.IsRegister()) {
1651 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1652 } else if (rhs.IsConstant()) {
1653 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1654 if (IsInt<32>(value)) {
1655 if (value == 0) {
1656 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1657 } else {
1658 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1659 }
1660 } else {
1661 // Value won't fit in an int.
1662 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1663 }
1664 } else {
1665 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1666 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001667 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001668 return;
1669 case Primitive::kPrimFloat: {
1670 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1671 if (rhs.IsConstant()) {
1672 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1673 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1674 } else if (rhs.IsStackSlot()) {
1675 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1676 } else {
1677 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1678 }
1679 GenerateFPJumps(cond, &true_label, &false_label);
1680 break;
1681 }
1682 case Primitive::kPrimDouble: {
1683 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1684 if (rhs.IsConstant()) {
1685 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1686 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1687 } else if (rhs.IsDoubleStackSlot()) {
1688 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1689 } else {
1690 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1691 }
1692 GenerateFPJumps(cond, &true_label, &false_label);
1693 break;
1694 }
1695 }
1696
1697 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001698 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001699
Roland Levillain4fa13f62015-07-06 18:11:54 +01001700 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001701 __ Bind(&false_label);
1702 __ xorl(reg, reg);
1703 __ jmp(&done_label);
1704
Roland Levillain4fa13f62015-07-06 18:11:54 +01001705 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001706 __ Bind(&true_label);
1707 __ movl(reg, Immediate(1));
1708 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001709}
1710
1711void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1712 VisitCondition(comp);
1713}
1714
1715void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1716 VisitCondition(comp);
1717}
1718
1719void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1720 VisitCondition(comp);
1721}
1722
1723void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1724 VisitCondition(comp);
1725}
1726
1727void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1728 VisitCondition(comp);
1729}
1730
1731void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1732 VisitCondition(comp);
1733}
1734
1735void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1736 VisitCondition(comp);
1737}
1738
1739void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1740 VisitCondition(comp);
1741}
1742
1743void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1744 VisitCondition(comp);
1745}
1746
1747void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1748 VisitCondition(comp);
1749}
1750
1751void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1752 VisitCondition(comp);
1753}
1754
1755void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1756 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001757}
1758
Aart Bike9f37602015-10-09 11:15:55 -07001759void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
1760 VisitCondition(comp);
1761}
1762
1763void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
1764 VisitCondition(comp);
1765}
1766
1767void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
1768 VisitCondition(comp);
1769}
1770
1771void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
1772 VisitCondition(comp);
1773}
1774
1775void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
1776 VisitCondition(comp);
1777}
1778
1779void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
1780 VisitCondition(comp);
1781}
1782
1783void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
1784 VisitCondition(comp);
1785}
1786
1787void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
1788 VisitCondition(comp);
1789}
1790
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001791void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001792 LocationSummary* locations =
1793 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001794 switch (compare->InputAt(0)->GetType()) {
1795 case Primitive::kPrimLong: {
1796 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001797 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1799 break;
1800 }
1801 case Primitive::kPrimFloat:
1802 case Primitive::kPrimDouble: {
1803 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001804 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001805 locations->SetOut(Location::RequiresRegister());
1806 break;
1807 }
1808 default:
1809 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1810 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001811}
1812
1813void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001814 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001815 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001816 Location left = locations->InAt(0);
1817 Location right = locations->InAt(1);
1818
Mark Mendell0c9497d2015-08-21 09:30:05 -04001819 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001820 Primitive::Type type = compare->InputAt(0)->GetType();
1821 switch (type) {
1822 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001823 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1824 if (right.IsConstant()) {
1825 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001826 if (IsInt<32>(value)) {
1827 if (value == 0) {
1828 __ testq(left_reg, left_reg);
1829 } else {
1830 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1831 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001832 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001833 // Value won't fit in an int.
1834 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001835 }
Mark Mendell40741f32015-04-20 22:10:34 -04001836 } else if (right.IsDoubleStackSlot()) {
1837 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001838 } else {
1839 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1840 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001841 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001842 }
1843 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001844 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1845 if (right.IsConstant()) {
1846 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1847 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1848 } else if (right.IsStackSlot()) {
1849 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1850 } else {
1851 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1852 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001853 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1854 break;
1855 }
1856 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001857 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1858 if (right.IsConstant()) {
1859 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1860 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1861 } else if (right.IsDoubleStackSlot()) {
1862 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1863 } else {
1864 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1865 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001866 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1867 break;
1868 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001869 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001870 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001871 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001872 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001873 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001874 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001875
Calin Juravle91debbc2014-11-26 19:01:09 +00001876 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001877 __ movl(out, Immediate(1));
1878 __ jmp(&done);
1879
1880 __ Bind(&less);
1881 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001882
1883 __ Bind(&done);
1884}
1885
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001886void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001887 LocationSummary* locations =
1888 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001889 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001890}
1891
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001892void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001893 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001894}
1895
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001896void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1897 LocationSummary* locations =
1898 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1899 locations->SetOut(Location::ConstantLocation(constant));
1900}
1901
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001902void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001903 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001904}
1905
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001906void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001907 LocationSummary* locations =
1908 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001909 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001910}
1911
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001912void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001913 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001914}
1915
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001916void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1917 LocationSummary* locations =
1918 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1919 locations->SetOut(Location::ConstantLocation(constant));
1920}
1921
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001922void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001923 // Will be generated at use site.
1924}
1925
1926void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1927 LocationSummary* locations =
1928 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1929 locations->SetOut(Location::ConstantLocation(constant));
1930}
1931
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001932void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1933 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001934 // Will be generated at use site.
1935}
1936
Calin Juravle27df7582015-04-17 19:12:31 +01001937void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1938 memory_barrier->SetLocations(nullptr);
1939}
1940
1941void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1942 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1943}
1944
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001945void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1946 ret->SetLocations(nullptr);
1947}
1948
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001949void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001950 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001951}
1952
1953void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001954 LocationSummary* locations =
1955 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001956 switch (ret->InputAt(0)->GetType()) {
1957 case Primitive::kPrimBoolean:
1958 case Primitive::kPrimByte:
1959 case Primitive::kPrimChar:
1960 case Primitive::kPrimShort:
1961 case Primitive::kPrimInt:
1962 case Primitive::kPrimNot:
1963 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001964 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001965 break;
1966
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001967 case Primitive::kPrimFloat:
1968 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001969 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001970 break;
1971
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001972 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001973 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001974 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001975}
1976
1977void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1978 if (kIsDebugBuild) {
1979 switch (ret->InputAt(0)->GetType()) {
1980 case Primitive::kPrimBoolean:
1981 case Primitive::kPrimByte:
1982 case Primitive::kPrimChar:
1983 case Primitive::kPrimShort:
1984 case Primitive::kPrimInt:
1985 case Primitive::kPrimNot:
1986 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001987 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001988 break;
1989
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001990 case Primitive::kPrimFloat:
1991 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001992 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001993 XMM0);
1994 break;
1995
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001996 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001997 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001998 }
1999 }
2000 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002001}
2002
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002003Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2004 switch (type) {
2005 case Primitive::kPrimBoolean:
2006 case Primitive::kPrimByte:
2007 case Primitive::kPrimChar:
2008 case Primitive::kPrimShort:
2009 case Primitive::kPrimInt:
2010 case Primitive::kPrimNot:
2011 case Primitive::kPrimLong:
2012 return Location::RegisterLocation(RAX);
2013
2014 case Primitive::kPrimVoid:
2015 return Location::NoLocation();
2016
2017 case Primitive::kPrimDouble:
2018 case Primitive::kPrimFloat:
2019 return Location::FpuRegisterLocation(XMM0);
2020 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002021
2022 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002023}
2024
2025Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2026 return Location::RegisterLocation(kMethodRegisterArgument);
2027}
2028
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002029Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002030 switch (type) {
2031 case Primitive::kPrimBoolean:
2032 case Primitive::kPrimByte:
2033 case Primitive::kPrimChar:
2034 case Primitive::kPrimShort:
2035 case Primitive::kPrimInt:
2036 case Primitive::kPrimNot: {
2037 uint32_t index = gp_index_++;
2038 stack_index_++;
2039 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002040 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002041 } else {
2042 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2043 }
2044 }
2045
2046 case Primitive::kPrimLong: {
2047 uint32_t index = gp_index_;
2048 stack_index_ += 2;
2049 if (index < calling_convention.GetNumberOfRegisters()) {
2050 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002051 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002052 } else {
2053 gp_index_ += 2;
2054 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2055 }
2056 }
2057
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002058 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002059 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002060 stack_index_++;
2061 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002062 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002063 } else {
2064 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2065 }
2066 }
2067
2068 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002069 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002070 stack_index_ += 2;
2071 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002072 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002073 } else {
2074 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2075 }
2076 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002077
2078 case Primitive::kPrimVoid:
2079 LOG(FATAL) << "Unexpected parameter type " << type;
2080 break;
2081 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002082 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002083}
2084
Calin Juravle175dc732015-08-25 15:42:32 +01002085void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2086 // The trampoline uses the same calling convention as dex calling conventions,
2087 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2088 // the method_idx.
2089 HandleInvoke(invoke);
2090}
2091
2092void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2093 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2094}
2095
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002096void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002097 // When we do not run baseline, explicit clinit checks triggered by static
2098 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2099 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002100
Mark Mendellfb8d2792015-03-31 22:16:59 -04002101 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002102 if (intrinsic.TryDispatch(invoke)) {
2103 return;
2104 }
2105
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002106 HandleInvoke(invoke);
2107}
2108
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002109static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2110 if (invoke->GetLocations()->Intrinsified()) {
2111 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2112 intrinsic.Dispatch(invoke);
2113 return true;
2114 }
2115 return false;
2116}
2117
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002118void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002119 // When we do not run baseline, explicit clinit checks triggered by static
2120 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2121 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002122
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002123 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2124 return;
2125 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002126
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002127 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002128 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002129 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002130 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002131}
2132
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002133void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002134 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002135 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002136}
2137
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002138void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002139 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002140 if (intrinsic.TryDispatch(invoke)) {
2141 return;
2142 }
2143
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002144 HandleInvoke(invoke);
2145}
2146
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002147void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002148 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2149 return;
2150 }
2151
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002152 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002153 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002154 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002155}
2156
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002157void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2158 HandleInvoke(invoke);
2159 // Add the hidden argument.
2160 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2161}
2162
2163void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2164 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002165 LocationSummary* locations = invoke->GetLocations();
2166 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2167 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002168 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2169 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002170 Location receiver = locations->InAt(0);
2171 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2172
Roland Levillain0d5a2812015-11-13 10:07:31 +00002173 // Set the hidden argument. This is safe to do this here, as RAX
2174 // won't be modified thereafter, before the `call` instruction.
2175 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002176 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002177
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002178 if (receiver.IsStackSlot()) {
2179 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002180 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002181 __ movl(temp, Address(temp, class_offset));
2182 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002183 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002184 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002185 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002186 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002187 // Instead of simply (possibly) unpoisoning `temp` here, we should
2188 // emit a read barrier for the previous class reference load.
2189 // However this is not required in practice, as this is an
2190 // intermediate/temporary reference and because the current
2191 // concurrent copying collector keeps the from-space memory
2192 // intact/accessible until the end of the marking phase (the
2193 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002194 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002195 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002196 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002197 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002198 __ call(Address(temp,
2199 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002200
2201 DCHECK(!codegen_->IsLeafMethod());
2202 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2203}
2204
Roland Levillain88cb1752014-10-20 16:36:47 +01002205void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2206 LocationSummary* locations =
2207 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2208 switch (neg->GetResultType()) {
2209 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002210 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002211 locations->SetInAt(0, Location::RequiresRegister());
2212 locations->SetOut(Location::SameAsFirstInput());
2213 break;
2214
Roland Levillain88cb1752014-10-20 16:36:47 +01002215 case Primitive::kPrimFloat:
2216 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002217 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002218 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002219 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002220 break;
2221
2222 default:
2223 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2224 }
2225}
2226
2227void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2228 LocationSummary* locations = neg->GetLocations();
2229 Location out = locations->Out();
2230 Location in = locations->InAt(0);
2231 switch (neg->GetResultType()) {
2232 case Primitive::kPrimInt:
2233 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002234 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002235 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002236 break;
2237
2238 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002239 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002240 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002242 break;
2243
Roland Levillain5368c212014-11-27 15:03:41 +00002244 case Primitive::kPrimFloat: {
2245 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002246 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002247 // Implement float negation with an exclusive or with value
2248 // 0x80000000 (mask for bit 31, representing the sign of a
2249 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002250 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002251 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002252 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002253 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002254
Roland Levillain5368c212014-11-27 15:03:41 +00002255 case Primitive::kPrimDouble: {
2256 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002257 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002258 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002259 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002260 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002261 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002262 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002263 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002264 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002265
2266 default:
2267 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2268 }
2269}
2270
Roland Levillaindff1f282014-11-05 14:15:05 +00002271void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2272 LocationSummary* locations =
2273 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2274 Primitive::Type result_type = conversion->GetResultType();
2275 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002276 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002277
David Brazdilb2bd1c52015-03-25 11:17:37 +00002278 // The Java language does not allow treating boolean as an integral type but
2279 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002280
Roland Levillaindff1f282014-11-05 14:15:05 +00002281 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002282 case Primitive::kPrimByte:
2283 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002284 case Primitive::kPrimBoolean:
2285 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002286 case Primitive::kPrimShort:
2287 case Primitive::kPrimInt:
2288 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002289 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002290 locations->SetInAt(0, Location::Any());
2291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2292 break;
2293
2294 default:
2295 LOG(FATAL) << "Unexpected type conversion from " << input_type
2296 << " to " << result_type;
2297 }
2298 break;
2299
Roland Levillain01a8d712014-11-14 16:27:39 +00002300 case Primitive::kPrimShort:
2301 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002302 case Primitive::kPrimBoolean:
2303 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002304 case Primitive::kPrimByte:
2305 case Primitive::kPrimInt:
2306 case Primitive::kPrimChar:
2307 // Processing a Dex `int-to-short' instruction.
2308 locations->SetInAt(0, Location::Any());
2309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2310 break;
2311
2312 default:
2313 LOG(FATAL) << "Unexpected type conversion from " << input_type
2314 << " to " << result_type;
2315 }
2316 break;
2317
Roland Levillain946e1432014-11-11 17:35:19 +00002318 case Primitive::kPrimInt:
2319 switch (input_type) {
2320 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002321 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002322 locations->SetInAt(0, Location::Any());
2323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2324 break;
2325
2326 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002327 // Processing a Dex `float-to-int' instruction.
2328 locations->SetInAt(0, Location::RequiresFpuRegister());
2329 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002330 break;
2331
Roland Levillain946e1432014-11-11 17:35:19 +00002332 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002333 // Processing a Dex `double-to-int' instruction.
2334 locations->SetInAt(0, Location::RequiresFpuRegister());
2335 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002336 break;
2337
2338 default:
2339 LOG(FATAL) << "Unexpected type conversion from " << input_type
2340 << " to " << result_type;
2341 }
2342 break;
2343
Roland Levillaindff1f282014-11-05 14:15:05 +00002344 case Primitive::kPrimLong:
2345 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002346 case Primitive::kPrimBoolean:
2347 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002348 case Primitive::kPrimByte:
2349 case Primitive::kPrimShort:
2350 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002351 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002352 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002353 // TODO: We would benefit from a (to-be-implemented)
2354 // Location::RegisterOrStackSlot requirement for this input.
2355 locations->SetInAt(0, Location::RequiresRegister());
2356 locations->SetOut(Location::RequiresRegister());
2357 break;
2358
2359 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002360 // Processing a Dex `float-to-long' instruction.
2361 locations->SetInAt(0, Location::RequiresFpuRegister());
2362 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002363 break;
2364
Roland Levillaindff1f282014-11-05 14:15:05 +00002365 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002366 // Processing a Dex `double-to-long' instruction.
2367 locations->SetInAt(0, Location::RequiresFpuRegister());
2368 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002369 break;
2370
2371 default:
2372 LOG(FATAL) << "Unexpected type conversion from " << input_type
2373 << " to " << result_type;
2374 }
2375 break;
2376
Roland Levillain981e4542014-11-14 11:47:14 +00002377 case Primitive::kPrimChar:
2378 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002379 case Primitive::kPrimBoolean:
2380 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002381 case Primitive::kPrimByte:
2382 case Primitive::kPrimShort:
2383 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002384 // Processing a Dex `int-to-char' instruction.
2385 locations->SetInAt(0, Location::Any());
2386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2387 break;
2388
2389 default:
2390 LOG(FATAL) << "Unexpected type conversion from " << input_type
2391 << " to " << result_type;
2392 }
2393 break;
2394
Roland Levillaindff1f282014-11-05 14:15:05 +00002395 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002396 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002397 case Primitive::kPrimBoolean:
2398 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002399 case Primitive::kPrimByte:
2400 case Primitive::kPrimShort:
2401 case Primitive::kPrimInt:
2402 case Primitive::kPrimChar:
2403 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002404 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002405 locations->SetOut(Location::RequiresFpuRegister());
2406 break;
2407
2408 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002409 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002410 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002411 locations->SetOut(Location::RequiresFpuRegister());
2412 break;
2413
Roland Levillaincff13742014-11-17 14:32:17 +00002414 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002415 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002416 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002417 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002418 break;
2419
2420 default:
2421 LOG(FATAL) << "Unexpected type conversion from " << input_type
2422 << " to " << result_type;
2423 };
2424 break;
2425
Roland Levillaindff1f282014-11-05 14:15:05 +00002426 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002427 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002428 case Primitive::kPrimBoolean:
2429 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002430 case Primitive::kPrimByte:
2431 case Primitive::kPrimShort:
2432 case Primitive::kPrimInt:
2433 case Primitive::kPrimChar:
2434 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002435 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002436 locations->SetOut(Location::RequiresFpuRegister());
2437 break;
2438
2439 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002440 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002441 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002442 locations->SetOut(Location::RequiresFpuRegister());
2443 break;
2444
Roland Levillaincff13742014-11-17 14:32:17 +00002445 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002446 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002447 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002448 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002449 break;
2450
2451 default:
2452 LOG(FATAL) << "Unexpected type conversion from " << input_type
2453 << " to " << result_type;
2454 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002455 break;
2456
2457 default:
2458 LOG(FATAL) << "Unexpected type conversion from " << input_type
2459 << " to " << result_type;
2460 }
2461}
2462
2463void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2464 LocationSummary* locations = conversion->GetLocations();
2465 Location out = locations->Out();
2466 Location in = locations->InAt(0);
2467 Primitive::Type result_type = conversion->GetResultType();
2468 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002469 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002470 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002471 case Primitive::kPrimByte:
2472 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002473 case Primitive::kPrimBoolean:
2474 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002475 case Primitive::kPrimShort:
2476 case Primitive::kPrimInt:
2477 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002478 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002479 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002480 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002481 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002482 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002483 Address(CpuRegister(RSP), in.GetStackIndex()));
2484 } else {
2485 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002486 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002487 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2488 }
2489 break;
2490
2491 default:
2492 LOG(FATAL) << "Unexpected type conversion from " << input_type
2493 << " to " << result_type;
2494 }
2495 break;
2496
Roland Levillain01a8d712014-11-14 16:27:39 +00002497 case Primitive::kPrimShort:
2498 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002499 case Primitive::kPrimBoolean:
2500 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002501 case Primitive::kPrimByte:
2502 case Primitive::kPrimInt:
2503 case Primitive::kPrimChar:
2504 // Processing a Dex `int-to-short' instruction.
2505 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002506 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002507 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002508 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002509 Address(CpuRegister(RSP), in.GetStackIndex()));
2510 } else {
2511 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002512 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002513 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2514 }
2515 break;
2516
2517 default:
2518 LOG(FATAL) << "Unexpected type conversion from " << input_type
2519 << " to " << result_type;
2520 }
2521 break;
2522
Roland Levillain946e1432014-11-11 17:35:19 +00002523 case Primitive::kPrimInt:
2524 switch (input_type) {
2525 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002526 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002527 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002528 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002529 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002530 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002531 Address(CpuRegister(RSP), in.GetStackIndex()));
2532 } else {
2533 DCHECK(in.IsConstant());
2534 DCHECK(in.GetConstant()->IsLongConstant());
2535 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002536 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002537 }
2538 break;
2539
Roland Levillain3f8f9362014-12-02 17:45:01 +00002540 case Primitive::kPrimFloat: {
2541 // Processing a Dex `float-to-int' instruction.
2542 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2543 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002544 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002545
2546 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002547 // if input >= (float)INT_MAX goto done
2548 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002549 __ j(kAboveEqual, &done);
2550 // if input == NaN goto nan
2551 __ j(kUnordered, &nan);
2552 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002553 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002554 __ jmp(&done);
2555 __ Bind(&nan);
2556 // output = 0
2557 __ xorl(output, output);
2558 __ Bind(&done);
2559 break;
2560 }
2561
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002562 case Primitive::kPrimDouble: {
2563 // Processing a Dex `double-to-int' instruction.
2564 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2565 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002566 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002567
2568 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002569 // if input >= (double)INT_MAX goto done
2570 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002571 __ j(kAboveEqual, &done);
2572 // if input == NaN goto nan
2573 __ j(kUnordered, &nan);
2574 // output = double-to-int-truncate(input)
2575 __ cvttsd2si(output, input);
2576 __ jmp(&done);
2577 __ Bind(&nan);
2578 // output = 0
2579 __ xorl(output, output);
2580 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002581 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002582 }
Roland Levillain946e1432014-11-11 17:35:19 +00002583
2584 default:
2585 LOG(FATAL) << "Unexpected type conversion from " << input_type
2586 << " to " << result_type;
2587 }
2588 break;
2589
Roland Levillaindff1f282014-11-05 14:15:05 +00002590 case Primitive::kPrimLong:
2591 switch (input_type) {
2592 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002593 case Primitive::kPrimBoolean:
2594 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002595 case Primitive::kPrimByte:
2596 case Primitive::kPrimShort:
2597 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002598 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002599 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002600 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002601 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002602 break;
2603
Roland Levillain624279f2014-12-04 11:54:28 +00002604 case Primitive::kPrimFloat: {
2605 // Processing a Dex `float-to-long' instruction.
2606 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2607 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002608 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002609
Mark Mendell92e83bf2015-05-07 11:25:03 -04002610 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002611 // if input >= (float)LONG_MAX goto done
2612 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002613 __ j(kAboveEqual, &done);
2614 // if input == NaN goto nan
2615 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002616 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002617 __ cvttss2si(output, input, true);
2618 __ jmp(&done);
2619 __ Bind(&nan);
2620 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002621 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002622 __ Bind(&done);
2623 break;
2624 }
2625
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002626 case Primitive::kPrimDouble: {
2627 // Processing a Dex `double-to-long' instruction.
2628 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2629 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002630 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002631
Mark Mendell92e83bf2015-05-07 11:25:03 -04002632 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002633 // if input >= (double)LONG_MAX goto done
2634 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002635 __ j(kAboveEqual, &done);
2636 // if input == NaN goto nan
2637 __ j(kUnordered, &nan);
2638 // output = double-to-long-truncate(input)
2639 __ cvttsd2si(output, input, true);
2640 __ jmp(&done);
2641 __ Bind(&nan);
2642 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002643 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002644 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002645 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002646 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002647
2648 default:
2649 LOG(FATAL) << "Unexpected type conversion from " << input_type
2650 << " to " << result_type;
2651 }
2652 break;
2653
Roland Levillain981e4542014-11-14 11:47:14 +00002654 case Primitive::kPrimChar:
2655 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002656 case Primitive::kPrimBoolean:
2657 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002658 case Primitive::kPrimByte:
2659 case Primitive::kPrimShort:
2660 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002661 // Processing a Dex `int-to-char' instruction.
2662 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002663 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002664 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002665 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002666 Address(CpuRegister(RSP), in.GetStackIndex()));
2667 } else {
2668 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002669 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002670 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2671 }
2672 break;
2673
2674 default:
2675 LOG(FATAL) << "Unexpected type conversion from " << input_type
2676 << " to " << result_type;
2677 }
2678 break;
2679
Roland Levillaindff1f282014-11-05 14:15:05 +00002680 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002681 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002682 case Primitive::kPrimBoolean:
2683 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002684 case Primitive::kPrimByte:
2685 case Primitive::kPrimShort:
2686 case Primitive::kPrimInt:
2687 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002688 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002689 if (in.IsRegister()) {
2690 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2691 } else if (in.IsConstant()) {
2692 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2693 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2694 if (v == 0) {
2695 __ xorps(dest, dest);
2696 } else {
2697 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2698 }
2699 } else {
2700 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2701 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2702 }
Roland Levillaincff13742014-11-17 14:32:17 +00002703 break;
2704
2705 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002706 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002707 if (in.IsRegister()) {
2708 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2709 } else if (in.IsConstant()) {
2710 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2711 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2712 if (v == 0) {
2713 __ xorps(dest, dest);
2714 } else {
2715 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2716 }
2717 } else {
2718 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2719 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2720 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002721 break;
2722
Roland Levillaincff13742014-11-17 14:32:17 +00002723 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002724 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002725 if (in.IsFpuRegister()) {
2726 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2727 } else if (in.IsConstant()) {
2728 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2729 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2730 if (bit_cast<int64_t, double>(v) == 0) {
2731 __ xorps(dest, dest);
2732 } else {
2733 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2734 }
2735 } else {
2736 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2737 Address(CpuRegister(RSP), in.GetStackIndex()));
2738 }
Roland Levillaincff13742014-11-17 14:32:17 +00002739 break;
2740
2741 default:
2742 LOG(FATAL) << "Unexpected type conversion from " << input_type
2743 << " to " << result_type;
2744 };
2745 break;
2746
Roland Levillaindff1f282014-11-05 14:15:05 +00002747 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002748 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002749 case Primitive::kPrimBoolean:
2750 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002751 case Primitive::kPrimByte:
2752 case Primitive::kPrimShort:
2753 case Primitive::kPrimInt:
2754 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002755 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002756 if (in.IsRegister()) {
2757 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2758 } else if (in.IsConstant()) {
2759 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2760 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2761 if (v == 0) {
2762 __ xorpd(dest, dest);
2763 } else {
2764 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2765 }
2766 } else {
2767 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2768 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2769 }
Roland Levillaincff13742014-11-17 14:32:17 +00002770 break;
2771
2772 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002773 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002774 if (in.IsRegister()) {
2775 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2776 } else if (in.IsConstant()) {
2777 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2778 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2779 if (v == 0) {
2780 __ xorpd(dest, dest);
2781 } else {
2782 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2783 }
2784 } else {
2785 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2786 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2787 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002788 break;
2789
Roland Levillaincff13742014-11-17 14:32:17 +00002790 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002791 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002792 if (in.IsFpuRegister()) {
2793 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2794 } else if (in.IsConstant()) {
2795 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2796 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2797 if (bit_cast<int32_t, float>(v) == 0) {
2798 __ xorpd(dest, dest);
2799 } else {
2800 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2801 }
2802 } else {
2803 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2804 Address(CpuRegister(RSP), in.GetStackIndex()));
2805 }
Roland Levillaincff13742014-11-17 14:32:17 +00002806 break;
2807
2808 default:
2809 LOG(FATAL) << "Unexpected type conversion from " << input_type
2810 << " to " << result_type;
2811 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002812 break;
2813
2814 default:
2815 LOG(FATAL) << "Unexpected type conversion from " << input_type
2816 << " to " << result_type;
2817 }
2818}
2819
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002820void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002821 LocationSummary* locations =
2822 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002823 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002824 case Primitive::kPrimInt: {
2825 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002826 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2827 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002828 break;
2829 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002830
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002831 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002832 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002833 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002834 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002835 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002836 break;
2837 }
2838
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002839 case Primitive::kPrimDouble:
2840 case Primitive::kPrimFloat: {
2841 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002842 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002843 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002844 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002845 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002846
2847 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002848 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002849 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002850}
2851
2852void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2853 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002854 Location first = locations->InAt(0);
2855 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002856 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002857
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002858 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002859 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002860 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002861 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2862 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002863 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2864 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002865 } else {
2866 __ leal(out.AsRegister<CpuRegister>(), Address(
2867 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2868 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002869 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002870 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2871 __ addl(out.AsRegister<CpuRegister>(),
2872 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2873 } else {
2874 __ leal(out.AsRegister<CpuRegister>(), Address(
2875 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2876 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002877 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002878 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002879 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002880 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002881 break;
2882 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002883
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002884 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002885 if (second.IsRegister()) {
2886 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2887 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002888 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2889 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002890 } else {
2891 __ leaq(out.AsRegister<CpuRegister>(), Address(
2892 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2893 }
2894 } else {
2895 DCHECK(second.IsConstant());
2896 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2897 int32_t int32_value = Low32Bits(value);
2898 DCHECK_EQ(int32_value, value);
2899 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2900 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2901 } else {
2902 __ leaq(out.AsRegister<CpuRegister>(), Address(
2903 first.AsRegister<CpuRegister>(), int32_value));
2904 }
2905 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002906 break;
2907 }
2908
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002909 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002910 if (second.IsFpuRegister()) {
2911 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2912 } else if (second.IsConstant()) {
2913 __ addss(first.AsFpuRegister<XmmRegister>(),
2914 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2915 } else {
2916 DCHECK(second.IsStackSlot());
2917 __ addss(first.AsFpuRegister<XmmRegister>(),
2918 Address(CpuRegister(RSP), second.GetStackIndex()));
2919 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002920 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002921 }
2922
2923 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002924 if (second.IsFpuRegister()) {
2925 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2926 } else if (second.IsConstant()) {
2927 __ addsd(first.AsFpuRegister<XmmRegister>(),
2928 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2929 } else {
2930 DCHECK(second.IsDoubleStackSlot());
2931 __ addsd(first.AsFpuRegister<XmmRegister>(),
2932 Address(CpuRegister(RSP), second.GetStackIndex()));
2933 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002934 break;
2935 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002936
2937 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002938 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002939 }
2940}
2941
2942void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002943 LocationSummary* locations =
2944 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002945 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002946 case Primitive::kPrimInt: {
2947 locations->SetInAt(0, Location::RequiresRegister());
2948 locations->SetInAt(1, Location::Any());
2949 locations->SetOut(Location::SameAsFirstInput());
2950 break;
2951 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002952 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002953 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002954 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002955 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002956 break;
2957 }
Calin Juravle11351682014-10-23 15:38:15 +01002958 case Primitive::kPrimFloat:
2959 case Primitive::kPrimDouble: {
2960 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002961 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002962 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002963 break;
Calin Juravle11351682014-10-23 15:38:15 +01002964 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002965 default:
Calin Juravle11351682014-10-23 15:38:15 +01002966 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002967 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002968}
2969
2970void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2971 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002972 Location first = locations->InAt(0);
2973 Location second = locations->InAt(1);
2974 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002975 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002976 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002977 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002978 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002979 } else if (second.IsConstant()) {
2980 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002982 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002983 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002984 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002985 break;
2986 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002987 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002988 if (second.IsConstant()) {
2989 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2990 DCHECK(IsInt<32>(value));
2991 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2992 } else {
2993 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2994 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002995 break;
2996 }
2997
Calin Juravle11351682014-10-23 15:38:15 +01002998 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002999 if (second.IsFpuRegister()) {
3000 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3001 } else if (second.IsConstant()) {
3002 __ subss(first.AsFpuRegister<XmmRegister>(),
3003 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3004 } else {
3005 DCHECK(second.IsStackSlot());
3006 __ subss(first.AsFpuRegister<XmmRegister>(),
3007 Address(CpuRegister(RSP), second.GetStackIndex()));
3008 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003009 break;
Calin Juravle11351682014-10-23 15:38:15 +01003010 }
3011
3012 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003013 if (second.IsFpuRegister()) {
3014 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3015 } else if (second.IsConstant()) {
3016 __ subsd(first.AsFpuRegister<XmmRegister>(),
3017 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3018 } else {
3019 DCHECK(second.IsDoubleStackSlot());
3020 __ subsd(first.AsFpuRegister<XmmRegister>(),
3021 Address(CpuRegister(RSP), second.GetStackIndex()));
3022 }
Calin Juravle11351682014-10-23 15:38:15 +01003023 break;
3024 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003025
3026 default:
Calin Juravle11351682014-10-23 15:38:15 +01003027 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003028 }
3029}
3030
Calin Juravle34bacdf2014-10-07 20:23:36 +01003031void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3032 LocationSummary* locations =
3033 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3034 switch (mul->GetResultType()) {
3035 case Primitive::kPrimInt: {
3036 locations->SetInAt(0, Location::RequiresRegister());
3037 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003038 if (mul->InputAt(1)->IsIntConstant()) {
3039 // Can use 3 operand multiply.
3040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3041 } else {
3042 locations->SetOut(Location::SameAsFirstInput());
3043 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003044 break;
3045 }
3046 case Primitive::kPrimLong: {
3047 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003048 locations->SetInAt(1, Location::Any());
3049 if (mul->InputAt(1)->IsLongConstant() &&
3050 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003051 // Can use 3 operand multiply.
3052 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3053 } else {
3054 locations->SetOut(Location::SameAsFirstInput());
3055 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003056 break;
3057 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003058 case Primitive::kPrimFloat:
3059 case Primitive::kPrimDouble: {
3060 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003061 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003062 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003063 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003064 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003065
3066 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003067 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003068 }
3069}
3070
3071void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3072 LocationSummary* locations = mul->GetLocations();
3073 Location first = locations->InAt(0);
3074 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003075 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003076 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003077 case Primitive::kPrimInt:
3078 // The constant may have ended up in a register, so test explicitly to avoid
3079 // problems where the output may not be the same as the first operand.
3080 if (mul->InputAt(1)->IsIntConstant()) {
3081 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3082 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3083 } else if (second.IsRegister()) {
3084 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003085 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003086 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003087 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003088 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003089 __ imull(first.AsRegister<CpuRegister>(),
3090 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003091 }
3092 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003093 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003094 // The constant may have ended up in a register, so test explicitly to avoid
3095 // problems where the output may not be the same as the first operand.
3096 if (mul->InputAt(1)->IsLongConstant()) {
3097 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3098 if (IsInt<32>(value)) {
3099 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3100 Immediate(static_cast<int32_t>(value)));
3101 } else {
3102 // Have to use the constant area.
3103 DCHECK(first.Equals(out));
3104 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3105 }
3106 } else if (second.IsRegister()) {
3107 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003108 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003109 } else {
3110 DCHECK(second.IsDoubleStackSlot());
3111 DCHECK(first.Equals(out));
3112 __ imulq(first.AsRegister<CpuRegister>(),
3113 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003114 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003115 break;
3116 }
3117
Calin Juravleb5bfa962014-10-21 18:02:24 +01003118 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003119 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003120 if (second.IsFpuRegister()) {
3121 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3122 } else if (second.IsConstant()) {
3123 __ mulss(first.AsFpuRegister<XmmRegister>(),
3124 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3125 } else {
3126 DCHECK(second.IsStackSlot());
3127 __ mulss(first.AsFpuRegister<XmmRegister>(),
3128 Address(CpuRegister(RSP), second.GetStackIndex()));
3129 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003130 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003131 }
3132
3133 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003134 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003135 if (second.IsFpuRegister()) {
3136 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3137 } else if (second.IsConstant()) {
3138 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3139 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3140 } else {
3141 DCHECK(second.IsDoubleStackSlot());
3142 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3143 Address(CpuRegister(RSP), second.GetStackIndex()));
3144 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003145 break;
3146 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003147
3148 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003149 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150 }
3151}
3152
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003153void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3154 uint32_t stack_adjustment, bool is_float) {
3155 if (source.IsStackSlot()) {
3156 DCHECK(is_float);
3157 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3158 } else if (source.IsDoubleStackSlot()) {
3159 DCHECK(!is_float);
3160 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3161 } else {
3162 // Write the value to the temporary location on the stack and load to FP stack.
3163 if (is_float) {
3164 Location stack_temp = Location::StackSlot(temp_offset);
3165 codegen_->Move(stack_temp, source);
3166 __ flds(Address(CpuRegister(RSP), temp_offset));
3167 } else {
3168 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3169 codegen_->Move(stack_temp, source);
3170 __ fldl(Address(CpuRegister(RSP), temp_offset));
3171 }
3172 }
3173}
3174
3175void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3176 Primitive::Type type = rem->GetResultType();
3177 bool is_float = type == Primitive::kPrimFloat;
3178 size_t elem_size = Primitive::ComponentSize(type);
3179 LocationSummary* locations = rem->GetLocations();
3180 Location first = locations->InAt(0);
3181 Location second = locations->InAt(1);
3182 Location out = locations->Out();
3183
3184 // Create stack space for 2 elements.
3185 // TODO: enhance register allocator to ask for stack temporaries.
3186 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3187
3188 // Load the values to the FP stack in reverse order, using temporaries if needed.
3189 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3190 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3191
3192 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003193 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003194 __ Bind(&retry);
3195 __ fprem();
3196
3197 // Move FP status to AX.
3198 __ fstsw();
3199
3200 // And see if the argument reduction is complete. This is signaled by the
3201 // C2 FPU flag bit set to 0.
3202 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3203 __ j(kNotEqual, &retry);
3204
3205 // We have settled on the final value. Retrieve it into an XMM register.
3206 // Store FP top of stack to real stack.
3207 if (is_float) {
3208 __ fsts(Address(CpuRegister(RSP), 0));
3209 } else {
3210 __ fstl(Address(CpuRegister(RSP), 0));
3211 }
3212
3213 // Pop the 2 items from the FP stack.
3214 __ fucompp();
3215
3216 // Load the value from the stack into an XMM register.
3217 DCHECK(out.IsFpuRegister()) << out;
3218 if (is_float) {
3219 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3220 } else {
3221 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3222 }
3223
3224 // And remove the temporary stack space we allocated.
3225 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3226}
3227
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003228void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3229 DCHECK(instruction->IsDiv() || instruction->IsRem());
3230
3231 LocationSummary* locations = instruction->GetLocations();
3232 Location second = locations->InAt(1);
3233 DCHECK(second.IsConstant());
3234
3235 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3236 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003237 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003238
3239 DCHECK(imm == 1 || imm == -1);
3240
3241 switch (instruction->GetResultType()) {
3242 case Primitive::kPrimInt: {
3243 if (instruction->IsRem()) {
3244 __ xorl(output_register, output_register);
3245 } else {
3246 __ movl(output_register, input_register);
3247 if (imm == -1) {
3248 __ negl(output_register);
3249 }
3250 }
3251 break;
3252 }
3253
3254 case Primitive::kPrimLong: {
3255 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003256 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003257 } else {
3258 __ movq(output_register, input_register);
3259 if (imm == -1) {
3260 __ negq(output_register);
3261 }
3262 }
3263 break;
3264 }
3265
3266 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003267 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003268 }
3269}
3270
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003271void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003272 LocationSummary* locations = instruction->GetLocations();
3273 Location second = locations->InAt(1);
3274
3275 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3276 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3277
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003278 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003279
3280 DCHECK(IsPowerOfTwo(std::abs(imm)));
3281
3282 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3283
3284 if (instruction->GetResultType() == Primitive::kPrimInt) {
3285 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
3286 __ testl(numerator, numerator);
3287 __ cmov(kGreaterEqual, tmp, numerator);
3288 int shift = CTZ(imm);
3289 __ sarl(tmp, Immediate(shift));
3290
3291 if (imm < 0) {
3292 __ negl(tmp);
3293 }
3294
3295 __ movl(output_register, tmp);
3296 } else {
3297 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3298 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3299
Mark Mendell92e83bf2015-05-07 11:25:03 -04003300 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003301 __ addq(rdx, numerator);
3302 __ testq(numerator, numerator);
3303 __ cmov(kGreaterEqual, rdx, numerator);
3304 int shift = CTZ(imm);
3305 __ sarq(rdx, Immediate(shift));
3306
3307 if (imm < 0) {
3308 __ negq(rdx);
3309 }
3310
3311 __ movq(output_register, rdx);
3312 }
3313}
3314
3315void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3316 DCHECK(instruction->IsDiv() || instruction->IsRem());
3317
3318 LocationSummary* locations = instruction->GetLocations();
3319 Location second = locations->InAt(1);
3320
3321 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3322 : locations->GetTemp(0).AsRegister<CpuRegister>();
3323 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3324 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3325 : locations->Out().AsRegister<CpuRegister>();
3326 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3327
3328 DCHECK_EQ(RAX, eax.AsRegister());
3329 DCHECK_EQ(RDX, edx.AsRegister());
3330 if (instruction->IsDiv()) {
3331 DCHECK_EQ(RAX, out.AsRegister());
3332 } else {
3333 DCHECK_EQ(RDX, out.AsRegister());
3334 }
3335
3336 int64_t magic;
3337 int shift;
3338
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003339 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003340 if (instruction->GetResultType() == Primitive::kPrimInt) {
3341 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3342
3343 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3344
3345 __ movl(numerator, eax);
3346
Mark Mendell0c9497d2015-08-21 09:30:05 -04003347 NearLabel no_div;
3348 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003349 __ testl(eax, eax);
3350 __ j(kNotEqual, &no_div);
3351
3352 __ xorl(out, out);
3353 __ jmp(&end);
3354
3355 __ Bind(&no_div);
3356
3357 __ movl(eax, Immediate(magic));
3358 __ imull(numerator);
3359
3360 if (imm > 0 && magic < 0) {
3361 __ addl(edx, numerator);
3362 } else if (imm < 0 && magic > 0) {
3363 __ subl(edx, numerator);
3364 }
3365
3366 if (shift != 0) {
3367 __ sarl(edx, Immediate(shift));
3368 }
3369
3370 __ movl(eax, edx);
3371 __ shrl(edx, Immediate(31));
3372 __ addl(edx, eax);
3373
3374 if (instruction->IsRem()) {
3375 __ movl(eax, numerator);
3376 __ imull(edx, Immediate(imm));
3377 __ subl(eax, edx);
3378 __ movl(edx, eax);
3379 } else {
3380 __ movl(eax, edx);
3381 }
3382 __ Bind(&end);
3383 } else {
3384 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3385
3386 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3387
3388 CpuRegister rax = eax;
3389 CpuRegister rdx = edx;
3390
3391 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3392
3393 // Save the numerator.
3394 __ movq(numerator, rax);
3395
3396 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003397 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003398
3399 // RDX:RAX = magic * numerator
3400 __ imulq(numerator);
3401
3402 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003403 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003404 __ addq(rdx, numerator);
3405 } else if (imm < 0 && magic > 0) {
3406 // RDX -= numerator
3407 __ subq(rdx, numerator);
3408 }
3409
3410 // Shift if needed.
3411 if (shift != 0) {
3412 __ sarq(rdx, Immediate(shift));
3413 }
3414
3415 // RDX += 1 if RDX < 0
3416 __ movq(rax, rdx);
3417 __ shrq(rdx, Immediate(63));
3418 __ addq(rdx, rax);
3419
3420 if (instruction->IsRem()) {
3421 __ movq(rax, numerator);
3422
3423 if (IsInt<32>(imm)) {
3424 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3425 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003426 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427 }
3428
3429 __ subq(rax, rdx);
3430 __ movq(rdx, rax);
3431 } else {
3432 __ movq(rax, rdx);
3433 }
3434 }
3435}
3436
Calin Juravlebacfec32014-11-14 15:54:36 +00003437void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3438 DCHECK(instruction->IsDiv() || instruction->IsRem());
3439 Primitive::Type type = instruction->GetResultType();
3440 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3441
3442 bool is_div = instruction->IsDiv();
3443 LocationSummary* locations = instruction->GetLocations();
3444
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3446 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003447
Roland Levillain271ab9c2014-11-27 15:23:57 +00003448 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003449 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003450
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003452 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003453
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003454 if (imm == 0) {
3455 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3456 } else if (imm == 1 || imm == -1) {
3457 DivRemOneOrMinusOne(instruction);
3458 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003459 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003460 } else {
3461 DCHECK(imm <= -2 || imm >= 2);
3462 GenerateDivRemWithAnyConstant(instruction);
3463 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003464 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003465 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003466 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3467 out.AsRegister(), type, is_div);
3468 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003469
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003470 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3471 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3472 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3473 // so it's safe to just use negl instead of more complex comparisons.
3474 if (type == Primitive::kPrimInt) {
3475 __ cmpl(second_reg, Immediate(-1));
3476 __ j(kEqual, slow_path->GetEntryLabel());
3477 // edx:eax <- sign-extended of eax
3478 __ cdq();
3479 // eax = quotient, edx = remainder
3480 __ idivl(second_reg);
3481 } else {
3482 __ cmpq(second_reg, Immediate(-1));
3483 __ j(kEqual, slow_path->GetEntryLabel());
3484 // rdx:rax <- sign-extended of rax
3485 __ cqo();
3486 // rax = quotient, rdx = remainder
3487 __ idivq(second_reg);
3488 }
3489 __ Bind(slow_path->GetExitLabel());
3490 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003491}
3492
Calin Juravle7c4954d2014-10-28 16:57:40 +00003493void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3494 LocationSummary* locations =
3495 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3496 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003497 case Primitive::kPrimInt:
3498 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003499 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003500 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003501 locations->SetOut(Location::SameAsFirstInput());
3502 // Intel uses edx:eax as the dividend.
3503 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003504 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3505 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3506 // output and request another temp.
3507 if (div->InputAt(1)->IsConstant()) {
3508 locations->AddTemp(Location::RequiresRegister());
3509 }
Calin Juravled0d48522014-11-04 16:40:20 +00003510 break;
3511 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003512
Calin Juravle7c4954d2014-10-28 16:57:40 +00003513 case Primitive::kPrimFloat:
3514 case Primitive::kPrimDouble: {
3515 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003516 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003517 locations->SetOut(Location::SameAsFirstInput());
3518 break;
3519 }
3520
3521 default:
3522 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3523 }
3524}
3525
3526void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3527 LocationSummary* locations = div->GetLocations();
3528 Location first = locations->InAt(0);
3529 Location second = locations->InAt(1);
3530 DCHECK(first.Equals(locations->Out()));
3531
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003532 Primitive::Type type = div->GetResultType();
3533 switch (type) {
3534 case Primitive::kPrimInt:
3535 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003536 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003537 break;
3538 }
3539
Calin Juravle7c4954d2014-10-28 16:57:40 +00003540 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003541 if (second.IsFpuRegister()) {
3542 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3543 } else if (second.IsConstant()) {
3544 __ divss(first.AsFpuRegister<XmmRegister>(),
3545 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3546 } else {
3547 DCHECK(second.IsStackSlot());
3548 __ divss(first.AsFpuRegister<XmmRegister>(),
3549 Address(CpuRegister(RSP), second.GetStackIndex()));
3550 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003551 break;
3552 }
3553
3554 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003555 if (second.IsFpuRegister()) {
3556 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3557 } else if (second.IsConstant()) {
3558 __ divsd(first.AsFpuRegister<XmmRegister>(),
3559 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3560 } else {
3561 DCHECK(second.IsDoubleStackSlot());
3562 __ divsd(first.AsFpuRegister<XmmRegister>(),
3563 Address(CpuRegister(RSP), second.GetStackIndex()));
3564 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003565 break;
3566 }
3567
3568 default:
3569 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3570 }
3571}
3572
Calin Juravlebacfec32014-11-14 15:54:36 +00003573void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003574 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003575 LocationSummary* locations =
3576 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003577
3578 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003579 case Primitive::kPrimInt:
3580 case Primitive::kPrimLong: {
3581 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003582 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003583 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3584 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003585 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3586 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3587 // output and request another temp.
3588 if (rem->InputAt(1)->IsConstant()) {
3589 locations->AddTemp(Location::RequiresRegister());
3590 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003591 break;
3592 }
3593
3594 case Primitive::kPrimFloat:
3595 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003596 locations->SetInAt(0, Location::Any());
3597 locations->SetInAt(1, Location::Any());
3598 locations->SetOut(Location::RequiresFpuRegister());
3599 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003600 break;
3601 }
3602
3603 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003604 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003605 }
3606}
3607
3608void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3609 Primitive::Type type = rem->GetResultType();
3610 switch (type) {
3611 case Primitive::kPrimInt:
3612 case Primitive::kPrimLong: {
3613 GenerateDivRemIntegral(rem);
3614 break;
3615 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003616 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003617 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003618 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003619 break;
3620 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003621 default:
3622 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3623 }
3624}
3625
Calin Juravled0d48522014-11-04 16:40:20 +00003626void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003627 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3628 ? LocationSummary::kCallOnSlowPath
3629 : LocationSummary::kNoCall;
3630 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003631 locations->SetInAt(0, Location::Any());
3632 if (instruction->HasUses()) {
3633 locations->SetOut(Location::SameAsFirstInput());
3634 }
3635}
3636
3637void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003638 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003639 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3640 codegen_->AddSlowPath(slow_path);
3641
3642 LocationSummary* locations = instruction->GetLocations();
3643 Location value = locations->InAt(0);
3644
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003645 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003646 case Primitive::kPrimByte:
3647 case Primitive::kPrimChar:
3648 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003649 case Primitive::kPrimInt: {
3650 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003651 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003652 __ j(kEqual, slow_path->GetEntryLabel());
3653 } else if (value.IsStackSlot()) {
3654 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3655 __ j(kEqual, slow_path->GetEntryLabel());
3656 } else {
3657 DCHECK(value.IsConstant()) << value;
3658 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3659 __ jmp(slow_path->GetEntryLabel());
3660 }
3661 }
3662 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003663 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003664 case Primitive::kPrimLong: {
3665 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003666 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003667 __ j(kEqual, slow_path->GetEntryLabel());
3668 } else if (value.IsDoubleStackSlot()) {
3669 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3670 __ j(kEqual, slow_path->GetEntryLabel());
3671 } else {
3672 DCHECK(value.IsConstant()) << value;
3673 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3674 __ jmp(slow_path->GetEntryLabel());
3675 }
3676 }
3677 break;
3678 }
3679 default:
3680 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003681 }
Calin Juravled0d48522014-11-04 16:40:20 +00003682}
3683
Calin Juravle9aec02f2014-11-18 23:06:35 +00003684void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3685 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3686
3687 LocationSummary* locations =
3688 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3689
3690 switch (op->GetResultType()) {
3691 case Primitive::kPrimInt:
3692 case Primitive::kPrimLong: {
3693 locations->SetInAt(0, Location::RequiresRegister());
3694 // The shift count needs to be in CL.
3695 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3696 locations->SetOut(Location::SameAsFirstInput());
3697 break;
3698 }
3699 default:
3700 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3701 }
3702}
3703
3704void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3705 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3706
3707 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003708 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003709 Location second = locations->InAt(1);
3710
3711 switch (op->GetResultType()) {
3712 case Primitive::kPrimInt: {
3713 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003714 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003715 if (op->IsShl()) {
3716 __ shll(first_reg, second_reg);
3717 } else if (op->IsShr()) {
3718 __ sarl(first_reg, second_reg);
3719 } else {
3720 __ shrl(first_reg, second_reg);
3721 }
3722 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003723 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003724 if (op->IsShl()) {
3725 __ shll(first_reg, imm);
3726 } else if (op->IsShr()) {
3727 __ sarl(first_reg, imm);
3728 } else {
3729 __ shrl(first_reg, imm);
3730 }
3731 }
3732 break;
3733 }
3734 case Primitive::kPrimLong: {
3735 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003736 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003737 if (op->IsShl()) {
3738 __ shlq(first_reg, second_reg);
3739 } else if (op->IsShr()) {
3740 __ sarq(first_reg, second_reg);
3741 } else {
3742 __ shrq(first_reg, second_reg);
3743 }
3744 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003745 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003746 if (op->IsShl()) {
3747 __ shlq(first_reg, imm);
3748 } else if (op->IsShr()) {
3749 __ sarq(first_reg, imm);
3750 } else {
3751 __ shrq(first_reg, imm);
3752 }
3753 }
3754 break;
3755 }
3756 default:
3757 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3758 }
3759}
3760
3761void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3762 HandleShift(shl);
3763}
3764
3765void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3766 HandleShift(shl);
3767}
3768
3769void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3770 HandleShift(shr);
3771}
3772
3773void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3774 HandleShift(shr);
3775}
3776
3777void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3778 HandleShift(ushr);
3779}
3780
3781void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3782 HandleShift(ushr);
3783}
3784
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003785void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003786 LocationSummary* locations =
3787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003788 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003789 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3790 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003791 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003792}
3793
3794void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003795 // Note: if heap poisoning is enabled, the entry point takes cares
3796 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003797 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3798 instruction,
3799 instruction->GetDexPc(),
3800 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003801 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003802
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003803 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003804}
3805
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003806void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3807 LocationSummary* locations =
3808 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3809 InvokeRuntimeCallingConvention calling_convention;
3810 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003811 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003812 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003813 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003814}
3815
3816void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3817 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003818 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3819 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003820 // Note: if heap poisoning is enabled, the entry point takes cares
3821 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003822 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3823 instruction,
3824 instruction->GetDexPc(),
3825 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003826 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003827
3828 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003829}
3830
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003831void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003832 LocationSummary* locations =
3833 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003834 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3835 if (location.IsStackSlot()) {
3836 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3837 } else if (location.IsDoubleStackSlot()) {
3838 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3839 }
3840 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003841}
3842
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003843void InstructionCodeGeneratorX86_64::VisitParameterValue(
3844 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003845 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003846}
3847
3848void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3849 LocationSummary* locations =
3850 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3851 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3852}
3853
3854void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3855 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3856 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003857}
3858
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003859void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003860 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003861 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003862 locations->SetInAt(0, Location::RequiresRegister());
3863 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003864}
3865
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003866void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3867 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3869 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003870 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003871 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003872 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003873 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003874 break;
3875
3876 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003877 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003878 break;
3879
3880 default:
3881 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3882 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003883}
3884
David Brazdil66d126e2015-04-03 16:02:44 +01003885void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3886 LocationSummary* locations =
3887 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3888 locations->SetInAt(0, Location::RequiresRegister());
3889 locations->SetOut(Location::SameAsFirstInput());
3890}
3891
3892void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003893 LocationSummary* locations = bool_not->GetLocations();
3894 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3895 locations->Out().AsRegister<CpuRegister>().AsRegister());
3896 Location out = locations->Out();
3897 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3898}
3899
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003900void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003901 LocationSummary* locations =
3902 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003903 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3904 locations->SetInAt(i, Location::Any());
3905 }
3906 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003907}
3908
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003909void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003910 LOG(FATAL) << "Unimplemented";
3911}
3912
Calin Juravle52c48962014-12-16 17:02:57 +00003913void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3914 /*
3915 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3916 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3917 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3918 */
3919 switch (kind) {
3920 case MemBarrierKind::kAnyAny: {
3921 __ mfence();
3922 break;
3923 }
3924 case MemBarrierKind::kAnyStore:
3925 case MemBarrierKind::kLoadAny:
3926 case MemBarrierKind::kStoreStore: {
3927 // nop
3928 break;
3929 }
3930 default:
3931 LOG(FATAL) << "Unexpected memory barier " << kind;
3932 }
3933}
3934
3935void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3936 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3937
Roland Levillain0d5a2812015-11-13 10:07:31 +00003938 bool object_field_get_with_read_barrier =
3939 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003940 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00003941 new (GetGraph()->GetArena()) LocationSummary(instruction,
3942 object_field_get_with_read_barrier ?
3943 LocationSummary::kCallOnSlowPath :
3944 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003945 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003946 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3947 locations->SetOut(Location::RequiresFpuRegister());
3948 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00003949 // The output overlaps for an object field get when read barriers
3950 // are enabled: we do not want the move to overwrite the object's
3951 // location, as we need it to emit the read barrier.
3952 locations->SetOut(
3953 Location::RequiresRegister(),
3954 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003955 }
Calin Juravle52c48962014-12-16 17:02:57 +00003956}
3957
3958void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3959 const FieldInfo& field_info) {
3960 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3961
3962 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00003963 Location base_loc = locations->InAt(0);
3964 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00003965 Location out = locations->Out();
3966 bool is_volatile = field_info.IsVolatile();
3967 Primitive::Type field_type = field_info.GetFieldType();
3968 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3969
3970 switch (field_type) {
3971 case Primitive::kPrimBoolean: {
3972 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3973 break;
3974 }
3975
3976 case Primitive::kPrimByte: {
3977 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3978 break;
3979 }
3980
3981 case Primitive::kPrimShort: {
3982 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3983 break;
3984 }
3985
3986 case Primitive::kPrimChar: {
3987 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3988 break;
3989 }
3990
3991 case Primitive::kPrimInt:
3992 case Primitive::kPrimNot: {
3993 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3994 break;
3995 }
3996
3997 case Primitive::kPrimLong: {
3998 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3999 break;
4000 }
4001
4002 case Primitive::kPrimFloat: {
4003 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4004 break;
4005 }
4006
4007 case Primitive::kPrimDouble: {
4008 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4009 break;
4010 }
4011
4012 case Primitive::kPrimVoid:
4013 LOG(FATAL) << "Unreachable type " << field_type;
4014 UNREACHABLE();
4015 }
4016
Calin Juravle77520bc2015-01-12 18:45:46 +00004017 codegen_->MaybeRecordImplicitNullCheck(instruction);
4018
Calin Juravle52c48962014-12-16 17:02:57 +00004019 if (is_volatile) {
4020 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4021 }
Roland Levillain4d027112015-07-01 15:41:14 +01004022
4023 if (field_type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004024 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004025 }
Calin Juravle52c48962014-12-16 17:02:57 +00004026}
4027
4028void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4029 const FieldInfo& field_info) {
4030 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4031
4032 LocationSummary* locations =
4033 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004034 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004035 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004036 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004037 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004038
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004039 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004040 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004041 if (is_volatile) {
4042 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4043 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4044 } else {
4045 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4046 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004047 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004048 if (is_volatile) {
4049 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4050 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4051 } else {
4052 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4053 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004054 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004055 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004056 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004057 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004058 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004059 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4060 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004061 locations->AddTemp(Location::RequiresRegister());
4062 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004063}
4064
Calin Juravle52c48962014-12-16 17:02:57 +00004065void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004066 const FieldInfo& field_info,
4067 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004068 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4069
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004070 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004071 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4072 Location value = locations->InAt(1);
4073 bool is_volatile = field_info.IsVolatile();
4074 Primitive::Type field_type = field_info.GetFieldType();
4075 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4076
4077 if (is_volatile) {
4078 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4079 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004080
Mark Mendellea5af682015-10-22 17:35:49 -04004081 bool maybe_record_implicit_null_check_done = false;
4082
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004083 switch (field_type) {
4084 case Primitive::kPrimBoolean:
4085 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004086 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004087 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004088 __ movb(Address(base, offset), Immediate(v));
4089 } else {
4090 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4091 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004092 break;
4093 }
4094
4095 case Primitive::kPrimShort:
4096 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004097 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004098 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004099 __ movw(Address(base, offset), Immediate(v));
4100 } else {
4101 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4102 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004103 break;
4104 }
4105
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004106 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004107 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004108 if (value.IsConstant()) {
4109 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004110 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4111 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4112 // Note: if heap poisoning is enabled, no need to poison
4113 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004114 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004115 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004116 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4117 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4118 __ movl(temp, value.AsRegister<CpuRegister>());
4119 __ PoisonHeapReference(temp);
4120 __ movl(Address(base, offset), temp);
4121 } else {
4122 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4123 }
Mark Mendell40741f32015-04-20 22:10:34 -04004124 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004125 break;
4126 }
4127
4128 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004129 if (value.IsConstant()) {
4130 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004131 codegen_->MoveInt64ToAddress(Address(base, offset),
4132 Address(base, offset + sizeof(int32_t)),
4133 v,
4134 instruction);
4135 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004136 } else {
4137 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4138 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004139 break;
4140 }
4141
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004142 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004143 if (value.IsConstant()) {
4144 int32_t v =
4145 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4146 __ movl(Address(base, offset), Immediate(v));
4147 } else {
4148 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4149 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004150 break;
4151 }
4152
4153 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004154 if (value.IsConstant()) {
4155 int64_t v =
4156 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4157 codegen_->MoveInt64ToAddress(Address(base, offset),
4158 Address(base, offset + sizeof(int32_t)),
4159 v,
4160 instruction);
4161 maybe_record_implicit_null_check_done = true;
4162 } else {
4163 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4164 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004165 break;
4166 }
4167
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004168 case Primitive::kPrimVoid:
4169 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004170 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004171 }
Calin Juravle52c48962014-12-16 17:02:57 +00004172
Mark Mendellea5af682015-10-22 17:35:49 -04004173 if (!maybe_record_implicit_null_check_done) {
4174 codegen_->MaybeRecordImplicitNullCheck(instruction);
4175 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004176
4177 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4178 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4179 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004180 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004181 }
4182
Calin Juravle52c48962014-12-16 17:02:57 +00004183 if (is_volatile) {
4184 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4185 }
4186}
4187
4188void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4189 HandleFieldSet(instruction, instruction->GetFieldInfo());
4190}
4191
4192void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004193 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004194}
4195
4196void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004197 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004198}
4199
4200void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004201 HandleFieldGet(instruction, instruction->GetFieldInfo());
4202}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004203
Calin Juravle52c48962014-12-16 17:02:57 +00004204void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4205 HandleFieldGet(instruction);
4206}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004207
Calin Juravle52c48962014-12-16 17:02:57 +00004208void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4209 HandleFieldGet(instruction, instruction->GetFieldInfo());
4210}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004211
Calin Juravle52c48962014-12-16 17:02:57 +00004212void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4213 HandleFieldSet(instruction, instruction->GetFieldInfo());
4214}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004215
Calin Juravle52c48962014-12-16 17:02:57 +00004216void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004217 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004218}
4219
Calin Juravlee460d1d2015-09-29 04:52:17 +01004220void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4221 HUnresolvedInstanceFieldGet* instruction) {
4222 FieldAccessCallingConventionX86_64 calling_convention;
4223 codegen_->CreateUnresolvedFieldLocationSummary(
4224 instruction, instruction->GetFieldType(), calling_convention);
4225}
4226
4227void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4228 HUnresolvedInstanceFieldGet* instruction) {
4229 FieldAccessCallingConventionX86_64 calling_convention;
4230 codegen_->GenerateUnresolvedFieldAccess(instruction,
4231 instruction->GetFieldType(),
4232 instruction->GetFieldIndex(),
4233 instruction->GetDexPc(),
4234 calling_convention);
4235}
4236
4237void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4238 HUnresolvedInstanceFieldSet* instruction) {
4239 FieldAccessCallingConventionX86_64 calling_convention;
4240 codegen_->CreateUnresolvedFieldLocationSummary(
4241 instruction, instruction->GetFieldType(), calling_convention);
4242}
4243
4244void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4245 HUnresolvedInstanceFieldSet* instruction) {
4246 FieldAccessCallingConventionX86_64 calling_convention;
4247 codegen_->GenerateUnresolvedFieldAccess(instruction,
4248 instruction->GetFieldType(),
4249 instruction->GetFieldIndex(),
4250 instruction->GetDexPc(),
4251 calling_convention);
4252}
4253
4254void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4255 HUnresolvedStaticFieldGet* instruction) {
4256 FieldAccessCallingConventionX86_64 calling_convention;
4257 codegen_->CreateUnresolvedFieldLocationSummary(
4258 instruction, instruction->GetFieldType(), calling_convention);
4259}
4260
4261void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4262 HUnresolvedStaticFieldGet* instruction) {
4263 FieldAccessCallingConventionX86_64 calling_convention;
4264 codegen_->GenerateUnresolvedFieldAccess(instruction,
4265 instruction->GetFieldType(),
4266 instruction->GetFieldIndex(),
4267 instruction->GetDexPc(),
4268 calling_convention);
4269}
4270
4271void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4272 HUnresolvedStaticFieldSet* instruction) {
4273 FieldAccessCallingConventionX86_64 calling_convention;
4274 codegen_->CreateUnresolvedFieldLocationSummary(
4275 instruction, instruction->GetFieldType(), calling_convention);
4276}
4277
4278void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4279 HUnresolvedStaticFieldSet* instruction) {
4280 FieldAccessCallingConventionX86_64 calling_convention;
4281 codegen_->GenerateUnresolvedFieldAccess(instruction,
4282 instruction->GetFieldType(),
4283 instruction->GetFieldIndex(),
4284 instruction->GetDexPc(),
4285 calling_convention);
4286}
4287
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004288void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004289 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4290 ? LocationSummary::kCallOnSlowPath
4291 : LocationSummary::kNoCall;
4292 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4293 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004294 ? Location::RequiresRegister()
4295 : Location::Any();
4296 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004297 if (instruction->HasUses()) {
4298 locations->SetOut(Location::SameAsFirstInput());
4299 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300}
4301
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004302void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004303 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4304 return;
4305 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004306 LocationSummary* locations = instruction->GetLocations();
4307 Location obj = locations->InAt(0);
4308
4309 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4310 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4311}
4312
4313void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004314 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004315 codegen_->AddSlowPath(slow_path);
4316
4317 LocationSummary* locations = instruction->GetLocations();
4318 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004319
4320 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004321 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004322 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004323 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004324 } else {
4325 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004326 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004327 __ jmp(slow_path->GetEntryLabel());
4328 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004329 }
4330 __ j(kEqual, slow_path->GetEntryLabel());
4331}
4332
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004333void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004334 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004335 GenerateImplicitNullCheck(instruction);
4336 } else {
4337 GenerateExplicitNullCheck(instruction);
4338 }
4339}
4340
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004341void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004342 bool object_array_get_with_read_barrier =
4343 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004344 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004345 new (GetGraph()->GetArena()) LocationSummary(instruction,
4346 object_array_get_with_read_barrier ?
4347 LocationSummary::kCallOnSlowPath :
4348 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004349 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004350 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004351 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4352 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4353 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004354 // The output overlaps for an object array get when read barriers
4355 // are enabled: we do not want the move to overwrite the array's
4356 // location, as we need it to emit the read barrier.
4357 locations->SetOut(
4358 Location::RequiresRegister(),
4359 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004360 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361}
4362
4363void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4364 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004365 Location obj_loc = locations->InAt(0);
4366 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004367 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004368 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004369
Roland Levillain4d027112015-07-01 15:41:14 +01004370 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004371 case Primitive::kPrimBoolean: {
4372 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004373 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004374 if (index.IsConstant()) {
4375 __ movzxb(out, Address(obj,
4376 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4377 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004378 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004379 }
4380 break;
4381 }
4382
4383 case Primitive::kPrimByte: {
4384 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004385 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004386 if (index.IsConstant()) {
4387 __ movsxb(out, Address(obj,
4388 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4389 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004390 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004391 }
4392 break;
4393 }
4394
4395 case Primitive::kPrimShort: {
4396 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004397 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004398 if (index.IsConstant()) {
4399 __ movsxw(out, Address(obj,
4400 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4401 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004402 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403 }
4404 break;
4405 }
4406
4407 case Primitive::kPrimChar: {
4408 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004409 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004410 if (index.IsConstant()) {
4411 __ movzxw(out, Address(obj,
4412 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4413 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004414 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004415 }
4416 break;
4417 }
4418
4419 case Primitive::kPrimInt:
4420 case Primitive::kPrimNot: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004421 static_assert(
4422 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4423 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004424 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004425 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004426 if (index.IsConstant()) {
4427 __ movl(out, Address(obj,
4428 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4429 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004430 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004431 }
4432 break;
4433 }
4434
4435 case Primitive::kPrimLong: {
4436 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004437 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004438 if (index.IsConstant()) {
4439 __ movq(out, Address(obj,
4440 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4441 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004442 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004443 }
4444 break;
4445 }
4446
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004447 case Primitive::kPrimFloat: {
4448 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004449 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004450 if (index.IsConstant()) {
4451 __ movss(out, Address(obj,
4452 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4453 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004454 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004455 }
4456 break;
4457 }
4458
4459 case Primitive::kPrimDouble: {
4460 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004461 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004462 if (index.IsConstant()) {
4463 __ movsd(out, Address(obj,
4464 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4465 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004466 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004467 }
4468 break;
4469 }
4470
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004471 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004472 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004473 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004474 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004475 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004476
4477 if (type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004478 static_assert(
4479 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4480 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4481 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4482 Location out = locations->Out();
4483 if (index.IsConstant()) {
4484 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4485 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4486 } else {
4487 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4488 }
Roland Levillain4d027112015-07-01 15:41:14 +01004489 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004490}
4491
4492void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004493 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004494
4495 bool needs_write_barrier =
4496 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004497 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004498 bool object_array_set_with_read_barrier =
4499 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004500
Nicolas Geoffray39468442014-09-02 15:17:15 +01004501 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004502 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004503 (may_need_runtime_call || object_array_set_with_read_barrier) ?
4504 LocationSummary::kCallOnSlowPath :
4505 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004506
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004507 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004508 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4509 if (Primitive::IsFloatingPointType(value_type)) {
4510 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004511 } else {
4512 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4513 }
4514
4515 if (needs_write_barrier) {
4516 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004517
4518 // This first temporary register is possibly used for heap
4519 // reference poisoning and/or read barrier emission too.
4520 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004521 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004522 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004523}
4524
4525void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4526 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004527 Location array_loc = locations->InAt(0);
4528 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004530 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004531 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004532 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004533 bool needs_write_barrier =
4534 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004535 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4536 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4537 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538
4539 switch (value_type) {
4540 case Primitive::kPrimBoolean:
4541 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004542 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4543 Address address = index.IsConstant()
4544 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4545 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4546 if (value.IsRegister()) {
4547 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004548 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004549 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004551 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004552 break;
4553 }
4554
4555 case Primitive::kPrimShort:
4556 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004557 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4558 Address address = index.IsConstant()
4559 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4560 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4561 if (value.IsRegister()) {
4562 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004563 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004564 DCHECK(value.IsConstant()) << value;
4565 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004566 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004567 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004568 break;
4569 }
4570
4571 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004572 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4573 Address address = index.IsConstant()
4574 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4575 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004576
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004577 if (!value.IsRegister()) {
4578 // Just setting null.
4579 DCHECK(instruction->InputAt(2)->IsNullConstant());
4580 DCHECK(value.IsConstant()) << value;
4581 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004582 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004583 DCHECK(!needs_write_barrier);
4584 DCHECK(!may_need_runtime_call);
4585 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004586 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004587
4588 DCHECK(needs_write_barrier);
4589 CpuRegister register_value = value.AsRegister<CpuRegister>();
4590 NearLabel done, not_null, do_put;
4591 SlowPathCode* slow_path = nullptr;
4592 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4593 if (may_need_runtime_call) {
4594 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4595 codegen_->AddSlowPath(slow_path);
4596 if (instruction->GetValueCanBeNull()) {
4597 __ testl(register_value, register_value);
4598 __ j(kNotEqual, &not_null);
4599 __ movl(address, Immediate(0));
4600 codegen_->MaybeRecordImplicitNullCheck(instruction);
4601 __ jmp(&done);
4602 __ Bind(&not_null);
4603 }
4604
Roland Levillain0d5a2812015-11-13 10:07:31 +00004605 if (kEmitCompilerReadBarrier) {
4606 // When read barriers are enabled, the type checking
4607 // instrumentation requires two read barriers:
4608 //
4609 // __ movl(temp2, temp);
4610 // // /* HeapReference<Class> */ temp = temp->component_type_
4611 // __ movl(temp, Address(temp, component_offset));
4612 // codegen_->GenerateReadBarrier(
4613 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4614 //
4615 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4616 // __ movl(temp2, Address(register_value, class_offset));
4617 // codegen_->GenerateReadBarrier(
4618 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4619 //
4620 // __ cmpl(temp, temp2);
4621 //
4622 // However, the second read barrier may trash `temp`, as it
4623 // is a temporary register, and as such would not be saved
4624 // along with live registers before calling the runtime (nor
4625 // restored afterwards). So in this case, we bail out and
4626 // delegate the work to the array set slow path.
4627 //
4628 // TODO: Extend the register allocator to support a new
4629 // "(locally) live temp" location so as to avoid always
4630 // going into the slow path when read barriers are enabled.
4631 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004632 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004633 // /* HeapReference<Class> */ temp = array->klass_
4634 __ movl(temp, Address(array, class_offset));
4635 codegen_->MaybeRecordImplicitNullCheck(instruction);
4636 __ MaybeUnpoisonHeapReference(temp);
4637
4638 // /* HeapReference<Class> */ temp = temp->component_type_
4639 __ movl(temp, Address(temp, component_offset));
4640 // If heap poisoning is enabled, no need to unpoison `temp`
4641 // nor the object reference in `register_value->klass`, as
4642 // we are comparing two poisoned references.
4643 __ cmpl(temp, Address(register_value, class_offset));
4644
4645 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4646 __ j(kEqual, &do_put);
4647 // If heap poisoning is enabled, the `temp` reference has
4648 // not been unpoisoned yet; unpoison it now.
4649 __ MaybeUnpoisonHeapReference(temp);
4650
4651 // /* HeapReference<Class> */ temp = temp->super_class_
4652 __ movl(temp, Address(temp, super_offset));
4653 // If heap poisoning is enabled, no need to unpoison
4654 // `temp`, as we are comparing against null below.
4655 __ testl(temp, temp);
4656 __ j(kNotEqual, slow_path->GetEntryLabel());
4657 __ Bind(&do_put);
4658 } else {
4659 __ j(kNotEqual, slow_path->GetEntryLabel());
4660 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004661 }
4662 }
4663
4664 if (kPoisonHeapReferences) {
4665 __ movl(temp, register_value);
4666 __ PoisonHeapReference(temp);
4667 __ movl(address, temp);
4668 } else {
4669 __ movl(address, register_value);
4670 }
4671 if (!may_need_runtime_call) {
4672 codegen_->MaybeRecordImplicitNullCheck(instruction);
4673 }
4674
4675 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4676 codegen_->MarkGCCard(
4677 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4678 __ Bind(&done);
4679
4680 if (slow_path != nullptr) {
4681 __ Bind(slow_path->GetExitLabel());
4682 }
4683
4684 break;
4685 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004686
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004687 case Primitive::kPrimInt: {
4688 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4689 Address address = index.IsConstant()
4690 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4691 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4692 if (value.IsRegister()) {
4693 __ movl(address, value.AsRegister<CpuRegister>());
4694 } else {
4695 DCHECK(value.IsConstant()) << value;
4696 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4697 __ movl(address, Immediate(v));
4698 }
4699 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004700 break;
4701 }
4702
4703 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004704 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4705 Address address = index.IsConstant()
4706 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4707 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4708 if (value.IsRegister()) {
4709 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004710 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004712 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004713 Address address_high = index.IsConstant()
4714 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4715 offset + sizeof(int32_t))
4716 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4717 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718 }
4719 break;
4720 }
4721
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004722 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004723 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4724 Address address = index.IsConstant()
4725 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4726 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004727 if (value.IsFpuRegister()) {
4728 __ movss(address, value.AsFpuRegister<XmmRegister>());
4729 } else {
4730 DCHECK(value.IsConstant());
4731 int32_t v =
4732 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4733 __ movl(address, Immediate(v));
4734 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004735 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004736 break;
4737 }
4738
4739 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004740 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4741 Address address = index.IsConstant()
4742 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4743 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004744 if (value.IsFpuRegister()) {
4745 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4746 codegen_->MaybeRecordImplicitNullCheck(instruction);
4747 } else {
4748 int64_t v =
4749 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4750 Address address_high = index.IsConstant()
4751 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4752 offset + sizeof(int32_t))
4753 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4754 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4755 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004756 break;
4757 }
4758
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004759 case Primitive::kPrimVoid:
4760 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004761 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004762 }
4763}
4764
4765void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004766 LocationSummary* locations =
4767 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004768 locations->SetInAt(0, Location::RequiresRegister());
4769 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004770}
4771
4772void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4773 LocationSummary* locations = instruction->GetLocations();
4774 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004775 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4776 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004777 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004778 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004779}
4780
4781void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004782 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4783 ? LocationSummary::kCallOnSlowPath
4784 : LocationSummary::kNoCall;
4785 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004786 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004787 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004788 if (instruction->HasUses()) {
4789 locations->SetOut(Location::SameAsFirstInput());
4790 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004791}
4792
4793void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4794 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004795 Location index_loc = locations->InAt(0);
4796 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004797 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004798 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004799
Mark Mendell99dbd682015-04-22 16:18:52 -04004800 if (length_loc.IsConstant()) {
4801 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4802 if (index_loc.IsConstant()) {
4803 // BCE will remove the bounds check if we are guarenteed to pass.
4804 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4805 if (index < 0 || index >= length) {
4806 codegen_->AddSlowPath(slow_path);
4807 __ jmp(slow_path->GetEntryLabel());
4808 } else {
4809 // Some optimization after BCE may have generated this, and we should not
4810 // generate a bounds check if it is a valid range.
4811 }
4812 return;
4813 }
4814
4815 // We have to reverse the jump condition because the length is the constant.
4816 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4817 __ cmpl(index_reg, Immediate(length));
4818 codegen_->AddSlowPath(slow_path);
4819 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004820 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004821 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4822 if (index_loc.IsConstant()) {
4823 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4824 __ cmpl(length, Immediate(value));
4825 } else {
4826 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4827 }
4828 codegen_->AddSlowPath(slow_path);
4829 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004830 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004831}
4832
4833void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4834 CpuRegister card,
4835 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004836 CpuRegister value,
4837 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004838 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004839 if (value_can_be_null) {
4840 __ testl(value, value);
4841 __ j(kEqual, &is_null);
4842 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004843 __ gs()->movq(card, Address::Absolute(
4844 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4845 __ movq(temp, object);
4846 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004847 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004848 if (value_can_be_null) {
4849 __ Bind(&is_null);
4850 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004851}
4852
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004853void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4854 temp->SetLocations(nullptr);
4855}
4856
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004857void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004858 // Nothing to do, this is driven by the code generator.
4859}
4860
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004861void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004862 LOG(FATAL) << "Unimplemented";
4863}
4864
4865void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004866 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4867}
4868
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004869void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4870 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4871}
4872
4873void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004874 HBasicBlock* block = instruction->GetBlock();
4875 if (block->GetLoopInformation() != nullptr) {
4876 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4877 // The back edge will generate the suspend check.
4878 return;
4879 }
4880 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4881 // The goto will generate the suspend check.
4882 return;
4883 }
4884 GenerateSuspendCheck(instruction, nullptr);
4885}
4886
4887void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4888 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004889 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004890 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4891 if (slow_path == nullptr) {
4892 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4893 instruction->SetSlowPath(slow_path);
4894 codegen_->AddSlowPath(slow_path);
4895 if (successor != nullptr) {
4896 DCHECK(successor->IsLoopHeader());
4897 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4898 }
4899 } else {
4900 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4901 }
4902
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004903 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004904 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004905 if (successor == nullptr) {
4906 __ j(kNotEqual, slow_path->GetEntryLabel());
4907 __ Bind(slow_path->GetReturnLabel());
4908 } else {
4909 __ j(kEqual, codegen_->GetLabelOf(successor));
4910 __ jmp(slow_path->GetEntryLabel());
4911 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004912}
4913
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004914X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4915 return codegen_->GetAssembler();
4916}
4917
4918void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004919 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004920 Location source = move->GetSource();
4921 Location destination = move->GetDestination();
4922
4923 if (source.IsRegister()) {
4924 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004925 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004926 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004927 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004928 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004929 } else {
4930 DCHECK(destination.IsDoubleStackSlot());
4931 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004932 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004933 }
4934 } else if (source.IsStackSlot()) {
4935 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004936 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004937 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004938 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004939 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004940 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004941 } else {
4942 DCHECK(destination.IsStackSlot());
4943 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4944 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4945 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004946 } else if (source.IsDoubleStackSlot()) {
4947 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004948 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004949 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004950 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004951 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4952 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004953 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004954 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004955 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4956 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4957 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004958 } else if (source.IsConstant()) {
4959 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004960 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4961 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004962 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004963 if (value == 0) {
4964 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4965 } else {
4966 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4967 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004968 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004969 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004970 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004971 }
4972 } else if (constant->IsLongConstant()) {
4973 int64_t value = constant->AsLongConstant()->GetValue();
4974 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004975 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004976 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004977 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004978 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004979 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004980 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004981 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004982 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004983 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004984 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4985 if (value == 0) {
4986 // easy FP 0.0.
4987 __ xorps(dest, dest);
4988 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004989 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004990 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004991 } else {
4992 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004993 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004994 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4995 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004996 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004997 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004998 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004999 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005000 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005001 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5002 if (value == 0) {
5003 __ xorpd(dest, dest);
5004 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005005 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005006 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005007 } else {
5008 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005009 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005010 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005011 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005012 } else if (source.IsFpuRegister()) {
5013 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005014 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005015 } else if (destination.IsStackSlot()) {
5016 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005017 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005018 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005019 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005020 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005021 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005022 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005023 }
5024}
5025
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005026void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005027 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005028 __ movl(Address(CpuRegister(RSP), mem), reg);
5029 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005030}
5031
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005032void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005033 ScratchRegisterScope ensure_scratch(
5034 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5035
5036 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5037 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5038 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5039 Address(CpuRegister(RSP), mem2 + stack_offset));
5040 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5041 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5042 CpuRegister(ensure_scratch.GetRegister()));
5043}
5044
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005045void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5046 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5047 __ movq(Address(CpuRegister(RSP), mem), reg);
5048 __ movq(reg, CpuRegister(TMP));
5049}
5050
5051void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5052 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005053 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005054
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005055 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5056 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5057 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5058 Address(CpuRegister(RSP), mem2 + stack_offset));
5059 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5060 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5061 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005062}
5063
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005064void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5065 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5066 __ movss(Address(CpuRegister(RSP), mem), reg);
5067 __ movd(reg, CpuRegister(TMP));
5068}
5069
5070void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5071 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5072 __ movsd(Address(CpuRegister(RSP), mem), reg);
5073 __ movd(reg, CpuRegister(TMP));
5074}
5075
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005076void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005077 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005078 Location source = move->GetSource();
5079 Location destination = move->GetDestination();
5080
5081 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005082 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005083 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005084 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005085 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005086 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005087 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005088 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5089 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005090 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005091 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005092 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005093 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5094 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005095 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005096 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5097 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5098 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005099 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005100 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005101 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005102 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005103 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005104 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005105 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005106 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005107 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005108 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005109 }
5110}
5111
5112
5113void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5114 __ pushq(CpuRegister(reg));
5115}
5116
5117
5118void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5119 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005120}
5121
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005122void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005123 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005124 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5125 Immediate(mirror::Class::kStatusInitialized));
5126 __ j(kLess, slow_path->GetEntryLabel());
5127 __ Bind(slow_path->GetExitLabel());
5128 // No need for memory fence, thanks to the X86_64 memory model.
5129}
5130
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005131void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005132 InvokeRuntimeCallingConvention calling_convention;
5133 CodeGenerator::CreateLoadClassLocationSummary(
5134 cls,
5135 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005136 Location::RegisterLocation(RAX),
5137 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005138}
5139
5140void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005141 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005142 if (cls->NeedsAccessCheck()) {
5143 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5144 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5145 cls,
5146 cls->GetDexPc(),
5147 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005148 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005149 return;
5150 }
5151
Roland Levillain0d5a2812015-11-13 10:07:31 +00005152 Location out_loc = locations->Out();
5153 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005154 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005155
Calin Juravle580b6092015-10-06 17:35:58 +01005156 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005157 DCHECK(!cls->CanCallRuntime());
5158 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005159 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5160 if (kEmitCompilerReadBarrier) {
5161 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5162 __ leaq(out, Address(current_method, declaring_class_offset));
5163 // /* mirror::Class* */ out = out->Read()
5164 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5165 } else {
5166 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5167 __ movl(out, Address(current_method, declaring_class_offset));
5168 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005169 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005170 // /* GcRoot<mirror::Class>[] */ out =
5171 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5172 __ movq(out, Address(current_method,
5173 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5174
5175 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5176 if (kEmitCompilerReadBarrier) {
5177 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
5178 __ leaq(out, Address(out, cache_offset));
5179 // /* mirror::Class* */ out = out->Read()
5180 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5181 } else {
5182 // /* GcRoot<mirror::Class> */ out = out[type_index]
5183 __ movl(out, Address(out, cache_offset));
5184 }
Roland Levillain4d027112015-07-01 15:41:14 +01005185
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005186 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5187 DCHECK(cls->CanCallRuntime());
5188 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5189 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5190 codegen_->AddSlowPath(slow_path);
5191 if (!cls->IsInDexCache()) {
5192 __ testl(out, out);
5193 __ j(kEqual, slow_path->GetEntryLabel());
5194 }
5195 if (cls->MustGenerateClinitCheck()) {
5196 GenerateClassInitializationCheck(slow_path, out);
5197 } else {
5198 __ Bind(slow_path->GetExitLabel());
5199 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005200 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005201 }
5202}
5203
5204void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5205 LocationSummary* locations =
5206 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5207 locations->SetInAt(0, Location::RequiresRegister());
5208 if (check->HasUses()) {
5209 locations->SetOut(Location::SameAsFirstInput());
5210 }
5211}
5212
5213void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005214 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005215 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005216 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005217 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005218 GenerateClassInitializationCheck(slow_path,
5219 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005220}
5221
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005222void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
5223 LocationSummary* locations =
5224 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005225 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005226 locations->SetOut(Location::RequiresRegister());
5227}
5228
5229void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005230 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005231 codegen_->AddSlowPath(slow_path);
5232
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005233 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005234 Location out_loc = locations->Out();
5235 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005236 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005237
5238 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5239 if (kEmitCompilerReadBarrier) {
5240 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5241 __ leaq(out, Address(current_method, declaring_class_offset));
5242 // /* mirror::Class* */ out = out->Read()
5243 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5244 } else {
5245 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5246 __ movl(out, Address(current_method, declaring_class_offset));
5247 }
5248
5249 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5250 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5251
5252 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5253 if (kEmitCompilerReadBarrier) {
5254 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5255 __ leaq(out, Address(out, cache_offset));
5256 // /* mirror::String* */ out = out->Read()
5257 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5258 } else {
5259 // /* GcRoot<mirror::String> */ out = out[string_index]
5260 __ movl(out, Address(out, cache_offset));
5261 }
5262
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005263 __ testl(out, out);
5264 __ j(kEqual, slow_path->GetEntryLabel());
5265 __ Bind(slow_path->GetExitLabel());
5266}
5267
David Brazdilcb1c0552015-08-04 16:22:25 +01005268static Address GetExceptionTlsAddress() {
5269 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
5270}
5271
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005272void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5273 LocationSummary* locations =
5274 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5275 locations->SetOut(Location::RequiresRegister());
5276}
5277
5278void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005279 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5280}
5281
5282void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5283 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5284}
5285
5286void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5287 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005288}
5289
5290void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5291 LocationSummary* locations =
5292 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5293 InvokeRuntimeCallingConvention calling_convention;
5294 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5295}
5296
5297void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005298 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5299 instruction,
5300 instruction->GetDexPc(),
5301 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005302 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005303}
5304
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005305void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005306 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005307 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5308 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005309 case TypeCheckKind::kExactCheck:
5310 case TypeCheckKind::kAbstractClassCheck:
5311 case TypeCheckKind::kClassHierarchyCheck:
5312 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005313 call_kind =
5314 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005315 break;
5316 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005317 case TypeCheckKind::kUnresolvedCheck:
5318 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005319 call_kind = LocationSummary::kCallOnSlowPath;
5320 break;
5321 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005322
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005323 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005324 locations->SetInAt(0, Location::RequiresRegister());
5325 locations->SetInAt(1, Location::Any());
5326 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5327 locations->SetOut(Location::RequiresRegister());
5328 // When read barriers are enabled, we need a temporary register for
5329 // some cases.
5330 if (kEmitCompilerReadBarrier &&
5331 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5332 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5333 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5334 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005335 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005336}
5337
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005338void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005339 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005340 Location obj_loc = locations->InAt(0);
5341 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005342 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005343 Location out_loc = locations->Out();
5344 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005345 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005346 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5347 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5348 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005349 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005350 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005351
5352 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005353 // Avoid null check if we know obj is not null.
5354 if (instruction->MustDoNullCheck()) {
5355 __ testl(obj, obj);
5356 __ j(kEqual, &zero);
5357 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005358
Roland Levillain0d5a2812015-11-13 10:07:31 +00005359 // /* HeapReference<Class> */ out = obj->klass_
5360 __ movl(out, Address(obj, class_offset));
5361 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005362
5363 switch (instruction->GetTypeCheckKind()) {
5364 case TypeCheckKind::kExactCheck: {
5365 if (cls.IsRegister()) {
5366 __ cmpl(out, cls.AsRegister<CpuRegister>());
5367 } else {
5368 DCHECK(cls.IsStackSlot()) << cls;
5369 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5370 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005371 if (zero.IsLinked()) {
5372 // Classes must be equal for the instanceof to succeed.
5373 __ j(kNotEqual, &zero);
5374 __ movl(out, Immediate(1));
5375 __ jmp(&done);
5376 } else {
5377 __ setcc(kEqual, out);
5378 // setcc only sets the low byte.
5379 __ andl(out, Immediate(1));
5380 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005381 break;
5382 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005383
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005384 case TypeCheckKind::kAbstractClassCheck: {
5385 // If the class is abstract, we eagerly fetch the super class of the
5386 // object to avoid doing a comparison we know will fail.
5387 NearLabel loop, success;
5388 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005389 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5390 if (kEmitCompilerReadBarrier) {
5391 // Save the value of `out` into `temp` before overwriting it
5392 // in the following move operation, as we will need it for the
5393 // read barrier below.
5394 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
5395 __ movl(temp, out);
5396 }
5397 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005398 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005399 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005400 __ testl(out, out);
5401 // If `out` is null, we use it for the result, and jump to `done`.
5402 __ j(kEqual, &done);
5403 if (cls.IsRegister()) {
5404 __ cmpl(out, cls.AsRegister<CpuRegister>());
5405 } else {
5406 DCHECK(cls.IsStackSlot()) << cls;
5407 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5408 }
5409 __ j(kNotEqual, &loop);
5410 __ movl(out, Immediate(1));
5411 if (zero.IsLinked()) {
5412 __ jmp(&done);
5413 }
5414 break;
5415 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005416
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005417 case TypeCheckKind::kClassHierarchyCheck: {
5418 // Walk over the class hierarchy to find a match.
5419 NearLabel loop, success;
5420 __ Bind(&loop);
5421 if (cls.IsRegister()) {
5422 __ cmpl(out, cls.AsRegister<CpuRegister>());
5423 } else {
5424 DCHECK(cls.IsStackSlot()) << cls;
5425 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5426 }
5427 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005428 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5429 if (kEmitCompilerReadBarrier) {
5430 // Save the value of `out` into `temp` before overwriting it
5431 // in the following move operation, as we will need it for the
5432 // read barrier below.
5433 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
5434 __ movl(temp, out);
5435 }
5436 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005437 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005438 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005439 __ testl(out, out);
5440 __ j(kNotEqual, &loop);
5441 // If `out` is null, we use it for the result, and jump to `done`.
5442 __ jmp(&done);
5443 __ Bind(&success);
5444 __ movl(out, Immediate(1));
5445 if (zero.IsLinked()) {
5446 __ jmp(&done);
5447 }
5448 break;
5449 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005450
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005451 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005452 // Do an exact check.
5453 NearLabel exact_check;
5454 if (cls.IsRegister()) {
5455 __ cmpl(out, cls.AsRegister<CpuRegister>());
5456 } else {
5457 DCHECK(cls.IsStackSlot()) << cls;
5458 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5459 }
5460 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005461 // Otherwise, we need to check that the object's class is a non-primitive array.
5462 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5463 if (kEmitCompilerReadBarrier) {
5464 // Save the value of `out` into `temp` before overwriting it
5465 // in the following move operation, as we will need it for the
5466 // read barrier below.
5467 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
5468 __ movl(temp, out);
5469 }
5470 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005471 __ movl(out, Address(out, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005472 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005473 __ testl(out, out);
5474 // If `out` is null, we use it for the result, and jump to `done`.
5475 __ j(kEqual, &done);
5476 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5477 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005478 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005479 __ movl(out, Immediate(1));
5480 __ jmp(&done);
5481 break;
5482 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005483
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005484 case TypeCheckKind::kArrayCheck: {
5485 if (cls.IsRegister()) {
5486 __ cmpl(out, cls.AsRegister<CpuRegister>());
5487 } else {
5488 DCHECK(cls.IsStackSlot()) << cls;
5489 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5490 }
5491 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005492 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5493 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005494 codegen_->AddSlowPath(slow_path);
5495 __ j(kNotEqual, slow_path->GetEntryLabel());
5496 __ movl(out, Immediate(1));
5497 if (zero.IsLinked()) {
5498 __ jmp(&done);
5499 }
5500 break;
5501 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005502
Calin Juravle98893e12015-10-02 21:05:03 +01005503 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005504 case TypeCheckKind::kInterfaceCheck: {
5505 // Note that we indeed only call on slow path, but we always go
5506 // into the slow path for the unresolved & interface check
5507 // cases.
5508 //
5509 // We cannot directly call the InstanceofNonTrivial runtime
5510 // entry point without resorting to a type checking slow path
5511 // here (i.e. by calling InvokeRuntime directly), as it would
5512 // require to assign fixed registers for the inputs of this
5513 // HInstanceOf instruction (following the runtime calling
5514 // convention), which might be cluttered by the potential first
5515 // read barrier emission at the beginning of this method.
5516 DCHECK(locations->OnlyCallsOnSlowPath());
5517 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5518 /* is_fatal */ false);
5519 codegen_->AddSlowPath(slow_path);
5520 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005521 if (zero.IsLinked()) {
5522 __ jmp(&done);
5523 }
5524 break;
5525 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005526 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005527
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005528 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005529 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005530 __ xorl(out, out);
5531 }
5532
5533 if (done.IsLinked()) {
5534 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005535 }
5536
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005537 if (slow_path != nullptr) {
5538 __ Bind(slow_path->GetExitLabel());
5539 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005540}
5541
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005542void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005543 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5544 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005545 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5546 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005547 case TypeCheckKind::kExactCheck:
5548 case TypeCheckKind::kAbstractClassCheck:
5549 case TypeCheckKind::kClassHierarchyCheck:
5550 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005551 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5552 LocationSummary::kCallOnSlowPath :
5553 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005554 break;
5555 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005556 case TypeCheckKind::kUnresolvedCheck:
5557 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005558 call_kind = LocationSummary::kCallOnSlowPath;
5559 break;
5560 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005561 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5562 locations->SetInAt(0, Location::RequiresRegister());
5563 locations->SetInAt(1, Location::Any());
5564 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5565 locations->AddTemp(Location::RequiresRegister());
5566 // When read barriers are enabled, we need an additional temporary
5567 // register for some cases.
5568 if (kEmitCompilerReadBarrier &&
5569 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5570 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5571 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005572 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005573 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005574}
5575
5576void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
5577 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005578 Location obj_loc = locations->InAt(0);
5579 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005580 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005581 Location temp_loc = locations->GetTemp(0);
5582 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005583 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5584 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5585 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5586 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005587
Roland Levillain0d5a2812015-11-13 10:07:31 +00005588 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5589 bool is_type_check_slow_path_fatal =
5590 (type_check_kind == TypeCheckKind::kExactCheck ||
5591 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5592 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5593 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5594 !instruction->CanThrowIntoCatchBlock();
5595 SlowPathCode* type_check_slow_path =
5596 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5597 is_type_check_slow_path_fatal);
5598 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005599
5600 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005601 // Avoid null check if we know obj is not null.
5602 if (instruction->MustDoNullCheck()) {
5603 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005604 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005605 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005606
Roland Levillain0d5a2812015-11-13 10:07:31 +00005607 // /* HeapReference<Class> */ temp = obj->klass_
5608 __ movl(temp, Address(obj, class_offset));
5609 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005610
Roland Levillain0d5a2812015-11-13 10:07:31 +00005611 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005612 case TypeCheckKind::kExactCheck:
5613 case TypeCheckKind::kArrayCheck: {
5614 if (cls.IsRegister()) {
5615 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5616 } else {
5617 DCHECK(cls.IsStackSlot()) << cls;
5618 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5619 }
5620 // Jump to slow path for throwing the exception or doing a
5621 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005622 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005623 break;
5624 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005625
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005626 case TypeCheckKind::kAbstractClassCheck: {
5627 // If the class is abstract, we eagerly fetch the super class of the
5628 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005629 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005630 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005631 Location temp2_loc =
5632 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5633 if (kEmitCompilerReadBarrier) {
5634 // Save the value of `temp` into `temp2` before overwriting it
5635 // in the following move operation, as we will need it for the
5636 // read barrier below.
5637 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
5638 __ movl(temp2, temp);
5639 }
5640 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005641 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005642 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5643
5644 // If the class reference currently in `temp` is not null, jump
5645 // to the `compare_classes` label to compare it with the checked
5646 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005647 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005648 __ j(kNotEqual, &compare_classes);
5649 // Otherwise, jump to the slow path to throw the exception.
5650 //
5651 // But before, move back the object's class into `temp` before
5652 // going into the slow path, as it has been overwritten in the
5653 // meantime.
5654 // /* HeapReference<Class> */ temp = obj->klass_
5655 __ movl(temp, Address(obj, class_offset));
5656 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5657 __ jmp(type_check_slow_path->GetEntryLabel());
5658
5659 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005660 if (cls.IsRegister()) {
5661 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5662 } else {
5663 DCHECK(cls.IsStackSlot()) << cls;
5664 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5665 }
5666 __ j(kNotEqual, &loop);
5667 break;
5668 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005669
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005670 case TypeCheckKind::kClassHierarchyCheck: {
5671 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005672 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005673 __ Bind(&loop);
5674 if (cls.IsRegister()) {
5675 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5676 } else {
5677 DCHECK(cls.IsStackSlot()) << cls;
5678 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5679 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005680 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005681
5682 Location temp2_loc =
5683 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5684 if (kEmitCompilerReadBarrier) {
5685 // Save the value of `temp` into `temp2` before overwriting it
5686 // in the following move operation, as we will need it for the
5687 // read barrier below.
5688 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
5689 __ movl(temp2, temp);
5690 }
5691 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005692 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005693 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5694
5695 // If the class reference currently in `temp` is not null, jump
5696 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005697 __ testl(temp, temp);
5698 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005699 // Otherwise, jump to the slow path to throw the exception.
5700 //
5701 // But before, move back the object's class into `temp` before
5702 // going into the slow path, as it has been overwritten in the
5703 // meantime.
5704 // /* HeapReference<Class> */ temp = obj->klass_
5705 __ movl(temp, Address(obj, class_offset));
5706 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5707 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005708 break;
5709 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005710
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005711 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005712 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005713 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005714 if (cls.IsRegister()) {
5715 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5716 } else {
5717 DCHECK(cls.IsStackSlot()) << cls;
5718 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5719 }
5720 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005721
5722 // Otherwise, we need to check that the object's class is a non-primitive array.
5723 Location temp2_loc =
5724 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5725 if (kEmitCompilerReadBarrier) {
5726 // Save the value of `temp` into `temp2` before overwriting it
5727 // in the following move operation, as we will need it for the
5728 // read barrier below.
5729 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
5730 __ movl(temp2, temp);
5731 }
5732 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005733 __ movl(temp, Address(temp, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005734 codegen_->MaybeGenerateReadBarrier(
5735 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5736
5737 // If the component type is not null (i.e. the object is indeed
5738 // an array), jump to label `check_non_primitive_component_type`
5739 // to further check that this component type is not a primitive
5740 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005741 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005742 __ j(kNotEqual, &check_non_primitive_component_type);
5743 // Otherwise, jump to the slow path to throw the exception.
5744 //
5745 // But before, move back the object's class into `temp` before
5746 // going into the slow path, as it has been overwritten in the
5747 // meantime.
5748 // /* HeapReference<Class> */ temp = obj->klass_
5749 __ movl(temp, Address(obj, class_offset));
5750 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5751 __ jmp(type_check_slow_path->GetEntryLabel());
5752
5753 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005755 __ j(kEqual, &done);
5756 // Same comment as above regarding `temp` and the slow path.
5757 // /* HeapReference<Class> */ temp = obj->klass_
5758 __ movl(temp, Address(obj, class_offset));
5759 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5760 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005761 break;
5762 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005763
Calin Juravle98893e12015-10-02 21:05:03 +01005764 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005765 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005766 // We always go into the type check slow path for the unresolved &
5767 // interface check cases.
5768 //
5769 // We cannot directly call the CheckCast runtime entry point
5770 // without resorting to a type checking slow path here (i.e. by
5771 // calling InvokeRuntime directly), as it would require to
5772 // assign fixed registers for the inputs of this HInstanceOf
5773 // instruction (following the runtime calling convention), which
5774 // might be cluttered by the potential first read barrier
5775 // emission at the beginning of this method.
5776 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005777 break;
5778 }
5779 __ Bind(&done);
5780
Roland Levillain0d5a2812015-11-13 10:07:31 +00005781 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005782}
5783
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005784void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5785 LocationSummary* locations =
5786 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5787 InvokeRuntimeCallingConvention calling_convention;
5788 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5789}
5790
5791void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005792 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5793 : QUICK_ENTRY_POINT(pUnlockObject),
5794 instruction,
5795 instruction->GetDexPc(),
5796 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005797 if (instruction->IsEnter()) {
5798 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5799 } else {
5800 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5801 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005802}
5803
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005804void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5805void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5806void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5807
5808void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5809 LocationSummary* locations =
5810 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5811 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5812 || instruction->GetResultType() == Primitive::kPrimLong);
5813 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005814 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005815 locations->SetOut(Location::SameAsFirstInput());
5816}
5817
5818void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5819 HandleBitwiseOperation(instruction);
5820}
5821
5822void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5823 HandleBitwiseOperation(instruction);
5824}
5825
5826void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5827 HandleBitwiseOperation(instruction);
5828}
5829
5830void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5831 LocationSummary* locations = instruction->GetLocations();
5832 Location first = locations->InAt(0);
5833 Location second = locations->InAt(1);
5834 DCHECK(first.Equals(locations->Out()));
5835
5836 if (instruction->GetResultType() == Primitive::kPrimInt) {
5837 if (second.IsRegister()) {
5838 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005839 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005840 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005841 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005842 } else {
5843 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005844 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005845 }
5846 } else if (second.IsConstant()) {
5847 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5848 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005849 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005850 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005851 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005852 } else {
5853 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005854 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005855 }
5856 } else {
5857 Address address(CpuRegister(RSP), second.GetStackIndex());
5858 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005859 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005860 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005861 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005862 } else {
5863 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005864 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005865 }
5866 }
5867 } else {
5868 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005869 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5870 bool second_is_constant = false;
5871 int64_t value = 0;
5872 if (second.IsConstant()) {
5873 second_is_constant = true;
5874 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005875 }
Mark Mendell40741f32015-04-20 22:10:34 -04005876 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005877
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005878 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005879 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005880 if (is_int32_value) {
5881 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5882 } else {
5883 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5884 }
5885 } else if (second.IsDoubleStackSlot()) {
5886 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005887 } else {
5888 __ andq(first_reg, second.AsRegister<CpuRegister>());
5889 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005890 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005891 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005892 if (is_int32_value) {
5893 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5894 } else {
5895 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5896 }
5897 } else if (second.IsDoubleStackSlot()) {
5898 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005899 } else {
5900 __ orq(first_reg, second.AsRegister<CpuRegister>());
5901 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005902 } else {
5903 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005904 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005905 if (is_int32_value) {
5906 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5907 } else {
5908 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5909 }
5910 } else if (second.IsDoubleStackSlot()) {
5911 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005912 } else {
5913 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5914 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005915 }
5916 }
5917}
5918
Roland Levillain0d5a2812015-11-13 10:07:31 +00005919void CodeGeneratorX86_64::GenerateReadBarrier(HInstruction* instruction,
5920 Location out,
5921 Location ref,
5922 Location obj,
5923 uint32_t offset,
5924 Location index) {
5925 DCHECK(kEmitCompilerReadBarrier);
5926
5927 // If heap poisoning is enabled, the unpoisoning of the loaded
5928 // reference will be carried out by the runtime within the slow
5929 // path.
5930 //
5931 // Note that `ref` currently does not get unpoisoned (when heap
5932 // poisoning is enabled), which is alright as the `ref` argument is
5933 // not used by the artReadBarrierSlow entry point.
5934 //
5935 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5936 SlowPathCode* slow_path = new (GetGraph()->GetArena())
5937 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
5938 AddSlowPath(slow_path);
5939
5940 // TODO: When read barrier has a fast path, add it here.
5941 /* Currently the read barrier call is inserted after the original load.
5942 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
5943 * original load. This load-load ordering is required by the read barrier.
5944 * The fast path/slow path (for Baker's algorithm) should look like:
5945 *
5946 * bool isGray = obj.LockWord & kReadBarrierMask;
5947 * lfence; // load fence or artificial data dependence to prevent load-load reordering
5948 * ref = obj.field; // this is the original load
5949 * if (isGray) {
5950 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
5951 * }
5952 */
5953
5954 __ jmp(slow_path->GetEntryLabel());
5955 __ Bind(slow_path->GetExitLabel());
5956}
5957
5958void CodeGeneratorX86_64::MaybeGenerateReadBarrier(HInstruction* instruction,
5959 Location out,
5960 Location ref,
5961 Location obj,
5962 uint32_t offset,
5963 Location index) {
5964 if (kEmitCompilerReadBarrier) {
5965 // If heap poisoning is enabled, unpoisoning will be taken care of
5966 // by the runtime within the slow path.
5967 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
5968 } else if (kPoisonHeapReferences) {
5969 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
5970 }
5971}
5972
5973void CodeGeneratorX86_64::GenerateReadBarrierForRoot(HInstruction* instruction,
5974 Location out,
5975 Location root) {
5976 DCHECK(kEmitCompilerReadBarrier);
5977
5978 // Note that GC roots are not affected by heap poisoning, so we do
5979 // not need to do anything special for this here.
5980 SlowPathCode* slow_path =
5981 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
5982 AddSlowPath(slow_path);
5983
5984 // TODO: Implement a fast path for ReadBarrierForRoot, performing
5985 // the following operation (for Baker's algorithm):
5986 //
5987 // if (thread.tls32_.is_gc_marking) {
5988 // root = Mark(root);
5989 // }
5990
5991 __ jmp(slow_path->GetEntryLabel());
5992 __ Bind(slow_path->GetExitLabel());
5993}
5994
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005995void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005996 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005997 LOG(FATAL) << "Unreachable";
5998}
5999
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006000void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006001 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006002 LOG(FATAL) << "Unreachable";
6003}
6004
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006005void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
6006 DCHECK(codegen_->IsBaseline());
6007 LocationSummary* locations =
6008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6009 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6010}
6011
6012void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6013 DCHECK(codegen_->IsBaseline());
6014 // Will be generated at use site.
6015}
6016
Mark Mendellfe57faa2015-09-18 09:26:15 -04006017// Simple implementation of packed switch - generate cascaded compare/jumps.
6018void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6019 LocationSummary* locations =
6020 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6021 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006022 locations->AddTemp(Location::RequiresRegister());
6023 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006024}
6025
6026void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6027 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006028 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006029 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006030 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6031 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6032 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006033 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6034
6035 // Should we generate smaller inline compare/jumps?
6036 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6037 // Figure out the correct compare values and jump conditions.
6038 // Handle the first compare/branch as a special case because it might
6039 // jump to the default case.
6040 DCHECK_GT(num_entries, 2u);
6041 Condition first_condition;
6042 uint32_t index;
6043 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6044 if (lower_bound != 0) {
6045 first_condition = kLess;
6046 __ cmpl(value_reg_in, Immediate(lower_bound));
6047 __ j(first_condition, codegen_->GetLabelOf(default_block));
6048 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6049
6050 index = 1;
6051 } else {
6052 // Handle all the compare/jumps below.
6053 first_condition = kBelow;
6054 index = 0;
6055 }
6056
6057 // Handle the rest of the compare/jumps.
6058 for (; index + 1 < num_entries; index += 2) {
6059 int32_t compare_to_value = lower_bound + index + 1;
6060 __ cmpl(value_reg_in, Immediate(compare_to_value));
6061 // Jump to successors[index] if value < case_value[index].
6062 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6063 // Jump to successors[index + 1] if value == case_value[index + 1].
6064 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6065 }
6066
6067 if (index != num_entries) {
6068 // There are an odd number of entries. Handle the last one.
6069 DCHECK_EQ(index + 1, num_entries);
6070 __ cmpl(value_reg_in, Immediate(lower_bound + index));
6071 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6072 }
6073
6074 // And the default for any other value.
6075 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6076 __ jmp(codegen_->GetLabelOf(default_block));
6077 }
6078 return;
6079 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006080
6081 // Remove the bias, if needed.
6082 Register value_reg_out = value_reg_in.AsRegister();
6083 if (lower_bound != 0) {
6084 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6085 value_reg_out = temp_reg.AsRegister();
6086 }
6087 CpuRegister value_reg(value_reg_out);
6088
6089 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006090 __ cmpl(value_reg, Immediate(num_entries - 1));
6091 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006092
Mark Mendell9c86b482015-09-18 13:36:07 -04006093 // We are in the range of the table.
6094 // Load the address of the jump table in the constant area.
6095 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006096
Mark Mendell9c86b482015-09-18 13:36:07 -04006097 // Load the (signed) offset from the jump table.
6098 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6099
6100 // Add the offset to the address of the table base.
6101 __ addq(temp_reg, base_reg);
6102
6103 // And jump.
6104 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006105}
6106
Mark Mendell92e83bf2015-05-07 11:25:03 -04006107void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6108 if (value == 0) {
6109 __ xorl(dest, dest);
6110 } else if (value > 0 && IsInt<32>(value)) {
6111 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
6112 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6113 } else {
6114 __ movq(dest, Immediate(value));
6115 }
6116}
6117
Mark Mendellcfa410b2015-05-25 16:02:44 -04006118void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6119 DCHECK(dest.IsDoubleStackSlot());
6120 if (IsInt<32>(value)) {
6121 // Can move directly as an int32 constant.
6122 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6123 Immediate(static_cast<int32_t>(value)));
6124 } else {
6125 Load64BitValue(CpuRegister(TMP), value);
6126 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6127 }
6128}
6129
Mark Mendell9c86b482015-09-18 13:36:07 -04006130/**
6131 * Class to handle late fixup of offsets into constant area.
6132 */
6133class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6134 public:
6135 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6136 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6137
6138 protected:
6139 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6140
6141 CodeGeneratorX86_64* codegen_;
6142
6143 private:
6144 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6145 // Patch the correct offset for the instruction. We use the address of the
6146 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6147 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6148 int32_t relative_position = constant_offset - pos;
6149
6150 // Patch in the right value.
6151 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6152 }
6153
6154 // Location in constant area that the fixup refers to.
6155 size_t offset_into_constant_area_;
6156};
6157
6158/**
6159 t * Class to handle late fixup of offsets to a jump table that will be created in the
6160 * constant area.
6161 */
6162class JumpTableRIPFixup : public RIPFixup {
6163 public:
6164 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6165 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6166
6167 void CreateJumpTable() {
6168 X86_64Assembler* assembler = codegen_->GetAssembler();
6169
6170 // Ensure that the reference to the jump table has the correct offset.
6171 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6172 SetOffset(offset_in_constant_table);
6173
6174 // Compute the offset from the start of the function to this jump table.
6175 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6176
6177 // Populate the jump table with the correct values for the jump table.
6178 int32_t num_entries = switch_instr_->GetNumEntries();
6179 HBasicBlock* block = switch_instr_->GetBlock();
6180 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6181 // The value that we want is the target offset - the position of the table.
6182 for (int32_t i = 0; i < num_entries; i++) {
6183 HBasicBlock* b = successors[i];
6184 Label* l = codegen_->GetLabelOf(b);
6185 DCHECK(l->IsBound());
6186 int32_t offset_to_block = l->Position() - current_table_offset;
6187 assembler->AppendInt32(offset_to_block);
6188 }
6189 }
6190
6191 private:
6192 const HPackedSwitch* switch_instr_;
6193};
6194
Mark Mendellf55c3e02015-03-26 21:07:46 -04006195void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6196 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006197 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006198 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6199 // 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 -04006200 assembler->Align(4, 0);
6201 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006202
6203 // Populate any jump tables.
6204 for (auto jump_table : fixups_to_jump_tables_) {
6205 jump_table->CreateJumpTable();
6206 }
6207
6208 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006209 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006210 }
6211
6212 // And finish up.
6213 CodeGenerator::Finalize(allocator);
6214}
6215
Mark Mendellf55c3e02015-03-26 21:07:46 -04006216Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6217 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6218 return Address::RIP(fixup);
6219}
6220
6221Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6222 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6223 return Address::RIP(fixup);
6224}
6225
6226Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6227 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6228 return Address::RIP(fixup);
6229}
6230
6231Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6232 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6233 return Address::RIP(fixup);
6234}
6235
Andreas Gampe85b62f22015-09-09 13:15:38 -07006236// TODO: trg as memory.
6237void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6238 if (!trg.IsValid()) {
6239 DCHECK(type == Primitive::kPrimVoid);
6240 return;
6241 }
6242
6243 DCHECK_NE(type, Primitive::kPrimVoid);
6244
6245 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6246 if (trg.Equals(return_loc)) {
6247 return;
6248 }
6249
6250 // Let the parallel move resolver take care of all of this.
6251 HParallelMove parallel_move(GetGraph()->GetArena());
6252 parallel_move.AddMove(return_loc, trg, type, nullptr);
6253 GetMoveResolver()->EmitNativeCode(&parallel_move);
6254}
6255
Mark Mendell9c86b482015-09-18 13:36:07 -04006256Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6257 // Create a fixup to be used to create and address the jump table.
6258 JumpTableRIPFixup* table_fixup =
6259 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6260
6261 // We have to populate the jump tables.
6262 fixups_to_jump_tables_.push_back(table_fixup);
6263 return Address::RIP(table_fixup);
6264}
6265
Mark Mendellea5af682015-10-22 17:35:49 -04006266void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6267 const Address& addr_high,
6268 int64_t v,
6269 HInstruction* instruction) {
6270 if (IsInt<32>(v)) {
6271 int32_t v_32 = v;
6272 __ movq(addr_low, Immediate(v_32));
6273 MaybeRecordImplicitNullCheck(instruction);
6274 } else {
6275 // Didn't fit in a register. Do it in pieces.
6276 int32_t low_v = Low32Bits(v);
6277 int32_t high_v = High32Bits(v);
6278 __ movl(addr_low, Immediate(low_v));
6279 MaybeRecordImplicitNullCheck(instruction);
6280 __ movl(addr_high, Immediate(high_v));
6281 }
6282}
6283
Roland Levillain4d027112015-07-01 15:41:14 +01006284#undef __
6285
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006286} // namespace x86_64
6287} // namespace art