blob: 1fee192e36df94b0e53097814a5ac463be639023 [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;
Zheng Xu59f054d2015-12-07 17:17:03 +080044// 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();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003758 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003759 }
3760}
3761
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003762void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3763 LocationSummary* locations =
3764 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3765
3766 switch (ror->GetResultType()) {
3767 case Primitive::kPrimInt:
3768 case Primitive::kPrimLong: {
3769 locations->SetInAt(0, Location::RequiresRegister());
3770 // The shift count needs to be in CL (unless it is a constant).
3771 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3772 locations->SetOut(Location::SameAsFirstInput());
3773 break;
3774 }
3775 default:
3776 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3777 UNREACHABLE();
3778 }
3779}
3780
3781void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3782 LocationSummary* locations = ror->GetLocations();
3783 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3784 Location second = locations->InAt(1);
3785
3786 switch (ror->GetResultType()) {
3787 case Primitive::kPrimInt:
3788 if (second.IsRegister()) {
3789 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3790 __ rorl(first_reg, second_reg);
3791 } else {
3792 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3793 __ rorl(first_reg, imm);
3794 }
3795 break;
3796 case Primitive::kPrimLong:
3797 if (second.IsRegister()) {
3798 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3799 __ rorq(first_reg, second_reg);
3800 } else {
3801 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
3802 __ rorq(first_reg, imm);
3803 }
3804 break;
3805 default:
3806 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3807 UNREACHABLE();
3808 }
3809}
3810
Calin Juravle9aec02f2014-11-18 23:06:35 +00003811void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3812 HandleShift(shl);
3813}
3814
3815void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3816 HandleShift(shl);
3817}
3818
3819void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3820 HandleShift(shr);
3821}
3822
3823void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3824 HandleShift(shr);
3825}
3826
3827void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3828 HandleShift(ushr);
3829}
3830
3831void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3832 HandleShift(ushr);
3833}
3834
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003835void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003836 LocationSummary* locations =
3837 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003838 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003839 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3840 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003841 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003842}
3843
3844void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003845 // Note: if heap poisoning is enabled, the entry point takes cares
3846 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003847 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3848 instruction,
3849 instruction->GetDexPc(),
3850 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003851 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003852
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003853 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003854}
3855
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003856void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3857 LocationSummary* locations =
3858 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3859 InvokeRuntimeCallingConvention calling_convention;
3860 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003861 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003862 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003863 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003864}
3865
3866void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3867 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003868 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3869 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003870 // Note: if heap poisoning is enabled, the entry point takes cares
3871 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003872 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3873 instruction,
3874 instruction->GetDexPc(),
3875 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003876 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003877
3878 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003879}
3880
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003881void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003882 LocationSummary* locations =
3883 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003884 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3885 if (location.IsStackSlot()) {
3886 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3887 } else if (location.IsDoubleStackSlot()) {
3888 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3889 }
3890 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003891}
3892
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003893void InstructionCodeGeneratorX86_64::VisitParameterValue(
3894 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003895 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003896}
3897
3898void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3899 LocationSummary* locations =
3900 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3901 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3902}
3903
3904void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3905 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3906 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003907}
3908
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003909void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003910 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003911 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003912 locations->SetInAt(0, Location::RequiresRegister());
3913 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003914}
3915
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003916void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3917 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003918 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3919 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003920 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003921 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003922 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003923 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003924 break;
3925
3926 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003927 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003928 break;
3929
3930 default:
3931 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3932 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003933}
3934
David Brazdil66d126e2015-04-03 16:02:44 +01003935void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3936 LocationSummary* locations =
3937 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3938 locations->SetInAt(0, Location::RequiresRegister());
3939 locations->SetOut(Location::SameAsFirstInput());
3940}
3941
3942void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003943 LocationSummary* locations = bool_not->GetLocations();
3944 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3945 locations->Out().AsRegister<CpuRegister>().AsRegister());
3946 Location out = locations->Out();
3947 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3948}
3949
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003950void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003951 LocationSummary* locations =
3952 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003953 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3954 locations->SetInAt(i, Location::Any());
3955 }
3956 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003957}
3958
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003959void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003960 LOG(FATAL) << "Unimplemented";
3961}
3962
Calin Juravle52c48962014-12-16 17:02:57 +00003963void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3964 /*
3965 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3966 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3967 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3968 */
3969 switch (kind) {
3970 case MemBarrierKind::kAnyAny: {
3971 __ mfence();
3972 break;
3973 }
3974 case MemBarrierKind::kAnyStore:
3975 case MemBarrierKind::kLoadAny:
3976 case MemBarrierKind::kStoreStore: {
3977 // nop
3978 break;
3979 }
3980 default:
3981 LOG(FATAL) << "Unexpected memory barier " << kind;
3982 }
3983}
3984
3985void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3986 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3987
Roland Levillain0d5a2812015-11-13 10:07:31 +00003988 bool object_field_get_with_read_barrier =
3989 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003990 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00003991 new (GetGraph()->GetArena()) LocationSummary(instruction,
3992 object_field_get_with_read_barrier ?
3993 LocationSummary::kCallOnSlowPath :
3994 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003995 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003996 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3997 locations->SetOut(Location::RequiresFpuRegister());
3998 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00003999 // The output overlaps for an object field get when read barriers
4000 // are enabled: we do not want the move to overwrite the object's
4001 // location, as we need it to emit the read barrier.
4002 locations->SetOut(
4003 Location::RequiresRegister(),
4004 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004005 }
Calin Juravle52c48962014-12-16 17:02:57 +00004006}
4007
4008void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4009 const FieldInfo& field_info) {
4010 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4011
4012 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004013 Location base_loc = locations->InAt(0);
4014 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004015 Location out = locations->Out();
4016 bool is_volatile = field_info.IsVolatile();
4017 Primitive::Type field_type = field_info.GetFieldType();
4018 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4019
4020 switch (field_type) {
4021 case Primitive::kPrimBoolean: {
4022 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4023 break;
4024 }
4025
4026 case Primitive::kPrimByte: {
4027 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4028 break;
4029 }
4030
4031 case Primitive::kPrimShort: {
4032 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4033 break;
4034 }
4035
4036 case Primitive::kPrimChar: {
4037 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4038 break;
4039 }
4040
4041 case Primitive::kPrimInt:
4042 case Primitive::kPrimNot: {
4043 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4044 break;
4045 }
4046
4047 case Primitive::kPrimLong: {
4048 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4049 break;
4050 }
4051
4052 case Primitive::kPrimFloat: {
4053 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4054 break;
4055 }
4056
4057 case Primitive::kPrimDouble: {
4058 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4059 break;
4060 }
4061
4062 case Primitive::kPrimVoid:
4063 LOG(FATAL) << "Unreachable type " << field_type;
4064 UNREACHABLE();
4065 }
4066
Calin Juravle77520bc2015-01-12 18:45:46 +00004067 codegen_->MaybeRecordImplicitNullCheck(instruction);
4068
Calin Juravle52c48962014-12-16 17:02:57 +00004069 if (is_volatile) {
4070 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4071 }
Roland Levillain4d027112015-07-01 15:41:14 +01004072
4073 if (field_type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004074 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004075 }
Calin Juravle52c48962014-12-16 17:02:57 +00004076}
4077
4078void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4079 const FieldInfo& field_info) {
4080 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4081
4082 LocationSummary* locations =
4083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004084 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004085 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004086 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004087 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004088
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004089 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004090 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004091 if (is_volatile) {
4092 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4093 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4094 } else {
4095 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4096 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004097 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004098 if (is_volatile) {
4099 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4100 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4101 } else {
4102 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4103 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004104 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004105 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004106 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004107 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004108 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004109 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4110 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004111 locations->AddTemp(Location::RequiresRegister());
4112 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004113}
4114
Calin Juravle52c48962014-12-16 17:02:57 +00004115void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004116 const FieldInfo& field_info,
4117 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004118 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4119
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004120 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004121 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4122 Location value = locations->InAt(1);
4123 bool is_volatile = field_info.IsVolatile();
4124 Primitive::Type field_type = field_info.GetFieldType();
4125 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4126
4127 if (is_volatile) {
4128 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4129 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004130
Mark Mendellea5af682015-10-22 17:35:49 -04004131 bool maybe_record_implicit_null_check_done = false;
4132
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004133 switch (field_type) {
4134 case Primitive::kPrimBoolean:
4135 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004136 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004137 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004138 __ movb(Address(base, offset), Immediate(v));
4139 } else {
4140 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4141 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004142 break;
4143 }
4144
4145 case Primitive::kPrimShort:
4146 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004147 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004148 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004149 __ movw(Address(base, offset), Immediate(v));
4150 } else {
4151 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4152 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004153 break;
4154 }
4155
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004156 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004157 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004158 if (value.IsConstant()) {
4159 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004160 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4161 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4162 // Note: if heap poisoning is enabled, no need to poison
4163 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004164 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004165 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004166 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4167 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4168 __ movl(temp, value.AsRegister<CpuRegister>());
4169 __ PoisonHeapReference(temp);
4170 __ movl(Address(base, offset), temp);
4171 } else {
4172 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4173 }
Mark Mendell40741f32015-04-20 22:10:34 -04004174 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004175 break;
4176 }
4177
4178 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004179 if (value.IsConstant()) {
4180 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004181 codegen_->MoveInt64ToAddress(Address(base, offset),
4182 Address(base, offset + sizeof(int32_t)),
4183 v,
4184 instruction);
4185 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004186 } else {
4187 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4188 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004189 break;
4190 }
4191
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004192 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004193 if (value.IsConstant()) {
4194 int32_t v =
4195 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4196 __ movl(Address(base, offset), Immediate(v));
4197 } else {
4198 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4199 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004200 break;
4201 }
4202
4203 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004204 if (value.IsConstant()) {
4205 int64_t v =
4206 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4207 codegen_->MoveInt64ToAddress(Address(base, offset),
4208 Address(base, offset + sizeof(int32_t)),
4209 v,
4210 instruction);
4211 maybe_record_implicit_null_check_done = true;
4212 } else {
4213 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4214 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004215 break;
4216 }
4217
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004218 case Primitive::kPrimVoid:
4219 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004220 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004221 }
Calin Juravle52c48962014-12-16 17:02:57 +00004222
Mark Mendellea5af682015-10-22 17:35:49 -04004223 if (!maybe_record_implicit_null_check_done) {
4224 codegen_->MaybeRecordImplicitNullCheck(instruction);
4225 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004226
4227 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4228 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4229 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004230 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004231 }
4232
Calin Juravle52c48962014-12-16 17:02:57 +00004233 if (is_volatile) {
4234 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4235 }
4236}
4237
4238void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4239 HandleFieldSet(instruction, instruction->GetFieldInfo());
4240}
4241
4242void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004243 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004244}
4245
4246void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004247 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004248}
4249
4250void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004251 HandleFieldGet(instruction, instruction->GetFieldInfo());
4252}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253
Calin Juravle52c48962014-12-16 17:02:57 +00004254void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4255 HandleFieldGet(instruction);
4256}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004257
Calin Juravle52c48962014-12-16 17:02:57 +00004258void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4259 HandleFieldGet(instruction, instruction->GetFieldInfo());
4260}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004261
Calin Juravle52c48962014-12-16 17:02:57 +00004262void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4263 HandleFieldSet(instruction, instruction->GetFieldInfo());
4264}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004265
Calin Juravle52c48962014-12-16 17:02:57 +00004266void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004267 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004268}
4269
Calin Juravlee460d1d2015-09-29 04:52:17 +01004270void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4271 HUnresolvedInstanceFieldGet* instruction) {
4272 FieldAccessCallingConventionX86_64 calling_convention;
4273 codegen_->CreateUnresolvedFieldLocationSummary(
4274 instruction, instruction->GetFieldType(), calling_convention);
4275}
4276
4277void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4278 HUnresolvedInstanceFieldGet* instruction) {
4279 FieldAccessCallingConventionX86_64 calling_convention;
4280 codegen_->GenerateUnresolvedFieldAccess(instruction,
4281 instruction->GetFieldType(),
4282 instruction->GetFieldIndex(),
4283 instruction->GetDexPc(),
4284 calling_convention);
4285}
4286
4287void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4288 HUnresolvedInstanceFieldSet* instruction) {
4289 FieldAccessCallingConventionX86_64 calling_convention;
4290 codegen_->CreateUnresolvedFieldLocationSummary(
4291 instruction, instruction->GetFieldType(), calling_convention);
4292}
4293
4294void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4295 HUnresolvedInstanceFieldSet* instruction) {
4296 FieldAccessCallingConventionX86_64 calling_convention;
4297 codegen_->GenerateUnresolvedFieldAccess(instruction,
4298 instruction->GetFieldType(),
4299 instruction->GetFieldIndex(),
4300 instruction->GetDexPc(),
4301 calling_convention);
4302}
4303
4304void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4305 HUnresolvedStaticFieldGet* instruction) {
4306 FieldAccessCallingConventionX86_64 calling_convention;
4307 codegen_->CreateUnresolvedFieldLocationSummary(
4308 instruction, instruction->GetFieldType(), calling_convention);
4309}
4310
4311void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4312 HUnresolvedStaticFieldGet* instruction) {
4313 FieldAccessCallingConventionX86_64 calling_convention;
4314 codegen_->GenerateUnresolvedFieldAccess(instruction,
4315 instruction->GetFieldType(),
4316 instruction->GetFieldIndex(),
4317 instruction->GetDexPc(),
4318 calling_convention);
4319}
4320
4321void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4322 HUnresolvedStaticFieldSet* instruction) {
4323 FieldAccessCallingConventionX86_64 calling_convention;
4324 codegen_->CreateUnresolvedFieldLocationSummary(
4325 instruction, instruction->GetFieldType(), calling_convention);
4326}
4327
4328void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4329 HUnresolvedStaticFieldSet* instruction) {
4330 FieldAccessCallingConventionX86_64 calling_convention;
4331 codegen_->GenerateUnresolvedFieldAccess(instruction,
4332 instruction->GetFieldType(),
4333 instruction->GetFieldIndex(),
4334 instruction->GetDexPc(),
4335 calling_convention);
4336}
4337
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004338void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004339 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4340 ? LocationSummary::kCallOnSlowPath
4341 : LocationSummary::kNoCall;
4342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4343 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004344 ? Location::RequiresRegister()
4345 : Location::Any();
4346 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004347 if (instruction->HasUses()) {
4348 locations->SetOut(Location::SameAsFirstInput());
4349 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004350}
4351
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004352void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004353 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4354 return;
4355 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004356 LocationSummary* locations = instruction->GetLocations();
4357 Location obj = locations->InAt(0);
4358
4359 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4360 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4361}
4362
4363void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004364 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004365 codegen_->AddSlowPath(slow_path);
4366
4367 LocationSummary* locations = instruction->GetLocations();
4368 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004369
4370 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004371 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004372 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004373 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004374 } else {
4375 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004376 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004377 __ jmp(slow_path->GetEntryLabel());
4378 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004379 }
4380 __ j(kEqual, slow_path->GetEntryLabel());
4381}
4382
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004383void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004384 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004385 GenerateImplicitNullCheck(instruction);
4386 } else {
4387 GenerateExplicitNullCheck(instruction);
4388 }
4389}
4390
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004391void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004392 bool object_array_get_with_read_barrier =
4393 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004394 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004395 new (GetGraph()->GetArena()) LocationSummary(instruction,
4396 object_array_get_with_read_barrier ?
4397 LocationSummary::kCallOnSlowPath :
4398 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004399 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004400 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004401 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4402 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4403 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004404 // The output overlaps for an object array get when read barriers
4405 // are enabled: we do not want the move to overwrite the array's
4406 // location, as we need it to emit the read barrier.
4407 locations->SetOut(
4408 Location::RequiresRegister(),
4409 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004410 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004411}
4412
4413void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4414 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004415 Location obj_loc = locations->InAt(0);
4416 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004417 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004418 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004419
Roland Levillain4d027112015-07-01 15:41:14 +01004420 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004421 case Primitive::kPrimBoolean: {
4422 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004423 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004424 if (index.IsConstant()) {
4425 __ movzxb(out, Address(obj,
4426 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4427 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004428 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004429 }
4430 break;
4431 }
4432
4433 case Primitive::kPrimByte: {
4434 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004435 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004436 if (index.IsConstant()) {
4437 __ movsxb(out, Address(obj,
4438 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4439 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004440 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004441 }
4442 break;
4443 }
4444
4445 case Primitive::kPrimShort: {
4446 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004447 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004448 if (index.IsConstant()) {
4449 __ movsxw(out, Address(obj,
4450 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4451 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004452 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004453 }
4454 break;
4455 }
4456
4457 case Primitive::kPrimChar: {
4458 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004459 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004460 if (index.IsConstant()) {
4461 __ movzxw(out, Address(obj,
4462 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4463 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004464 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004465 }
4466 break;
4467 }
4468
4469 case Primitive::kPrimInt:
4470 case Primitive::kPrimNot: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004471 static_assert(
4472 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4473 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004474 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004475 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004476 if (index.IsConstant()) {
4477 __ movl(out, Address(obj,
4478 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4479 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004480 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004481 }
4482 break;
4483 }
4484
4485 case Primitive::kPrimLong: {
4486 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004487 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004488 if (index.IsConstant()) {
4489 __ movq(out, Address(obj,
4490 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4491 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004492 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004493 }
4494 break;
4495 }
4496
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004497 case Primitive::kPrimFloat: {
4498 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004499 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004500 if (index.IsConstant()) {
4501 __ movss(out, Address(obj,
4502 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4503 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004504 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004505 }
4506 break;
4507 }
4508
4509 case Primitive::kPrimDouble: {
4510 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004511 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004512 if (index.IsConstant()) {
4513 __ movsd(out, Address(obj,
4514 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4515 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004516 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004517 }
4518 break;
4519 }
4520
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004521 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004522 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004523 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004524 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004525 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004526
4527 if (type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004528 static_assert(
4529 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4530 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4531 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4532 Location out = locations->Out();
4533 if (index.IsConstant()) {
4534 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4535 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4536 } else {
4537 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4538 }
Roland Levillain4d027112015-07-01 15:41:14 +01004539 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540}
4541
4542void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004543 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004544
4545 bool needs_write_barrier =
4546 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004547 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004548 bool object_array_set_with_read_barrier =
4549 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004550
Nicolas Geoffray39468442014-09-02 15:17:15 +01004551 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004552 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004553 (may_need_runtime_call || object_array_set_with_read_barrier) ?
4554 LocationSummary::kCallOnSlowPath :
4555 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004556
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004557 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004558 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4559 if (Primitive::IsFloatingPointType(value_type)) {
4560 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004561 } else {
4562 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4563 }
4564
4565 if (needs_write_barrier) {
4566 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004567
4568 // This first temporary register is possibly used for heap
4569 // reference poisoning and/or read barrier emission too.
4570 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004571 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004572 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004573}
4574
4575void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4576 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004577 Location array_loc = locations->InAt(0);
4578 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004579 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004580 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004581 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004582 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004583 bool needs_write_barrier =
4584 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004585 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4586 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4587 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004588
4589 switch (value_type) {
4590 case Primitive::kPrimBoolean:
4591 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004592 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4593 Address address = index.IsConstant()
4594 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4595 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4596 if (value.IsRegister()) {
4597 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004599 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004601 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004602 break;
4603 }
4604
4605 case Primitive::kPrimShort:
4606 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004607 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4608 Address address = index.IsConstant()
4609 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4610 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4611 if (value.IsRegister()) {
4612 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004613 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004614 DCHECK(value.IsConstant()) << value;
4615 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004617 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004618 break;
4619 }
4620
4621 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004622 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4623 Address address = index.IsConstant()
4624 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4625 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004626
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004627 if (!value.IsRegister()) {
4628 // Just setting null.
4629 DCHECK(instruction->InputAt(2)->IsNullConstant());
4630 DCHECK(value.IsConstant()) << value;
4631 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004632 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004633 DCHECK(!needs_write_barrier);
4634 DCHECK(!may_need_runtime_call);
4635 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004636 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004637
4638 DCHECK(needs_write_barrier);
4639 CpuRegister register_value = value.AsRegister<CpuRegister>();
4640 NearLabel done, not_null, do_put;
4641 SlowPathCode* slow_path = nullptr;
4642 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4643 if (may_need_runtime_call) {
4644 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4645 codegen_->AddSlowPath(slow_path);
4646 if (instruction->GetValueCanBeNull()) {
4647 __ testl(register_value, register_value);
4648 __ j(kNotEqual, &not_null);
4649 __ movl(address, Immediate(0));
4650 codegen_->MaybeRecordImplicitNullCheck(instruction);
4651 __ jmp(&done);
4652 __ Bind(&not_null);
4653 }
4654
Roland Levillain0d5a2812015-11-13 10:07:31 +00004655 if (kEmitCompilerReadBarrier) {
4656 // When read barriers are enabled, the type checking
4657 // instrumentation requires two read barriers:
4658 //
4659 // __ movl(temp2, temp);
4660 // // /* HeapReference<Class> */ temp = temp->component_type_
4661 // __ movl(temp, Address(temp, component_offset));
4662 // codegen_->GenerateReadBarrier(
4663 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4664 //
4665 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4666 // __ movl(temp2, Address(register_value, class_offset));
4667 // codegen_->GenerateReadBarrier(
4668 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4669 //
4670 // __ cmpl(temp, temp2);
4671 //
4672 // However, the second read barrier may trash `temp`, as it
4673 // is a temporary register, and as such would not be saved
4674 // along with live registers before calling the runtime (nor
4675 // restored afterwards). So in this case, we bail out and
4676 // delegate the work to the array set slow path.
4677 //
4678 // TODO: Extend the register allocator to support a new
4679 // "(locally) live temp" location so as to avoid always
4680 // going into the slow path when read barriers are enabled.
4681 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004682 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004683 // /* HeapReference<Class> */ temp = array->klass_
4684 __ movl(temp, Address(array, class_offset));
4685 codegen_->MaybeRecordImplicitNullCheck(instruction);
4686 __ MaybeUnpoisonHeapReference(temp);
4687
4688 // /* HeapReference<Class> */ temp = temp->component_type_
4689 __ movl(temp, Address(temp, component_offset));
4690 // If heap poisoning is enabled, no need to unpoison `temp`
4691 // nor the object reference in `register_value->klass`, as
4692 // we are comparing two poisoned references.
4693 __ cmpl(temp, Address(register_value, class_offset));
4694
4695 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4696 __ j(kEqual, &do_put);
4697 // If heap poisoning is enabled, the `temp` reference has
4698 // not been unpoisoned yet; unpoison it now.
4699 __ MaybeUnpoisonHeapReference(temp);
4700
4701 // /* HeapReference<Class> */ temp = temp->super_class_
4702 __ movl(temp, Address(temp, super_offset));
4703 // If heap poisoning is enabled, no need to unpoison
4704 // `temp`, as we are comparing against null below.
4705 __ testl(temp, temp);
4706 __ j(kNotEqual, slow_path->GetEntryLabel());
4707 __ Bind(&do_put);
4708 } else {
4709 __ j(kNotEqual, slow_path->GetEntryLabel());
4710 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004711 }
4712 }
4713
4714 if (kPoisonHeapReferences) {
4715 __ movl(temp, register_value);
4716 __ PoisonHeapReference(temp);
4717 __ movl(address, temp);
4718 } else {
4719 __ movl(address, register_value);
4720 }
4721 if (!may_need_runtime_call) {
4722 codegen_->MaybeRecordImplicitNullCheck(instruction);
4723 }
4724
4725 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4726 codegen_->MarkGCCard(
4727 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4728 __ Bind(&done);
4729
4730 if (slow_path != nullptr) {
4731 __ Bind(slow_path->GetExitLabel());
4732 }
4733
4734 break;
4735 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004736
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004737 case Primitive::kPrimInt: {
4738 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4739 Address address = index.IsConstant()
4740 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4741 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4742 if (value.IsRegister()) {
4743 __ movl(address, value.AsRegister<CpuRegister>());
4744 } else {
4745 DCHECK(value.IsConstant()) << value;
4746 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4747 __ movl(address, Immediate(v));
4748 }
4749 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 break;
4751 }
4752
4753 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004754 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4755 Address address = index.IsConstant()
4756 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4757 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4758 if (value.IsRegister()) {
4759 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004760 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004761 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004762 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004763 Address address_high = index.IsConstant()
4764 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4765 offset + sizeof(int32_t))
4766 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4767 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768 }
4769 break;
4770 }
4771
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004772 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4774 Address address = index.IsConstant()
4775 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4776 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004777 if (value.IsFpuRegister()) {
4778 __ movss(address, value.AsFpuRegister<XmmRegister>());
4779 } else {
4780 DCHECK(value.IsConstant());
4781 int32_t v =
4782 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4783 __ movl(address, Immediate(v));
4784 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004785 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004786 break;
4787 }
4788
4789 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004790 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4791 Address address = index.IsConstant()
4792 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4793 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004794 if (value.IsFpuRegister()) {
4795 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4796 codegen_->MaybeRecordImplicitNullCheck(instruction);
4797 } else {
4798 int64_t v =
4799 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4800 Address address_high = index.IsConstant()
4801 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4802 offset + sizeof(int32_t))
4803 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4804 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4805 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004806 break;
4807 }
4808
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004809 case Primitive::kPrimVoid:
4810 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004811 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004812 }
4813}
4814
4815void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004816 LocationSummary* locations =
4817 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004818 locations->SetInAt(0, Location::RequiresRegister());
4819 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004820}
4821
4822void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4823 LocationSummary* locations = instruction->GetLocations();
4824 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004825 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4826 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004827 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004828 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004829}
4830
4831void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004832 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4833 ? LocationSummary::kCallOnSlowPath
4834 : LocationSummary::kNoCall;
4835 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004836 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004837 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004838 if (instruction->HasUses()) {
4839 locations->SetOut(Location::SameAsFirstInput());
4840 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004841}
4842
4843void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4844 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004845 Location index_loc = locations->InAt(0);
4846 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004847 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004848 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004849
Mark Mendell99dbd682015-04-22 16:18:52 -04004850 if (length_loc.IsConstant()) {
4851 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4852 if (index_loc.IsConstant()) {
4853 // BCE will remove the bounds check if we are guarenteed to pass.
4854 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4855 if (index < 0 || index >= length) {
4856 codegen_->AddSlowPath(slow_path);
4857 __ jmp(slow_path->GetEntryLabel());
4858 } else {
4859 // Some optimization after BCE may have generated this, and we should not
4860 // generate a bounds check if it is a valid range.
4861 }
4862 return;
4863 }
4864
4865 // We have to reverse the jump condition because the length is the constant.
4866 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4867 __ cmpl(index_reg, Immediate(length));
4868 codegen_->AddSlowPath(slow_path);
4869 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004870 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004871 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4872 if (index_loc.IsConstant()) {
4873 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4874 __ cmpl(length, Immediate(value));
4875 } else {
4876 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4877 }
4878 codegen_->AddSlowPath(slow_path);
4879 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004880 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004881}
4882
4883void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4884 CpuRegister card,
4885 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004886 CpuRegister value,
4887 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004888 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004889 if (value_can_be_null) {
4890 __ testl(value, value);
4891 __ j(kEqual, &is_null);
4892 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004893 __ gs()->movq(card, Address::Absolute(
4894 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4895 __ movq(temp, object);
4896 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004897 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004898 if (value_can_be_null) {
4899 __ Bind(&is_null);
4900 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004901}
4902
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004903void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4904 temp->SetLocations(nullptr);
4905}
4906
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004907void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004908 // Nothing to do, this is driven by the code generator.
4909}
4910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004911void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004912 LOG(FATAL) << "Unimplemented";
4913}
4914
4915void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004916 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4917}
4918
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004919void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4921}
4922
4923void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004924 HBasicBlock* block = instruction->GetBlock();
4925 if (block->GetLoopInformation() != nullptr) {
4926 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4927 // The back edge will generate the suspend check.
4928 return;
4929 }
4930 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4931 // The goto will generate the suspend check.
4932 return;
4933 }
4934 GenerateSuspendCheck(instruction, nullptr);
4935}
4936
4937void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4938 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004939 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004940 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4941 if (slow_path == nullptr) {
4942 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4943 instruction->SetSlowPath(slow_path);
4944 codegen_->AddSlowPath(slow_path);
4945 if (successor != nullptr) {
4946 DCHECK(successor->IsLoopHeader());
4947 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4948 }
4949 } else {
4950 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4951 }
4952
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004953 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004954 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004955 if (successor == nullptr) {
4956 __ j(kNotEqual, slow_path->GetEntryLabel());
4957 __ Bind(slow_path->GetReturnLabel());
4958 } else {
4959 __ j(kEqual, codegen_->GetLabelOf(successor));
4960 __ jmp(slow_path->GetEntryLabel());
4961 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004962}
4963
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004964X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4965 return codegen_->GetAssembler();
4966}
4967
4968void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004969 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004970 Location source = move->GetSource();
4971 Location destination = move->GetDestination();
4972
4973 if (source.IsRegister()) {
4974 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004975 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004976 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004977 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004978 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004979 } else {
4980 DCHECK(destination.IsDoubleStackSlot());
4981 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004982 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004983 }
4984 } else if (source.IsStackSlot()) {
4985 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004986 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004987 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004988 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004989 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004990 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004991 } else {
4992 DCHECK(destination.IsStackSlot());
4993 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4994 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4995 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004996 } else if (source.IsDoubleStackSlot()) {
4997 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004998 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004999 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005000 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005001 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5002 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005003 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005004 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005005 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5006 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5007 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005008 } else if (source.IsConstant()) {
5009 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005010 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5011 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005012 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005013 if (value == 0) {
5014 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5015 } else {
5016 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5017 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005018 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005019 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005020 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005021 }
5022 } else if (constant->IsLongConstant()) {
5023 int64_t value = constant->AsLongConstant()->GetValue();
5024 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005025 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005026 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005027 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005028 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005029 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005030 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005031 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005032 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005033 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005034 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5035 if (value == 0) {
5036 // easy FP 0.0.
5037 __ xorps(dest, dest);
5038 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005039 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005040 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005041 } else {
5042 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04005043 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005044 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5045 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005046 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005047 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005048 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005049 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005050 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005051 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5052 if (value == 0) {
5053 __ xorpd(dest, dest);
5054 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005055 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005056 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005057 } else {
5058 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005059 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005060 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005061 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005062 } else if (source.IsFpuRegister()) {
5063 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005064 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005065 } else if (destination.IsStackSlot()) {
5066 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005067 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005068 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005069 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005070 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005071 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005072 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005073 }
5074}
5075
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005076void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005077 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005078 __ movl(Address(CpuRegister(RSP), mem), reg);
5079 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005080}
5081
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005082void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005083 ScratchRegisterScope ensure_scratch(
5084 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5085
5086 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5087 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5088 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5089 Address(CpuRegister(RSP), mem2 + stack_offset));
5090 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5091 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5092 CpuRegister(ensure_scratch.GetRegister()));
5093}
5094
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005095void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5096 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5097 __ movq(Address(CpuRegister(RSP), mem), reg);
5098 __ movq(reg, CpuRegister(TMP));
5099}
5100
5101void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5102 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005103 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005104
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005105 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5106 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5107 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5108 Address(CpuRegister(RSP), mem2 + stack_offset));
5109 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5110 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5111 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005112}
5113
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005114void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5115 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5116 __ movss(Address(CpuRegister(RSP), mem), reg);
5117 __ movd(reg, CpuRegister(TMP));
5118}
5119
5120void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5121 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5122 __ movsd(Address(CpuRegister(RSP), mem), reg);
5123 __ movd(reg, CpuRegister(TMP));
5124}
5125
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005126void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005127 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005128 Location source = move->GetSource();
5129 Location destination = move->GetDestination();
5130
5131 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005132 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005133 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005134 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005135 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005136 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005137 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005138 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5139 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005140 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005141 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005142 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005143 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5144 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005145 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005146 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5147 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5148 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005149 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005150 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005151 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005152 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005153 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005154 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005155 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005156 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005157 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005158 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005159 }
5160}
5161
5162
5163void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5164 __ pushq(CpuRegister(reg));
5165}
5166
5167
5168void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5169 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005170}
5171
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005172void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005173 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005174 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5175 Immediate(mirror::Class::kStatusInitialized));
5176 __ j(kLess, slow_path->GetEntryLabel());
5177 __ Bind(slow_path->GetExitLabel());
5178 // No need for memory fence, thanks to the X86_64 memory model.
5179}
5180
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005181void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005182 InvokeRuntimeCallingConvention calling_convention;
5183 CodeGenerator::CreateLoadClassLocationSummary(
5184 cls,
5185 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005186 Location::RegisterLocation(RAX),
5187 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005188}
5189
5190void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005191 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005192 if (cls->NeedsAccessCheck()) {
5193 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5194 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5195 cls,
5196 cls->GetDexPc(),
5197 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005198 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005199 return;
5200 }
5201
Roland Levillain0d5a2812015-11-13 10:07:31 +00005202 Location out_loc = locations->Out();
5203 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005204 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005205
Calin Juravle580b6092015-10-06 17:35:58 +01005206 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005207 DCHECK(!cls->CanCallRuntime());
5208 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005209 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5210 if (kEmitCompilerReadBarrier) {
5211 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5212 __ leaq(out, Address(current_method, declaring_class_offset));
5213 // /* mirror::Class* */ out = out->Read()
5214 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5215 } else {
5216 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5217 __ movl(out, Address(current_method, declaring_class_offset));
5218 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005219 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005220 // /* GcRoot<mirror::Class>[] */ out =
5221 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5222 __ movq(out, Address(current_method,
5223 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5224
5225 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5226 if (kEmitCompilerReadBarrier) {
5227 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
5228 __ leaq(out, Address(out, cache_offset));
5229 // /* mirror::Class* */ out = out->Read()
5230 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5231 } else {
5232 // /* GcRoot<mirror::Class> */ out = out[type_index]
5233 __ movl(out, Address(out, cache_offset));
5234 }
Roland Levillain4d027112015-07-01 15:41:14 +01005235
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005236 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5237 DCHECK(cls->CanCallRuntime());
5238 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5239 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5240 codegen_->AddSlowPath(slow_path);
5241 if (!cls->IsInDexCache()) {
5242 __ testl(out, out);
5243 __ j(kEqual, slow_path->GetEntryLabel());
5244 }
5245 if (cls->MustGenerateClinitCheck()) {
5246 GenerateClassInitializationCheck(slow_path, out);
5247 } else {
5248 __ Bind(slow_path->GetExitLabel());
5249 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005250 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005251 }
5252}
5253
5254void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5255 LocationSummary* locations =
5256 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5257 locations->SetInAt(0, Location::RequiresRegister());
5258 if (check->HasUses()) {
5259 locations->SetOut(Location::SameAsFirstInput());
5260 }
5261}
5262
5263void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005264 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005265 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005266 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005267 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005268 GenerateClassInitializationCheck(slow_path,
5269 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005270}
5271
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005272void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005273 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5274 ? LocationSummary::kCallOnSlowPath
5275 : LocationSummary::kNoCall;
5276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005277 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005278 locations->SetOut(Location::RequiresRegister());
5279}
5280
5281void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005282 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005283 Location out_loc = locations->Out();
5284 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005285 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005286
5287 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5288 if (kEmitCompilerReadBarrier) {
5289 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5290 __ leaq(out, Address(current_method, declaring_class_offset));
5291 // /* mirror::Class* */ out = out->Read()
5292 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5293 } else {
5294 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5295 __ movl(out, Address(current_method, declaring_class_offset));
5296 }
5297
5298 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5299 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5300
5301 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5302 if (kEmitCompilerReadBarrier) {
5303 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5304 __ leaq(out, Address(out, cache_offset));
5305 // /* mirror::String* */ out = out->Read()
5306 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5307 } else {
5308 // /* GcRoot<mirror::String> */ out = out[string_index]
5309 __ movl(out, Address(out, cache_offset));
5310 }
5311
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005312 if (!load->IsInDexCache()) {
5313 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5314 codegen_->AddSlowPath(slow_path);
5315 __ testl(out, out);
5316 __ j(kEqual, slow_path->GetEntryLabel());
5317 __ Bind(slow_path->GetExitLabel());
5318 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005319}
5320
David Brazdilcb1c0552015-08-04 16:22:25 +01005321static Address GetExceptionTlsAddress() {
5322 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
5323}
5324
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005325void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5326 LocationSummary* locations =
5327 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5328 locations->SetOut(Location::RequiresRegister());
5329}
5330
5331void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005332 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5333}
5334
5335void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5336 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5337}
5338
5339void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5340 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005341}
5342
5343void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5344 LocationSummary* locations =
5345 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5346 InvokeRuntimeCallingConvention calling_convention;
5347 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5348}
5349
5350void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005351 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5352 instruction,
5353 instruction->GetDexPc(),
5354 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005355 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005356}
5357
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005358void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005359 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005360 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5361 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005362 case TypeCheckKind::kExactCheck:
5363 case TypeCheckKind::kAbstractClassCheck:
5364 case TypeCheckKind::kClassHierarchyCheck:
5365 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005366 call_kind =
5367 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005368 break;
5369 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005370 case TypeCheckKind::kUnresolvedCheck:
5371 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005372 call_kind = LocationSummary::kCallOnSlowPath;
5373 break;
5374 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005375
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005377 locations->SetInAt(0, Location::RequiresRegister());
5378 locations->SetInAt(1, Location::Any());
5379 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5380 locations->SetOut(Location::RequiresRegister());
5381 // When read barriers are enabled, we need a temporary register for
5382 // some cases.
5383 if (kEmitCompilerReadBarrier &&
5384 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5385 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5386 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5387 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005388 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005389}
5390
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005391void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005392 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005393 Location obj_loc = locations->InAt(0);
5394 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005395 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005396 Location out_loc = locations->Out();
5397 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005398 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005399 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5400 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5401 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005402 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005403 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005404
5405 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005406 // Avoid null check if we know obj is not null.
5407 if (instruction->MustDoNullCheck()) {
5408 __ testl(obj, obj);
5409 __ j(kEqual, &zero);
5410 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005411
Roland Levillain0d5a2812015-11-13 10:07:31 +00005412 // /* HeapReference<Class> */ out = obj->klass_
5413 __ movl(out, Address(obj, class_offset));
5414 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005415
5416 switch (instruction->GetTypeCheckKind()) {
5417 case TypeCheckKind::kExactCheck: {
5418 if (cls.IsRegister()) {
5419 __ cmpl(out, cls.AsRegister<CpuRegister>());
5420 } else {
5421 DCHECK(cls.IsStackSlot()) << cls;
5422 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5423 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005424 if (zero.IsLinked()) {
5425 // Classes must be equal for the instanceof to succeed.
5426 __ j(kNotEqual, &zero);
5427 __ movl(out, Immediate(1));
5428 __ jmp(&done);
5429 } else {
5430 __ setcc(kEqual, out);
5431 // setcc only sets the low byte.
5432 __ andl(out, Immediate(1));
5433 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005434 break;
5435 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005436
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005437 case TypeCheckKind::kAbstractClassCheck: {
5438 // If the class is abstract, we eagerly fetch the super class of the
5439 // object to avoid doing a comparison we know will fail.
5440 NearLabel loop, success;
5441 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005442 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5443 if (kEmitCompilerReadBarrier) {
5444 // Save the value of `out` into `temp` before overwriting it
5445 // in the following move operation, as we will need it for the
5446 // read barrier below.
5447 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
5448 __ movl(temp, out);
5449 }
5450 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005451 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005452 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005453 __ testl(out, out);
5454 // If `out` is null, we use it for the result, and jump to `done`.
5455 __ j(kEqual, &done);
5456 if (cls.IsRegister()) {
5457 __ cmpl(out, cls.AsRegister<CpuRegister>());
5458 } else {
5459 DCHECK(cls.IsStackSlot()) << cls;
5460 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5461 }
5462 __ j(kNotEqual, &loop);
5463 __ movl(out, Immediate(1));
5464 if (zero.IsLinked()) {
5465 __ jmp(&done);
5466 }
5467 break;
5468 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005470 case TypeCheckKind::kClassHierarchyCheck: {
5471 // Walk over the class hierarchy to find a match.
5472 NearLabel loop, success;
5473 __ Bind(&loop);
5474 if (cls.IsRegister()) {
5475 __ cmpl(out, cls.AsRegister<CpuRegister>());
5476 } else {
5477 DCHECK(cls.IsStackSlot()) << cls;
5478 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5479 }
5480 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005481 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5482 if (kEmitCompilerReadBarrier) {
5483 // Save the value of `out` into `temp` before overwriting it
5484 // in the following move operation, as we will need it for the
5485 // read barrier below.
5486 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
5487 __ movl(temp, out);
5488 }
5489 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005490 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005491 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005492 __ testl(out, out);
5493 __ j(kNotEqual, &loop);
5494 // If `out` is null, we use it for the result, and jump to `done`.
5495 __ jmp(&done);
5496 __ Bind(&success);
5497 __ movl(out, Immediate(1));
5498 if (zero.IsLinked()) {
5499 __ jmp(&done);
5500 }
5501 break;
5502 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005503
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005504 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005505 // Do an exact check.
5506 NearLabel exact_check;
5507 if (cls.IsRegister()) {
5508 __ cmpl(out, cls.AsRegister<CpuRegister>());
5509 } else {
5510 DCHECK(cls.IsStackSlot()) << cls;
5511 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5512 }
5513 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005514 // Otherwise, we need to check that the object's class is a non-primitive array.
5515 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5516 if (kEmitCompilerReadBarrier) {
5517 // Save the value of `out` into `temp` before overwriting it
5518 // in the following move operation, as we will need it for the
5519 // read barrier below.
5520 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
5521 __ movl(temp, out);
5522 }
5523 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005524 __ movl(out, Address(out, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005525 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005526 __ testl(out, out);
5527 // If `out` is null, we use it for the result, and jump to `done`.
5528 __ j(kEqual, &done);
5529 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5530 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005531 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005532 __ movl(out, Immediate(1));
5533 __ jmp(&done);
5534 break;
5535 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005536
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005537 case TypeCheckKind::kArrayCheck: {
5538 if (cls.IsRegister()) {
5539 __ cmpl(out, cls.AsRegister<CpuRegister>());
5540 } else {
5541 DCHECK(cls.IsStackSlot()) << cls;
5542 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5543 }
5544 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005545 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5546 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005547 codegen_->AddSlowPath(slow_path);
5548 __ j(kNotEqual, slow_path->GetEntryLabel());
5549 __ movl(out, Immediate(1));
5550 if (zero.IsLinked()) {
5551 __ jmp(&done);
5552 }
5553 break;
5554 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005555
Calin Juravle98893e12015-10-02 21:05:03 +01005556 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005557 case TypeCheckKind::kInterfaceCheck: {
5558 // Note that we indeed only call on slow path, but we always go
5559 // into the slow path for the unresolved & interface check
5560 // cases.
5561 //
5562 // We cannot directly call the InstanceofNonTrivial runtime
5563 // entry point without resorting to a type checking slow path
5564 // here (i.e. by calling InvokeRuntime directly), as it would
5565 // require to assign fixed registers for the inputs of this
5566 // HInstanceOf instruction (following the runtime calling
5567 // convention), which might be cluttered by the potential first
5568 // read barrier emission at the beginning of this method.
5569 DCHECK(locations->OnlyCallsOnSlowPath());
5570 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5571 /* is_fatal */ false);
5572 codegen_->AddSlowPath(slow_path);
5573 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005574 if (zero.IsLinked()) {
5575 __ jmp(&done);
5576 }
5577 break;
5578 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005579 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005580
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005581 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005582 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005583 __ xorl(out, out);
5584 }
5585
5586 if (done.IsLinked()) {
5587 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005588 }
5589
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005590 if (slow_path != nullptr) {
5591 __ Bind(slow_path->GetExitLabel());
5592 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005593}
5594
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005595void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005596 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5597 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005598 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5599 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005600 case TypeCheckKind::kExactCheck:
5601 case TypeCheckKind::kAbstractClassCheck:
5602 case TypeCheckKind::kClassHierarchyCheck:
5603 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005604 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5605 LocationSummary::kCallOnSlowPath :
5606 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005607 break;
5608 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005609 case TypeCheckKind::kUnresolvedCheck:
5610 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005611 call_kind = LocationSummary::kCallOnSlowPath;
5612 break;
5613 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005614 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5615 locations->SetInAt(0, Location::RequiresRegister());
5616 locations->SetInAt(1, Location::Any());
5617 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5618 locations->AddTemp(Location::RequiresRegister());
5619 // When read barriers are enabled, we need an additional temporary
5620 // register for some cases.
5621 if (kEmitCompilerReadBarrier &&
5622 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5623 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5624 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005625 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005626 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005627}
5628
5629void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
5630 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005631 Location obj_loc = locations->InAt(0);
5632 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005633 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005634 Location temp_loc = locations->GetTemp(0);
5635 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005636 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5637 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5638 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5639 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005640
Roland Levillain0d5a2812015-11-13 10:07:31 +00005641 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5642 bool is_type_check_slow_path_fatal =
5643 (type_check_kind == TypeCheckKind::kExactCheck ||
5644 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5645 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5646 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5647 !instruction->CanThrowIntoCatchBlock();
5648 SlowPathCode* type_check_slow_path =
5649 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5650 is_type_check_slow_path_fatal);
5651 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005652
5653 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005654 // Avoid null check if we know obj is not null.
5655 if (instruction->MustDoNullCheck()) {
5656 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005657 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005658 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005659
Roland Levillain0d5a2812015-11-13 10:07:31 +00005660 // /* HeapReference<Class> */ temp = obj->klass_
5661 __ movl(temp, Address(obj, class_offset));
5662 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005663
Roland Levillain0d5a2812015-11-13 10:07:31 +00005664 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005665 case TypeCheckKind::kExactCheck:
5666 case TypeCheckKind::kArrayCheck: {
5667 if (cls.IsRegister()) {
5668 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5669 } else {
5670 DCHECK(cls.IsStackSlot()) << cls;
5671 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5672 }
5673 // Jump to slow path for throwing the exception or doing a
5674 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005675 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005676 break;
5677 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005678
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005679 case TypeCheckKind::kAbstractClassCheck: {
5680 // If the class is abstract, we eagerly fetch the super class of the
5681 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005682 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005683 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005684 Location temp2_loc =
5685 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5686 if (kEmitCompilerReadBarrier) {
5687 // Save the value of `temp` into `temp2` before overwriting it
5688 // in the following move operation, as we will need it for the
5689 // read barrier below.
5690 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
5691 __ movl(temp2, temp);
5692 }
5693 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005694 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005695 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5696
5697 // If the class reference currently in `temp` is not null, jump
5698 // to the `compare_classes` label to compare it with the checked
5699 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005700 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005701 __ j(kNotEqual, &compare_classes);
5702 // Otherwise, jump to the slow path to throw the exception.
5703 //
5704 // But before, move back the object's class into `temp` before
5705 // going into the slow path, as it has been overwritten in the
5706 // meantime.
5707 // /* HeapReference<Class> */ temp = obj->klass_
5708 __ movl(temp, Address(obj, class_offset));
5709 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5710 __ jmp(type_check_slow_path->GetEntryLabel());
5711
5712 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005713 if (cls.IsRegister()) {
5714 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5715 } else {
5716 DCHECK(cls.IsStackSlot()) << cls;
5717 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5718 }
5719 __ j(kNotEqual, &loop);
5720 break;
5721 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005722
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005723 case TypeCheckKind::kClassHierarchyCheck: {
5724 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005725 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005726 __ Bind(&loop);
5727 if (cls.IsRegister()) {
5728 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5729 } else {
5730 DCHECK(cls.IsStackSlot()) << cls;
5731 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5732 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005733 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005734
5735 Location temp2_loc =
5736 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5737 if (kEmitCompilerReadBarrier) {
5738 // Save the value of `temp` into `temp2` before overwriting it
5739 // in the following move operation, as we will need it for the
5740 // read barrier below.
5741 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
5742 __ movl(temp2, temp);
5743 }
5744 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005745 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005746 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5747
5748 // If the class reference currently in `temp` is not null, jump
5749 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005750 __ testl(temp, temp);
5751 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005752 // Otherwise, jump to the slow path to throw the exception.
5753 //
5754 // But before, move back the object's class into `temp` before
5755 // going into the slow path, as it has been overwritten in the
5756 // meantime.
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
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005765 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005766 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005767 if (cls.IsRegister()) {
5768 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5769 } else {
5770 DCHECK(cls.IsStackSlot()) << cls;
5771 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5772 }
5773 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005774
5775 // Otherwise, we need to check that the object's class is a non-primitive array.
5776 Location temp2_loc =
5777 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5778 if (kEmitCompilerReadBarrier) {
5779 // Save the value of `temp` into `temp2` before overwriting it
5780 // in the following move operation, as we will need it for the
5781 // read barrier below.
5782 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
5783 __ movl(temp2, temp);
5784 }
5785 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005786 __ movl(temp, Address(temp, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005787 codegen_->MaybeGenerateReadBarrier(
5788 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5789
5790 // If the component type is not null (i.e. the object is indeed
5791 // an array), jump to label `check_non_primitive_component_type`
5792 // to further check that this component type is not a primitive
5793 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005794 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005795 __ j(kNotEqual, &check_non_primitive_component_type);
5796 // Otherwise, jump to the slow path to throw the exception.
5797 //
5798 // But before, move back the object's class into `temp` before
5799 // going into the slow path, as it has been overwritten in the
5800 // meantime.
5801 // /* HeapReference<Class> */ temp = obj->klass_
5802 __ movl(temp, Address(obj, class_offset));
5803 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5804 __ jmp(type_check_slow_path->GetEntryLabel());
5805
5806 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005807 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005808 __ j(kEqual, &done);
5809 // Same comment as above regarding `temp` and the slow path.
5810 // /* HeapReference<Class> */ temp = obj->klass_
5811 __ movl(temp, Address(obj, class_offset));
5812 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5813 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005814 break;
5815 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005816
Calin Juravle98893e12015-10-02 21:05:03 +01005817 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005818 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005819 // We always go into the type check slow path for the unresolved &
5820 // interface check cases.
5821 //
5822 // We cannot directly call the CheckCast runtime entry point
5823 // without resorting to a type checking slow path here (i.e. by
5824 // calling InvokeRuntime directly), as it would require to
5825 // assign fixed registers for the inputs of this HInstanceOf
5826 // instruction (following the runtime calling convention), which
5827 // might be cluttered by the potential first read barrier
5828 // emission at the beginning of this method.
5829 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005830 break;
5831 }
5832 __ Bind(&done);
5833
Roland Levillain0d5a2812015-11-13 10:07:31 +00005834 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005835}
5836
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005837void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5838 LocationSummary* locations =
5839 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5840 InvokeRuntimeCallingConvention calling_convention;
5841 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5842}
5843
5844void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005845 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5846 : QUICK_ENTRY_POINT(pUnlockObject),
5847 instruction,
5848 instruction->GetDexPc(),
5849 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005850 if (instruction->IsEnter()) {
5851 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5852 } else {
5853 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5854 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005855}
5856
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005857void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5858void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5859void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5860
5861void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5862 LocationSummary* locations =
5863 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5864 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5865 || instruction->GetResultType() == Primitive::kPrimLong);
5866 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005867 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005868 locations->SetOut(Location::SameAsFirstInput());
5869}
5870
5871void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5872 HandleBitwiseOperation(instruction);
5873}
5874
5875void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5876 HandleBitwiseOperation(instruction);
5877}
5878
5879void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5880 HandleBitwiseOperation(instruction);
5881}
5882
5883void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5884 LocationSummary* locations = instruction->GetLocations();
5885 Location first = locations->InAt(0);
5886 Location second = locations->InAt(1);
5887 DCHECK(first.Equals(locations->Out()));
5888
5889 if (instruction->GetResultType() == Primitive::kPrimInt) {
5890 if (second.IsRegister()) {
5891 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005892 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005893 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005894 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005895 } else {
5896 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005897 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005898 }
5899 } else if (second.IsConstant()) {
5900 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5901 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005902 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005903 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005904 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005905 } else {
5906 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005907 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005908 }
5909 } else {
5910 Address address(CpuRegister(RSP), second.GetStackIndex());
5911 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005912 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005913 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005914 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005915 } else {
5916 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005917 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005918 }
5919 }
5920 } else {
5921 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005922 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5923 bool second_is_constant = false;
5924 int64_t value = 0;
5925 if (second.IsConstant()) {
5926 second_is_constant = true;
5927 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005928 }
Mark Mendell40741f32015-04-20 22:10:34 -04005929 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005930
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005931 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005932 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005933 if (is_int32_value) {
5934 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5935 } else {
5936 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5937 }
5938 } else if (second.IsDoubleStackSlot()) {
5939 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005940 } else {
5941 __ andq(first_reg, second.AsRegister<CpuRegister>());
5942 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005943 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005944 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005945 if (is_int32_value) {
5946 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5947 } else {
5948 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5949 }
5950 } else if (second.IsDoubleStackSlot()) {
5951 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005952 } else {
5953 __ orq(first_reg, second.AsRegister<CpuRegister>());
5954 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005955 } else {
5956 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005957 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005958 if (is_int32_value) {
5959 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5960 } else {
5961 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5962 }
5963 } else if (second.IsDoubleStackSlot()) {
5964 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005965 } else {
5966 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5967 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005968 }
5969 }
5970}
5971
Roland Levillain0d5a2812015-11-13 10:07:31 +00005972void CodeGeneratorX86_64::GenerateReadBarrier(HInstruction* instruction,
5973 Location out,
5974 Location ref,
5975 Location obj,
5976 uint32_t offset,
5977 Location index) {
5978 DCHECK(kEmitCompilerReadBarrier);
5979
5980 // If heap poisoning is enabled, the unpoisoning of the loaded
5981 // reference will be carried out by the runtime within the slow
5982 // path.
5983 //
5984 // Note that `ref` currently does not get unpoisoned (when heap
5985 // poisoning is enabled), which is alright as the `ref` argument is
5986 // not used by the artReadBarrierSlow entry point.
5987 //
5988 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5989 SlowPathCode* slow_path = new (GetGraph()->GetArena())
5990 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
5991 AddSlowPath(slow_path);
5992
5993 // TODO: When read barrier has a fast path, add it here.
5994 /* Currently the read barrier call is inserted after the original load.
5995 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
5996 * original load. This load-load ordering is required by the read barrier.
5997 * The fast path/slow path (for Baker's algorithm) should look like:
5998 *
5999 * bool isGray = obj.LockWord & kReadBarrierMask;
6000 * lfence; // load fence or artificial data dependence to prevent load-load reordering
6001 * ref = obj.field; // this is the original load
6002 * if (isGray) {
6003 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
6004 * }
6005 */
6006
6007 __ jmp(slow_path->GetEntryLabel());
6008 __ Bind(slow_path->GetExitLabel());
6009}
6010
6011void CodeGeneratorX86_64::MaybeGenerateReadBarrier(HInstruction* instruction,
6012 Location out,
6013 Location ref,
6014 Location obj,
6015 uint32_t offset,
6016 Location index) {
6017 if (kEmitCompilerReadBarrier) {
6018 // If heap poisoning is enabled, unpoisoning will be taken care of
6019 // by the runtime within the slow path.
6020 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
6021 } else if (kPoisonHeapReferences) {
6022 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6023 }
6024}
6025
6026void CodeGeneratorX86_64::GenerateReadBarrierForRoot(HInstruction* instruction,
6027 Location out,
6028 Location root) {
6029 DCHECK(kEmitCompilerReadBarrier);
6030
6031 // Note that GC roots are not affected by heap poisoning, so we do
6032 // not need to do anything special for this here.
6033 SlowPathCode* slow_path =
6034 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6035 AddSlowPath(slow_path);
6036
6037 // TODO: Implement a fast path for ReadBarrierForRoot, performing
6038 // the following operation (for Baker's algorithm):
6039 //
6040 // if (thread.tls32_.is_gc_marking) {
6041 // root = Mark(root);
6042 // }
6043
6044 __ jmp(slow_path->GetEntryLabel());
6045 __ Bind(slow_path->GetExitLabel());
6046}
6047
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006048void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006049 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006050 LOG(FATAL) << "Unreachable";
6051}
6052
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006053void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006054 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006055 LOG(FATAL) << "Unreachable";
6056}
6057
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006058void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
6059 DCHECK(codegen_->IsBaseline());
6060 LocationSummary* locations =
6061 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6062 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6063}
6064
6065void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6066 DCHECK(codegen_->IsBaseline());
6067 // Will be generated at use site.
6068}
6069
Mark Mendellfe57faa2015-09-18 09:26:15 -04006070// Simple implementation of packed switch - generate cascaded compare/jumps.
6071void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6072 LocationSummary* locations =
6073 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6074 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006075 locations->AddTemp(Location::RequiresRegister());
6076 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006077}
6078
6079void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6080 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu59f054d2015-12-07 17:17:03 +08006081 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006082 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006083 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6084 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6085 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Zheng Xu59f054d2015-12-07 17:17:03 +08006086 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6087
6088 // Should we generate smaller inline compare/jumps?
6089 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6090 // Figure out the correct compare values and jump conditions.
6091 // Handle the first compare/branch as a special case because it might
6092 // jump to the default case.
6093 DCHECK_GT(num_entries, 2u);
6094 Condition first_condition;
6095 uint32_t index;
6096 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6097 if (lower_bound != 0) {
6098 first_condition = kLess;
6099 __ cmpl(value_reg_in, Immediate(lower_bound));
6100 __ j(first_condition, codegen_->GetLabelOf(default_block));
6101 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6102
6103 index = 1;
6104 } else {
6105 // Handle all the compare/jumps below.
6106 first_condition = kBelow;
6107 index = 0;
6108 }
6109
6110 // Handle the rest of the compare/jumps.
6111 for (; index + 1 < num_entries; index += 2) {
6112 int32_t compare_to_value = lower_bound + index + 1;
6113 __ cmpl(value_reg_in, Immediate(compare_to_value));
6114 // Jump to successors[index] if value < case_value[index].
6115 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6116 // Jump to successors[index + 1] if value == case_value[index + 1].
6117 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6118 }
6119
6120 if (index != num_entries) {
6121 // There are an odd number of entries. Handle the last one.
6122 DCHECK_EQ(index + 1, num_entries);
6123 __ cmpl(value_reg_in, Immediate(lower_bound + index));
6124 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6125 }
6126
6127 // And the default for any other value.
6128 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6129 __ jmp(codegen_->GetLabelOf(default_block));
6130 }
6131 return;
6132 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006133
6134 // Remove the bias, if needed.
6135 Register value_reg_out = value_reg_in.AsRegister();
6136 if (lower_bound != 0) {
6137 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6138 value_reg_out = temp_reg.AsRegister();
6139 }
6140 CpuRegister value_reg(value_reg_out);
6141
6142 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006143 __ cmpl(value_reg, Immediate(num_entries - 1));
6144 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006145
Mark Mendell9c86b482015-09-18 13:36:07 -04006146 // We are in the range of the table.
6147 // Load the address of the jump table in the constant area.
6148 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006149
Mark Mendell9c86b482015-09-18 13:36:07 -04006150 // Load the (signed) offset from the jump table.
6151 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6152
6153 // Add the offset to the address of the table base.
6154 __ addq(temp_reg, base_reg);
6155
6156 // And jump.
6157 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006158}
6159
Mark Mendell92e83bf2015-05-07 11:25:03 -04006160void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6161 if (value == 0) {
6162 __ xorl(dest, dest);
6163 } else if (value > 0 && IsInt<32>(value)) {
6164 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
6165 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6166 } else {
6167 __ movq(dest, Immediate(value));
6168 }
6169}
6170
Mark Mendellcfa410b2015-05-25 16:02:44 -04006171void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6172 DCHECK(dest.IsDoubleStackSlot());
6173 if (IsInt<32>(value)) {
6174 // Can move directly as an int32 constant.
6175 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6176 Immediate(static_cast<int32_t>(value)));
6177 } else {
6178 Load64BitValue(CpuRegister(TMP), value);
6179 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6180 }
6181}
6182
Mark Mendell9c86b482015-09-18 13:36:07 -04006183/**
6184 * Class to handle late fixup of offsets into constant area.
6185 */
6186class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6187 public:
6188 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6189 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6190
6191 protected:
6192 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6193
6194 CodeGeneratorX86_64* codegen_;
6195
6196 private:
6197 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6198 // Patch the correct offset for the instruction. We use the address of the
6199 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6200 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6201 int32_t relative_position = constant_offset - pos;
6202
6203 // Patch in the right value.
6204 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6205 }
6206
6207 // Location in constant area that the fixup refers to.
6208 size_t offset_into_constant_area_;
6209};
6210
6211/**
6212 t * Class to handle late fixup of offsets to a jump table that will be created in the
6213 * constant area.
6214 */
6215class JumpTableRIPFixup : public RIPFixup {
6216 public:
6217 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6218 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6219
6220 void CreateJumpTable() {
6221 X86_64Assembler* assembler = codegen_->GetAssembler();
6222
6223 // Ensure that the reference to the jump table has the correct offset.
6224 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6225 SetOffset(offset_in_constant_table);
6226
6227 // Compute the offset from the start of the function to this jump table.
6228 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6229
6230 // Populate the jump table with the correct values for the jump table.
6231 int32_t num_entries = switch_instr_->GetNumEntries();
6232 HBasicBlock* block = switch_instr_->GetBlock();
6233 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6234 // The value that we want is the target offset - the position of the table.
6235 for (int32_t i = 0; i < num_entries; i++) {
6236 HBasicBlock* b = successors[i];
6237 Label* l = codegen_->GetLabelOf(b);
6238 DCHECK(l->IsBound());
6239 int32_t offset_to_block = l->Position() - current_table_offset;
6240 assembler->AppendInt32(offset_to_block);
6241 }
6242 }
6243
6244 private:
6245 const HPackedSwitch* switch_instr_;
6246};
6247
Mark Mendellf55c3e02015-03-26 21:07:46 -04006248void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6249 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006250 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006251 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6252 // 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 -04006253 assembler->Align(4, 0);
6254 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006255
6256 // Populate any jump tables.
6257 for (auto jump_table : fixups_to_jump_tables_) {
6258 jump_table->CreateJumpTable();
6259 }
6260
6261 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006262 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006263 }
6264
6265 // And finish up.
6266 CodeGenerator::Finalize(allocator);
6267}
6268
Mark Mendellf55c3e02015-03-26 21:07:46 -04006269Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6270 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6271 return Address::RIP(fixup);
6272}
6273
6274Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6275 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6276 return Address::RIP(fixup);
6277}
6278
6279Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6280 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6281 return Address::RIP(fixup);
6282}
6283
6284Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6285 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6286 return Address::RIP(fixup);
6287}
6288
Andreas Gampe85b62f22015-09-09 13:15:38 -07006289// TODO: trg as memory.
6290void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6291 if (!trg.IsValid()) {
6292 DCHECK(type == Primitive::kPrimVoid);
6293 return;
6294 }
6295
6296 DCHECK_NE(type, Primitive::kPrimVoid);
6297
6298 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6299 if (trg.Equals(return_loc)) {
6300 return;
6301 }
6302
6303 // Let the parallel move resolver take care of all of this.
6304 HParallelMove parallel_move(GetGraph()->GetArena());
6305 parallel_move.AddMove(return_loc, trg, type, nullptr);
6306 GetMoveResolver()->EmitNativeCode(&parallel_move);
6307}
6308
Mark Mendell9c86b482015-09-18 13:36:07 -04006309Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6310 // Create a fixup to be used to create and address the jump table.
6311 JumpTableRIPFixup* table_fixup =
6312 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6313
6314 // We have to populate the jump tables.
6315 fixups_to_jump_tables_.push_back(table_fixup);
6316 return Address::RIP(table_fixup);
6317}
6318
Mark Mendellea5af682015-10-22 17:35:49 -04006319void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6320 const Address& addr_high,
6321 int64_t v,
6322 HInstruction* instruction) {
6323 if (IsInt<32>(v)) {
6324 int32_t v_32 = v;
6325 __ movq(addr_low, Immediate(v_32));
6326 MaybeRecordImplicitNullCheck(instruction);
6327 } else {
6328 // Didn't fit in a register. Do it in pieces.
6329 int32_t low_v = Low32Bits(v);
6330 int32_t high_v = High32Bits(v);
6331 __ movl(addr_low, Immediate(low_v));
6332 MaybeRecordImplicitNullCheck(instruction);
6333 __ movl(addr_high, Immediate(high_v));
6334 }
6335}
6336
Roland Levillain4d027112015-07-01 15:41:14 +01006337#undef __
6338
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006339} // namespace x86_64
6340} // namespace art