blob: 1abda7da6fea0846b383d12758dfe60a3285e324 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "intrinsics.h"
25#include "intrinsics_x86_64.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "mirror/object_reference.h"
29#include "thread.h"
30#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "utils/x86_64/assembler_x86_64.h"
33#include "utils/x86_64/managed_register_x86_64.h"
34
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010035namespace art {
36
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010040namespace x86_64 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = RDI;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000044// The compare/jump sequence will generate about (1.5 * num_entries) instructions. A jump
45// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
46// generates less code/data with a small num_entries.
47static constexpr uint32_t kPackedSwitchJumpTableThreshold = 5;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000049static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051
Mark Mendell24f2dfa2015-01-14 19:51:45 -050052static constexpr int kC2ConditionMask = 0x400;
53
Roland Levillain7cbd27f2016-08-11 23:53:33 +010054// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
55#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070056#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, 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 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010069 x86_64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Roland Levillain0d5a2812015-11-13 10:07:31 +000070 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());
Serban Constantinescuba45db02016-07-12 22:53:02 +010091 x86_64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000092 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000093 }
94
Alexandre Rames8158f282015-08-07 10:26:17 +010095 bool IsFatal() const OVERRIDE { return true; }
96
Alexandre Rames9931f312015-06-19 14:47:01 +010097 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
98
Calin Juravled0d48522014-11-04 16:40:20 +000099 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000100 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
101};
102
Andreas Gampe85b62f22015-09-09 13:15:38 -0700103class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000104 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000105 DivRemMinusOneSlowPathX86_64(HInstruction* at, Register reg, Primitive::Type type, bool is_div)
106 : SlowPathCode(at), cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000110 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000111 if (is_div_) {
112 __ negl(cpu_reg_);
113 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400114 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000115 }
116
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000117 } else {
118 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000119 if (is_div_) {
120 __ negq(cpu_reg_);
121 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400122 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000124 }
Calin Juravled0d48522014-11-04 16:40:20 +0000125 __ jmp(GetExitLabel());
126 }
127
Alexandre Rames9931f312015-06-19 14:47:01 +0100128 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
129
Calin Juravled0d48522014-11-04 16:40:20 +0000130 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000131 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000132 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000133 const bool is_div_;
134 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000135};
136
Andreas Gampe85b62f22015-09-09 13:15:38 -0700137class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100139 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000140 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000142 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000143 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100145 x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000146 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100147 if (successor_ == nullptr) {
148 __ jmp(GetReturnLabel());
149 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000150 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000152 }
153
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100154 Label* GetReturnLabel() {
155 DCHECK(successor_ == nullptr);
156 return &return_label_;
157 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100159 HBasicBlock* GetSuccessor() const {
160 return successor_;
161 }
162
Alexandre Rames9931f312015-06-19 14:47:01 +0100163 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
164
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 Label return_label_;
168
169 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
170};
171
Andreas Gampe85b62f22015-09-09 13:15:38 -0700172class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100174 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000175 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000177 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000179 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000181 if (instruction_->CanThrowIntoCatchBlock()) {
182 // Live registers will be restored in the catch block if caught.
183 SaveLiveRegisters(codegen, instruction_->GetLocations());
184 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400185 // Are we using an array length from memory?
186 HInstruction* array_length = instruction_->InputAt(1);
187 Location length_loc = locations->InAt(1);
188 InvokeRuntimeCallingConvention calling_convention;
189 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
190 // Load the array length into our temporary.
191 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
192 Location array_loc = array_length->GetLocations()->InAt(0);
193 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
194 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
195 // Check for conflicts with index.
196 if (length_loc.Equals(locations->InAt(0))) {
197 // We know we aren't using parameter 2.
198 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
199 }
200 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700201 if (mirror::kUseStringCompression) {
202 __ andl(length_loc.AsRegister<CpuRegister>(), Immediate(INT32_MAX));
203 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400204 }
205
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000206 // We're moving two locations to locations that could overlap, so we need a parallel
207 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000208 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100209 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000210 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100211 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400212 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100213 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
214 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100215 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
216 ? kQuickThrowStringBounds
217 : kQuickThrowArrayBounds;
218 x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100219 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000220 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100221 }
222
Alexandre Rames8158f282015-08-07 10:26:17 +0100223 bool IsFatal() const OVERRIDE { return true; }
224
Alexandre Rames9931f312015-06-19 14:47:01 +0100225 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
226
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100227 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100228 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
229};
230
Andreas Gampe85b62f22015-09-09 13:15:38 -0700231class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100232 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000233 LoadClassSlowPathX86_64(HLoadClass* cls,
234 HInstruction* at,
235 uint32_t dex_pc,
236 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000237 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
239 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000241 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000242 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000243 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100244 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100245
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000246 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000247
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100248 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100250 x86_64_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage : kQuickInitializeType,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000251 at_,
252 dex_pc_,
253 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000254 if (do_clinit_) {
255 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
256 } else {
257 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
258 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100259
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000260 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000261 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262 if (out.IsValid()) {
263 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000264 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000265 }
266
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000267 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100268 __ jmp(GetExitLabel());
269 }
270
Alexandre Rames9931f312015-06-19 14:47:01 +0100271 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
272
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100273 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 // The class this slow path will load.
275 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100276
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 // The instruction where this slow path is happening.
278 // (Might be the load class or an initialization check).
279 HInstruction* const at_;
280
281 // The dex PC of `at_`.
282 const uint32_t dex_pc_;
283
284 // Whether to initialize the class.
285 const bool do_clinit_;
286
287 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100288};
289
Vladimir Markoaad75c62016-10-03 08:46:48 +0000290class LoadStringSlowPathX86_64 : public SlowPathCode {
291 public:
292 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
293
294 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
295 LocationSummary* locations = instruction_->GetLocations();
296 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
297
298 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
299 __ Bind(GetEntryLabel());
300 SaveLiveRegisters(codegen, locations);
301
Vladimir Markoaad75c62016-10-03 08:46:48 +0000302 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100303 // Custom calling convention: RAX serves as both input and output.
304 __ movl(CpuRegister(RAX), Immediate(string_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000305 x86_64_codegen->InvokeRuntime(kQuickResolveString,
306 instruction_,
307 instruction_->GetDexPc(),
308 this);
309 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
310 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
311 RestoreLiveRegisters(codegen, locations);
312
313 // Store the resolved String to the BSS entry.
314 __ movl(Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false),
315 locations->Out().AsRegister<CpuRegister>());
316 Label* fixup_label = x86_64_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
317 __ Bind(fixup_label);
318
319 __ jmp(GetExitLabel());
320 }
321
322 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
323
324 private:
325 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
326};
327
Andreas Gampe85b62f22015-09-09 13:15:38 -0700328class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000331 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800335 Location arg0, arg1;
336 if (instruction_->IsInstanceOf()) {
337 arg0 = locations->InAt(1);
338 arg1 = locations->Out();
339 } else {
340 arg0 = locations->InAt(0);
341 arg1 = locations->InAt(1);
342 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100343 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 DCHECK(instruction_->IsCheckCast()
345 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
Roland Levillain0d5a2812015-11-13 10:07:31 +0000347 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000349
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000350 if (!is_fatal_) {
351 SaveLiveRegisters(codegen, locations);
352 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353
354 // We're moving two locations to locations that could overlap, so we need a parallel
355 // move resolver.
356 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800357 codegen->EmitParallelMoves(arg0,
358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
359 Primitive::kPrimNot,
360 arg1,
361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000363 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100364 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800365 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Class*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000366 } else {
367 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800368 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
369 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000370 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 if (!is_fatal_) {
373 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000374 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000375 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000377 RestoreLiveRegisters(codegen, locations);
378 __ jmp(GetExitLabel());
379 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000380 }
381
Alexandre Rames9931f312015-06-19 14:47:01 +0100382 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
383
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000384 bool IsFatal() const OVERRIDE { return is_fatal_; }
385
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000386 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000387 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000388
389 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
390};
391
Andreas Gampe85b62f22015-09-09 13:15:38 -0700392class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393 public:
Aart Bik42249c32016-01-07 15:33:50 -0800394 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000395 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000398 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100400 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000401 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700402 }
403
Alexandre Rames9931f312015-06-19 14:47:01 +0100404 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
405
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700406 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700407 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
408};
409
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100410class ArraySetSlowPathX86_64 : public SlowPathCode {
411 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000412 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
415 LocationSummary* locations = instruction_->GetLocations();
416 __ Bind(GetEntryLabel());
417 SaveLiveRegisters(codegen, locations);
418
419 InvokeRuntimeCallingConvention calling_convention;
420 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
421 parallel_move.AddMove(
422 locations->InAt(0),
423 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
424 Primitive::kPrimNot,
425 nullptr);
426 parallel_move.AddMove(
427 locations->InAt(1),
428 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
429 Primitive::kPrimInt,
430 nullptr);
431 parallel_move.AddMove(
432 locations->InAt(2),
433 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
434 Primitive::kPrimNot,
435 nullptr);
436 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
437
Roland Levillain0d5a2812015-11-13 10:07:31 +0000438 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100439 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), 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 Levillaina1aa3b12016-10-26 13:03:38 +0100451// Slow path marking an object reference `ref` during a read
452// barrier. The field `obj.field` in the object `obj` holding this
453// reference does not get updated by this slow path after marking (see
454// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
455//
456// This means that after the execution of this slow path, `ref` will
457// always be up-to-date, but `obj.field` may not; i.e., after the
458// flip, `ref` will be a to-space reference, but `obj.field` will
459// probably still be a from-space reference (unless it gets updated by
460// another thread, or if another thread installed another object
461// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000462class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
463 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100464 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
465 Location ref,
466 bool unpoison_ref_before_marking)
467 : SlowPathCode(instruction),
468 ref_(ref),
469 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000470 DCHECK(kEmitCompilerReadBarrier);
471 }
472
473 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
474
475 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
476 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100477 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
478 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000479 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100480 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000481 DCHECK(instruction_->IsInstanceFieldGet() ||
482 instruction_->IsStaticFieldGet() ||
483 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100484 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000485 instruction_->IsLoadClass() ||
486 instruction_->IsLoadString() ||
487 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100488 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100489 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
490 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000491 << "Unexpected instruction in read barrier marking slow path: "
492 << instruction_->DebugName();
493
494 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100495 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000496 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000498 }
Roland Levillain4359e612016-07-20 11:32:19 +0100499 // No need to save live registers; it's taken care of by the
500 // entrypoint. Also, there is no need to update the stack mask,
501 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000502 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 DCHECK_NE(ref_reg, RSP);
504 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100505 // "Compact" slow path, saving two moves.
506 //
507 // Instead of using the standard runtime calling convention (input
508 // and output in R0):
509 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100510 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100511 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100513 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100515 // of a dedicated entrypoint:
516 //
517 // rX <- ReadBarrierMarkRegX(rX)
518 //
519 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100520 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100521 // This runtime call does not require a stack map.
522 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000523 __ jmp(GetExitLabel());
524 }
525
526 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100527 // The location (register) of the marked object reference.
528 const Location ref_;
529 // Should the reference in `ref_` be unpoisoned prior to marking it?
530 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000531
532 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
533};
534
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100535// Slow path marking an object reference `ref` during a read barrier,
536// and if needed, atomically updating the field `obj.field` in the
537// object `obj` holding this reference after marking (contrary to
538// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
539// `obj.field`).
540//
541// This means that after the execution of this slow path, both `ref`
542// and `obj.field` will be up-to-date; i.e., after the flip, both will
543// hold the same to-space reference (unless another thread installed
544// another object reference (different from `ref`) in `obj.field`).
545class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
546 public:
547 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
548 Location ref,
549 CpuRegister obj,
550 const Address& field_addr,
551 bool unpoison_ref_before_marking,
552 CpuRegister temp1,
553 CpuRegister temp2)
554 : SlowPathCode(instruction),
555 ref_(ref),
556 obj_(obj),
557 field_addr_(field_addr),
558 unpoison_ref_before_marking_(unpoison_ref_before_marking),
559 temp1_(temp1),
560 temp2_(temp2) {
561 DCHECK(kEmitCompilerReadBarrier);
562 }
563
564 const char* GetDescription() const OVERRIDE {
565 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
566 }
567
568 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
569 LocationSummary* locations = instruction_->GetLocations();
570 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
571 Register ref_reg = ref_cpu_reg.AsRegister();
572 DCHECK(locations->CanCall());
573 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
574 // This slow path is only used by the UnsafeCASObject intrinsic.
575 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
576 << "Unexpected instruction in read barrier marking and field updating slow path: "
577 << instruction_->DebugName();
578 DCHECK(instruction_->GetLocations()->Intrinsified());
579 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
580
581 __ Bind(GetEntryLabel());
582 if (unpoison_ref_before_marking_) {
583 // Object* ref = ref_addr->AsMirrorPtr()
584 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
585 }
586
587 // Save the old (unpoisoned) reference.
588 __ movl(temp1_, ref_cpu_reg);
589
590 // No need to save live registers; it's taken care of by the
591 // entrypoint. Also, there is no need to update the stack mask,
592 // as this runtime call will not trigger a garbage collection.
593 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
594 DCHECK_NE(ref_reg, RSP);
595 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
596 // "Compact" slow path, saving two moves.
597 //
598 // Instead of using the standard runtime calling convention (input
599 // and output in R0):
600 //
601 // RDI <- ref
602 // RAX <- ReadBarrierMark(RDI)
603 // ref <- RAX
604 //
605 // we just use rX (the register containing `ref`) as input and output
606 // of a dedicated entrypoint:
607 //
608 // rX <- ReadBarrierMarkRegX(rX)
609 //
610 int32_t entry_point_offset =
611 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
612 // This runtime call does not require a stack map.
613 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
614
615 // If the new reference is different from the old reference,
616 // update the field in the holder (`*field_addr`).
617 //
618 // Note that this field could also hold a different object, if
619 // another thread had concurrently changed it. In that case, the
620 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
621 // operation below would abort the CAS, leaving the field as-is.
622 NearLabel done;
623 __ cmpl(temp1_, ref_cpu_reg);
624 __ j(kEqual, &done);
625
626 // Update the the holder's field atomically. This may fail if
627 // mutator updates before us, but it's OK. This is achived
628 // using a strong compare-and-set (CAS) operation with relaxed
629 // memory synchronization ordering, where the expected value is
630 // the old reference and the desired value is the new reference.
631 // This operation is implemented with a 32-bit LOCK CMPXLCHG
632 // instruction, which requires the expected value (the old
633 // reference) to be in EAX. Save RAX beforehand, and move the
634 // expected value (stored in `temp1_`) into EAX.
635 __ movq(temp2_, CpuRegister(RAX));
636 __ movl(CpuRegister(RAX), temp1_);
637
638 // Convenience aliases.
639 CpuRegister base = obj_;
640 CpuRegister expected = CpuRegister(RAX);
641 CpuRegister value = ref_cpu_reg;
642
643 bool base_equals_value = (base.AsRegister() == value.AsRegister());
644 Register value_reg = ref_reg;
645 if (kPoisonHeapReferences) {
646 if (base_equals_value) {
647 // If `base` and `value` are the same register location, move
648 // `value_reg` to a temporary register. This way, poisoning
649 // `value_reg` won't invalidate `base`.
650 value_reg = temp1_.AsRegister();
651 __ movl(CpuRegister(value_reg), base);
652 }
653
654 // Check that the register allocator did not assign the location
655 // of `expected` (RAX) to `value` nor to `base`, so that heap
656 // poisoning (when enabled) works as intended below.
657 // - If `value` were equal to `expected`, both references would
658 // be poisoned twice, meaning they would not be poisoned at
659 // all, as heap poisoning uses address negation.
660 // - If `base` were equal to `expected`, poisoning `expected`
661 // would invalidate `base`.
662 DCHECK_NE(value_reg, expected.AsRegister());
663 DCHECK_NE(base.AsRegister(), expected.AsRegister());
664
665 __ PoisonHeapReference(expected);
666 __ PoisonHeapReference(CpuRegister(value_reg));
667 }
668
669 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
670
671 // If heap poisoning is enabled, we need to unpoison the values
672 // that were poisoned earlier.
673 if (kPoisonHeapReferences) {
674 if (base_equals_value) {
675 // `value_reg` has been moved to a temporary register, no need
676 // to unpoison it.
677 } else {
678 __ UnpoisonHeapReference(CpuRegister(value_reg));
679 }
680 // No need to unpoison `expected` (RAX), as it is be overwritten below.
681 }
682
683 // Restore RAX.
684 __ movq(CpuRegister(RAX), temp2_);
685
686 __ Bind(&done);
687 __ jmp(GetExitLabel());
688 }
689
690 private:
691 // The location (register) of the marked object reference.
692 const Location ref_;
693 // The register containing the object holding the marked object reference field.
694 const CpuRegister obj_;
695 // The address of the marked reference field. The base of this address must be `obj_`.
696 const Address field_addr_;
697
698 // Should the reference in `ref_` be unpoisoned prior to marking it?
699 const bool unpoison_ref_before_marking_;
700
701 const CpuRegister temp1_;
702 const CpuRegister temp2_;
703
704 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
705};
706
Roland Levillain0d5a2812015-11-13 10:07:31 +0000707// Slow path generating a read barrier for a heap reference.
708class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
709 public:
710 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
711 Location out,
712 Location ref,
713 Location obj,
714 uint32_t offset,
715 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000716 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000717 out_(out),
718 ref_(ref),
719 obj_(obj),
720 offset_(offset),
721 index_(index) {
722 DCHECK(kEmitCompilerReadBarrier);
723 // If `obj` is equal to `out` or `ref`, it means the initial
724 // object has been overwritten by (or after) the heap object
725 // reference load to be instrumented, e.g.:
726 //
727 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000728 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000729 //
730 // In that case, we have lost the information about the original
731 // object, and the emitted read barrier cannot work properly.
732 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
733 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
734}
735
736 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
737 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
738 LocationSummary* locations = instruction_->GetLocations();
739 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
740 DCHECK(locations->CanCall());
741 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100742 DCHECK(instruction_->IsInstanceFieldGet() ||
743 instruction_->IsStaticFieldGet() ||
744 instruction_->IsArrayGet() ||
745 instruction_->IsInstanceOf() ||
746 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100747 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000748 << "Unexpected instruction in read barrier for heap reference slow path: "
749 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000750
751 __ Bind(GetEntryLabel());
752 SaveLiveRegisters(codegen, locations);
753
754 // We may have to change the index's value, but as `index_` is a
755 // constant member (like other "inputs" of this slow path),
756 // introduce a copy of it, `index`.
757 Location index = index_;
758 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100759 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000760 if (instruction_->IsArrayGet()) {
761 // Compute real offset and store it in index_.
762 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
763 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
764 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
765 // We are about to change the value of `index_reg` (see the
766 // calls to art::x86_64::X86_64Assembler::shll and
767 // art::x86_64::X86_64Assembler::AddImmediate below), but it
768 // has not been saved by the previous call to
769 // art::SlowPathCode::SaveLiveRegisters, as it is a
770 // callee-save register --
771 // art::SlowPathCode::SaveLiveRegisters does not consider
772 // callee-save registers, as it has been designed with the
773 // assumption that callee-save registers are supposed to be
774 // handled by the called function. So, as a callee-save
775 // register, `index_reg` _would_ eventually be saved onto
776 // the stack, but it would be too late: we would have
777 // changed its value earlier. Therefore, we manually save
778 // it here into another freely available register,
779 // `free_reg`, chosen of course among the caller-save
780 // registers (as a callee-save `free_reg` register would
781 // exhibit the same problem).
782 //
783 // Note we could have requested a temporary register from
784 // the register allocator instead; but we prefer not to, as
785 // this is a slow path, and we know we can find a
786 // caller-save register that is available.
787 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
788 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
789 index_reg = free_reg;
790 index = Location::RegisterLocation(index_reg);
791 } else {
792 // The initial register stored in `index_` has already been
793 // saved in the call to art::SlowPathCode::SaveLiveRegisters
794 // (as it is not a callee-save register), so we can freely
795 // use it.
796 }
797 // Shifting the index value contained in `index_reg` by the
798 // scale factor (2) cannot overflow in practice, as the
799 // runtime is unable to allocate object arrays with a size
800 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
801 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
802 static_assert(
803 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
804 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
805 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
806 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100807 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
808 // intrinsics, `index_` is not shifted by a scale factor of 2
809 // (as in the case of ArrayGet), as it is actually an offset
810 // to an object field within an object.
811 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000812 DCHECK(instruction_->GetLocations()->Intrinsified());
813 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
814 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
815 << instruction_->AsInvoke()->GetIntrinsic();
816 DCHECK_EQ(offset_, 0U);
817 DCHECK(index_.IsRegister());
818 }
819 }
820
821 // We're moving two or three locations to locations that could
822 // overlap, so we need a parallel move resolver.
823 InvokeRuntimeCallingConvention calling_convention;
824 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
825 parallel_move.AddMove(ref_,
826 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
827 Primitive::kPrimNot,
828 nullptr);
829 parallel_move.AddMove(obj_,
830 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
831 Primitive::kPrimNot,
832 nullptr);
833 if (index.IsValid()) {
834 parallel_move.AddMove(index,
835 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
836 Primitive::kPrimInt,
837 nullptr);
838 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
839 } else {
840 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
841 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
842 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100843 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000844 instruction_,
845 instruction_->GetDexPc(),
846 this);
847 CheckEntrypointTypes<
848 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
849 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
850
851 RestoreLiveRegisters(codegen, locations);
852 __ jmp(GetExitLabel());
853 }
854
855 const char* GetDescription() const OVERRIDE {
856 return "ReadBarrierForHeapReferenceSlowPathX86_64";
857 }
858
859 private:
860 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
861 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
862 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
863 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
864 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
865 return static_cast<CpuRegister>(i);
866 }
867 }
868 // We shall never fail to find a free caller-save register, as
869 // there are more than two core caller-save registers on x86-64
870 // (meaning it is possible to find one which is different from
871 // `ref` and `obj`).
872 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
873 LOG(FATAL) << "Could not find a free caller-save register";
874 UNREACHABLE();
875 }
876
Roland Levillain0d5a2812015-11-13 10:07:31 +0000877 const Location out_;
878 const Location ref_;
879 const Location obj_;
880 const uint32_t offset_;
881 // An additional location containing an index to an array.
882 // Only used for HArrayGet and the UnsafeGetObject &
883 // UnsafeGetObjectVolatile intrinsics.
884 const Location index_;
885
886 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
887};
888
889// Slow path generating a read barrier for a GC root.
890class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
891 public:
892 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000893 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000894 DCHECK(kEmitCompilerReadBarrier);
895 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000896
897 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
898 LocationSummary* locations = instruction_->GetLocations();
899 DCHECK(locations->CanCall());
900 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000901 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
902 << "Unexpected instruction in read barrier for GC root slow path: "
903 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000904
905 __ Bind(GetEntryLabel());
906 SaveLiveRegisters(codegen, locations);
907
908 InvokeRuntimeCallingConvention calling_convention;
909 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
910 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100911 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000912 instruction_,
913 instruction_->GetDexPc(),
914 this);
915 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
916 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
917
918 RestoreLiveRegisters(codegen, locations);
919 __ jmp(GetExitLabel());
920 }
921
922 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
923
924 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000925 const Location out_;
926 const Location root_;
927
928 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
929};
930
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100931#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100932// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
933#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100934
Roland Levillain4fa13f62015-07-06 18:11:54 +0100935inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700936 switch (cond) {
937 case kCondEQ: return kEqual;
938 case kCondNE: return kNotEqual;
939 case kCondLT: return kLess;
940 case kCondLE: return kLessEqual;
941 case kCondGT: return kGreater;
942 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700943 case kCondB: return kBelow;
944 case kCondBE: return kBelowEqual;
945 case kCondA: return kAbove;
946 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700947 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100948 LOG(FATAL) << "Unreachable";
949 UNREACHABLE();
950}
951
Aart Bike9f37602015-10-09 11:15:55 -0700952// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100953inline Condition X86_64FPCondition(IfCondition cond) {
954 switch (cond) {
955 case kCondEQ: return kEqual;
956 case kCondNE: return kNotEqual;
957 case kCondLT: return kBelow;
958 case kCondLE: return kBelowEqual;
959 case kCondGT: return kAbove;
960 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700961 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100962 };
963 LOG(FATAL) << "Unreachable";
964 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700965}
966
Vladimir Markodc151b22015-10-15 18:02:30 +0100967HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
968 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100969 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100970 switch (desired_dispatch_info.code_ptr_location) {
971 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
972 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
973 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
974 return HInvokeStaticOrDirect::DispatchInfo {
975 desired_dispatch_info.method_load_kind,
976 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
977 desired_dispatch_info.method_load_data,
978 0u
979 };
980 default:
981 return desired_dispatch_info;
982 }
983}
984
Serguei Katkov288c7a82016-05-16 11:53:15 +0600985Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
986 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800987 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000988 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
989 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100990 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000991 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100992 uint32_t offset =
993 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
994 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000995 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100996 }
Vladimir Marko58155012015-08-19 12:49:41 +0000997 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000998 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000999 break;
1000 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
1001 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
1002 break;
1003 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
1004 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001005 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
1006 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00001007 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
1008 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001009 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +00001010 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001011 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001012 // Bind a new fixup label at the end of the "movl" insn.
1013 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001014 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko58155012015-08-19 12:49:41 +00001015 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001016 }
Vladimir Marko58155012015-08-19 12:49:41 +00001017 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001018 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00001019 Register method_reg;
1020 CpuRegister reg = temp.AsRegister<CpuRegister>();
1021 if (current_method.IsRegister()) {
1022 method_reg = current_method.AsRegister<Register>();
1023 } else {
1024 DCHECK(invoke->GetLocations()->Intrinsified());
1025 DCHECK(!current_method.IsValid());
1026 method_reg = reg.AsRegister();
1027 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
1028 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001029 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01001030 __ movq(reg,
1031 Address(CpuRegister(method_reg),
1032 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01001033 // temp = temp[index_in_cache];
1034 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
1035 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00001036 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
1037 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01001038 }
Vladimir Marko58155012015-08-19 12:49:41 +00001039 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06001040 return callee_method;
1041}
1042
1043void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
1044 Location temp) {
1045 // All registers are assumed to be correctly set up.
1046 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00001047
1048 switch (invoke->GetCodePtrLocation()) {
1049 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1050 __ call(&frame_entry_label_);
1051 break;
1052 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001053 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
1054 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00001055 Label* label = &relative_call_patches_.back().label;
1056 __ call(label); // Bind to the patch label, override at link time.
1057 __ Bind(label); // Bind the label at the end of the "call" insn.
1058 break;
1059 }
1060 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
1061 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01001062 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
1063 LOG(FATAL) << "Unsupported";
1064 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00001065 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1066 // (callee_method + offset_of_quick_compiled_code)()
1067 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1068 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001069 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001070 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001071 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001072
1073 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001074}
1075
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001076void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
1077 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1078 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1079 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001080
1081 // Use the calling convention instead of the location of the receiver, as
1082 // intrinsics may have put the receiver in a different register. In the intrinsics
1083 // slow path, the arguments have been moved to the right place, so here we are
1084 // guaranteed that the receiver is the first register of the calling convention.
1085 InvokeDexCallingConvention calling_convention;
1086 Register receiver = calling_convention.GetRegisterAt(0);
1087
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001088 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001089 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001090 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001091 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001092 // Instead of simply (possibly) unpoisoning `temp` here, we should
1093 // emit a read barrier for the previous class reference load.
1094 // However this is not required in practice, as this is an
1095 // intermediate/temporary reference and because the current
1096 // concurrent copying collector keeps the from-space memory
1097 // intact/accessible until the end of the marking phase (the
1098 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001099 __ MaybeUnpoisonHeapReference(temp);
1100 // temp = temp->GetMethodAt(method_offset);
1101 __ movq(temp, Address(temp, method_offset));
1102 // call temp->GetEntryPoint();
1103 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001104 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001105}
1106
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001107void CodeGeneratorX86_64::RecordSimplePatch() {
1108 if (GetCompilerOptions().GetIncludePatchInformation()) {
1109 simple_patches_.emplace_back();
1110 __ Bind(&simple_patches_.back());
1111 }
1112}
1113
Vladimir Markoaad75c62016-10-03 08:46:48 +00001114void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
1115 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001116 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
1117 __ Bind(&string_patches_.back().label);
1118}
1119
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001120void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
1121 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
1122 __ Bind(&type_patches_.back().label);
1123}
1124
Vladimir Markoaad75c62016-10-03 08:46:48 +00001125Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1126 DCHECK(!GetCompilerOptions().IsBootImage());
1127 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
1128 return &string_patches_.back().label;
1129}
1130
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001131Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
1132 uint32_t element_offset) {
1133 // Add a patch entry and return the label.
1134 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
1135 return &pc_relative_dex_cache_patches_.back().label;
1136}
1137
Vladimir Markoaad75c62016-10-03 08:46:48 +00001138// The label points to the end of the "movl" or another instruction but the literal offset
1139// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1140constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1141
1142template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1143inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1144 const ArenaDeque<PatchInfo<Label>>& infos,
1145 ArenaVector<LinkerPatch>* linker_patches) {
1146 for (const PatchInfo<Label>& info : infos) {
1147 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1148 linker_patches->push_back(
1149 Factory(literal_offset, &info.dex_file, info.label.Position(), info.index));
1150 }
1151}
1152
Vladimir Marko58155012015-08-19 12:49:41 +00001153void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1154 DCHECK(linker_patches->empty());
1155 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001156 method_patches_.size() +
1157 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001158 pc_relative_dex_cache_patches_.size() +
1159 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001160 string_patches_.size() +
1161 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001162 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001163 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001164 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001165 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00001166 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001167 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001168 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001169 linker_patches->push_back(
1170 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00001171 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001172 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1173 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001174 for (const Label& label : simple_patches_) {
1175 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1176 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1177 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001178 if (!GetCompilerOptions().IsBootImage()) {
1179 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
1180 } else {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001181 // These are always PC-relative, see GetSupportedLoadStringKind().
Vladimir Markoaad75c62016-10-03 08:46:48 +00001182 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001183 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001184 // These are always PC-relative, see GetSupportedLoadClassKind().
1185 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
Vladimir Marko58155012015-08-19 12:49:41 +00001186}
1187
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001188void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001189 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001190}
1191
1192void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001193 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001194}
1195
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001196size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1197 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1198 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001199}
1200
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001201size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1202 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1203 return kX86_64WordSize;
1204}
1205
1206size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1207 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1208 return kX86_64WordSize;
1209}
1210
1211size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1212 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1213 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001214}
1215
Calin Juravle175dc732015-08-25 15:42:32 +01001216void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1217 HInstruction* instruction,
1218 uint32_t dex_pc,
1219 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001220 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001221 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1222 if (EntrypointRequiresStackMap(entrypoint)) {
1223 RecordPcInfo(instruction, dex_pc, slow_path);
1224 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001225}
1226
Roland Levillaindec8f632016-07-22 17:10:06 +01001227void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1228 HInstruction* instruction,
1229 SlowPathCode* slow_path) {
1230 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001231 GenerateInvokeRuntime(entry_point_offset);
1232}
1233
1234void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001235 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1236}
1237
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001238static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001239// Use a fake return address register to mimic Quick.
1240static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001241CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001242 const X86_64InstructionSetFeatures& isa_features,
1243 const CompilerOptions& compiler_options,
1244 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001245 : CodeGenerator(graph,
1246 kNumberOfCpuRegisters,
1247 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001248 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001249 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1250 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001251 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001252 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1253 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001254 compiler_options,
1255 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001256 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001257 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001258 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001259 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001260 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001261 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001262 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001263 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1264 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001265 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001266 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1267 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001268 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray4acd0362016-11-09 17:30:31 +00001269 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001270 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1271}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001272
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001273InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1274 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001275 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001276 assembler_(codegen->GetAssembler()),
1277 codegen_(codegen) {}
1278
David Brazdil58282f42016-01-14 12:45:10 +00001279void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001280 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001281 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001282
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001283 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001284 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001285}
1286
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001287static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001288 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001289}
David Srbecky9d8606d2015-04-12 09:35:32 +01001290
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001291static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001292 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001293}
1294
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001295void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001296 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001297 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001298 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001299 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001300 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001301
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001302 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001303 __ testq(CpuRegister(RAX), Address(
1304 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001305 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001306 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001307
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001308 if (HasEmptyFrame()) {
1309 return;
1310 }
1311
Nicolas Geoffray98893962015-01-21 12:32:32 +00001312 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001313 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001314 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001315 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001316 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1317 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001318 }
1319 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001320
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001321 int adjust = GetFrameSize() - GetCoreSpillSize();
1322 __ subq(CpuRegister(RSP), Immediate(adjust));
1323 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001324 uint32_t xmm_spill_location = GetFpuSpillStart();
1325 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001326
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001327 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1328 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001329 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1330 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1331 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001332 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001333 }
1334
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001335 // Save the current method if we need it. Note that we do not
1336 // do this in HCurrentMethod, as the instruction might have been removed
1337 // in the SSA graph.
1338 if (RequiresCurrentMethod()) {
1339 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1340 CpuRegister(kMethodRegisterArgument));
1341 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001342}
1343
1344void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001345 __ cfi().RememberState();
1346 if (!HasEmptyFrame()) {
1347 uint32_t xmm_spill_location = GetFpuSpillStart();
1348 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1349 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1350 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1351 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1352 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1353 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1354 }
1355 }
1356
1357 int adjust = GetFrameSize() - GetCoreSpillSize();
1358 __ addq(CpuRegister(RSP), Immediate(adjust));
1359 __ cfi().AdjustCFAOffset(-adjust);
1360
1361 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1362 Register reg = kCoreCalleeSaves[i];
1363 if (allocated_registers_.ContainsCoreRegister(reg)) {
1364 __ popq(CpuRegister(reg));
1365 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1366 __ cfi().Restore(DWARFReg(reg));
1367 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001368 }
1369 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001370 __ ret();
1371 __ cfi().RestoreState();
1372 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001373}
1374
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001375void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1376 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001377}
1378
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001379void CodeGeneratorX86_64::Move(Location destination, Location source) {
1380 if (source.Equals(destination)) {
1381 return;
1382 }
1383 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001384 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001385 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001386 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001387 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001388 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001389 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001390 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1391 } else if (source.IsConstant()) {
1392 HConstant* constant = source.GetConstant();
1393 if (constant->IsLongConstant()) {
1394 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1395 } else {
1396 Load32BitValue(dest, GetInt32ValueOf(constant));
1397 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001398 } else {
1399 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001400 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001401 }
1402 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001403 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001404 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001405 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001406 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001407 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1408 } else if (source.IsConstant()) {
1409 HConstant* constant = source.GetConstant();
1410 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1411 if (constant->IsFloatConstant()) {
1412 Load32BitValue(dest, static_cast<int32_t>(value));
1413 } else {
1414 Load64BitValue(dest, value);
1415 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001416 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001417 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001418 } else {
1419 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001420 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001421 }
1422 } else if (destination.IsStackSlot()) {
1423 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001424 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001425 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001426 } else if (source.IsFpuRegister()) {
1427 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001428 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001429 } else if (source.IsConstant()) {
1430 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001431 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001432 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001433 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001434 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001435 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1436 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001437 }
1438 } else {
1439 DCHECK(destination.IsDoubleStackSlot());
1440 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001441 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001442 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001443 } else if (source.IsFpuRegister()) {
1444 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001445 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001446 } else if (source.IsConstant()) {
1447 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001448 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1449 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001450 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001451 } else {
1452 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001453 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1454 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001455 }
1456 }
1457}
1458
Calin Juravle175dc732015-08-25 15:42:32 +01001459void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1460 DCHECK(location.IsRegister());
1461 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1462}
1463
Calin Juravlee460d1d2015-09-29 04:52:17 +01001464void CodeGeneratorX86_64::MoveLocation(
1465 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1466 Move(dst, src);
1467}
1468
1469void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1470 if (location.IsRegister()) {
1471 locations->AddTemp(location);
1472 } else {
1473 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1474 }
1475}
1476
David Brazdilfc6a86a2015-06-26 10:33:45 +00001477void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001478 DCHECK(!successor->IsExitBlock());
1479
1480 HBasicBlock* block = got->GetBlock();
1481 HInstruction* previous = got->GetPrevious();
1482
1483 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001484 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001485 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1486 return;
1487 }
1488
1489 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1490 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1491 }
1492 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001493 __ jmp(codegen_->GetLabelOf(successor));
1494 }
1495}
1496
David Brazdilfc6a86a2015-06-26 10:33:45 +00001497void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1498 got->SetLocations(nullptr);
1499}
1500
1501void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1502 HandleGoto(got, got->GetSuccessor());
1503}
1504
1505void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1506 try_boundary->SetLocations(nullptr);
1507}
1508
1509void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1510 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1511 if (!successor->IsExitBlock()) {
1512 HandleGoto(try_boundary, successor);
1513 }
1514}
1515
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001516void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1517 exit->SetLocations(nullptr);
1518}
1519
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001520void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001521}
1522
Mark Mendell152408f2015-12-31 12:28:50 -05001523template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001524void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001525 LabelType* true_label,
1526 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001527 if (cond->IsFPConditionTrueIfNaN()) {
1528 __ j(kUnordered, true_label);
1529 } else if (cond->IsFPConditionFalseIfNaN()) {
1530 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001531 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001532 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001533}
1534
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001535void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001536 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001537
Mark Mendellc4701932015-04-10 13:18:51 -04001538 Location left = locations->InAt(0);
1539 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001540 Primitive::Type type = condition->InputAt(0)->GetType();
1541 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001542 case Primitive::kPrimBoolean:
1543 case Primitive::kPrimByte:
1544 case Primitive::kPrimChar:
1545 case Primitive::kPrimShort:
1546 case Primitive::kPrimInt:
1547 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001548 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001549 break;
1550 }
Mark Mendellc4701932015-04-10 13:18:51 -04001551 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001552 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001553 break;
1554 }
1555 case Primitive::kPrimFloat: {
1556 if (right.IsFpuRegister()) {
1557 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1558 } else if (right.IsConstant()) {
1559 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1560 codegen_->LiteralFloatAddress(
1561 right.GetConstant()->AsFloatConstant()->GetValue()));
1562 } else {
1563 DCHECK(right.IsStackSlot());
1564 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1565 Address(CpuRegister(RSP), right.GetStackIndex()));
1566 }
Mark Mendellc4701932015-04-10 13:18:51 -04001567 break;
1568 }
1569 case Primitive::kPrimDouble: {
1570 if (right.IsFpuRegister()) {
1571 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1572 } else if (right.IsConstant()) {
1573 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1574 codegen_->LiteralDoubleAddress(
1575 right.GetConstant()->AsDoubleConstant()->GetValue()));
1576 } else {
1577 DCHECK(right.IsDoubleStackSlot());
1578 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1579 Address(CpuRegister(RSP), right.GetStackIndex()));
1580 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001581 break;
1582 }
1583 default:
1584 LOG(FATAL) << "Unexpected condition type " << type;
1585 }
1586}
1587
1588template<class LabelType>
1589void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1590 LabelType* true_target_in,
1591 LabelType* false_target_in) {
1592 // Generated branching requires both targets to be explicit. If either of the
1593 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1594 LabelType fallthrough_target;
1595 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1596 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1597
1598 // Generate the comparison to set the CC.
1599 GenerateCompareTest(condition);
1600
1601 // Now generate the correct jump(s).
1602 Primitive::Type type = condition->InputAt(0)->GetType();
1603 switch (type) {
1604 case Primitive::kPrimLong: {
1605 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1606 break;
1607 }
1608 case Primitive::kPrimFloat: {
1609 GenerateFPJumps(condition, true_target, false_target);
1610 break;
1611 }
1612 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001613 GenerateFPJumps(condition, true_target, false_target);
1614 break;
1615 }
1616 default:
1617 LOG(FATAL) << "Unexpected condition type " << type;
1618 }
1619
David Brazdil0debae72015-11-12 18:37:00 +00001620 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001621 __ jmp(false_target);
1622 }
David Brazdil0debae72015-11-12 18:37:00 +00001623
1624 if (fallthrough_target.IsLinked()) {
1625 __ Bind(&fallthrough_target);
1626 }
Mark Mendellc4701932015-04-10 13:18:51 -04001627}
1628
David Brazdil0debae72015-11-12 18:37:00 +00001629static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1630 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1631 // are set only strictly before `branch`. We can't use the eflags on long
1632 // conditions if they are materialized due to the complex branching.
1633 return cond->IsCondition() &&
1634 cond->GetNext() == branch &&
1635 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1636}
1637
Mark Mendell152408f2015-12-31 12:28:50 -05001638template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001639void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001640 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001641 LabelType* true_target,
1642 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001643 HInstruction* cond = instruction->InputAt(condition_input_index);
1644
1645 if (true_target == nullptr && false_target == nullptr) {
1646 // Nothing to do. The code always falls through.
1647 return;
1648 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001649 // Constant condition, statically compared against "true" (integer value 1).
1650 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001651 if (true_target != nullptr) {
1652 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001653 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001654 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001655 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001656 if (false_target != nullptr) {
1657 __ jmp(false_target);
1658 }
1659 }
1660 return;
1661 }
1662
1663 // The following code generates these patterns:
1664 // (1) true_target == nullptr && false_target != nullptr
1665 // - opposite condition true => branch to false_target
1666 // (2) true_target != nullptr && false_target == nullptr
1667 // - condition true => branch to true_target
1668 // (3) true_target != nullptr && false_target != nullptr
1669 // - condition true => branch to true_target
1670 // - branch to false_target
1671 if (IsBooleanValueOrMaterializedCondition(cond)) {
1672 if (AreEflagsSetFrom(cond, instruction)) {
1673 if (true_target == nullptr) {
1674 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1675 } else {
1676 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1677 }
1678 } else {
1679 // Materialized condition, compare against 0.
1680 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1681 if (lhs.IsRegister()) {
1682 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1683 } else {
1684 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1685 }
1686 if (true_target == nullptr) {
1687 __ j(kEqual, false_target);
1688 } else {
1689 __ j(kNotEqual, true_target);
1690 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001691 }
1692 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001693 // Condition has not been materialized, use its inputs as the
1694 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001695 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001696
David Brazdil0debae72015-11-12 18:37:00 +00001697 // If this is a long or FP comparison that has been folded into
1698 // the HCondition, generate the comparison directly.
1699 Primitive::Type type = condition->InputAt(0)->GetType();
1700 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1701 GenerateCompareTestAndBranch(condition, true_target, false_target);
1702 return;
1703 }
1704
1705 Location lhs = condition->GetLocations()->InAt(0);
1706 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001707 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001708 if (true_target == nullptr) {
1709 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1710 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001711 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001712 }
Dave Allison20dfc792014-06-16 20:44:29 -07001713 }
David Brazdil0debae72015-11-12 18:37:00 +00001714
1715 // If neither branch falls through (case 3), the conditional branch to `true_target`
1716 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1717 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001718 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001719 }
1720}
1721
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001722void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001723 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1724 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001725 locations->SetInAt(0, Location::Any());
1726 }
1727}
1728
1729void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001730 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1731 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1732 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1733 nullptr : codegen_->GetLabelOf(true_successor);
1734 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1735 nullptr : codegen_->GetLabelOf(false_successor);
1736 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001737}
1738
1739void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1740 LocationSummary* locations = new (GetGraph()->GetArena())
1741 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001742 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001743 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001744 locations->SetInAt(0, Location::Any());
1745 }
1746}
1747
1748void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001749 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001750 GenerateTestAndBranch<Label>(deoptimize,
1751 /* condition_input_index */ 0,
1752 slow_path->GetEntryLabel(),
1753 /* false_target */ nullptr);
1754}
1755
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001756static bool SelectCanUseCMOV(HSelect* select) {
1757 // There are no conditional move instructions for XMMs.
1758 if (Primitive::IsFloatingPointType(select->GetType())) {
1759 return false;
1760 }
1761
1762 // A FP condition doesn't generate the single CC that we need.
1763 HInstruction* condition = select->GetCondition();
1764 if (condition->IsCondition() &&
1765 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1766 return false;
1767 }
1768
1769 // We can generate a CMOV for this Select.
1770 return true;
1771}
1772
David Brazdil74eb1b22015-12-14 11:44:01 +00001773void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1774 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1775 if (Primitive::IsFloatingPointType(select->GetType())) {
1776 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001777 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001778 } else {
1779 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001780 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001781 if (select->InputAt(1)->IsConstant()) {
1782 locations->SetInAt(1, Location::RequiresRegister());
1783 } else {
1784 locations->SetInAt(1, Location::Any());
1785 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001786 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001787 locations->SetInAt(1, Location::Any());
1788 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001789 }
1790 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1791 locations->SetInAt(2, Location::RequiresRegister());
1792 }
1793 locations->SetOut(Location::SameAsFirstInput());
1794}
1795
1796void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1797 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001798 if (SelectCanUseCMOV(select)) {
1799 // If both the condition and the source types are integer, we can generate
1800 // a CMOV to implement Select.
1801 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001802 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001803 DCHECK(locations->InAt(0).Equals(locations->Out()));
1804
1805 HInstruction* select_condition = select->GetCondition();
1806 Condition cond = kNotEqual;
1807
1808 // Figure out how to test the 'condition'.
1809 if (select_condition->IsCondition()) {
1810 HCondition* condition = select_condition->AsCondition();
1811 if (!condition->IsEmittedAtUseSite()) {
1812 // This was a previously materialized condition.
1813 // Can we use the existing condition code?
1814 if (AreEflagsSetFrom(condition, select)) {
1815 // Materialization was the previous instruction. Condition codes are right.
1816 cond = X86_64IntegerCondition(condition->GetCondition());
1817 } else {
1818 // No, we have to recreate the condition code.
1819 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1820 __ testl(cond_reg, cond_reg);
1821 }
1822 } else {
1823 GenerateCompareTest(condition);
1824 cond = X86_64IntegerCondition(condition->GetCondition());
1825 }
1826 } else {
1827 // Must be a boolean condition, which needs to be compared to 0.
1828 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1829 __ testl(cond_reg, cond_reg);
1830 }
1831
1832 // If the condition is true, overwrite the output, which already contains false.
1833 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001834 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1835 if (value_true_loc.IsRegister()) {
1836 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1837 } else {
1838 __ cmov(cond,
1839 value_false,
1840 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1841 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001842 } else {
1843 NearLabel false_target;
1844 GenerateTestAndBranch<NearLabel>(select,
1845 /* condition_input_index */ 2,
1846 /* true_target */ nullptr,
1847 &false_target);
1848 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1849 __ Bind(&false_target);
1850 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001851}
1852
David Srbecky0cf44932015-12-09 14:09:59 +00001853void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1854 new (GetGraph()->GetArena()) LocationSummary(info);
1855}
1856
David Srbeckyd28f4a02016-03-14 17:14:24 +00001857void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1858 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001859}
1860
1861void CodeGeneratorX86_64::GenerateNop() {
1862 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001863}
1864
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001865void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001866 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001867 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001868 // Handle the long/FP comparisons made in instruction simplification.
1869 switch (cond->InputAt(0)->GetType()) {
1870 case Primitive::kPrimLong:
1871 locations->SetInAt(0, Location::RequiresRegister());
1872 locations->SetInAt(1, Location::Any());
1873 break;
1874 case Primitive::kPrimFloat:
1875 case Primitive::kPrimDouble:
1876 locations->SetInAt(0, Location::RequiresFpuRegister());
1877 locations->SetInAt(1, Location::Any());
1878 break;
1879 default:
1880 locations->SetInAt(0, Location::RequiresRegister());
1881 locations->SetInAt(1, Location::Any());
1882 break;
1883 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001884 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001885 locations->SetOut(Location::RequiresRegister());
1886 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001887}
1888
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001889void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001890 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001891 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001892 }
Mark Mendellc4701932015-04-10 13:18:51 -04001893
1894 LocationSummary* locations = cond->GetLocations();
1895 Location lhs = locations->InAt(0);
1896 Location rhs = locations->InAt(1);
1897 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001898 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001899
1900 switch (cond->InputAt(0)->GetType()) {
1901 default:
1902 // Integer case.
1903
1904 // Clear output register: setcc only sets the low byte.
1905 __ xorl(reg, reg);
1906
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001907 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001908 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001909 return;
1910 case Primitive::kPrimLong:
1911 // Clear output register: setcc only sets the low byte.
1912 __ xorl(reg, reg);
1913
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001914 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001915 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001916 return;
1917 case Primitive::kPrimFloat: {
1918 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1919 if (rhs.IsConstant()) {
1920 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1921 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1922 } else if (rhs.IsStackSlot()) {
1923 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1924 } else {
1925 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1926 }
1927 GenerateFPJumps(cond, &true_label, &false_label);
1928 break;
1929 }
1930 case Primitive::kPrimDouble: {
1931 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1932 if (rhs.IsConstant()) {
1933 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1934 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1935 } else if (rhs.IsDoubleStackSlot()) {
1936 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1937 } else {
1938 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1939 }
1940 GenerateFPJumps(cond, &true_label, &false_label);
1941 break;
1942 }
1943 }
1944
1945 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001946 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001947
Roland Levillain4fa13f62015-07-06 18:11:54 +01001948 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001949 __ Bind(&false_label);
1950 __ xorl(reg, reg);
1951 __ jmp(&done_label);
1952
Roland Levillain4fa13f62015-07-06 18:11:54 +01001953 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001954 __ Bind(&true_label);
1955 __ movl(reg, Immediate(1));
1956 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001965}
1966
1967void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001985}
1986
1987void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001989}
1990
1991void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001993}
1994
1995void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001997}
1998
1999void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002001}
2002
2003void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002005}
2006
Aart Bike9f37602015-10-09 11:15:55 -07002007void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002009}
2010
2011void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002013}
2014
2015void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002017}
2018
2019void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002020 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002021}
2022
2023void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002024 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002025}
2026
2027void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002028 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002029}
2030
2031void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002032 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002033}
2034
2035void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002036 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002037}
2038
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002039void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002040 LocationSummary* locations =
2041 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002042 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002043 case Primitive::kPrimBoolean:
2044 case Primitive::kPrimByte:
2045 case Primitive::kPrimShort:
2046 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002047 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00002048 case Primitive::kPrimLong: {
2049 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002050 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002051 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2052 break;
2053 }
2054 case Primitive::kPrimFloat:
2055 case Primitive::kPrimDouble: {
2056 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002057 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002058 locations->SetOut(Location::RequiresRegister());
2059 break;
2060 }
2061 default:
2062 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2063 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002064}
2065
2066void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002067 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002068 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002069 Location left = locations->InAt(0);
2070 Location right = locations->InAt(1);
2071
Mark Mendell0c9497d2015-08-21 09:30:05 -04002072 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00002073 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002074 Condition less_cond = kLess;
2075
Calin Juravleddb7df22014-11-25 20:56:51 +00002076 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002077 case Primitive::kPrimBoolean:
2078 case Primitive::kPrimByte:
2079 case Primitive::kPrimShort:
2080 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002081 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002082 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002083 break;
2084 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002085 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002086 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002087 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002088 }
2089 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04002090 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2091 if (right.IsConstant()) {
2092 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2093 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2094 } else if (right.IsStackSlot()) {
2095 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2096 } else {
2097 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2098 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002099 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002100 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002101 break;
2102 }
2103 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04002104 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2105 if (right.IsConstant()) {
2106 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2107 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2108 } else if (right.IsDoubleStackSlot()) {
2109 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2110 } else {
2111 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2112 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002113 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002114 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002115 break;
2116 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002117 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002118 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002119 }
Aart Bika19616e2016-02-01 18:57:58 -08002120
Calin Juravleddb7df22014-11-25 20:56:51 +00002121 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002122 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002123 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002124
Calin Juravle91debbc2014-11-26 19:01:09 +00002125 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002126 __ movl(out, Immediate(1));
2127 __ jmp(&done);
2128
2129 __ Bind(&less);
2130 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002131
2132 __ Bind(&done);
2133}
2134
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002135void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002136 LocationSummary* locations =
2137 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002138 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002139}
2140
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002141void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002142 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002143}
2144
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002145void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2146 LocationSummary* locations =
2147 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2148 locations->SetOut(Location::ConstantLocation(constant));
2149}
2150
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002151void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002152 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002153}
2154
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002155void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002156 LocationSummary* locations =
2157 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002158 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002159}
2160
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002161void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002162 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002163}
2164
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002165void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2166 LocationSummary* locations =
2167 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2168 locations->SetOut(Location::ConstantLocation(constant));
2169}
2170
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002171void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002172 // Will be generated at use site.
2173}
2174
2175void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2176 LocationSummary* locations =
2177 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2178 locations->SetOut(Location::ConstantLocation(constant));
2179}
2180
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002181void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2182 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002183 // Will be generated at use site.
2184}
2185
Calin Juravle27df7582015-04-17 19:12:31 +01002186void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2187 memory_barrier->SetLocations(nullptr);
2188}
2189
2190void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002191 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002192}
2193
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002194void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2195 ret->SetLocations(nullptr);
2196}
2197
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002198void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002199 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002200}
2201
2202void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002203 LocationSummary* locations =
2204 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002205 switch (ret->InputAt(0)->GetType()) {
2206 case Primitive::kPrimBoolean:
2207 case Primitive::kPrimByte:
2208 case Primitive::kPrimChar:
2209 case Primitive::kPrimShort:
2210 case Primitive::kPrimInt:
2211 case Primitive::kPrimNot:
2212 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002213 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002214 break;
2215
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002216 case Primitive::kPrimFloat:
2217 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002218 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002219 break;
2220
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002221 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002222 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002223 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002224}
2225
2226void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2227 if (kIsDebugBuild) {
2228 switch (ret->InputAt(0)->GetType()) {
2229 case Primitive::kPrimBoolean:
2230 case Primitive::kPrimByte:
2231 case Primitive::kPrimChar:
2232 case Primitive::kPrimShort:
2233 case Primitive::kPrimInt:
2234 case Primitive::kPrimNot:
2235 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002236 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002237 break;
2238
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002239 case Primitive::kPrimFloat:
2240 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002242 XMM0);
2243 break;
2244
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002245 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002246 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002247 }
2248 }
2249 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002250}
2251
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002252Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2253 switch (type) {
2254 case Primitive::kPrimBoolean:
2255 case Primitive::kPrimByte:
2256 case Primitive::kPrimChar:
2257 case Primitive::kPrimShort:
2258 case Primitive::kPrimInt:
2259 case Primitive::kPrimNot:
2260 case Primitive::kPrimLong:
2261 return Location::RegisterLocation(RAX);
2262
2263 case Primitive::kPrimVoid:
2264 return Location::NoLocation();
2265
2266 case Primitive::kPrimDouble:
2267 case Primitive::kPrimFloat:
2268 return Location::FpuRegisterLocation(XMM0);
2269 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002270
2271 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002272}
2273
2274Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2275 return Location::RegisterLocation(kMethodRegisterArgument);
2276}
2277
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002278Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002279 switch (type) {
2280 case Primitive::kPrimBoolean:
2281 case Primitive::kPrimByte:
2282 case Primitive::kPrimChar:
2283 case Primitive::kPrimShort:
2284 case Primitive::kPrimInt:
2285 case Primitive::kPrimNot: {
2286 uint32_t index = gp_index_++;
2287 stack_index_++;
2288 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002289 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002290 } else {
2291 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2292 }
2293 }
2294
2295 case Primitive::kPrimLong: {
2296 uint32_t index = gp_index_;
2297 stack_index_ += 2;
2298 if (index < calling_convention.GetNumberOfRegisters()) {
2299 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002300 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002301 } else {
2302 gp_index_ += 2;
2303 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2304 }
2305 }
2306
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002307 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002308 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002309 stack_index_++;
2310 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002311 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002312 } else {
2313 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2314 }
2315 }
2316
2317 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002318 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002319 stack_index_ += 2;
2320 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002321 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002322 } else {
2323 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2324 }
2325 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002326
2327 case Primitive::kPrimVoid:
2328 LOG(FATAL) << "Unexpected parameter type " << type;
2329 break;
2330 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002331 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002332}
2333
Calin Juravle175dc732015-08-25 15:42:32 +01002334void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2335 // The trampoline uses the same calling convention as dex calling conventions,
2336 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2337 // the method_idx.
2338 HandleInvoke(invoke);
2339}
2340
2341void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2342 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2343}
2344
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002345void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002346 // Explicit clinit checks triggered by static invokes must have been pruned by
2347 // art::PrepareForRegisterAllocation.
2348 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002349
Mark Mendellfb8d2792015-03-31 22:16:59 -04002350 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002351 if (intrinsic.TryDispatch(invoke)) {
2352 return;
2353 }
2354
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002355 HandleInvoke(invoke);
2356}
2357
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002358static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2359 if (invoke->GetLocations()->Intrinsified()) {
2360 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2361 intrinsic.Dispatch(invoke);
2362 return true;
2363 }
2364 return false;
2365}
2366
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002367void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002368 // Explicit clinit checks triggered by static invokes must have been pruned by
2369 // art::PrepareForRegisterAllocation.
2370 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002371
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002372 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2373 return;
2374 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002375
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002376 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002377 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002378 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002379 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002380}
2381
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002382void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002383 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002384 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002385}
2386
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002387void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002388 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002389 if (intrinsic.TryDispatch(invoke)) {
2390 return;
2391 }
2392
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002393 HandleInvoke(invoke);
2394}
2395
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002396void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002397 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2398 return;
2399 }
2400
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002401 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002402 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002403 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002404}
2405
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002406void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2407 HandleInvoke(invoke);
2408 // Add the hidden argument.
2409 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2410}
2411
2412void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2413 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002414 LocationSummary* locations = invoke->GetLocations();
2415 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2416 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002417 Location receiver = locations->InAt(0);
2418 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2419
Roland Levillain0d5a2812015-11-13 10:07:31 +00002420 // Set the hidden argument. This is safe to do this here, as RAX
2421 // won't be modified thereafter, before the `call` instruction.
2422 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002423 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002424
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002425 if (receiver.IsStackSlot()) {
2426 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002427 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002428 __ movl(temp, Address(temp, class_offset));
2429 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002430 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002431 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002432 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002433 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002434 // Instead of simply (possibly) unpoisoning `temp` here, we should
2435 // emit a read barrier for the previous class reference load.
2436 // However this is not required in practice, as this is an
2437 // intermediate/temporary reference and because the current
2438 // concurrent copying collector keeps the from-space memory
2439 // intact/accessible until the end of the marking phase (the
2440 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002441 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002442 // temp = temp->GetAddressOfIMT()
2443 __ movq(temp,
2444 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2445 // temp = temp->GetImtEntryAt(method_offset);
2446 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002447 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002448 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002449 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002450 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002451 __ call(Address(
2452 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002453
2454 DCHECK(!codegen_->IsLeafMethod());
2455 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2456}
2457
Roland Levillain88cb1752014-10-20 16:36:47 +01002458void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2459 LocationSummary* locations =
2460 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2461 switch (neg->GetResultType()) {
2462 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002463 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002464 locations->SetInAt(0, Location::RequiresRegister());
2465 locations->SetOut(Location::SameAsFirstInput());
2466 break;
2467
Roland Levillain88cb1752014-10-20 16:36:47 +01002468 case Primitive::kPrimFloat:
2469 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002470 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002471 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002472 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002473 break;
2474
2475 default:
2476 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2477 }
2478}
2479
2480void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2481 LocationSummary* locations = neg->GetLocations();
2482 Location out = locations->Out();
2483 Location in = locations->InAt(0);
2484 switch (neg->GetResultType()) {
2485 case Primitive::kPrimInt:
2486 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002487 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002488 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002489 break;
2490
2491 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002492 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002493 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002494 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002495 break;
2496
Roland Levillain5368c212014-11-27 15:03:41 +00002497 case Primitive::kPrimFloat: {
2498 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002499 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002500 // Implement float negation with an exclusive or with value
2501 // 0x80000000 (mask for bit 31, representing the sign of a
2502 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002503 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002504 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002505 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002506 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002507
Roland Levillain5368c212014-11-27 15:03:41 +00002508 case Primitive::kPrimDouble: {
2509 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002510 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002511 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002512 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002513 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002514 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002516 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002517 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002518
2519 default:
2520 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2521 }
2522}
2523
Roland Levillaindff1f282014-11-05 14:15:05 +00002524void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2525 LocationSummary* locations =
2526 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2527 Primitive::Type result_type = conversion->GetResultType();
2528 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002529 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002530
David Brazdilb2bd1c52015-03-25 11:17:37 +00002531 // The Java language does not allow treating boolean as an integral type but
2532 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002533
Roland Levillaindff1f282014-11-05 14:15:05 +00002534 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002535 case Primitive::kPrimByte:
2536 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002537 case Primitive::kPrimLong:
2538 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002539 case Primitive::kPrimBoolean:
2540 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002541 case Primitive::kPrimShort:
2542 case Primitive::kPrimInt:
2543 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002544 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002545 locations->SetInAt(0, Location::Any());
2546 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2547 break;
2548
2549 default:
2550 LOG(FATAL) << "Unexpected type conversion from " << input_type
2551 << " to " << result_type;
2552 }
2553 break;
2554
Roland Levillain01a8d712014-11-14 16:27:39 +00002555 case Primitive::kPrimShort:
2556 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002557 case Primitive::kPrimLong:
2558 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002559 case Primitive::kPrimBoolean:
2560 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002561 case Primitive::kPrimByte:
2562 case Primitive::kPrimInt:
2563 case Primitive::kPrimChar:
2564 // Processing a Dex `int-to-short' instruction.
2565 locations->SetInAt(0, Location::Any());
2566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2567 break;
2568
2569 default:
2570 LOG(FATAL) << "Unexpected type conversion from " << input_type
2571 << " to " << result_type;
2572 }
2573 break;
2574
Roland Levillain946e1432014-11-11 17:35:19 +00002575 case Primitive::kPrimInt:
2576 switch (input_type) {
2577 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002578 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002579 locations->SetInAt(0, Location::Any());
2580 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2581 break;
2582
2583 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002584 // Processing a Dex `float-to-int' instruction.
2585 locations->SetInAt(0, Location::RequiresFpuRegister());
2586 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002587 break;
2588
Roland Levillain946e1432014-11-11 17:35:19 +00002589 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002590 // Processing a Dex `double-to-int' instruction.
2591 locations->SetInAt(0, Location::RequiresFpuRegister());
2592 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002593 break;
2594
2595 default:
2596 LOG(FATAL) << "Unexpected type conversion from " << input_type
2597 << " to " << result_type;
2598 }
2599 break;
2600
Roland Levillaindff1f282014-11-05 14:15:05 +00002601 case Primitive::kPrimLong:
2602 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002603 case Primitive::kPrimBoolean:
2604 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002605 case Primitive::kPrimByte:
2606 case Primitive::kPrimShort:
2607 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002608 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002609 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002610 // TODO: We would benefit from a (to-be-implemented)
2611 // Location::RegisterOrStackSlot requirement for this input.
2612 locations->SetInAt(0, Location::RequiresRegister());
2613 locations->SetOut(Location::RequiresRegister());
2614 break;
2615
2616 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002617 // Processing a Dex `float-to-long' instruction.
2618 locations->SetInAt(0, Location::RequiresFpuRegister());
2619 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002620 break;
2621
Roland Levillaindff1f282014-11-05 14:15:05 +00002622 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002623 // Processing a Dex `double-to-long' instruction.
2624 locations->SetInAt(0, Location::RequiresFpuRegister());
2625 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002626 break;
2627
2628 default:
2629 LOG(FATAL) << "Unexpected type conversion from " << input_type
2630 << " to " << result_type;
2631 }
2632 break;
2633
Roland Levillain981e4542014-11-14 11:47:14 +00002634 case Primitive::kPrimChar:
2635 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002636 case Primitive::kPrimLong:
2637 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002638 case Primitive::kPrimBoolean:
2639 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002640 case Primitive::kPrimByte:
2641 case Primitive::kPrimShort:
2642 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002643 // Processing a Dex `int-to-char' instruction.
2644 locations->SetInAt(0, Location::Any());
2645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2646 break;
2647
2648 default:
2649 LOG(FATAL) << "Unexpected type conversion from " << input_type
2650 << " to " << result_type;
2651 }
2652 break;
2653
Roland Levillaindff1f282014-11-05 14:15:05 +00002654 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002655 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002656 case Primitive::kPrimBoolean:
2657 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002658 case Primitive::kPrimByte:
2659 case Primitive::kPrimShort:
2660 case Primitive::kPrimInt:
2661 case Primitive::kPrimChar:
2662 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002663 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002664 locations->SetOut(Location::RequiresFpuRegister());
2665 break;
2666
2667 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002668 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002669 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002670 locations->SetOut(Location::RequiresFpuRegister());
2671 break;
2672
Roland Levillaincff13742014-11-17 14:32:17 +00002673 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002674 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002675 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002676 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002677 break;
2678
2679 default:
2680 LOG(FATAL) << "Unexpected type conversion from " << input_type
2681 << " to " << result_type;
2682 };
2683 break;
2684
Roland Levillaindff1f282014-11-05 14:15:05 +00002685 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002686 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002687 case Primitive::kPrimBoolean:
2688 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002689 case Primitive::kPrimByte:
2690 case Primitive::kPrimShort:
2691 case Primitive::kPrimInt:
2692 case Primitive::kPrimChar:
2693 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002694 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002695 locations->SetOut(Location::RequiresFpuRegister());
2696 break;
2697
2698 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002699 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002700 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002701 locations->SetOut(Location::RequiresFpuRegister());
2702 break;
2703
Roland Levillaincff13742014-11-17 14:32:17 +00002704 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002705 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002706 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002707 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002708 break;
2709
2710 default:
2711 LOG(FATAL) << "Unexpected type conversion from " << input_type
2712 << " to " << result_type;
2713 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002714 break;
2715
2716 default:
2717 LOG(FATAL) << "Unexpected type conversion from " << input_type
2718 << " to " << result_type;
2719 }
2720}
2721
2722void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2723 LocationSummary* locations = conversion->GetLocations();
2724 Location out = locations->Out();
2725 Location in = locations->InAt(0);
2726 Primitive::Type result_type = conversion->GetResultType();
2727 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002728 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002729 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002730 case Primitive::kPrimByte:
2731 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002732 case Primitive::kPrimLong:
2733 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002734 case Primitive::kPrimBoolean:
2735 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002736 case Primitive::kPrimShort:
2737 case Primitive::kPrimInt:
2738 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002739 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002740 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002741 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002742 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002743 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002744 Address(CpuRegister(RSP), in.GetStackIndex()));
2745 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002746 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002747 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002748 }
2749 break;
2750
2751 default:
2752 LOG(FATAL) << "Unexpected type conversion from " << input_type
2753 << " to " << result_type;
2754 }
2755 break;
2756
Roland Levillain01a8d712014-11-14 16:27:39 +00002757 case Primitive::kPrimShort:
2758 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002759 case Primitive::kPrimLong:
2760 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002761 case Primitive::kPrimBoolean:
2762 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002763 case Primitive::kPrimByte:
2764 case Primitive::kPrimInt:
2765 case Primitive::kPrimChar:
2766 // Processing a Dex `int-to-short' instruction.
2767 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002768 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002769 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002771 Address(CpuRegister(RSP), in.GetStackIndex()));
2772 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002774 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002775 }
2776 break;
2777
2778 default:
2779 LOG(FATAL) << "Unexpected type conversion from " << input_type
2780 << " to " << result_type;
2781 }
2782 break;
2783
Roland Levillain946e1432014-11-11 17:35:19 +00002784 case Primitive::kPrimInt:
2785 switch (input_type) {
2786 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002787 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002788 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002790 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002791 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002792 Address(CpuRegister(RSP), in.GetStackIndex()));
2793 } else {
2794 DCHECK(in.IsConstant());
2795 DCHECK(in.GetConstant()->IsLongConstant());
2796 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002797 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002798 }
2799 break;
2800
Roland Levillain3f8f9362014-12-02 17:45:01 +00002801 case Primitive::kPrimFloat: {
2802 // Processing a Dex `float-to-int' instruction.
2803 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2804 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002805 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002806
2807 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002808 // if input >= (float)INT_MAX goto done
2809 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002810 __ j(kAboveEqual, &done);
2811 // if input == NaN goto nan
2812 __ j(kUnordered, &nan);
2813 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002814 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002815 __ jmp(&done);
2816 __ Bind(&nan);
2817 // output = 0
2818 __ xorl(output, output);
2819 __ Bind(&done);
2820 break;
2821 }
2822
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002823 case Primitive::kPrimDouble: {
2824 // Processing a Dex `double-to-int' instruction.
2825 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2826 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002827 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002828
2829 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002830 // if input >= (double)INT_MAX goto done
2831 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002832 __ j(kAboveEqual, &done);
2833 // if input == NaN goto nan
2834 __ j(kUnordered, &nan);
2835 // output = double-to-int-truncate(input)
2836 __ cvttsd2si(output, input);
2837 __ jmp(&done);
2838 __ Bind(&nan);
2839 // output = 0
2840 __ xorl(output, output);
2841 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002842 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002843 }
Roland Levillain946e1432014-11-11 17:35:19 +00002844
2845 default:
2846 LOG(FATAL) << "Unexpected type conversion from " << input_type
2847 << " to " << result_type;
2848 }
2849 break;
2850
Roland Levillaindff1f282014-11-05 14:15:05 +00002851 case Primitive::kPrimLong:
2852 switch (input_type) {
2853 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002854 case Primitive::kPrimBoolean:
2855 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002856 case Primitive::kPrimByte:
2857 case Primitive::kPrimShort:
2858 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002859 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002860 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002861 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002862 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002863 break;
2864
Roland Levillain624279f2014-12-04 11:54:28 +00002865 case Primitive::kPrimFloat: {
2866 // Processing a Dex `float-to-long' instruction.
2867 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2868 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002869 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002870
Mark Mendell92e83bf2015-05-07 11:25:03 -04002871 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002872 // if input >= (float)LONG_MAX goto done
2873 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002874 __ j(kAboveEqual, &done);
2875 // if input == NaN goto nan
2876 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002877 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002878 __ cvttss2si(output, input, true);
2879 __ jmp(&done);
2880 __ Bind(&nan);
2881 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002882 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002883 __ Bind(&done);
2884 break;
2885 }
2886
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002887 case Primitive::kPrimDouble: {
2888 // Processing a Dex `double-to-long' instruction.
2889 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2890 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002891 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002892
Mark Mendell92e83bf2015-05-07 11:25:03 -04002893 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002894 // if input >= (double)LONG_MAX goto done
2895 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002896 __ j(kAboveEqual, &done);
2897 // if input == NaN goto nan
2898 __ j(kUnordered, &nan);
2899 // output = double-to-long-truncate(input)
2900 __ cvttsd2si(output, input, true);
2901 __ jmp(&done);
2902 __ Bind(&nan);
2903 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002904 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002905 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002906 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002907 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002908
2909 default:
2910 LOG(FATAL) << "Unexpected type conversion from " << input_type
2911 << " to " << result_type;
2912 }
2913 break;
2914
Roland Levillain981e4542014-11-14 11:47:14 +00002915 case Primitive::kPrimChar:
2916 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002917 case Primitive::kPrimLong:
2918 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002919 case Primitive::kPrimBoolean:
2920 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002921 case Primitive::kPrimByte:
2922 case Primitive::kPrimShort:
2923 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002924 // Processing a Dex `int-to-char' instruction.
2925 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002926 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002927 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002928 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002929 Address(CpuRegister(RSP), in.GetStackIndex()));
2930 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002931 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002932 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002933 }
2934 break;
2935
2936 default:
2937 LOG(FATAL) << "Unexpected type conversion from " << input_type
2938 << " to " << result_type;
2939 }
2940 break;
2941
Roland Levillaindff1f282014-11-05 14:15:05 +00002942 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002943 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002944 case Primitive::kPrimBoolean:
2945 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002946 case Primitive::kPrimByte:
2947 case Primitive::kPrimShort:
2948 case Primitive::kPrimInt:
2949 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002950 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002951 if (in.IsRegister()) {
2952 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2953 } else if (in.IsConstant()) {
2954 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2955 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002956 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002957 } else {
2958 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2959 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2960 }
Roland Levillaincff13742014-11-17 14:32:17 +00002961 break;
2962
2963 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002964 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002965 if (in.IsRegister()) {
2966 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2967 } else if (in.IsConstant()) {
2968 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2969 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002970 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002971 } else {
2972 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2973 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2974 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002975 break;
2976
Roland Levillaincff13742014-11-17 14:32:17 +00002977 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002978 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002979 if (in.IsFpuRegister()) {
2980 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2981 } else if (in.IsConstant()) {
2982 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2983 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002984 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002985 } else {
2986 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2987 Address(CpuRegister(RSP), in.GetStackIndex()));
2988 }
Roland Levillaincff13742014-11-17 14:32:17 +00002989 break;
2990
2991 default:
2992 LOG(FATAL) << "Unexpected type conversion from " << input_type
2993 << " to " << result_type;
2994 };
2995 break;
2996
Roland Levillaindff1f282014-11-05 14:15:05 +00002997 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002998 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002999 case Primitive::kPrimBoolean:
3000 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003001 case Primitive::kPrimByte:
3002 case Primitive::kPrimShort:
3003 case Primitive::kPrimInt:
3004 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003005 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003006 if (in.IsRegister()) {
3007 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3008 } else if (in.IsConstant()) {
3009 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3010 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003011 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003012 } else {
3013 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3014 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3015 }
Roland Levillaincff13742014-11-17 14:32:17 +00003016 break;
3017
3018 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003019 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003020 if (in.IsRegister()) {
3021 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3022 } else if (in.IsConstant()) {
3023 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3024 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003025 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003026 } else {
3027 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3028 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3029 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003030 break;
3031
Roland Levillaincff13742014-11-17 14:32:17 +00003032 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003033 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003034 if (in.IsFpuRegister()) {
3035 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3036 } else if (in.IsConstant()) {
3037 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3038 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003039 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003040 } else {
3041 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3042 Address(CpuRegister(RSP), in.GetStackIndex()));
3043 }
Roland Levillaincff13742014-11-17 14:32:17 +00003044 break;
3045
3046 default:
3047 LOG(FATAL) << "Unexpected type conversion from " << input_type
3048 << " to " << result_type;
3049 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003050 break;
3051
3052 default:
3053 LOG(FATAL) << "Unexpected type conversion from " << input_type
3054 << " to " << result_type;
3055 }
3056}
3057
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003058void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003059 LocationSummary* locations =
3060 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003061 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003062 case Primitive::kPrimInt: {
3063 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003064 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003066 break;
3067 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003068
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003069 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003070 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003071 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003072 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003074 break;
3075 }
3076
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003077 case Primitive::kPrimDouble:
3078 case Primitive::kPrimFloat: {
3079 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003080 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003081 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003082 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003083 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003084
3085 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003086 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003087 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003088}
3089
3090void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3091 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003092 Location first = locations->InAt(0);
3093 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003094 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003095
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003096 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003097 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003098 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003099 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3100 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003101 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3102 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003103 } else {
3104 __ leal(out.AsRegister<CpuRegister>(), Address(
3105 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3106 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003107 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003108 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3109 __ addl(out.AsRegister<CpuRegister>(),
3110 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3111 } else {
3112 __ leal(out.AsRegister<CpuRegister>(), Address(
3113 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3114 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003115 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003116 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003117 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003118 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003119 break;
3120 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003121
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003122 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003123 if (second.IsRegister()) {
3124 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3125 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003126 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3127 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003128 } else {
3129 __ leaq(out.AsRegister<CpuRegister>(), Address(
3130 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3131 }
3132 } else {
3133 DCHECK(second.IsConstant());
3134 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3135 int32_t int32_value = Low32Bits(value);
3136 DCHECK_EQ(int32_value, value);
3137 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3138 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3139 } else {
3140 __ leaq(out.AsRegister<CpuRegister>(), Address(
3141 first.AsRegister<CpuRegister>(), int32_value));
3142 }
3143 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003144 break;
3145 }
3146
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003147 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003148 if (second.IsFpuRegister()) {
3149 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3150 } else if (second.IsConstant()) {
3151 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003152 codegen_->LiteralFloatAddress(
3153 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003154 } else {
3155 DCHECK(second.IsStackSlot());
3156 __ addss(first.AsFpuRegister<XmmRegister>(),
3157 Address(CpuRegister(RSP), second.GetStackIndex()));
3158 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003159 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003160 }
3161
3162 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003163 if (second.IsFpuRegister()) {
3164 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3165 } else if (second.IsConstant()) {
3166 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003167 codegen_->LiteralDoubleAddress(
3168 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003169 } else {
3170 DCHECK(second.IsDoubleStackSlot());
3171 __ addsd(first.AsFpuRegister<XmmRegister>(),
3172 Address(CpuRegister(RSP), second.GetStackIndex()));
3173 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003174 break;
3175 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003176
3177 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003178 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003179 }
3180}
3181
3182void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003183 LocationSummary* locations =
3184 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003185 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003186 case Primitive::kPrimInt: {
3187 locations->SetInAt(0, Location::RequiresRegister());
3188 locations->SetInAt(1, Location::Any());
3189 locations->SetOut(Location::SameAsFirstInput());
3190 break;
3191 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003192 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003193 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003194 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003195 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003196 break;
3197 }
Calin Juravle11351682014-10-23 15:38:15 +01003198 case Primitive::kPrimFloat:
3199 case Primitive::kPrimDouble: {
3200 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003201 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003202 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003203 break;
Calin Juravle11351682014-10-23 15:38:15 +01003204 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003205 default:
Calin Juravle11351682014-10-23 15:38:15 +01003206 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003207 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003208}
3209
3210void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3211 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003212 Location first = locations->InAt(0);
3213 Location second = locations->InAt(1);
3214 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003215 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003216 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003217 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003218 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003219 } else if (second.IsConstant()) {
3220 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003221 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003222 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003223 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003224 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003225 break;
3226 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003227 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003228 if (second.IsConstant()) {
3229 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3230 DCHECK(IsInt<32>(value));
3231 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3232 } else {
3233 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3234 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003235 break;
3236 }
3237
Calin Juravle11351682014-10-23 15:38:15 +01003238 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003239 if (second.IsFpuRegister()) {
3240 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3241 } else if (second.IsConstant()) {
3242 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003243 codegen_->LiteralFloatAddress(
3244 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003245 } else {
3246 DCHECK(second.IsStackSlot());
3247 __ subss(first.AsFpuRegister<XmmRegister>(),
3248 Address(CpuRegister(RSP), second.GetStackIndex()));
3249 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003250 break;
Calin Juravle11351682014-10-23 15:38:15 +01003251 }
3252
3253 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003254 if (second.IsFpuRegister()) {
3255 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3256 } else if (second.IsConstant()) {
3257 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003258 codegen_->LiteralDoubleAddress(
3259 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003260 } else {
3261 DCHECK(second.IsDoubleStackSlot());
3262 __ subsd(first.AsFpuRegister<XmmRegister>(),
3263 Address(CpuRegister(RSP), second.GetStackIndex()));
3264 }
Calin Juravle11351682014-10-23 15:38:15 +01003265 break;
3266 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003267
3268 default:
Calin Juravle11351682014-10-23 15:38:15 +01003269 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003270 }
3271}
3272
Calin Juravle34bacdf2014-10-07 20:23:36 +01003273void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3274 LocationSummary* locations =
3275 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3276 switch (mul->GetResultType()) {
3277 case Primitive::kPrimInt: {
3278 locations->SetInAt(0, Location::RequiresRegister());
3279 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003280 if (mul->InputAt(1)->IsIntConstant()) {
3281 // Can use 3 operand multiply.
3282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3283 } else {
3284 locations->SetOut(Location::SameAsFirstInput());
3285 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003286 break;
3287 }
3288 case Primitive::kPrimLong: {
3289 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003290 locations->SetInAt(1, Location::Any());
3291 if (mul->InputAt(1)->IsLongConstant() &&
3292 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003293 // Can use 3 operand multiply.
3294 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3295 } else {
3296 locations->SetOut(Location::SameAsFirstInput());
3297 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003298 break;
3299 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003300 case Primitive::kPrimFloat:
3301 case Primitive::kPrimDouble: {
3302 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003303 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003304 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003305 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003306 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003307
3308 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003309 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003310 }
3311}
3312
3313void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3314 LocationSummary* locations = mul->GetLocations();
3315 Location first = locations->InAt(0);
3316 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003317 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003318 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003319 case Primitive::kPrimInt:
3320 // The constant may have ended up in a register, so test explicitly to avoid
3321 // problems where the output may not be the same as the first operand.
3322 if (mul->InputAt(1)->IsIntConstant()) {
3323 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3324 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3325 } else if (second.IsRegister()) {
3326 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003327 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003328 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003329 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003330 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003331 __ imull(first.AsRegister<CpuRegister>(),
3332 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003333 }
3334 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003335 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003336 // The constant may have ended up in a register, so test explicitly to avoid
3337 // problems where the output may not be the same as the first operand.
3338 if (mul->InputAt(1)->IsLongConstant()) {
3339 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3340 if (IsInt<32>(value)) {
3341 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3342 Immediate(static_cast<int32_t>(value)));
3343 } else {
3344 // Have to use the constant area.
3345 DCHECK(first.Equals(out));
3346 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3347 }
3348 } else if (second.IsRegister()) {
3349 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003350 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003351 } else {
3352 DCHECK(second.IsDoubleStackSlot());
3353 DCHECK(first.Equals(out));
3354 __ imulq(first.AsRegister<CpuRegister>(),
3355 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003356 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003357 break;
3358 }
3359
Calin Juravleb5bfa962014-10-21 18:02:24 +01003360 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003361 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003362 if (second.IsFpuRegister()) {
3363 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3364 } else if (second.IsConstant()) {
3365 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003366 codegen_->LiteralFloatAddress(
3367 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003368 } else {
3369 DCHECK(second.IsStackSlot());
3370 __ mulss(first.AsFpuRegister<XmmRegister>(),
3371 Address(CpuRegister(RSP), second.GetStackIndex()));
3372 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003373 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003374 }
3375
3376 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003377 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003378 if (second.IsFpuRegister()) {
3379 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3380 } else if (second.IsConstant()) {
3381 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003382 codegen_->LiteralDoubleAddress(
3383 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003384 } else {
3385 DCHECK(second.IsDoubleStackSlot());
3386 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3387 Address(CpuRegister(RSP), second.GetStackIndex()));
3388 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003389 break;
3390 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003391
3392 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003393 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003394 }
3395}
3396
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003397void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3398 uint32_t stack_adjustment, bool is_float) {
3399 if (source.IsStackSlot()) {
3400 DCHECK(is_float);
3401 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3402 } else if (source.IsDoubleStackSlot()) {
3403 DCHECK(!is_float);
3404 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3405 } else {
3406 // Write the value to the temporary location on the stack and load to FP stack.
3407 if (is_float) {
3408 Location stack_temp = Location::StackSlot(temp_offset);
3409 codegen_->Move(stack_temp, source);
3410 __ flds(Address(CpuRegister(RSP), temp_offset));
3411 } else {
3412 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3413 codegen_->Move(stack_temp, source);
3414 __ fldl(Address(CpuRegister(RSP), temp_offset));
3415 }
3416 }
3417}
3418
3419void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3420 Primitive::Type type = rem->GetResultType();
3421 bool is_float = type == Primitive::kPrimFloat;
3422 size_t elem_size = Primitive::ComponentSize(type);
3423 LocationSummary* locations = rem->GetLocations();
3424 Location first = locations->InAt(0);
3425 Location second = locations->InAt(1);
3426 Location out = locations->Out();
3427
3428 // Create stack space for 2 elements.
3429 // TODO: enhance register allocator to ask for stack temporaries.
3430 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3431
3432 // Load the values to the FP stack in reverse order, using temporaries if needed.
3433 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3434 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3435
3436 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003437 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003438 __ Bind(&retry);
3439 __ fprem();
3440
3441 // Move FP status to AX.
3442 __ fstsw();
3443
3444 // And see if the argument reduction is complete. This is signaled by the
3445 // C2 FPU flag bit set to 0.
3446 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3447 __ j(kNotEqual, &retry);
3448
3449 // We have settled on the final value. Retrieve it into an XMM register.
3450 // Store FP top of stack to real stack.
3451 if (is_float) {
3452 __ fsts(Address(CpuRegister(RSP), 0));
3453 } else {
3454 __ fstl(Address(CpuRegister(RSP), 0));
3455 }
3456
3457 // Pop the 2 items from the FP stack.
3458 __ fucompp();
3459
3460 // Load the value from the stack into an XMM register.
3461 DCHECK(out.IsFpuRegister()) << out;
3462 if (is_float) {
3463 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3464 } else {
3465 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3466 }
3467
3468 // And remove the temporary stack space we allocated.
3469 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3470}
3471
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003472void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3473 DCHECK(instruction->IsDiv() || instruction->IsRem());
3474
3475 LocationSummary* locations = instruction->GetLocations();
3476 Location second = locations->InAt(1);
3477 DCHECK(second.IsConstant());
3478
3479 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3480 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003481 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003482
3483 DCHECK(imm == 1 || imm == -1);
3484
3485 switch (instruction->GetResultType()) {
3486 case Primitive::kPrimInt: {
3487 if (instruction->IsRem()) {
3488 __ xorl(output_register, output_register);
3489 } else {
3490 __ movl(output_register, input_register);
3491 if (imm == -1) {
3492 __ negl(output_register);
3493 }
3494 }
3495 break;
3496 }
3497
3498 case Primitive::kPrimLong: {
3499 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003500 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501 } else {
3502 __ movq(output_register, input_register);
3503 if (imm == -1) {
3504 __ negq(output_register);
3505 }
3506 }
3507 break;
3508 }
3509
3510 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003511 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003512 }
3513}
3514
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003515void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003516 LocationSummary* locations = instruction->GetLocations();
3517 Location second = locations->InAt(1);
3518
3519 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3520 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3521
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003522 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003523 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3524 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525
3526 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3527
3528 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003529 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003530 __ testl(numerator, numerator);
3531 __ cmov(kGreaterEqual, tmp, numerator);
3532 int shift = CTZ(imm);
3533 __ sarl(tmp, Immediate(shift));
3534
3535 if (imm < 0) {
3536 __ negl(tmp);
3537 }
3538
3539 __ movl(output_register, tmp);
3540 } else {
3541 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3542 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3543
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003544 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003545 __ addq(rdx, numerator);
3546 __ testq(numerator, numerator);
3547 __ cmov(kGreaterEqual, rdx, numerator);
3548 int shift = CTZ(imm);
3549 __ sarq(rdx, Immediate(shift));
3550
3551 if (imm < 0) {
3552 __ negq(rdx);
3553 }
3554
3555 __ movq(output_register, rdx);
3556 }
3557}
3558
3559void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3560 DCHECK(instruction->IsDiv() || instruction->IsRem());
3561
3562 LocationSummary* locations = instruction->GetLocations();
3563 Location second = locations->InAt(1);
3564
3565 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3566 : locations->GetTemp(0).AsRegister<CpuRegister>();
3567 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3568 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3569 : locations->Out().AsRegister<CpuRegister>();
3570 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3571
3572 DCHECK_EQ(RAX, eax.AsRegister());
3573 DCHECK_EQ(RDX, edx.AsRegister());
3574 if (instruction->IsDiv()) {
3575 DCHECK_EQ(RAX, out.AsRegister());
3576 } else {
3577 DCHECK_EQ(RDX, out.AsRegister());
3578 }
3579
3580 int64_t magic;
3581 int shift;
3582
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003583 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003584 if (instruction->GetResultType() == Primitive::kPrimInt) {
3585 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3586
3587 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3588
3589 __ movl(numerator, eax);
3590
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003591 __ movl(eax, Immediate(magic));
3592 __ imull(numerator);
3593
3594 if (imm > 0 && magic < 0) {
3595 __ addl(edx, numerator);
3596 } else if (imm < 0 && magic > 0) {
3597 __ subl(edx, numerator);
3598 }
3599
3600 if (shift != 0) {
3601 __ sarl(edx, Immediate(shift));
3602 }
3603
3604 __ movl(eax, edx);
3605 __ shrl(edx, Immediate(31));
3606 __ addl(edx, eax);
3607
3608 if (instruction->IsRem()) {
3609 __ movl(eax, numerator);
3610 __ imull(edx, Immediate(imm));
3611 __ subl(eax, edx);
3612 __ movl(edx, eax);
3613 } else {
3614 __ movl(eax, edx);
3615 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003616 } else {
3617 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3618
3619 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3620
3621 CpuRegister rax = eax;
3622 CpuRegister rdx = edx;
3623
3624 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3625
3626 // Save the numerator.
3627 __ movq(numerator, rax);
3628
3629 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003630 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003631
3632 // RDX:RAX = magic * numerator
3633 __ imulq(numerator);
3634
3635 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003636 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003637 __ addq(rdx, numerator);
3638 } else if (imm < 0 && magic > 0) {
3639 // RDX -= numerator
3640 __ subq(rdx, numerator);
3641 }
3642
3643 // Shift if needed.
3644 if (shift != 0) {
3645 __ sarq(rdx, Immediate(shift));
3646 }
3647
3648 // RDX += 1 if RDX < 0
3649 __ movq(rax, rdx);
3650 __ shrq(rdx, Immediate(63));
3651 __ addq(rdx, rax);
3652
3653 if (instruction->IsRem()) {
3654 __ movq(rax, numerator);
3655
3656 if (IsInt<32>(imm)) {
3657 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3658 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003659 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003660 }
3661
3662 __ subq(rax, rdx);
3663 __ movq(rdx, rax);
3664 } else {
3665 __ movq(rax, rdx);
3666 }
3667 }
3668}
3669
Calin Juravlebacfec32014-11-14 15:54:36 +00003670void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3671 DCHECK(instruction->IsDiv() || instruction->IsRem());
3672 Primitive::Type type = instruction->GetResultType();
3673 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3674
3675 bool is_div = instruction->IsDiv();
3676 LocationSummary* locations = instruction->GetLocations();
3677
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003678 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3679 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003680
Roland Levillain271ab9c2014-11-27 15:23:57 +00003681 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003682 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003683
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003684 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003685 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003686
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003687 if (imm == 0) {
3688 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3689 } else if (imm == 1 || imm == -1) {
3690 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003691 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003692 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003693 } else {
3694 DCHECK(imm <= -2 || imm >= 2);
3695 GenerateDivRemWithAnyConstant(instruction);
3696 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003697 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003698 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003699 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003700 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003701 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003702
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003703 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3704 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3705 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3706 // so it's safe to just use negl instead of more complex comparisons.
3707 if (type == Primitive::kPrimInt) {
3708 __ cmpl(second_reg, Immediate(-1));
3709 __ j(kEqual, slow_path->GetEntryLabel());
3710 // edx:eax <- sign-extended of eax
3711 __ cdq();
3712 // eax = quotient, edx = remainder
3713 __ idivl(second_reg);
3714 } else {
3715 __ cmpq(second_reg, Immediate(-1));
3716 __ j(kEqual, slow_path->GetEntryLabel());
3717 // rdx:rax <- sign-extended of rax
3718 __ cqo();
3719 // rax = quotient, rdx = remainder
3720 __ idivq(second_reg);
3721 }
3722 __ Bind(slow_path->GetExitLabel());
3723 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003724}
3725
Calin Juravle7c4954d2014-10-28 16:57:40 +00003726void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3727 LocationSummary* locations =
3728 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3729 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003730 case Primitive::kPrimInt:
3731 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003732 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003733 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003734 locations->SetOut(Location::SameAsFirstInput());
3735 // Intel uses edx:eax as the dividend.
3736 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003737 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3738 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3739 // output and request another temp.
3740 if (div->InputAt(1)->IsConstant()) {
3741 locations->AddTemp(Location::RequiresRegister());
3742 }
Calin Juravled0d48522014-11-04 16:40:20 +00003743 break;
3744 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003745
Calin Juravle7c4954d2014-10-28 16:57:40 +00003746 case Primitive::kPrimFloat:
3747 case Primitive::kPrimDouble: {
3748 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003749 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003750 locations->SetOut(Location::SameAsFirstInput());
3751 break;
3752 }
3753
3754 default:
3755 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3756 }
3757}
3758
3759void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3760 LocationSummary* locations = div->GetLocations();
3761 Location first = locations->InAt(0);
3762 Location second = locations->InAt(1);
3763 DCHECK(first.Equals(locations->Out()));
3764
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003765 Primitive::Type type = div->GetResultType();
3766 switch (type) {
3767 case Primitive::kPrimInt:
3768 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003769 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003770 break;
3771 }
3772
Calin Juravle7c4954d2014-10-28 16:57:40 +00003773 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003774 if (second.IsFpuRegister()) {
3775 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3776 } else if (second.IsConstant()) {
3777 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003778 codegen_->LiteralFloatAddress(
3779 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003780 } else {
3781 DCHECK(second.IsStackSlot());
3782 __ divss(first.AsFpuRegister<XmmRegister>(),
3783 Address(CpuRegister(RSP), second.GetStackIndex()));
3784 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003785 break;
3786 }
3787
3788 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003789 if (second.IsFpuRegister()) {
3790 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3791 } else if (second.IsConstant()) {
3792 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003793 codegen_->LiteralDoubleAddress(
3794 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003795 } else {
3796 DCHECK(second.IsDoubleStackSlot());
3797 __ divsd(first.AsFpuRegister<XmmRegister>(),
3798 Address(CpuRegister(RSP), second.GetStackIndex()));
3799 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003800 break;
3801 }
3802
3803 default:
3804 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3805 }
3806}
3807
Calin Juravlebacfec32014-11-14 15:54:36 +00003808void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003809 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003810 LocationSummary* locations =
3811 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003812
3813 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003814 case Primitive::kPrimInt:
3815 case Primitive::kPrimLong: {
3816 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003817 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003818 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3819 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003820 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3821 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3822 // output and request another temp.
3823 if (rem->InputAt(1)->IsConstant()) {
3824 locations->AddTemp(Location::RequiresRegister());
3825 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003826 break;
3827 }
3828
3829 case Primitive::kPrimFloat:
3830 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003831 locations->SetInAt(0, Location::Any());
3832 locations->SetInAt(1, Location::Any());
3833 locations->SetOut(Location::RequiresFpuRegister());
3834 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003835 break;
3836 }
3837
3838 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003839 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003840 }
3841}
3842
3843void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3844 Primitive::Type type = rem->GetResultType();
3845 switch (type) {
3846 case Primitive::kPrimInt:
3847 case Primitive::kPrimLong: {
3848 GenerateDivRemIntegral(rem);
3849 break;
3850 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003851 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003852 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003853 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003854 break;
3855 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003856 default:
3857 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3858 }
3859}
3860
Calin Juravled0d48522014-11-04 16:40:20 +00003861void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003862 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003863 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003864}
3865
3866void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003867 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003868 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3869 codegen_->AddSlowPath(slow_path);
3870
3871 LocationSummary* locations = instruction->GetLocations();
3872 Location value = locations->InAt(0);
3873
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003874 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003875 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003876 case Primitive::kPrimByte:
3877 case Primitive::kPrimChar:
3878 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003879 case Primitive::kPrimInt: {
3880 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003881 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003882 __ j(kEqual, slow_path->GetEntryLabel());
3883 } else if (value.IsStackSlot()) {
3884 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3885 __ j(kEqual, slow_path->GetEntryLabel());
3886 } else {
3887 DCHECK(value.IsConstant()) << value;
3888 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003889 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003890 }
3891 }
3892 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003893 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003894 case Primitive::kPrimLong: {
3895 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003896 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003897 __ j(kEqual, slow_path->GetEntryLabel());
3898 } else if (value.IsDoubleStackSlot()) {
3899 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3900 __ j(kEqual, slow_path->GetEntryLabel());
3901 } else {
3902 DCHECK(value.IsConstant()) << value;
3903 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003904 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003905 }
3906 }
3907 break;
3908 }
3909 default:
3910 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003911 }
Calin Juravled0d48522014-11-04 16:40:20 +00003912}
3913
Calin Juravle9aec02f2014-11-18 23:06:35 +00003914void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3915 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3916
3917 LocationSummary* locations =
3918 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3919
3920 switch (op->GetResultType()) {
3921 case Primitive::kPrimInt:
3922 case Primitive::kPrimLong: {
3923 locations->SetInAt(0, Location::RequiresRegister());
3924 // The shift count needs to be in CL.
3925 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3926 locations->SetOut(Location::SameAsFirstInput());
3927 break;
3928 }
3929 default:
3930 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3931 }
3932}
3933
3934void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3935 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3936
3937 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003938 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003939 Location second = locations->InAt(1);
3940
3941 switch (op->GetResultType()) {
3942 case Primitive::kPrimInt: {
3943 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003944 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003945 if (op->IsShl()) {
3946 __ shll(first_reg, second_reg);
3947 } else if (op->IsShr()) {
3948 __ sarl(first_reg, second_reg);
3949 } else {
3950 __ shrl(first_reg, second_reg);
3951 }
3952 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003953 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954 if (op->IsShl()) {
3955 __ shll(first_reg, imm);
3956 } else if (op->IsShr()) {
3957 __ sarl(first_reg, imm);
3958 } else {
3959 __ shrl(first_reg, imm);
3960 }
3961 }
3962 break;
3963 }
3964 case Primitive::kPrimLong: {
3965 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003966 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003967 if (op->IsShl()) {
3968 __ shlq(first_reg, second_reg);
3969 } else if (op->IsShr()) {
3970 __ sarq(first_reg, second_reg);
3971 } else {
3972 __ shrq(first_reg, second_reg);
3973 }
3974 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003975 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003976 if (op->IsShl()) {
3977 __ shlq(first_reg, imm);
3978 } else if (op->IsShr()) {
3979 __ sarq(first_reg, imm);
3980 } else {
3981 __ shrq(first_reg, imm);
3982 }
3983 }
3984 break;
3985 }
3986 default:
3987 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003988 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003989 }
3990}
3991
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003992void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3993 LocationSummary* locations =
3994 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3995
3996 switch (ror->GetResultType()) {
3997 case Primitive::kPrimInt:
3998 case Primitive::kPrimLong: {
3999 locations->SetInAt(0, Location::RequiresRegister());
4000 // The shift count needs to be in CL (unless it is a constant).
4001 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4002 locations->SetOut(Location::SameAsFirstInput());
4003 break;
4004 }
4005 default:
4006 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4007 UNREACHABLE();
4008 }
4009}
4010
4011void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4012 LocationSummary* locations = ror->GetLocations();
4013 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4014 Location second = locations->InAt(1);
4015
4016 switch (ror->GetResultType()) {
4017 case Primitive::kPrimInt:
4018 if (second.IsRegister()) {
4019 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4020 __ rorl(first_reg, second_reg);
4021 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004022 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004023 __ rorl(first_reg, imm);
4024 }
4025 break;
4026 case Primitive::kPrimLong:
4027 if (second.IsRegister()) {
4028 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4029 __ rorq(first_reg, second_reg);
4030 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004031 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004032 __ rorq(first_reg, imm);
4033 }
4034 break;
4035 default:
4036 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4037 UNREACHABLE();
4038 }
4039}
4040
Calin Juravle9aec02f2014-11-18 23:06:35 +00004041void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4042 HandleShift(shl);
4043}
4044
4045void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4046 HandleShift(shl);
4047}
4048
4049void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4050 HandleShift(shr);
4051}
4052
4053void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4054 HandleShift(shr);
4055}
4056
4057void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4058 HandleShift(ushr);
4059}
4060
4061void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4062 HandleShift(ushr);
4063}
4064
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004065void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004066 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004067 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004068 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004069 if (instruction->IsStringAlloc()) {
4070 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4071 } else {
4072 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4073 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4074 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004075 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004076}
4077
4078void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004079 // Note: if heap poisoning is enabled, the entry point takes cares
4080 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004081 if (instruction->IsStringAlloc()) {
4082 // String is allocated through StringFactory. Call NewEmptyString entry point.
4083 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004084 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004085 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4086 __ call(Address(temp, code_offset.SizeValue()));
4087 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4088 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004089 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004090 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4091 DCHECK(!codegen_->IsLeafMethod());
4092 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004093}
4094
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004095void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4096 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004098 InvokeRuntimeCallingConvention calling_convention;
4099 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004100 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004101 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004102 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004103}
4104
4105void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
4106 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004107 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
4108 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004109 // Note: if heap poisoning is enabled, the entry point takes cares
4110 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004111 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004112 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004113
4114 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004115}
4116
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004117void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004118 LocationSummary* locations =
4119 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004120 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4121 if (location.IsStackSlot()) {
4122 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4123 } else if (location.IsDoubleStackSlot()) {
4124 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4125 }
4126 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004127}
4128
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004129void InstructionCodeGeneratorX86_64::VisitParameterValue(
4130 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004131 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004132}
4133
4134void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4135 LocationSummary* locations =
4136 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4137 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4138}
4139
4140void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4141 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4142 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004143}
4144
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004145void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4146 LocationSummary* locations =
4147 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4148 locations->SetInAt(0, Location::RequiresRegister());
4149 locations->SetOut(Location::RequiresRegister());
4150}
4151
4152void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4153 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004154 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004155 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004156 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004157 __ movq(locations->Out().AsRegister<CpuRegister>(),
4158 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004159 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004160 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004161 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004162 __ movq(locations->Out().AsRegister<CpuRegister>(),
4163 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4164 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004165 __ movq(locations->Out().AsRegister<CpuRegister>(),
4166 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004167 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004168}
4169
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004170void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004171 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004172 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004173 locations->SetInAt(0, Location::RequiresRegister());
4174 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004175}
4176
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004177void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4178 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004179 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4180 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004181 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004182 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004183 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004184 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004185 break;
4186
4187 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004188 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004189 break;
4190
4191 default:
4192 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4193 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004194}
4195
David Brazdil66d126e2015-04-03 16:02:44 +01004196void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4197 LocationSummary* locations =
4198 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4199 locations->SetInAt(0, Location::RequiresRegister());
4200 locations->SetOut(Location::SameAsFirstInput());
4201}
4202
4203void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004204 LocationSummary* locations = bool_not->GetLocations();
4205 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4206 locations->Out().AsRegister<CpuRegister>().AsRegister());
4207 Location out = locations->Out();
4208 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4209}
4210
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004211void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004212 LocationSummary* locations =
4213 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004214 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004215 locations->SetInAt(i, Location::Any());
4216 }
4217 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004218}
4219
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004220void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004221 LOG(FATAL) << "Unimplemented";
4222}
4223
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004224void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004225 /*
4226 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004227 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004228 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4229 */
4230 switch (kind) {
4231 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004232 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004233 break;
4234 }
4235 case MemBarrierKind::kAnyStore:
4236 case MemBarrierKind::kLoadAny:
4237 case MemBarrierKind::kStoreStore: {
4238 // nop
4239 break;
4240 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004241 case MemBarrierKind::kNTStoreStore:
4242 // Non-Temporal Store/Store needs an explicit fence.
4243 MemoryFence(/* non-temporal */ true);
4244 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004245 }
4246}
4247
4248void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4249 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4250
Roland Levillain0d5a2812015-11-13 10:07:31 +00004251 bool object_field_get_with_read_barrier =
4252 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004253 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004254 new (GetGraph()->GetArena()) LocationSummary(instruction,
4255 object_field_get_with_read_barrier ?
4256 LocationSummary::kCallOnSlowPath :
4257 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004258 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004259 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004260 }
Calin Juravle52c48962014-12-16 17:02:57 +00004261 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004262 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4263 locations->SetOut(Location::RequiresFpuRegister());
4264 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004265 // The output overlaps for an object field get when read barriers
4266 // are enabled: we do not want the move to overwrite the object's
4267 // location, as we need it to emit the read barrier.
4268 locations->SetOut(
4269 Location::RequiresRegister(),
4270 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004271 }
Calin Juravle52c48962014-12-16 17:02:57 +00004272}
4273
4274void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4275 const FieldInfo& field_info) {
4276 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4277
4278 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004279 Location base_loc = locations->InAt(0);
4280 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004281 Location out = locations->Out();
4282 bool is_volatile = field_info.IsVolatile();
4283 Primitive::Type field_type = field_info.GetFieldType();
4284 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4285
4286 switch (field_type) {
4287 case Primitive::kPrimBoolean: {
4288 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4289 break;
4290 }
4291
4292 case Primitive::kPrimByte: {
4293 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4294 break;
4295 }
4296
4297 case Primitive::kPrimShort: {
4298 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4299 break;
4300 }
4301
4302 case Primitive::kPrimChar: {
4303 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4304 break;
4305 }
4306
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004307 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004308 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4309 break;
4310 }
4311
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004312 case Primitive::kPrimNot: {
4313 // /* HeapReference<Object> */ out = *(base + offset)
4314 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004315 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004316 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004317 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004318 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004319 if (is_volatile) {
4320 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4321 }
4322 } else {
4323 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4324 codegen_->MaybeRecordImplicitNullCheck(instruction);
4325 if (is_volatile) {
4326 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4327 }
4328 // If read barriers are enabled, emit read barriers other than
4329 // Baker's using a slow path (and also unpoison the loaded
4330 // reference, if heap poisoning is enabled).
4331 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4332 }
4333 break;
4334 }
4335
Calin Juravle52c48962014-12-16 17:02:57 +00004336 case Primitive::kPrimLong: {
4337 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4338 break;
4339 }
4340
4341 case Primitive::kPrimFloat: {
4342 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4343 break;
4344 }
4345
4346 case Primitive::kPrimDouble: {
4347 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4348 break;
4349 }
4350
4351 case Primitive::kPrimVoid:
4352 LOG(FATAL) << "Unreachable type " << field_type;
4353 UNREACHABLE();
4354 }
4355
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004356 if (field_type == Primitive::kPrimNot) {
4357 // Potential implicit null checks, in the case of reference
4358 // fields, are handled in the previous switch statement.
4359 } else {
4360 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004361 }
Roland Levillain4d027112015-07-01 15:41:14 +01004362
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004363 if (is_volatile) {
4364 if (field_type == Primitive::kPrimNot) {
4365 // Memory barriers, in the case of references, are also handled
4366 // in the previous switch statement.
4367 } else {
4368 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4369 }
Roland Levillain4d027112015-07-01 15:41:14 +01004370 }
Calin Juravle52c48962014-12-16 17:02:57 +00004371}
4372
4373void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4374 const FieldInfo& field_info) {
4375 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4376
4377 LocationSummary* locations =
4378 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004379 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004380 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004381 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004382 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004383
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004384 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004385 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004386 if (is_volatile) {
4387 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4388 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4389 } else {
4390 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4391 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004392 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004393 if (is_volatile) {
4394 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4395 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4396 } else {
4397 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4398 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004399 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004400 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004401 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004402 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004403 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004404 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4405 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004406 locations->AddTemp(Location::RequiresRegister());
4407 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004408}
4409
Calin Juravle52c48962014-12-16 17:02:57 +00004410void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004411 const FieldInfo& field_info,
4412 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004413 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4414
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004415 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004416 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4417 Location value = locations->InAt(1);
4418 bool is_volatile = field_info.IsVolatile();
4419 Primitive::Type field_type = field_info.GetFieldType();
4420 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4421
4422 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004423 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004424 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004425
Mark Mendellea5af682015-10-22 17:35:49 -04004426 bool maybe_record_implicit_null_check_done = false;
4427
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428 switch (field_type) {
4429 case Primitive::kPrimBoolean:
4430 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004431 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004432 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004433 __ movb(Address(base, offset), Immediate(v));
4434 } else {
4435 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4436 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004437 break;
4438 }
4439
4440 case Primitive::kPrimShort:
4441 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004442 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004443 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004444 __ movw(Address(base, offset), Immediate(v));
4445 } else {
4446 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4447 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448 break;
4449 }
4450
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004451 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004452 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004453 if (value.IsConstant()) {
4454 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004455 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4456 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4457 // Note: if heap poisoning is enabled, no need to poison
4458 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004459 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004460 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004461 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4462 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4463 __ movl(temp, value.AsRegister<CpuRegister>());
4464 __ PoisonHeapReference(temp);
4465 __ movl(Address(base, offset), temp);
4466 } else {
4467 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4468 }
Mark Mendell40741f32015-04-20 22:10:34 -04004469 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004470 break;
4471 }
4472
4473 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004474 if (value.IsConstant()) {
4475 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004476 codegen_->MoveInt64ToAddress(Address(base, offset),
4477 Address(base, offset + sizeof(int32_t)),
4478 v,
4479 instruction);
4480 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004481 } else {
4482 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4483 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484 break;
4485 }
4486
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004487 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004488 if (value.IsConstant()) {
4489 int32_t v =
4490 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4491 __ movl(Address(base, offset), Immediate(v));
4492 } else {
4493 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4494 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004495 break;
4496 }
4497
4498 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004499 if (value.IsConstant()) {
4500 int64_t v =
4501 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4502 codegen_->MoveInt64ToAddress(Address(base, offset),
4503 Address(base, offset + sizeof(int32_t)),
4504 v,
4505 instruction);
4506 maybe_record_implicit_null_check_done = true;
4507 } else {
4508 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4509 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004510 break;
4511 }
4512
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004513 case Primitive::kPrimVoid:
4514 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004515 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004516 }
Calin Juravle52c48962014-12-16 17:02:57 +00004517
Mark Mendellea5af682015-10-22 17:35:49 -04004518 if (!maybe_record_implicit_null_check_done) {
4519 codegen_->MaybeRecordImplicitNullCheck(instruction);
4520 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004521
4522 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4523 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4524 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004525 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004526 }
4527
Calin Juravle52c48962014-12-16 17:02:57 +00004528 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004529 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004530 }
4531}
4532
4533void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4534 HandleFieldSet(instruction, instruction->GetFieldInfo());
4535}
4536
4537void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004538 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004539}
4540
4541void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004542 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004543}
4544
4545void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004546 HandleFieldGet(instruction, instruction->GetFieldInfo());
4547}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004548
Calin Juravle52c48962014-12-16 17:02:57 +00004549void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4550 HandleFieldGet(instruction);
4551}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004552
Calin Juravle52c48962014-12-16 17:02:57 +00004553void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4554 HandleFieldGet(instruction, instruction->GetFieldInfo());
4555}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004556
Calin Juravle52c48962014-12-16 17:02:57 +00004557void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4558 HandleFieldSet(instruction, instruction->GetFieldInfo());
4559}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004560
Calin Juravle52c48962014-12-16 17:02:57 +00004561void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004562 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004563}
4564
Calin Juravlee460d1d2015-09-29 04:52:17 +01004565void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4566 HUnresolvedInstanceFieldGet* instruction) {
4567 FieldAccessCallingConventionX86_64 calling_convention;
4568 codegen_->CreateUnresolvedFieldLocationSummary(
4569 instruction, instruction->GetFieldType(), calling_convention);
4570}
4571
4572void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4573 HUnresolvedInstanceFieldGet* instruction) {
4574 FieldAccessCallingConventionX86_64 calling_convention;
4575 codegen_->GenerateUnresolvedFieldAccess(instruction,
4576 instruction->GetFieldType(),
4577 instruction->GetFieldIndex(),
4578 instruction->GetDexPc(),
4579 calling_convention);
4580}
4581
4582void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4583 HUnresolvedInstanceFieldSet* instruction) {
4584 FieldAccessCallingConventionX86_64 calling_convention;
4585 codegen_->CreateUnresolvedFieldLocationSummary(
4586 instruction, instruction->GetFieldType(), calling_convention);
4587}
4588
4589void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4590 HUnresolvedInstanceFieldSet* instruction) {
4591 FieldAccessCallingConventionX86_64 calling_convention;
4592 codegen_->GenerateUnresolvedFieldAccess(instruction,
4593 instruction->GetFieldType(),
4594 instruction->GetFieldIndex(),
4595 instruction->GetDexPc(),
4596 calling_convention);
4597}
4598
4599void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4600 HUnresolvedStaticFieldGet* instruction) {
4601 FieldAccessCallingConventionX86_64 calling_convention;
4602 codegen_->CreateUnresolvedFieldLocationSummary(
4603 instruction, instruction->GetFieldType(), calling_convention);
4604}
4605
4606void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4607 HUnresolvedStaticFieldGet* instruction) {
4608 FieldAccessCallingConventionX86_64 calling_convention;
4609 codegen_->GenerateUnresolvedFieldAccess(instruction,
4610 instruction->GetFieldType(),
4611 instruction->GetFieldIndex(),
4612 instruction->GetDexPc(),
4613 calling_convention);
4614}
4615
4616void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4617 HUnresolvedStaticFieldSet* instruction) {
4618 FieldAccessCallingConventionX86_64 calling_convention;
4619 codegen_->CreateUnresolvedFieldLocationSummary(
4620 instruction, instruction->GetFieldType(), calling_convention);
4621}
4622
4623void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4624 HUnresolvedStaticFieldSet* instruction) {
4625 FieldAccessCallingConventionX86_64 calling_convention;
4626 codegen_->GenerateUnresolvedFieldAccess(instruction,
4627 instruction->GetFieldType(),
4628 instruction->GetFieldIndex(),
4629 instruction->GetDexPc(),
4630 calling_convention);
4631}
4632
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004633void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004634 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4635 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4636 ? Location::RequiresRegister()
4637 : Location::Any();
4638 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004639}
4640
Calin Juravle2ae48182016-03-16 14:05:09 +00004641void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4642 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004643 return;
4644 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004645 LocationSummary* locations = instruction->GetLocations();
4646 Location obj = locations->InAt(0);
4647
4648 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004649 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004650}
4651
Calin Juravle2ae48182016-03-16 14:05:09 +00004652void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004653 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004654 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655
4656 LocationSummary* locations = instruction->GetLocations();
4657 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004658
4659 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004660 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004661 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004662 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004663 } else {
4664 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004665 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004666 __ jmp(slow_path->GetEntryLabel());
4667 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004668 }
4669 __ j(kEqual, slow_path->GetEntryLabel());
4670}
4671
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004672void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004673 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004674}
4675
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004676void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004677 bool object_array_get_with_read_barrier =
4678 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004679 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004680 new (GetGraph()->GetArena()) LocationSummary(instruction,
4681 object_array_get_with_read_barrier ?
4682 LocationSummary::kCallOnSlowPath :
4683 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004684 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004685 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004686 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004687 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004688 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004689 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4690 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4691 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004692 // The output overlaps for an object array get when read barriers
4693 // are enabled: we do not want the move to overwrite the array's
4694 // location, as we need it to emit the read barrier.
4695 locations->SetOut(
4696 Location::RequiresRegister(),
4697 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004698 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004699}
4700
4701void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4702 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004703 Location obj_loc = locations->InAt(0);
4704 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004705 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004706 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004707 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004708
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004709 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004710 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004712 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004713 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004714 break;
4715 }
4716
4717 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004718 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004719 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004720 break;
4721 }
4722
4723 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004724 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004725 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004726 break;
4727 }
4728
4729 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004730 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004731 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4732 // Branch cases into compressed and uncompressed for each index's type.
4733 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4734 NearLabel done, not_compressed;
4735 __ cmpl(Address(obj, count_offset), Immediate(0));
4736 codegen_->MaybeRecordImplicitNullCheck(instruction);
4737 __ j(kGreaterEqual, &not_compressed);
4738 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4739 __ jmp(&done);
4740 __ Bind(&not_compressed);
4741 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4742 __ Bind(&done);
4743 } else {
4744 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4745 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004746 break;
4747 }
4748
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004749 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004750 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004751 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004752 break;
4753 }
4754
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004755 case Primitive::kPrimNot: {
4756 static_assert(
4757 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4758 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004759 // /* HeapReference<Object> */ out =
4760 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4761 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004762 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004763 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004764 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004765 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004766 } else {
4767 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004768 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4769 codegen_->MaybeRecordImplicitNullCheck(instruction);
4770 // If read barriers are enabled, emit read barriers other than
4771 // Baker's using a slow path (and also unpoison the loaded
4772 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004773 if (index.IsConstant()) {
4774 uint32_t offset =
4775 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004776 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4777 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004778 codegen_->MaybeGenerateReadBarrierSlow(
4779 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4780 }
4781 }
4782 break;
4783 }
4784
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004785 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004786 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004787 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004788 break;
4789 }
4790
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004791 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004792 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004793 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004794 break;
4795 }
4796
4797 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004798 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004799 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004800 break;
4801 }
4802
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004803 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004804 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004805 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004806 }
Roland Levillain4d027112015-07-01 15:41:14 +01004807
4808 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004809 // Potential implicit null checks, in the case of reference
4810 // arrays, are handled in the previous switch statement.
4811 } else {
4812 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004813 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004814}
4815
4816void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004817 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004818
4819 bool needs_write_barrier =
4820 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004821 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004822
Nicolas Geoffray39468442014-09-02 15:17:15 +01004823 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004824 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004825 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004826 LocationSummary::kCallOnSlowPath :
4827 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004828
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004829 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004830 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4831 if (Primitive::IsFloatingPointType(value_type)) {
4832 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004833 } else {
4834 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4835 }
4836
4837 if (needs_write_barrier) {
4838 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004839 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004840 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004841 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842}
4843
4844void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4845 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004846 Location array_loc = locations->InAt(0);
4847 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004848 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004849 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004850 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004851 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004852 bool needs_write_barrier =
4853 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004854 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4855 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4856 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004857
4858 switch (value_type) {
4859 case Primitive::kPrimBoolean:
4860 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004861 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004862 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004863 if (value.IsRegister()) {
4864 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004865 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004866 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004867 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004868 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004869 break;
4870 }
4871
4872 case Primitive::kPrimShort:
4873 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004874 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004875 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004876 if (value.IsRegister()) {
4877 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004878 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004879 DCHECK(value.IsConstant()) << value;
4880 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004881 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004882 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004883 break;
4884 }
4885
4886 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004887 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004888 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004889
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004890 if (!value.IsRegister()) {
4891 // Just setting null.
4892 DCHECK(instruction->InputAt(2)->IsNullConstant());
4893 DCHECK(value.IsConstant()) << value;
4894 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004895 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004896 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004897 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004898 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004899 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004900
4901 DCHECK(needs_write_barrier);
4902 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004903 // We cannot use a NearLabel for `done`, as its range may be too
4904 // short when Baker read barriers are enabled.
4905 Label done;
4906 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004907 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004908 Location temp_loc = locations->GetTemp(0);
4909 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004910 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004911 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4912 codegen_->AddSlowPath(slow_path);
4913 if (instruction->GetValueCanBeNull()) {
4914 __ testl(register_value, register_value);
4915 __ j(kNotEqual, &not_null);
4916 __ movl(address, Immediate(0));
4917 codegen_->MaybeRecordImplicitNullCheck(instruction);
4918 __ jmp(&done);
4919 __ Bind(&not_null);
4920 }
4921
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004922 // Note that when Baker read barriers are enabled, the type
4923 // checks are performed without read barriers. This is fine,
4924 // even in the case where a class object is in the from-space
4925 // after the flip, as a comparison involving such a type would
4926 // not produce a false positive; it may of course produce a
4927 // false negative, in which case we would take the ArraySet
4928 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004929
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004930 // /* HeapReference<Class> */ temp = array->klass_
4931 __ movl(temp, Address(array, class_offset));
4932 codegen_->MaybeRecordImplicitNullCheck(instruction);
4933 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004934
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004935 // /* HeapReference<Class> */ temp = temp->component_type_
4936 __ movl(temp, Address(temp, component_offset));
4937 // If heap poisoning is enabled, no need to unpoison `temp`
4938 // nor the object reference in `register_value->klass`, as
4939 // we are comparing two poisoned references.
4940 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004941
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004942 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4943 __ j(kEqual, &do_put);
4944 // If heap poisoning is enabled, the `temp` reference has
4945 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004946 __ MaybeUnpoisonHeapReference(temp);
4947
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004948 // If heap poisoning is enabled, no need to unpoison the
4949 // heap reference loaded below, as it is only used for a
4950 // comparison with null.
4951 __ cmpl(Address(temp, super_offset), Immediate(0));
4952 __ j(kNotEqual, slow_path->GetEntryLabel());
4953 __ Bind(&do_put);
4954 } else {
4955 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004956 }
4957 }
4958
4959 if (kPoisonHeapReferences) {
4960 __ movl(temp, register_value);
4961 __ PoisonHeapReference(temp);
4962 __ movl(address, temp);
4963 } else {
4964 __ movl(address, register_value);
4965 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004966 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004967 codegen_->MaybeRecordImplicitNullCheck(instruction);
4968 }
4969
4970 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4971 codegen_->MarkGCCard(
4972 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4973 __ Bind(&done);
4974
4975 if (slow_path != nullptr) {
4976 __ Bind(slow_path->GetExitLabel());
4977 }
4978
4979 break;
4980 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004981
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004982 case Primitive::kPrimInt: {
4983 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004984 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004985 if (value.IsRegister()) {
4986 __ movl(address, value.AsRegister<CpuRegister>());
4987 } else {
4988 DCHECK(value.IsConstant()) << value;
4989 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4990 __ movl(address, Immediate(v));
4991 }
4992 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004993 break;
4994 }
4995
4996 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004997 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004998 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004999 if (value.IsRegister()) {
5000 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005001 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005002 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005003 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005004 Address address_high =
5005 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005006 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005007 }
5008 break;
5009 }
5010
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005011 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005012 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005013 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005014 if (value.IsFpuRegister()) {
5015 __ movss(address, value.AsFpuRegister<XmmRegister>());
5016 } else {
5017 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005018 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005019 __ movl(address, Immediate(v));
5020 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005021 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005022 break;
5023 }
5024
5025 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005026 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005027 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005028 if (value.IsFpuRegister()) {
5029 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5030 codegen_->MaybeRecordImplicitNullCheck(instruction);
5031 } else {
5032 int64_t v =
5033 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005034 Address address_high =
5035 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005036 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5037 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005038 break;
5039 }
5040
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041 case Primitive::kPrimVoid:
5042 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005043 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005044 }
5045}
5046
5047void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005048 LocationSummary* locations =
5049 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005050 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005051 if (!instruction->IsEmittedAtUseSite()) {
5052 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5053 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005054}
5055
5056void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005057 if (instruction->IsEmittedAtUseSite()) {
5058 return;
5059 }
5060
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005062 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005063 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5064 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005065 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005066 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005067 // Mask out most significant bit in case the array is String's array of char.
5068 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5069 __ andl(out, Immediate(INT32_MAX));
5070 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071}
5072
5073void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005074 RegisterSet caller_saves = RegisterSet::Empty();
5075 InvokeRuntimeCallingConvention calling_convention;
5076 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5077 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5078 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005079 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005080 HInstruction* length = instruction->InputAt(1);
5081 if (!length->IsEmittedAtUseSite()) {
5082 locations->SetInAt(1, Location::RegisterOrConstant(length));
5083 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005084}
5085
5086void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5087 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005088 Location index_loc = locations->InAt(0);
5089 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005090 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005091
Mark Mendell99dbd682015-04-22 16:18:52 -04005092 if (length_loc.IsConstant()) {
5093 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5094 if (index_loc.IsConstant()) {
5095 // BCE will remove the bounds check if we are guarenteed to pass.
5096 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5097 if (index < 0 || index >= length) {
5098 codegen_->AddSlowPath(slow_path);
5099 __ jmp(slow_path->GetEntryLabel());
5100 } else {
5101 // Some optimization after BCE may have generated this, and we should not
5102 // generate a bounds check if it is a valid range.
5103 }
5104 return;
5105 }
5106
5107 // We have to reverse the jump condition because the length is the constant.
5108 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5109 __ cmpl(index_reg, Immediate(length));
5110 codegen_->AddSlowPath(slow_path);
5111 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005112 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005113 HInstruction* array_length = instruction->InputAt(1);
5114 if (array_length->IsEmittedAtUseSite()) {
5115 // Address the length field in the array.
5116 DCHECK(array_length->IsArrayLength());
5117 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5118 Location array_loc = array_length->GetLocations()->InAt(0);
5119 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005120 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5121 CpuRegister length_reg = CpuRegister(TMP);
5122 __ movl(length_reg, array_len);
5123 codegen_->MaybeRecordImplicitNullCheck(array_length);
5124 __ andl(length_reg, Immediate(INT32_MAX));
5125 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005126 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005127 // Checking the bound for general case:
5128 // Array of char or String's array when the compression feature off.
5129 if (index_loc.IsConstant()) {
5130 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5131 __ cmpl(array_len, Immediate(value));
5132 } else {
5133 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5134 }
5135 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005136 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005137 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005138 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005139 }
5140 codegen_->AddSlowPath(slow_path);
5141 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005142 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143}
5144
5145void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5146 CpuRegister card,
5147 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005148 CpuRegister value,
5149 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005150 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005151 if (value_can_be_null) {
5152 __ testl(value, value);
5153 __ j(kEqual, &is_null);
5154 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005155 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005156 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005157 __ movq(temp, object);
5158 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005159 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005160 if (value_can_be_null) {
5161 __ Bind(&is_null);
5162 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163}
5164
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005165void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005166 LOG(FATAL) << "Unimplemented";
5167}
5168
5169void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005170 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5171}
5172
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005173void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005174 LocationSummary* locations =
5175 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005176 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005177}
5178
5179void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005180 HBasicBlock* block = instruction->GetBlock();
5181 if (block->GetLoopInformation() != nullptr) {
5182 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5183 // The back edge will generate the suspend check.
5184 return;
5185 }
5186 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5187 // The goto will generate the suspend check.
5188 return;
5189 }
5190 GenerateSuspendCheck(instruction, nullptr);
5191}
5192
5193void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5194 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005195 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005196 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5197 if (slow_path == nullptr) {
5198 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5199 instruction->SetSlowPath(slow_path);
5200 codegen_->AddSlowPath(slow_path);
5201 if (successor != nullptr) {
5202 DCHECK(successor->IsLoopHeader());
5203 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5204 }
5205 } else {
5206 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5207 }
5208
Andreas Gampe542451c2016-07-26 09:02:02 -07005209 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005210 /* no_rip */ true),
5211 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005212 if (successor == nullptr) {
5213 __ j(kNotEqual, slow_path->GetEntryLabel());
5214 __ Bind(slow_path->GetReturnLabel());
5215 } else {
5216 __ j(kEqual, codegen_->GetLabelOf(successor));
5217 __ jmp(slow_path->GetEntryLabel());
5218 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005219}
5220
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005221X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5222 return codegen_->GetAssembler();
5223}
5224
5225void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005226 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005227 Location source = move->GetSource();
5228 Location destination = move->GetDestination();
5229
5230 if (source.IsRegister()) {
5231 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005232 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005233 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005234 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005235 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005236 } else {
5237 DCHECK(destination.IsDoubleStackSlot());
5238 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005239 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005240 }
5241 } else if (source.IsStackSlot()) {
5242 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005243 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005244 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005245 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005246 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005247 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005248 } else {
5249 DCHECK(destination.IsStackSlot());
5250 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5251 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5252 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005253 } else if (source.IsDoubleStackSlot()) {
5254 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005255 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005256 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005257 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005258 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5259 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005260 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005261 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005262 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5263 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5264 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005265 } else if (source.IsConstant()) {
5266 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005267 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5268 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005269 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005270 if (value == 0) {
5271 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5272 } else {
5273 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5274 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005275 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005276 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005277 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005278 }
5279 } else if (constant->IsLongConstant()) {
5280 int64_t value = constant->AsLongConstant()->GetValue();
5281 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005282 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005283 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005284 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005285 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005286 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005287 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005288 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005289 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005290 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005291 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005292 } else {
5293 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005294 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005295 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5296 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005297 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005299 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005300 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005301 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005302 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005303 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005304 } else {
5305 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005306 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005307 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005308 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005309 } else if (source.IsFpuRegister()) {
5310 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005311 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005312 } else if (destination.IsStackSlot()) {
5313 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005314 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005315 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005316 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005317 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005318 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005319 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005320 }
5321}
5322
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005323void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005324 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005325 __ movl(Address(CpuRegister(RSP), mem), reg);
5326 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005327}
5328
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005329void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005330 ScratchRegisterScope ensure_scratch(
5331 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5332
5333 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5334 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5335 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5336 Address(CpuRegister(RSP), mem2 + stack_offset));
5337 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5338 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5339 CpuRegister(ensure_scratch.GetRegister()));
5340}
5341
Mark Mendell8a1c7282015-06-29 15:41:28 -04005342void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5343 __ movq(CpuRegister(TMP), reg1);
5344 __ movq(reg1, reg2);
5345 __ movq(reg2, CpuRegister(TMP));
5346}
5347
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005348void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5349 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5350 __ movq(Address(CpuRegister(RSP), mem), reg);
5351 __ movq(reg, CpuRegister(TMP));
5352}
5353
5354void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5355 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005356 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005357
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005358 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5359 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5360 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5361 Address(CpuRegister(RSP), mem2 + stack_offset));
5362 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5363 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5364 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005365}
5366
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005367void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5368 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5369 __ movss(Address(CpuRegister(RSP), mem), reg);
5370 __ movd(reg, CpuRegister(TMP));
5371}
5372
5373void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5374 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5375 __ movsd(Address(CpuRegister(RSP), mem), reg);
5376 __ movd(reg, CpuRegister(TMP));
5377}
5378
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005379void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005380 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005381 Location source = move->GetSource();
5382 Location destination = move->GetDestination();
5383
5384 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005385 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005386 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005387 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005388 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005389 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005390 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005391 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5392 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005393 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005394 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005395 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005396 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5397 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005398 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005399 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5400 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5401 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005402 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005403 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005404 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005405 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005406 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005407 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005408 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005409 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005410 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005411 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005412 }
5413}
5414
5415
5416void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5417 __ pushq(CpuRegister(reg));
5418}
5419
5420
5421void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5422 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005423}
5424
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005425void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005426 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005427 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5428 Immediate(mirror::Class::kStatusInitialized));
5429 __ j(kLess, slow_path->GetEntryLabel());
5430 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005431 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005432}
5433
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005434HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5435 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005436 switch (desired_class_load_kind) {
5437 case HLoadClass::LoadKind::kReferrersClass:
5438 break;
5439 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5440 DCHECK(!GetCompilerOptions().GetCompilePic());
5441 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5442 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5443 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5444 DCHECK(GetCompilerOptions().GetCompilePic());
5445 break;
5446 case HLoadClass::LoadKind::kBootImageAddress:
5447 break;
5448 case HLoadClass::LoadKind::kDexCacheAddress:
5449 DCHECK(Runtime::Current()->UseJitCompilation());
5450 break;
5451 case HLoadClass::LoadKind::kDexCachePcRelative:
5452 DCHECK(!Runtime::Current()->UseJitCompilation());
5453 break;
5454 case HLoadClass::LoadKind::kDexCacheViaMethod:
5455 break;
5456 }
5457 return desired_class_load_kind;
5458}
5459
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005460void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005461 if (cls->NeedsAccessCheck()) {
5462 InvokeRuntimeCallingConvention calling_convention;
5463 CodeGenerator::CreateLoadClassLocationSummary(
5464 cls,
5465 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5466 Location::RegisterLocation(RAX),
5467 /* code_generator_supports_read_barrier */ true);
5468 return;
5469 }
5470
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005471 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5472 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005473 ? LocationSummary::kCallOnSlowPath
5474 : LocationSummary::kNoCall;
5475 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005476 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005477 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005478 }
5479
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005480 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5481 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5482 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5483 locations->SetInAt(0, Location::RequiresRegister());
5484 }
5485 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005486}
5487
5488void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005489 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005490 if (cls->NeedsAccessCheck()) {
5491 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005492 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005493 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005494 return;
5495 }
5496
Roland Levillain0d5a2812015-11-13 10:07:31 +00005497 Location out_loc = locations->Out();
5498 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005499
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005500 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005501 bool generate_null_check = false;
5502 switch (cls->GetLoadKind()) {
5503 case HLoadClass::LoadKind::kReferrersClass: {
5504 DCHECK(!cls->CanCallRuntime());
5505 DCHECK(!cls->MustGenerateClinitCheck());
5506 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5507 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5508 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005509 cls,
5510 out_loc,
5511 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005512 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005513 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005514 break;
5515 }
5516 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005517 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005518 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5519 codegen_->RecordTypePatch(cls);
5520 break;
5521 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005522 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005523 DCHECK_NE(cls->GetAddress(), 0u);
5524 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5525 __ movl(out, Immediate(address)); // Zero-extended.
5526 codegen_->RecordSimplePatch();
5527 break;
5528 }
5529 case HLoadClass::LoadKind::kDexCacheAddress: {
5530 DCHECK_NE(cls->GetAddress(), 0u);
5531 // /* GcRoot<mirror::Class> */ out = *address
5532 if (IsUint<32>(cls->GetAddress())) {
5533 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005534 GenerateGcRootFieldLoad(cls,
5535 out_loc,
5536 address,
Roland Levillain00468f32016-10-27 18:02:48 +01005537 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005538 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005539 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005540 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5541 __ movq(out, Immediate(cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005542 GenerateGcRootFieldLoad(cls,
5543 out_loc,
5544 Address(out, 0),
Roland Levillain00468f32016-10-27 18:02:48 +01005545 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005546 requires_read_barrier);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005547 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005548 generate_null_check = !cls->IsInDexCache();
5549 break;
5550 }
5551 case HLoadClass::LoadKind::kDexCachePcRelative: {
5552 uint32_t offset = cls->GetDexCacheElementOffset();
5553 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5554 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5555 /* no_rip */ false);
5556 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005557 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005558 generate_null_check = !cls->IsInDexCache();
5559 break;
5560 }
5561 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5562 // /* GcRoot<mirror::Class>[] */ out =
5563 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5564 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5565 __ movq(out,
5566 Address(current_method,
5567 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5568 // /* GcRoot<mirror::Class> */ out = out[type_index]
5569 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005570 cls,
5571 out_loc,
5572 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
Roland Levillain00468f32016-10-27 18:02:48 +01005573 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005574 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005575 generate_null_check = !cls->IsInDexCache();
5576 break;
5577 }
5578 default:
5579 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5580 UNREACHABLE();
5581 }
5582
5583 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5584 DCHECK(cls->CanCallRuntime());
5585 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5586 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5587 codegen_->AddSlowPath(slow_path);
5588 if (generate_null_check) {
5589 __ testl(out, out);
5590 __ j(kEqual, slow_path->GetEntryLabel());
5591 }
5592 if (cls->MustGenerateClinitCheck()) {
5593 GenerateClassInitializationCheck(slow_path, out);
5594 } else {
5595 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005596 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005597 }
5598}
5599
5600void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5601 LocationSummary* locations =
5602 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5603 locations->SetInAt(0, Location::RequiresRegister());
5604 if (check->HasUses()) {
5605 locations->SetOut(Location::SameAsFirstInput());
5606 }
5607}
5608
5609void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005610 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005611 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005612 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005613 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005614 GenerateClassInitializationCheck(slow_path,
5615 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005616}
5617
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005618HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5619 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005620 switch (desired_string_load_kind) {
5621 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5622 DCHECK(!GetCompilerOptions().GetCompilePic());
5623 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5624 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5625 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5626 DCHECK(GetCompilerOptions().GetCompilePic());
5627 break;
5628 case HLoadString::LoadKind::kBootImageAddress:
5629 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005630 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005631 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005632 break;
5633 case HLoadString::LoadKind::kDexCacheViaMethod:
5634 break;
5635 }
5636 return desired_string_load_kind;
5637}
5638
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005639void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005640 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00005641 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5642 ? LocationSummary::kCallOnMainOnly
5643 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005644 : LocationSummary::kNoCall;
5645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005646 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005647 locations->SetOut(Location::RegisterLocation(RAX));
5648 } else {
5649 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005650 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5651 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5652 // Rely on the pResolveString and/or marking to save everything.
5653 // Custom calling convention: RAX serves as both input and output.
5654 RegisterSet caller_saves = RegisterSet::Empty();
5655 caller_saves.Add(Location::RegisterLocation(RAX));
5656 locations->SetCustomSlowPathCallerSaves(caller_saves);
5657 } else {
5658 // For non-Baker read barrier we have a temp-clobbering call.
5659 }
5660 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005661 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005662}
5663
5664void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005665 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005666 Location out_loc = locations->Out();
5667 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005668
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005669 switch (load->GetLoadKind()) {
5670 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005671 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005672 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005673 return; // No dex cache slow path.
5674 }
5675 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005676 DCHECK_NE(load->GetAddress(), 0u);
5677 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5678 __ movl(out, Immediate(address)); // Zero-extended.
5679 codegen_->RecordSimplePatch();
5680 return; // No dex cache slow path.
5681 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005682 case HLoadString::LoadKind::kBssEntry: {
5683 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5684 /* no_rip */ false);
5685 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5686 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Roland Levillain00468f32016-10-27 18:02:48 +01005687 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005688 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5689 codegen_->AddSlowPath(slow_path);
5690 __ testl(out, out);
5691 __ j(kEqual, slow_path->GetEntryLabel());
5692 __ Bind(slow_path->GetExitLabel());
5693 return;
5694 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005695 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005696 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005697 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005698
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005699 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005700 // Custom calling convention: RAX serves as both input and output.
5701 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex()));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005702 codegen_->InvokeRuntime(kQuickResolveString,
5703 load,
5704 load->GetDexPc());
5705 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005706}
5707
David Brazdilcb1c0552015-08-04 16:22:25 +01005708static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005709 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005710 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005711}
5712
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005713void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5714 LocationSummary* locations =
5715 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5716 locations->SetOut(Location::RequiresRegister());
5717}
5718
5719void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005720 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5721}
5722
5723void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5724 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5725}
5726
5727void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5728 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005729}
5730
5731void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5732 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005733 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005734 InvokeRuntimeCallingConvention calling_convention;
5735 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5736}
5737
5738void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005739 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005740 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005741}
5742
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005743static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5744 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005745 // We need a temporary for holding the iftable length.
5746 return true;
5747 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005748 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005749 !kUseBakerReadBarrier &&
5750 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005751 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5752 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5753}
5754
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005755static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5756 return kEmitCompilerReadBarrier &&
5757 !kUseBakerReadBarrier &&
5758 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5759 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5760 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5761}
5762
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005763void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005765 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005766 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005767 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005768 case TypeCheckKind::kExactCheck:
5769 case TypeCheckKind::kAbstractClassCheck:
5770 case TypeCheckKind::kClassHierarchyCheck:
5771 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005772 call_kind =
5773 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005774 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005775 break;
5776 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005777 case TypeCheckKind::kUnresolvedCheck:
5778 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005779 call_kind = LocationSummary::kCallOnSlowPath;
5780 break;
5781 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005782
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005783 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005784 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005785 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005786 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005787 locations->SetInAt(0, Location::RequiresRegister());
5788 locations->SetInAt(1, Location::Any());
5789 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5790 locations->SetOut(Location::RequiresRegister());
5791 // When read barriers are enabled, we need a temporary register for
5792 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005793 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005794 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005795 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005796}
5797
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005798void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005799 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005800 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005801 Location obj_loc = locations->InAt(0);
5802 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005803 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005804 Location out_loc = locations->Out();
5805 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005806 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005807 locations->GetTemp(0) :
5808 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005809 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005810 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5811 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5812 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005813 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005814 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005815
5816 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005817 // Avoid null check if we know obj is not null.
5818 if (instruction->MustDoNullCheck()) {
5819 __ testl(obj, obj);
5820 __ j(kEqual, &zero);
5821 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005822
Roland Levillain0d5a2812015-11-13 10:07:31 +00005823 // /* HeapReference<Class> */ out = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005824 GenerateReferenceLoadTwoRegisters(instruction,
5825 out_loc,
5826 obj_loc,
5827 class_offset,
5828 kEmitCompilerReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005830 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831 case TypeCheckKind::kExactCheck: {
5832 if (cls.IsRegister()) {
5833 __ cmpl(out, cls.AsRegister<CpuRegister>());
5834 } else {
5835 DCHECK(cls.IsStackSlot()) << cls;
5836 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5837 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005838 if (zero.IsLinked()) {
5839 // Classes must be equal for the instanceof to succeed.
5840 __ j(kNotEqual, &zero);
5841 __ movl(out, Immediate(1));
5842 __ jmp(&done);
5843 } else {
5844 __ setcc(kEqual, out);
5845 // setcc only sets the low byte.
5846 __ andl(out, Immediate(1));
5847 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005848 break;
5849 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005850
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851 case TypeCheckKind::kAbstractClassCheck: {
5852 // If the class is abstract, we eagerly fetch the super class of the
5853 // object to avoid doing a comparison we know will fail.
5854 NearLabel loop, success;
5855 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005856 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005857 GenerateReferenceLoadOneRegister(instruction,
5858 out_loc,
5859 super_offset,
5860 maybe_temp_loc,
5861 kEmitCompilerReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005862 __ testl(out, out);
5863 // If `out` is null, we use it for the result, and jump to `done`.
5864 __ j(kEqual, &done);
5865 if (cls.IsRegister()) {
5866 __ cmpl(out, cls.AsRegister<CpuRegister>());
5867 } else {
5868 DCHECK(cls.IsStackSlot()) << cls;
5869 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5870 }
5871 __ j(kNotEqual, &loop);
5872 __ movl(out, Immediate(1));
5873 if (zero.IsLinked()) {
5874 __ jmp(&done);
5875 }
5876 break;
5877 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005878
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005879 case TypeCheckKind::kClassHierarchyCheck: {
5880 // Walk over the class hierarchy to find a match.
5881 NearLabel loop, success;
5882 __ Bind(&loop);
5883 if (cls.IsRegister()) {
5884 __ cmpl(out, cls.AsRegister<CpuRegister>());
5885 } else {
5886 DCHECK(cls.IsStackSlot()) << cls;
5887 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5888 }
5889 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005890 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005891 GenerateReferenceLoadOneRegister(instruction,
5892 out_loc,
5893 super_offset,
5894 maybe_temp_loc,
5895 kEmitCompilerReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005896 __ testl(out, out);
5897 __ j(kNotEqual, &loop);
5898 // If `out` is null, we use it for the result, and jump to `done`.
5899 __ jmp(&done);
5900 __ Bind(&success);
5901 __ movl(out, Immediate(1));
5902 if (zero.IsLinked()) {
5903 __ jmp(&done);
5904 }
5905 break;
5906 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005907
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005908 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005909 // Do an exact check.
5910 NearLabel exact_check;
5911 if (cls.IsRegister()) {
5912 __ cmpl(out, cls.AsRegister<CpuRegister>());
5913 } else {
5914 DCHECK(cls.IsStackSlot()) << cls;
5915 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5916 }
5917 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005918 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005919 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005920 GenerateReferenceLoadOneRegister(instruction,
5921 out_loc,
5922 component_offset,
5923 maybe_temp_loc,
5924 kEmitCompilerReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 __ testl(out, out);
5926 // If `out` is null, we use it for the result, and jump to `done`.
5927 __ j(kEqual, &done);
5928 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5929 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005930 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005931 __ movl(out, Immediate(1));
5932 __ jmp(&done);
5933 break;
5934 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005935
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005936 case TypeCheckKind::kArrayCheck: {
5937 if (cls.IsRegister()) {
5938 __ cmpl(out, cls.AsRegister<CpuRegister>());
5939 } else {
5940 DCHECK(cls.IsStackSlot()) << cls;
5941 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5942 }
5943 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005944 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5945 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005946 codegen_->AddSlowPath(slow_path);
5947 __ j(kNotEqual, slow_path->GetEntryLabel());
5948 __ movl(out, Immediate(1));
5949 if (zero.IsLinked()) {
5950 __ jmp(&done);
5951 }
5952 break;
5953 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005954
Calin Juravle98893e12015-10-02 21:05:03 +01005955 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005956 case TypeCheckKind::kInterfaceCheck: {
5957 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005958 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005959 // cases.
5960 //
5961 // We cannot directly call the InstanceofNonTrivial runtime
5962 // entry point without resorting to a type checking slow path
5963 // here (i.e. by calling InvokeRuntime directly), as it would
5964 // require to assign fixed registers for the inputs of this
5965 // HInstanceOf instruction (following the runtime calling
5966 // convention), which might be cluttered by the potential first
5967 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005968 //
5969 // TODO: Introduce a new runtime entry point taking the object
5970 // to test (instead of its class) as argument, and let it deal
5971 // with the read barrier issues. This will let us refactor this
5972 // case of the `switch` code as it was previously (with a direct
5973 // call to the runtime not using a type checking slow path).
5974 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005975 DCHECK(locations->OnlyCallsOnSlowPath());
5976 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5977 /* is_fatal */ false);
5978 codegen_->AddSlowPath(slow_path);
5979 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005980 if (zero.IsLinked()) {
5981 __ jmp(&done);
5982 }
5983 break;
5984 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005985 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005986
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005988 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005989 __ xorl(out, out);
5990 }
5991
5992 if (done.IsLinked()) {
5993 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005994 }
5995
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005996 if (slow_path != nullptr) {
5997 __ Bind(slow_path->GetExitLabel());
5998 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005999}
6000
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006001static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006002 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006003 case TypeCheckKind::kExactCheck:
6004 case TypeCheckKind::kAbstractClassCheck:
6005 case TypeCheckKind::kClassHierarchyCheck:
6006 case TypeCheckKind::kArrayObjectCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006007 return !throws_into_catch && !kEmitCompilerReadBarrier;
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006008 case TypeCheckKind::kInterfaceCheck:
6009 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006010 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006011 case TypeCheckKind::kUnresolvedCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006012 return false;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006013 }
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006014 LOG(FATAL) << "Unreachable";
6015 UNREACHABLE();
6016}
6017
6018void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
6019 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6020 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6021 bool is_fatal_slow_path = IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch);
6022 LocationSummary::CallKind call_kind = is_fatal_slow_path
6023 ? LocationSummary::kNoCall
6024 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006025 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6026 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006027 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6028 // Require a register for the interface check since there is a loop that compares the class to
6029 // a memory address.
6030 locations->SetInAt(1, Location::RequiresRegister());
6031 } else {
6032 locations->SetInAt(1, Location::Any());
6033 }
6034
Roland Levillain0d5a2812015-11-13 10:07:31 +00006035 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6036 locations->AddTemp(Location::RequiresRegister());
6037 // When read barriers are enabled, we need an additional temporary
6038 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006039 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006040 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006041 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006042}
6043
6044void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006045 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006046 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006047 Location obj_loc = locations->InAt(0);
6048 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006049 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006050 Location temp_loc = locations->GetTemp(0);
6051 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006052 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006053 locations->GetTemp(1) :
6054 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006055 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6056 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6057 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6058 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6059 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6060 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006061 const uint32_t object_array_data_offset =
6062 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006063
Roland Levillain0d5a2812015-11-13 10:07:31 +00006064 bool is_type_check_slow_path_fatal =
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006065 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006066 SlowPathCode* type_check_slow_path =
6067 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6068 is_type_check_slow_path_fatal);
6069 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006070
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006071
6072 NearLabel done;
6073 // Avoid null check if we know obj is not null.
6074 if (instruction->MustDoNullCheck()) {
6075 __ testl(obj, obj);
6076 __ j(kEqual, &done);
6077 }
6078
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 case TypeCheckKind::kExactCheck:
6081 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006082 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006083 GenerateReferenceLoadTwoRegisters(instruction,
6084 temp_loc,
6085 obj_loc,
6086 class_offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006087 /*emit_read_barrier*/ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006088 if (cls.IsRegister()) {
6089 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6090 } else {
6091 DCHECK(cls.IsStackSlot()) << cls;
6092 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6093 }
6094 // Jump to slow path for throwing the exception or doing a
6095 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006096 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006097 break;
6098 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006099
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006100 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006101 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006102 GenerateReferenceLoadTwoRegisters(instruction,
6103 temp_loc,
6104 obj_loc,
6105 class_offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006106 /*emit_read_barrier*/ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006107 // If the class is abstract, we eagerly fetch the super class of the
6108 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006109 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006110 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006111 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006112 GenerateReferenceLoadOneRegister(instruction,
6113 temp_loc,
6114 super_offset,
6115 maybe_temp2_loc,
6116 /*emit_read_barrier*/ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006117
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006118 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6119 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006120 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006121 // Otherwise, compare the classes.
6122 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006123 if (cls.IsRegister()) {
6124 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6125 } else {
6126 DCHECK(cls.IsStackSlot()) << cls;
6127 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6128 }
6129 __ j(kNotEqual, &loop);
6130 break;
6131 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006132
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006134 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006135 GenerateReferenceLoadTwoRegisters(instruction,
6136 temp_loc,
6137 obj_loc,
6138 class_offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006139 /*emit_read_barrier*/ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006140 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006141 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 __ Bind(&loop);
6143 if (cls.IsRegister()) {
6144 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6145 } else {
6146 DCHECK(cls.IsStackSlot()) << cls;
6147 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6148 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006149 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006150
Roland Levillain0d5a2812015-11-13 10:07:31 +00006151 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006152 GenerateReferenceLoadOneRegister(instruction,
6153 temp_loc,
6154 super_offset,
6155 maybe_temp2_loc,
6156 /*emit_read_barrier*/ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006157
6158 // If the class reference currently in `temp` is not null, jump
6159 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006160 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006161 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006162 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006163 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006164 break;
6165 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006166
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006168 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006169 GenerateReferenceLoadTwoRegisters(instruction,
6170 temp_loc,
6171 obj_loc,
6172 class_offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006173 /*emit_read_barrier*/ false);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006174 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006175 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006176 if (cls.IsRegister()) {
6177 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6178 } else {
6179 DCHECK(cls.IsStackSlot()) << cls;
6180 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6181 }
6182 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183
6184 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006185 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006186 GenerateReferenceLoadOneRegister(instruction,
6187 temp_loc,
6188 component_offset,
6189 maybe_temp2_loc,
6190 /*emit_read_barrier*/ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006191
6192 // If the component type is not null (i.e. the object is indeed
6193 // an array), jump to label `check_non_primitive_component_type`
6194 // to further check that this component type is not a primitive
6195 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006197 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006198 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006199 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006200 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201 break;
6202 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006203
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006204 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006205 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006206 //
6207 // We cannot directly call the CheckCast runtime entry point
6208 // without resorting to a type checking slow path here (i.e. by
6209 // calling InvokeRuntime directly), as it would require to
6210 // assign fixed registers for the inputs of this HInstanceOf
6211 // instruction (following the runtime calling convention), which
6212 // might be cluttered by the potential first read barrier
6213 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006214 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006215 break;
6216 }
6217
6218 case TypeCheckKind::kInterfaceCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006219 // Fast path for the interface check. We always go slow path for heap poisoning since
6220 // unpoisoning cls would require an extra temp.
6221 if (!kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006222 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6223 // doing this.
6224 // /* HeapReference<Class> */ temp = obj->klass_
6225 GenerateReferenceLoadTwoRegisters(instruction,
6226 temp_loc,
6227 obj_loc,
6228 class_offset,
6229 /*emit_read_barrier*/ false);
6230
6231 // /* HeapReference<Class> */ temp = temp->iftable_
6232 GenerateReferenceLoadTwoRegisters(instruction,
6233 temp_loc,
6234 temp_loc,
6235 iftable_offset,
6236 /*emit_read_barrier*/ false);
6237 NearLabel is_null;
6238 // Null iftable means it is empty.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006239 __ testl(temp, temp);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006240 __ j(kZero, &is_null);
6241
6242 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006243 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006244
6245 NearLabel start_loop;
6246 __ Bind(&start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006247 __ cmpl(cls.AsRegister<CpuRegister>(), Address(temp, object_array_data_offset));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006248 __ j(kEqual, &done); // Return if same class.
6249 // Go to next interface.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006250 __ addl(temp, Immediate(2 * kHeapReferenceSize));
6251 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006252 __ j(kNotZero, &start_loop);
6253 __ Bind(&is_null);
6254 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006255 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006256 break;
6257 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006258
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006259 if (done.IsLinked()) {
6260 __ Bind(&done);
6261 }
6262
Roland Levillain0d5a2812015-11-13 10:07:31 +00006263 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006264}
6265
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006266void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6267 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006268 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006269 InvokeRuntimeCallingConvention calling_convention;
6270 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6271}
6272
6273void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006274 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006275 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006276 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006277 if (instruction->IsEnter()) {
6278 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6279 } else {
6280 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6281 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006282}
6283
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006284void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6285void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6286void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6287
6288void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6289 LocationSummary* locations =
6290 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6291 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6292 || instruction->GetResultType() == Primitive::kPrimLong);
6293 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006294 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006295 locations->SetOut(Location::SameAsFirstInput());
6296}
6297
6298void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6299 HandleBitwiseOperation(instruction);
6300}
6301
6302void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6303 HandleBitwiseOperation(instruction);
6304}
6305
6306void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6307 HandleBitwiseOperation(instruction);
6308}
6309
6310void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6311 LocationSummary* locations = instruction->GetLocations();
6312 Location first = locations->InAt(0);
6313 Location second = locations->InAt(1);
6314 DCHECK(first.Equals(locations->Out()));
6315
6316 if (instruction->GetResultType() == Primitive::kPrimInt) {
6317 if (second.IsRegister()) {
6318 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006319 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006320 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006321 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006322 } else {
6323 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006324 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006325 }
6326 } else if (second.IsConstant()) {
6327 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6328 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006329 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006330 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006331 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006332 } else {
6333 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006334 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006335 }
6336 } else {
6337 Address address(CpuRegister(RSP), second.GetStackIndex());
6338 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006339 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006340 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006341 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006342 } else {
6343 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006344 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006345 }
6346 }
6347 } else {
6348 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006349 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6350 bool second_is_constant = false;
6351 int64_t value = 0;
6352 if (second.IsConstant()) {
6353 second_is_constant = true;
6354 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006355 }
Mark Mendell40741f32015-04-20 22:10:34 -04006356 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006357
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006358 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006359 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006360 if (is_int32_value) {
6361 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6362 } else {
6363 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6364 }
6365 } else if (second.IsDoubleStackSlot()) {
6366 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006367 } else {
6368 __ andq(first_reg, second.AsRegister<CpuRegister>());
6369 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006370 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006371 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006372 if (is_int32_value) {
6373 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6374 } else {
6375 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6376 }
6377 } else if (second.IsDoubleStackSlot()) {
6378 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006379 } else {
6380 __ orq(first_reg, second.AsRegister<CpuRegister>());
6381 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006382 } else {
6383 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006384 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006385 if (is_int32_value) {
6386 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6387 } else {
6388 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6389 }
6390 } else if (second.IsDoubleStackSlot()) {
6391 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006392 } else {
6393 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6394 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006395 }
6396 }
6397}
6398
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006399void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6400 Location out,
6401 uint32_t offset,
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006402 Location maybe_temp,
6403 bool emit_read_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006404 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006405 if (emit_read_barrier) {
6406 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006407 if (kUseBakerReadBarrier) {
6408 // Load with fast path based Baker's read barrier.
6409 // /* HeapReference<Object> */ out = *(out + offset)
6410 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006411 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006412 } else {
6413 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006414 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006415 // in the following move operation, as we will need it for the
6416 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006417 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006418 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006419 // /* HeapReference<Object> */ out = *(out + offset)
6420 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006421 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006422 }
6423 } else {
6424 // Plain load with no read barrier.
6425 // /* HeapReference<Object> */ out = *(out + offset)
6426 __ movl(out_reg, Address(out_reg, offset));
6427 __ MaybeUnpoisonHeapReference(out_reg);
6428 }
6429}
6430
6431void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6432 Location out,
6433 Location obj,
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006434 uint32_t offset,
6435 bool emit_read_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006436 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6437 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006438 if (emit_read_barrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006439 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006440 if (kUseBakerReadBarrier) {
6441 // Load with fast path based Baker's read barrier.
6442 // /* HeapReference<Object> */ out = *(obj + offset)
6443 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006444 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006445 } else {
6446 // Load with slow path based read barrier.
6447 // /* HeapReference<Object> */ out = *(obj + offset)
6448 __ movl(out_reg, Address(obj_reg, offset));
6449 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6450 }
6451 } else {
6452 // Plain load with no read barrier.
6453 // /* HeapReference<Object> */ out = *(obj + offset)
6454 __ movl(out_reg, Address(obj_reg, offset));
6455 __ MaybeUnpoisonHeapReference(out_reg);
6456 }
6457}
6458
6459void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6460 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006461 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006462 Label* fixup_label,
6463 bool requires_read_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006464 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006465 if (requires_read_barrier) {
6466 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006467 if (kUseBakerReadBarrier) {
6468 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6469 // Baker's read barrier are used:
6470 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006471 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006472 // if (Thread::Current()->GetIsGcMarking()) {
6473 // root = ReadBarrier::Mark(root)
6474 // }
6475
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006476 // /* GcRoot<mirror::Object> */ root = *address
6477 __ movl(root_reg, address);
6478 if (fixup_label != nullptr) {
6479 __ Bind(fixup_label);
6480 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006481 static_assert(
6482 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6483 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6484 "have different sizes.");
6485 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6486 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6487 "have different sizes.");
6488
Vladimir Marko953437b2016-08-24 08:30:46 +00006489 // Slow path marking the GC root `root`.
6490 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006491 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006492 codegen_->AddSlowPath(slow_path);
6493
Andreas Gampe542451c2016-07-26 09:02:02 -07006494 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006495 /* no_rip */ true),
6496 Immediate(0));
6497 __ j(kNotEqual, slow_path->GetEntryLabel());
6498 __ Bind(slow_path->GetExitLabel());
6499 } else {
6500 // GC root loaded through a slow path for read barriers other
6501 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006502 // /* GcRoot<mirror::Object>* */ root = address
6503 __ leaq(root_reg, address);
6504 if (fixup_label != nullptr) {
6505 __ Bind(fixup_label);
6506 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006507 // /* mirror::Object* */ root = root->Read()
6508 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6509 }
6510 } else {
6511 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006512 // /* GcRoot<mirror::Object> */ root = *address
6513 __ movl(root_reg, address);
6514 if (fixup_label != nullptr) {
6515 __ Bind(fixup_label);
6516 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006517 // Note that GC roots are not affected by heap poisoning, thus we
6518 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006519 }
6520}
6521
6522void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6523 Location ref,
6524 CpuRegister obj,
6525 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006526 bool needs_null_check) {
6527 DCHECK(kEmitCompilerReadBarrier);
6528 DCHECK(kUseBakerReadBarrier);
6529
6530 // /* HeapReference<Object> */ ref = *(obj + offset)
6531 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006532 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006533}
6534
6535void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6536 Location ref,
6537 CpuRegister obj,
6538 uint32_t data_offset,
6539 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006540 bool needs_null_check) {
6541 DCHECK(kEmitCompilerReadBarrier);
6542 DCHECK(kUseBakerReadBarrier);
6543
Roland Levillain3d312422016-06-23 13:53:42 +01006544 static_assert(
6545 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6546 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006547 // /* HeapReference<Object> */ ref =
6548 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006549 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006550 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006551}
6552
6553void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6554 Location ref,
6555 CpuRegister obj,
6556 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006557 bool needs_null_check,
6558 bool always_update_field,
6559 CpuRegister* temp1,
6560 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006561 DCHECK(kEmitCompilerReadBarrier);
6562 DCHECK(kUseBakerReadBarrier);
6563
6564 // In slow path based read barriers, the read barrier call is
6565 // inserted after the original load. However, in fast path based
6566 // Baker's read barriers, we need to perform the load of
6567 // mirror::Object::monitor_ *before* the original reference load.
6568 // This load-load ordering is required by the read barrier.
6569 // The fast path/slow path (for Baker's algorithm) should look like:
6570 //
6571 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6572 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6573 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006574 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006575 // if (is_gray) {
6576 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6577 // }
6578 //
6579 // Note: the original implementation in ReadBarrier::Barrier is
6580 // slightly more complex as:
6581 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006582 // the high-bits of rb_state, which are expected to be all zeroes
6583 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6584 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006585 // - it performs additional checks that we do not do here for
6586 // performance reasons.
6587
6588 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006589 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6590
Vladimir Marko953437b2016-08-24 08:30:46 +00006591 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006592 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6593 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006594 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6595 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6596 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6597
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006598 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006599 // ref = ReadBarrier::Mark(ref);
6600 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6601 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006602 if (needs_null_check) {
6603 MaybeRecordImplicitNullCheck(instruction);
6604 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006605
6606 // Load fence to prevent load-load reordering.
6607 // Note that this is a no-op, thanks to the x86-64 memory model.
6608 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6609
6610 // The actual reference load.
6611 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006612 __ movl(ref_reg, src); // Flags are unaffected.
6613
6614 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6615 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006616 SlowPathCode* slow_path;
6617 if (always_update_field) {
6618 DCHECK(temp1 != nullptr);
6619 DCHECK(temp2 != nullptr);
6620 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
6621 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6622 } else {
6623 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6624 instruction, ref, /* unpoison_ref_before_marking */ true);
6625 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006626 AddSlowPath(slow_path);
6627
6628 // We have done the "if" of the gray bit check above, now branch based on the flags.
6629 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006630
6631 // Object* ref = ref_addr->AsMirrorPtr()
6632 __ MaybeUnpoisonHeapReference(ref_reg);
6633
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006634 __ Bind(slow_path->GetExitLabel());
6635}
6636
6637void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6638 Location out,
6639 Location ref,
6640 Location obj,
6641 uint32_t offset,
6642 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006643 DCHECK(kEmitCompilerReadBarrier);
6644
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006645 // Insert a slow path based read barrier *after* the reference load.
6646 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006647 // If heap poisoning is enabled, the unpoisoning of the loaded
6648 // reference will be carried out by the runtime within the slow
6649 // path.
6650 //
6651 // Note that `ref` currently does not get unpoisoned (when heap
6652 // poisoning is enabled), which is alright as the `ref` argument is
6653 // not used by the artReadBarrierSlow entry point.
6654 //
6655 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6656 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6657 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6658 AddSlowPath(slow_path);
6659
Roland Levillain0d5a2812015-11-13 10:07:31 +00006660 __ jmp(slow_path->GetEntryLabel());
6661 __ Bind(slow_path->GetExitLabel());
6662}
6663
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006664void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6665 Location out,
6666 Location ref,
6667 Location obj,
6668 uint32_t offset,
6669 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006671 // Baker's read barriers shall be handled by the fast path
6672 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6673 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006674 // If heap poisoning is enabled, unpoisoning will be taken care of
6675 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006676 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006677 } else if (kPoisonHeapReferences) {
6678 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6679 }
6680}
6681
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006682void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6683 Location out,
6684 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006685 DCHECK(kEmitCompilerReadBarrier);
6686
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006687 // Insert a slow path based read barrier *after* the GC root load.
6688 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006689 // Note that GC roots are not affected by heap poisoning, so we do
6690 // not need to do anything special for this here.
6691 SlowPathCode* slow_path =
6692 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6693 AddSlowPath(slow_path);
6694
Roland Levillain0d5a2812015-11-13 10:07:31 +00006695 __ jmp(slow_path->GetEntryLabel());
6696 __ Bind(slow_path->GetExitLabel());
6697}
6698
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006699void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006700 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006701 LOG(FATAL) << "Unreachable";
6702}
6703
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006704void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006705 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006706 LOG(FATAL) << "Unreachable";
6707}
6708
Mark Mendellfe57faa2015-09-18 09:26:15 -04006709// Simple implementation of packed switch - generate cascaded compare/jumps.
6710void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6711 LocationSummary* locations =
6712 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6713 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006714 locations->AddTemp(Location::RequiresRegister());
6715 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006716}
6717
6718void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6719 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006720 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006721 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006722 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6723 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6724 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006725 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6726
6727 // Should we generate smaller inline compare/jumps?
6728 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6729 // Figure out the correct compare values and jump conditions.
6730 // Handle the first compare/branch as a special case because it might
6731 // jump to the default case.
6732 DCHECK_GT(num_entries, 2u);
6733 Condition first_condition;
6734 uint32_t index;
6735 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6736 if (lower_bound != 0) {
6737 first_condition = kLess;
6738 __ cmpl(value_reg_in, Immediate(lower_bound));
6739 __ j(first_condition, codegen_->GetLabelOf(default_block));
6740 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6741
6742 index = 1;
6743 } else {
6744 // Handle all the compare/jumps below.
6745 first_condition = kBelow;
6746 index = 0;
6747 }
6748
6749 // Handle the rest of the compare/jumps.
6750 for (; index + 1 < num_entries; index += 2) {
6751 int32_t compare_to_value = lower_bound + index + 1;
6752 __ cmpl(value_reg_in, Immediate(compare_to_value));
6753 // Jump to successors[index] if value < case_value[index].
6754 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6755 // Jump to successors[index + 1] if value == case_value[index + 1].
6756 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6757 }
6758
6759 if (index != num_entries) {
6760 // There are an odd number of entries. Handle the last one.
6761 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006762 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006763 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6764 }
6765
6766 // And the default for any other value.
6767 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6768 __ jmp(codegen_->GetLabelOf(default_block));
6769 }
6770 return;
6771 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006772
6773 // Remove the bias, if needed.
6774 Register value_reg_out = value_reg_in.AsRegister();
6775 if (lower_bound != 0) {
6776 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6777 value_reg_out = temp_reg.AsRegister();
6778 }
6779 CpuRegister value_reg(value_reg_out);
6780
6781 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006782 __ cmpl(value_reg, Immediate(num_entries - 1));
6783 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006784
Mark Mendell9c86b482015-09-18 13:36:07 -04006785 // We are in the range of the table.
6786 // Load the address of the jump table in the constant area.
6787 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006788
Mark Mendell9c86b482015-09-18 13:36:07 -04006789 // Load the (signed) offset from the jump table.
6790 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6791
6792 // Add the offset to the address of the table base.
6793 __ addq(temp_reg, base_reg);
6794
6795 // And jump.
6796 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006797}
6798
Aart Bikc5d47542016-01-27 17:00:35 -08006799void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6800 if (value == 0) {
6801 __ xorl(dest, dest);
6802 } else {
6803 __ movl(dest, Immediate(value));
6804 }
6805}
6806
Mark Mendell92e83bf2015-05-07 11:25:03 -04006807void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6808 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006809 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006810 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006811 } else if (IsUint<32>(value)) {
6812 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006813 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6814 } else {
6815 __ movq(dest, Immediate(value));
6816 }
6817}
6818
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006819void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6820 if (value == 0) {
6821 __ xorps(dest, dest);
6822 } else {
6823 __ movss(dest, LiteralInt32Address(value));
6824 }
6825}
6826
6827void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6828 if (value == 0) {
6829 __ xorpd(dest, dest);
6830 } else {
6831 __ movsd(dest, LiteralInt64Address(value));
6832 }
6833}
6834
6835void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6836 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6837}
6838
6839void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6840 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6841}
6842
Aart Bika19616e2016-02-01 18:57:58 -08006843void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6844 if (value == 0) {
6845 __ testl(dest, dest);
6846 } else {
6847 __ cmpl(dest, Immediate(value));
6848 }
6849}
6850
6851void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6852 if (IsInt<32>(value)) {
6853 if (value == 0) {
6854 __ testq(dest, dest);
6855 } else {
6856 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6857 }
6858 } else {
6859 // Value won't fit in an int.
6860 __ cmpq(dest, LiteralInt64Address(value));
6861 }
6862}
6863
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006864void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6865 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006866 GenerateIntCompare(lhs_reg, rhs);
6867}
6868
6869void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006870 if (rhs.IsConstant()) {
6871 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006872 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006873 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006874 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006875 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006876 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006877 }
6878}
6879
6880void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6881 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6882 if (rhs.IsConstant()) {
6883 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6884 Compare64BitValue(lhs_reg, value);
6885 } else if (rhs.IsDoubleStackSlot()) {
6886 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6887 } else {
6888 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6889 }
6890}
6891
6892Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6893 Location index,
6894 ScaleFactor scale,
6895 uint32_t data_offset) {
6896 return index.IsConstant() ?
6897 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6898 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6899}
6900
Mark Mendellcfa410b2015-05-25 16:02:44 -04006901void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6902 DCHECK(dest.IsDoubleStackSlot());
6903 if (IsInt<32>(value)) {
6904 // Can move directly as an int32 constant.
6905 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6906 Immediate(static_cast<int32_t>(value)));
6907 } else {
6908 Load64BitValue(CpuRegister(TMP), value);
6909 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6910 }
6911}
6912
Mark Mendell9c86b482015-09-18 13:36:07 -04006913/**
6914 * Class to handle late fixup of offsets into constant area.
6915 */
6916class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6917 public:
6918 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6919 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6920
6921 protected:
6922 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6923
6924 CodeGeneratorX86_64* codegen_;
6925
6926 private:
6927 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6928 // Patch the correct offset for the instruction. We use the address of the
6929 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6930 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6931 int32_t relative_position = constant_offset - pos;
6932
6933 // Patch in the right value.
6934 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6935 }
6936
6937 // Location in constant area that the fixup refers to.
6938 size_t offset_into_constant_area_;
6939};
6940
6941/**
6942 t * Class to handle late fixup of offsets to a jump table that will be created in the
6943 * constant area.
6944 */
6945class JumpTableRIPFixup : public RIPFixup {
6946 public:
6947 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6948 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6949
6950 void CreateJumpTable() {
6951 X86_64Assembler* assembler = codegen_->GetAssembler();
6952
6953 // Ensure that the reference to the jump table has the correct offset.
6954 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6955 SetOffset(offset_in_constant_table);
6956
6957 // Compute the offset from the start of the function to this jump table.
6958 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6959
6960 // Populate the jump table with the correct values for the jump table.
6961 int32_t num_entries = switch_instr_->GetNumEntries();
6962 HBasicBlock* block = switch_instr_->GetBlock();
6963 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6964 // The value that we want is the target offset - the position of the table.
6965 for (int32_t i = 0; i < num_entries; i++) {
6966 HBasicBlock* b = successors[i];
6967 Label* l = codegen_->GetLabelOf(b);
6968 DCHECK(l->IsBound());
6969 int32_t offset_to_block = l->Position() - current_table_offset;
6970 assembler->AppendInt32(offset_to_block);
6971 }
6972 }
6973
6974 private:
6975 const HPackedSwitch* switch_instr_;
6976};
6977
Mark Mendellf55c3e02015-03-26 21:07:46 -04006978void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6979 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006980 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006981 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6982 // 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 -04006983 assembler->Align(4, 0);
6984 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006985
6986 // Populate any jump tables.
6987 for (auto jump_table : fixups_to_jump_tables_) {
6988 jump_table->CreateJumpTable();
6989 }
6990
6991 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006992 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006993 }
6994
6995 // And finish up.
6996 CodeGenerator::Finalize(allocator);
6997}
6998
Mark Mendellf55c3e02015-03-26 21:07:46 -04006999Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
7000 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7001 return Address::RIP(fixup);
7002}
7003
7004Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
7005 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7006 return Address::RIP(fixup);
7007}
7008
7009Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
7010 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7011 return Address::RIP(fixup);
7012}
7013
7014Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
7015 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7016 return Address::RIP(fixup);
7017}
7018
Andreas Gampe85b62f22015-09-09 13:15:38 -07007019// TODO: trg as memory.
7020void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7021 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007022 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007023 return;
7024 }
7025
7026 DCHECK_NE(type, Primitive::kPrimVoid);
7027
7028 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7029 if (trg.Equals(return_loc)) {
7030 return;
7031 }
7032
7033 // Let the parallel move resolver take care of all of this.
7034 HParallelMove parallel_move(GetGraph()->GetArena());
7035 parallel_move.AddMove(return_loc, trg, type, nullptr);
7036 GetMoveResolver()->EmitNativeCode(&parallel_move);
7037}
7038
Mark Mendell9c86b482015-09-18 13:36:07 -04007039Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7040 // Create a fixup to be used to create and address the jump table.
7041 JumpTableRIPFixup* table_fixup =
7042 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7043
7044 // We have to populate the jump tables.
7045 fixups_to_jump_tables_.push_back(table_fixup);
7046 return Address::RIP(table_fixup);
7047}
7048
Mark Mendellea5af682015-10-22 17:35:49 -04007049void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7050 const Address& addr_high,
7051 int64_t v,
7052 HInstruction* instruction) {
7053 if (IsInt<32>(v)) {
7054 int32_t v_32 = v;
7055 __ movq(addr_low, Immediate(v_32));
7056 MaybeRecordImplicitNullCheck(instruction);
7057 } else {
7058 // Didn't fit in a register. Do it in pieces.
7059 int32_t low_v = Low32Bits(v);
7060 int32_t high_v = High32Bits(v);
7061 __ movl(addr_low, Immediate(low_v));
7062 MaybeRecordImplicitNullCheck(instruction);
7063 __ movl(addr_high, Immediate(high_v));
7064 }
7065}
7066
Roland Levillain4d027112015-07-01 15:41:14 +01007067#undef __
7068
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007069} // namespace x86_64
7070} // namespace art