blob: f05dbba29491ed0a2e4bab2a1cf7471ed79c1ea3 [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 Geoffray88f288e2016-06-29 08:17:52 +00002260 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2261 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002262 Location receiver = locations->InAt(0);
2263 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2264
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 // Set the hidden argument. This is safe to do this here, as RAX
2266 // won't be modified thereafter, before the `call` instruction.
2267 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002268 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002269
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002270 if (receiver.IsStackSlot()) {
2271 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002272 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002273 __ movl(temp, Address(temp, class_offset));
2274 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002275 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002276 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002278 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002279 // Instead of simply (possibly) unpoisoning `temp` here, we should
2280 // emit a read barrier for the previous class reference load.
2281 // However this is not required in practice, as this is an
2282 // intermediate/temporary reference and because the current
2283 // concurrent copying collector keeps the from-space memory
2284 // intact/accessible until the end of the marking phase (the
2285 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002286 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002287 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002288 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002289 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002290 __ call(Address(temp,
2291 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002292
2293 DCHECK(!codegen_->IsLeafMethod());
2294 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2295}
2296
Roland Levillain88cb1752014-10-20 16:36:47 +01002297void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2298 LocationSummary* locations =
2299 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2300 switch (neg->GetResultType()) {
2301 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002302 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002303 locations->SetInAt(0, Location::RequiresRegister());
2304 locations->SetOut(Location::SameAsFirstInput());
2305 break;
2306
Roland Levillain88cb1752014-10-20 16:36:47 +01002307 case Primitive::kPrimFloat:
2308 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002309 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002310 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002311 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002312 break;
2313
2314 default:
2315 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2316 }
2317}
2318
2319void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2320 LocationSummary* locations = neg->GetLocations();
2321 Location out = locations->Out();
2322 Location in = locations->InAt(0);
2323 switch (neg->GetResultType()) {
2324 case Primitive::kPrimInt:
2325 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002326 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002327 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002328 break;
2329
2330 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002331 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002332 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002333 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002334 break;
2335
Roland Levillain5368c212014-11-27 15:03:41 +00002336 case Primitive::kPrimFloat: {
2337 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002338 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002339 // Implement float negation with an exclusive or with value
2340 // 0x80000000 (mask for bit 31, representing the sign of a
2341 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002342 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002343 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002344 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002345 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002346
Roland Levillain5368c212014-11-27 15:03:41 +00002347 case Primitive::kPrimDouble: {
2348 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002349 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002350 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002351 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002352 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002353 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002354 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002355 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002356 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002357
2358 default:
2359 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2360 }
2361}
2362
Roland Levillaindff1f282014-11-05 14:15:05 +00002363void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2364 LocationSummary* locations =
2365 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2366 Primitive::Type result_type = conversion->GetResultType();
2367 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002368 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002369
David Brazdilb2bd1c52015-03-25 11:17:37 +00002370 // The Java language does not allow treating boolean as an integral type but
2371 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002372
Roland Levillaindff1f282014-11-05 14:15:05 +00002373 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002374 case Primitive::kPrimByte:
2375 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002376 case Primitive::kPrimLong:
2377 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002378 case Primitive::kPrimBoolean:
2379 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002380 case Primitive::kPrimShort:
2381 case Primitive::kPrimInt:
2382 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002383 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002384 locations->SetInAt(0, Location::Any());
2385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2386 break;
2387
2388 default:
2389 LOG(FATAL) << "Unexpected type conversion from " << input_type
2390 << " to " << result_type;
2391 }
2392 break;
2393
Roland Levillain01a8d712014-11-14 16:27:39 +00002394 case Primitive::kPrimShort:
2395 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002396 case Primitive::kPrimLong:
2397 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002398 case Primitive::kPrimBoolean:
2399 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002400 case Primitive::kPrimByte:
2401 case Primitive::kPrimInt:
2402 case Primitive::kPrimChar:
2403 // Processing a Dex `int-to-short' instruction.
2404 locations->SetInAt(0, Location::Any());
2405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2406 break;
2407
2408 default:
2409 LOG(FATAL) << "Unexpected type conversion from " << input_type
2410 << " to " << result_type;
2411 }
2412 break;
2413
Roland Levillain946e1432014-11-11 17:35:19 +00002414 case Primitive::kPrimInt:
2415 switch (input_type) {
2416 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002417 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002418 locations->SetInAt(0, Location::Any());
2419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2420 break;
2421
2422 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002423 // Processing a Dex `float-to-int' instruction.
2424 locations->SetInAt(0, Location::RequiresFpuRegister());
2425 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002426 break;
2427
Roland Levillain946e1432014-11-11 17:35:19 +00002428 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002429 // Processing a Dex `double-to-int' instruction.
2430 locations->SetInAt(0, Location::RequiresFpuRegister());
2431 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002432 break;
2433
2434 default:
2435 LOG(FATAL) << "Unexpected type conversion from " << input_type
2436 << " to " << result_type;
2437 }
2438 break;
2439
Roland Levillaindff1f282014-11-05 14:15:05 +00002440 case Primitive::kPrimLong:
2441 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002442 case Primitive::kPrimBoolean:
2443 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002444 case Primitive::kPrimByte:
2445 case Primitive::kPrimShort:
2446 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002447 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002448 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002449 // TODO: We would benefit from a (to-be-implemented)
2450 // Location::RegisterOrStackSlot requirement for this input.
2451 locations->SetInAt(0, Location::RequiresRegister());
2452 locations->SetOut(Location::RequiresRegister());
2453 break;
2454
2455 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002456 // Processing a Dex `float-to-long' instruction.
2457 locations->SetInAt(0, Location::RequiresFpuRegister());
2458 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002459 break;
2460
Roland Levillaindff1f282014-11-05 14:15:05 +00002461 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002462 // Processing a Dex `double-to-long' instruction.
2463 locations->SetInAt(0, Location::RequiresFpuRegister());
2464 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002465 break;
2466
2467 default:
2468 LOG(FATAL) << "Unexpected type conversion from " << input_type
2469 << " to " << result_type;
2470 }
2471 break;
2472
Roland Levillain981e4542014-11-14 11:47:14 +00002473 case Primitive::kPrimChar:
2474 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002475 case Primitive::kPrimLong:
2476 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002477 case Primitive::kPrimBoolean:
2478 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002479 case Primitive::kPrimByte:
2480 case Primitive::kPrimShort:
2481 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002482 // Processing a Dex `int-to-char' instruction.
2483 locations->SetInAt(0, Location::Any());
2484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2485 break;
2486
2487 default:
2488 LOG(FATAL) << "Unexpected type conversion from " << input_type
2489 << " to " << result_type;
2490 }
2491 break;
2492
Roland Levillaindff1f282014-11-05 14:15:05 +00002493 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002494 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002495 case Primitive::kPrimBoolean:
2496 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002497 case Primitive::kPrimByte:
2498 case Primitive::kPrimShort:
2499 case Primitive::kPrimInt:
2500 case Primitive::kPrimChar:
2501 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002502 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002503 locations->SetOut(Location::RequiresFpuRegister());
2504 break;
2505
2506 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002507 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002508 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002509 locations->SetOut(Location::RequiresFpuRegister());
2510 break;
2511
Roland Levillaincff13742014-11-17 14:32:17 +00002512 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002513 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002514 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002515 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002516 break;
2517
2518 default:
2519 LOG(FATAL) << "Unexpected type conversion from " << input_type
2520 << " to " << result_type;
2521 };
2522 break;
2523
Roland Levillaindff1f282014-11-05 14:15:05 +00002524 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002525 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002526 case Primitive::kPrimBoolean:
2527 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002528 case Primitive::kPrimByte:
2529 case Primitive::kPrimShort:
2530 case Primitive::kPrimInt:
2531 case Primitive::kPrimChar:
2532 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002533 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002534 locations->SetOut(Location::RequiresFpuRegister());
2535 break;
2536
2537 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002538 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002539 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002540 locations->SetOut(Location::RequiresFpuRegister());
2541 break;
2542
Roland Levillaincff13742014-11-17 14:32:17 +00002543 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002544 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002545 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002546 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002547 break;
2548
2549 default:
2550 LOG(FATAL) << "Unexpected type conversion from " << input_type
2551 << " to " << result_type;
2552 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002553 break;
2554
2555 default:
2556 LOG(FATAL) << "Unexpected type conversion from " << input_type
2557 << " to " << result_type;
2558 }
2559}
2560
2561void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2562 LocationSummary* locations = conversion->GetLocations();
2563 Location out = locations->Out();
2564 Location in = locations->InAt(0);
2565 Primitive::Type result_type = conversion->GetResultType();
2566 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002567 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002568 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002569 case Primitive::kPrimByte:
2570 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002571 case Primitive::kPrimLong:
2572 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002573 case Primitive::kPrimBoolean:
2574 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002575 case Primitive::kPrimShort:
2576 case Primitive::kPrimInt:
2577 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002578 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002579 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002580 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002581 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002582 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002583 Address(CpuRegister(RSP), in.GetStackIndex()));
2584 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002585 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002586 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002587 }
2588 break;
2589
2590 default:
2591 LOG(FATAL) << "Unexpected type conversion from " << input_type
2592 << " to " << result_type;
2593 }
2594 break;
2595
Roland Levillain01a8d712014-11-14 16:27:39 +00002596 case Primitive::kPrimShort:
2597 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002598 case Primitive::kPrimLong:
2599 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002600 case Primitive::kPrimBoolean:
2601 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002602 case Primitive::kPrimByte:
2603 case Primitive::kPrimInt:
2604 case Primitive::kPrimChar:
2605 // Processing a Dex `int-to-short' instruction.
2606 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002607 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002608 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002609 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002610 Address(CpuRegister(RSP), in.GetStackIndex()));
2611 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002612 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002613 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002614 }
2615 break;
2616
2617 default:
2618 LOG(FATAL) << "Unexpected type conversion from " << input_type
2619 << " to " << result_type;
2620 }
2621 break;
2622
Roland Levillain946e1432014-11-11 17:35:19 +00002623 case Primitive::kPrimInt:
2624 switch (input_type) {
2625 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002626 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002627 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002628 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002629 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002630 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002631 Address(CpuRegister(RSP), in.GetStackIndex()));
2632 } else {
2633 DCHECK(in.IsConstant());
2634 DCHECK(in.GetConstant()->IsLongConstant());
2635 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002636 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002637 }
2638 break;
2639
Roland Levillain3f8f9362014-12-02 17:45:01 +00002640 case Primitive::kPrimFloat: {
2641 // Processing a Dex `float-to-int' instruction.
2642 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2643 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002644 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002645
2646 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002647 // if input >= (float)INT_MAX goto done
2648 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002649 __ j(kAboveEqual, &done);
2650 // if input == NaN goto nan
2651 __ j(kUnordered, &nan);
2652 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002653 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002654 __ jmp(&done);
2655 __ Bind(&nan);
2656 // output = 0
2657 __ xorl(output, output);
2658 __ Bind(&done);
2659 break;
2660 }
2661
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002662 case Primitive::kPrimDouble: {
2663 // Processing a Dex `double-to-int' instruction.
2664 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2665 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002666 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002667
2668 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002669 // if input >= (double)INT_MAX goto done
2670 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002671 __ j(kAboveEqual, &done);
2672 // if input == NaN goto nan
2673 __ j(kUnordered, &nan);
2674 // output = double-to-int-truncate(input)
2675 __ cvttsd2si(output, input);
2676 __ jmp(&done);
2677 __ Bind(&nan);
2678 // output = 0
2679 __ xorl(output, output);
2680 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002681 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002682 }
Roland Levillain946e1432014-11-11 17:35:19 +00002683
2684 default:
2685 LOG(FATAL) << "Unexpected type conversion from " << input_type
2686 << " to " << result_type;
2687 }
2688 break;
2689
Roland Levillaindff1f282014-11-05 14:15:05 +00002690 case Primitive::kPrimLong:
2691 switch (input_type) {
2692 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002693 case Primitive::kPrimBoolean:
2694 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002695 case Primitive::kPrimByte:
2696 case Primitive::kPrimShort:
2697 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002698 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002699 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002700 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002701 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002702 break;
2703
Roland Levillain624279f2014-12-04 11:54:28 +00002704 case Primitive::kPrimFloat: {
2705 // Processing a Dex `float-to-long' instruction.
2706 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2707 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002708 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002709
Mark Mendell92e83bf2015-05-07 11:25:03 -04002710 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002711 // if input >= (float)LONG_MAX goto done
2712 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002713 __ j(kAboveEqual, &done);
2714 // if input == NaN goto nan
2715 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002716 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002717 __ cvttss2si(output, input, true);
2718 __ jmp(&done);
2719 __ Bind(&nan);
2720 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002721 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002722 __ Bind(&done);
2723 break;
2724 }
2725
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002726 case Primitive::kPrimDouble: {
2727 // Processing a Dex `double-to-long' instruction.
2728 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2729 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002730 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002731
Mark Mendell92e83bf2015-05-07 11:25:03 -04002732 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002733 // if input >= (double)LONG_MAX goto done
2734 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002735 __ j(kAboveEqual, &done);
2736 // if input == NaN goto nan
2737 __ j(kUnordered, &nan);
2738 // output = double-to-long-truncate(input)
2739 __ cvttsd2si(output, input, true);
2740 __ jmp(&done);
2741 __ Bind(&nan);
2742 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002743 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002744 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002745 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002746 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002747
2748 default:
2749 LOG(FATAL) << "Unexpected type conversion from " << input_type
2750 << " to " << result_type;
2751 }
2752 break;
2753
Roland Levillain981e4542014-11-14 11:47:14 +00002754 case Primitive::kPrimChar:
2755 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002756 case Primitive::kPrimLong:
2757 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002758 case Primitive::kPrimBoolean:
2759 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002760 case Primitive::kPrimByte:
2761 case Primitive::kPrimShort:
2762 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002763 // Processing a Dex `int-to-char' instruction.
2764 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002765 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002766 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002767 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002768 Address(CpuRegister(RSP), in.GetStackIndex()));
2769 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002771 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002772 }
2773 break;
2774
2775 default:
2776 LOG(FATAL) << "Unexpected type conversion from " << input_type
2777 << " to " << result_type;
2778 }
2779 break;
2780
Roland Levillaindff1f282014-11-05 14:15:05 +00002781 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002782 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002783 case Primitive::kPrimBoolean:
2784 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002785 case Primitive::kPrimByte:
2786 case Primitive::kPrimShort:
2787 case Primitive::kPrimInt:
2788 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002789 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002790 if (in.IsRegister()) {
2791 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2792 } else if (in.IsConstant()) {
2793 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2794 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002795 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002796 } else {
2797 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2798 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2799 }
Roland Levillaincff13742014-11-17 14:32:17 +00002800 break;
2801
2802 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002803 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002804 if (in.IsRegister()) {
2805 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2806 } else if (in.IsConstant()) {
2807 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2808 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002809 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002810 } else {
2811 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2812 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2813 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002814 break;
2815
Roland Levillaincff13742014-11-17 14:32:17 +00002816 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002817 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002818 if (in.IsFpuRegister()) {
2819 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2820 } else if (in.IsConstant()) {
2821 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2822 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002823 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002824 } else {
2825 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2826 Address(CpuRegister(RSP), in.GetStackIndex()));
2827 }
Roland Levillaincff13742014-11-17 14:32:17 +00002828 break;
2829
2830 default:
2831 LOG(FATAL) << "Unexpected type conversion from " << input_type
2832 << " to " << result_type;
2833 };
2834 break;
2835
Roland Levillaindff1f282014-11-05 14:15:05 +00002836 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002837 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002838 case Primitive::kPrimBoolean:
2839 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002840 case Primitive::kPrimByte:
2841 case Primitive::kPrimShort:
2842 case Primitive::kPrimInt:
2843 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002844 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002845 if (in.IsRegister()) {
2846 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2847 } else if (in.IsConstant()) {
2848 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2849 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002850 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002851 } else {
2852 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2853 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2854 }
Roland Levillaincff13742014-11-17 14:32:17 +00002855 break;
2856
2857 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002858 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002859 if (in.IsRegister()) {
2860 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2861 } else if (in.IsConstant()) {
2862 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2863 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002864 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002865 } else {
2866 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2867 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2868 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002869 break;
2870
Roland Levillaincff13742014-11-17 14:32:17 +00002871 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002872 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002873 if (in.IsFpuRegister()) {
2874 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2875 } else if (in.IsConstant()) {
2876 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2877 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002878 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002879 } else {
2880 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2881 Address(CpuRegister(RSP), in.GetStackIndex()));
2882 }
Roland Levillaincff13742014-11-17 14:32:17 +00002883 break;
2884
2885 default:
2886 LOG(FATAL) << "Unexpected type conversion from " << input_type
2887 << " to " << result_type;
2888 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002889 break;
2890
2891 default:
2892 LOG(FATAL) << "Unexpected type conversion from " << input_type
2893 << " to " << result_type;
2894 }
2895}
2896
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002897void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002898 LocationSummary* locations =
2899 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002900 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002901 case Primitive::kPrimInt: {
2902 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002903 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2904 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002905 break;
2906 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002907
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002908 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002909 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002910 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002911 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002913 break;
2914 }
2915
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002916 case Primitive::kPrimDouble:
2917 case Primitive::kPrimFloat: {
2918 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002919 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002920 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002921 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002922 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002923
2924 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002925 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002926 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002927}
2928
2929void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2930 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002931 Location first = locations->InAt(0);
2932 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002933 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002934
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002935 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002936 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002937 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002938 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2939 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002940 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2941 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002942 } else {
2943 __ leal(out.AsRegister<CpuRegister>(), Address(
2944 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2945 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002946 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002947 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2948 __ addl(out.AsRegister<CpuRegister>(),
2949 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2950 } else {
2951 __ leal(out.AsRegister<CpuRegister>(), Address(
2952 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2953 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002954 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002955 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002956 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002957 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002958 break;
2959 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002960
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002961 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002962 if (second.IsRegister()) {
2963 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2964 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002965 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2966 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002967 } else {
2968 __ leaq(out.AsRegister<CpuRegister>(), Address(
2969 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2970 }
2971 } else {
2972 DCHECK(second.IsConstant());
2973 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2974 int32_t int32_value = Low32Bits(value);
2975 DCHECK_EQ(int32_value, value);
2976 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2977 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2978 } else {
2979 __ leaq(out.AsRegister<CpuRegister>(), Address(
2980 first.AsRegister<CpuRegister>(), int32_value));
2981 }
2982 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002983 break;
2984 }
2985
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002986 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002987 if (second.IsFpuRegister()) {
2988 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2989 } else if (second.IsConstant()) {
2990 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002991 codegen_->LiteralFloatAddress(
2992 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04002993 } else {
2994 DCHECK(second.IsStackSlot());
2995 __ addss(first.AsFpuRegister<XmmRegister>(),
2996 Address(CpuRegister(RSP), second.GetStackIndex()));
2997 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002998 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002999 }
3000
3001 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003002 if (second.IsFpuRegister()) {
3003 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3004 } else if (second.IsConstant()) {
3005 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003006 codegen_->LiteralDoubleAddress(
3007 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003008 } else {
3009 DCHECK(second.IsDoubleStackSlot());
3010 __ addsd(first.AsFpuRegister<XmmRegister>(),
3011 Address(CpuRegister(RSP), second.GetStackIndex()));
3012 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003013 break;
3014 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003015
3016 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003017 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003018 }
3019}
3020
3021void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003022 LocationSummary* locations =
3023 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003024 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003025 case Primitive::kPrimInt: {
3026 locations->SetInAt(0, Location::RequiresRegister());
3027 locations->SetInAt(1, Location::Any());
3028 locations->SetOut(Location::SameAsFirstInput());
3029 break;
3030 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003031 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003032 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003033 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003034 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003035 break;
3036 }
Calin Juravle11351682014-10-23 15:38:15 +01003037 case Primitive::kPrimFloat:
3038 case Primitive::kPrimDouble: {
3039 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003040 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003041 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003042 break;
Calin Juravle11351682014-10-23 15:38:15 +01003043 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003044 default:
Calin Juravle11351682014-10-23 15:38:15 +01003045 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003046 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003047}
3048
3049void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3050 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003051 Location first = locations->InAt(0);
3052 Location second = locations->InAt(1);
3053 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003054 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003055 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003056 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003058 } else if (second.IsConstant()) {
3059 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003060 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003061 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003062 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003063 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003064 break;
3065 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003066 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003067 if (second.IsConstant()) {
3068 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3069 DCHECK(IsInt<32>(value));
3070 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3071 } else {
3072 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3073 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003074 break;
3075 }
3076
Calin Juravle11351682014-10-23 15:38:15 +01003077 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003078 if (second.IsFpuRegister()) {
3079 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3080 } else if (second.IsConstant()) {
3081 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003082 codegen_->LiteralFloatAddress(
3083 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003084 } else {
3085 DCHECK(second.IsStackSlot());
3086 __ subss(first.AsFpuRegister<XmmRegister>(),
3087 Address(CpuRegister(RSP), second.GetStackIndex()));
3088 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003089 break;
Calin Juravle11351682014-10-23 15:38:15 +01003090 }
3091
3092 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003093 if (second.IsFpuRegister()) {
3094 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3095 } else if (second.IsConstant()) {
3096 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003097 codegen_->LiteralDoubleAddress(
3098 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003099 } else {
3100 DCHECK(second.IsDoubleStackSlot());
3101 __ subsd(first.AsFpuRegister<XmmRegister>(),
3102 Address(CpuRegister(RSP), second.GetStackIndex()));
3103 }
Calin Juravle11351682014-10-23 15:38:15 +01003104 break;
3105 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003106
3107 default:
Calin Juravle11351682014-10-23 15:38:15 +01003108 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003109 }
3110}
3111
Calin Juravle34bacdf2014-10-07 20:23:36 +01003112void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3113 LocationSummary* locations =
3114 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3115 switch (mul->GetResultType()) {
3116 case Primitive::kPrimInt: {
3117 locations->SetInAt(0, Location::RequiresRegister());
3118 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003119 if (mul->InputAt(1)->IsIntConstant()) {
3120 // Can use 3 operand multiply.
3121 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3122 } else {
3123 locations->SetOut(Location::SameAsFirstInput());
3124 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003125 break;
3126 }
3127 case Primitive::kPrimLong: {
3128 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003129 locations->SetInAt(1, Location::Any());
3130 if (mul->InputAt(1)->IsLongConstant() &&
3131 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003132 // Can use 3 operand multiply.
3133 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3134 } else {
3135 locations->SetOut(Location::SameAsFirstInput());
3136 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003137 break;
3138 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003139 case Primitive::kPrimFloat:
3140 case Primitive::kPrimDouble: {
3141 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003142 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003143 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003144 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003145 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003146
3147 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003148 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003149 }
3150}
3151
3152void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3153 LocationSummary* locations = mul->GetLocations();
3154 Location first = locations->InAt(0);
3155 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003156 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003157 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003158 case Primitive::kPrimInt:
3159 // The constant may have ended up in a register, so test explicitly to avoid
3160 // problems where the output may not be the same as the first operand.
3161 if (mul->InputAt(1)->IsIntConstant()) {
3162 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3163 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3164 } else if (second.IsRegister()) {
3165 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003166 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003167 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003168 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003169 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003170 __ imull(first.AsRegister<CpuRegister>(),
3171 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003172 }
3173 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003174 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003175 // The constant may have ended up in a register, so test explicitly to avoid
3176 // problems where the output may not be the same as the first operand.
3177 if (mul->InputAt(1)->IsLongConstant()) {
3178 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3179 if (IsInt<32>(value)) {
3180 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3181 Immediate(static_cast<int32_t>(value)));
3182 } else {
3183 // Have to use the constant area.
3184 DCHECK(first.Equals(out));
3185 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3186 }
3187 } else if (second.IsRegister()) {
3188 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003189 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003190 } else {
3191 DCHECK(second.IsDoubleStackSlot());
3192 DCHECK(first.Equals(out));
3193 __ imulq(first.AsRegister<CpuRegister>(),
3194 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003195 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003196 break;
3197 }
3198
Calin Juravleb5bfa962014-10-21 18:02:24 +01003199 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003200 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003201 if (second.IsFpuRegister()) {
3202 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3203 } else if (second.IsConstant()) {
3204 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003205 codegen_->LiteralFloatAddress(
3206 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003207 } else {
3208 DCHECK(second.IsStackSlot());
3209 __ mulss(first.AsFpuRegister<XmmRegister>(),
3210 Address(CpuRegister(RSP), second.GetStackIndex()));
3211 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003212 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003213 }
3214
3215 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003216 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003217 if (second.IsFpuRegister()) {
3218 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3219 } else if (second.IsConstant()) {
3220 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003221 codegen_->LiteralDoubleAddress(
3222 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003223 } else {
3224 DCHECK(second.IsDoubleStackSlot());
3225 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3226 Address(CpuRegister(RSP), second.GetStackIndex()));
3227 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003228 break;
3229 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003230
3231 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003232 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003233 }
3234}
3235
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003236void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3237 uint32_t stack_adjustment, bool is_float) {
3238 if (source.IsStackSlot()) {
3239 DCHECK(is_float);
3240 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3241 } else if (source.IsDoubleStackSlot()) {
3242 DCHECK(!is_float);
3243 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3244 } else {
3245 // Write the value to the temporary location on the stack and load to FP stack.
3246 if (is_float) {
3247 Location stack_temp = Location::StackSlot(temp_offset);
3248 codegen_->Move(stack_temp, source);
3249 __ flds(Address(CpuRegister(RSP), temp_offset));
3250 } else {
3251 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3252 codegen_->Move(stack_temp, source);
3253 __ fldl(Address(CpuRegister(RSP), temp_offset));
3254 }
3255 }
3256}
3257
3258void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3259 Primitive::Type type = rem->GetResultType();
3260 bool is_float = type == Primitive::kPrimFloat;
3261 size_t elem_size = Primitive::ComponentSize(type);
3262 LocationSummary* locations = rem->GetLocations();
3263 Location first = locations->InAt(0);
3264 Location second = locations->InAt(1);
3265 Location out = locations->Out();
3266
3267 // Create stack space for 2 elements.
3268 // TODO: enhance register allocator to ask for stack temporaries.
3269 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3270
3271 // Load the values to the FP stack in reverse order, using temporaries if needed.
3272 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3273 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3274
3275 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003276 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003277 __ Bind(&retry);
3278 __ fprem();
3279
3280 // Move FP status to AX.
3281 __ fstsw();
3282
3283 // And see if the argument reduction is complete. This is signaled by the
3284 // C2 FPU flag bit set to 0.
3285 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3286 __ j(kNotEqual, &retry);
3287
3288 // We have settled on the final value. Retrieve it into an XMM register.
3289 // Store FP top of stack to real stack.
3290 if (is_float) {
3291 __ fsts(Address(CpuRegister(RSP), 0));
3292 } else {
3293 __ fstl(Address(CpuRegister(RSP), 0));
3294 }
3295
3296 // Pop the 2 items from the FP stack.
3297 __ fucompp();
3298
3299 // Load the value from the stack into an XMM register.
3300 DCHECK(out.IsFpuRegister()) << out;
3301 if (is_float) {
3302 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3303 } else {
3304 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3305 }
3306
3307 // And remove the temporary stack space we allocated.
3308 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3309}
3310
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003311void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3312 DCHECK(instruction->IsDiv() || instruction->IsRem());
3313
3314 LocationSummary* locations = instruction->GetLocations();
3315 Location second = locations->InAt(1);
3316 DCHECK(second.IsConstant());
3317
3318 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3319 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003320 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003321
3322 DCHECK(imm == 1 || imm == -1);
3323
3324 switch (instruction->GetResultType()) {
3325 case Primitive::kPrimInt: {
3326 if (instruction->IsRem()) {
3327 __ xorl(output_register, output_register);
3328 } else {
3329 __ movl(output_register, input_register);
3330 if (imm == -1) {
3331 __ negl(output_register);
3332 }
3333 }
3334 break;
3335 }
3336
3337 case Primitive::kPrimLong: {
3338 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003339 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003340 } else {
3341 __ movq(output_register, input_register);
3342 if (imm == -1) {
3343 __ negq(output_register);
3344 }
3345 }
3346 break;
3347 }
3348
3349 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003350 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003351 }
3352}
3353
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003354void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003355 LocationSummary* locations = instruction->GetLocations();
3356 Location second = locations->InAt(1);
3357
3358 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3359 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3360
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003361 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003362 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3363 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003364
3365 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3366
3367 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003368 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003369 __ testl(numerator, numerator);
3370 __ cmov(kGreaterEqual, tmp, numerator);
3371 int shift = CTZ(imm);
3372 __ sarl(tmp, Immediate(shift));
3373
3374 if (imm < 0) {
3375 __ negl(tmp);
3376 }
3377
3378 __ movl(output_register, tmp);
3379 } else {
3380 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3381 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3382
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003383 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003384 __ addq(rdx, numerator);
3385 __ testq(numerator, numerator);
3386 __ cmov(kGreaterEqual, rdx, numerator);
3387 int shift = CTZ(imm);
3388 __ sarq(rdx, Immediate(shift));
3389
3390 if (imm < 0) {
3391 __ negq(rdx);
3392 }
3393
3394 __ movq(output_register, rdx);
3395 }
3396}
3397
3398void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3399 DCHECK(instruction->IsDiv() || instruction->IsRem());
3400
3401 LocationSummary* locations = instruction->GetLocations();
3402 Location second = locations->InAt(1);
3403
3404 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3405 : locations->GetTemp(0).AsRegister<CpuRegister>();
3406 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3407 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3408 : locations->Out().AsRegister<CpuRegister>();
3409 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3410
3411 DCHECK_EQ(RAX, eax.AsRegister());
3412 DCHECK_EQ(RDX, edx.AsRegister());
3413 if (instruction->IsDiv()) {
3414 DCHECK_EQ(RAX, out.AsRegister());
3415 } else {
3416 DCHECK_EQ(RDX, out.AsRegister());
3417 }
3418
3419 int64_t magic;
3420 int shift;
3421
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003422 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003423 if (instruction->GetResultType() == Primitive::kPrimInt) {
3424 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3425
3426 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3427
3428 __ movl(numerator, eax);
3429
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003430 __ movl(eax, Immediate(magic));
3431 __ imull(numerator);
3432
3433 if (imm > 0 && magic < 0) {
3434 __ addl(edx, numerator);
3435 } else if (imm < 0 && magic > 0) {
3436 __ subl(edx, numerator);
3437 }
3438
3439 if (shift != 0) {
3440 __ sarl(edx, Immediate(shift));
3441 }
3442
3443 __ movl(eax, edx);
3444 __ shrl(edx, Immediate(31));
3445 __ addl(edx, eax);
3446
3447 if (instruction->IsRem()) {
3448 __ movl(eax, numerator);
3449 __ imull(edx, Immediate(imm));
3450 __ subl(eax, edx);
3451 __ movl(edx, eax);
3452 } else {
3453 __ movl(eax, edx);
3454 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003455 } else {
3456 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3457
3458 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3459
3460 CpuRegister rax = eax;
3461 CpuRegister rdx = edx;
3462
3463 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3464
3465 // Save the numerator.
3466 __ movq(numerator, rax);
3467
3468 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003469 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003470
3471 // RDX:RAX = magic * numerator
3472 __ imulq(numerator);
3473
3474 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003475 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003476 __ addq(rdx, numerator);
3477 } else if (imm < 0 && magic > 0) {
3478 // RDX -= numerator
3479 __ subq(rdx, numerator);
3480 }
3481
3482 // Shift if needed.
3483 if (shift != 0) {
3484 __ sarq(rdx, Immediate(shift));
3485 }
3486
3487 // RDX += 1 if RDX < 0
3488 __ movq(rax, rdx);
3489 __ shrq(rdx, Immediate(63));
3490 __ addq(rdx, rax);
3491
3492 if (instruction->IsRem()) {
3493 __ movq(rax, numerator);
3494
3495 if (IsInt<32>(imm)) {
3496 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3497 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003498 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003499 }
3500
3501 __ subq(rax, rdx);
3502 __ movq(rdx, rax);
3503 } else {
3504 __ movq(rax, rdx);
3505 }
3506 }
3507}
3508
Calin Juravlebacfec32014-11-14 15:54:36 +00003509void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3510 DCHECK(instruction->IsDiv() || instruction->IsRem());
3511 Primitive::Type type = instruction->GetResultType();
3512 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3513
3514 bool is_div = instruction->IsDiv();
3515 LocationSummary* locations = instruction->GetLocations();
3516
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003517 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3518 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003519
Roland Levillain271ab9c2014-11-27 15:23:57 +00003520 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003522
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003523 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003524 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003525
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003526 if (imm == 0) {
3527 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3528 } else if (imm == 1 || imm == -1) {
3529 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003530 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003531 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003532 } else {
3533 DCHECK(imm <= -2 || imm >= 2);
3534 GenerateDivRemWithAnyConstant(instruction);
3535 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003536 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003537 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003538 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003539 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003541
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003542 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3543 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3544 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3545 // so it's safe to just use negl instead of more complex comparisons.
3546 if (type == Primitive::kPrimInt) {
3547 __ cmpl(second_reg, Immediate(-1));
3548 __ j(kEqual, slow_path->GetEntryLabel());
3549 // edx:eax <- sign-extended of eax
3550 __ cdq();
3551 // eax = quotient, edx = remainder
3552 __ idivl(second_reg);
3553 } else {
3554 __ cmpq(second_reg, Immediate(-1));
3555 __ j(kEqual, slow_path->GetEntryLabel());
3556 // rdx:rax <- sign-extended of rax
3557 __ cqo();
3558 // rax = quotient, rdx = remainder
3559 __ idivq(second_reg);
3560 }
3561 __ Bind(slow_path->GetExitLabel());
3562 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003563}
3564
Calin Juravle7c4954d2014-10-28 16:57:40 +00003565void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3566 LocationSummary* locations =
3567 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3568 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003569 case Primitive::kPrimInt:
3570 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003571 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003572 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003573 locations->SetOut(Location::SameAsFirstInput());
3574 // Intel uses edx:eax as the dividend.
3575 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003576 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3577 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3578 // output and request another temp.
3579 if (div->InputAt(1)->IsConstant()) {
3580 locations->AddTemp(Location::RequiresRegister());
3581 }
Calin Juravled0d48522014-11-04 16:40:20 +00003582 break;
3583 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003584
Calin Juravle7c4954d2014-10-28 16:57:40 +00003585 case Primitive::kPrimFloat:
3586 case Primitive::kPrimDouble: {
3587 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003588 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003589 locations->SetOut(Location::SameAsFirstInput());
3590 break;
3591 }
3592
3593 default:
3594 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3595 }
3596}
3597
3598void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3599 LocationSummary* locations = div->GetLocations();
3600 Location first = locations->InAt(0);
3601 Location second = locations->InAt(1);
3602 DCHECK(first.Equals(locations->Out()));
3603
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003604 Primitive::Type type = div->GetResultType();
3605 switch (type) {
3606 case Primitive::kPrimInt:
3607 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003608 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003609 break;
3610 }
3611
Calin Juravle7c4954d2014-10-28 16:57:40 +00003612 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003613 if (second.IsFpuRegister()) {
3614 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3615 } else if (second.IsConstant()) {
3616 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003617 codegen_->LiteralFloatAddress(
3618 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003619 } else {
3620 DCHECK(second.IsStackSlot());
3621 __ divss(first.AsFpuRegister<XmmRegister>(),
3622 Address(CpuRegister(RSP), second.GetStackIndex()));
3623 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003624 break;
3625 }
3626
3627 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003628 if (second.IsFpuRegister()) {
3629 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3630 } else if (second.IsConstant()) {
3631 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003632 codegen_->LiteralDoubleAddress(
3633 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003634 } else {
3635 DCHECK(second.IsDoubleStackSlot());
3636 __ divsd(first.AsFpuRegister<XmmRegister>(),
3637 Address(CpuRegister(RSP), second.GetStackIndex()));
3638 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003639 break;
3640 }
3641
3642 default:
3643 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3644 }
3645}
3646
Calin Juravlebacfec32014-11-14 15:54:36 +00003647void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003648 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003649 LocationSummary* locations =
3650 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003651
3652 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003653 case Primitive::kPrimInt:
3654 case Primitive::kPrimLong: {
3655 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003656 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003657 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3658 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003659 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3660 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3661 // output and request another temp.
3662 if (rem->InputAt(1)->IsConstant()) {
3663 locations->AddTemp(Location::RequiresRegister());
3664 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003665 break;
3666 }
3667
3668 case Primitive::kPrimFloat:
3669 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003670 locations->SetInAt(0, Location::Any());
3671 locations->SetInAt(1, Location::Any());
3672 locations->SetOut(Location::RequiresFpuRegister());
3673 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003674 break;
3675 }
3676
3677 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003678 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003679 }
3680}
3681
3682void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3683 Primitive::Type type = rem->GetResultType();
3684 switch (type) {
3685 case Primitive::kPrimInt:
3686 case Primitive::kPrimLong: {
3687 GenerateDivRemIntegral(rem);
3688 break;
3689 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003690 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003691 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003692 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003693 break;
3694 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003695 default:
3696 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3697 }
3698}
3699
Calin Juravled0d48522014-11-04 16:40:20 +00003700void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003701 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3702 ? LocationSummary::kCallOnSlowPath
3703 : LocationSummary::kNoCall;
3704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003705 locations->SetInAt(0, Location::Any());
3706 if (instruction->HasUses()) {
3707 locations->SetOut(Location::SameAsFirstInput());
3708 }
3709}
3710
3711void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003712 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003713 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3714 codegen_->AddSlowPath(slow_path);
3715
3716 LocationSummary* locations = instruction->GetLocations();
3717 Location value = locations->InAt(0);
3718
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003719 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003720 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003721 case Primitive::kPrimByte:
3722 case Primitive::kPrimChar:
3723 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003724 case Primitive::kPrimInt: {
3725 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003726 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003727 __ j(kEqual, slow_path->GetEntryLabel());
3728 } else if (value.IsStackSlot()) {
3729 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3730 __ j(kEqual, slow_path->GetEntryLabel());
3731 } else {
3732 DCHECK(value.IsConstant()) << value;
3733 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3734 __ jmp(slow_path->GetEntryLabel());
3735 }
3736 }
3737 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003738 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003739 case Primitive::kPrimLong: {
3740 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003741 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003742 __ j(kEqual, slow_path->GetEntryLabel());
3743 } else if (value.IsDoubleStackSlot()) {
3744 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3745 __ j(kEqual, slow_path->GetEntryLabel());
3746 } else {
3747 DCHECK(value.IsConstant()) << value;
3748 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3749 __ jmp(slow_path->GetEntryLabel());
3750 }
3751 }
3752 break;
3753 }
3754 default:
3755 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003756 }
Calin Juravled0d48522014-11-04 16:40:20 +00003757}
3758
Calin Juravle9aec02f2014-11-18 23:06:35 +00003759void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3760 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3761
3762 LocationSummary* locations =
3763 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3764
3765 switch (op->GetResultType()) {
3766 case Primitive::kPrimInt:
3767 case Primitive::kPrimLong: {
3768 locations->SetInAt(0, Location::RequiresRegister());
3769 // The shift count needs to be in CL.
3770 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3771 locations->SetOut(Location::SameAsFirstInput());
3772 break;
3773 }
3774 default:
3775 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3776 }
3777}
3778
3779void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3780 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3781
3782 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003783 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003784 Location second = locations->InAt(1);
3785
3786 switch (op->GetResultType()) {
3787 case Primitive::kPrimInt: {
3788 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003789 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003790 if (op->IsShl()) {
3791 __ shll(first_reg, second_reg);
3792 } else if (op->IsShr()) {
3793 __ sarl(first_reg, second_reg);
3794 } else {
3795 __ shrl(first_reg, second_reg);
3796 }
3797 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003798 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003799 if (op->IsShl()) {
3800 __ shll(first_reg, imm);
3801 } else if (op->IsShr()) {
3802 __ sarl(first_reg, imm);
3803 } else {
3804 __ shrl(first_reg, imm);
3805 }
3806 }
3807 break;
3808 }
3809 case Primitive::kPrimLong: {
3810 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003811 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003812 if (op->IsShl()) {
3813 __ shlq(first_reg, second_reg);
3814 } else if (op->IsShr()) {
3815 __ sarq(first_reg, second_reg);
3816 } else {
3817 __ shrq(first_reg, second_reg);
3818 }
3819 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003820 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003821 if (op->IsShl()) {
3822 __ shlq(first_reg, imm);
3823 } else if (op->IsShr()) {
3824 __ sarq(first_reg, imm);
3825 } else {
3826 __ shrq(first_reg, imm);
3827 }
3828 }
3829 break;
3830 }
3831 default:
3832 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003833 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003834 }
3835}
3836
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003837void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3838 LocationSummary* locations =
3839 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3840
3841 switch (ror->GetResultType()) {
3842 case Primitive::kPrimInt:
3843 case Primitive::kPrimLong: {
3844 locations->SetInAt(0, Location::RequiresRegister());
3845 // The shift count needs to be in CL (unless it is a constant).
3846 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3847 locations->SetOut(Location::SameAsFirstInput());
3848 break;
3849 }
3850 default:
3851 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3852 UNREACHABLE();
3853 }
3854}
3855
3856void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
3857 LocationSummary* locations = ror->GetLocations();
3858 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
3859 Location second = locations->InAt(1);
3860
3861 switch (ror->GetResultType()) {
3862 case Primitive::kPrimInt:
3863 if (second.IsRegister()) {
3864 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3865 __ rorl(first_reg, second_reg);
3866 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003867 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003868 __ rorl(first_reg, imm);
3869 }
3870 break;
3871 case Primitive::kPrimLong:
3872 if (second.IsRegister()) {
3873 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3874 __ rorq(first_reg, second_reg);
3875 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003876 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003877 __ rorq(first_reg, imm);
3878 }
3879 break;
3880 default:
3881 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3882 UNREACHABLE();
3883 }
3884}
3885
Calin Juravle9aec02f2014-11-18 23:06:35 +00003886void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3887 HandleShift(shl);
3888}
3889
3890void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3891 HandleShift(shl);
3892}
3893
3894void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3895 HandleShift(shr);
3896}
3897
3898void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3899 HandleShift(shr);
3900}
3901
3902void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3903 HandleShift(ushr);
3904}
3905
3906void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3907 HandleShift(ushr);
3908}
3909
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003910void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003911 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003912 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003913 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003914 if (instruction->IsStringAlloc()) {
3915 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3916 } else {
3917 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3918 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3919 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003920 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003921}
3922
3923void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003924 // Note: if heap poisoning is enabled, the entry point takes cares
3925 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003926 if (instruction->IsStringAlloc()) {
3927 // String is allocated through StringFactory. Call NewEmptyString entry point.
3928 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
3929 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize);
3930 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
3931 __ call(Address(temp, code_offset.SizeValue()));
3932 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3933 } else {
3934 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3935 instruction,
3936 instruction->GetDexPc(),
3937 nullptr);
3938 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3939 DCHECK(!codegen_->IsLeafMethod());
3940 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003941}
3942
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003943void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3944 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003945 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003946 InvokeRuntimeCallingConvention calling_convention;
3947 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003948 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003949 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003950 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003951}
3952
3953void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3954 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003955 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3956 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003957 // Note: if heap poisoning is enabled, the entry point takes cares
3958 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003959 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3960 instruction,
3961 instruction->GetDexPc(),
3962 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003963 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003964
3965 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003966}
3967
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003968void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003969 LocationSummary* locations =
3970 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003971 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3972 if (location.IsStackSlot()) {
3973 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3974 } else if (location.IsDoubleStackSlot()) {
3975 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3976 }
3977 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003978}
3979
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003980void InstructionCodeGeneratorX86_64::VisitParameterValue(
3981 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003982 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003983}
3984
3985void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3986 LocationSummary* locations =
3987 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3988 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3989}
3990
3991void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3992 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3993 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003994}
3995
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003996void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
3997 LocationSummary* locations =
3998 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3999 locations->SetInAt(0, Location::RequiresRegister());
4000 locations->SetOut(Location::RequiresRegister());
4001}
4002
4003void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4004 LocationSummary* locations = instruction->GetLocations();
4005 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004006 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004007 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4008 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
4009 } else {
Nicolas Geoffray88f288e2016-06-29 08:17:52 +00004010 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4011 instruction->GetIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004012 }
4013 __ movq(locations->Out().AsRegister<CpuRegister>(),
4014 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
4015}
4016
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004017void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004018 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004019 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004020 locations->SetInAt(0, Location::RequiresRegister());
4021 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004022}
4023
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004024void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4025 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004026 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4027 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004028 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004029 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004030 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004031 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004032 break;
4033
4034 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004035 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004036 break;
4037
4038 default:
4039 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4040 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004041}
4042
David Brazdil66d126e2015-04-03 16:02:44 +01004043void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4044 LocationSummary* locations =
4045 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4046 locations->SetInAt(0, Location::RequiresRegister());
4047 locations->SetOut(Location::SameAsFirstInput());
4048}
4049
4050void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004051 LocationSummary* locations = bool_not->GetLocations();
4052 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4053 locations->Out().AsRegister<CpuRegister>().AsRegister());
4054 Location out = locations->Out();
4055 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4056}
4057
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004058void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004059 LocationSummary* locations =
4060 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004061 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004062 locations->SetInAt(i, Location::Any());
4063 }
4064 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004065}
4066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004067void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004068 LOG(FATAL) << "Unimplemented";
4069}
4070
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004071void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004072 /*
4073 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004074 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004075 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4076 */
4077 switch (kind) {
4078 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004079 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004080 break;
4081 }
4082 case MemBarrierKind::kAnyStore:
4083 case MemBarrierKind::kLoadAny:
4084 case MemBarrierKind::kStoreStore: {
4085 // nop
4086 break;
4087 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004088 case MemBarrierKind::kNTStoreStore:
4089 // Non-Temporal Store/Store needs an explicit fence.
4090 MemoryFence(/* non-temporal */ true);
4091 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004092 }
4093}
4094
4095void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4096 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4097
Roland Levillain0d5a2812015-11-13 10:07:31 +00004098 bool object_field_get_with_read_barrier =
4099 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004100 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004101 new (GetGraph()->GetArena()) LocationSummary(instruction,
4102 object_field_get_with_read_barrier ?
4103 LocationSummary::kCallOnSlowPath :
4104 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004105 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004106 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4107 locations->SetOut(Location::RequiresFpuRegister());
4108 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004109 // The output overlaps for an object field get when read barriers
4110 // are enabled: we do not want the move to overwrite the object's
4111 // location, as we need it to emit the read barrier.
4112 locations->SetOut(
4113 Location::RequiresRegister(),
4114 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004115 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004116 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4117 // We need a temporary register for the read barrier marking slow
4118 // path in CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier.
4119 locations->AddTemp(Location::RequiresRegister());
4120 }
Calin Juravle52c48962014-12-16 17:02:57 +00004121}
4122
4123void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4124 const FieldInfo& field_info) {
4125 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4126
4127 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004128 Location base_loc = locations->InAt(0);
4129 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004130 Location out = locations->Out();
4131 bool is_volatile = field_info.IsVolatile();
4132 Primitive::Type field_type = field_info.GetFieldType();
4133 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4134
4135 switch (field_type) {
4136 case Primitive::kPrimBoolean: {
4137 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4138 break;
4139 }
4140
4141 case Primitive::kPrimByte: {
4142 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4143 break;
4144 }
4145
4146 case Primitive::kPrimShort: {
4147 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4148 break;
4149 }
4150
4151 case Primitive::kPrimChar: {
4152 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4153 break;
4154 }
4155
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004156 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004157 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4158 break;
4159 }
4160
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004161 case Primitive::kPrimNot: {
4162 // /* HeapReference<Object> */ out = *(base + offset)
4163 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4164 Location temp_loc = locations->GetTemp(0);
4165 // Note that a potential implicit null check is handled in this
4166 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4167 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4168 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4169 if (is_volatile) {
4170 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4171 }
4172 } else {
4173 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4174 codegen_->MaybeRecordImplicitNullCheck(instruction);
4175 if (is_volatile) {
4176 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4177 }
4178 // If read barriers are enabled, emit read barriers other than
4179 // Baker's using a slow path (and also unpoison the loaded
4180 // reference, if heap poisoning is enabled).
4181 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4182 }
4183 break;
4184 }
4185
Calin Juravle52c48962014-12-16 17:02:57 +00004186 case Primitive::kPrimLong: {
4187 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4188 break;
4189 }
4190
4191 case Primitive::kPrimFloat: {
4192 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4193 break;
4194 }
4195
4196 case Primitive::kPrimDouble: {
4197 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4198 break;
4199 }
4200
4201 case Primitive::kPrimVoid:
4202 LOG(FATAL) << "Unreachable type " << field_type;
4203 UNREACHABLE();
4204 }
4205
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004206 if (field_type == Primitive::kPrimNot) {
4207 // Potential implicit null checks, in the case of reference
4208 // fields, are handled in the previous switch statement.
4209 } else {
4210 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004211 }
Roland Levillain4d027112015-07-01 15:41:14 +01004212
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004213 if (is_volatile) {
4214 if (field_type == Primitive::kPrimNot) {
4215 // Memory barriers, in the case of references, are also handled
4216 // in the previous switch statement.
4217 } else {
4218 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4219 }
Roland Levillain4d027112015-07-01 15:41:14 +01004220 }
Calin Juravle52c48962014-12-16 17:02:57 +00004221}
4222
4223void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4224 const FieldInfo& field_info) {
4225 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4226
4227 LocationSummary* locations =
4228 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004229 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004230 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004231 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004232 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004233
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004234 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004235 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004236 if (is_volatile) {
4237 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4238 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4239 } else {
4240 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4241 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004242 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004243 if (is_volatile) {
4244 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4245 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4246 } else {
4247 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4248 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004249 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004250 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004251 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004252 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004253 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004254 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4255 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004256 locations->AddTemp(Location::RequiresRegister());
4257 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004258}
4259
Calin Juravle52c48962014-12-16 17:02:57 +00004260void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004261 const FieldInfo& field_info,
4262 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004263 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4264
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004265 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004266 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4267 Location value = locations->InAt(1);
4268 bool is_volatile = field_info.IsVolatile();
4269 Primitive::Type field_type = field_info.GetFieldType();
4270 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4271
4272 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004273 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004274 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004275
Mark Mendellea5af682015-10-22 17:35:49 -04004276 bool maybe_record_implicit_null_check_done = false;
4277
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004278 switch (field_type) {
4279 case Primitive::kPrimBoolean:
4280 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004281 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004282 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004283 __ movb(Address(base, offset), Immediate(v));
4284 } else {
4285 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4286 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004287 break;
4288 }
4289
4290 case Primitive::kPrimShort:
4291 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004292 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004293 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004294 __ movw(Address(base, offset), Immediate(v));
4295 } else {
4296 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4297 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004298 break;
4299 }
4300
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004301 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004302 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004303 if (value.IsConstant()) {
4304 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004305 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4306 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4307 // Note: if heap poisoning is enabled, no need to poison
4308 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004309 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004310 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004311 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4312 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4313 __ movl(temp, value.AsRegister<CpuRegister>());
4314 __ PoisonHeapReference(temp);
4315 __ movl(Address(base, offset), temp);
4316 } else {
4317 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4318 }
Mark Mendell40741f32015-04-20 22:10:34 -04004319 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004320 break;
4321 }
4322
4323 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004324 if (value.IsConstant()) {
4325 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004326 codegen_->MoveInt64ToAddress(Address(base, offset),
4327 Address(base, offset + sizeof(int32_t)),
4328 v,
4329 instruction);
4330 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004331 } else {
4332 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4333 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004334 break;
4335 }
4336
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004337 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004338 if (value.IsConstant()) {
4339 int32_t v =
4340 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4341 __ movl(Address(base, offset), Immediate(v));
4342 } else {
4343 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4344 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004345 break;
4346 }
4347
4348 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004349 if (value.IsConstant()) {
4350 int64_t v =
4351 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4352 codegen_->MoveInt64ToAddress(Address(base, offset),
4353 Address(base, offset + sizeof(int32_t)),
4354 v,
4355 instruction);
4356 maybe_record_implicit_null_check_done = true;
4357 } else {
4358 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4359 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004360 break;
4361 }
4362
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363 case Primitive::kPrimVoid:
4364 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004365 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004366 }
Calin Juravle52c48962014-12-16 17:02:57 +00004367
Mark Mendellea5af682015-10-22 17:35:49 -04004368 if (!maybe_record_implicit_null_check_done) {
4369 codegen_->MaybeRecordImplicitNullCheck(instruction);
4370 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004371
4372 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4373 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4374 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004375 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004376 }
4377
Calin Juravle52c48962014-12-16 17:02:57 +00004378 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004379 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004380 }
4381}
4382
4383void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4384 HandleFieldSet(instruction, instruction->GetFieldInfo());
4385}
4386
4387void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004388 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004389}
4390
4391void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004392 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004393}
4394
4395void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004396 HandleFieldGet(instruction, instruction->GetFieldInfo());
4397}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004398
Calin Juravle52c48962014-12-16 17:02:57 +00004399void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4400 HandleFieldGet(instruction);
4401}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004402
Calin Juravle52c48962014-12-16 17:02:57 +00004403void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4404 HandleFieldGet(instruction, instruction->GetFieldInfo());
4405}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004406
Calin Juravle52c48962014-12-16 17:02:57 +00004407void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4408 HandleFieldSet(instruction, instruction->GetFieldInfo());
4409}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004410
Calin Juravle52c48962014-12-16 17:02:57 +00004411void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004412 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004413}
4414
Calin Juravlee460d1d2015-09-29 04:52:17 +01004415void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4416 HUnresolvedInstanceFieldGet* instruction) {
4417 FieldAccessCallingConventionX86_64 calling_convention;
4418 codegen_->CreateUnresolvedFieldLocationSummary(
4419 instruction, instruction->GetFieldType(), calling_convention);
4420}
4421
4422void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4423 HUnresolvedInstanceFieldGet* instruction) {
4424 FieldAccessCallingConventionX86_64 calling_convention;
4425 codegen_->GenerateUnresolvedFieldAccess(instruction,
4426 instruction->GetFieldType(),
4427 instruction->GetFieldIndex(),
4428 instruction->GetDexPc(),
4429 calling_convention);
4430}
4431
4432void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4433 HUnresolvedInstanceFieldSet* instruction) {
4434 FieldAccessCallingConventionX86_64 calling_convention;
4435 codegen_->CreateUnresolvedFieldLocationSummary(
4436 instruction, instruction->GetFieldType(), calling_convention);
4437}
4438
4439void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4440 HUnresolvedInstanceFieldSet* instruction) {
4441 FieldAccessCallingConventionX86_64 calling_convention;
4442 codegen_->GenerateUnresolvedFieldAccess(instruction,
4443 instruction->GetFieldType(),
4444 instruction->GetFieldIndex(),
4445 instruction->GetDexPc(),
4446 calling_convention);
4447}
4448
4449void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4450 HUnresolvedStaticFieldGet* instruction) {
4451 FieldAccessCallingConventionX86_64 calling_convention;
4452 codegen_->CreateUnresolvedFieldLocationSummary(
4453 instruction, instruction->GetFieldType(), calling_convention);
4454}
4455
4456void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4457 HUnresolvedStaticFieldGet* instruction) {
4458 FieldAccessCallingConventionX86_64 calling_convention;
4459 codegen_->GenerateUnresolvedFieldAccess(instruction,
4460 instruction->GetFieldType(),
4461 instruction->GetFieldIndex(),
4462 instruction->GetDexPc(),
4463 calling_convention);
4464}
4465
4466void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4467 HUnresolvedStaticFieldSet* instruction) {
4468 FieldAccessCallingConventionX86_64 calling_convention;
4469 codegen_->CreateUnresolvedFieldLocationSummary(
4470 instruction, instruction->GetFieldType(), calling_convention);
4471}
4472
4473void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4474 HUnresolvedStaticFieldSet* instruction) {
4475 FieldAccessCallingConventionX86_64 calling_convention;
4476 codegen_->GenerateUnresolvedFieldAccess(instruction,
4477 instruction->GetFieldType(),
4478 instruction->GetFieldIndex(),
4479 instruction->GetDexPc(),
4480 calling_convention);
4481}
4482
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004483void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004484 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4485 ? LocationSummary::kCallOnSlowPath
4486 : LocationSummary::kNoCall;
4487 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4488 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004489 ? Location::RequiresRegister()
4490 : Location::Any();
4491 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004492 if (instruction->HasUses()) {
4493 locations->SetOut(Location::SameAsFirstInput());
4494 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004495}
4496
Calin Juravle2ae48182016-03-16 14:05:09 +00004497void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4498 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004499 return;
4500 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004501 LocationSummary* locations = instruction->GetLocations();
4502 Location obj = locations->InAt(0);
4503
4504 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004505 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004506}
4507
Calin Juravle2ae48182016-03-16 14:05:09 +00004508void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004509 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004510 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004511
4512 LocationSummary* locations = instruction->GetLocations();
4513 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004514
4515 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004516 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004517 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004518 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004519 } else {
4520 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004521 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004522 __ jmp(slow_path->GetEntryLabel());
4523 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004524 }
4525 __ j(kEqual, slow_path->GetEntryLabel());
4526}
4527
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004528void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004529 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004530}
4531
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004532void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004533 bool object_array_get_with_read_barrier =
4534 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004535 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004536 new (GetGraph()->GetArena()) LocationSummary(instruction,
4537 object_array_get_with_read_barrier ?
4538 LocationSummary::kCallOnSlowPath :
4539 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004540 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004541 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004542 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4543 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4544 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004545 // The output overlaps for an object array get when read barriers
4546 // are enabled: we do not want the move to overwrite the array's
4547 // location, as we need it to emit the read barrier.
4548 locations->SetOut(
4549 Location::RequiresRegister(),
4550 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004551 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004552 // We need a temporary register for the read barrier marking slow
4553 // path in CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
4554 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4555 locations->AddTemp(Location::RequiresRegister());
4556 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004557}
4558
4559void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4560 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004561 Location obj_loc = locations->InAt(0);
4562 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004563 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004564 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004565 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004566
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004567 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004568 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004569 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004570 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004571 if (index.IsConstant()) {
4572 __ movzxb(out, Address(obj,
4573 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4574 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004575 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 }
4577 break;
4578 }
4579
4580 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004581 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004582 if (index.IsConstant()) {
4583 __ movsxb(out, Address(obj,
4584 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4585 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004586 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004587 }
4588 break;
4589 }
4590
4591 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004592 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004593 if (index.IsConstant()) {
4594 __ movsxw(out, Address(obj,
4595 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4596 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004597 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598 }
4599 break;
4600 }
4601
4602 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004603 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004604 if (index.IsConstant()) {
4605 __ movzxw(out, Address(obj,
4606 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4607 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004608 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004609 }
4610 break;
4611 }
4612
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004613 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004614 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004615 if (index.IsConstant()) {
4616 __ movl(out, Address(obj,
4617 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4618 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004619 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004620 }
4621 break;
4622 }
4623
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004624 case Primitive::kPrimNot: {
4625 static_assert(
4626 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4627 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004628 // /* HeapReference<Object> */ out =
4629 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4630 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4631 Location temp = locations->GetTemp(0);
4632 // Note that a potential implicit null check is handled in this
4633 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4634 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4635 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4636 } else {
4637 CpuRegister out = out_loc.AsRegister<CpuRegister>();
4638 if (index.IsConstant()) {
4639 uint32_t offset =
4640 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4641 __ movl(out, Address(obj, offset));
4642 codegen_->MaybeRecordImplicitNullCheck(instruction);
4643 // If read barriers are enabled, emit read barriers other than
4644 // Baker's using a slow path (and also unpoison the loaded
4645 // reference, if heap poisoning is enabled).
4646 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4647 } else {
4648 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
4649 codegen_->MaybeRecordImplicitNullCheck(instruction);
4650 // If read barriers are enabled, emit read barriers other than
4651 // Baker's using a slow path (and also unpoison the loaded
4652 // reference, if heap poisoning is enabled).
4653 codegen_->MaybeGenerateReadBarrierSlow(
4654 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4655 }
4656 }
4657 break;
4658 }
4659
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004660 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004661 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004662 if (index.IsConstant()) {
4663 __ movq(out, Address(obj,
4664 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4665 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004666 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667 }
4668 break;
4669 }
4670
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004671 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004672 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004673 if (index.IsConstant()) {
4674 __ movss(out, Address(obj,
4675 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4676 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004677 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004678 }
4679 break;
4680 }
4681
4682 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004683 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004684 if (index.IsConstant()) {
4685 __ movsd(out, Address(obj,
4686 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4687 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004688 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004689 }
4690 break;
4691 }
4692
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004693 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004694 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004695 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004696 }
Roland Levillain4d027112015-07-01 15:41:14 +01004697
4698 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004699 // Potential implicit null checks, in the case of reference
4700 // arrays, are handled in the previous switch statement.
4701 } else {
4702 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004703 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704}
4705
4706void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004707 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004708
4709 bool needs_write_barrier =
4710 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004711 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004712 bool object_array_set_with_read_barrier =
4713 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004714
Nicolas Geoffray39468442014-09-02 15:17:15 +01004715 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004716 instruction,
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004717 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004718 LocationSummary::kCallOnSlowPath :
4719 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004720
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004721 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004722 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4723 if (Primitive::IsFloatingPointType(value_type)) {
4724 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004725 } else {
4726 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4727 }
4728
4729 if (needs_write_barrier) {
4730 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004731
4732 // This first temporary register is possibly used for heap
4733 // reference poisoning and/or read barrier emission too.
4734 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004735 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004736 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737}
4738
4739void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4740 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004741 Location array_loc = locations->InAt(0);
4742 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004743 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004744 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004745 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004746 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004747 bool needs_write_barrier =
4748 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004749 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4750 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4751 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004752
4753 switch (value_type) {
4754 case Primitive::kPrimBoolean:
4755 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004756 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4757 Address address = index.IsConstant()
4758 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4759 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4760 if (value.IsRegister()) {
4761 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004762 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004763 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004764 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004765 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004766 break;
4767 }
4768
4769 case Primitive::kPrimShort:
4770 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004771 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4772 Address address = index.IsConstant()
4773 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4774 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4775 if (value.IsRegister()) {
4776 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004777 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004778 DCHECK(value.IsConstant()) << value;
4779 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004780 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004781 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004782 break;
4783 }
4784
4785 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004786 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4787 Address address = index.IsConstant()
4788 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4789 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004790
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004791 if (!value.IsRegister()) {
4792 // Just setting null.
4793 DCHECK(instruction->InputAt(2)->IsNullConstant());
4794 DCHECK(value.IsConstant()) << value;
4795 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004796 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004797 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004798 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004800 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004801
4802 DCHECK(needs_write_barrier);
4803 CpuRegister register_value = value.AsRegister<CpuRegister>();
4804 NearLabel done, not_null, do_put;
4805 SlowPathCode* slow_path = nullptr;
4806 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004807 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004808 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4809 codegen_->AddSlowPath(slow_path);
4810 if (instruction->GetValueCanBeNull()) {
4811 __ testl(register_value, register_value);
4812 __ j(kNotEqual, &not_null);
4813 __ movl(address, Immediate(0));
4814 codegen_->MaybeRecordImplicitNullCheck(instruction);
4815 __ jmp(&done);
4816 __ Bind(&not_null);
4817 }
4818
Roland Levillain0d5a2812015-11-13 10:07:31 +00004819 if (kEmitCompilerReadBarrier) {
4820 // When read barriers are enabled, the type checking
4821 // instrumentation requires two read barriers:
4822 //
4823 // __ movl(temp2, temp);
4824 // // /* HeapReference<Class> */ temp = temp->component_type_
4825 // __ movl(temp, Address(temp, component_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004826 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004827 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4828 //
4829 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4830 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004831 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00004832 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4833 //
4834 // __ cmpl(temp, temp2);
4835 //
4836 // However, the second read barrier may trash `temp`, as it
4837 // is a temporary register, and as such would not be saved
4838 // along with live registers before calling the runtime (nor
4839 // restored afterwards). So in this case, we bail out and
4840 // delegate the work to the array set slow path.
4841 //
4842 // TODO: Extend the register allocator to support a new
4843 // "(locally) live temp" location so as to avoid always
4844 // going into the slow path when read barriers are enabled.
4845 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004847 // /* HeapReference<Class> */ temp = array->klass_
4848 __ movl(temp, Address(array, class_offset));
4849 codegen_->MaybeRecordImplicitNullCheck(instruction);
4850 __ MaybeUnpoisonHeapReference(temp);
4851
4852 // /* HeapReference<Class> */ temp = temp->component_type_
4853 __ movl(temp, Address(temp, component_offset));
4854 // If heap poisoning is enabled, no need to unpoison `temp`
4855 // nor the object reference in `register_value->klass`, as
4856 // we are comparing two poisoned references.
4857 __ cmpl(temp, Address(register_value, class_offset));
4858
4859 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4860 __ j(kEqual, &do_put);
4861 // If heap poisoning is enabled, the `temp` reference has
4862 // not been unpoisoned yet; unpoison it now.
4863 __ MaybeUnpoisonHeapReference(temp);
4864
4865 // /* HeapReference<Class> */ temp = temp->super_class_
4866 __ movl(temp, Address(temp, super_offset));
4867 // If heap poisoning is enabled, no need to unpoison
4868 // `temp`, as we are comparing against null below.
4869 __ testl(temp, temp);
4870 __ j(kNotEqual, slow_path->GetEntryLabel());
4871 __ Bind(&do_put);
4872 } else {
4873 __ j(kNotEqual, slow_path->GetEntryLabel());
4874 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004875 }
4876 }
4877
4878 if (kPoisonHeapReferences) {
4879 __ movl(temp, register_value);
4880 __ PoisonHeapReference(temp);
4881 __ movl(address, temp);
4882 } else {
4883 __ movl(address, register_value);
4884 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004885 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004886 codegen_->MaybeRecordImplicitNullCheck(instruction);
4887 }
4888
4889 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4890 codegen_->MarkGCCard(
4891 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4892 __ Bind(&done);
4893
4894 if (slow_path != nullptr) {
4895 __ Bind(slow_path->GetExitLabel());
4896 }
4897
4898 break;
4899 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004900
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004901 case Primitive::kPrimInt: {
4902 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4903 Address address = index.IsConstant()
4904 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4905 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4906 if (value.IsRegister()) {
4907 __ movl(address, value.AsRegister<CpuRegister>());
4908 } else {
4909 DCHECK(value.IsConstant()) << value;
4910 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4911 __ movl(address, Immediate(v));
4912 }
4913 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004914 break;
4915 }
4916
4917 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004918 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4919 Address address = index.IsConstant()
4920 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4921 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4922 if (value.IsRegister()) {
4923 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004924 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004925 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004926 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004927 Address address_high = index.IsConstant()
4928 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4929 offset + sizeof(int32_t))
4930 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4931 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004932 }
4933 break;
4934 }
4935
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004936 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004937 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4938 Address address = index.IsConstant()
4939 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4940 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004941 if (value.IsFpuRegister()) {
4942 __ movss(address, value.AsFpuRegister<XmmRegister>());
4943 } else {
4944 DCHECK(value.IsConstant());
4945 int32_t v =
4946 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4947 __ movl(address, Immediate(v));
4948 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004949 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004950 break;
4951 }
4952
4953 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004954 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4955 Address address = index.IsConstant()
4956 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4957 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004958 if (value.IsFpuRegister()) {
4959 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4960 codegen_->MaybeRecordImplicitNullCheck(instruction);
4961 } else {
4962 int64_t v =
4963 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4964 Address address_high = index.IsConstant()
4965 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4966 offset + sizeof(int32_t))
4967 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4968 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4969 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004970 break;
4971 }
4972
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004973 case Primitive::kPrimVoid:
4974 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004975 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004976 }
4977}
4978
4979void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004980 LocationSummary* locations =
4981 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004982 locations->SetInAt(0, Location::RequiresRegister());
4983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004984}
4985
4986void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4987 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004988 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004989 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4990 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004991 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004992 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004993}
4994
4995void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004996 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4997 ? LocationSummary::kCallOnSlowPath
4998 : LocationSummary::kNoCall;
4999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005000 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005001 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005002 if (instruction->HasUses()) {
5003 locations->SetOut(Location::SameAsFirstInput());
5004 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005005}
5006
5007void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5008 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005009 Location index_loc = locations->InAt(0);
5010 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005011 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005012 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005013
Mark Mendell99dbd682015-04-22 16:18:52 -04005014 if (length_loc.IsConstant()) {
5015 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5016 if (index_loc.IsConstant()) {
5017 // BCE will remove the bounds check if we are guarenteed to pass.
5018 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5019 if (index < 0 || index >= length) {
5020 codegen_->AddSlowPath(slow_path);
5021 __ jmp(slow_path->GetEntryLabel());
5022 } else {
5023 // Some optimization after BCE may have generated this, and we should not
5024 // generate a bounds check if it is a valid range.
5025 }
5026 return;
5027 }
5028
5029 // We have to reverse the jump condition because the length is the constant.
5030 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5031 __ cmpl(index_reg, Immediate(length));
5032 codegen_->AddSlowPath(slow_path);
5033 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005034 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005035 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5036 if (index_loc.IsConstant()) {
5037 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5038 __ cmpl(length, Immediate(value));
5039 } else {
5040 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5041 }
5042 codegen_->AddSlowPath(slow_path);
5043 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005044 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005045}
5046
5047void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5048 CpuRegister card,
5049 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005050 CpuRegister value,
5051 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005052 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005053 if (value_can_be_null) {
5054 __ testl(value, value);
5055 __ j(kEqual, &is_null);
5056 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005057 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64WordSize>().Int32Value(),
5058 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005059 __ movq(temp, object);
5060 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005061 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005062 if (value_can_be_null) {
5063 __ Bind(&is_null);
5064 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005065}
5066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005067void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005068 LOG(FATAL) << "Unimplemented";
5069}
5070
5071void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005072 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5073}
5074
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005075void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5076 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5077}
5078
5079void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005080 HBasicBlock* block = instruction->GetBlock();
5081 if (block->GetLoopInformation() != nullptr) {
5082 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5083 // The back edge will generate the suspend check.
5084 return;
5085 }
5086 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5087 // The goto will generate the suspend check.
5088 return;
5089 }
5090 GenerateSuspendCheck(instruction, nullptr);
5091}
5092
5093void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5094 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005095 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005096 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5097 if (slow_path == nullptr) {
5098 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5099 instruction->SetSlowPath(slow_path);
5100 codegen_->AddSlowPath(slow_path);
5101 if (successor != nullptr) {
5102 DCHECK(successor->IsLoopHeader());
5103 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5104 }
5105 } else {
5106 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5107 }
5108
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005109 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(),
5110 /* no_rip */ true),
5111 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005112 if (successor == nullptr) {
5113 __ j(kNotEqual, slow_path->GetEntryLabel());
5114 __ Bind(slow_path->GetReturnLabel());
5115 } else {
5116 __ j(kEqual, codegen_->GetLabelOf(successor));
5117 __ jmp(slow_path->GetEntryLabel());
5118 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005119}
5120
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005121X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5122 return codegen_->GetAssembler();
5123}
5124
5125void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005126 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005127 Location source = move->GetSource();
5128 Location destination = move->GetDestination();
5129
5130 if (source.IsRegister()) {
5131 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005132 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005133 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005134 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005135 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005136 } else {
5137 DCHECK(destination.IsDoubleStackSlot());
5138 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005139 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005140 }
5141 } else if (source.IsStackSlot()) {
5142 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005143 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005144 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005145 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005146 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005147 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005148 } else {
5149 DCHECK(destination.IsStackSlot());
5150 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5151 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5152 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005153 } else if (source.IsDoubleStackSlot()) {
5154 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005155 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005156 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005157 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005158 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5159 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005160 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005161 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005162 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5163 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5164 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005165 } else if (source.IsConstant()) {
5166 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005167 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5168 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005169 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005170 if (value == 0) {
5171 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5172 } else {
5173 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5174 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005175 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005176 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005177 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005178 }
5179 } else if (constant->IsLongConstant()) {
5180 int64_t value = constant->AsLongConstant()->GetValue();
5181 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005182 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005183 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005184 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005185 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005186 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005187 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005188 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005189 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005190 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005191 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005192 } else {
5193 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005194 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005195 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5196 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005197 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005198 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005199 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005200 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005201 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005202 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005203 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005204 } else {
5205 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005206 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005207 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005208 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005209 } else if (source.IsFpuRegister()) {
5210 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005211 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005212 } else if (destination.IsStackSlot()) {
5213 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005214 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005215 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005216 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005217 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005218 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005219 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005220 }
5221}
5222
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005223void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005224 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005225 __ movl(Address(CpuRegister(RSP), mem), reg);
5226 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005227}
5228
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005229void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005230 ScratchRegisterScope ensure_scratch(
5231 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5232
5233 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5234 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5235 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5236 Address(CpuRegister(RSP), mem2 + stack_offset));
5237 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5238 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5239 CpuRegister(ensure_scratch.GetRegister()));
5240}
5241
Mark Mendell8a1c7282015-06-29 15:41:28 -04005242void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5243 __ movq(CpuRegister(TMP), reg1);
5244 __ movq(reg1, reg2);
5245 __ movq(reg2, CpuRegister(TMP));
5246}
5247
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005248void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5249 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5250 __ movq(Address(CpuRegister(RSP), mem), reg);
5251 __ movq(reg, CpuRegister(TMP));
5252}
5253
5254void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5255 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005256 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005257
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005258 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5259 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5260 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5261 Address(CpuRegister(RSP), mem2 + stack_offset));
5262 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5263 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5264 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005265}
5266
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005267void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5268 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5269 __ movss(Address(CpuRegister(RSP), mem), reg);
5270 __ movd(reg, CpuRegister(TMP));
5271}
5272
5273void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5274 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5275 __ movsd(Address(CpuRegister(RSP), mem), reg);
5276 __ movd(reg, CpuRegister(TMP));
5277}
5278
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005279void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005280 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005281 Location source = move->GetSource();
5282 Location destination = move->GetDestination();
5283
5284 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005285 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005286 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005287 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005288 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005289 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005290 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005291 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5292 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005293 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005294 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005295 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005296 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5297 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005299 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5300 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5301 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005302 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005303 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005304 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005305 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005306 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005307 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005308 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005309 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005310 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005311 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005312 }
5313}
5314
5315
5316void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5317 __ pushq(CpuRegister(reg));
5318}
5319
5320
5321void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5322 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005323}
5324
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005325void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005326 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005327 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5328 Immediate(mirror::Class::kStatusInitialized));
5329 __ j(kLess, slow_path->GetEntryLabel());
5330 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005331 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005332}
5333
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005334HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5335 HLoadClass::LoadKind desired_class_load_kind) {
5336 if (kEmitCompilerReadBarrier) {
5337 switch (desired_class_load_kind) {
5338 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5339 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5340 case HLoadClass::LoadKind::kBootImageAddress:
5341 // TODO: Implement for read barrier.
5342 return HLoadClass::LoadKind::kDexCacheViaMethod;
5343 default:
5344 break;
5345 }
5346 }
5347 switch (desired_class_load_kind) {
5348 case HLoadClass::LoadKind::kReferrersClass:
5349 break;
5350 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5351 DCHECK(!GetCompilerOptions().GetCompilePic());
5352 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5353 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5354 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5355 DCHECK(GetCompilerOptions().GetCompilePic());
5356 break;
5357 case HLoadClass::LoadKind::kBootImageAddress:
5358 break;
5359 case HLoadClass::LoadKind::kDexCacheAddress:
5360 DCHECK(Runtime::Current()->UseJitCompilation());
5361 break;
5362 case HLoadClass::LoadKind::kDexCachePcRelative:
5363 DCHECK(!Runtime::Current()->UseJitCompilation());
5364 break;
5365 case HLoadClass::LoadKind::kDexCacheViaMethod:
5366 break;
5367 }
5368 return desired_class_load_kind;
5369}
5370
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005371void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005372 if (cls->NeedsAccessCheck()) {
5373 InvokeRuntimeCallingConvention calling_convention;
5374 CodeGenerator::CreateLoadClassLocationSummary(
5375 cls,
5376 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5377 Location::RegisterLocation(RAX),
5378 /* code_generator_supports_read_barrier */ true);
5379 return;
5380 }
5381
5382 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5383 ? LocationSummary::kCallOnSlowPath
5384 : LocationSummary::kNoCall;
5385 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5386 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5387 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5388 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5389 locations->SetInAt(0, Location::RequiresRegister());
5390 }
5391 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005392}
5393
5394void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005395 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005396 if (cls->NeedsAccessCheck()) {
5397 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5398 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5399 cls,
5400 cls->GetDexPc(),
5401 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005402 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005403 return;
5404 }
5405
Roland Levillain0d5a2812015-11-13 10:07:31 +00005406 Location out_loc = locations->Out();
5407 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005408
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005409 bool generate_null_check = false;
5410 switch (cls->GetLoadKind()) {
5411 case HLoadClass::LoadKind::kReferrersClass: {
5412 DCHECK(!cls->CanCallRuntime());
5413 DCHECK(!cls->MustGenerateClinitCheck());
5414 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5415 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5416 GenerateGcRootFieldLoad(
5417 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5418 break;
5419 }
5420 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5421 DCHECK(!kEmitCompilerReadBarrier);
5422 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5423 codegen_->RecordTypePatch(cls);
5424 break;
5425 case HLoadClass::LoadKind::kBootImageAddress: {
5426 DCHECK(!kEmitCompilerReadBarrier);
5427 DCHECK_NE(cls->GetAddress(), 0u);
5428 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5429 __ movl(out, Immediate(address)); // Zero-extended.
5430 codegen_->RecordSimplePatch();
5431 break;
5432 }
5433 case HLoadClass::LoadKind::kDexCacheAddress: {
5434 DCHECK_NE(cls->GetAddress(), 0u);
5435 // /* GcRoot<mirror::Class> */ out = *address
5436 if (IsUint<32>(cls->GetAddress())) {
5437 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
5438 GenerateGcRootFieldLoad(cls, out_loc, address);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005439 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005440 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5441 __ movq(out, Immediate(cls->GetAddress()));
5442 GenerateGcRootFieldLoad(cls, out_loc, Address(out, 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005443 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005444 generate_null_check = !cls->IsInDexCache();
5445 break;
5446 }
5447 case HLoadClass::LoadKind::kDexCachePcRelative: {
5448 uint32_t offset = cls->GetDexCacheElementOffset();
5449 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5450 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5451 /* no_rip */ false);
5452 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5453 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label);
5454 generate_null_check = !cls->IsInDexCache();
5455 break;
5456 }
5457 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5458 // /* GcRoot<mirror::Class>[] */ out =
5459 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5460 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5461 __ movq(out,
5462 Address(current_method,
5463 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5464 // /* GcRoot<mirror::Class> */ out = out[type_index]
5465 GenerateGcRootFieldLoad(
5466 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
5467 generate_null_check = !cls->IsInDexCache();
5468 break;
5469 }
5470 default:
5471 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5472 UNREACHABLE();
5473 }
5474
5475 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5476 DCHECK(cls->CanCallRuntime());
5477 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5478 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5479 codegen_->AddSlowPath(slow_path);
5480 if (generate_null_check) {
5481 __ testl(out, out);
5482 __ j(kEqual, slow_path->GetEntryLabel());
5483 }
5484 if (cls->MustGenerateClinitCheck()) {
5485 GenerateClassInitializationCheck(slow_path, out);
5486 } else {
5487 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005488 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005489 }
5490}
5491
5492void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5493 LocationSummary* locations =
5494 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5495 locations->SetInAt(0, Location::RequiresRegister());
5496 if (check->HasUses()) {
5497 locations->SetOut(Location::SameAsFirstInput());
5498 }
5499}
5500
5501void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005502 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005503 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005504 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005505 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005506 GenerateClassInitializationCheck(slow_path,
5507 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005508}
5509
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005510HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5511 HLoadString::LoadKind desired_string_load_kind) {
5512 if (kEmitCompilerReadBarrier) {
5513 switch (desired_string_load_kind) {
5514 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5515 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5516 case HLoadString::LoadKind::kBootImageAddress:
5517 // TODO: Implement for read barrier.
5518 return HLoadString::LoadKind::kDexCacheViaMethod;
5519 default:
5520 break;
5521 }
5522 }
5523 switch (desired_string_load_kind) {
5524 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5525 DCHECK(!GetCompilerOptions().GetCompilePic());
5526 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5527 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5528 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5529 DCHECK(GetCompilerOptions().GetCompilePic());
5530 break;
5531 case HLoadString::LoadKind::kBootImageAddress:
5532 break;
5533 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005534 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005535 break;
5536 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005537 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005538 break;
5539 case HLoadString::LoadKind::kDexCacheViaMethod:
5540 break;
5541 }
5542 return desired_string_load_kind;
5543}
5544
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005545void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005546 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005547 ? LocationSummary::kCallOnSlowPath
5548 : LocationSummary::kNoCall;
5549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005550 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5551 locations->SetInAt(0, Location::RequiresRegister());
5552 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005553 locations->SetOut(Location::RequiresRegister());
5554}
5555
5556void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005557 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005558 Location out_loc = locations->Out();
5559 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005560
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005561 switch (load->GetLoadKind()) {
5562 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5563 DCHECK(!kEmitCompilerReadBarrier);
5564 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5565 codegen_->RecordStringPatch(load);
5566 return; // No dex cache slow path.
5567 }
5568 case HLoadString::LoadKind::kBootImageAddress: {
5569 DCHECK(!kEmitCompilerReadBarrier);
5570 DCHECK_NE(load->GetAddress(), 0u);
5571 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5572 __ movl(out, Immediate(address)); // Zero-extended.
5573 codegen_->RecordSimplePatch();
5574 return; // No dex cache slow path.
5575 }
5576 case HLoadString::LoadKind::kDexCacheAddress: {
5577 DCHECK_NE(load->GetAddress(), 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005578 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005579 if (IsUint<32>(load->GetAddress())) {
5580 Address address = Address::Absolute(load->GetAddress(), /* no_rip */ true);
5581 GenerateGcRootFieldLoad(load, out_loc, address);
5582 } else {
5583 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5584 __ movq(out, Immediate(load->GetAddress()));
5585 GenerateGcRootFieldLoad(load, out_loc, Address(out, 0));
5586 }
5587 break;
5588 }
5589 case HLoadString::LoadKind::kDexCachePcRelative: {
5590 uint32_t offset = load->GetDexCacheElementOffset();
5591 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
5592 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5593 /* no_rip */ false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005594 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005595 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label);
5596 break;
5597 }
5598 case HLoadString::LoadKind::kDexCacheViaMethod: {
5599 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5600
5601 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5602 GenerateGcRootFieldLoad(
5603 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5604 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5605 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5606 // /* GcRoot<mirror::String> */ out = out[string_index]
5607 GenerateGcRootFieldLoad(
5608 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
5609 break;
5610 }
5611 default:
5612 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5613 UNREACHABLE();
5614 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005615
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005616 if (!load->IsInDexCache()) {
5617 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5618 codegen_->AddSlowPath(slow_path);
5619 __ testl(out, out);
5620 __ j(kEqual, slow_path->GetEntryLabel());
5621 __ Bind(slow_path->GetExitLabel());
5622 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005623}
5624
David Brazdilcb1c0552015-08-04 16:22:25 +01005625static Address GetExceptionTlsAddress() {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005626 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(),
5627 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005628}
5629
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005630void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5631 LocationSummary* locations =
5632 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5633 locations->SetOut(Location::RequiresRegister());
5634}
5635
5636void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005637 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5638}
5639
5640void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5641 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5642}
5643
5644void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5645 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005646}
5647
5648void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5649 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005650 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005651 InvokeRuntimeCallingConvention calling_convention;
5652 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5653}
5654
5655void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005656 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5657 instruction,
5658 instruction->GetDexPc(),
5659 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005660 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005661}
5662
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005663static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5664 return kEmitCompilerReadBarrier &&
5665 (kUseBakerReadBarrier ||
5666 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5667 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5668 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5669}
5670
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005671void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005672 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005673 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5674 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005675 case TypeCheckKind::kExactCheck:
5676 case TypeCheckKind::kAbstractClassCheck:
5677 case TypeCheckKind::kClassHierarchyCheck:
5678 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005679 call_kind =
5680 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005681 break;
5682 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005683 case TypeCheckKind::kUnresolvedCheck:
5684 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005685 call_kind = LocationSummary::kCallOnSlowPath;
5686 break;
5687 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005688
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005689 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005690 locations->SetInAt(0, Location::RequiresRegister());
5691 locations->SetInAt(1, Location::Any());
5692 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5693 locations->SetOut(Location::RequiresRegister());
5694 // When read barriers are enabled, we need a temporary register for
5695 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005696 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005697 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005698 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005699}
5700
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005701void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005702 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005703 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005704 Location obj_loc = locations->InAt(0);
5705 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005706 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005707 Location out_loc = locations->Out();
5708 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005709 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005710 locations->GetTemp(0) :
5711 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005712 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005713 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5714 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5715 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005716 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005717 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005718
5719 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005720 // Avoid null check if we know obj is not null.
5721 if (instruction->MustDoNullCheck()) {
5722 __ testl(obj, obj);
5723 __ j(kEqual, &zero);
5724 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005725
Roland Levillain0d5a2812015-11-13 10:07:31 +00005726 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005727 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005728
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005729 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005730 case TypeCheckKind::kExactCheck: {
5731 if (cls.IsRegister()) {
5732 __ cmpl(out, cls.AsRegister<CpuRegister>());
5733 } else {
5734 DCHECK(cls.IsStackSlot()) << cls;
5735 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5736 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005737 if (zero.IsLinked()) {
5738 // Classes must be equal for the instanceof to succeed.
5739 __ j(kNotEqual, &zero);
5740 __ movl(out, Immediate(1));
5741 __ jmp(&done);
5742 } else {
5743 __ setcc(kEqual, out);
5744 // setcc only sets the low byte.
5745 __ andl(out, Immediate(1));
5746 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005747 break;
5748 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005749
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005750 case TypeCheckKind::kAbstractClassCheck: {
5751 // If the class is abstract, we eagerly fetch the super class of the
5752 // object to avoid doing a comparison we know will fail.
5753 NearLabel loop, success;
5754 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005755 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005756 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005757 __ testl(out, out);
5758 // If `out` is null, we use it for the result, and jump to `done`.
5759 __ j(kEqual, &done);
5760 if (cls.IsRegister()) {
5761 __ cmpl(out, cls.AsRegister<CpuRegister>());
5762 } else {
5763 DCHECK(cls.IsStackSlot()) << cls;
5764 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5765 }
5766 __ j(kNotEqual, &loop);
5767 __ movl(out, Immediate(1));
5768 if (zero.IsLinked()) {
5769 __ jmp(&done);
5770 }
5771 break;
5772 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005773
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005774 case TypeCheckKind::kClassHierarchyCheck: {
5775 // Walk over the class hierarchy to find a match.
5776 NearLabel loop, success;
5777 __ Bind(&loop);
5778 if (cls.IsRegister()) {
5779 __ cmpl(out, cls.AsRegister<CpuRegister>());
5780 } else {
5781 DCHECK(cls.IsStackSlot()) << cls;
5782 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5783 }
5784 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005785 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005786 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005787 __ testl(out, out);
5788 __ j(kNotEqual, &loop);
5789 // If `out` is null, we use it for the result, and jump to `done`.
5790 __ jmp(&done);
5791 __ Bind(&success);
5792 __ movl(out, Immediate(1));
5793 if (zero.IsLinked()) {
5794 __ jmp(&done);
5795 }
5796 break;
5797 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005798
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005799 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005800 // Do an exact check.
5801 NearLabel exact_check;
5802 if (cls.IsRegister()) {
5803 __ cmpl(out, cls.AsRegister<CpuRegister>());
5804 } else {
5805 DCHECK(cls.IsStackSlot()) << cls;
5806 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5807 }
5808 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005809 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005810 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005811 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005812 __ testl(out, out);
5813 // If `out` is null, we use it for the result, and jump to `done`.
5814 __ j(kEqual, &done);
5815 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5816 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005817 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005818 __ movl(out, Immediate(1));
5819 __ jmp(&done);
5820 break;
5821 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005822
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005823 case TypeCheckKind::kArrayCheck: {
5824 if (cls.IsRegister()) {
5825 __ cmpl(out, cls.AsRegister<CpuRegister>());
5826 } else {
5827 DCHECK(cls.IsStackSlot()) << cls;
5828 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5829 }
5830 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005831 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5832 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005833 codegen_->AddSlowPath(slow_path);
5834 __ j(kNotEqual, slow_path->GetEntryLabel());
5835 __ movl(out, Immediate(1));
5836 if (zero.IsLinked()) {
5837 __ jmp(&done);
5838 }
5839 break;
5840 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005841
Calin Juravle98893e12015-10-02 21:05:03 +01005842 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005843 case TypeCheckKind::kInterfaceCheck: {
5844 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005845 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005846 // cases.
5847 //
5848 // We cannot directly call the InstanceofNonTrivial runtime
5849 // entry point without resorting to a type checking slow path
5850 // here (i.e. by calling InvokeRuntime directly), as it would
5851 // require to assign fixed registers for the inputs of this
5852 // HInstanceOf instruction (following the runtime calling
5853 // convention), which might be cluttered by the potential first
5854 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005855 //
5856 // TODO: Introduce a new runtime entry point taking the object
5857 // to test (instead of its class) as argument, and let it deal
5858 // with the read barrier issues. This will let us refactor this
5859 // case of the `switch` code as it was previously (with a direct
5860 // call to the runtime not using a type checking slow path).
5861 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862 DCHECK(locations->OnlyCallsOnSlowPath());
5863 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5864 /* is_fatal */ false);
5865 codegen_->AddSlowPath(slow_path);
5866 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005867 if (zero.IsLinked()) {
5868 __ jmp(&done);
5869 }
5870 break;
5871 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005872 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005873
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005874 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005875 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005876 __ xorl(out, out);
5877 }
5878
5879 if (done.IsLinked()) {
5880 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005881 }
5882
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005883 if (slow_path != nullptr) {
5884 __ Bind(slow_path->GetExitLabel());
5885 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005886}
5887
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005888void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5890 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005891 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5892 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005893 case TypeCheckKind::kExactCheck:
5894 case TypeCheckKind::kAbstractClassCheck:
5895 case TypeCheckKind::kClassHierarchyCheck:
5896 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005897 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5898 LocationSummary::kCallOnSlowPath :
5899 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005900 break;
5901 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005902 case TypeCheckKind::kUnresolvedCheck:
5903 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005904 call_kind = LocationSummary::kCallOnSlowPath;
5905 break;
5906 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005907 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5908 locations->SetInAt(0, Location::RequiresRegister());
5909 locations->SetInAt(1, Location::Any());
5910 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5911 locations->AddTemp(Location::RequiresRegister());
5912 // When read barriers are enabled, we need an additional temporary
5913 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005914 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005915 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005917}
5918
5919void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005920 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005921 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005922 Location obj_loc = locations->InAt(0);
5923 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005924 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005925 Location temp_loc = locations->GetTemp(0);
5926 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005927 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005928 locations->GetTemp(1) :
5929 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5931 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5932 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5933 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005934
Roland Levillain0d5a2812015-11-13 10:07:31 +00005935 bool is_type_check_slow_path_fatal =
5936 (type_check_kind == TypeCheckKind::kExactCheck ||
5937 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5938 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5939 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5940 !instruction->CanThrowIntoCatchBlock();
5941 SlowPathCode* type_check_slow_path =
5942 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5943 is_type_check_slow_path_fatal);
5944 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005945
Roland Levillain0d5a2812015-11-13 10:07:31 +00005946 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005947 case TypeCheckKind::kExactCheck:
5948 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005949 NearLabel done;
5950 // Avoid null check if we know obj is not null.
5951 if (instruction->MustDoNullCheck()) {
5952 __ testl(obj, obj);
5953 __ j(kEqual, &done);
5954 }
5955
5956 // /* HeapReference<Class> */ temp = obj->klass_
5957 GenerateReferenceLoadTwoRegisters(
5958 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5959
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005960 if (cls.IsRegister()) {
5961 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5962 } else {
5963 DCHECK(cls.IsStackSlot()) << cls;
5964 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5965 }
5966 // Jump to slow path for throwing the exception or doing a
5967 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005968 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005969 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005970 break;
5971 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005972
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005973 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005974 NearLabel done;
5975 // Avoid null check if we know obj is not null.
5976 if (instruction->MustDoNullCheck()) {
5977 __ testl(obj, obj);
5978 __ j(kEqual, &done);
5979 }
5980
5981 // /* HeapReference<Class> */ temp = obj->klass_
5982 GenerateReferenceLoadTwoRegisters(
5983 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5984
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005985 // If the class is abstract, we eagerly fetch the super class of the
5986 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005987 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005988 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005989 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005990 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005991
5992 // If the class reference currently in `temp` is not null, jump
5993 // to the `compare_classes` label to compare it with the checked
5994 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005995 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005996 __ j(kNotEqual, &compare_classes);
5997 // Otherwise, jump to the slow path to throw the exception.
5998 //
5999 // But before, move back the object's class into `temp` before
6000 // going into the slow path, as it has been overwritten in the
6001 // meantime.
6002 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006003 GenerateReferenceLoadTwoRegisters(
6004 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006005 __ jmp(type_check_slow_path->GetEntryLabel());
6006
6007 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006008 if (cls.IsRegister()) {
6009 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6010 } else {
6011 DCHECK(cls.IsStackSlot()) << cls;
6012 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6013 }
6014 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00006015 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006016 break;
6017 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006018
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006019 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006020 NearLabel done;
6021 // Avoid null check if we know obj is not null.
6022 if (instruction->MustDoNullCheck()) {
6023 __ testl(obj, obj);
6024 __ j(kEqual, &done);
6025 }
6026
6027 // /* HeapReference<Class> */ temp = obj->klass_
6028 GenerateReferenceLoadTwoRegisters(
6029 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6030
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006031 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006032 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033 __ Bind(&loop);
6034 if (cls.IsRegister()) {
6035 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6036 } else {
6037 DCHECK(cls.IsStackSlot()) << cls;
6038 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6039 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006040 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006041
Roland Levillain0d5a2812015-11-13 10:07:31 +00006042 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006043 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006044
6045 // If the class reference currently in `temp` is not null, jump
6046 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 __ testl(temp, temp);
6048 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006049 // Otherwise, jump to the slow path to throw the exception.
6050 //
6051 // But before, move back the object's class into `temp` before
6052 // going into the slow path, as it has been overwritten in the
6053 // meantime.
6054 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006055 GenerateReferenceLoadTwoRegisters(
6056 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006057 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006058 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006059 break;
6060 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006061
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006063 // We cannot use a NearLabel here, as its range might be too
6064 // short in some cases when read barriers are enabled. This has
6065 // been observed for instance when the code emitted for this
6066 // case uses high x86-64 registers (R8-R15).
6067 Label done;
6068 // Avoid null check if we know obj is not null.
6069 if (instruction->MustDoNullCheck()) {
6070 __ testl(obj, obj);
6071 __ j(kEqual, &done);
6072 }
6073
6074 // /* HeapReference<Class> */ temp = obj->klass_
6075 GenerateReferenceLoadTwoRegisters(
6076 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6077
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006078 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006080 if (cls.IsRegister()) {
6081 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6082 } else {
6083 DCHECK(cls.IsStackSlot()) << cls;
6084 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6085 }
6086 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006087
6088 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006089 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006090 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006091
6092 // If the component type is not null (i.e. the object is indeed
6093 // an array), jump to label `check_non_primitive_component_type`
6094 // to further check that this component type is not a primitive
6095 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006096 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006097 __ j(kNotEqual, &check_non_primitive_component_type);
6098 // Otherwise, jump to the slow path to throw the exception.
6099 //
6100 // But before, move back the object's class into `temp` before
6101 // going into the slow path, as it has been overwritten in the
6102 // meantime.
6103 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006104 GenerateReferenceLoadTwoRegisters(
6105 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106 __ jmp(type_check_slow_path->GetEntryLabel());
6107
6108 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006109 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006110 __ j(kEqual, &done);
6111 // Same comment as above regarding `temp` and the slow path.
6112 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006113 GenerateReferenceLoadTwoRegisters(
6114 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006115 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006116 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006117 break;
6118 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006119
Calin Juravle98893e12015-10-02 21:05:03 +01006120 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006121 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006122 NearLabel done;
6123 // Avoid null check if we know obj is not null.
6124 if (instruction->MustDoNullCheck()) {
6125 __ testl(obj, obj);
6126 __ j(kEqual, &done);
6127 }
6128
6129 // /* HeapReference<Class> */ temp = obj->klass_
6130 GenerateReferenceLoadTwoRegisters(
6131 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6132
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006133 // We always go into the type check slow path for the unresolved
6134 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006135 //
6136 // We cannot directly call the CheckCast runtime entry point
6137 // without resorting to a type checking slow path here (i.e. by
6138 // calling InvokeRuntime directly), as it would require to
6139 // assign fixed registers for the inputs of this HInstanceOf
6140 // instruction (following the runtime calling convention), which
6141 // might be cluttered by the potential first read barrier
6142 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006143 //
6144 // TODO: Introduce a new runtime entry point taking the object
6145 // to test (instead of its class) as argument, and let it deal
6146 // with the read barrier issues. This will let us refactor this
6147 // case of the `switch` code as it was previously (with a direct
6148 // call to the runtime not using a type checking slow path).
6149 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006150 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006151 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 break;
6153 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006154
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006156}
6157
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006158void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6159 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006160 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006161 InvokeRuntimeCallingConvention calling_convention;
6162 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6163}
6164
6165void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006166 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6167 : QUICK_ENTRY_POINT(pUnlockObject),
6168 instruction,
6169 instruction->GetDexPc(),
6170 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006171 if (instruction->IsEnter()) {
6172 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6173 } else {
6174 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6175 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006176}
6177
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006178void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6179void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6180void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6181
6182void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6183 LocationSummary* locations =
6184 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6185 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6186 || instruction->GetResultType() == Primitive::kPrimLong);
6187 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006188 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006189 locations->SetOut(Location::SameAsFirstInput());
6190}
6191
6192void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6193 HandleBitwiseOperation(instruction);
6194}
6195
6196void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6197 HandleBitwiseOperation(instruction);
6198}
6199
6200void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6201 HandleBitwiseOperation(instruction);
6202}
6203
6204void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6205 LocationSummary* locations = instruction->GetLocations();
6206 Location first = locations->InAt(0);
6207 Location second = locations->InAt(1);
6208 DCHECK(first.Equals(locations->Out()));
6209
6210 if (instruction->GetResultType() == Primitive::kPrimInt) {
6211 if (second.IsRegister()) {
6212 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006213 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006214 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006215 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006216 } else {
6217 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006218 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006219 }
6220 } else if (second.IsConstant()) {
6221 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6222 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006223 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006224 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006225 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006226 } else {
6227 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006228 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006229 }
6230 } else {
6231 Address address(CpuRegister(RSP), second.GetStackIndex());
6232 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006233 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006234 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006235 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006236 } else {
6237 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006238 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006239 }
6240 }
6241 } else {
6242 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006243 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6244 bool second_is_constant = false;
6245 int64_t value = 0;
6246 if (second.IsConstant()) {
6247 second_is_constant = true;
6248 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006249 }
Mark Mendell40741f32015-04-20 22:10:34 -04006250 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006251
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006252 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006253 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006254 if (is_int32_value) {
6255 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6256 } else {
6257 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6258 }
6259 } else if (second.IsDoubleStackSlot()) {
6260 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006261 } else {
6262 __ andq(first_reg, second.AsRegister<CpuRegister>());
6263 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006264 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006265 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006266 if (is_int32_value) {
6267 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6268 } else {
6269 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6270 }
6271 } else if (second.IsDoubleStackSlot()) {
6272 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006273 } else {
6274 __ orq(first_reg, second.AsRegister<CpuRegister>());
6275 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006276 } else {
6277 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006278 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006279 if (is_int32_value) {
6280 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6281 } else {
6282 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6283 }
6284 } else if (second.IsDoubleStackSlot()) {
6285 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006286 } else {
6287 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6288 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006289 }
6290 }
6291}
6292
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006293void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6294 Location out,
6295 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006296 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006297 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6298 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006299 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006300 if (kUseBakerReadBarrier) {
6301 // Load with fast path based Baker's read barrier.
6302 // /* HeapReference<Object> */ out = *(out + offset)
6303 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006304 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006305 } else {
6306 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006307 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006308 // in the following move operation, as we will need it for the
6309 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006310 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006311 // /* HeapReference<Object> */ out = *(out + offset)
6312 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006313 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006314 }
6315 } else {
6316 // Plain load with no read barrier.
6317 // /* HeapReference<Object> */ out = *(out + offset)
6318 __ movl(out_reg, Address(out_reg, offset));
6319 __ MaybeUnpoisonHeapReference(out_reg);
6320 }
6321}
6322
6323void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6324 Location out,
6325 Location obj,
6326 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006327 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006328 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6329 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6330 if (kEmitCompilerReadBarrier) {
6331 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006332 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006333 // Load with fast path based Baker's read barrier.
6334 // /* HeapReference<Object> */ out = *(obj + offset)
6335 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006336 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006337 } else {
6338 // Load with slow path based read barrier.
6339 // /* HeapReference<Object> */ out = *(obj + offset)
6340 __ movl(out_reg, Address(obj_reg, offset));
6341 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6342 }
6343 } else {
6344 // Plain load with no read barrier.
6345 // /* HeapReference<Object> */ out = *(obj + offset)
6346 __ movl(out_reg, Address(obj_reg, offset));
6347 __ MaybeUnpoisonHeapReference(out_reg);
6348 }
6349}
6350
6351void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6352 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006353 const Address& address,
6354 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006355 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6356 if (kEmitCompilerReadBarrier) {
6357 if (kUseBakerReadBarrier) {
6358 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6359 // Baker's read barrier are used:
6360 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006361 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006362 // if (Thread::Current()->GetIsGcMarking()) {
6363 // root = ReadBarrier::Mark(root)
6364 // }
6365
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006366 // /* GcRoot<mirror::Object> */ root = *address
6367 __ movl(root_reg, address);
6368 if (fixup_label != nullptr) {
6369 __ Bind(fixup_label);
6370 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006371 static_assert(
6372 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6373 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6374 "have different sizes.");
6375 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6376 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6377 "have different sizes.");
6378
6379 // Slow path used to mark the GC root `root`.
6380 SlowPathCode* slow_path =
6381 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, root, root);
6382 codegen_->AddSlowPath(slow_path);
6383
6384 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64WordSize>().Int32Value(),
6385 /* no_rip */ true),
6386 Immediate(0));
6387 __ j(kNotEqual, slow_path->GetEntryLabel());
6388 __ Bind(slow_path->GetExitLabel());
6389 } else {
6390 // GC root loaded through a slow path for read barriers other
6391 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006392 // /* GcRoot<mirror::Object>* */ root = address
6393 __ leaq(root_reg, address);
6394 if (fixup_label != nullptr) {
6395 __ Bind(fixup_label);
6396 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006397 // /* mirror::Object* */ root = root->Read()
6398 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6399 }
6400 } else {
6401 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006402 // /* GcRoot<mirror::Object> */ root = *address
6403 __ movl(root_reg, address);
6404 if (fixup_label != nullptr) {
6405 __ Bind(fixup_label);
6406 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006407 // Note that GC roots are not affected by heap poisoning, thus we
6408 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006409 }
6410}
6411
6412void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6413 Location ref,
6414 CpuRegister obj,
6415 uint32_t offset,
6416 Location temp,
6417 bool needs_null_check) {
6418 DCHECK(kEmitCompilerReadBarrier);
6419 DCHECK(kUseBakerReadBarrier);
6420
6421 // /* HeapReference<Object> */ ref = *(obj + offset)
6422 Address src(obj, offset);
6423 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6424}
6425
6426void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6427 Location ref,
6428 CpuRegister obj,
6429 uint32_t data_offset,
6430 Location index,
6431 Location temp,
6432 bool needs_null_check) {
6433 DCHECK(kEmitCompilerReadBarrier);
6434 DCHECK(kUseBakerReadBarrier);
6435
Roland Levillain3d312422016-06-23 13:53:42 +01006436 static_assert(
6437 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6438 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006439 // /* HeapReference<Object> */ ref =
6440 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6441 Address src = index.IsConstant() ?
6442 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6443 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
6444 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6445}
6446
6447void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6448 Location ref,
6449 CpuRegister obj,
6450 const Address& src,
6451 Location temp,
6452 bool needs_null_check) {
6453 DCHECK(kEmitCompilerReadBarrier);
6454 DCHECK(kUseBakerReadBarrier);
6455
6456 // In slow path based read barriers, the read barrier call is
6457 // inserted after the original load. However, in fast path based
6458 // Baker's read barriers, we need to perform the load of
6459 // mirror::Object::monitor_ *before* the original reference load.
6460 // This load-load ordering is required by the read barrier.
6461 // The fast path/slow path (for Baker's algorithm) should look like:
6462 //
6463 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6464 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6465 // HeapReference<Object> ref = *src; // Original reference load.
6466 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6467 // if (is_gray) {
6468 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6469 // }
6470 //
6471 // Note: the original implementation in ReadBarrier::Barrier is
6472 // slightly more complex as:
6473 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006474 // the high-bits of rb_state, which are expected to be all zeroes
6475 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6476 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006477 // - it performs additional checks that we do not do here for
6478 // performance reasons.
6479
6480 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
6481 CpuRegister temp_reg = temp.AsRegister<CpuRegister>();
6482 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6483
6484 // /* int32_t */ monitor = obj->monitor_
6485 __ movl(temp_reg, Address(obj, monitor_offset));
6486 if (needs_null_check) {
6487 MaybeRecordImplicitNullCheck(instruction);
6488 }
6489 // /* LockWord */ lock_word = LockWord(monitor)
6490 static_assert(sizeof(LockWord) == sizeof(int32_t),
6491 "art::LockWord and int32_t have different sizes.");
6492 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6493 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6494 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6495 static_assert(
6496 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6497 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6498
6499 // Load fence to prevent load-load reordering.
6500 // Note that this is a no-op, thanks to the x86-64 memory model.
6501 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6502
6503 // The actual reference load.
6504 // /* HeapReference<Object> */ ref = *src
6505 __ movl(ref_reg, src);
6506
6507 // Object* ref = ref_addr->AsMirrorPtr()
6508 __ MaybeUnpoisonHeapReference(ref_reg);
6509
6510 // Slow path used to mark the object `ref` when it is gray.
6511 SlowPathCode* slow_path =
6512 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(instruction, ref, ref);
6513 AddSlowPath(slow_path);
6514
6515 // if (rb_state == ReadBarrier::gray_ptr_)
6516 // ref = ReadBarrier::Mark(ref);
6517 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6518 __ j(kEqual, slow_path->GetEntryLabel());
6519 __ Bind(slow_path->GetExitLabel());
6520}
6521
6522void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6523 Location out,
6524 Location ref,
6525 Location obj,
6526 uint32_t offset,
6527 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006528 DCHECK(kEmitCompilerReadBarrier);
6529
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006530 // Insert a slow path based read barrier *after* the reference load.
6531 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006532 // If heap poisoning is enabled, the unpoisoning of the loaded
6533 // reference will be carried out by the runtime within the slow
6534 // path.
6535 //
6536 // Note that `ref` currently does not get unpoisoned (when heap
6537 // poisoning is enabled), which is alright as the `ref` argument is
6538 // not used by the artReadBarrierSlow entry point.
6539 //
6540 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6541 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6542 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6543 AddSlowPath(slow_path);
6544
Roland Levillain0d5a2812015-11-13 10:07:31 +00006545 __ jmp(slow_path->GetEntryLabel());
6546 __ Bind(slow_path->GetExitLabel());
6547}
6548
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006549void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6550 Location out,
6551 Location ref,
6552 Location obj,
6553 uint32_t offset,
6554 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006555 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006556 // Baker's read barriers shall be handled by the fast path
6557 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6558 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 // If heap poisoning is enabled, unpoisoning will be taken care of
6560 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006561 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006562 } else if (kPoisonHeapReferences) {
6563 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6564 }
6565}
6566
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006567void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6568 Location out,
6569 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006570 DCHECK(kEmitCompilerReadBarrier);
6571
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006572 // Insert a slow path based read barrier *after* the GC root load.
6573 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006574 // Note that GC roots are not affected by heap poisoning, so we do
6575 // not need to do anything special for this here.
6576 SlowPathCode* slow_path =
6577 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6578 AddSlowPath(slow_path);
6579
Roland Levillain0d5a2812015-11-13 10:07:31 +00006580 __ jmp(slow_path->GetEntryLabel());
6581 __ Bind(slow_path->GetExitLabel());
6582}
6583
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006584void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006585 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006586 LOG(FATAL) << "Unreachable";
6587}
6588
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006589void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006590 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006591 LOG(FATAL) << "Unreachable";
6592}
6593
Mark Mendellfe57faa2015-09-18 09:26:15 -04006594// Simple implementation of packed switch - generate cascaded compare/jumps.
6595void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6596 LocationSummary* locations =
6597 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6598 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006599 locations->AddTemp(Location::RequiresRegister());
6600 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006601}
6602
6603void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6604 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006605 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006606 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006607 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6608 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6609 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006610 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6611
6612 // Should we generate smaller inline compare/jumps?
6613 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6614 // Figure out the correct compare values and jump conditions.
6615 // Handle the first compare/branch as a special case because it might
6616 // jump to the default case.
6617 DCHECK_GT(num_entries, 2u);
6618 Condition first_condition;
6619 uint32_t index;
6620 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6621 if (lower_bound != 0) {
6622 first_condition = kLess;
6623 __ cmpl(value_reg_in, Immediate(lower_bound));
6624 __ j(first_condition, codegen_->GetLabelOf(default_block));
6625 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6626
6627 index = 1;
6628 } else {
6629 // Handle all the compare/jumps below.
6630 first_condition = kBelow;
6631 index = 0;
6632 }
6633
6634 // Handle the rest of the compare/jumps.
6635 for (; index + 1 < num_entries; index += 2) {
6636 int32_t compare_to_value = lower_bound + index + 1;
6637 __ cmpl(value_reg_in, Immediate(compare_to_value));
6638 // Jump to successors[index] if value < case_value[index].
6639 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6640 // Jump to successors[index + 1] if value == case_value[index + 1].
6641 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6642 }
6643
6644 if (index != num_entries) {
6645 // There are an odd number of entries. Handle the last one.
6646 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006647 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006648 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6649 }
6650
6651 // And the default for any other value.
6652 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6653 __ jmp(codegen_->GetLabelOf(default_block));
6654 }
6655 return;
6656 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006657
6658 // Remove the bias, if needed.
6659 Register value_reg_out = value_reg_in.AsRegister();
6660 if (lower_bound != 0) {
6661 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6662 value_reg_out = temp_reg.AsRegister();
6663 }
6664 CpuRegister value_reg(value_reg_out);
6665
6666 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006667 __ cmpl(value_reg, Immediate(num_entries - 1));
6668 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006669
Mark Mendell9c86b482015-09-18 13:36:07 -04006670 // We are in the range of the table.
6671 // Load the address of the jump table in the constant area.
6672 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006673
Mark Mendell9c86b482015-09-18 13:36:07 -04006674 // Load the (signed) offset from the jump table.
6675 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6676
6677 // Add the offset to the address of the table base.
6678 __ addq(temp_reg, base_reg);
6679
6680 // And jump.
6681 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006682}
6683
Aart Bikc5d47542016-01-27 17:00:35 -08006684void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6685 if (value == 0) {
6686 __ xorl(dest, dest);
6687 } else {
6688 __ movl(dest, Immediate(value));
6689 }
6690}
6691
Mark Mendell92e83bf2015-05-07 11:25:03 -04006692void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6693 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006694 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006695 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006696 } else if (IsUint<32>(value)) {
6697 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006698 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6699 } else {
6700 __ movq(dest, Immediate(value));
6701 }
6702}
6703
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006704void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6705 if (value == 0) {
6706 __ xorps(dest, dest);
6707 } else {
6708 __ movss(dest, LiteralInt32Address(value));
6709 }
6710}
6711
6712void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6713 if (value == 0) {
6714 __ xorpd(dest, dest);
6715 } else {
6716 __ movsd(dest, LiteralInt64Address(value));
6717 }
6718}
6719
6720void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6721 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6722}
6723
6724void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6725 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6726}
6727
Aart Bika19616e2016-02-01 18:57:58 -08006728void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6729 if (value == 0) {
6730 __ testl(dest, dest);
6731 } else {
6732 __ cmpl(dest, Immediate(value));
6733 }
6734}
6735
6736void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6737 if (IsInt<32>(value)) {
6738 if (value == 0) {
6739 __ testq(dest, dest);
6740 } else {
6741 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6742 }
6743 } else {
6744 // Value won't fit in an int.
6745 __ cmpq(dest, LiteralInt64Address(value));
6746 }
6747}
6748
Mark Mendellcfa410b2015-05-25 16:02:44 -04006749void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6750 DCHECK(dest.IsDoubleStackSlot());
6751 if (IsInt<32>(value)) {
6752 // Can move directly as an int32 constant.
6753 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6754 Immediate(static_cast<int32_t>(value)));
6755 } else {
6756 Load64BitValue(CpuRegister(TMP), value);
6757 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6758 }
6759}
6760
Mark Mendell9c86b482015-09-18 13:36:07 -04006761/**
6762 * Class to handle late fixup of offsets into constant area.
6763 */
6764class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6765 public:
6766 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6767 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6768
6769 protected:
6770 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6771
6772 CodeGeneratorX86_64* codegen_;
6773
6774 private:
6775 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6776 // Patch the correct offset for the instruction. We use the address of the
6777 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6778 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6779 int32_t relative_position = constant_offset - pos;
6780
6781 // Patch in the right value.
6782 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6783 }
6784
6785 // Location in constant area that the fixup refers to.
6786 size_t offset_into_constant_area_;
6787};
6788
6789/**
6790 t * Class to handle late fixup of offsets to a jump table that will be created in the
6791 * constant area.
6792 */
6793class JumpTableRIPFixup : public RIPFixup {
6794 public:
6795 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6796 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6797
6798 void CreateJumpTable() {
6799 X86_64Assembler* assembler = codegen_->GetAssembler();
6800
6801 // Ensure that the reference to the jump table has the correct offset.
6802 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6803 SetOffset(offset_in_constant_table);
6804
6805 // Compute the offset from the start of the function to this jump table.
6806 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6807
6808 // Populate the jump table with the correct values for the jump table.
6809 int32_t num_entries = switch_instr_->GetNumEntries();
6810 HBasicBlock* block = switch_instr_->GetBlock();
6811 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6812 // The value that we want is the target offset - the position of the table.
6813 for (int32_t i = 0; i < num_entries; i++) {
6814 HBasicBlock* b = successors[i];
6815 Label* l = codegen_->GetLabelOf(b);
6816 DCHECK(l->IsBound());
6817 int32_t offset_to_block = l->Position() - current_table_offset;
6818 assembler->AppendInt32(offset_to_block);
6819 }
6820 }
6821
6822 private:
6823 const HPackedSwitch* switch_instr_;
6824};
6825
Mark Mendellf55c3e02015-03-26 21:07:46 -04006826void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6827 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006828 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006829 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6830 // 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 -04006831 assembler->Align(4, 0);
6832 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006833
6834 // Populate any jump tables.
6835 for (auto jump_table : fixups_to_jump_tables_) {
6836 jump_table->CreateJumpTable();
6837 }
6838
6839 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006840 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006841 }
6842
6843 // And finish up.
6844 CodeGenerator::Finalize(allocator);
6845}
6846
Mark Mendellf55c3e02015-03-26 21:07:46 -04006847Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6848 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6849 return Address::RIP(fixup);
6850}
6851
6852Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6853 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6854 return Address::RIP(fixup);
6855}
6856
6857Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6858 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6859 return Address::RIP(fixup);
6860}
6861
6862Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6863 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6864 return Address::RIP(fixup);
6865}
6866
Andreas Gampe85b62f22015-09-09 13:15:38 -07006867// TODO: trg as memory.
6868void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6869 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006870 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006871 return;
6872 }
6873
6874 DCHECK_NE(type, Primitive::kPrimVoid);
6875
6876 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6877 if (trg.Equals(return_loc)) {
6878 return;
6879 }
6880
6881 // Let the parallel move resolver take care of all of this.
6882 HParallelMove parallel_move(GetGraph()->GetArena());
6883 parallel_move.AddMove(return_loc, trg, type, nullptr);
6884 GetMoveResolver()->EmitNativeCode(&parallel_move);
6885}
6886
Mark Mendell9c86b482015-09-18 13:36:07 -04006887Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6888 // Create a fixup to be used to create and address the jump table.
6889 JumpTableRIPFixup* table_fixup =
6890 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6891
6892 // We have to populate the jump tables.
6893 fixups_to_jump_tables_.push_back(table_fixup);
6894 return Address::RIP(table_fixup);
6895}
6896
Mark Mendellea5af682015-10-22 17:35:49 -04006897void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6898 const Address& addr_high,
6899 int64_t v,
6900 HInstruction* instruction) {
6901 if (IsInt<32>(v)) {
6902 int32_t v_32 = v;
6903 __ movq(addr_low, Immediate(v_32));
6904 MaybeRecordImplicitNullCheck(instruction);
6905 } else {
6906 // Didn't fit in a register. Do it in pieces.
6907 int32_t low_v = Low32Bits(v);
6908 int32_t high_v = High32Bits(v);
6909 __ movl(addr_low, Immediate(low_v));
6910 MaybeRecordImplicitNullCheck(instruction);
6911 __ movl(addr_high, Immediate(high_v));
6912 }
6913}
6914
Roland Levillain4d027112015-07-01 15:41:14 +01006915#undef __
6916
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006917} // namespace x86_64
6918} // namespace art