blob: 41b62a4a12d8e6d410266c7a762e5359506148e8 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "intrinsics.h"
25#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "mirror/object_reference.h"
29#include "thread.h"
30#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "utils/x86_64/assembler_x86_64.h"
33#include "utils/x86_64/managed_register_x86_64.h"
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace art {
36
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace x86_64 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000044// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
45// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
46// generates less code/data with a small num_entries.
47static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000049static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Mark Mendell24f2dfa2015-01-14 19:51:45 -050052static constexpr int kC2ConditionMask = 0x400;
53
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070054// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
55#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Calin Juravle175dc732015-08-25 15:42:32 +010056#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Andreas Gampe85b62f22015-09-09 13:15:38 -070058class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000060 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Alexandre Rames2ed20af2015-03-06 13:55:35 +000062 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000063 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000065 if (instruction_->CanThrowIntoCatchBlock()) {
66 // Live registers will be restored in the catch block if caught.
67 SaveLiveRegisters(codegen, instruction_->GetLocations());
68 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000069 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
70 instruction_,
71 instruction_->GetDexPc(),
72 this);
Roland Levillain888d0672015-11-23 18:53:50 +000073 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 }
75
Alexandre Rames8158f282015-08-07 10:26:17 +010076 bool IsFatal() const OVERRIDE { return true; }
77
Alexandre Rames9931f312015-06-19 14:47:01 +010078 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
79
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 private:
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:
David Srbecky9cd6d372016-02-09 15:24:47 +000086 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000087
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:
Calin Juravled0d48522014-11-04 16:40:20 +0000107 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
108};
109
Andreas Gampe85b62f22015-09-09 13:15:38 -0700110class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000112 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div)
113 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000114
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000116 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000117 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 if (is_div_) {
119 __ negl(cpu_reg_);
120 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400121 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 }
123
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000124 } else {
125 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 if (is_div_) {
127 __ negq(cpu_reg_);
128 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400129 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000130 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000131 }
Calin Juravled0d48522014-11-04 16:40:20 +0000132 __ jmp(GetExitLabel());
133 }
134
Alexandre Rames9931f312015-06-19 14:47:01 +0100135 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
136
Calin Juravled0d48522014-11-04 16:40:20 +0000137 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000139 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000140 const bool is_div_;
141 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000142};
143
Andreas Gampe85b62f22015-09-09 13:15:38 -0700144class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100146 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000147 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000149 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000150 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000152 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000153 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
154 instruction_,
155 instruction_->GetDexPc(),
156 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000157 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000158 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 if (successor_ == nullptr) {
160 __ jmp(GetReturnLabel());
161 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000162 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 }
165
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 Label* GetReturnLabel() {
167 DCHECK(successor_ == nullptr);
168 return &return_label_;
169 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100171 HBasicBlock* GetSuccessor() const {
172 return successor_;
173 }
174
Alexandre Rames9931f312015-06-19 14:47:01 +0100175 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
176
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000179 Label return_label_;
180
181 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
182};
183
Andreas Gampe85b62f22015-09-09 13:15:38 -0700184class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100186 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000187 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100188
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100190 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000191 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000193 if (instruction_->CanThrowIntoCatchBlock()) {
194 // Live registers will be restored in the catch block if caught.
195 SaveLiveRegisters(codegen, instruction_->GetLocations());
196 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000197 // We're moving two locations to locations that could overlap, so we need a parallel
198 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100199 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000200 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100201 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000202 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100203 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100204 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100205 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
206 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100207 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
208 ? QUICK_ENTRY_POINT(pThrowStringBounds)
209 : QUICK_ENTRY_POINT(pThrowArrayBounds);
210 x86_64_codegen->InvokeRuntime(entry_point_offset,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000211 instruction_,
212 instruction_->GetDexPc(),
213 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100214 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000215 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100216 }
217
Alexandre Rames8158f282015-08-07 10:26:17 +0100218 bool IsFatal() const OVERRIDE { return true; }
219
Alexandre Rames9931f312015-06-19 14:47:01 +0100220 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
221
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100222 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100223 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)
David Srbecky9cd6d372016-02-09 15:24:47 +0000232 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 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:
David Srbecky9cd6d372016-02-09 15:24:47 +0000289 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000290
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;
David Srbecky9cd6d372016-02-09 15:24:47 +0000300 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
301 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(string_index));
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:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000315 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
316};
317
Andreas Gampe85b62f22015-09-09 13:15:38 -0700318class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000320 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
326 : locations->Out();
327 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000328 DCHECK(instruction_->IsCheckCast()
329 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
Roland Levillain0d5a2812015-11-13 10:07:31 +0000331 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000333
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334 if (!is_fatal_) {
335 SaveLiveRegisters(codegen, locations);
336 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
338 // We're moving two locations to locations that could overlap, so we need a parallel
339 // move resolver.
340 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000341 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100342 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000343 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100344 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100345 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100346 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
347 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000349 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000350 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
351 instruction_,
352 dex_pc,
353 this);
354 CheckEntrypointTypes<
355 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 } else {
357 DCHECK(instruction_->IsCheckCast());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000358 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
359 instruction_,
360 dex_pc,
361 this);
362 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000363 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000365 if (!is_fatal_) {
366 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000367 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 RestoreLiveRegisters(codegen, locations);
371 __ jmp(GetExitLabel());
372 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000373 }
374
Alexandre Rames9931f312015-06-19 14:47:01 +0100375 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000377 bool IsFatal() const OVERRIDE { return is_fatal_; }
378
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000379 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000380 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000381
382 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
383};
384
Andreas Gampe85b62f22015-09-09 13:15:38 -0700385class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 public:
Aart Bik42249c32016-01-07 15:33:50 -0800387 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000391 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700392 __ Bind(GetEntryLabel());
393 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000394 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
Aart Bik42249c32016-01-07 15:33:50 -0800395 instruction_,
396 instruction_->GetDexPc(),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000397 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000398 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 }
400
Alexandre Rames9931f312015-06-19 14:47:01 +0100401 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
402
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700403 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700404 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
405};
406
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100407class ArraySetSlowPathX86_64 : public SlowPathCode {
408 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000409 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100410
411 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
412 LocationSummary* locations = instruction_->GetLocations();
413 __ Bind(GetEntryLabel());
414 SaveLiveRegisters(codegen, locations);
415
416 InvokeRuntimeCallingConvention calling_convention;
417 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
418 parallel_move.AddMove(
419 locations->InAt(0),
420 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
421 Primitive::kPrimNot,
422 nullptr);
423 parallel_move.AddMove(
424 locations->InAt(1),
425 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
426 Primitive::kPrimInt,
427 nullptr);
428 parallel_move.AddMove(
429 locations->InAt(2),
430 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
431 Primitive::kPrimNot,
432 nullptr);
433 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
434
Roland Levillain0d5a2812015-11-13 10:07:31 +0000435 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
436 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
437 instruction_,
438 instruction_->GetDexPc(),
439 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000440 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100441 RestoreLiveRegisters(codegen, locations);
442 __ jmp(GetExitLabel());
443 }
444
445 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
446
447 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100448 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
449};
450
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000451// Slow path marking an object during a read barrier.
452class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
453 public:
454 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000455 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000456 DCHECK(kEmitCompilerReadBarrier);
457 }
458
459 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
460
461 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
462 LocationSummary* locations = instruction_->GetLocations();
463 Register reg_out = out_.AsRegister<Register>();
464 DCHECK(locations->CanCall());
465 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
466 DCHECK(instruction_->IsInstanceFieldGet() ||
467 instruction_->IsStaticFieldGet() ||
468 instruction_->IsArrayGet() ||
469 instruction_->IsLoadClass() ||
470 instruction_->IsLoadString() ||
471 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100472 instruction_->IsCheckCast() ||
473 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
474 instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000475 << "Unexpected instruction in read barrier marking slow path: "
476 << instruction_->DebugName();
477
478 __ Bind(GetEntryLabel());
479 SaveLiveRegisters(codegen, locations);
480
481 InvokeRuntimeCallingConvention calling_convention;
482 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
483 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
484 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
485 instruction_,
486 instruction_->GetDexPc(),
487 this);
488 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
489 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
490
491 RestoreLiveRegisters(codegen, locations);
492 __ jmp(GetExitLabel());
493 }
494
495 private:
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000496 const Location out_;
497 const Location obj_;
498
499 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
500};
501
Roland Levillain0d5a2812015-11-13 10:07:31 +0000502// Slow path generating a read barrier for a heap reference.
503class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
504 public:
505 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
506 Location out,
507 Location ref,
508 Location obj,
509 uint32_t offset,
510 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000511 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000512 out_(out),
513 ref_(ref),
514 obj_(obj),
515 offset_(offset),
516 index_(index) {
517 DCHECK(kEmitCompilerReadBarrier);
518 // If `obj` is equal to `out` or `ref`, it means the initial
519 // object has been overwritten by (or after) the heap object
520 // reference load to be instrumented, e.g.:
521 //
522 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000523 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000524 //
525 // In that case, we have lost the information about the original
526 // object, and the emitted read barrier cannot work properly.
527 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
528 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
529}
530
531 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
532 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
533 LocationSummary* locations = instruction_->GetLocations();
534 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
535 DCHECK(locations->CanCall());
536 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100537 DCHECK(instruction_->IsInstanceFieldGet() ||
538 instruction_->IsStaticFieldGet() ||
539 instruction_->IsArrayGet() ||
540 instruction_->IsInstanceOf() ||
541 instruction_->IsCheckCast() ||
542 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000543 instruction_->GetLocations()->Intrinsified()))
544 << "Unexpected instruction in read barrier for heap reference slow path: "
545 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000546
547 __ Bind(GetEntryLabel());
548 SaveLiveRegisters(codegen, locations);
549
550 // We may have to change the index's value, but as `index_` is a
551 // constant member (like other "inputs" of this slow path),
552 // introduce a copy of it, `index`.
553 Location index = index_;
554 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100555 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000556 if (instruction_->IsArrayGet()) {
557 // Compute real offset and store it in index_.
558 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
559 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
560 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
561 // We are about to change the value of `index_reg` (see the
562 // calls to art::x86_64::X86_64Assembler::shll and
563 // art::x86_64::X86_64Assembler::AddImmediate below), but it
564 // has not been saved by the previous call to
565 // art::SlowPathCode::SaveLiveRegisters, as it is a
566 // callee-save register --
567 // art::SlowPathCode::SaveLiveRegisters does not consider
568 // callee-save registers, as it has been designed with the
569 // assumption that callee-save registers are supposed to be
570 // handled by the called function. So, as a callee-save
571 // register, `index_reg` _would_ eventually be saved onto
572 // the stack, but it would be too late: we would have
573 // changed its value earlier. Therefore, we manually save
574 // it here into another freely available register,
575 // `free_reg`, chosen of course among the caller-save
576 // registers (as a callee-save `free_reg` register would
577 // exhibit the same problem).
578 //
579 // Note we could have requested a temporary register from
580 // the register allocator instead; but we prefer not to, as
581 // this is a slow path, and we know we can find a
582 // caller-save register that is available.
583 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
584 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
585 index_reg = free_reg;
586 index = Location::RegisterLocation(index_reg);
587 } else {
588 // The initial register stored in `index_` has already been
589 // saved in the call to art::SlowPathCode::SaveLiveRegisters
590 // (as it is not a callee-save register), so we can freely
591 // use it.
592 }
593 // Shifting the index value contained in `index_reg` by the
594 // scale factor (2) cannot overflow in practice, as the
595 // runtime is unable to allocate object arrays with a size
596 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
597 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
598 static_assert(
599 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
600 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
601 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
602 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100603 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
604 // intrinsics, `index_` is not shifted by a scale factor of 2
605 // (as in the case of ArrayGet), as it is actually an offset
606 // to an object field within an object.
607 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000608 DCHECK(instruction_->GetLocations()->Intrinsified());
609 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
610 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
611 << instruction_->AsInvoke()->GetIntrinsic();
612 DCHECK_EQ(offset_, 0U);
613 DCHECK(index_.IsRegister());
614 }
615 }
616
617 // We're moving two or three locations to locations that could
618 // overlap, so we need a parallel move resolver.
619 InvokeRuntimeCallingConvention calling_convention;
620 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
621 parallel_move.AddMove(ref_,
622 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
623 Primitive::kPrimNot,
624 nullptr);
625 parallel_move.AddMove(obj_,
626 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
627 Primitive::kPrimNot,
628 nullptr);
629 if (index.IsValid()) {
630 parallel_move.AddMove(index,
631 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
632 Primitive::kPrimInt,
633 nullptr);
634 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
635 } else {
636 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
637 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
638 }
639 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
640 instruction_,
641 instruction_->GetDexPc(),
642 this);
643 CheckEntrypointTypes<
644 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
645 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
646
647 RestoreLiveRegisters(codegen, locations);
648 __ jmp(GetExitLabel());
649 }
650
651 const char* GetDescription() const OVERRIDE {
652 return "ReadBarrierForHeapReferenceSlowPathX86_64";
653 }
654
655 private:
656 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
657 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
658 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
659 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
660 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
661 return static_cast<CpuRegister>(i);
662 }
663 }
664 // We shall never fail to find a free caller-save register, as
665 // there are more than two core caller-save registers on x86-64
666 // (meaning it is possible to find one which is different from
667 // `ref` and `obj`).
668 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
669 LOG(FATAL) << "Could not find a free caller-save register";
670 UNREACHABLE();
671 }
672
Roland Levillain0d5a2812015-11-13 10:07:31 +0000673 const Location out_;
674 const Location ref_;
675 const Location obj_;
676 const uint32_t offset_;
677 // An additional location containing an index to an array.
678 // Only used for HArrayGet and the UnsafeGetObject &
679 // UnsafeGetObjectVolatile intrinsics.
680 const Location index_;
681
682 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
683};
684
685// Slow path generating a read barrier for a GC root.
686class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
687 public:
688 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000689 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000690 DCHECK(kEmitCompilerReadBarrier);
691 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000692
693 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
694 LocationSummary* locations = instruction_->GetLocations();
695 DCHECK(locations->CanCall());
696 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000697 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
698 << "Unexpected instruction in read barrier for GC root slow path: "
699 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000700
701 __ Bind(GetEntryLabel());
702 SaveLiveRegisters(codegen, locations);
703
704 InvokeRuntimeCallingConvention calling_convention;
705 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
706 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
707 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
708 instruction_,
709 instruction_->GetDexPc(),
710 this);
711 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
712 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
713
714 RestoreLiveRegisters(codegen, locations);
715 __ jmp(GetExitLabel());
716 }
717
718 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
719
720 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000721 const Location out_;
722 const Location root_;
723
724 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
725};
726
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100727#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700728// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
729#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100730
Roland Levillain4fa13f62015-07-06 18:11:54 +0100731inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700732 switch (cond) {
733 case kCondEQ: return kEqual;
734 case kCondNE: return kNotEqual;
735 case kCondLT: return kLess;
736 case kCondLE: return kLessEqual;
737 case kCondGT: return kGreater;
738 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700739 case kCondB: return kBelow;
740 case kCondBE: return kBelowEqual;
741 case kCondA: return kAbove;
742 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700743 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100744 LOG(FATAL) << "Unreachable";
745 UNREACHABLE();
746}
747
Aart Bike9f37602015-10-09 11:15:55 -0700748// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100749inline Condition X86_64FPCondition(IfCondition cond) {
750 switch (cond) {
751 case kCondEQ: return kEqual;
752 case kCondNE: return kNotEqual;
753 case kCondLT: return kBelow;
754 case kCondLE: return kBelowEqual;
755 case kCondGT: return kAbove;
756 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700757 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100758 };
759 LOG(FATAL) << "Unreachable";
760 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700761}
762
Vladimir Markodc151b22015-10-15 18:02:30 +0100763HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
764 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
765 MethodReference target_method ATTRIBUTE_UNUSED) {
766 switch (desired_dispatch_info.code_ptr_location) {
767 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
768 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
769 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
770 return HInvokeStaticOrDirect::DispatchInfo {
771 desired_dispatch_info.method_load_kind,
772 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
773 desired_dispatch_info.method_load_data,
774 0u
775 };
776 default:
777 return desired_dispatch_info;
778 }
779}
780
Serguei Katkov288c7a82016-05-16 11:53:15 +0600781Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
782 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800783 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000784 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
785 switch (invoke->GetMethodLoadKind()) {
786 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
787 // temp = thread->string_init_entrypoint
Nicolas Geoffray7f59d592015-12-29 16:20:52 +0000788 __ gs()->movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000789 Address::Absolute(invoke->GetStringInitOffset(), /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000790 break;
791 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000792 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000793 break;
794 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
795 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
796 break;
797 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
798 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
799 method_patches_.emplace_back(invoke->GetTargetMethod());
800 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
801 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000802 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +0000803 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000804 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000805 // Bind a new fixup label at the end of the "movl" insn.
806 uint32_t offset = invoke->GetDexCacheArrayOffset();
807 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko58155012015-08-19 12:49:41 +0000808 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000809 }
Vladimir Marko58155012015-08-19 12:49:41 +0000810 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +0000811 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000812 Register method_reg;
813 CpuRegister reg = temp.AsRegister<CpuRegister>();
814 if (current_method.IsRegister()) {
815 method_reg = current_method.AsRegister<Register>();
816 } else {
817 DCHECK(invoke->GetLocations()->Intrinsified());
818 DCHECK(!current_method.IsValid());
819 method_reg = reg.AsRegister();
820 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
821 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000822 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100823 __ movq(reg,
824 Address(CpuRegister(method_reg),
825 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +0100826 // temp = temp[index_in_cache];
827 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
828 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +0000829 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
830 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100831 }
Vladimir Marko58155012015-08-19 12:49:41 +0000832 }
Serguei Katkov288c7a82016-05-16 11:53:15 +0600833 return callee_method;
834}
835
836void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
837 Location temp) {
838 // All registers are assumed to be correctly set up.
839 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +0000840
841 switch (invoke->GetCodePtrLocation()) {
842 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
843 __ call(&frame_entry_label_);
844 break;
845 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
846 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
847 Label* label = &relative_call_patches_.back().label;
848 __ call(label); // Bind to the patch label, override at link time.
849 __ Bind(label); // Bind the label at the end of the "call" insn.
850 break;
851 }
852 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
853 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100854 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
855 LOG(FATAL) << "Unsupported";
856 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000857 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
858 // (callee_method + offset_of_quick_compiled_code)()
859 __ call(Address(callee_method.AsRegister<CpuRegister>(),
860 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
861 kX86_64WordSize).SizeValue()));
862 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000863 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800864
865 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800866}
867
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000868void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
869 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
870 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
871 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000872
873 // Use the calling convention instead of the location of the receiver, as
874 // intrinsics may have put the receiver in a different register. In the intrinsics
875 // slow path, the arguments have been moved to the right place, so here we are
876 // guaranteed that the receiver is the first register of the calling convention.
877 InvokeDexCallingConvention calling_convention;
878 Register receiver = calling_convention.GetRegisterAt(0);
879
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000880 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000881 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000882 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000883 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000884 // Instead of simply (possibly) unpoisoning `temp` here, we should
885 // emit a read barrier for the previous class reference load.
886 // However this is not required in practice, as this is an
887 // intermediate/temporary reference and because the current
888 // concurrent copying collector keeps the from-space memory
889 // intact/accessible until the end of the marking phase (the
890 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000891 __ MaybeUnpoisonHeapReference(temp);
892 // temp = temp->GetMethodAt(method_offset);
893 __ movq(temp, Address(temp, method_offset));
894 // call temp->GetEntryPoint();
895 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
896 kX86_64WordSize).SizeValue()));
897}
898
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000899void CodeGeneratorX86_64::RecordSimplePatch() {
900 if (GetCompilerOptions().GetIncludePatchInformation()) {
901 simple_patches_.emplace_back();
902 __ Bind(&simple_patches_.back());
903 }
904}
905
906void CodeGeneratorX86_64::RecordStringPatch(HLoadString* load_string) {
907 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
908 __ Bind(&string_patches_.back().label);
909}
910
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100911void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
912 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
913 __ Bind(&type_patches_.back().label);
914}
915
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000916Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
917 uint32_t element_offset) {
918 // Add a patch entry and return the label.
919 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
920 return &pc_relative_dex_cache_patches_.back().label;
921}
922
Vladimir Marko58155012015-08-19 12:49:41 +0000923void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
924 DCHECK(linker_patches->empty());
925 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000926 method_patches_.size() +
927 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000928 pc_relative_dex_cache_patches_.size() +
929 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100930 string_patches_.size() +
931 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000932 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000933 // The label points to the end of the "movl" insn but the literal offset for method
934 // patch needs to point to the embedded constant which occupies the last 4 bytes.
935 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000936 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000937 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000938 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
939 info.target_method.dex_file,
940 info.target_method.dex_method_index));
941 }
942 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000943 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000944 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
945 info.target_method.dex_file,
946 info.target_method.dex_method_index));
947 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000948 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
949 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000950 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
951 &info.target_dex_file,
952 info.label.Position(),
953 info.element_offset));
954 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000955 for (const Label& label : simple_patches_) {
956 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
957 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
958 }
959 for (const StringPatchInfo<Label>& info : string_patches_) {
960 // These are always PC-relative, see GetSupportedLoadStringKind().
961 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
962 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
963 &info.dex_file,
964 info.label.Position(),
965 info.string_index));
966 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100967 for (const TypePatchInfo<Label>& info : type_patches_) {
968 // These are always PC-relative, see GetSupportedLoadClassKind().
969 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
970 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
971 &info.dex_file,
972 info.label.Position(),
973 info.type_index));
974 }
Vladimir Marko58155012015-08-19 12:49:41 +0000975}
976
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100977void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100978 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100979}
980
981void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100982 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100983}
984
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100985size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
986 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
987 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100988}
989
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100990size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
991 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
992 return kX86_64WordSize;
993}
994
995size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
996 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
997 return kX86_64WordSize;
998}
999
1000size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1001 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1002 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001003}
1004
Calin Juravle175dc732015-08-25 15:42:32 +01001005void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1006 HInstruction* instruction,
1007 uint32_t dex_pc,
1008 SlowPathCode* slow_path) {
1009 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
1010 instruction,
1011 dex_pc,
1012 slow_path);
1013}
1014
1015void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +01001016 HInstruction* instruction,
1017 uint32_t dex_pc,
1018 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001019 ValidateInvokeRuntime(instruction, slow_path);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001020 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
Alexandre Rames8158f282015-08-07 10:26:17 +01001021 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +01001022}
1023
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001024static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001025// Use a fake return address register to mimic Quick.
1026static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001027CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001028 const X86_64InstructionSetFeatures& isa_features,
1029 const CompilerOptions& compiler_options,
1030 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001031 : CodeGenerator(graph,
1032 kNumberOfCpuRegisters,
1033 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001034 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001035 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1036 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001037 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001038 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1039 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001040 compiler_options,
1041 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001042 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001043 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001044 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001045 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001046 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001047 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001048 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001049 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1050 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001051 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001052 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1053 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001054 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -04001055 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001056 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1057}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001058
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001059InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1060 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001061 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062 assembler_(codegen->GetAssembler()),
1063 codegen_(codegen) {}
1064
David Brazdil58282f42016-01-14 12:45:10 +00001065void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001066 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001067 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001069 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001070 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001071}
1072
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001073static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001074 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001075}
David Srbecky9d8606d2015-04-12 09:35:32 +01001076
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001077static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001078 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001079}
1080
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001081void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001082 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001083 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001084 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001085 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001086 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001087
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001088 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001089 __ testq(CpuRegister(RAX), Address(
1090 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001091 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001092 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001093
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001094 if (HasEmptyFrame()) {
1095 return;
1096 }
1097
Nicolas Geoffray98893962015-01-21 12:32:32 +00001098 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001099 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001100 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001101 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001102 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1103 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001104 }
1105 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001106
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001107 int adjust = GetFrameSize() - GetCoreSpillSize();
1108 __ subq(CpuRegister(RSP), Immediate(adjust));
1109 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001110 uint32_t xmm_spill_location = GetFpuSpillStart();
1111 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001112
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001113 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1114 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001115 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1116 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1117 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001118 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001119 }
1120
Mathieu Chartiere401d142015-04-22 13:56:20 -07001121 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001122 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001123}
1124
1125void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001126 __ cfi().RememberState();
1127 if (!HasEmptyFrame()) {
1128 uint32_t xmm_spill_location = GetFpuSpillStart();
1129 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1130 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1131 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1132 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1133 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1134 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1135 }
1136 }
1137
1138 int adjust = GetFrameSize() - GetCoreSpillSize();
1139 __ addq(CpuRegister(RSP), Immediate(adjust));
1140 __ cfi().AdjustCFAOffset(-adjust);
1141
1142 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1143 Register reg = kCoreCalleeSaves[i];
1144 if (allocated_registers_.ContainsCoreRegister(reg)) {
1145 __ popq(CpuRegister(reg));
1146 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1147 __ cfi().Restore(DWARFReg(reg));
1148 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001149 }
1150 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001151 __ ret();
1152 __ cfi().RestoreState();
1153 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001154}
1155
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001156void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1157 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001158}
1159
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001160void CodeGeneratorX86_64::Move(Location destination, Location source) {
1161 if (source.Equals(destination)) {
1162 return;
1163 }
1164 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001165 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001166 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001167 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001169 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001170 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001171 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1172 } else if (source.IsConstant()) {
1173 HConstant* constant = source.GetConstant();
1174 if (constant->IsLongConstant()) {
1175 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1176 } else {
1177 Load32BitValue(dest, GetInt32ValueOf(constant));
1178 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001179 } else {
1180 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001181 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001182 }
1183 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001184 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001185 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001186 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001188 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1189 } else if (source.IsConstant()) {
1190 HConstant* constant = source.GetConstant();
1191 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1192 if (constant->IsFloatConstant()) {
1193 Load32BitValue(dest, static_cast<int32_t>(value));
1194 } else {
1195 Load64BitValue(dest, value);
1196 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001197 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001198 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001199 } else {
1200 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001201 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001202 }
1203 } else if (destination.IsStackSlot()) {
1204 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001206 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 } else if (source.IsFpuRegister()) {
1208 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001209 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001210 } else if (source.IsConstant()) {
1211 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001212 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001213 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001214 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001215 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001216 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1217 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001218 }
1219 } else {
1220 DCHECK(destination.IsDoubleStackSlot());
1221 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001223 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001224 } else if (source.IsFpuRegister()) {
1225 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001227 } else if (source.IsConstant()) {
1228 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001229 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001230 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001231 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001232 } else {
1233 DCHECK(constant->IsLongConstant());
1234 value = constant->AsLongConstant()->GetValue();
1235 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001236 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001237 } else {
1238 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001239 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1240 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001241 }
1242 }
1243}
1244
Calin Juravle175dc732015-08-25 15:42:32 +01001245void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1246 DCHECK(location.IsRegister());
1247 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1248}
1249
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250void CodeGeneratorX86_64::MoveLocation(
1251 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1252 Move(dst, src);
1253}
1254
1255void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1256 if (location.IsRegister()) {
1257 locations->AddTemp(location);
1258 } else {
1259 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1260 }
1261}
1262
David Brazdilfc6a86a2015-06-26 10:33:45 +00001263void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001264 DCHECK(!successor->IsExitBlock());
1265
1266 HBasicBlock* block = got->GetBlock();
1267 HInstruction* previous = got->GetPrevious();
1268
1269 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001270 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001271 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1272 return;
1273 }
1274
1275 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1276 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1277 }
1278 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001279 __ jmp(codegen_->GetLabelOf(successor));
1280 }
1281}
1282
David Brazdilfc6a86a2015-06-26 10:33:45 +00001283void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1284 got->SetLocations(nullptr);
1285}
1286
1287void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1288 HandleGoto(got, got->GetSuccessor());
1289}
1290
1291void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1292 try_boundary->SetLocations(nullptr);
1293}
1294
1295void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1296 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1297 if (!successor->IsExitBlock()) {
1298 HandleGoto(try_boundary, successor);
1299 }
1300}
1301
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001302void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1303 exit->SetLocations(nullptr);
1304}
1305
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001306void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001307}
1308
Mark Mendell152408f2015-12-31 12:28:50 -05001309template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001310void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001311 LabelType* true_label,
1312 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001313 if (cond->IsFPConditionTrueIfNaN()) {
1314 __ j(kUnordered, true_label);
1315 } else if (cond->IsFPConditionFalseIfNaN()) {
1316 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001317 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001318 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001319}
1320
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001321void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001322 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001323
Mark Mendellc4701932015-04-10 13:18:51 -04001324 Location left = locations->InAt(0);
1325 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001326 Primitive::Type type = condition->InputAt(0)->GetType();
1327 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001328 case Primitive::kPrimBoolean:
1329 case Primitive::kPrimByte:
1330 case Primitive::kPrimChar:
1331 case Primitive::kPrimShort:
1332 case Primitive::kPrimInt:
1333 case Primitive::kPrimNot: {
1334 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1335 if (right.IsConstant()) {
1336 int32_t value = CodeGenerator::GetInt32ValueOf(right.GetConstant());
1337 if (value == 0) {
1338 __ testl(left_reg, left_reg);
1339 } else {
1340 __ cmpl(left_reg, Immediate(value));
1341 }
1342 } else if (right.IsStackSlot()) {
1343 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1344 } else {
1345 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1346 }
1347 break;
1348 }
Mark Mendellc4701932015-04-10 13:18:51 -04001349 case Primitive::kPrimLong: {
1350 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1351 if (right.IsConstant()) {
1352 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001353 codegen_->Compare64BitValue(left_reg, value);
Mark Mendellc4701932015-04-10 13:18:51 -04001354 } else if (right.IsDoubleStackSlot()) {
1355 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1356 } else {
1357 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1358 }
Mark Mendellc4701932015-04-10 13:18:51 -04001359 break;
1360 }
1361 case Primitive::kPrimFloat: {
1362 if (right.IsFpuRegister()) {
1363 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1364 } else if (right.IsConstant()) {
1365 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1366 codegen_->LiteralFloatAddress(
1367 right.GetConstant()->AsFloatConstant()->GetValue()));
1368 } else {
1369 DCHECK(right.IsStackSlot());
1370 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1371 Address(CpuRegister(RSP), right.GetStackIndex()));
1372 }
Mark Mendellc4701932015-04-10 13:18:51 -04001373 break;
1374 }
1375 case Primitive::kPrimDouble: {
1376 if (right.IsFpuRegister()) {
1377 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1378 } else if (right.IsConstant()) {
1379 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1380 codegen_->LiteralDoubleAddress(
1381 right.GetConstant()->AsDoubleConstant()->GetValue()));
1382 } else {
1383 DCHECK(right.IsDoubleStackSlot());
1384 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1385 Address(CpuRegister(RSP), right.GetStackIndex()));
1386 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001387 break;
1388 }
1389 default:
1390 LOG(FATAL) << "Unexpected condition type " << type;
1391 }
1392}
1393
1394template<class LabelType>
1395void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1396 LabelType* true_target_in,
1397 LabelType* false_target_in) {
1398 // Generated branching requires both targets to be explicit. If either of the
1399 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1400 LabelType fallthrough_target;
1401 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1402 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1403
1404 // Generate the comparison to set the CC.
1405 GenerateCompareTest(condition);
1406
1407 // Now generate the correct jump(s).
1408 Primitive::Type type = condition->InputAt(0)->GetType();
1409 switch (type) {
1410 case Primitive::kPrimLong: {
1411 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1412 break;
1413 }
1414 case Primitive::kPrimFloat: {
1415 GenerateFPJumps(condition, true_target, false_target);
1416 break;
1417 }
1418 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001419 GenerateFPJumps(condition, true_target, false_target);
1420 break;
1421 }
1422 default:
1423 LOG(FATAL) << "Unexpected condition type " << type;
1424 }
1425
David Brazdil0debae72015-11-12 18:37:00 +00001426 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001427 __ jmp(false_target);
1428 }
David Brazdil0debae72015-11-12 18:37:00 +00001429
1430 if (fallthrough_target.IsLinked()) {
1431 __ Bind(&fallthrough_target);
1432 }
Mark Mendellc4701932015-04-10 13:18:51 -04001433}
1434
David Brazdil0debae72015-11-12 18:37:00 +00001435static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1436 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1437 // are set only strictly before `branch`. We can't use the eflags on long
1438 // conditions if they are materialized due to the complex branching.
1439 return cond->IsCondition() &&
1440 cond->GetNext() == branch &&
1441 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1442}
1443
Mark Mendell152408f2015-12-31 12:28:50 -05001444template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001445void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001446 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001447 LabelType* true_target,
1448 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001449 HInstruction* cond = instruction->InputAt(condition_input_index);
1450
1451 if (true_target == nullptr && false_target == nullptr) {
1452 // Nothing to do. The code always falls through.
1453 return;
1454 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001455 // Constant condition, statically compared against "true" (integer value 1).
1456 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001457 if (true_target != nullptr) {
1458 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001459 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001460 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001461 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001462 if (false_target != nullptr) {
1463 __ jmp(false_target);
1464 }
1465 }
1466 return;
1467 }
1468
1469 // The following code generates these patterns:
1470 // (1) true_target == nullptr && false_target != nullptr
1471 // - opposite condition true => branch to false_target
1472 // (2) true_target != nullptr && false_target == nullptr
1473 // - condition true => branch to true_target
1474 // (3) true_target != nullptr && false_target != nullptr
1475 // - condition true => branch to true_target
1476 // - branch to false_target
1477 if (IsBooleanValueOrMaterializedCondition(cond)) {
1478 if (AreEflagsSetFrom(cond, instruction)) {
1479 if (true_target == nullptr) {
1480 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1481 } else {
1482 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1483 }
1484 } else {
1485 // Materialized condition, compare against 0.
1486 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1487 if (lhs.IsRegister()) {
1488 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1489 } else {
1490 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1491 }
1492 if (true_target == nullptr) {
1493 __ j(kEqual, false_target);
1494 } else {
1495 __ j(kNotEqual, true_target);
1496 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001497 }
1498 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001499 // Condition has not been materialized, use its inputs as the
1500 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001501 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001502
David Brazdil0debae72015-11-12 18:37:00 +00001503 // If this is a long or FP comparison that has been folded into
1504 // the HCondition, generate the comparison directly.
1505 Primitive::Type type = condition->InputAt(0)->GetType();
1506 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1507 GenerateCompareTestAndBranch(condition, true_target, false_target);
1508 return;
1509 }
1510
1511 Location lhs = condition->GetLocations()->InAt(0);
1512 Location rhs = condition->GetLocations()->InAt(1);
1513 if (rhs.IsRegister()) {
1514 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1515 } else if (rhs.IsConstant()) {
1516 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001517 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001518 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001519 __ cmpl(lhs.AsRegister<CpuRegister>(),
1520 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1521 }
1522 if (true_target == nullptr) {
1523 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1524 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001525 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001526 }
Dave Allison20dfc792014-06-16 20:44:29 -07001527 }
David Brazdil0debae72015-11-12 18:37:00 +00001528
1529 // If neither branch falls through (case 3), the conditional branch to `true_target`
1530 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1531 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001532 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001533 }
1534}
1535
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001536void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1538 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001539 locations->SetInAt(0, Location::Any());
1540 }
1541}
1542
1543void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001544 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1545 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1546 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1547 nullptr : codegen_->GetLabelOf(true_successor);
1548 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1549 nullptr : codegen_->GetLabelOf(false_successor);
1550 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001551}
1552
1553void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1554 LocationSummary* locations = new (GetGraph()->GetArena())
1555 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001556 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001557 locations->SetInAt(0, Location::Any());
1558 }
1559}
1560
1561void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001562 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001563 GenerateTestAndBranch<Label>(deoptimize,
1564 /* condition_input_index */ 0,
1565 slow_path->GetEntryLabel(),
1566 /* false_target */ nullptr);
1567}
1568
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001569static bool SelectCanUseCMOV(HSelect* select) {
1570 // There are no conditional move instructions for XMMs.
1571 if (Primitive::IsFloatingPointType(select->GetType())) {
1572 return false;
1573 }
1574
1575 // A FP condition doesn't generate the single CC that we need.
1576 HInstruction* condition = select->GetCondition();
1577 if (condition->IsCondition() &&
1578 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1579 return false;
1580 }
1581
1582 // We can generate a CMOV for this Select.
1583 return true;
1584}
1585
David Brazdil74eb1b22015-12-14 11:44:01 +00001586void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1588 if (Primitive::IsFloatingPointType(select->GetType())) {
1589 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001590 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001591 } else {
1592 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001593 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001594 if (select->InputAt(1)->IsConstant()) {
1595 locations->SetInAt(1, Location::RequiresRegister());
1596 } else {
1597 locations->SetInAt(1, Location::Any());
1598 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001599 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001600 locations->SetInAt(1, Location::Any());
1601 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001602 }
1603 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1604 locations->SetInAt(2, Location::RequiresRegister());
1605 }
1606 locations->SetOut(Location::SameAsFirstInput());
1607}
1608
1609void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1610 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001611 if (SelectCanUseCMOV(select)) {
1612 // If both the condition and the source types are integer, we can generate
1613 // a CMOV to implement Select.
1614 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001615 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001616 DCHECK(locations->InAt(0).Equals(locations->Out()));
1617
1618 HInstruction* select_condition = select->GetCondition();
1619 Condition cond = kNotEqual;
1620
1621 // Figure out how to test the 'condition'.
1622 if (select_condition->IsCondition()) {
1623 HCondition* condition = select_condition->AsCondition();
1624 if (!condition->IsEmittedAtUseSite()) {
1625 // This was a previously materialized condition.
1626 // Can we use the existing condition code?
1627 if (AreEflagsSetFrom(condition, select)) {
1628 // Materialization was the previous instruction. Condition codes are right.
1629 cond = X86_64IntegerCondition(condition->GetCondition());
1630 } else {
1631 // No, we have to recreate the condition code.
1632 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1633 __ testl(cond_reg, cond_reg);
1634 }
1635 } else {
1636 GenerateCompareTest(condition);
1637 cond = X86_64IntegerCondition(condition->GetCondition());
1638 }
1639 } else {
1640 // Must be a boolean condition, which needs to be compared to 0.
1641 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1642 __ testl(cond_reg, cond_reg);
1643 }
1644
1645 // If the condition is true, overwrite the output, which already contains false.
1646 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001647 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1648 if (value_true_loc.IsRegister()) {
1649 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1650 } else {
1651 __ cmov(cond,
1652 value_false,
1653 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1654 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001655 } else {
1656 NearLabel false_target;
1657 GenerateTestAndBranch<NearLabel>(select,
1658 /* condition_input_index */ 2,
1659 /* true_target */ nullptr,
1660 &false_target);
1661 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1662 __ Bind(&false_target);
1663 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001664}
1665
David Srbecky0cf44932015-12-09 14:09:59 +00001666void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1667 new (GetGraph()->GetArena()) LocationSummary(info);
1668}
1669
David Srbeckyd28f4a02016-03-14 17:14:24 +00001670void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1671 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001672}
1673
1674void CodeGeneratorX86_64::GenerateNop() {
1675 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001676}
1677
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001678void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001679 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001680 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001681 // Handle the long/FP comparisons made in instruction simplification.
1682 switch (cond->InputAt(0)->GetType()) {
1683 case Primitive::kPrimLong:
1684 locations->SetInAt(0, Location::RequiresRegister());
1685 locations->SetInAt(1, Location::Any());
1686 break;
1687 case Primitive::kPrimFloat:
1688 case Primitive::kPrimDouble:
1689 locations->SetInAt(0, Location::RequiresFpuRegister());
1690 locations->SetInAt(1, Location::Any());
1691 break;
1692 default:
1693 locations->SetInAt(0, Location::RequiresRegister());
1694 locations->SetInAt(1, Location::Any());
1695 break;
1696 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001697 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001698 locations->SetOut(Location::RequiresRegister());
1699 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001700}
1701
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001703 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001704 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001705 }
Mark Mendellc4701932015-04-10 13:18:51 -04001706
1707 LocationSummary* locations = cond->GetLocations();
1708 Location lhs = locations->InAt(0);
1709 Location rhs = locations->InAt(1);
1710 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001711 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001712
1713 switch (cond->InputAt(0)->GetType()) {
1714 default:
1715 // Integer case.
1716
1717 // Clear output register: setcc only sets the low byte.
1718 __ xorl(reg, reg);
1719
1720 if (rhs.IsRegister()) {
1721 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1722 } else if (rhs.IsConstant()) {
1723 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001724 codegen_->Compare32BitValue(lhs.AsRegister<CpuRegister>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001725 } else {
1726 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1727 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001728 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001729 return;
1730 case Primitive::kPrimLong:
1731 // Clear output register: setcc only sets the low byte.
1732 __ xorl(reg, reg);
1733
1734 if (rhs.IsRegister()) {
1735 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1736 } else if (rhs.IsConstant()) {
1737 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001738 codegen_->Compare64BitValue(lhs.AsRegister<CpuRegister>(), value);
Mark Mendellc4701932015-04-10 13:18:51 -04001739 } else {
1740 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1741 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001742 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001743 return;
1744 case Primitive::kPrimFloat: {
1745 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1746 if (rhs.IsConstant()) {
1747 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1748 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1749 } else if (rhs.IsStackSlot()) {
1750 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1751 } else {
1752 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1753 }
1754 GenerateFPJumps(cond, &true_label, &false_label);
1755 break;
1756 }
1757 case Primitive::kPrimDouble: {
1758 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1759 if (rhs.IsConstant()) {
1760 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1761 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1762 } else if (rhs.IsDoubleStackSlot()) {
1763 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1764 } else {
1765 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1766 }
1767 GenerateFPJumps(cond, &true_label, &false_label);
1768 break;
1769 }
1770 }
1771
1772 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001773 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001774
Roland Levillain4fa13f62015-07-06 18:11:54 +01001775 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001776 __ Bind(&false_label);
1777 __ xorl(reg, reg);
1778 __ jmp(&done_label);
1779
Roland Levillain4fa13f62015-07-06 18:11:54 +01001780 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001781 __ Bind(&true_label);
1782 __ movl(reg, Immediate(1));
1783 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001784}
1785
1786void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001788}
1789
1790void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001791 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001792}
1793
1794void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001795 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001796}
1797
1798void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001799 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001800}
1801
1802void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001803 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001804}
1805
1806void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001807 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001808}
1809
1810void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001811 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001812}
1813
1814void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001815 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001816}
1817
1818void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001819 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001820}
1821
1822void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001824}
1825
1826void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001827 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001828}
1829
1830void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001831 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001832}
1833
Aart Bike9f37602015-10-09 11:15:55 -07001834void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001835 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001836}
1837
1838void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001839 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001840}
1841
1842void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001843 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001844}
1845
1846void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001847 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001848}
1849
1850void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001851 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001852}
1853
1854void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001855 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001856}
1857
1858void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001859 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001860}
1861
1862void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001863 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001864}
1865
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001866void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001867 LocationSummary* locations =
1868 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001869 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001870 case Primitive::kPrimBoolean:
1871 case Primitive::kPrimByte:
1872 case Primitive::kPrimShort:
1873 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001874 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00001875 case Primitive::kPrimLong: {
1876 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001877 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001878 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1879 break;
1880 }
1881 case Primitive::kPrimFloat:
1882 case Primitive::kPrimDouble: {
1883 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001884 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001885 locations->SetOut(Location::RequiresRegister());
1886 break;
1887 }
1888 default:
1889 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1890 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001891}
1892
1893void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001894 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001895 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001896 Location left = locations->InAt(0);
1897 Location right = locations->InAt(1);
1898
Mark Mendell0c9497d2015-08-21 09:30:05 -04001899 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001900 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08001901 Condition less_cond = kLess;
1902
Calin Juravleddb7df22014-11-25 20:56:51 +00001903 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001904 case Primitive::kPrimBoolean:
1905 case Primitive::kPrimByte:
1906 case Primitive::kPrimShort:
1907 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001908 case Primitive::kPrimInt: {
1909 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1910 if (right.IsConstant()) {
1911 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
1912 codegen_->Compare32BitValue(left_reg, value);
1913 } else if (right.IsStackSlot()) {
1914 __ cmpl(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1915 } else {
1916 __ cmpl(left_reg, right.AsRegister<CpuRegister>());
1917 }
1918 break;
1919 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001920 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001921 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1922 if (right.IsConstant()) {
1923 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Aart Bika19616e2016-02-01 18:57:58 -08001924 codegen_->Compare64BitValue(left_reg, value);
Mark Mendell40741f32015-04-20 22:10:34 -04001925 } else if (right.IsDoubleStackSlot()) {
1926 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001927 } else {
1928 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1929 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001930 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001931 }
1932 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001933 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1934 if (right.IsConstant()) {
1935 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1936 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1937 } else if (right.IsStackSlot()) {
1938 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1939 } else {
1940 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1941 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001942 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001943 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001944 break;
1945 }
1946 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001947 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1948 if (right.IsConstant()) {
1949 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1950 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1951 } else if (right.IsDoubleStackSlot()) {
1952 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1953 } else {
1954 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1955 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001956 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08001957 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00001958 break;
1959 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001960 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001961 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001962 }
Aart Bika19616e2016-02-01 18:57:58 -08001963
Calin Juravleddb7df22014-11-25 20:56:51 +00001964 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001965 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08001966 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00001967
Calin Juravle91debbc2014-11-26 19:01:09 +00001968 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001969 __ movl(out, Immediate(1));
1970 __ jmp(&done);
1971
1972 __ Bind(&less);
1973 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001974
1975 __ Bind(&done);
1976}
1977
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001978void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001979 LocationSummary* locations =
1980 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001981 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001982}
1983
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001984void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001985 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001986}
1987
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001988void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1989 LocationSummary* locations =
1990 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1991 locations->SetOut(Location::ConstantLocation(constant));
1992}
1993
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001994void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001995 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001996}
1997
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001998void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001999 LocationSummary* locations =
2000 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002001 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002002}
2003
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002004void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002005 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002006}
2007
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002008void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2009 LocationSummary* locations =
2010 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2011 locations->SetOut(Location::ConstantLocation(constant));
2012}
2013
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002014void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002015 // Will be generated at use site.
2016}
2017
2018void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2019 LocationSummary* locations =
2020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2021 locations->SetOut(Location::ConstantLocation(constant));
2022}
2023
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002024void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2025 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002026 // Will be generated at use site.
2027}
2028
Calin Juravle27df7582015-04-17 19:12:31 +01002029void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2030 memory_barrier->SetLocations(nullptr);
2031}
2032
2033void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002034 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002035}
2036
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002037void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2038 ret->SetLocations(nullptr);
2039}
2040
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002041void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002042 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002043}
2044
2045void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002046 LocationSummary* locations =
2047 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002048 switch (ret->InputAt(0)->GetType()) {
2049 case Primitive::kPrimBoolean:
2050 case Primitive::kPrimByte:
2051 case Primitive::kPrimChar:
2052 case Primitive::kPrimShort:
2053 case Primitive::kPrimInt:
2054 case Primitive::kPrimNot:
2055 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002056 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002057 break;
2058
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002059 case Primitive::kPrimFloat:
2060 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002061 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002062 break;
2063
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002064 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002065 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002066 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002067}
2068
2069void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2070 if (kIsDebugBuild) {
2071 switch (ret->InputAt(0)->GetType()) {
2072 case Primitive::kPrimBoolean:
2073 case Primitive::kPrimByte:
2074 case Primitive::kPrimChar:
2075 case Primitive::kPrimShort:
2076 case Primitive::kPrimInt:
2077 case Primitive::kPrimNot:
2078 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002079 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002080 break;
2081
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002082 case Primitive::kPrimFloat:
2083 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002084 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002085 XMM0);
2086 break;
2087
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002088 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002089 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002090 }
2091 }
2092 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002093}
2094
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002095Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2096 switch (type) {
2097 case Primitive::kPrimBoolean:
2098 case Primitive::kPrimByte:
2099 case Primitive::kPrimChar:
2100 case Primitive::kPrimShort:
2101 case Primitive::kPrimInt:
2102 case Primitive::kPrimNot:
2103 case Primitive::kPrimLong:
2104 return Location::RegisterLocation(RAX);
2105
2106 case Primitive::kPrimVoid:
2107 return Location::NoLocation();
2108
2109 case Primitive::kPrimDouble:
2110 case Primitive::kPrimFloat:
2111 return Location::FpuRegisterLocation(XMM0);
2112 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002113
2114 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002115}
2116
2117Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2118 return Location::RegisterLocation(kMethodRegisterArgument);
2119}
2120
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002121Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002122 switch (type) {
2123 case Primitive::kPrimBoolean:
2124 case Primitive::kPrimByte:
2125 case Primitive::kPrimChar:
2126 case Primitive::kPrimShort:
2127 case Primitive::kPrimInt:
2128 case Primitive::kPrimNot: {
2129 uint32_t index = gp_index_++;
2130 stack_index_++;
2131 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002132 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002133 } else {
2134 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2135 }
2136 }
2137
2138 case Primitive::kPrimLong: {
2139 uint32_t index = gp_index_;
2140 stack_index_ += 2;
2141 if (index < calling_convention.GetNumberOfRegisters()) {
2142 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002143 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002144 } else {
2145 gp_index_ += 2;
2146 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2147 }
2148 }
2149
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002150 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002151 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002152 stack_index_++;
2153 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002154 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002155 } else {
2156 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2157 }
2158 }
2159
2160 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002161 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002162 stack_index_ += 2;
2163 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002164 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002165 } else {
2166 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2167 }
2168 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002169
2170 case Primitive::kPrimVoid:
2171 LOG(FATAL) << "Unexpected parameter type " << type;
2172 break;
2173 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002174 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002175}
2176
Calin Juravle175dc732015-08-25 15:42:32 +01002177void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2178 // The trampoline uses the same calling convention as dex calling conventions,
2179 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2180 // the method_idx.
2181 HandleInvoke(invoke);
2182}
2183
2184void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2185 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2186}
2187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002188void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002189 // Explicit clinit checks triggered by static invokes must have been pruned by
2190 // art::PrepareForRegisterAllocation.
2191 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002192
Mark Mendellfb8d2792015-03-31 22:16:59 -04002193 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002194 if (intrinsic.TryDispatch(invoke)) {
2195 return;
2196 }
2197
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002198 HandleInvoke(invoke);
2199}
2200
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002201static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2202 if (invoke->GetLocations()->Intrinsified()) {
2203 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2204 intrinsic.Dispatch(invoke);
2205 return true;
2206 }
2207 return false;
2208}
2209
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002210void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002211 // Explicit clinit checks triggered by static invokes must have been pruned by
2212 // art::PrepareForRegisterAllocation.
2213 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002214
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002215 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2216 return;
2217 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002218
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002219 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002220 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002221 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002222 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002223}
2224
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002225void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002226 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002227 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002228}
2229
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002230void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002231 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002232 if (intrinsic.TryDispatch(invoke)) {
2233 return;
2234 }
2235
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002236 HandleInvoke(invoke);
2237}
2238
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002239void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002240 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2241 return;
2242 }
2243
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002244 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002245 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002246 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002247}
2248
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002249void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2250 HandleInvoke(invoke);
2251 // Add the hidden argument.
2252 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2253}
2254
2255void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2256 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002257 LocationSummary* locations = invoke->GetLocations();
2258 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2259 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002260 Location receiver = locations->InAt(0);
2261 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2262
Roland Levillain0d5a2812015-11-13 10:07:31 +00002263 // Set the hidden argument. This is safe to do this here, as RAX
2264 // won't be modified thereafter, before the `call` instruction.
2265 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002266 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002267
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002268 if (receiver.IsStackSlot()) {
2269 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002270 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002271 __ movl(temp, Address(temp, class_offset));
2272 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002273 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002274 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002275 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002276 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002277 // Instead of simply (possibly) unpoisoning `temp` here, we should
2278 // emit a read barrier for the previous class reference load.
2279 // However this is not required in practice, as this is an
2280 // intermediate/temporary reference and because the current
2281 // concurrent copying collector keeps the from-space memory
2282 // intact/accessible until the end of the marking phase (the
2283 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002284 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002285 // temp = temp->GetAddressOfIMT()
2286 __ movq(temp,
2287 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2288 // temp = temp->GetImtEntryAt(method_offset);
2289 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2290 invoke->GetImtIndex() % ImTable::kSize, kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002291 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002292 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002293 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002294 __ call(Address(temp,
2295 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002296
2297 DCHECK(!codegen_->IsLeafMethod());
2298 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2299}
2300
Roland Levillain88cb1752014-10-20 16:36:47 +01002301void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2302 LocationSummary* locations =
2303 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2304 switch (neg->GetResultType()) {
2305 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002306 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002307 locations->SetInAt(0, Location::RequiresRegister());
2308 locations->SetOut(Location::SameAsFirstInput());
2309 break;
2310
Roland Levillain88cb1752014-10-20 16:36:47 +01002311 case Primitive::kPrimFloat:
2312 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002313 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002314 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002315 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002316 break;
2317
2318 default:
2319 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2320 }
2321}
2322
2323void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2324 LocationSummary* locations = neg->GetLocations();
2325 Location out = locations->Out();
2326 Location in = locations->InAt(0);
2327 switch (neg->GetResultType()) {
2328 case Primitive::kPrimInt:
2329 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002330 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002332 break;
2333
2334 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002335 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002336 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002338 break;
2339
Roland Levillain5368c212014-11-27 15:03:41 +00002340 case Primitive::kPrimFloat: {
2341 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002342 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002343 // Implement float negation with an exclusive or with value
2344 // 0x80000000 (mask for bit 31, representing the sign of a
2345 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002346 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002348 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002349 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002350
Roland Levillain5368c212014-11-27 15:03:41 +00002351 case Primitive::kPrimDouble: {
2352 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002353 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002354 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002355 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002356 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002357 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002358 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002359 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002360 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002361
2362 default:
2363 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2364 }
2365}
2366
Roland Levillaindff1f282014-11-05 14:15:05 +00002367void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2368 LocationSummary* locations =
2369 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2370 Primitive::Type result_type = conversion->GetResultType();
2371 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002372 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002373
David Brazdilb2bd1c52015-03-25 11:17:37 +00002374 // The Java language does not allow treating boolean as an integral type but
2375 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002376
Roland Levillaindff1f282014-11-05 14:15:05 +00002377 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002378 case Primitive::kPrimByte:
2379 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002380 case Primitive::kPrimLong:
2381 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002382 case Primitive::kPrimBoolean:
2383 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002384 case Primitive::kPrimShort:
2385 case Primitive::kPrimInt:
2386 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002387 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002388 locations->SetInAt(0, Location::Any());
2389 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2390 break;
2391
2392 default:
2393 LOG(FATAL) << "Unexpected type conversion from " << input_type
2394 << " to " << result_type;
2395 }
2396 break;
2397
Roland Levillain01a8d712014-11-14 16:27:39 +00002398 case Primitive::kPrimShort:
2399 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002400 case Primitive::kPrimLong:
2401 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002402 case Primitive::kPrimBoolean:
2403 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002404 case Primitive::kPrimByte:
2405 case Primitive::kPrimInt:
2406 case Primitive::kPrimChar:
2407 // Processing a Dex `int-to-short' instruction.
2408 locations->SetInAt(0, Location::Any());
2409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2410 break;
2411
2412 default:
2413 LOG(FATAL) << "Unexpected type conversion from " << input_type
2414 << " to " << result_type;
2415 }
2416 break;
2417
Roland Levillain946e1432014-11-11 17:35:19 +00002418 case Primitive::kPrimInt:
2419 switch (input_type) {
2420 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002421 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002422 locations->SetInAt(0, Location::Any());
2423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2424 break;
2425
2426 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002427 // Processing a Dex `float-to-int' instruction.
2428 locations->SetInAt(0, Location::RequiresFpuRegister());
2429 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002430 break;
2431
Roland Levillain946e1432014-11-11 17:35:19 +00002432 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002433 // Processing a Dex `double-to-int' instruction.
2434 locations->SetInAt(0, Location::RequiresFpuRegister());
2435 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002436 break;
2437
2438 default:
2439 LOG(FATAL) << "Unexpected type conversion from " << input_type
2440 << " to " << result_type;
2441 }
2442 break;
2443
Roland Levillaindff1f282014-11-05 14:15:05 +00002444 case Primitive::kPrimLong:
2445 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002446 case Primitive::kPrimBoolean:
2447 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002448 case Primitive::kPrimByte:
2449 case Primitive::kPrimShort:
2450 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002451 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002452 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002453 // TODO: We would benefit from a (to-be-implemented)
2454 // Location::RegisterOrStackSlot requirement for this input.
2455 locations->SetInAt(0, Location::RequiresRegister());
2456 locations->SetOut(Location::RequiresRegister());
2457 break;
2458
2459 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002460 // Processing a Dex `float-to-long' instruction.
2461 locations->SetInAt(0, Location::RequiresFpuRegister());
2462 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002463 break;
2464
Roland Levillaindff1f282014-11-05 14:15:05 +00002465 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002466 // Processing a Dex `double-to-long' instruction.
2467 locations->SetInAt(0, Location::RequiresFpuRegister());
2468 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002469 break;
2470
2471 default:
2472 LOG(FATAL) << "Unexpected type conversion from " << input_type
2473 << " to " << result_type;
2474 }
2475 break;
2476
Roland Levillain981e4542014-11-14 11:47:14 +00002477 case Primitive::kPrimChar:
2478 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002479 case Primitive::kPrimLong:
2480 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002481 case Primitive::kPrimBoolean:
2482 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002483 case Primitive::kPrimByte:
2484 case Primitive::kPrimShort:
2485 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002486 // Processing a Dex `int-to-char' instruction.
2487 locations->SetInAt(0, Location::Any());
2488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2489 break;
2490
2491 default:
2492 LOG(FATAL) << "Unexpected type conversion from " << input_type
2493 << " to " << result_type;
2494 }
2495 break;
2496
Roland Levillaindff1f282014-11-05 14:15:05 +00002497 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002498 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002499 case Primitive::kPrimBoolean:
2500 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002501 case Primitive::kPrimByte:
2502 case Primitive::kPrimShort:
2503 case Primitive::kPrimInt:
2504 case Primitive::kPrimChar:
2505 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002506 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002507 locations->SetOut(Location::RequiresFpuRegister());
2508 break;
2509
2510 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002511 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002512 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002513 locations->SetOut(Location::RequiresFpuRegister());
2514 break;
2515
Roland Levillaincff13742014-11-17 14:32:17 +00002516 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002517 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002518 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002519 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002520 break;
2521
2522 default:
2523 LOG(FATAL) << "Unexpected type conversion from " << input_type
2524 << " to " << result_type;
2525 };
2526 break;
2527
Roland Levillaindff1f282014-11-05 14:15:05 +00002528 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002529 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002530 case Primitive::kPrimBoolean:
2531 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002532 case Primitive::kPrimByte:
2533 case Primitive::kPrimShort:
2534 case Primitive::kPrimInt:
2535 case Primitive::kPrimChar:
2536 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002537 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002538 locations->SetOut(Location::RequiresFpuRegister());
2539 break;
2540
2541 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002542 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002543 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002544 locations->SetOut(Location::RequiresFpuRegister());
2545 break;
2546
Roland Levillaincff13742014-11-17 14:32:17 +00002547 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002548 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002549 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002550 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002551 break;
2552
2553 default:
2554 LOG(FATAL) << "Unexpected type conversion from " << input_type
2555 << " to " << result_type;
2556 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002557 break;
2558
2559 default:
2560 LOG(FATAL) << "Unexpected type conversion from " << input_type
2561 << " to " << result_type;
2562 }
2563}
2564
2565void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2566 LocationSummary* locations = conversion->GetLocations();
2567 Location out = locations->Out();
2568 Location in = locations->InAt(0);
2569 Primitive::Type result_type = conversion->GetResultType();
2570 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002571 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002572 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002573 case Primitive::kPrimByte:
2574 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002575 case Primitive::kPrimLong:
2576 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002577 case Primitive::kPrimBoolean:
2578 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002579 case Primitive::kPrimShort:
2580 case Primitive::kPrimInt:
2581 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002582 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002583 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002584 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002585 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002586 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002587 Address(CpuRegister(RSP), in.GetStackIndex()));
2588 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002589 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002590 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002591 }
2592 break;
2593
2594 default:
2595 LOG(FATAL) << "Unexpected type conversion from " << input_type
2596 << " to " << result_type;
2597 }
2598 break;
2599
Roland Levillain01a8d712014-11-14 16:27:39 +00002600 case Primitive::kPrimShort:
2601 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002602 case Primitive::kPrimLong:
2603 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002604 case Primitive::kPrimBoolean:
2605 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002606 case Primitive::kPrimByte:
2607 case Primitive::kPrimInt:
2608 case Primitive::kPrimChar:
2609 // Processing a Dex `int-to-short' instruction.
2610 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002611 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002612 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002613 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002614 Address(CpuRegister(RSP), in.GetStackIndex()));
2615 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002616 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002617 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002618 }
2619 break;
2620
2621 default:
2622 LOG(FATAL) << "Unexpected type conversion from " << input_type
2623 << " to " << result_type;
2624 }
2625 break;
2626
Roland Levillain946e1432014-11-11 17:35:19 +00002627 case Primitive::kPrimInt:
2628 switch (input_type) {
2629 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002630 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002631 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002632 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002633 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002634 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002635 Address(CpuRegister(RSP), in.GetStackIndex()));
2636 } else {
2637 DCHECK(in.IsConstant());
2638 DCHECK(in.GetConstant()->IsLongConstant());
2639 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002641 }
2642 break;
2643
Roland Levillain3f8f9362014-12-02 17:45:01 +00002644 case Primitive::kPrimFloat: {
2645 // Processing a Dex `float-to-int' instruction.
2646 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2647 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002648 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002649
2650 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002651 // if input >= (float)INT_MAX goto done
2652 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002653 __ j(kAboveEqual, &done);
2654 // if input == NaN goto nan
2655 __ j(kUnordered, &nan);
2656 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002657 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002658 __ jmp(&done);
2659 __ Bind(&nan);
2660 // output = 0
2661 __ xorl(output, output);
2662 __ Bind(&done);
2663 break;
2664 }
2665
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002666 case Primitive::kPrimDouble: {
2667 // Processing a Dex `double-to-int' instruction.
2668 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2669 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002670 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002671
2672 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002673 // if input >= (double)INT_MAX goto done
2674 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002675 __ j(kAboveEqual, &done);
2676 // if input == NaN goto nan
2677 __ j(kUnordered, &nan);
2678 // output = double-to-int-truncate(input)
2679 __ cvttsd2si(output, input);
2680 __ jmp(&done);
2681 __ Bind(&nan);
2682 // output = 0
2683 __ xorl(output, output);
2684 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002685 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002686 }
Roland Levillain946e1432014-11-11 17:35:19 +00002687
2688 default:
2689 LOG(FATAL) << "Unexpected type conversion from " << input_type
2690 << " to " << result_type;
2691 }
2692 break;
2693
Roland Levillaindff1f282014-11-05 14:15:05 +00002694 case Primitive::kPrimLong:
2695 switch (input_type) {
2696 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002697 case Primitive::kPrimBoolean:
2698 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002699 case Primitive::kPrimByte:
2700 case Primitive::kPrimShort:
2701 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002702 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002703 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002704 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002705 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002706 break;
2707
Roland Levillain624279f2014-12-04 11:54:28 +00002708 case Primitive::kPrimFloat: {
2709 // Processing a Dex `float-to-long' instruction.
2710 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2711 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002712 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002713
Mark Mendell92e83bf2015-05-07 11:25:03 -04002714 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002715 // if input >= (float)LONG_MAX goto done
2716 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002717 __ j(kAboveEqual, &done);
2718 // if input == NaN goto nan
2719 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002720 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002721 __ cvttss2si(output, input, true);
2722 __ jmp(&done);
2723 __ Bind(&nan);
2724 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002725 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002726 __ Bind(&done);
2727 break;
2728 }
2729
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002730 case Primitive::kPrimDouble: {
2731 // Processing a Dex `double-to-long' instruction.
2732 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2733 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002734 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002735
Mark Mendell92e83bf2015-05-07 11:25:03 -04002736 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002737 // if input >= (double)LONG_MAX goto done
2738 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002739 __ j(kAboveEqual, &done);
2740 // if input == NaN goto nan
2741 __ j(kUnordered, &nan);
2742 // output = double-to-long-truncate(input)
2743 __ cvttsd2si(output, input, true);
2744 __ jmp(&done);
2745 __ Bind(&nan);
2746 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002747 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002748 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002749 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002750 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002751
2752 default:
2753 LOG(FATAL) << "Unexpected type conversion from " << input_type
2754 << " to " << result_type;
2755 }
2756 break;
2757
Roland Levillain981e4542014-11-14 11:47:14 +00002758 case Primitive::kPrimChar:
2759 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002760 case Primitive::kPrimLong:
2761 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002762 case Primitive::kPrimBoolean:
2763 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002764 case Primitive::kPrimByte:
2765 case Primitive::kPrimShort:
2766 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002767 // Processing a Dex `int-to-char' instruction.
2768 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002769 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002770 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002772 Address(CpuRegister(RSP), in.GetStackIndex()));
2773 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002775 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002776 }
2777 break;
2778
2779 default:
2780 LOG(FATAL) << "Unexpected type conversion from " << input_type
2781 << " to " << result_type;
2782 }
2783 break;
2784
Roland Levillaindff1f282014-11-05 14:15:05 +00002785 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002786 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002787 case Primitive::kPrimBoolean:
2788 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002789 case Primitive::kPrimByte:
2790 case Primitive::kPrimShort:
2791 case Primitive::kPrimInt:
2792 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002793 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002794 if (in.IsRegister()) {
2795 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2796 } else if (in.IsConstant()) {
2797 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2798 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002799 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002800 } else {
2801 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2802 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2803 }
Roland Levillaincff13742014-11-17 14:32:17 +00002804 break;
2805
2806 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002807 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002808 if (in.IsRegister()) {
2809 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2810 } else if (in.IsConstant()) {
2811 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2812 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002813 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002814 } else {
2815 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2816 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2817 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002818 break;
2819
Roland Levillaincff13742014-11-17 14:32:17 +00002820 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002821 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002822 if (in.IsFpuRegister()) {
2823 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2824 } else if (in.IsConstant()) {
2825 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2826 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002827 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002828 } else {
2829 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2830 Address(CpuRegister(RSP), in.GetStackIndex()));
2831 }
Roland Levillaincff13742014-11-17 14:32:17 +00002832 break;
2833
2834 default:
2835 LOG(FATAL) << "Unexpected type conversion from " << input_type
2836 << " to " << result_type;
2837 };
2838 break;
2839
Roland Levillaindff1f282014-11-05 14:15:05 +00002840 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002841 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002842 case Primitive::kPrimBoolean:
2843 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002844 case Primitive::kPrimByte:
2845 case Primitive::kPrimShort:
2846 case Primitive::kPrimInt:
2847 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002848 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002849 if (in.IsRegister()) {
2850 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2851 } else if (in.IsConstant()) {
2852 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2853 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002854 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002855 } else {
2856 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2857 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2858 }
Roland Levillaincff13742014-11-17 14:32:17 +00002859 break;
2860
2861 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002862 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002863 if (in.IsRegister()) {
2864 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2865 } else if (in.IsConstant()) {
2866 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2867 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002868 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002869 } else {
2870 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2871 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2872 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002873 break;
2874
Roland Levillaincff13742014-11-17 14:32:17 +00002875 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002876 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002877 if (in.IsFpuRegister()) {
2878 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2879 } else if (in.IsConstant()) {
2880 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2881 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002882 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002883 } else {
2884 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2885 Address(CpuRegister(RSP), in.GetStackIndex()));
2886 }
Roland Levillaincff13742014-11-17 14:32:17 +00002887 break;
2888
2889 default:
2890 LOG(FATAL) << "Unexpected type conversion from " << input_type
2891 << " to " << result_type;
2892 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002893 break;
2894
2895 default:
2896 LOG(FATAL) << "Unexpected type conversion from " << input_type
2897 << " to " << result_type;
2898 }
2899}
2900
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002901void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002902 LocationSummary* locations =
2903 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002904 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002905 case Primitive::kPrimInt: {
2906 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002907 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002909 break;
2910 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002911
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002912 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002913 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002914 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002915 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002917 break;
2918 }
2919
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002920 case Primitive::kPrimDouble:
2921 case Primitive::kPrimFloat: {
2922 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002923 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002924 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002925 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002926 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002927
2928 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002929 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002930 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002931}
2932
2933void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2934 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002935 Location first = locations->InAt(0);
2936 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002937 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002938
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002939 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002940 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002941 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002942 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2943 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002944 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2945 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002946 } else {
2947 __ leal(out.AsRegister<CpuRegister>(), Address(
2948 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2949 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002950 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002951 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2952 __ addl(out.AsRegister<CpuRegister>(),
2953 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2954 } else {
2955 __ leal(out.AsRegister<CpuRegister>(), Address(
2956 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2957 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002958 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002959 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002960 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002961 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002962 break;
2963 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002964
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002965 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002966 if (second.IsRegister()) {
2967 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2968 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002969 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2970 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002971 } else {
2972 __ leaq(out.AsRegister<CpuRegister>(), Address(
2973 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2974 }
2975 } else {
2976 DCHECK(second.IsConstant());
2977 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2978 int32_t int32_value = Low32Bits(value);
2979 DCHECK_EQ(int32_value, value);
2980 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2981 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2982 } else {
2983 __ leaq(out.AsRegister<CpuRegister>(), Address(
2984 first.AsRegister<CpuRegister>(), int32_value));
2985 }
2986 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002987 break;
2988 }
2989
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002990 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002991 if (second.IsFpuRegister()) {
2992 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2993 } else if (second.IsConstant()) {
2994 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002995 codegen_->LiteralFloatAddress(
2996 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002997 } else {
2998 DCHECK(second.IsStackSlot());
2999 __ addss(first.AsFpuRegister<XmmRegister>(),
3000 Address(CpuRegister(RSP), second.GetStackIndex()));
3001 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003002 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003003 }
3004
3005 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003006 if (second.IsFpuRegister()) {
3007 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3008 } else if (second.IsConstant()) {
3009 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003010 codegen_->LiteralDoubleAddress(
3011 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003012 } else {
3013 DCHECK(second.IsDoubleStackSlot());
3014 __ addsd(first.AsFpuRegister<XmmRegister>(),
3015 Address(CpuRegister(RSP), second.GetStackIndex()));
3016 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003017 break;
3018 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003019
3020 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003021 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003022 }
3023}
3024
3025void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003026 LocationSummary* locations =
3027 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003028 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003029 case Primitive::kPrimInt: {
3030 locations->SetInAt(0, Location::RequiresRegister());
3031 locations->SetInAt(1, Location::Any());
3032 locations->SetOut(Location::SameAsFirstInput());
3033 break;
3034 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003036 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003037 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003038 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003039 break;
3040 }
Calin Juravle11351682014-10-23 15:38:15 +01003041 case Primitive::kPrimFloat:
3042 case Primitive::kPrimDouble: {
3043 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003044 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003045 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003046 break;
Calin Juravle11351682014-10-23 15:38:15 +01003047 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003048 default:
Calin Juravle11351682014-10-23 15:38:15 +01003049 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003050 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003051}
3052
3053void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3054 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003055 Location first = locations->InAt(0);
3056 Location second = locations->InAt(1);
3057 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003058 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003059 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003060 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003061 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003062 } else if (second.IsConstant()) {
3063 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003065 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003066 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003067 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003068 break;
3069 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003070 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003071 if (second.IsConstant()) {
3072 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3073 DCHECK(IsInt<32>(value));
3074 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3075 } else {
3076 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3077 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003078 break;
3079 }
3080
Calin Juravle11351682014-10-23 15:38:15 +01003081 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003082 if (second.IsFpuRegister()) {
3083 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3084 } else if (second.IsConstant()) {
3085 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003086 codegen_->LiteralFloatAddress(
3087 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003088 } else {
3089 DCHECK(second.IsStackSlot());
3090 __ subss(first.AsFpuRegister<XmmRegister>(),
3091 Address(CpuRegister(RSP), second.GetStackIndex()));
3092 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003093 break;
Calin Juravle11351682014-10-23 15:38:15 +01003094 }
3095
3096 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003097 if (second.IsFpuRegister()) {
3098 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3099 } else if (second.IsConstant()) {
3100 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003101 codegen_->LiteralDoubleAddress(
3102 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003103 } else {
3104 DCHECK(second.IsDoubleStackSlot());
3105 __ subsd(first.AsFpuRegister<XmmRegister>(),
3106 Address(CpuRegister(RSP), second.GetStackIndex()));
3107 }
Calin Juravle11351682014-10-23 15:38:15 +01003108 break;
3109 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003110
3111 default:
Calin Juravle11351682014-10-23 15:38:15 +01003112 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003113 }
3114}
3115
Calin Juravle34bacdf2014-10-07 20:23:36 +01003116void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3117 LocationSummary* locations =
3118 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3119 switch (mul->GetResultType()) {
3120 case Primitive::kPrimInt: {
3121 locations->SetInAt(0, Location::RequiresRegister());
3122 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003123 if (mul->InputAt(1)->IsIntConstant()) {
3124 // Can use 3 operand multiply.
3125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3126 } else {
3127 locations->SetOut(Location::SameAsFirstInput());
3128 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003129 break;
3130 }
3131 case Primitive::kPrimLong: {
3132 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003133 locations->SetInAt(1, Location::Any());
3134 if (mul->InputAt(1)->IsLongConstant() &&
3135 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003136 // Can use 3 operand multiply.
3137 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3138 } else {
3139 locations->SetOut(Location::SameAsFirstInput());
3140 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003141 break;
3142 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003143 case Primitive::kPrimFloat:
3144 case Primitive::kPrimDouble: {
3145 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003146 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003147 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003148 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003149 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150
3151 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003152 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003153 }
3154}
3155
3156void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3157 LocationSummary* locations = mul->GetLocations();
3158 Location first = locations->InAt(0);
3159 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003160 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003162 case Primitive::kPrimInt:
3163 // The constant may have ended up in a register, so test explicitly to avoid
3164 // problems where the output may not be the same as the first operand.
3165 if (mul->InputAt(1)->IsIntConstant()) {
3166 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3167 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3168 } else if (second.IsRegister()) {
3169 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003170 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003172 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003173 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003174 __ imull(first.AsRegister<CpuRegister>(),
3175 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003176 }
3177 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003178 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003179 // The constant may have ended up in a register, so test explicitly to avoid
3180 // problems where the output may not be the same as the first operand.
3181 if (mul->InputAt(1)->IsLongConstant()) {
3182 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3183 if (IsInt<32>(value)) {
3184 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3185 Immediate(static_cast<int32_t>(value)));
3186 } else {
3187 // Have to use the constant area.
3188 DCHECK(first.Equals(out));
3189 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3190 }
3191 } else if (second.IsRegister()) {
3192 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003193 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003194 } else {
3195 DCHECK(second.IsDoubleStackSlot());
3196 DCHECK(first.Equals(out));
3197 __ imulq(first.AsRegister<CpuRegister>(),
3198 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003199 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 break;
3201 }
3202
Calin Juravleb5bfa962014-10-21 18:02:24 +01003203 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003204 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003205 if (second.IsFpuRegister()) {
3206 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3207 } else if (second.IsConstant()) {
3208 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003209 codegen_->LiteralFloatAddress(
3210 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003211 } else {
3212 DCHECK(second.IsStackSlot());
3213 __ mulss(first.AsFpuRegister<XmmRegister>(),
3214 Address(CpuRegister(RSP), second.GetStackIndex()));
3215 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003216 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003217 }
3218
3219 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003220 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003221 if (second.IsFpuRegister()) {
3222 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3223 } else if (second.IsConstant()) {
3224 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003225 codegen_->LiteralDoubleAddress(
3226 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003227 } else {
3228 DCHECK(second.IsDoubleStackSlot());
3229 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3230 Address(CpuRegister(RSP), second.GetStackIndex()));
3231 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003232 break;
3233 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003234
3235 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003236 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003237 }
3238}
3239
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003240void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3241 uint32_t stack_adjustment, bool is_float) {
3242 if (source.IsStackSlot()) {
3243 DCHECK(is_float);
3244 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3245 } else if (source.IsDoubleStackSlot()) {
3246 DCHECK(!is_float);
3247 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3248 } else {
3249 // Write the value to the temporary location on the stack and load to FP stack.
3250 if (is_float) {
3251 Location stack_temp = Location::StackSlot(temp_offset);
3252 codegen_->Move(stack_temp, source);
3253 __ flds(Address(CpuRegister(RSP), temp_offset));
3254 } else {
3255 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3256 codegen_->Move(stack_temp, source);
3257 __ fldl(Address(CpuRegister(RSP), temp_offset));
3258 }
3259 }
3260}
3261
3262void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3263 Primitive::Type type = rem->GetResultType();
3264 bool is_float = type == Primitive::kPrimFloat;
3265 size_t elem_size = Primitive::ComponentSize(type);
3266 LocationSummary* locations = rem->GetLocations();
3267 Location first = locations->InAt(0);
3268 Location second = locations->InAt(1);
3269 Location out = locations->Out();
3270
3271 // Create stack space for 2 elements.
3272 // TODO: enhance register allocator to ask for stack temporaries.
3273 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3274
3275 // Load the values to the FP stack in reverse order, using temporaries if needed.
3276 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3277 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3278
3279 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003280 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003281 __ Bind(&retry);
3282 __ fprem();
3283
3284 // Move FP status to AX.
3285 __ fstsw();
3286
3287 // And see if the argument reduction is complete. This is signaled by the
3288 // C2 FPU flag bit set to 0.
3289 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3290 __ j(kNotEqual, &retry);
3291
3292 // We have settled on the final value. Retrieve it into an XMM register.
3293 // Store FP top of stack to real stack.
3294 if (is_float) {
3295 __ fsts(Address(CpuRegister(RSP), 0));
3296 } else {
3297 __ fstl(Address(CpuRegister(RSP), 0));
3298 }
3299
3300 // Pop the 2 items from the FP stack.
3301 __ fucompp();
3302
3303 // Load the value from the stack into an XMM register.
3304 DCHECK(out.IsFpuRegister()) << out;
3305 if (is_float) {
3306 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3307 } else {
3308 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3309 }
3310
3311 // And remove the temporary stack space we allocated.
3312 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3313}
3314
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003315void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3316 DCHECK(instruction->IsDiv() || instruction->IsRem());
3317
3318 LocationSummary* locations = instruction->GetLocations();
3319 Location second = locations->InAt(1);
3320 DCHECK(second.IsConstant());
3321
3322 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3323 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003324 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003325
3326 DCHECK(imm == 1 || imm == -1);
3327
3328 switch (instruction->GetResultType()) {
3329 case Primitive::kPrimInt: {
3330 if (instruction->IsRem()) {
3331 __ xorl(output_register, output_register);
3332 } else {
3333 __ movl(output_register, input_register);
3334 if (imm == -1) {
3335 __ negl(output_register);
3336 }
3337 }
3338 break;
3339 }
3340
3341 case Primitive::kPrimLong: {
3342 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003343 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003344 } else {
3345 __ movq(output_register, input_register);
3346 if (imm == -1) {
3347 __ negq(output_register);
3348 }
3349 }
3350 break;
3351 }
3352
3353 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003354 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003355 }
3356}
3357
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003358void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003359 LocationSummary* locations = instruction->GetLocations();
3360 Location second = locations->InAt(1);
3361
3362 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3363 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3364
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003365 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003366 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3367 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003368
3369 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3370
3371 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003372 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003373 __ testl(numerator, numerator);
3374 __ cmov(kGreaterEqual, tmp, numerator);
3375 int shift = CTZ(imm);
3376 __ sarl(tmp, Immediate(shift));
3377
3378 if (imm < 0) {
3379 __ negl(tmp);
3380 }
3381
3382 __ movl(output_register, tmp);
3383 } else {
3384 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3385 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3386
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003387 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003388 __ addq(rdx, numerator);
3389 __ testq(numerator, numerator);
3390 __ cmov(kGreaterEqual, rdx, numerator);
3391 int shift = CTZ(imm);
3392 __ sarq(rdx, Immediate(shift));
3393
3394 if (imm < 0) {
3395 __ negq(rdx);
3396 }
3397
3398 __ movq(output_register, rdx);
3399 }
3400}
3401
3402void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3403 DCHECK(instruction->IsDiv() || instruction->IsRem());
3404
3405 LocationSummary* locations = instruction->GetLocations();
3406 Location second = locations->InAt(1);
3407
3408 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3409 : locations->GetTemp(0).AsRegister<CpuRegister>();
3410 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3411 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3412 : locations->Out().AsRegister<CpuRegister>();
3413 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3414
3415 DCHECK_EQ(RAX, eax.AsRegister());
3416 DCHECK_EQ(RDX, edx.AsRegister());
3417 if (instruction->IsDiv()) {
3418 DCHECK_EQ(RAX, out.AsRegister());
3419 } else {
3420 DCHECK_EQ(RDX, out.AsRegister());
3421 }
3422
3423 int64_t magic;
3424 int shift;
3425
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003426 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427 if (instruction->GetResultType() == Primitive::kPrimInt) {
3428 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3429
3430 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3431
3432 __ movl(numerator, eax);
3433
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434 __ movl(eax, Immediate(magic));
3435 __ imull(numerator);
3436
3437 if (imm > 0 && magic < 0) {
3438 __ addl(edx, numerator);
3439 } else if (imm < 0 && magic > 0) {
3440 __ subl(edx, numerator);
3441 }
3442
3443 if (shift != 0) {
3444 __ sarl(edx, Immediate(shift));
3445 }
3446
3447 __ movl(eax, edx);
3448 __ shrl(edx, Immediate(31));
3449 __ addl(edx, eax);
3450
3451 if (instruction->IsRem()) {
3452 __ movl(eax, numerator);
3453 __ imull(edx, Immediate(imm));
3454 __ subl(eax, edx);
3455 __ movl(edx, eax);
3456 } else {
3457 __ movl(eax, edx);
3458 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003459 } else {
3460 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3461
3462 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3463
3464 CpuRegister rax = eax;
3465 CpuRegister rdx = edx;
3466
3467 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3468
3469 // Save the numerator.
3470 __ movq(numerator, rax);
3471
3472 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003473 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474
3475 // RDX:RAX = magic * numerator
3476 __ imulq(numerator);
3477
3478 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003479 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003480 __ addq(rdx, numerator);
3481 } else if (imm < 0 && magic > 0) {
3482 // RDX -= numerator
3483 __ subq(rdx, numerator);
3484 }
3485
3486 // Shift if needed.
3487 if (shift != 0) {
3488 __ sarq(rdx, Immediate(shift));
3489 }
3490
3491 // RDX += 1 if RDX < 0
3492 __ movq(rax, rdx);
3493 __ shrq(rdx, Immediate(63));
3494 __ addq(rdx, rax);
3495
3496 if (instruction->IsRem()) {
3497 __ movq(rax, numerator);
3498
3499 if (IsInt<32>(imm)) {
3500 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3501 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003502 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003503 }
3504
3505 __ subq(rax, rdx);
3506 __ movq(rdx, rax);
3507 } else {
3508 __ movq(rax, rdx);
3509 }
3510 }
3511}
3512
Calin Juravlebacfec32014-11-14 15:54:36 +00003513void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3514 DCHECK(instruction->IsDiv() || instruction->IsRem());
3515 Primitive::Type type = instruction->GetResultType();
3516 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3517
3518 bool is_div = instruction->IsDiv();
3519 LocationSummary* locations = instruction->GetLocations();
3520
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3522 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003523
Roland Levillain271ab9c2014-11-27 15:23:57 +00003524 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003526
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003527 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003528 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003529
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003530 if (imm == 0) {
3531 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3532 } else if (imm == 1 || imm == -1) {
3533 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003534 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003535 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003536 } else {
3537 DCHECK(imm <= -2 || imm >= 2);
3538 GenerateDivRemWithAnyConstant(instruction);
3539 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003540 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003541 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003542 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003543 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003545
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003546 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3547 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3548 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3549 // so it's safe to just use negl instead of more complex comparisons.
3550 if (type == Primitive::kPrimInt) {
3551 __ cmpl(second_reg, Immediate(-1));
3552 __ j(kEqual, slow_path->GetEntryLabel());
3553 // edx:eax <- sign-extended of eax
3554 __ cdq();
3555 // eax = quotient, edx = remainder
3556 __ idivl(second_reg);
3557 } else {
3558 __ cmpq(second_reg, Immediate(-1));
3559 __ j(kEqual, slow_path->GetEntryLabel());
3560 // rdx:rax <- sign-extended of rax
3561 __ cqo();
3562 // rax = quotient, rdx = remainder
3563 __ idivq(second_reg);
3564 }
3565 __ Bind(slow_path->GetExitLabel());
3566 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003567}
3568
Calin Juravle7c4954d2014-10-28 16:57:40 +00003569void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3570 LocationSummary* locations =
3571 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3572 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003573 case Primitive::kPrimInt:
3574 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003575 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003576 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003577 locations->SetOut(Location::SameAsFirstInput());
3578 // Intel uses edx:eax as the dividend.
3579 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3581 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3582 // output and request another temp.
3583 if (div->InputAt(1)->IsConstant()) {
3584 locations->AddTemp(Location::RequiresRegister());
3585 }
Calin Juravled0d48522014-11-04 16:40:20 +00003586 break;
3587 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003588
Calin Juravle7c4954d2014-10-28 16:57:40 +00003589 case Primitive::kPrimFloat:
3590 case Primitive::kPrimDouble: {
3591 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003592 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003593 locations->SetOut(Location::SameAsFirstInput());
3594 break;
3595 }
3596
3597 default:
3598 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3599 }
3600}
3601
3602void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3603 LocationSummary* locations = div->GetLocations();
3604 Location first = locations->InAt(0);
3605 Location second = locations->InAt(1);
3606 DCHECK(first.Equals(locations->Out()));
3607
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003608 Primitive::Type type = div->GetResultType();
3609 switch (type) {
3610 case Primitive::kPrimInt:
3611 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003612 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003613 break;
3614 }
3615
Calin Juravle7c4954d2014-10-28 16:57:40 +00003616 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003617 if (second.IsFpuRegister()) {
3618 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3619 } else if (second.IsConstant()) {
3620 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003621 codegen_->LiteralFloatAddress(
3622 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003623 } else {
3624 DCHECK(second.IsStackSlot());
3625 __ divss(first.AsFpuRegister<XmmRegister>(),
3626 Address(CpuRegister(RSP), second.GetStackIndex()));
3627 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003628 break;
3629 }
3630
3631 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003632 if (second.IsFpuRegister()) {
3633 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3634 } else if (second.IsConstant()) {
3635 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003636 codegen_->LiteralDoubleAddress(
3637 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003638 } else {
3639 DCHECK(second.IsDoubleStackSlot());
3640 __ divsd(first.AsFpuRegister<XmmRegister>(),
3641 Address(CpuRegister(RSP), second.GetStackIndex()));
3642 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003643 break;
3644 }
3645
3646 default:
3647 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3648 }
3649}
3650
Calin Juravlebacfec32014-11-14 15:54:36 +00003651void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003652 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003653 LocationSummary* locations =
3654 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003655
3656 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003657 case Primitive::kPrimInt:
3658 case Primitive::kPrimLong: {
3659 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003660 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003661 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3662 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003663 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3664 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3665 // output and request another temp.
3666 if (rem->InputAt(1)->IsConstant()) {
3667 locations->AddTemp(Location::RequiresRegister());
3668 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003669 break;
3670 }
3671
3672 case Primitive::kPrimFloat:
3673 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003674 locations->SetInAt(0, Location::Any());
3675 locations->SetInAt(1, Location::Any());
3676 locations->SetOut(Location::RequiresFpuRegister());
3677 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003678 break;
3679 }
3680
3681 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003682 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003683 }
3684}
3685
3686void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3687 Primitive::Type type = rem->GetResultType();
3688 switch (type) {
3689 case Primitive::kPrimInt:
3690 case Primitive::kPrimLong: {
3691 GenerateDivRemIntegral(rem);
3692 break;
3693 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003694 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003695 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003696 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003697 break;
3698 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003699 default:
3700 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3701 }
3702}
3703
Calin Juravled0d48522014-11-04 16:40:20 +00003704void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003705 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3706 ? LocationSummary::kCallOnSlowPath
3707 : LocationSummary::kNoCall;
3708 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003709 locations->SetInAt(0, Location::Any());
3710 if (instruction->HasUses()) {
3711 locations->SetOut(Location::SameAsFirstInput());
3712 }
3713}
3714
3715void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003716 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003717 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3718 codegen_->AddSlowPath(slow_path);
3719
3720 LocationSummary* locations = instruction->GetLocations();
3721 Location value = locations->InAt(0);
3722
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003723 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003724 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003725 case Primitive::kPrimByte:
3726 case Primitive::kPrimChar:
3727 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003728 case Primitive::kPrimInt: {
3729 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003730 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003731 __ j(kEqual, slow_path->GetEntryLabel());
3732 } else if (value.IsStackSlot()) {
3733 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3734 __ j(kEqual, slow_path->GetEntryLabel());
3735 } else {
3736 DCHECK(value.IsConstant()) << value;
3737 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3738 __ jmp(slow_path->GetEntryLabel());
3739 }
3740 }
3741 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003742 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003743 case Primitive::kPrimLong: {
3744 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003745 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003746 __ j(kEqual, slow_path->GetEntryLabel());
3747 } else if (value.IsDoubleStackSlot()) {
3748 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3749 __ j(kEqual, slow_path->GetEntryLabel());
3750 } else {
3751 DCHECK(value.IsConstant()) << value;
3752 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3753 __ jmp(slow_path->GetEntryLabel());
3754 }
3755 }
3756 break;
3757 }
3758 default:
3759 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003760 }
Calin Juravled0d48522014-11-04 16:40:20 +00003761}
3762
Calin Juravle9aec02f2014-11-18 23:06:35 +00003763void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3764 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3765
3766 LocationSummary* locations =
3767 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3768
3769 switch (op->GetResultType()) {
3770 case Primitive::kPrimInt:
3771 case Primitive::kPrimLong: {
3772 locations->SetInAt(0, Location::RequiresRegister());
3773 // The shift count needs to be in CL.
3774 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3775 locations->SetOut(Location::SameAsFirstInput());
3776 break;
3777 }
3778 default:
3779 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3780 }
3781}
3782
3783void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3784 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3785
3786 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003787 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003788 Location second = locations->InAt(1);
3789
3790 switch (op->GetResultType()) {
3791 case Primitive::kPrimInt: {
3792 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003793 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003794 if (op->IsShl()) {
3795 __ shll(first_reg, second_reg);
3796 } else if (op->IsShr()) {
3797 __ sarl(first_reg, second_reg);
3798 } else {
3799 __ shrl(first_reg, second_reg);
3800 }
3801 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003802 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003803 if (op->IsShl()) {
3804 __ shll(first_reg, imm);
3805 } else if (op->IsShr()) {
3806 __ sarl(first_reg, imm);
3807 } else {
3808 __ shrl(first_reg, imm);
3809 }
3810 }
3811 break;
3812 }
3813 case Primitive::kPrimLong: {
3814 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003815 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003816 if (op->IsShl()) {
3817 __ shlq(first_reg, second_reg);
3818 } else if (op->IsShr()) {
3819 __ sarq(first_reg, second_reg);
3820 } else {
3821 __ shrq(first_reg, second_reg);
3822 }
3823 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003824 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003825 if (op->IsShl()) {
3826 __ shlq(first_reg, imm);
3827 } else if (op->IsShr()) {
3828 __ sarq(first_reg, imm);
3829 } else {
3830 __ shrq(first_reg, imm);
3831 }
3832 }
3833 break;
3834 }
3835 default:
3836 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003837 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003838 }
3839}
3840
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003841void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3842 LocationSummary* locations =
3843 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3844
3845 switch (ror->GetResultType()) {
3846 case Primitive::kPrimInt:
3847 case Primitive::kPrimLong: {
3848 locations->SetInAt(0, Location::RequiresRegister());
3849 // The shift count needs to be in CL (unless it is a constant).
3850 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3851 locations->SetOut(Location::SameAsFirstInput());
3852 break;
3853 }
3854 default:
3855 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3856 UNREACHABLE();
3857 }
3858}
3859
3860void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3861 LocationSummary* locations = ror->GetLocations();
3862 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3863 Location second = locations->InAt(1);
3864
3865 switch (ror->GetResultType()) {
3866 case Primitive::kPrimInt:
3867 if (second.IsRegister()) {
3868 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3869 __ rorl(first_reg, second_reg);
3870 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003871 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003872 __ rorl(first_reg, imm);
3873 }
3874 break;
3875 case Primitive::kPrimLong:
3876 if (second.IsRegister()) {
3877 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3878 __ rorq(first_reg, second_reg);
3879 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003880 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003881 __ rorq(first_reg, imm);
3882 }
3883 break;
3884 default:
3885 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3886 UNREACHABLE();
3887 }
3888}
3889
Calin Juravle9aec02f2014-11-18 23:06:35 +00003890void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3891 HandleShift(shl);
3892}
3893
3894void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3895 HandleShift(shl);
3896}
3897
3898void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3899 HandleShift(shr);
3900}
3901
3902void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3903 HandleShift(shr);
3904}
3905
3906void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3907 HandleShift(ushr);
3908}
3909
3910void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3911 HandleShift(ushr);
3912}
3913
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003914void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003915 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003917 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003918 if (instruction->IsStringAlloc()) {
3919 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3920 } else {
3921 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3922 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3923 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003924 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003925}
3926
3927void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003928 // Note: if heap poisoning is enabled, the entry point takes cares
3929 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003930 if (instruction->IsStringAlloc()) {
3931 // String is allocated through StringFactory. Call NewEmptyString entry point.
3932 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3933 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3934 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3935 __ call(Address(temp, code_offset.SizeValue()));
3936 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3937 } else {
3938 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3939 instruction,
3940 instruction->GetDexPc(),
3941 nullptr);
3942 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3943 DCHECK(!codegen_->IsLeafMethod());
3944 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003945}
3946
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003947void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3948 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003949 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003950 InvokeRuntimeCallingConvention calling_convention;
3951 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003952 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003953 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003954 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003955}
3956
3957void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3958 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003959 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3960 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003961 // Note: if heap poisoning is enabled, the entry point takes cares
3962 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003963 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3964 instruction,
3965 instruction->GetDexPc(),
3966 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003967 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003968
3969 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003970}
3971
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003972void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003973 LocationSummary* locations =
3974 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003975 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3976 if (location.IsStackSlot()) {
3977 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3978 } else if (location.IsDoubleStackSlot()) {
3979 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3980 }
3981 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003982}
3983
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003984void InstructionCodeGeneratorX86_64::VisitParameterValue(
3985 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003986 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003987}
3988
3989void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3990 LocationSummary* locations =
3991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3992 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3993}
3994
3995void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3996 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3997 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003998}
3999
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004000void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4001 LocationSummary* locations =
4002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4003 locations->SetInAt(0, Location::RequiresRegister());
4004 locations->SetOut(Location::RequiresRegister());
4005}
4006
4007void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4008 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004010 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004011 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004012 __ movq(locations->Out().AsRegister<CpuRegister>(),
4013 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004014 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004015 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
4016 instruction->GetIndex() % ImTable::kSize, kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004017 __ movq(locations->Out().AsRegister<CpuRegister>(),
4018 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4019 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004020 __ movq(locations->Out().AsRegister<CpuRegister>(),
4021 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004022 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004023}
4024
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004025void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004026 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004027 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004028 locations->SetInAt(0, Location::RequiresRegister());
4029 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004030}
4031
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004032void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4033 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004034 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4035 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004036 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004037 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004038 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004039 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004040 break;
4041
4042 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004044 break;
4045
4046 default:
4047 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4048 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004049}
4050
David Brazdil66d126e2015-04-03 16:02:44 +01004051void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4052 LocationSummary* locations =
4053 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4054 locations->SetInAt(0, Location::RequiresRegister());
4055 locations->SetOut(Location::SameAsFirstInput());
4056}
4057
4058void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004059 LocationSummary* locations = bool_not->GetLocations();
4060 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4061 locations->Out().AsRegister<CpuRegister>().AsRegister());
4062 Location out = locations->Out();
4063 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4064}
4065
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004066void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004067 LocationSummary* locations =
4068 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004069 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004070 locations->SetInAt(i, Location::Any());
4071 }
4072 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004073}
4074
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004075void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004076 LOG(FATAL) << "Unimplemented";
4077}
4078
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004079void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004080 /*
4081 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004082 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004083 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4084 */
4085 switch (kind) {
4086 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004087 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004088 break;
4089 }
4090 case MemBarrierKind::kAnyStore:
4091 case MemBarrierKind::kLoadAny:
4092 case MemBarrierKind::kStoreStore: {
4093 // nop
4094 break;
4095 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004096 case MemBarrierKind::kNTStoreStore:
4097 // Non-Temporal Store/Store needs an explicit fence.
4098 MemoryFence(/* non-temporal */ true);
4099 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004100 }
4101}
4102
4103void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4104 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4105
Roland Levillain0d5a2812015-11-13 10:07:31 +00004106 bool object_field_get_with_read_barrier =
4107 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004108 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004109 new (GetGraph()->GetArena()) LocationSummary(instruction,
4110 object_field_get_with_read_barrier ?
4111 LocationSummary::kCallOnSlowPath :
4112 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004113 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004114 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4115 locations->SetOut(Location::RequiresFpuRegister());
4116 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004117 // The output overlaps for an object field get when read barriers
4118 // are enabled: we do not want the move to overwrite the object's
4119 // location, as we need it to emit the read barrier.
4120 locations->SetOut(
4121 Location::RequiresRegister(),
4122 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004123 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004124 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4125 // We need a temporary register for the read barrier marking slow
4126 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4127 locations->AddTemp(Location::RequiresRegister());
4128 }
Calin Juravle52c48962014-12-16 17:02:57 +00004129}
4130
4131void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4132 const FieldInfo& field_info) {
4133 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4134
4135 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004136 Location base_loc = locations->InAt(0);
4137 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004138 Location out = locations->Out();
4139 bool is_volatile = field_info.IsVolatile();
4140 Primitive::Type field_type = field_info.GetFieldType();
4141 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4142
4143 switch (field_type) {
4144 case Primitive::kPrimBoolean: {
4145 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4146 break;
4147 }
4148
4149 case Primitive::kPrimByte: {
4150 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4151 break;
4152 }
4153
4154 case Primitive::kPrimShort: {
4155 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4156 break;
4157 }
4158
4159 case Primitive::kPrimChar: {
4160 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4161 break;
4162 }
4163
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004164 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004165 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4166 break;
4167 }
4168
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004169 case Primitive::kPrimNot: {
4170 // /* HeapReference<Object> */ out = *(base + offset)
4171 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4172 Location temp_loc = locations->GetTemp(0);
4173 // Note that a potential implicit null check is handled in this
4174 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4175 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4176 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4177 if (is_volatile) {
4178 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4179 }
4180 } else {
4181 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4182 codegen_->MaybeRecordImplicitNullCheck(instruction);
4183 if (is_volatile) {
4184 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4185 }
4186 // If read barriers are enabled, emit read barriers other than
4187 // Baker's using a slow path (and also unpoison the loaded
4188 // reference, if heap poisoning is enabled).
4189 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4190 }
4191 break;
4192 }
4193
Calin Juravle52c48962014-12-16 17:02:57 +00004194 case Primitive::kPrimLong: {
4195 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4196 break;
4197 }
4198
4199 case Primitive::kPrimFloat: {
4200 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4201 break;
4202 }
4203
4204 case Primitive::kPrimDouble: {
4205 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4206 break;
4207 }
4208
4209 case Primitive::kPrimVoid:
4210 LOG(FATAL) << "Unreachable type " << field_type;
4211 UNREACHABLE();
4212 }
4213
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004214 if (field_type == Primitive::kPrimNot) {
4215 // Potential implicit null checks, in the case of reference
4216 // fields, are handled in the previous switch statement.
4217 } else {
4218 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004219 }
Roland Levillain4d027112015-07-01 15:41:14 +01004220
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004221 if (is_volatile) {
4222 if (field_type == Primitive::kPrimNot) {
4223 // Memory barriers, in the case of references, are also handled
4224 // in the previous switch statement.
4225 } else {
4226 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4227 }
Roland Levillain4d027112015-07-01 15:41:14 +01004228 }
Calin Juravle52c48962014-12-16 17:02:57 +00004229}
4230
4231void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4232 const FieldInfo& field_info) {
4233 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4234
4235 LocationSummary* locations =
4236 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004237 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004238 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004239 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004240 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004241
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004242 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004243 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004244 if (is_volatile) {
4245 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4246 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4247 } else {
4248 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4249 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004250 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004251 if (is_volatile) {
4252 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4253 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4254 } else {
4255 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4256 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004257 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004258 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004259 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004260 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004261 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004262 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4263 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004264 locations->AddTemp(Location::RequiresRegister());
4265 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004266}
4267
Calin Juravle52c48962014-12-16 17:02:57 +00004268void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004269 const FieldInfo& field_info,
4270 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004271 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4272
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004273 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004274 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4275 Location value = locations->InAt(1);
4276 bool is_volatile = field_info.IsVolatile();
4277 Primitive::Type field_type = field_info.GetFieldType();
4278 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4279
4280 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004281 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004282 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004283
Mark Mendellea5af682015-10-22 17:35:49 -04004284 bool maybe_record_implicit_null_check_done = false;
4285
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004286 switch (field_type) {
4287 case Primitive::kPrimBoolean:
4288 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004289 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004290 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004291 __ movb(Address(base, offset), Immediate(v));
4292 } else {
4293 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4294 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004295 break;
4296 }
4297
4298 case Primitive::kPrimShort:
4299 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004300 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004301 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004302 __ movw(Address(base, offset), Immediate(v));
4303 } else {
4304 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4305 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306 break;
4307 }
4308
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004309 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004310 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004311 if (value.IsConstant()) {
4312 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004313 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4314 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4315 // Note: if heap poisoning is enabled, no need to poison
4316 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004317 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004318 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004319 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4320 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4321 __ movl(temp, value.AsRegister<CpuRegister>());
4322 __ PoisonHeapReference(temp);
4323 __ movl(Address(base, offset), temp);
4324 } else {
4325 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4326 }
Mark Mendell40741f32015-04-20 22:10:34 -04004327 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004328 break;
4329 }
4330
4331 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004332 if (value.IsConstant()) {
4333 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004334 codegen_->MoveInt64ToAddress(Address(base, offset),
4335 Address(base, offset + sizeof(int32_t)),
4336 v,
4337 instruction);
4338 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004339 } else {
4340 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4341 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004342 break;
4343 }
4344
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004345 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004346 if (value.IsConstant()) {
4347 int32_t v =
4348 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4349 __ movl(Address(base, offset), Immediate(v));
4350 } else {
4351 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4352 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004353 break;
4354 }
4355
4356 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004357 if (value.IsConstant()) {
4358 int64_t v =
4359 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4360 codegen_->MoveInt64ToAddress(Address(base, offset),
4361 Address(base, offset + sizeof(int32_t)),
4362 v,
4363 instruction);
4364 maybe_record_implicit_null_check_done = true;
4365 } else {
4366 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4367 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004368 break;
4369 }
4370
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371 case Primitive::kPrimVoid:
4372 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004373 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004374 }
Calin Juravle52c48962014-12-16 17:02:57 +00004375
Mark Mendellea5af682015-10-22 17:35:49 -04004376 if (!maybe_record_implicit_null_check_done) {
4377 codegen_->MaybeRecordImplicitNullCheck(instruction);
4378 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004379
4380 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4381 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4382 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004383 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004384 }
4385
Calin Juravle52c48962014-12-16 17:02:57 +00004386 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004387 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004388 }
4389}
4390
4391void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4392 HandleFieldSet(instruction, instruction->GetFieldInfo());
4393}
4394
4395void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004396 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004397}
4398
4399void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004400 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004401}
4402
4403void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004404 HandleFieldGet(instruction, instruction->GetFieldInfo());
4405}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004406
Calin Juravle52c48962014-12-16 17:02:57 +00004407void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4408 HandleFieldGet(instruction);
4409}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004410
Calin Juravle52c48962014-12-16 17:02:57 +00004411void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4412 HandleFieldGet(instruction, instruction->GetFieldInfo());
4413}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004414
Calin Juravle52c48962014-12-16 17:02:57 +00004415void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4416 HandleFieldSet(instruction, instruction->GetFieldInfo());
4417}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004418
Calin Juravle52c48962014-12-16 17:02:57 +00004419void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004420 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004421}
4422
Calin Juravlee460d1d2015-09-29 04:52:17 +01004423void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4424 HUnresolvedInstanceFieldGet* instruction) {
4425 FieldAccessCallingConventionX86_64 calling_convention;
4426 codegen_->CreateUnresolvedFieldLocationSummary(
4427 instruction, instruction->GetFieldType(), calling_convention);
4428}
4429
4430void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4431 HUnresolvedInstanceFieldGet* instruction) {
4432 FieldAccessCallingConventionX86_64 calling_convention;
4433 codegen_->GenerateUnresolvedFieldAccess(instruction,
4434 instruction->GetFieldType(),
4435 instruction->GetFieldIndex(),
4436 instruction->GetDexPc(),
4437 calling_convention);
4438}
4439
4440void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4441 HUnresolvedInstanceFieldSet* instruction) {
4442 FieldAccessCallingConventionX86_64 calling_convention;
4443 codegen_->CreateUnresolvedFieldLocationSummary(
4444 instruction, instruction->GetFieldType(), calling_convention);
4445}
4446
4447void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4448 HUnresolvedInstanceFieldSet* instruction) {
4449 FieldAccessCallingConventionX86_64 calling_convention;
4450 codegen_->GenerateUnresolvedFieldAccess(instruction,
4451 instruction->GetFieldType(),
4452 instruction->GetFieldIndex(),
4453 instruction->GetDexPc(),
4454 calling_convention);
4455}
4456
4457void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4458 HUnresolvedStaticFieldGet* instruction) {
4459 FieldAccessCallingConventionX86_64 calling_convention;
4460 codegen_->CreateUnresolvedFieldLocationSummary(
4461 instruction, instruction->GetFieldType(), calling_convention);
4462}
4463
4464void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4465 HUnresolvedStaticFieldGet* instruction) {
4466 FieldAccessCallingConventionX86_64 calling_convention;
4467 codegen_->GenerateUnresolvedFieldAccess(instruction,
4468 instruction->GetFieldType(),
4469 instruction->GetFieldIndex(),
4470 instruction->GetDexPc(),
4471 calling_convention);
4472}
4473
4474void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4475 HUnresolvedStaticFieldSet* instruction) {
4476 FieldAccessCallingConventionX86_64 calling_convention;
4477 codegen_->CreateUnresolvedFieldLocationSummary(
4478 instruction, instruction->GetFieldType(), calling_convention);
4479}
4480
4481void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4482 HUnresolvedStaticFieldSet* instruction) {
4483 FieldAccessCallingConventionX86_64 calling_convention;
4484 codegen_->GenerateUnresolvedFieldAccess(instruction,
4485 instruction->GetFieldType(),
4486 instruction->GetFieldIndex(),
4487 instruction->GetDexPc(),
4488 calling_convention);
4489}
4490
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004491void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004492 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4493 ? LocationSummary::kCallOnSlowPath
4494 : LocationSummary::kNoCall;
4495 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4496 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004497 ? Location::RequiresRegister()
4498 : Location::Any();
4499 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004500 if (instruction->HasUses()) {
4501 locations->SetOut(Location::SameAsFirstInput());
4502 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004503}
4504
Calin Juravle2ae48182016-03-16 14:05:09 +00004505void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4506 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004507 return;
4508 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004509 LocationSummary* locations = instruction->GetLocations();
4510 Location obj = locations->InAt(0);
4511
4512 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004513 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004514}
4515
Calin Juravle2ae48182016-03-16 14:05:09 +00004516void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004517 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004518 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004519
4520 LocationSummary* locations = instruction->GetLocations();
4521 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004522
4523 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004524 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004525 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004526 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004527 } else {
4528 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004529 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004530 __ jmp(slow_path->GetEntryLabel());
4531 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004532 }
4533 __ j(kEqual, slow_path->GetEntryLabel());
4534}
4535
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004536void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004537 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004538}
4539
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004541 bool object_array_get_with_read_barrier =
4542 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004543 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004544 new (GetGraph()->GetArena()) LocationSummary(instruction,
4545 object_array_get_with_read_barrier ?
4546 LocationSummary::kCallOnSlowPath :
4547 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004548 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004549 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004550 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4551 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4552 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004553 // The output overlaps for an object array get when read barriers
4554 // are enabled: we do not want the move to overwrite the array's
4555 // location, as we need it to emit the read barrier.
4556 locations->SetOut(
4557 Location::RequiresRegister(),
4558 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004559 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004560 // We need a temporary register for the read barrier marking slow
4561 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4562 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4563 locations->AddTemp(Location::RequiresRegister());
4564 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004565}
4566
4567void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4568 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004569 Location obj_loc = locations->InAt(0);
4570 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004571 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004572 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004573 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004574
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004575 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004576 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004578 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004579 if (index.IsConstant()) {
4580 __ movzxb(out, Address(obj,
4581 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4582 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004583 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004584 }
4585 break;
4586 }
4587
4588 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004589 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590 if (index.IsConstant()) {
4591 __ movsxb(out, Address(obj,
4592 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4593 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004594 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004595 }
4596 break;
4597 }
4598
4599 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004600 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601 if (index.IsConstant()) {
4602 __ movsxw(out, Address(obj,
4603 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4604 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004605 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004606 }
4607 break;
4608 }
4609
4610 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004611 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004612 if (index.IsConstant()) {
4613 __ movzxw(out, Address(obj,
4614 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4615 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004616 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004617 }
4618 break;
4619 }
4620
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004621 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004622 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004623 if (index.IsConstant()) {
4624 __ movl(out, Address(obj,
4625 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4626 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004627 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004628 }
4629 break;
4630 }
4631
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004632 case Primitive::kPrimNot: {
4633 static_assert(
4634 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4635 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004636 // /* HeapReference<Object> */ out =
4637 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4638 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4639 Location temp = locations->GetTemp(0);
4640 // Note that a potential implicit null check is handled in this
4641 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4642 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4643 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4644 } else {
4645 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4646 if (index.IsConstant()) {
4647 uint32_t offset =
4648 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4649 __ movl(out, Address(obj, offset));
4650 codegen_->MaybeRecordImplicitNullCheck(instruction);
4651 // If read barriers are enabled, emit read barriers other than
4652 // Baker's using a slow path (and also unpoison the loaded
4653 // reference, if heap poisoning is enabled).
4654 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4655 } else {
4656 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4657 codegen_->MaybeRecordImplicitNullCheck(instruction);
4658 // If read barriers are enabled, emit read barriers other than
4659 // Baker's using a slow path (and also unpoison the loaded
4660 // reference, if heap poisoning is enabled).
4661 codegen_->MaybeGenerateReadBarrierSlow(
4662 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4663 }
4664 }
4665 break;
4666 }
4667
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004668 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004669 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004670 if (index.IsConstant()) {
4671 __ movq(out, Address(obj,
4672 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4673 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004674 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004675 }
4676 break;
4677 }
4678
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004679 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004680 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004681 if (index.IsConstant()) {
4682 __ movss(out, Address(obj,
4683 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4684 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004685 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004686 }
4687 break;
4688 }
4689
4690 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004691 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004692 if (index.IsConstant()) {
4693 __ movsd(out, Address(obj,
4694 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4695 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004696 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004697 }
4698 break;
4699 }
4700
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004701 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004702 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004703 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704 }
Roland Levillain4d027112015-07-01 15:41:14 +01004705
4706 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004707 // Potential implicit null checks, in the case of reference
4708 // arrays, are handled in the previous switch statement.
4709 } else {
4710 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004711 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004712}
4713
4714void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004715 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004716
4717 bool needs_write_barrier =
4718 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004719 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004720 bool object_array_set_with_read_barrier =
4721 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004722
Nicolas Geoffray39468442014-09-02 15:17:15 +01004723 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004724 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004725 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004726 LocationSummary::kCallOnSlowPath :
4727 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004728
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004729 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004730 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4731 if (Primitive::IsFloatingPointType(value_type)) {
4732 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004733 } else {
4734 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4735 }
4736
4737 if (needs_write_barrier) {
4738 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004739
4740 // This first temporary register is possibly used for heap
4741 // reference poisoning and/or read barrier emission too.
4742 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004743 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004744 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004745}
4746
4747void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4748 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004749 Location array_loc = locations->InAt(0);
4750 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004751 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004752 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004753 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004754 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004755 bool needs_write_barrier =
4756 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004757 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4758 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4759 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004760
4761 switch (value_type) {
4762 case Primitive::kPrimBoolean:
4763 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004764 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4765 Address address = index.IsConstant()
4766 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4767 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4768 if (value.IsRegister()) {
4769 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004770 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004771 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004772 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004773 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004774 break;
4775 }
4776
4777 case Primitive::kPrimShort:
4778 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004779 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4780 Address address = index.IsConstant()
4781 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4782 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4783 if (value.IsRegister()) {
4784 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004785 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004786 DCHECK(value.IsConstant()) << value;
4787 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004788 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004789 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004790 break;
4791 }
4792
4793 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004794 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4795 Address address = index.IsConstant()
4796 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4797 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004798
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 if (!value.IsRegister()) {
4800 // Just setting null.
4801 DCHECK(instruction->InputAt(2)->IsNullConstant());
4802 DCHECK(value.IsConstant()) << value;
4803 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004804 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004805 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004806 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004807 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004808 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004809
4810 DCHECK(needs_write_barrier);
4811 CpuRegister register_value = value.AsRegister<CpuRegister>();
4812 NearLabel done, not_null, do_put;
4813 SlowPathCode* slow_path = nullptr;
4814 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004815 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004816 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4817 codegen_->AddSlowPath(slow_path);
4818 if (instruction->GetValueCanBeNull()) {
4819 __ testl(register_value, register_value);
4820 __ j(kNotEqual, &not_null);
4821 __ movl(address, Immediate(0));
4822 codegen_->MaybeRecordImplicitNullCheck(instruction);
4823 __ jmp(&done);
4824 __ Bind(&not_null);
4825 }
4826
Roland Levillain0d5a2812015-11-13 10:07:31 +00004827 if (kEmitCompilerReadBarrier) {
4828 // When read barriers are enabled, the type checking
4829 // instrumentation requires two read barriers:
4830 //
4831 // __ movl(temp2, temp);
4832 // // /* HeapReference<Class> */ temp = temp->component_type_
4833 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004834 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004835 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4836 //
4837 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4838 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004839 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004840 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4841 //
4842 // __ cmpl(temp, temp2);
4843 //
4844 // However, the second read barrier may trash `temp`, as it
4845 // is a temporary register, and as such would not be saved
4846 // along with live registers before calling the runtime (nor
4847 // restored afterwards). So in this case, we bail out and
4848 // delegate the work to the array set slow path.
4849 //
4850 // TODO: Extend the register allocator to support a new
4851 // "(locally) live temp" location so as to avoid always
4852 // going into the slow path when read barriers are enabled.
4853 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004854 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004855 // /* HeapReference<Class> */ temp = array->klass_
4856 __ movl(temp, Address(array, class_offset));
4857 codegen_->MaybeRecordImplicitNullCheck(instruction);
4858 __ MaybeUnpoisonHeapReference(temp);
4859
4860 // /* HeapReference<Class> */ temp = temp->component_type_
4861 __ movl(temp, Address(temp, component_offset));
4862 // If heap poisoning is enabled, no need to unpoison `temp`
4863 // nor the object reference in `register_value->klass`, as
4864 // we are comparing two poisoned references.
4865 __ cmpl(temp, Address(register_value, class_offset));
4866
4867 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4868 __ j(kEqual, &do_put);
4869 // If heap poisoning is enabled, the `temp` reference has
4870 // not been unpoisoned yet; unpoison it now.
4871 __ MaybeUnpoisonHeapReference(temp);
4872
4873 // /* HeapReference<Class> */ temp = temp->super_class_
4874 __ movl(temp, Address(temp, super_offset));
4875 // If heap poisoning is enabled, no need to unpoison
4876 // `temp`, as we are comparing against null below.
4877 __ testl(temp, temp);
4878 __ j(kNotEqual, slow_path->GetEntryLabel());
4879 __ Bind(&do_put);
4880 } else {
4881 __ j(kNotEqual, slow_path->GetEntryLabel());
4882 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004883 }
4884 }
4885
4886 if (kPoisonHeapReferences) {
4887 __ movl(temp, register_value);
4888 __ PoisonHeapReference(temp);
4889 __ movl(address, temp);
4890 } else {
4891 __ movl(address, register_value);
4892 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004893 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004894 codegen_->MaybeRecordImplicitNullCheck(instruction);
4895 }
4896
4897 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4898 codegen_->MarkGCCard(
4899 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4900 __ Bind(&done);
4901
4902 if (slow_path != nullptr) {
4903 __ Bind(slow_path->GetExitLabel());
4904 }
4905
4906 break;
4907 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004908
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004909 case Primitive::kPrimInt: {
4910 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4911 Address address = index.IsConstant()
4912 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4913 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4914 if (value.IsRegister()) {
4915 __ movl(address, value.AsRegister<CpuRegister>());
4916 } else {
4917 DCHECK(value.IsConstant()) << value;
4918 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4919 __ movl(address, Immediate(v));
4920 }
4921 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004922 break;
4923 }
4924
4925 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004926 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4927 Address address = index.IsConstant()
4928 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4929 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4930 if (value.IsRegister()) {
4931 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004932 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004933 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004934 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004935 Address address_high = index.IsConstant()
4936 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4937 offset + sizeof(int32_t))
4938 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4939 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004940 }
4941 break;
4942 }
4943
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004944 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004945 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4946 Address address = index.IsConstant()
4947 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4948 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004949 if (value.IsFpuRegister()) {
4950 __ movss(address, value.AsFpuRegister<XmmRegister>());
4951 } else {
4952 DCHECK(value.IsConstant());
4953 int32_t v =
4954 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4955 __ movl(address, Immediate(v));
4956 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004957 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004958 break;
4959 }
4960
4961 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004962 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4963 Address address = index.IsConstant()
4964 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4965 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004966 if (value.IsFpuRegister()) {
4967 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4968 codegen_->MaybeRecordImplicitNullCheck(instruction);
4969 } else {
4970 int64_t v =
4971 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4972 Address address_high = index.IsConstant()
4973 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4974 offset + sizeof(int32_t))
4975 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4976 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4977 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004978 break;
4979 }
4980
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004981 case Primitive::kPrimVoid:
4982 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004983 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004984 }
4985}
4986
4987void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004988 LocationSummary* locations =
4989 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004990 locations->SetInAt(0, Location::RequiresRegister());
4991 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004992}
4993
4994void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4995 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004996 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004997 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4998 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004999 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005000 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001}
5002
5003void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005004 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5005 ? LocationSummary::kCallOnSlowPath
5006 : LocationSummary::kNoCall;
5007 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005008 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005009 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005010 if (instruction->HasUses()) {
5011 locations->SetOut(Location::SameAsFirstInput());
5012 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005013}
5014
5015void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5016 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005017 Location index_loc = locations->InAt(0);
5018 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005019 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005020 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005021
Mark Mendell99dbd682015-04-22 16:18:52 -04005022 if (length_loc.IsConstant()) {
5023 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5024 if (index_loc.IsConstant()) {
5025 // BCE will remove the bounds check if we are guarenteed to pass.
5026 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5027 if (index < 0 || index >= length) {
5028 codegen_->AddSlowPath(slow_path);
5029 __ jmp(slow_path->GetEntryLabel());
5030 } else {
5031 // Some optimization after BCE may have generated this, and we should not
5032 // generate a bounds check if it is a valid range.
5033 }
5034 return;
5035 }
5036
5037 // We have to reverse the jump condition because the length is the constant.
5038 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5039 __ cmpl(index_reg, Immediate(length));
5040 codegen_->AddSlowPath(slow_path);
5041 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005042 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005043 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5044 if (index_loc.IsConstant()) {
5045 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5046 __ cmpl(length, Immediate(value));
5047 } else {
5048 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5049 }
5050 codegen_->AddSlowPath(slow_path);
5051 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005052 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005053}
5054
5055void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5056 CpuRegister card,
5057 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005058 CpuRegister value,
5059 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005060 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005061 if (value_can_be_null) {
5062 __ testl(value, value);
5063 __ j(kEqual, &is_null);
5064 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005065 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5066 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005067 __ movq(temp, object);
5068 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005069 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005070 if (value_can_be_null) {
5071 __ Bind(&is_null);
5072 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005073}
5074
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005075void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005076 LOG(FATAL) << "Unimplemented";
5077}
5078
5079void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005080 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5081}
5082
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005083void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5084 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5085}
5086
5087void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005088 HBasicBlock* block = instruction->GetBlock();
5089 if (block->GetLoopInformation() != nullptr) {
5090 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5091 // The back edge will generate the suspend check.
5092 return;
5093 }
5094 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5095 // The goto will generate the suspend check.
5096 return;
5097 }
5098 GenerateSuspendCheck(instruction, nullptr);
5099}
5100
5101void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5102 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005103 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005104 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5105 if (slow_path == nullptr) {
5106 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5107 instruction->SetSlowPath(slow_path);
5108 codegen_->AddSlowPath(slow_path);
5109 if (successor != nullptr) {
5110 DCHECK(successor->IsLoopHeader());
5111 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5112 }
5113 } else {
5114 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5115 }
5116
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005117 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5118 /* no_rip */ true),
5119 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005120 if (successor == nullptr) {
5121 __ j(kNotEqual, slow_path->GetEntryLabel());
5122 __ Bind(slow_path->GetReturnLabel());
5123 } else {
5124 __ j(kEqual, codegen_->GetLabelOf(successor));
5125 __ jmp(slow_path->GetEntryLabel());
5126 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005127}
5128
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005129X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5130 return codegen_->GetAssembler();
5131}
5132
5133void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005134 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005135 Location source = move->GetSource();
5136 Location destination = move->GetDestination();
5137
5138 if (source.IsRegister()) {
5139 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005140 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005141 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005142 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005143 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005144 } else {
5145 DCHECK(destination.IsDoubleStackSlot());
5146 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005147 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005148 }
5149 } else if (source.IsStackSlot()) {
5150 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005151 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005152 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005153 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005154 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005155 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005156 } else {
5157 DCHECK(destination.IsStackSlot());
5158 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5159 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5160 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005161 } else if (source.IsDoubleStackSlot()) {
5162 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005163 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005164 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005165 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005166 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5167 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005168 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005169 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005170 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5171 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5172 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005173 } else if (source.IsConstant()) {
5174 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005175 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5176 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005177 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005178 if (value == 0) {
5179 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5180 } else {
5181 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5182 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005183 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005184 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005185 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005186 }
5187 } else if (constant->IsLongConstant()) {
5188 int64_t value = constant->AsLongConstant()->GetValue();
5189 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005190 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005191 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005192 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005193 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005194 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005195 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005196 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005197 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005198 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005199 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005200 } else {
5201 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005202 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005203 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5204 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005205 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005206 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005207 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005208 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005209 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005210 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005211 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005212 } else {
5213 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005214 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005215 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005216 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005217 } else if (source.IsFpuRegister()) {
5218 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005219 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005220 } else if (destination.IsStackSlot()) {
5221 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005222 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005223 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005224 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005225 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005226 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005227 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005228 }
5229}
5230
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005231void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005232 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005233 __ movl(Address(CpuRegister(RSP), mem), reg);
5234 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005235}
5236
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005237void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005238 ScratchRegisterScope ensure_scratch(
5239 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5240
5241 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5242 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5243 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5244 Address(CpuRegister(RSP), mem2 + stack_offset));
5245 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5246 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5247 CpuRegister(ensure_scratch.GetRegister()));
5248}
5249
Mark Mendell8a1c7282015-06-29 15:41:28 -04005250void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5251 __ movq(CpuRegister(TMP), reg1);
5252 __ movq(reg1, reg2);
5253 __ movq(reg2, CpuRegister(TMP));
5254}
5255
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005256void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5257 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5258 __ movq(Address(CpuRegister(RSP), mem), reg);
5259 __ movq(reg, CpuRegister(TMP));
5260}
5261
5262void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5263 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005264 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005265
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005266 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5267 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5268 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5269 Address(CpuRegister(RSP), mem2 + stack_offset));
5270 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5271 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5272 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005273}
5274
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005275void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5276 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5277 __ movss(Address(CpuRegister(RSP), mem), reg);
5278 __ movd(reg, CpuRegister(TMP));
5279}
5280
5281void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5282 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5283 __ movsd(Address(CpuRegister(RSP), mem), reg);
5284 __ movd(reg, CpuRegister(TMP));
5285}
5286
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005287void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005288 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005289 Location source = move->GetSource();
5290 Location destination = move->GetDestination();
5291
5292 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005293 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005294 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005295 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005296 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005297 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005298 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005299 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5300 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005301 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005302 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005303 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005304 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5305 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005306 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005307 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5308 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5309 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005310 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005311 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005312 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005313 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005314 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005315 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005316 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005317 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005318 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005319 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005320 }
5321}
5322
5323
5324void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5325 __ pushq(CpuRegister(reg));
5326}
5327
5328
5329void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5330 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005331}
5332
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005333void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005334 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005335 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5336 Immediate(mirror::Class::kStatusInitialized));
5337 __ j(kLess, slow_path->GetEntryLabel());
5338 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005339 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005340}
5341
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005342HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5343 HLoadClass::LoadKind desired_class_load_kind) {
5344 if (kEmitCompilerReadBarrier) {
5345 switch (desired_class_load_kind) {
5346 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5347 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5348 case HLoadClass::LoadKind::kBootImageAddress:
5349 // TODO: Implement for read barrier.
5350 return HLoadClass::LoadKind::kDexCacheViaMethod;
5351 default:
5352 break;
5353 }
5354 }
5355 switch (desired_class_load_kind) {
5356 case HLoadClass::LoadKind::kReferrersClass:
5357 break;
5358 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5359 DCHECK(!GetCompilerOptions().GetCompilePic());
5360 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5361 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5362 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5363 DCHECK(GetCompilerOptions().GetCompilePic());
5364 break;
5365 case HLoadClass::LoadKind::kBootImageAddress:
5366 break;
5367 case HLoadClass::LoadKind::kDexCacheAddress:
5368 DCHECK(Runtime::Current()->UseJitCompilation());
5369 break;
5370 case HLoadClass::LoadKind::kDexCachePcRelative:
5371 DCHECK(!Runtime::Current()->UseJitCompilation());
5372 break;
5373 case HLoadClass::LoadKind::kDexCacheViaMethod:
5374 break;
5375 }
5376 return desired_class_load_kind;
5377}
5378
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005379void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005380 if (cls->NeedsAccessCheck()) {
5381 InvokeRuntimeCallingConvention calling_convention;
5382 CodeGenerator::CreateLoadClassLocationSummary(
5383 cls,
5384 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5385 Location::RegisterLocation(RAX),
5386 /* code_generator_supports_read_barrier */ true);
5387 return;
5388 }
5389
5390 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5391 ? LocationSummary::kCallOnSlowPath
5392 : LocationSummary::kNoCall;
5393 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5394 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5395 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5396 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5397 locations->SetInAt(0, Location::RequiresRegister());
5398 }
5399 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005400}
5401
5402void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005403 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005404 if (cls->NeedsAccessCheck()) {
5405 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5406 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5407 cls,
5408 cls->GetDexPc(),
5409 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005410 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005411 return;
5412 }
5413
Roland Levillain0d5a2812015-11-13 10:07:31 +00005414 Location out_loc = locations->Out();
5415 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005416
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005417 bool generate_null_check = false;
5418 switch (cls->GetLoadKind()) {
5419 case HLoadClass::LoadKind::kReferrersClass: {
5420 DCHECK(!cls->CanCallRuntime());
5421 DCHECK(!cls->MustGenerateClinitCheck());
5422 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5423 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5424 GenerateGcRootFieldLoad(
5425 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5426 break;
5427 }
5428 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5429 DCHECK(!kEmitCompilerReadBarrier);
5430 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5431 codegen_->RecordTypePatch(cls);
5432 break;
5433 case HLoadClass::LoadKind::kBootImageAddress: {
5434 DCHECK(!kEmitCompilerReadBarrier);
5435 DCHECK_NE(cls->GetAddress(), 0u);
5436 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5437 __ movl(out, Immediate(address)); // Zero-extended.
5438 codegen_->RecordSimplePatch();
5439 break;
5440 }
5441 case HLoadClass::LoadKind::kDexCacheAddress: {
5442 DCHECK_NE(cls->GetAddress(), 0u);
5443 // /* GcRoot<mirror::Class> */ out = *address
5444 if (IsUint<32>(cls->GetAddress())) {
5445 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
5446 GenerateGcRootFieldLoad(cls, out_loc, address);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005447 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005448 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5449 __ movq(out, Immediate(cls->GetAddress()));
5450 GenerateGcRootFieldLoad(cls, out_loc, Address(out, 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005451 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005452 generate_null_check = !cls->IsInDexCache();
5453 break;
5454 }
5455 case HLoadClass::LoadKind::kDexCachePcRelative: {
5456 uint32_t offset = cls->GetDexCacheElementOffset();
5457 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5458 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5459 /* no_rip */ false);
5460 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5461 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label);
5462 generate_null_check = !cls->IsInDexCache();
5463 break;
5464 }
5465 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5466 // /* GcRoot<mirror::Class>[] */ out =
5467 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5468 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5469 __ movq(out,
5470 Address(current_method,
5471 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5472 // /* GcRoot<mirror::Class> */ out = out[type_index]
5473 GenerateGcRootFieldLoad(
5474 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
5475 generate_null_check = !cls->IsInDexCache();
5476 break;
5477 }
5478 default:
5479 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5480 UNREACHABLE();
5481 }
5482
5483 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5484 DCHECK(cls->CanCallRuntime());
5485 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5486 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5487 codegen_->AddSlowPath(slow_path);
5488 if (generate_null_check) {
5489 __ testl(out, out);
5490 __ j(kEqual, slow_path->GetEntryLabel());
5491 }
5492 if (cls->MustGenerateClinitCheck()) {
5493 GenerateClassInitializationCheck(slow_path, out);
5494 } else {
5495 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005496 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005497 }
5498}
5499
5500void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5501 LocationSummary* locations =
5502 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5503 locations->SetInAt(0, Location::RequiresRegister());
5504 if (check->HasUses()) {
5505 locations->SetOut(Location::SameAsFirstInput());
5506 }
5507}
5508
5509void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005510 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005511 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005512 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005513 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005514 GenerateClassInitializationCheck(slow_path,
5515 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005516}
5517
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005518HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5519 HLoadString::LoadKind desired_string_load_kind) {
5520 if (kEmitCompilerReadBarrier) {
5521 switch (desired_string_load_kind) {
5522 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5523 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5524 case HLoadString::LoadKind::kBootImageAddress:
5525 // TODO: Implement for read barrier.
5526 return HLoadString::LoadKind::kDexCacheViaMethod;
5527 default:
5528 break;
5529 }
5530 }
5531 switch (desired_string_load_kind) {
5532 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5533 DCHECK(!GetCompilerOptions().GetCompilePic());
5534 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5535 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5536 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5537 DCHECK(GetCompilerOptions().GetCompilePic());
5538 break;
5539 case HLoadString::LoadKind::kBootImageAddress:
5540 break;
5541 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005542 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005543 break;
5544 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005545 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005546 break;
5547 case HLoadString::LoadKind::kDexCacheViaMethod:
5548 break;
5549 }
5550 return desired_string_load_kind;
5551}
5552
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005553void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005554 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005555 ? LocationSummary::kCallOnSlowPath
5556 : LocationSummary::kNoCall;
5557 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005558 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5559 locations->SetInAt(0, Location::RequiresRegister());
5560 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005561 locations->SetOut(Location::RequiresRegister());
5562}
5563
5564void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005565 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005566 Location out_loc = locations->Out();
5567 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005568
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005569 switch (load->GetLoadKind()) {
5570 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5571 DCHECK(!kEmitCompilerReadBarrier);
5572 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5573 codegen_->RecordStringPatch(load);
5574 return; // No dex cache slow path.
5575 }
5576 case HLoadString::LoadKind::kBootImageAddress: {
5577 DCHECK(!kEmitCompilerReadBarrier);
5578 DCHECK_NE(load->GetAddress(), 0u);
5579 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5580 __ movl(out, Immediate(address)); // Zero-extended.
5581 codegen_->RecordSimplePatch();
5582 return; // No dex cache slow path.
5583 }
5584 case HLoadString::LoadKind::kDexCacheAddress: {
5585 DCHECK_NE(load->GetAddress(), 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005586 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005587 if (IsUint<32>(load->GetAddress())) {
5588 Address address = Address::Absolute(load->GetAddress(), /* no_rip */ true);
5589 GenerateGcRootFieldLoad(load, out_loc, address);
5590 } else {
5591 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5592 __ movq(out, Immediate(load->GetAddress()));
5593 GenerateGcRootFieldLoad(load, out_loc, Address(out, 0));
5594 }
5595 break;
5596 }
5597 case HLoadString::LoadKind::kDexCachePcRelative: {
5598 uint32_t offset = load->GetDexCacheElementOffset();
5599 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
5600 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5601 /* no_rip */ false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005602 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005603 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5604 break;
5605 }
5606 case HLoadString::LoadKind::kDexCacheViaMethod: {
5607 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5608
5609 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5610 GenerateGcRootFieldLoad(
5611 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5612 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5613 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5614 // /* GcRoot<mirror::String> */ out = out[string_index]
5615 GenerateGcRootFieldLoad(
5616 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
5617 break;
5618 }
5619 default:
5620 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5621 UNREACHABLE();
5622 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005623
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005624 if (!load->IsInDexCache()) {
5625 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5626 codegen_->AddSlowPath(slow_path);
5627 __ testl(out, out);
5628 __ j(kEqual, slow_path->GetEntryLabel());
5629 __ Bind(slow_path->GetExitLabel());
5630 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005631}
5632
David Brazdilcb1c0552015-08-04 16:22:25 +01005633static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005634 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5635 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005636}
5637
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005638void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5639 LocationSummary* locations =
5640 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5641 locations->SetOut(Location::RequiresRegister());
5642}
5643
5644void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005645 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5646}
5647
5648void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5649 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5650}
5651
5652void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5653 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005654}
5655
5656void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5657 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005658 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005659 InvokeRuntimeCallingConvention calling_convention;
5660 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5661}
5662
5663void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005664 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5665 instruction,
5666 instruction->GetDexPc(),
5667 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005668 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005669}
5670
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005671static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5672 return kEmitCompilerReadBarrier &&
5673 (kUseBakerReadBarrier ||
5674 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5675 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5676 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5677}
5678
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005679void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005680 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005681 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5682 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005683 case TypeCheckKind::kExactCheck:
5684 case TypeCheckKind::kAbstractClassCheck:
5685 case TypeCheckKind::kClassHierarchyCheck:
5686 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005687 call_kind =
5688 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005689 break;
5690 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005691 case TypeCheckKind::kUnresolvedCheck:
5692 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005693 call_kind = LocationSummary::kCallOnSlowPath;
5694 break;
5695 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005696
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005697 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005698 locations->SetInAt(0, Location::RequiresRegister());
5699 locations->SetInAt(1, Location::Any());
5700 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5701 locations->SetOut(Location::RequiresRegister());
5702 // When read barriers are enabled, we need a temporary register for
5703 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005704 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005705 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005706 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005707}
5708
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005709void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005710 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005711 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005712 Location obj_loc = locations->InAt(0);
5713 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005714 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005715 Location out_loc = locations->Out();
5716 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005717 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005718 locations->GetTemp(0) :
5719 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005720 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005721 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5722 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5723 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005724 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005725 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005726
5727 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005728 // Avoid null check if we know obj is not null.
5729 if (instruction->MustDoNullCheck()) {
5730 __ testl(obj, obj);
5731 __ j(kEqual, &zero);
5732 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005733
Roland Levillain0d5a2812015-11-13 10:07:31 +00005734 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005735 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005736
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005737 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005738 case TypeCheckKind::kExactCheck: {
5739 if (cls.IsRegister()) {
5740 __ cmpl(out, cls.AsRegister<CpuRegister>());
5741 } else {
5742 DCHECK(cls.IsStackSlot()) << cls;
5743 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5744 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005745 if (zero.IsLinked()) {
5746 // Classes must be equal for the instanceof to succeed.
5747 __ j(kNotEqual, &zero);
5748 __ movl(out, Immediate(1));
5749 __ jmp(&done);
5750 } else {
5751 __ setcc(kEqual, out);
5752 // setcc only sets the low byte.
5753 __ andl(out, Immediate(1));
5754 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005755 break;
5756 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005757
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005758 case TypeCheckKind::kAbstractClassCheck: {
5759 // If the class is abstract, we eagerly fetch the super class of the
5760 // object to avoid doing a comparison we know will fail.
5761 NearLabel loop, success;
5762 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005763 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005764 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005765 __ testl(out, out);
5766 // If `out` is null, we use it for the result, and jump to `done`.
5767 __ j(kEqual, &done);
5768 if (cls.IsRegister()) {
5769 __ cmpl(out, cls.AsRegister<CpuRegister>());
5770 } else {
5771 DCHECK(cls.IsStackSlot()) << cls;
5772 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5773 }
5774 __ j(kNotEqual, &loop);
5775 __ movl(out, Immediate(1));
5776 if (zero.IsLinked()) {
5777 __ jmp(&done);
5778 }
5779 break;
5780 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005781
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005782 case TypeCheckKind::kClassHierarchyCheck: {
5783 // Walk over the class hierarchy to find a match.
5784 NearLabel loop, success;
5785 __ Bind(&loop);
5786 if (cls.IsRegister()) {
5787 __ cmpl(out, cls.AsRegister<CpuRegister>());
5788 } else {
5789 DCHECK(cls.IsStackSlot()) << cls;
5790 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5791 }
5792 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005793 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005794 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005795 __ testl(out, out);
5796 __ j(kNotEqual, &loop);
5797 // If `out` is null, we use it for the result, and jump to `done`.
5798 __ jmp(&done);
5799 __ Bind(&success);
5800 __ movl(out, Immediate(1));
5801 if (zero.IsLinked()) {
5802 __ jmp(&done);
5803 }
5804 break;
5805 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005806
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005807 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005808 // Do an exact check.
5809 NearLabel exact_check;
5810 if (cls.IsRegister()) {
5811 __ cmpl(out, cls.AsRegister<CpuRegister>());
5812 } else {
5813 DCHECK(cls.IsStackSlot()) << cls;
5814 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5815 }
5816 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005818 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005819 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005820 __ testl(out, out);
5821 // If `out` is null, we use it for the result, and jump to `done`.
5822 __ j(kEqual, &done);
5823 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5824 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005825 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005826 __ movl(out, Immediate(1));
5827 __ jmp(&done);
5828 break;
5829 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005830
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831 case TypeCheckKind::kArrayCheck: {
5832 if (cls.IsRegister()) {
5833 __ cmpl(out, cls.AsRegister<CpuRegister>());
5834 } else {
5835 DCHECK(cls.IsStackSlot()) << cls;
5836 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5837 }
5838 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5840 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 codegen_->AddSlowPath(slow_path);
5842 __ j(kNotEqual, slow_path->GetEntryLabel());
5843 __ movl(out, Immediate(1));
5844 if (zero.IsLinked()) {
5845 __ jmp(&done);
5846 }
5847 break;
5848 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005849
Calin Juravle98893e12015-10-02 21:05:03 +01005850 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005851 case TypeCheckKind::kInterfaceCheck: {
5852 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005853 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005854 // cases.
5855 //
5856 // We cannot directly call the InstanceofNonTrivial runtime
5857 // entry point without resorting to a type checking slow path
5858 // here (i.e. by calling InvokeRuntime directly), as it would
5859 // require to assign fixed registers for the inputs of this
5860 // HInstanceOf instruction (following the runtime calling
5861 // convention), which might be cluttered by the potential first
5862 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005863 //
5864 // TODO: Introduce a new runtime entry point taking the object
5865 // to test (instead of its class) as argument, and let it deal
5866 // with the read barrier issues. This will let us refactor this
5867 // case of the `switch` code as it was previously (with a direct
5868 // call to the runtime not using a type checking slow path).
5869 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005870 DCHECK(locations->OnlyCallsOnSlowPath());
5871 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5872 /* is_fatal */ false);
5873 codegen_->AddSlowPath(slow_path);
5874 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005875 if (zero.IsLinked()) {
5876 __ jmp(&done);
5877 }
5878 break;
5879 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005880 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005881
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005883 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005884 __ xorl(out, out);
5885 }
5886
5887 if (done.IsLinked()) {
5888 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005889 }
5890
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005891 if (slow_path != nullptr) {
5892 __ Bind(slow_path->GetExitLabel());
5893 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005894}
5895
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005896void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005897 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5898 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005899 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5900 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005901 case TypeCheckKind::kExactCheck:
5902 case TypeCheckKind::kAbstractClassCheck:
5903 case TypeCheckKind::kClassHierarchyCheck:
5904 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005905 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5906 LocationSummary::kCallOnSlowPath :
5907 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005908 break;
5909 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005910 case TypeCheckKind::kUnresolvedCheck:
5911 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005912 call_kind = LocationSummary::kCallOnSlowPath;
5913 break;
5914 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005915 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5916 locations->SetInAt(0, Location::RequiresRegister());
5917 locations->SetInAt(1, Location::Any());
5918 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5919 locations->AddTemp(Location::RequiresRegister());
5920 // When read barriers are enabled, we need an additional temporary
5921 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005922 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005923 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005924 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005925}
5926
5927void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005928 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005929 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005930 Location obj_loc = locations->InAt(0);
5931 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005932 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005933 Location temp_loc = locations->GetTemp(0);
5934 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005935 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005936 locations->GetTemp(1) :
5937 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005938 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5939 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5940 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5941 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005942
Roland Levillain0d5a2812015-11-13 10:07:31 +00005943 bool is_type_check_slow_path_fatal =
5944 (type_check_kind == TypeCheckKind::kExactCheck ||
5945 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5946 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5947 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5948 !instruction->CanThrowIntoCatchBlock();
5949 SlowPathCode* type_check_slow_path =
5950 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5951 is_type_check_slow_path_fatal);
5952 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953
Roland Levillain0d5a2812015-11-13 10:07:31 +00005954 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005955 case TypeCheckKind::kExactCheck:
5956 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005957 NearLabel done;
5958 // Avoid null check if we know obj is not null.
5959 if (instruction->MustDoNullCheck()) {
5960 __ testl(obj, obj);
5961 __ j(kEqual, &done);
5962 }
5963
5964 // /* HeapReference<Class> */ temp = obj->klass_
5965 GenerateReferenceLoadTwoRegisters(
5966 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5967
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 if (cls.IsRegister()) {
5969 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5970 } else {
5971 DCHECK(cls.IsStackSlot()) << cls;
5972 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5973 }
5974 // Jump to slow path for throwing the exception or doing a
5975 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005976 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005977 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005978 break;
5979 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005980
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005981 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005982 NearLabel done;
5983 // Avoid null check if we know obj is not null.
5984 if (instruction->MustDoNullCheck()) {
5985 __ testl(obj, obj);
5986 __ j(kEqual, &done);
5987 }
5988
5989 // /* HeapReference<Class> */ temp = obj->klass_
5990 GenerateReferenceLoadTwoRegisters(
5991 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5992
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005993 // If the class is abstract, we eagerly fetch the super class of the
5994 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005995 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005996 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005997 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005998 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005999
6000 // If the class reference currently in `temp` is not null, jump
6001 // to the `compare_classes` label to compare it with the checked
6002 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006003 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006004 __ j(kNotEqual, &compare_classes);
6005 // Otherwise, jump to the slow path to throw the exception.
6006 //
6007 // But before, move back the object's class into `temp` before
6008 // going into the slow path, as it has been overwritten in the
6009 // meantime.
6010 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006011 GenerateReferenceLoadTwoRegisters(
6012 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013 __ jmp(type_check_slow_path->GetEntryLabel());
6014
6015 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006016 if (cls.IsRegister()) {
6017 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6018 } else {
6019 DCHECK(cls.IsStackSlot()) << cls;
6020 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6021 }
6022 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00006023 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006024 break;
6025 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006026
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006027 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006028 NearLabel done;
6029 // Avoid null check if we know obj is not null.
6030 if (instruction->MustDoNullCheck()) {
6031 __ testl(obj, obj);
6032 __ j(kEqual, &done);
6033 }
6034
6035 // /* HeapReference<Class> */ temp = obj->klass_
6036 GenerateReferenceLoadTwoRegisters(
6037 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6038
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006039 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006040 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006041 __ Bind(&loop);
6042 if (cls.IsRegister()) {
6043 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6044 } else {
6045 DCHECK(cls.IsStackSlot()) << cls;
6046 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6047 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006048 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006049
Roland Levillain0d5a2812015-11-13 10:07:31 +00006050 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006051 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006052
6053 // If the class reference currently in `temp` is not null, jump
6054 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006055 __ testl(temp, temp);
6056 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006057 // Otherwise, jump to the slow path to throw the exception.
6058 //
6059 // But before, move back the object's class into `temp` before
6060 // going into the slow path, as it has been overwritten in the
6061 // meantime.
6062 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006063 GenerateReferenceLoadTwoRegisters(
6064 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006065 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006066 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006067 break;
6068 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006069
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006070 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006071 // We cannot use a NearLabel here, as its range might be too
6072 // short in some cases when read barriers are enabled. This has
6073 // been observed for instance when the code emitted for this
6074 // case uses high x86-64 registers (R8-R15).
6075 Label done;
6076 // Avoid null check if we know obj is not null.
6077 if (instruction->MustDoNullCheck()) {
6078 __ testl(obj, obj);
6079 __ j(kEqual, &done);
6080 }
6081
6082 // /* HeapReference<Class> */ temp = obj->klass_
6083 GenerateReferenceLoadTwoRegisters(
6084 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6085
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006086 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006087 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006088 if (cls.IsRegister()) {
6089 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6090 } else {
6091 DCHECK(cls.IsStackSlot()) << cls;
6092 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6093 }
6094 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006095
6096 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006097 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006098 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006099
6100 // If the component type is not null (i.e. the object is indeed
6101 // an array), jump to label `check_non_primitive_component_type`
6102 // to further check that this component type is not a primitive
6103 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006104 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006105 __ j(kNotEqual, &check_non_primitive_component_type);
6106 // Otherwise, jump to the slow path to throw the exception.
6107 //
6108 // But before, move back the object's class into `temp` before
6109 // going into the slow path, as it has been overwritten in the
6110 // meantime.
6111 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006112 GenerateReferenceLoadTwoRegisters(
6113 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006114 __ jmp(type_check_slow_path->GetEntryLabel());
6115
6116 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006117 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006118 __ j(kEqual, &done);
6119 // Same comment as above regarding `temp` and the slow path.
6120 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006121 GenerateReferenceLoadTwoRegisters(
6122 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006123 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006124 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006125 break;
6126 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006127
Calin Juravle98893e12015-10-02 21:05:03 +01006128 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006129 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006130 NearLabel done;
6131 // Avoid null check if we know obj is not null.
6132 if (instruction->MustDoNullCheck()) {
6133 __ testl(obj, obj);
6134 __ j(kEqual, &done);
6135 }
6136
6137 // /* HeapReference<Class> */ temp = obj->klass_
6138 GenerateReferenceLoadTwoRegisters(
6139 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6140
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006141 // We always go into the type check slow path for the unresolved
6142 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006143 //
6144 // We cannot directly call the CheckCast runtime entry point
6145 // without resorting to a type checking slow path here (i.e. by
6146 // calling InvokeRuntime directly), as it would require to
6147 // assign fixed registers for the inputs of this HInstanceOf
6148 // instruction (following the runtime calling convention), which
6149 // might be cluttered by the potential first read barrier
6150 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006151 //
6152 // TODO: Introduce a new runtime entry point taking the object
6153 // to test (instead of its class) as argument, and let it deal
6154 // with the read barrier issues. This will let us refactor this
6155 // case of the `switch` code as it was previously (with a direct
6156 // call to the runtime not using a type checking slow path).
6157 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006158 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006159 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006160 break;
6161 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006162
Roland Levillain0d5a2812015-11-13 10:07:31 +00006163 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006164}
6165
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006166void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6167 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006168 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006169 InvokeRuntimeCallingConvention calling_convention;
6170 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6171}
6172
6173void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006174 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6175 : QUICK_ENTRY_POINT(pUnlockObject),
6176 instruction,
6177 instruction->GetDexPc(),
6178 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006179 if (instruction->IsEnter()) {
6180 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6181 } else {
6182 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6183 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006184}
6185
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006186void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6187void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6188void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6189
6190void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6191 LocationSummary* locations =
6192 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6193 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6194 || instruction->GetResultType() == Primitive::kPrimLong);
6195 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006196 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006197 locations->SetOut(Location::SameAsFirstInput());
6198}
6199
6200void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6201 HandleBitwiseOperation(instruction);
6202}
6203
6204void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6205 HandleBitwiseOperation(instruction);
6206}
6207
6208void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6209 HandleBitwiseOperation(instruction);
6210}
6211
6212void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6213 LocationSummary* locations = instruction->GetLocations();
6214 Location first = locations->InAt(0);
6215 Location second = locations->InAt(1);
6216 DCHECK(first.Equals(locations->Out()));
6217
6218 if (instruction->GetResultType() == Primitive::kPrimInt) {
6219 if (second.IsRegister()) {
6220 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006221 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006222 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006223 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006224 } else {
6225 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006226 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006227 }
6228 } else if (second.IsConstant()) {
6229 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6230 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006231 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006232 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006233 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006234 } else {
6235 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006236 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006237 }
6238 } else {
6239 Address address(CpuRegister(RSP), second.GetStackIndex());
6240 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006241 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006242 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006243 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006244 } else {
6245 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006246 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006247 }
6248 }
6249 } else {
6250 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006251 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6252 bool second_is_constant = false;
6253 int64_t value = 0;
6254 if (second.IsConstant()) {
6255 second_is_constant = true;
6256 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006257 }
Mark Mendell40741f32015-04-20 22:10:34 -04006258 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006259
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006260 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006261 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006262 if (is_int32_value) {
6263 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6264 } else {
6265 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6266 }
6267 } else if (second.IsDoubleStackSlot()) {
6268 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006269 } else {
6270 __ andq(first_reg, second.AsRegister<CpuRegister>());
6271 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006272 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006273 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006274 if (is_int32_value) {
6275 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6276 } else {
6277 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6278 }
6279 } else if (second.IsDoubleStackSlot()) {
6280 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006281 } else {
6282 __ orq(first_reg, second.AsRegister<CpuRegister>());
6283 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006284 } else {
6285 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006286 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006287 if (is_int32_value) {
6288 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6289 } else {
6290 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6291 }
6292 } else if (second.IsDoubleStackSlot()) {
6293 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006294 } else {
6295 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6296 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006297 }
6298 }
6299}
6300
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006301void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6302 Location out,
6303 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006304 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006305 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6306 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006307 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006308 if (kUseBakerReadBarrier) {
6309 // Load with fast path based Baker's read barrier.
6310 // /* HeapReference<Object> */ out = *(out + offset)
6311 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006312 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006313 } else {
6314 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006315 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006316 // in the following move operation, as we will need it for the
6317 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006318 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006319 // /* HeapReference<Object> */ out = *(out + offset)
6320 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006321 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006322 }
6323 } else {
6324 // Plain load with no read barrier.
6325 // /* HeapReference<Object> */ out = *(out + offset)
6326 __ movl(out_reg, Address(out_reg, offset));
6327 __ MaybeUnpoisonHeapReference(out_reg);
6328 }
6329}
6330
6331void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6332 Location out,
6333 Location obj,
6334 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006335 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006336 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6337 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6338 if (kEmitCompilerReadBarrier) {
6339 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006340 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006341 // Load with fast path based Baker's read barrier.
6342 // /* HeapReference<Object> */ out = *(obj + offset)
6343 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006344 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006345 } else {
6346 // Load with slow path based read barrier.
6347 // /* HeapReference<Object> */ out = *(obj + offset)
6348 __ movl(out_reg, Address(obj_reg, offset));
6349 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6350 }
6351 } else {
6352 // Plain load with no read barrier.
6353 // /* HeapReference<Object> */ out = *(obj + offset)
6354 __ movl(out_reg, Address(obj_reg, offset));
6355 __ MaybeUnpoisonHeapReference(out_reg);
6356 }
6357}
6358
6359void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6360 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006361 const Address& address,
6362 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006363 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6364 if (kEmitCompilerReadBarrier) {
6365 if (kUseBakerReadBarrier) {
6366 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6367 // Baker's read barrier are used:
6368 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006369 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006370 // if (Thread::Current()->GetIsGcMarking()) {
6371 // root = ReadBarrier::Mark(root)
6372 // }
6373
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006374 // /* GcRoot<mirror::Object> */ root = *address
6375 __ movl(root_reg, address);
6376 if (fixup_label != nullptr) {
6377 __ Bind(fixup_label);
6378 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006379 static_assert(
6380 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6381 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6382 "have different sizes.");
6383 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6384 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6385 "have different sizes.");
6386
6387 // Slow path used to mark the GC root `root`.
6388 SlowPathCode* slow_path =
6389 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6390 codegen_->AddSlowPath(slow_path);
6391
6392 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6393 /* no_rip */ true),
6394 Immediate(0));
6395 __ j(kNotEqual, slow_path->GetEntryLabel());
6396 __ Bind(slow_path->GetExitLabel());
6397 } else {
6398 // GC root loaded through a slow path for read barriers other
6399 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006400 // /* GcRoot<mirror::Object>* */ root = address
6401 __ leaq(root_reg, address);
6402 if (fixup_label != nullptr) {
6403 __ Bind(fixup_label);
6404 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006405 // /* mirror::Object* */ root = root->Read()
6406 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6407 }
6408 } else {
6409 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006410 // /* GcRoot<mirror::Object> */ root = *address
6411 __ movl(root_reg, address);
6412 if (fixup_label != nullptr) {
6413 __ Bind(fixup_label);
6414 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006415 // Note that GC roots are not affected by heap poisoning, thus we
6416 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006417 }
6418}
6419
6420void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6421 Location ref,
6422 CpuRegister obj,
6423 uint32_t offset,
6424 Location temp,
6425 bool needs_null_check) {
6426 DCHECK(kEmitCompilerReadBarrier);
6427 DCHECK(kUseBakerReadBarrier);
6428
6429 // /* HeapReference<Object> */ ref = *(obj + offset)
6430 Address src(obj, offset);
6431 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6432}
6433
6434void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6435 Location ref,
6436 CpuRegister obj,
6437 uint32_t data_offset,
6438 Location index,
6439 Location temp,
6440 bool needs_null_check) {
6441 DCHECK(kEmitCompilerReadBarrier);
6442 DCHECK(kUseBakerReadBarrier);
6443
Roland Levillain3d312422016-06-23 13:53:42 +01006444 static_assert(
6445 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6446 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006447 // /* HeapReference<Object> */ ref =
6448 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6449 Address src = index.IsConstant() ?
6450 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6451 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6452 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6453}
6454
6455void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6456 Location ref,
6457 CpuRegister obj,
6458 const Address& src,
6459 Location temp,
6460 bool needs_null_check) {
6461 DCHECK(kEmitCompilerReadBarrier);
6462 DCHECK(kUseBakerReadBarrier);
6463
6464 // In slow path based read barriers, the read barrier call is
6465 // inserted after the original load. However, in fast path based
6466 // Baker's read barriers, we need to perform the load of
6467 // mirror::Object::monitor_ *before* the original reference load.
6468 // This load-load ordering is required by the read barrier.
6469 // The fast path/slow path (for Baker's algorithm) should look like:
6470 //
6471 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6472 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6473 // HeapReference<Object> ref = *src; // Original reference load.
6474 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6475 // if (is_gray) {
6476 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6477 // }
6478 //
6479 // Note: the original implementation in ReadBarrier::Barrier is
6480 // slightly more complex as:
6481 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006482 // the high-bits of rb_state, which are expected to be all zeroes
6483 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6484 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006485 // - it performs additional checks that we do not do here for
6486 // performance reasons.
6487
6488 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6489 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6490 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6491
6492 // /* int32_t */ monitor = obj->monitor_
6493 __ movl(temp_reg, Address(obj, monitor_offset));
6494 if (needs_null_check) {
6495 MaybeRecordImplicitNullCheck(instruction);
6496 }
6497 // /* LockWord */ lock_word = LockWord(monitor)
6498 static_assert(sizeof(LockWord) == sizeof(int32_t),
6499 "art::LockWord and int32_t have different sizes.");
6500 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6501 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6502 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6503 static_assert(
6504 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6505 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6506
6507 // Load fence to prevent load-load reordering.
6508 // Note that this is a no-op, thanks to the x86-64 memory model.
6509 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6510
6511 // The actual reference load.
6512 // /* HeapReference<Object> */ ref = *src
6513 __ movl(ref_reg, src);
6514
6515 // Object* ref = ref_addr->AsMirrorPtr()
6516 __ MaybeUnpoisonHeapReference(ref_reg);
6517
6518 // Slow path used to mark the object `ref` when it is gray.
6519 SlowPathCode* slow_path =
6520 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6521 AddSlowPath(slow_path);
6522
6523 // if (rb_state == ReadBarrier::gray_ptr_)
6524 // ref = ReadBarrier::Mark(ref);
6525 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6526 __ j(kEqual, slow_path->GetEntryLabel());
6527 __ Bind(slow_path->GetExitLabel());
6528}
6529
6530void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6531 Location out,
6532 Location ref,
6533 Location obj,
6534 uint32_t offset,
6535 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006536 DCHECK(kEmitCompilerReadBarrier);
6537
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006538 // Insert a slow path based read barrier *after* the reference load.
6539 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006540 // If heap poisoning is enabled, the unpoisoning of the loaded
6541 // reference will be carried out by the runtime within the slow
6542 // path.
6543 //
6544 // Note that `ref` currently does not get unpoisoned (when heap
6545 // poisoning is enabled), which is alright as the `ref` argument is
6546 // not used by the artReadBarrierSlow entry point.
6547 //
6548 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6549 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6550 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6551 AddSlowPath(slow_path);
6552
Roland Levillain0d5a2812015-11-13 10:07:31 +00006553 __ jmp(slow_path->GetEntryLabel());
6554 __ Bind(slow_path->GetExitLabel());
6555}
6556
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006557void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6558 Location out,
6559 Location ref,
6560 Location obj,
6561 uint32_t offset,
6562 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006563 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006564 // Baker's read barriers shall be handled by the fast path
6565 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6566 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006567 // If heap poisoning is enabled, unpoisoning will be taken care of
6568 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006569 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006570 } else if (kPoisonHeapReferences) {
6571 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6572 }
6573}
6574
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006575void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6576 Location out,
6577 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006578 DCHECK(kEmitCompilerReadBarrier);
6579
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006580 // Insert a slow path based read barrier *after* the GC root load.
6581 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006582 // Note that GC roots are not affected by heap poisoning, so we do
6583 // not need to do anything special for this here.
6584 SlowPathCode* slow_path =
6585 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6586 AddSlowPath(slow_path);
6587
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588 __ jmp(slow_path->GetEntryLabel());
6589 __ Bind(slow_path->GetExitLabel());
6590}
6591
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006592void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006593 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006594 LOG(FATAL) << "Unreachable";
6595}
6596
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006597void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006598 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006599 LOG(FATAL) << "Unreachable";
6600}
6601
Mark Mendellfe57faa2015-09-18 09:26:15 -04006602// Simple implementation of packed switch - generate cascaded compare/jumps.
6603void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6604 LocationSummary* locations =
6605 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6606 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006607 locations->AddTemp(Location::RequiresRegister());
6608 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006609}
6610
6611void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6612 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006613 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006614 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006615 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6616 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6617 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006618 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6619
6620 // Should we generate smaller inline compare/jumps?
6621 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6622 // Figure out the correct compare values and jump conditions.
6623 // Handle the first compare/branch as a special case because it might
6624 // jump to the default case.
6625 DCHECK_GT(num_entries, 2u);
6626 Condition first_condition;
6627 uint32_t index;
6628 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6629 if (lower_bound != 0) {
6630 first_condition = kLess;
6631 __ cmpl(value_reg_in, Immediate(lower_bound));
6632 __ j(first_condition, codegen_->GetLabelOf(default_block));
6633 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6634
6635 index = 1;
6636 } else {
6637 // Handle all the compare/jumps below.
6638 first_condition = kBelow;
6639 index = 0;
6640 }
6641
6642 // Handle the rest of the compare/jumps.
6643 for (; index + 1 < num_entries; index += 2) {
6644 int32_t compare_to_value = lower_bound + index + 1;
6645 __ cmpl(value_reg_in, Immediate(compare_to_value));
6646 // Jump to successors[index] if value < case_value[index].
6647 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6648 // Jump to successors[index + 1] if value == case_value[index + 1].
6649 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6650 }
6651
6652 if (index != num_entries) {
6653 // There are an odd number of entries. Handle the last one.
6654 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006655 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006656 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6657 }
6658
6659 // And the default for any other value.
6660 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6661 __ jmp(codegen_->GetLabelOf(default_block));
6662 }
6663 return;
6664 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006665
6666 // Remove the bias, if needed.
6667 Register value_reg_out = value_reg_in.AsRegister();
6668 if (lower_bound != 0) {
6669 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6670 value_reg_out = temp_reg.AsRegister();
6671 }
6672 CpuRegister value_reg(value_reg_out);
6673
6674 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006675 __ cmpl(value_reg, Immediate(num_entries - 1));
6676 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006677
Mark Mendell9c86b482015-09-18 13:36:07 -04006678 // We are in the range of the table.
6679 // Load the address of the jump table in the constant area.
6680 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006681
Mark Mendell9c86b482015-09-18 13:36:07 -04006682 // Load the (signed) offset from the jump table.
6683 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6684
6685 // Add the offset to the address of the table base.
6686 __ addq(temp_reg, base_reg);
6687
6688 // And jump.
6689 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006690}
6691
Aart Bikc5d47542016-01-27 17:00:35 -08006692void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6693 if (value == 0) {
6694 __ xorl(dest, dest);
6695 } else {
6696 __ movl(dest, Immediate(value));
6697 }
6698}
6699
Mark Mendell92e83bf2015-05-07 11:25:03 -04006700void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6701 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006702 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006703 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006704 } else if (IsUint<32>(value)) {
6705 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006706 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6707 } else {
6708 __ movq(dest, Immediate(value));
6709 }
6710}
6711
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006712void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6713 if (value == 0) {
6714 __ xorps(dest, dest);
6715 } else {
6716 __ movss(dest, LiteralInt32Address(value));
6717 }
6718}
6719
6720void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6721 if (value == 0) {
6722 __ xorpd(dest, dest);
6723 } else {
6724 __ movsd(dest, LiteralInt64Address(value));
6725 }
6726}
6727
6728void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6729 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6730}
6731
6732void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6733 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6734}
6735
Aart Bika19616e2016-02-01 18:57:58 -08006736void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6737 if (value == 0) {
6738 __ testl(dest, dest);
6739 } else {
6740 __ cmpl(dest, Immediate(value));
6741 }
6742}
6743
6744void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6745 if (IsInt<32>(value)) {
6746 if (value == 0) {
6747 __ testq(dest, dest);
6748 } else {
6749 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6750 }
6751 } else {
6752 // Value won't fit in an int.
6753 __ cmpq(dest, LiteralInt64Address(value));
6754 }
6755}
6756
Mark Mendellcfa410b2015-05-25 16:02:44 -04006757void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6758 DCHECK(dest.IsDoubleStackSlot());
6759 if (IsInt<32>(value)) {
6760 // Can move directly as an int32 constant.
6761 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6762 Immediate(static_cast<int32_t>(value)));
6763 } else {
6764 Load64BitValue(CpuRegister(TMP), value);
6765 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6766 }
6767}
6768
Mark Mendell9c86b482015-09-18 13:36:07 -04006769/**
6770 * Class to handle late fixup of offsets into constant area.
6771 */
6772class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6773 public:
6774 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6775 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6776
6777 protected:
6778 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6779
6780 CodeGeneratorX86_64* codegen_;
6781
6782 private:
6783 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6784 // Patch the correct offset for the instruction. We use the address of the
6785 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6786 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6787 int32_t relative_position = constant_offset - pos;
6788
6789 // Patch in the right value.
6790 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6791 }
6792
6793 // Location in constant area that the fixup refers to.
6794 size_t offset_into_constant_area_;
6795};
6796
6797/**
6798 t * Class to handle late fixup of offsets to a jump table that will be created in the
6799 * constant area.
6800 */
6801class JumpTableRIPFixup : public RIPFixup {
6802 public:
6803 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6804 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6805
6806 void CreateJumpTable() {
6807 X86_64Assembler* assembler = codegen_->GetAssembler();
6808
6809 // Ensure that the reference to the jump table has the correct offset.
6810 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6811 SetOffset(offset_in_constant_table);
6812
6813 // Compute the offset from the start of the function to this jump table.
6814 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6815
6816 // Populate the jump table with the correct values for the jump table.
6817 int32_t num_entries = switch_instr_->GetNumEntries();
6818 HBasicBlock* block = switch_instr_->GetBlock();
6819 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6820 // The value that we want is the target offset - the position of the table.
6821 for (int32_t i = 0; i < num_entries; i++) {
6822 HBasicBlock* b = successors[i];
6823 Label* l = codegen_->GetLabelOf(b);
6824 DCHECK(l->IsBound());
6825 int32_t offset_to_block = l->Position() - current_table_offset;
6826 assembler->AppendInt32(offset_to_block);
6827 }
6828 }
6829
6830 private:
6831 const HPackedSwitch* switch_instr_;
6832};
6833
Mark Mendellf55c3e02015-03-26 21:07:46 -04006834void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6835 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006836 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006837 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6838 // 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 -04006839 assembler->Align(4, 0);
6840 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006841
6842 // Populate any jump tables.
6843 for (auto jump_table : fixups_to_jump_tables_) {
6844 jump_table->CreateJumpTable();
6845 }
6846
6847 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006848 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006849 }
6850
6851 // And finish up.
6852 CodeGenerator::Finalize(allocator);
6853}
6854
Mark Mendellf55c3e02015-03-26 21:07:46 -04006855Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6856 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6857 return Address::RIP(fixup);
6858}
6859
6860Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6861 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6862 return Address::RIP(fixup);
6863}
6864
6865Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6866 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6867 return Address::RIP(fixup);
6868}
6869
6870Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6871 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6872 return Address::RIP(fixup);
6873}
6874
Andreas Gampe85b62f22015-09-09 13:15:38 -07006875// TODO: trg as memory.
6876void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6877 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006878 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006879 return;
6880 }
6881
6882 DCHECK_NE(type, Primitive::kPrimVoid);
6883
6884 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6885 if (trg.Equals(return_loc)) {
6886 return;
6887 }
6888
6889 // Let the parallel move resolver take care of all of this.
6890 HParallelMove parallel_move(GetGraph()->GetArena());
6891 parallel_move.AddMove(return_loc, trg, type, nullptr);
6892 GetMoveResolver()->EmitNativeCode(&parallel_move);
6893}
6894
Mark Mendell9c86b482015-09-18 13:36:07 -04006895Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6896 // Create a fixup to be used to create and address the jump table.
6897 JumpTableRIPFixup* table_fixup =
6898 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6899
6900 // We have to populate the jump tables.
6901 fixups_to_jump_tables_.push_back(table_fixup);
6902 return Address::RIP(table_fixup);
6903}
6904
Mark Mendellea5af682015-10-22 17:35:49 -04006905void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6906 const Address& addr_high,
6907 int64_t v,
6908 HInstruction* instruction) {
6909 if (IsInt<32>(v)) {
6910 int32_t v_32 = v;
6911 __ movq(addr_low, Immediate(v_32));
6912 MaybeRecordImplicitNullCheck(instruction);
6913 } else {
6914 // Didn't fit in a register. Do it in pieces.
6915 int32_t low_v = Low32Bits(v);
6916 int32_t high_v = High32Bits(v);
6917 __ movl(addr_low, Immediate(low_v));
6918 MaybeRecordImplicitNullCheck(instruction);
6919 __ movl(addr_high, Immediate(high_v));
6920 }
6921}
6922
Roland Levillain4d027112015-07-01 15:41:14 +01006923#undef __
6924
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006925} // namespace x86_64
6926} // namespace art