blob: 2425a4c3cbbc9af9e6756d8b1000ee8984e21273 [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) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100202 __ shrl(length_loc.AsRegister<CpuRegister>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700203 }
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();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100335 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 DCHECK(instruction_->IsCheckCast()
337 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338
Roland Levillain0d5a2812015-11-13 10:07:31 +0000339 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000341
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000342 if (!is_fatal_) {
343 SaveLiveRegisters(codegen, locations);
344 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
346 // We're moving two locations to locations that could overlap, so we need a parallel
347 // move resolver.
348 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800349 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800350 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
351 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800352 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800353 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
354 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000355 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100356 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800357 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000358 } else {
359 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800360 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
361 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000362 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000363
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000364 if (!is_fatal_) {
365 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000366 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000367 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000368
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000369 RestoreLiveRegisters(codegen, locations);
370 __ jmp(GetExitLabel());
371 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000372 }
373
Alexandre Rames9931f312015-06-19 14:47:01 +0100374 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 bool IsFatal() const OVERRIDE { return is_fatal_; }
377
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000378 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000379 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000380
381 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
382};
383
Andreas Gampe85b62f22015-09-09 13:15:38 -0700384class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385 public:
Aart Bik42249c32016-01-07 15:33:50 -0800386 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000387 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700388
389 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000390 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100392 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000393 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 }
395
Alexandre Rames9931f312015-06-19 14:47:01 +0100396 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
397
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
400};
401
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100402class ArraySetSlowPathX86_64 : public SlowPathCode {
403 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000404 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100405
406 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
407 LocationSummary* locations = instruction_->GetLocations();
408 __ Bind(GetEntryLabel());
409 SaveLiveRegisters(codegen, locations);
410
411 InvokeRuntimeCallingConvention calling_convention;
412 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
413 parallel_move.AddMove(
414 locations->InAt(0),
415 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
416 Primitive::kPrimNot,
417 nullptr);
418 parallel_move.AddMove(
419 locations->InAt(1),
420 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
421 Primitive::kPrimInt,
422 nullptr);
423 parallel_move.AddMove(
424 locations->InAt(2),
425 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
426 Primitive::kPrimNot,
427 nullptr);
428 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
429
Roland Levillain0d5a2812015-11-13 10:07:31 +0000430 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100431 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000432 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100433 RestoreLiveRegisters(codegen, locations);
434 __ jmp(GetExitLabel());
435 }
436
437 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
438
439 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100440 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
441};
442
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100443// Slow path marking an object reference `ref` during a read
444// barrier. The field `obj.field` in the object `obj` holding this
445// reference does not get updated by this slow path after marking (see
446// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
447//
448// This means that after the execution of this slow path, `ref` will
449// always be up-to-date, but `obj.field` may not; i.e., after the
450// flip, `ref` will be a to-space reference, but `obj.field` will
451// probably still be a from-space reference (unless it gets updated by
452// another thread, or if another thread installed another object
453// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000454class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
455 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100456 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
457 Location ref,
458 bool unpoison_ref_before_marking)
459 : SlowPathCode(instruction),
460 ref_(ref),
461 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000462 DCHECK(kEmitCompilerReadBarrier);
463 }
464
465 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
466
467 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
468 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100469 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
470 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000471 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100472 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000473 DCHECK(instruction_->IsInstanceFieldGet() ||
474 instruction_->IsStaticFieldGet() ||
475 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100476 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000477 instruction_->IsLoadClass() ||
478 instruction_->IsLoadString() ||
479 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100480 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100481 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
482 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000483 << "Unexpected instruction in read barrier marking slow path: "
484 << instruction_->DebugName();
485
486 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100487 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000488 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100489 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000490 }
Roland Levillain4359e612016-07-20 11:32:19 +0100491 // No need to save live registers; it's taken care of by the
492 // entrypoint. Also, there is no need to update the stack mask,
493 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000494 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100495 DCHECK_NE(ref_reg, RSP);
496 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100497 // "Compact" slow path, saving two moves.
498 //
499 // Instead of using the standard runtime calling convention (input
500 // and output in R0):
501 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100502 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100503 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100504 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100505 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100507 // of a dedicated entrypoint:
508 //
509 // rX <- ReadBarrierMarkRegX(rX)
510 //
511 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100513 // This runtime call does not require a stack map.
514 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000515 __ jmp(GetExitLabel());
516 }
517
518 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100519 // The location (register) of the marked object reference.
520 const Location ref_;
521 // Should the reference in `ref_` be unpoisoned prior to marking it?
522 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000523
524 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
525};
526
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100527// Slow path marking an object reference `ref` during a read barrier,
528// and if needed, atomically updating the field `obj.field` in the
529// object `obj` holding this reference after marking (contrary to
530// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
531// `obj.field`).
532//
533// This means that after the execution of this slow path, both `ref`
534// and `obj.field` will be up-to-date; i.e., after the flip, both will
535// hold the same to-space reference (unless another thread installed
536// another object reference (different from `ref`) in `obj.field`).
537class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
538 public:
539 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
540 Location ref,
541 CpuRegister obj,
542 const Address& field_addr,
543 bool unpoison_ref_before_marking,
544 CpuRegister temp1,
545 CpuRegister temp2)
546 : SlowPathCode(instruction),
547 ref_(ref),
548 obj_(obj),
549 field_addr_(field_addr),
550 unpoison_ref_before_marking_(unpoison_ref_before_marking),
551 temp1_(temp1),
552 temp2_(temp2) {
553 DCHECK(kEmitCompilerReadBarrier);
554 }
555
556 const char* GetDescription() const OVERRIDE {
557 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
558 }
559
560 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
561 LocationSummary* locations = instruction_->GetLocations();
562 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
563 Register ref_reg = ref_cpu_reg.AsRegister();
564 DCHECK(locations->CanCall());
565 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
566 // This slow path is only used by the UnsafeCASObject intrinsic.
567 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
568 << "Unexpected instruction in read barrier marking and field updating slow path: "
569 << instruction_->DebugName();
570 DCHECK(instruction_->GetLocations()->Intrinsified());
571 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
572
573 __ Bind(GetEntryLabel());
574 if (unpoison_ref_before_marking_) {
575 // Object* ref = ref_addr->AsMirrorPtr()
576 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
577 }
578
579 // Save the old (unpoisoned) reference.
580 __ movl(temp1_, ref_cpu_reg);
581
582 // No need to save live registers; it's taken care of by the
583 // entrypoint. Also, there is no need to update the stack mask,
584 // as this runtime call will not trigger a garbage collection.
585 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
586 DCHECK_NE(ref_reg, RSP);
587 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
588 // "Compact" slow path, saving two moves.
589 //
590 // Instead of using the standard runtime calling convention (input
591 // and output in R0):
592 //
593 // RDI <- ref
594 // RAX <- ReadBarrierMark(RDI)
595 // ref <- RAX
596 //
597 // we just use rX (the register containing `ref`) as input and output
598 // of a dedicated entrypoint:
599 //
600 // rX <- ReadBarrierMarkRegX(rX)
601 //
602 int32_t entry_point_offset =
603 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
604 // This runtime call does not require a stack map.
605 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
606
607 // If the new reference is different from the old reference,
608 // update the field in the holder (`*field_addr`).
609 //
610 // Note that this field could also hold a different object, if
611 // another thread had concurrently changed it. In that case, the
612 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
613 // operation below would abort the CAS, leaving the field as-is.
614 NearLabel done;
615 __ cmpl(temp1_, ref_cpu_reg);
616 __ j(kEqual, &done);
617
618 // Update the the holder's field atomically. This may fail if
619 // mutator updates before us, but it's OK. This is achived
620 // using a strong compare-and-set (CAS) operation with relaxed
621 // memory synchronization ordering, where the expected value is
622 // the old reference and the desired value is the new reference.
623 // This operation is implemented with a 32-bit LOCK CMPXLCHG
624 // instruction, which requires the expected value (the old
625 // reference) to be in EAX. Save RAX beforehand, and move the
626 // expected value (stored in `temp1_`) into EAX.
627 __ movq(temp2_, CpuRegister(RAX));
628 __ movl(CpuRegister(RAX), temp1_);
629
630 // Convenience aliases.
631 CpuRegister base = obj_;
632 CpuRegister expected = CpuRegister(RAX);
633 CpuRegister value = ref_cpu_reg;
634
635 bool base_equals_value = (base.AsRegister() == value.AsRegister());
636 Register value_reg = ref_reg;
637 if (kPoisonHeapReferences) {
638 if (base_equals_value) {
639 // If `base` and `value` are the same register location, move
640 // `value_reg` to a temporary register. This way, poisoning
641 // `value_reg` won't invalidate `base`.
642 value_reg = temp1_.AsRegister();
643 __ movl(CpuRegister(value_reg), base);
644 }
645
646 // Check that the register allocator did not assign the location
647 // of `expected` (RAX) to `value` nor to `base`, so that heap
648 // poisoning (when enabled) works as intended below.
649 // - If `value` were equal to `expected`, both references would
650 // be poisoned twice, meaning they would not be poisoned at
651 // all, as heap poisoning uses address negation.
652 // - If `base` were equal to `expected`, poisoning `expected`
653 // would invalidate `base`.
654 DCHECK_NE(value_reg, expected.AsRegister());
655 DCHECK_NE(base.AsRegister(), expected.AsRegister());
656
657 __ PoisonHeapReference(expected);
658 __ PoisonHeapReference(CpuRegister(value_reg));
659 }
660
661 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
662
663 // If heap poisoning is enabled, we need to unpoison the values
664 // that were poisoned earlier.
665 if (kPoisonHeapReferences) {
666 if (base_equals_value) {
667 // `value_reg` has been moved to a temporary register, no need
668 // to unpoison it.
669 } else {
670 __ UnpoisonHeapReference(CpuRegister(value_reg));
671 }
672 // No need to unpoison `expected` (RAX), as it is be overwritten below.
673 }
674
675 // Restore RAX.
676 __ movq(CpuRegister(RAX), temp2_);
677
678 __ Bind(&done);
679 __ jmp(GetExitLabel());
680 }
681
682 private:
683 // The location (register) of the marked object reference.
684 const Location ref_;
685 // The register containing the object holding the marked object reference field.
686 const CpuRegister obj_;
687 // The address of the marked reference field. The base of this address must be `obj_`.
688 const Address field_addr_;
689
690 // Should the reference in `ref_` be unpoisoned prior to marking it?
691 const bool unpoison_ref_before_marking_;
692
693 const CpuRegister temp1_;
694 const CpuRegister temp2_;
695
696 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
697};
698
Roland Levillain0d5a2812015-11-13 10:07:31 +0000699// Slow path generating a read barrier for a heap reference.
700class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
701 public:
702 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
703 Location out,
704 Location ref,
705 Location obj,
706 uint32_t offset,
707 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000708 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000709 out_(out),
710 ref_(ref),
711 obj_(obj),
712 offset_(offset),
713 index_(index) {
714 DCHECK(kEmitCompilerReadBarrier);
715 // If `obj` is equal to `out` or `ref`, it means the initial
716 // object has been overwritten by (or after) the heap object
717 // reference load to be instrumented, e.g.:
718 //
719 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000720 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000721 //
722 // In that case, we have lost the information about the original
723 // object, and the emitted read barrier cannot work properly.
724 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
725 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
726}
727
728 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
729 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
730 LocationSummary* locations = instruction_->GetLocations();
731 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
732 DCHECK(locations->CanCall());
733 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100734 DCHECK(instruction_->IsInstanceFieldGet() ||
735 instruction_->IsStaticFieldGet() ||
736 instruction_->IsArrayGet() ||
737 instruction_->IsInstanceOf() ||
738 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100739 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000740 << "Unexpected instruction in read barrier for heap reference slow path: "
741 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000742
743 __ Bind(GetEntryLabel());
744 SaveLiveRegisters(codegen, locations);
745
746 // We may have to change the index's value, but as `index_` is a
747 // constant member (like other "inputs" of this slow path),
748 // introduce a copy of it, `index`.
749 Location index = index_;
750 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100751 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000752 if (instruction_->IsArrayGet()) {
753 // Compute real offset and store it in index_.
754 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
755 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
756 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
757 // We are about to change the value of `index_reg` (see the
758 // calls to art::x86_64::X86_64Assembler::shll and
759 // art::x86_64::X86_64Assembler::AddImmediate below), but it
760 // has not been saved by the previous call to
761 // art::SlowPathCode::SaveLiveRegisters, as it is a
762 // callee-save register --
763 // art::SlowPathCode::SaveLiveRegisters does not consider
764 // callee-save registers, as it has been designed with the
765 // assumption that callee-save registers are supposed to be
766 // handled by the called function. So, as a callee-save
767 // register, `index_reg` _would_ eventually be saved onto
768 // the stack, but it would be too late: we would have
769 // changed its value earlier. Therefore, we manually save
770 // it here into another freely available register,
771 // `free_reg`, chosen of course among the caller-save
772 // registers (as a callee-save `free_reg` register would
773 // exhibit the same problem).
774 //
775 // Note we could have requested a temporary register from
776 // the register allocator instead; but we prefer not to, as
777 // this is a slow path, and we know we can find a
778 // caller-save register that is available.
779 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
780 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
781 index_reg = free_reg;
782 index = Location::RegisterLocation(index_reg);
783 } else {
784 // The initial register stored in `index_` has already been
785 // saved in the call to art::SlowPathCode::SaveLiveRegisters
786 // (as it is not a callee-save register), so we can freely
787 // use it.
788 }
789 // Shifting the index value contained in `index_reg` by the
790 // scale factor (2) cannot overflow in practice, as the
791 // runtime is unable to allocate object arrays with a size
792 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
793 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
794 static_assert(
795 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
796 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
797 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
798 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100799 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
800 // intrinsics, `index_` is not shifted by a scale factor of 2
801 // (as in the case of ArrayGet), as it is actually an offset
802 // to an object field within an object.
803 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000804 DCHECK(instruction_->GetLocations()->Intrinsified());
805 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
806 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
807 << instruction_->AsInvoke()->GetIntrinsic();
808 DCHECK_EQ(offset_, 0U);
809 DCHECK(index_.IsRegister());
810 }
811 }
812
813 // We're moving two or three locations to locations that could
814 // overlap, so we need a parallel move resolver.
815 InvokeRuntimeCallingConvention calling_convention;
816 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
817 parallel_move.AddMove(ref_,
818 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
819 Primitive::kPrimNot,
820 nullptr);
821 parallel_move.AddMove(obj_,
822 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
823 Primitive::kPrimNot,
824 nullptr);
825 if (index.IsValid()) {
826 parallel_move.AddMove(index,
827 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
828 Primitive::kPrimInt,
829 nullptr);
830 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
831 } else {
832 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
833 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
834 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100835 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000836 instruction_,
837 instruction_->GetDexPc(),
838 this);
839 CheckEntrypointTypes<
840 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
841 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
842
843 RestoreLiveRegisters(codegen, locations);
844 __ jmp(GetExitLabel());
845 }
846
847 const char* GetDescription() const OVERRIDE {
848 return "ReadBarrierForHeapReferenceSlowPathX86_64";
849 }
850
851 private:
852 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
853 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
854 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
855 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
856 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
857 return static_cast<CpuRegister>(i);
858 }
859 }
860 // We shall never fail to find a free caller-save register, as
861 // there are more than two core caller-save registers on x86-64
862 // (meaning it is possible to find one which is different from
863 // `ref` and `obj`).
864 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
865 LOG(FATAL) << "Could not find a free caller-save register";
866 UNREACHABLE();
867 }
868
Roland Levillain0d5a2812015-11-13 10:07:31 +0000869 const Location out_;
870 const Location ref_;
871 const Location obj_;
872 const uint32_t offset_;
873 // An additional location containing an index to an array.
874 // Only used for HArrayGet and the UnsafeGetObject &
875 // UnsafeGetObjectVolatile intrinsics.
876 const Location index_;
877
878 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
879};
880
881// Slow path generating a read barrier for a GC root.
882class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
883 public:
884 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000885 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000886 DCHECK(kEmitCompilerReadBarrier);
887 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000888
889 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
890 LocationSummary* locations = instruction_->GetLocations();
891 DCHECK(locations->CanCall());
892 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000893 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
894 << "Unexpected instruction in read barrier for GC root slow path: "
895 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000896
897 __ Bind(GetEntryLabel());
898 SaveLiveRegisters(codegen, locations);
899
900 InvokeRuntimeCallingConvention calling_convention;
901 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
902 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100903 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000904 instruction_,
905 instruction_->GetDexPc(),
906 this);
907 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
908 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
909
910 RestoreLiveRegisters(codegen, locations);
911 __ jmp(GetExitLabel());
912 }
913
914 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
915
916 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000917 const Location out_;
918 const Location root_;
919
920 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
921};
922
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100923#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100924// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
925#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100926
Roland Levillain4fa13f62015-07-06 18:11:54 +0100927inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700928 switch (cond) {
929 case kCondEQ: return kEqual;
930 case kCondNE: return kNotEqual;
931 case kCondLT: return kLess;
932 case kCondLE: return kLessEqual;
933 case kCondGT: return kGreater;
934 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700935 case kCondB: return kBelow;
936 case kCondBE: return kBelowEqual;
937 case kCondA: return kAbove;
938 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700939 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100940 LOG(FATAL) << "Unreachable";
941 UNREACHABLE();
942}
943
Aart Bike9f37602015-10-09 11:15:55 -0700944// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100945inline Condition X86_64FPCondition(IfCondition cond) {
946 switch (cond) {
947 case kCondEQ: return kEqual;
948 case kCondNE: return kNotEqual;
949 case kCondLT: return kBelow;
950 case kCondLE: return kBelowEqual;
951 case kCondGT: return kAbove;
952 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700953 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100954 };
955 LOG(FATAL) << "Unreachable";
956 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700957}
958
Vladimir Markodc151b22015-10-15 18:02:30 +0100959HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
960 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100961 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100962 switch (desired_dispatch_info.code_ptr_location) {
963 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
964 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
965 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
966 return HInvokeStaticOrDirect::DispatchInfo {
967 desired_dispatch_info.method_load_kind,
968 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
969 desired_dispatch_info.method_load_data,
970 0u
971 };
972 default:
973 return desired_dispatch_info;
974 }
975}
976
Serguei Katkov288c7a82016-05-16 11:53:15 +0600977Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
978 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800979 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000980 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
981 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100982 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000983 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100984 uint32_t offset =
985 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
986 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000987 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100988 }
Vladimir Marko58155012015-08-19 12:49:41 +0000989 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000990 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000991 break;
992 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
993 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
994 break;
995 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
996 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000997 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
998 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +0000999 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
1000 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001001 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +00001002 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001003 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001004 // Bind a new fixup label at the end of the "movl" insn.
1005 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001006 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko58155012015-08-19 12:49:41 +00001007 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001008 }
Vladimir Marko58155012015-08-19 12:49:41 +00001009 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001010 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00001011 Register method_reg;
1012 CpuRegister reg = temp.AsRegister<CpuRegister>();
1013 if (current_method.IsRegister()) {
1014 method_reg = current_method.AsRegister<Register>();
1015 } else {
1016 DCHECK(invoke->GetLocations()->Intrinsified());
1017 DCHECK(!current_method.IsValid());
1018 method_reg = reg.AsRegister();
1019 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
1020 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001021 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01001022 __ movq(reg,
1023 Address(CpuRegister(method_reg),
1024 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01001025 // temp = temp[index_in_cache];
1026 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
1027 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00001028 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
1029 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01001030 }
Vladimir Marko58155012015-08-19 12:49:41 +00001031 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06001032 return callee_method;
1033}
1034
1035void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
1036 Location temp) {
1037 // All registers are assumed to be correctly set up.
1038 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00001039
1040 switch (invoke->GetCodePtrLocation()) {
1041 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1042 __ call(&frame_entry_label_);
1043 break;
1044 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001045 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
1046 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00001047 Label* label = &relative_call_patches_.back().label;
1048 __ call(label); // Bind to the patch label, override at link time.
1049 __ Bind(label); // Bind the label at the end of the "call" insn.
1050 break;
1051 }
1052 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
1053 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01001054 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
1055 LOG(FATAL) << "Unsupported";
1056 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00001057 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1058 // (callee_method + offset_of_quick_compiled_code)()
1059 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1060 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001061 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001062 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001063 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001064
1065 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001066}
1067
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001068void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
1069 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1070 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1071 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001072
1073 // Use the calling convention instead of the location of the receiver, as
1074 // intrinsics may have put the receiver in a different register. In the intrinsics
1075 // slow path, the arguments have been moved to the right place, so here we are
1076 // guaranteed that the receiver is the first register of the calling convention.
1077 InvokeDexCallingConvention calling_convention;
1078 Register receiver = calling_convention.GetRegisterAt(0);
1079
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001080 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001081 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001082 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001083 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001084 // Instead of simply (possibly) unpoisoning `temp` here, we should
1085 // emit a read barrier for the previous class reference load.
1086 // However this is not required in practice, as this is an
1087 // intermediate/temporary reference and because the current
1088 // concurrent copying collector keeps the from-space memory
1089 // intact/accessible until the end of the marking phase (the
1090 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001091 __ MaybeUnpoisonHeapReference(temp);
1092 // temp = temp->GetMethodAt(method_offset);
1093 __ movq(temp, Address(temp, method_offset));
1094 // call temp->GetEntryPoint();
1095 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001096 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001097}
1098
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001099void CodeGeneratorX86_64::RecordSimplePatch() {
1100 if (GetCompilerOptions().GetIncludePatchInformation()) {
1101 simple_patches_.emplace_back();
1102 __ Bind(&simple_patches_.back());
1103 }
1104}
1105
Vladimir Markoaad75c62016-10-03 08:46:48 +00001106void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
1107 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001108 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
1109 __ Bind(&string_patches_.back().label);
1110}
1111
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001112void CodeGeneratorX86_64::RecordTypePatch(HLoadClass* load_class) {
1113 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
1114 __ Bind(&type_patches_.back().label);
1115}
1116
Vladimir Markoaad75c62016-10-03 08:46:48 +00001117Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1118 DCHECK(!GetCompilerOptions().IsBootImage());
1119 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
1120 return &string_patches_.back().label;
1121}
1122
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001123Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
1124 uint32_t element_offset) {
1125 // Add a patch entry and return the label.
1126 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
1127 return &pc_relative_dex_cache_patches_.back().label;
1128}
1129
Vladimir Markoaad75c62016-10-03 08:46:48 +00001130// The label points to the end of the "movl" or another instruction but the literal offset
1131// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1132constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1133
1134template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1135inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1136 const ArenaDeque<PatchInfo<Label>>& infos,
1137 ArenaVector<LinkerPatch>* linker_patches) {
1138 for (const PatchInfo<Label>& info : infos) {
1139 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1140 linker_patches->push_back(
1141 Factory(literal_offset, &info.dex_file, info.label.Position(), info.index));
1142 }
1143}
1144
Vladimir Marko58155012015-08-19 12:49:41 +00001145void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1146 DCHECK(linker_patches->empty());
1147 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001148 method_patches_.size() +
1149 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001150 pc_relative_dex_cache_patches_.size() +
1151 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001152 string_patches_.size() +
1153 type_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001154 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001155 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001156 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001157 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00001158 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001159 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001160 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001161 linker_patches->push_back(
1162 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00001163 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001164 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1165 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001166 for (const Label& label : simple_patches_) {
1167 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1168 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1169 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001170 if (!GetCompilerOptions().IsBootImage()) {
1171 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
1172 } else {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001173 // These are always PC-relative, see GetSupportedLoadStringKind().
Vladimir Markoaad75c62016-10-03 08:46:48 +00001174 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001175 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001176 // These are always PC-relative, see GetSupportedLoadClassKind().
1177 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
Vladimir Marko58155012015-08-19 12:49:41 +00001178}
1179
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001180void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001181 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001182}
1183
1184void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001185 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001186}
1187
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001188size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1189 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1190 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001191}
1192
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001193size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1194 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1195 return kX86_64WordSize;
1196}
1197
1198size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1199 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1200 return kX86_64WordSize;
1201}
1202
1203size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1204 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1205 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001206}
1207
Calin Juravle175dc732015-08-25 15:42:32 +01001208void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1209 HInstruction* instruction,
1210 uint32_t dex_pc,
1211 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001212 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001213 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1214 if (EntrypointRequiresStackMap(entrypoint)) {
1215 RecordPcInfo(instruction, dex_pc, slow_path);
1216 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001217}
1218
Roland Levillaindec8f632016-07-22 17:10:06 +01001219void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1220 HInstruction* instruction,
1221 SlowPathCode* slow_path) {
1222 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001223 GenerateInvokeRuntime(entry_point_offset);
1224}
1225
1226void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001227 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1228}
1229
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001230static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001231// Use a fake return address register to mimic Quick.
1232static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001233CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001234 const X86_64InstructionSetFeatures& isa_features,
1235 const CompilerOptions& compiler_options,
1236 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001237 : CodeGenerator(graph,
1238 kNumberOfCpuRegisters,
1239 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001240 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001241 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1242 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001243 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001244 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1245 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001246 compiler_options,
1247 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001248 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001249 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001250 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001251 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001252 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001253 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001254 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +01001255 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1256 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001257 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001258 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1259 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001260 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00001261 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001262 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1263}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001265InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1266 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001267 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001268 assembler_(codegen->GetAssembler()),
1269 codegen_(codegen) {}
1270
David Brazdil58282f42016-01-14 12:45:10 +00001271void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001272 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001273 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001274
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001275 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001276 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001277}
1278
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001279static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001280 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001281}
David Srbecky9d8606d2015-04-12 09:35:32 +01001282
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001283static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001284 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001285}
1286
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001287void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001288 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001289 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001290 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001291 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001292 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001293
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001294 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001295 __ testq(CpuRegister(RAX), Address(
1296 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001297 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001298 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001299
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001300 if (HasEmptyFrame()) {
1301 return;
1302 }
1303
Nicolas Geoffray98893962015-01-21 12:32:32 +00001304 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001305 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001306 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001307 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001308 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1309 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001310 }
1311 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001312
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001313 int adjust = GetFrameSize() - GetCoreSpillSize();
1314 __ subq(CpuRegister(RSP), Immediate(adjust));
1315 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001316 uint32_t xmm_spill_location = GetFpuSpillStart();
1317 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001318
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001319 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1320 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001321 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1322 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1323 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001324 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001325 }
1326
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001327 // Save the current method if we need it. Note that we do not
1328 // do this in HCurrentMethod, as the instruction might have been removed
1329 // in the SSA graph.
1330 if (RequiresCurrentMethod()) {
1331 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1332 CpuRegister(kMethodRegisterArgument));
1333 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001334}
1335
1336void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001337 __ cfi().RememberState();
1338 if (!HasEmptyFrame()) {
1339 uint32_t xmm_spill_location = GetFpuSpillStart();
1340 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1341 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1342 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1343 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1344 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1345 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1346 }
1347 }
1348
1349 int adjust = GetFrameSize() - GetCoreSpillSize();
1350 __ addq(CpuRegister(RSP), Immediate(adjust));
1351 __ cfi().AdjustCFAOffset(-adjust);
1352
1353 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1354 Register reg = kCoreCalleeSaves[i];
1355 if (allocated_registers_.ContainsCoreRegister(reg)) {
1356 __ popq(CpuRegister(reg));
1357 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1358 __ cfi().Restore(DWARFReg(reg));
1359 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001360 }
1361 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001362 __ ret();
1363 __ cfi().RestoreState();
1364 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001365}
1366
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001367void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1368 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001369}
1370
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001371void CodeGeneratorX86_64::Move(Location destination, Location source) {
1372 if (source.Equals(destination)) {
1373 return;
1374 }
1375 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001376 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001377 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001378 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001379 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001380 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001381 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001382 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1383 } else if (source.IsConstant()) {
1384 HConstant* constant = source.GetConstant();
1385 if (constant->IsLongConstant()) {
1386 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1387 } else {
1388 Load32BitValue(dest, GetInt32ValueOf(constant));
1389 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001390 } else {
1391 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001392 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001393 }
1394 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001395 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001396 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001397 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001398 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001399 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1400 } else if (source.IsConstant()) {
1401 HConstant* constant = source.GetConstant();
1402 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1403 if (constant->IsFloatConstant()) {
1404 Load32BitValue(dest, static_cast<int32_t>(value));
1405 } else {
1406 Load64BitValue(dest, value);
1407 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001408 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001409 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001410 } else {
1411 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001412 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001413 }
1414 } else if (destination.IsStackSlot()) {
1415 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001416 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001417 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001418 } else if (source.IsFpuRegister()) {
1419 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001420 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001421 } else if (source.IsConstant()) {
1422 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001423 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001424 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001425 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001426 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001427 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1428 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001429 }
1430 } else {
1431 DCHECK(destination.IsDoubleStackSlot());
1432 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001433 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001434 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001435 } else if (source.IsFpuRegister()) {
1436 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001437 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001438 } else if (source.IsConstant()) {
1439 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001440 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1441 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001442 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001443 } else {
1444 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001445 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1446 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001447 }
1448 }
1449}
1450
Calin Juravle175dc732015-08-25 15:42:32 +01001451void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1452 DCHECK(location.IsRegister());
1453 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1454}
1455
Calin Juravlee460d1d2015-09-29 04:52:17 +01001456void CodeGeneratorX86_64::MoveLocation(
1457 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1458 Move(dst, src);
1459}
1460
1461void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1462 if (location.IsRegister()) {
1463 locations->AddTemp(location);
1464 } else {
1465 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1466 }
1467}
1468
David Brazdilfc6a86a2015-06-26 10:33:45 +00001469void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001470 DCHECK(!successor->IsExitBlock());
1471
1472 HBasicBlock* block = got->GetBlock();
1473 HInstruction* previous = got->GetPrevious();
1474
1475 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001476 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001477 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1478 return;
1479 }
1480
1481 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1482 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1483 }
1484 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001485 __ jmp(codegen_->GetLabelOf(successor));
1486 }
1487}
1488
David Brazdilfc6a86a2015-06-26 10:33:45 +00001489void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1490 got->SetLocations(nullptr);
1491}
1492
1493void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1494 HandleGoto(got, got->GetSuccessor());
1495}
1496
1497void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1498 try_boundary->SetLocations(nullptr);
1499}
1500
1501void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1502 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1503 if (!successor->IsExitBlock()) {
1504 HandleGoto(try_boundary, successor);
1505 }
1506}
1507
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001508void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1509 exit->SetLocations(nullptr);
1510}
1511
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001512void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001513}
1514
Mark Mendell152408f2015-12-31 12:28:50 -05001515template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001516void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001517 LabelType* true_label,
1518 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001519 if (cond->IsFPConditionTrueIfNaN()) {
1520 __ j(kUnordered, true_label);
1521 } else if (cond->IsFPConditionFalseIfNaN()) {
1522 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001523 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001524 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001525}
1526
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001527void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001528 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001529
Mark Mendellc4701932015-04-10 13:18:51 -04001530 Location left = locations->InAt(0);
1531 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001532 Primitive::Type type = condition->InputAt(0)->GetType();
1533 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001534 case Primitive::kPrimBoolean:
1535 case Primitive::kPrimByte:
1536 case Primitive::kPrimChar:
1537 case Primitive::kPrimShort:
1538 case Primitive::kPrimInt:
1539 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001540 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001541 break;
1542 }
Mark Mendellc4701932015-04-10 13:18:51 -04001543 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001544 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001545 break;
1546 }
1547 case Primitive::kPrimFloat: {
1548 if (right.IsFpuRegister()) {
1549 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1550 } else if (right.IsConstant()) {
1551 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1552 codegen_->LiteralFloatAddress(
1553 right.GetConstant()->AsFloatConstant()->GetValue()));
1554 } else {
1555 DCHECK(right.IsStackSlot());
1556 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1557 Address(CpuRegister(RSP), right.GetStackIndex()));
1558 }
Mark Mendellc4701932015-04-10 13:18:51 -04001559 break;
1560 }
1561 case Primitive::kPrimDouble: {
1562 if (right.IsFpuRegister()) {
1563 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1564 } else if (right.IsConstant()) {
1565 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1566 codegen_->LiteralDoubleAddress(
1567 right.GetConstant()->AsDoubleConstant()->GetValue()));
1568 } else {
1569 DCHECK(right.IsDoubleStackSlot());
1570 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1571 Address(CpuRegister(RSP), right.GetStackIndex()));
1572 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001573 break;
1574 }
1575 default:
1576 LOG(FATAL) << "Unexpected condition type " << type;
1577 }
1578}
1579
1580template<class LabelType>
1581void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1582 LabelType* true_target_in,
1583 LabelType* false_target_in) {
1584 // Generated branching requires both targets to be explicit. If either of the
1585 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1586 LabelType fallthrough_target;
1587 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1588 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1589
1590 // Generate the comparison to set the CC.
1591 GenerateCompareTest(condition);
1592
1593 // Now generate the correct jump(s).
1594 Primitive::Type type = condition->InputAt(0)->GetType();
1595 switch (type) {
1596 case Primitive::kPrimLong: {
1597 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1598 break;
1599 }
1600 case Primitive::kPrimFloat: {
1601 GenerateFPJumps(condition, true_target, false_target);
1602 break;
1603 }
1604 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001605 GenerateFPJumps(condition, true_target, false_target);
1606 break;
1607 }
1608 default:
1609 LOG(FATAL) << "Unexpected condition type " << type;
1610 }
1611
David Brazdil0debae72015-11-12 18:37:00 +00001612 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001613 __ jmp(false_target);
1614 }
David Brazdil0debae72015-11-12 18:37:00 +00001615
1616 if (fallthrough_target.IsLinked()) {
1617 __ Bind(&fallthrough_target);
1618 }
Mark Mendellc4701932015-04-10 13:18:51 -04001619}
1620
David Brazdil0debae72015-11-12 18:37:00 +00001621static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1622 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1623 // are set only strictly before `branch`. We can't use the eflags on long
1624 // conditions if they are materialized due to the complex branching.
1625 return cond->IsCondition() &&
1626 cond->GetNext() == branch &&
1627 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1628}
1629
Mark Mendell152408f2015-12-31 12:28:50 -05001630template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001631void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001632 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001633 LabelType* true_target,
1634 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001635 HInstruction* cond = instruction->InputAt(condition_input_index);
1636
1637 if (true_target == nullptr && false_target == nullptr) {
1638 // Nothing to do. The code always falls through.
1639 return;
1640 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001641 // Constant condition, statically compared against "true" (integer value 1).
1642 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001643 if (true_target != nullptr) {
1644 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001645 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001646 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001647 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001648 if (false_target != nullptr) {
1649 __ jmp(false_target);
1650 }
1651 }
1652 return;
1653 }
1654
1655 // The following code generates these patterns:
1656 // (1) true_target == nullptr && false_target != nullptr
1657 // - opposite condition true => branch to false_target
1658 // (2) true_target != nullptr && false_target == nullptr
1659 // - condition true => branch to true_target
1660 // (3) true_target != nullptr && false_target != nullptr
1661 // - condition true => branch to true_target
1662 // - branch to false_target
1663 if (IsBooleanValueOrMaterializedCondition(cond)) {
1664 if (AreEflagsSetFrom(cond, instruction)) {
1665 if (true_target == nullptr) {
1666 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1667 } else {
1668 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1669 }
1670 } else {
1671 // Materialized condition, compare against 0.
1672 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1673 if (lhs.IsRegister()) {
1674 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1675 } else {
1676 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1677 }
1678 if (true_target == nullptr) {
1679 __ j(kEqual, false_target);
1680 } else {
1681 __ j(kNotEqual, true_target);
1682 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001683 }
1684 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001685 // Condition has not been materialized, use its inputs as the
1686 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001687 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001688
David Brazdil0debae72015-11-12 18:37:00 +00001689 // If this is a long or FP comparison that has been folded into
1690 // the HCondition, generate the comparison directly.
1691 Primitive::Type type = condition->InputAt(0)->GetType();
1692 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1693 GenerateCompareTestAndBranch(condition, true_target, false_target);
1694 return;
1695 }
1696
1697 Location lhs = condition->GetLocations()->InAt(0);
1698 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001699 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001700 if (true_target == nullptr) {
1701 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1702 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001703 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001704 }
Dave Allison20dfc792014-06-16 20:44:29 -07001705 }
David Brazdil0debae72015-11-12 18:37:00 +00001706
1707 // If neither branch falls through (case 3), the conditional branch to `true_target`
1708 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1709 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001710 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001711 }
1712}
1713
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001714void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001715 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1716 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001717 locations->SetInAt(0, Location::Any());
1718 }
1719}
1720
1721void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001722 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1723 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1724 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1725 nullptr : codegen_->GetLabelOf(true_successor);
1726 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1727 nullptr : codegen_->GetLabelOf(false_successor);
1728 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001729}
1730
1731void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1732 LocationSummary* locations = new (GetGraph()->GetArena())
1733 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001734 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001735 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001736 locations->SetInAt(0, Location::Any());
1737 }
1738}
1739
1740void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001741 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001742 GenerateTestAndBranch<Label>(deoptimize,
1743 /* condition_input_index */ 0,
1744 slow_path->GetEntryLabel(),
1745 /* false_target */ nullptr);
1746}
1747
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001748static bool SelectCanUseCMOV(HSelect* select) {
1749 // There are no conditional move instructions for XMMs.
1750 if (Primitive::IsFloatingPointType(select->GetType())) {
1751 return false;
1752 }
1753
1754 // A FP condition doesn't generate the single CC that we need.
1755 HInstruction* condition = select->GetCondition();
1756 if (condition->IsCondition() &&
1757 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1758 return false;
1759 }
1760
1761 // We can generate a CMOV for this Select.
1762 return true;
1763}
1764
David Brazdil74eb1b22015-12-14 11:44:01 +00001765void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1766 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1767 if (Primitive::IsFloatingPointType(select->GetType())) {
1768 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001769 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001770 } else {
1771 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001772 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001773 if (select->InputAt(1)->IsConstant()) {
1774 locations->SetInAt(1, Location::RequiresRegister());
1775 } else {
1776 locations->SetInAt(1, Location::Any());
1777 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001778 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001779 locations->SetInAt(1, Location::Any());
1780 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001781 }
1782 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1783 locations->SetInAt(2, Location::RequiresRegister());
1784 }
1785 locations->SetOut(Location::SameAsFirstInput());
1786}
1787
1788void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1789 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001790 if (SelectCanUseCMOV(select)) {
1791 // If both the condition and the source types are integer, we can generate
1792 // a CMOV to implement Select.
1793 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001794 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001795 DCHECK(locations->InAt(0).Equals(locations->Out()));
1796
1797 HInstruction* select_condition = select->GetCondition();
1798 Condition cond = kNotEqual;
1799
1800 // Figure out how to test the 'condition'.
1801 if (select_condition->IsCondition()) {
1802 HCondition* condition = select_condition->AsCondition();
1803 if (!condition->IsEmittedAtUseSite()) {
1804 // This was a previously materialized condition.
1805 // Can we use the existing condition code?
1806 if (AreEflagsSetFrom(condition, select)) {
1807 // Materialization was the previous instruction. Condition codes are right.
1808 cond = X86_64IntegerCondition(condition->GetCondition());
1809 } else {
1810 // No, we have to recreate the condition code.
1811 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1812 __ testl(cond_reg, cond_reg);
1813 }
1814 } else {
1815 GenerateCompareTest(condition);
1816 cond = X86_64IntegerCondition(condition->GetCondition());
1817 }
1818 } else {
1819 // Must be a boolean condition, which needs to be compared to 0.
1820 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1821 __ testl(cond_reg, cond_reg);
1822 }
1823
1824 // If the condition is true, overwrite the output, which already contains false.
1825 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001826 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1827 if (value_true_loc.IsRegister()) {
1828 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1829 } else {
1830 __ cmov(cond,
1831 value_false,
1832 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1833 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001834 } else {
1835 NearLabel false_target;
1836 GenerateTestAndBranch<NearLabel>(select,
1837 /* condition_input_index */ 2,
1838 /* true_target */ nullptr,
1839 &false_target);
1840 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1841 __ Bind(&false_target);
1842 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001843}
1844
David Srbecky0cf44932015-12-09 14:09:59 +00001845void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1846 new (GetGraph()->GetArena()) LocationSummary(info);
1847}
1848
David Srbeckyd28f4a02016-03-14 17:14:24 +00001849void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1850 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001851}
1852
1853void CodeGeneratorX86_64::GenerateNop() {
1854 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001855}
1856
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001857void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001858 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001859 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001860 // Handle the long/FP comparisons made in instruction simplification.
1861 switch (cond->InputAt(0)->GetType()) {
1862 case Primitive::kPrimLong:
1863 locations->SetInAt(0, Location::RequiresRegister());
1864 locations->SetInAt(1, Location::Any());
1865 break;
1866 case Primitive::kPrimFloat:
1867 case Primitive::kPrimDouble:
1868 locations->SetInAt(0, Location::RequiresFpuRegister());
1869 locations->SetInAt(1, Location::Any());
1870 break;
1871 default:
1872 locations->SetInAt(0, Location::RequiresRegister());
1873 locations->SetInAt(1, Location::Any());
1874 break;
1875 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001876 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001877 locations->SetOut(Location::RequiresRegister());
1878 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001879}
1880
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001881void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001882 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001883 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001884 }
Mark Mendellc4701932015-04-10 13:18:51 -04001885
1886 LocationSummary* locations = cond->GetLocations();
1887 Location lhs = locations->InAt(0);
1888 Location rhs = locations->InAt(1);
1889 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001890 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001891
1892 switch (cond->InputAt(0)->GetType()) {
1893 default:
1894 // Integer case.
1895
1896 // Clear output register: setcc only sets the low byte.
1897 __ xorl(reg, reg);
1898
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001899 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001900 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001901 return;
1902 case Primitive::kPrimLong:
1903 // Clear output register: setcc only sets the low byte.
1904 __ xorl(reg, reg);
1905
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001906 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001907 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001908 return;
1909 case Primitive::kPrimFloat: {
1910 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1911 if (rhs.IsConstant()) {
1912 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1913 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1914 } else if (rhs.IsStackSlot()) {
1915 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1916 } else {
1917 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1918 }
1919 GenerateFPJumps(cond, &true_label, &false_label);
1920 break;
1921 }
1922 case Primitive::kPrimDouble: {
1923 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1924 if (rhs.IsConstant()) {
1925 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1926 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1927 } else if (rhs.IsDoubleStackSlot()) {
1928 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1929 } else {
1930 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1931 }
1932 GenerateFPJumps(cond, &true_label, &false_label);
1933 break;
1934 }
1935 }
1936
1937 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001938 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001939
Roland Levillain4fa13f62015-07-06 18:11:54 +01001940 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001941 __ Bind(&false_label);
1942 __ xorl(reg, reg);
1943 __ jmp(&done_label);
1944
Roland Levillain4fa13f62015-07-06 18:11:54 +01001945 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001946 __ Bind(&true_label);
1947 __ movl(reg, Immediate(1));
1948 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001949}
1950
1951void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001952 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001953}
1954
1955void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001956 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001965}
1966
1967void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001985}
1986
1987void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001989}
1990
1991void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001993}
1994
1995void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001997}
1998
Aart Bike9f37602015-10-09 11:15:55 -07001999void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002001}
2002
2003void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002005}
2006
2007void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002009}
2010
2011void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002013}
2014
2015void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002017}
2018
2019void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002020 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002021}
2022
2023void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002024 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002025}
2026
2027void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002028 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002029}
2030
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002031void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002032 LocationSummary* locations =
2033 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002034 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002035 case Primitive::kPrimBoolean:
2036 case Primitive::kPrimByte:
2037 case Primitive::kPrimShort:
2038 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002039 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00002040 case Primitive::kPrimLong: {
2041 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002042 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002043 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2044 break;
2045 }
2046 case Primitive::kPrimFloat:
2047 case Primitive::kPrimDouble: {
2048 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002049 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002050 locations->SetOut(Location::RequiresRegister());
2051 break;
2052 }
2053 default:
2054 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2055 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002056}
2057
2058void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002059 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002060 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002061 Location left = locations->InAt(0);
2062 Location right = locations->InAt(1);
2063
Mark Mendell0c9497d2015-08-21 09:30:05 -04002064 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00002065 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002066 Condition less_cond = kLess;
2067
Calin Juravleddb7df22014-11-25 20:56:51 +00002068 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002069 case Primitive::kPrimBoolean:
2070 case Primitive::kPrimByte:
2071 case Primitive::kPrimShort:
2072 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002073 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002074 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002075 break;
2076 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002077 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002078 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002079 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002080 }
2081 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04002082 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2083 if (right.IsConstant()) {
2084 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2085 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2086 } else if (right.IsStackSlot()) {
2087 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2088 } else {
2089 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2090 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002091 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002092 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002093 break;
2094 }
2095 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04002096 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2097 if (right.IsConstant()) {
2098 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2099 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2100 } else if (right.IsDoubleStackSlot()) {
2101 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2102 } else {
2103 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2104 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002105 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002106 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002107 break;
2108 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002109 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002110 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002111 }
Aart Bika19616e2016-02-01 18:57:58 -08002112
Calin Juravleddb7df22014-11-25 20:56:51 +00002113 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002114 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002115 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002116
Calin Juravle91debbc2014-11-26 19:01:09 +00002117 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002118 __ movl(out, Immediate(1));
2119 __ jmp(&done);
2120
2121 __ Bind(&less);
2122 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002123
2124 __ Bind(&done);
2125}
2126
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002127void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002128 LocationSummary* locations =
2129 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002130 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002131}
2132
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002133void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002134 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002135}
2136
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002137void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2138 LocationSummary* locations =
2139 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2140 locations->SetOut(Location::ConstantLocation(constant));
2141}
2142
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002143void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002144 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002145}
2146
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002147void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002148 LocationSummary* locations =
2149 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002150 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002151}
2152
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002153void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002154 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002155}
2156
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002157void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2158 LocationSummary* locations =
2159 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2160 locations->SetOut(Location::ConstantLocation(constant));
2161}
2162
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002163void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002164 // Will be generated at use site.
2165}
2166
2167void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2168 LocationSummary* locations =
2169 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2170 locations->SetOut(Location::ConstantLocation(constant));
2171}
2172
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002173void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2174 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002175 // Will be generated at use site.
2176}
2177
Calin Juravle27df7582015-04-17 19:12:31 +01002178void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2179 memory_barrier->SetLocations(nullptr);
2180}
2181
2182void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002183 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002184}
2185
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002186void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2187 ret->SetLocations(nullptr);
2188}
2189
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002190void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002191 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002192}
2193
2194void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002195 LocationSummary* locations =
2196 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002197 switch (ret->InputAt(0)->GetType()) {
2198 case Primitive::kPrimBoolean:
2199 case Primitive::kPrimByte:
2200 case Primitive::kPrimChar:
2201 case Primitive::kPrimShort:
2202 case Primitive::kPrimInt:
2203 case Primitive::kPrimNot:
2204 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002205 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002206 break;
2207
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002208 case Primitive::kPrimFloat:
2209 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002210 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002211 break;
2212
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002213 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002214 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002215 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002216}
2217
2218void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2219 if (kIsDebugBuild) {
2220 switch (ret->InputAt(0)->GetType()) {
2221 case Primitive::kPrimBoolean:
2222 case Primitive::kPrimByte:
2223 case Primitive::kPrimChar:
2224 case Primitive::kPrimShort:
2225 case Primitive::kPrimInt:
2226 case Primitive::kPrimNot:
2227 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002228 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002229 break;
2230
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002231 case Primitive::kPrimFloat:
2232 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002233 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002234 XMM0);
2235 break;
2236
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002237 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002238 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002239 }
2240 }
2241 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002242}
2243
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002244Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2245 switch (type) {
2246 case Primitive::kPrimBoolean:
2247 case Primitive::kPrimByte:
2248 case Primitive::kPrimChar:
2249 case Primitive::kPrimShort:
2250 case Primitive::kPrimInt:
2251 case Primitive::kPrimNot:
2252 case Primitive::kPrimLong:
2253 return Location::RegisterLocation(RAX);
2254
2255 case Primitive::kPrimVoid:
2256 return Location::NoLocation();
2257
2258 case Primitive::kPrimDouble:
2259 case Primitive::kPrimFloat:
2260 return Location::FpuRegisterLocation(XMM0);
2261 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002262
2263 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002264}
2265
2266Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2267 return Location::RegisterLocation(kMethodRegisterArgument);
2268}
2269
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002270Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002271 switch (type) {
2272 case Primitive::kPrimBoolean:
2273 case Primitive::kPrimByte:
2274 case Primitive::kPrimChar:
2275 case Primitive::kPrimShort:
2276 case Primitive::kPrimInt:
2277 case Primitive::kPrimNot: {
2278 uint32_t index = gp_index_++;
2279 stack_index_++;
2280 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002281 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002282 } else {
2283 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2284 }
2285 }
2286
2287 case Primitive::kPrimLong: {
2288 uint32_t index = gp_index_;
2289 stack_index_ += 2;
2290 if (index < calling_convention.GetNumberOfRegisters()) {
2291 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002292 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002293 } else {
2294 gp_index_ += 2;
2295 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2296 }
2297 }
2298
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002299 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002300 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002301 stack_index_++;
2302 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002303 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002304 } else {
2305 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2306 }
2307 }
2308
2309 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002310 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002311 stack_index_ += 2;
2312 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002313 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002314 } else {
2315 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2316 }
2317 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002318
2319 case Primitive::kPrimVoid:
2320 LOG(FATAL) << "Unexpected parameter type " << type;
2321 break;
2322 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002323 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002324}
2325
Calin Juravle175dc732015-08-25 15:42:32 +01002326void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2327 // The trampoline uses the same calling convention as dex calling conventions,
2328 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2329 // the method_idx.
2330 HandleInvoke(invoke);
2331}
2332
2333void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2334 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2335}
2336
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002337void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002338 // Explicit clinit checks triggered by static invokes must have been pruned by
2339 // art::PrepareForRegisterAllocation.
2340 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002341
Mark Mendellfb8d2792015-03-31 22:16:59 -04002342 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002343 if (intrinsic.TryDispatch(invoke)) {
2344 return;
2345 }
2346
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002347 HandleInvoke(invoke);
2348}
2349
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002350static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2351 if (invoke->GetLocations()->Intrinsified()) {
2352 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2353 intrinsic.Dispatch(invoke);
2354 return true;
2355 }
2356 return false;
2357}
2358
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002359void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002360 // Explicit clinit checks triggered by static invokes must have been pruned by
2361 // art::PrepareForRegisterAllocation.
2362 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002363
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002364 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2365 return;
2366 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002367
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002368 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002369 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002370 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002371 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002372}
2373
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002374void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002375 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002376 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002377}
2378
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002379void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002380 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002381 if (intrinsic.TryDispatch(invoke)) {
2382 return;
2383 }
2384
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002385 HandleInvoke(invoke);
2386}
2387
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002388void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002389 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2390 return;
2391 }
2392
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002393 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002394 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002395 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002396}
2397
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002398void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2399 HandleInvoke(invoke);
2400 // Add the hidden argument.
2401 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2402}
2403
2404void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2405 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002406 LocationSummary* locations = invoke->GetLocations();
2407 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2408 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002409 Location receiver = locations->InAt(0);
2410 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2411
Roland Levillain0d5a2812015-11-13 10:07:31 +00002412 // Set the hidden argument. This is safe to do this here, as RAX
2413 // won't be modified thereafter, before the `call` instruction.
2414 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002415 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002416
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002417 if (receiver.IsStackSlot()) {
2418 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002419 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002420 __ movl(temp, Address(temp, class_offset));
2421 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002422 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002423 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002424 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002425 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002426 // Instead of simply (possibly) unpoisoning `temp` here, we should
2427 // emit a read barrier for the previous class reference load.
2428 // However this is not required in practice, as this is an
2429 // intermediate/temporary reference and because the current
2430 // concurrent copying collector keeps the from-space memory
2431 // intact/accessible until the end of the marking phase (the
2432 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002433 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002434 // temp = temp->GetAddressOfIMT()
2435 __ movq(temp,
2436 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2437 // temp = temp->GetImtEntryAt(method_offset);
2438 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002439 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002440 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002441 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002442 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002443 __ call(Address(
2444 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002445
2446 DCHECK(!codegen_->IsLeafMethod());
2447 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2448}
2449
Roland Levillain88cb1752014-10-20 16:36:47 +01002450void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2451 LocationSummary* locations =
2452 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2453 switch (neg->GetResultType()) {
2454 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002455 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002456 locations->SetInAt(0, Location::RequiresRegister());
2457 locations->SetOut(Location::SameAsFirstInput());
2458 break;
2459
Roland Levillain88cb1752014-10-20 16:36:47 +01002460 case Primitive::kPrimFloat:
2461 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002462 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002463 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002464 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002465 break;
2466
2467 default:
2468 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2469 }
2470}
2471
2472void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2473 LocationSummary* locations = neg->GetLocations();
2474 Location out = locations->Out();
2475 Location in = locations->InAt(0);
2476 switch (neg->GetResultType()) {
2477 case Primitive::kPrimInt:
2478 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002479 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002480 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002481 break;
2482
2483 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002484 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002485 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002486 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002487 break;
2488
Roland Levillain5368c212014-11-27 15:03:41 +00002489 case Primitive::kPrimFloat: {
2490 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002491 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002492 // Implement float negation with an exclusive or with value
2493 // 0x80000000 (mask for bit 31, representing the sign of a
2494 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002495 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002497 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002498 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002499
Roland Levillain5368c212014-11-27 15:03:41 +00002500 case Primitive::kPrimDouble: {
2501 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002502 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002503 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002504 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002505 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002506 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002507 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002508 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002509 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002510
2511 default:
2512 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2513 }
2514}
2515
Roland Levillaindff1f282014-11-05 14:15:05 +00002516void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2517 LocationSummary* locations =
2518 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2519 Primitive::Type result_type = conversion->GetResultType();
2520 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002521 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002522
David Brazdilb2bd1c52015-03-25 11:17:37 +00002523 // The Java language does not allow treating boolean as an integral type but
2524 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002525
Roland Levillaindff1f282014-11-05 14:15:05 +00002526 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002527 case Primitive::kPrimByte:
2528 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002529 case Primitive::kPrimLong:
2530 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002531 case Primitive::kPrimBoolean:
2532 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002533 case Primitive::kPrimShort:
2534 case Primitive::kPrimInt:
2535 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002536 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002537 locations->SetInAt(0, Location::Any());
2538 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2539 break;
2540
2541 default:
2542 LOG(FATAL) << "Unexpected type conversion from " << input_type
2543 << " to " << result_type;
2544 }
2545 break;
2546
Roland Levillain01a8d712014-11-14 16:27:39 +00002547 case Primitive::kPrimShort:
2548 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002549 case Primitive::kPrimLong:
2550 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002551 case Primitive::kPrimBoolean:
2552 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002553 case Primitive::kPrimByte:
2554 case Primitive::kPrimInt:
2555 case Primitive::kPrimChar:
2556 // Processing a Dex `int-to-short' instruction.
2557 locations->SetInAt(0, Location::Any());
2558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2559 break;
2560
2561 default:
2562 LOG(FATAL) << "Unexpected type conversion from " << input_type
2563 << " to " << result_type;
2564 }
2565 break;
2566
Roland Levillain946e1432014-11-11 17:35:19 +00002567 case Primitive::kPrimInt:
2568 switch (input_type) {
2569 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002570 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002571 locations->SetInAt(0, Location::Any());
2572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2573 break;
2574
2575 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002576 // Processing a Dex `float-to-int' instruction.
2577 locations->SetInAt(0, Location::RequiresFpuRegister());
2578 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002579 break;
2580
Roland Levillain946e1432014-11-11 17:35:19 +00002581 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002582 // Processing a Dex `double-to-int' instruction.
2583 locations->SetInAt(0, Location::RequiresFpuRegister());
2584 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002585 break;
2586
2587 default:
2588 LOG(FATAL) << "Unexpected type conversion from " << input_type
2589 << " to " << result_type;
2590 }
2591 break;
2592
Roland Levillaindff1f282014-11-05 14:15:05 +00002593 case Primitive::kPrimLong:
2594 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002595 case Primitive::kPrimBoolean:
2596 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002597 case Primitive::kPrimByte:
2598 case Primitive::kPrimShort:
2599 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002600 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002601 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002602 // TODO: We would benefit from a (to-be-implemented)
2603 // Location::RegisterOrStackSlot requirement for this input.
2604 locations->SetInAt(0, Location::RequiresRegister());
2605 locations->SetOut(Location::RequiresRegister());
2606 break;
2607
2608 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002609 // Processing a Dex `float-to-long' instruction.
2610 locations->SetInAt(0, Location::RequiresFpuRegister());
2611 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002612 break;
2613
Roland Levillaindff1f282014-11-05 14:15:05 +00002614 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002615 // Processing a Dex `double-to-long' instruction.
2616 locations->SetInAt(0, Location::RequiresFpuRegister());
2617 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002618 break;
2619
2620 default:
2621 LOG(FATAL) << "Unexpected type conversion from " << input_type
2622 << " to " << result_type;
2623 }
2624 break;
2625
Roland Levillain981e4542014-11-14 11:47:14 +00002626 case Primitive::kPrimChar:
2627 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002628 case Primitive::kPrimLong:
2629 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002630 case Primitive::kPrimBoolean:
2631 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002632 case Primitive::kPrimByte:
2633 case Primitive::kPrimShort:
2634 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002635 // Processing a Dex `int-to-char' instruction.
2636 locations->SetInAt(0, Location::Any());
2637 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2638 break;
2639
2640 default:
2641 LOG(FATAL) << "Unexpected type conversion from " << input_type
2642 << " to " << result_type;
2643 }
2644 break;
2645
Roland Levillaindff1f282014-11-05 14:15:05 +00002646 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002647 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002648 case Primitive::kPrimBoolean:
2649 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002650 case Primitive::kPrimByte:
2651 case Primitive::kPrimShort:
2652 case Primitive::kPrimInt:
2653 case Primitive::kPrimChar:
2654 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002655 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002656 locations->SetOut(Location::RequiresFpuRegister());
2657 break;
2658
2659 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002660 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002661 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002662 locations->SetOut(Location::RequiresFpuRegister());
2663 break;
2664
Roland Levillaincff13742014-11-17 14:32:17 +00002665 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002666 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002667 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002668 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002669 break;
2670
2671 default:
2672 LOG(FATAL) << "Unexpected type conversion from " << input_type
2673 << " to " << result_type;
2674 };
2675 break;
2676
Roland Levillaindff1f282014-11-05 14:15:05 +00002677 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002678 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002679 case Primitive::kPrimBoolean:
2680 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002681 case Primitive::kPrimByte:
2682 case Primitive::kPrimShort:
2683 case Primitive::kPrimInt:
2684 case Primitive::kPrimChar:
2685 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002686 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002687 locations->SetOut(Location::RequiresFpuRegister());
2688 break;
2689
2690 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002691 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002692 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002693 locations->SetOut(Location::RequiresFpuRegister());
2694 break;
2695
Roland Levillaincff13742014-11-17 14:32:17 +00002696 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002697 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002698 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002699 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002700 break;
2701
2702 default:
2703 LOG(FATAL) << "Unexpected type conversion from " << input_type
2704 << " to " << result_type;
2705 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002706 break;
2707
2708 default:
2709 LOG(FATAL) << "Unexpected type conversion from " << input_type
2710 << " to " << result_type;
2711 }
2712}
2713
2714void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2715 LocationSummary* locations = conversion->GetLocations();
2716 Location out = locations->Out();
2717 Location in = locations->InAt(0);
2718 Primitive::Type result_type = conversion->GetResultType();
2719 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002720 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002721 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002722 case Primitive::kPrimByte:
2723 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002724 case Primitive::kPrimLong:
2725 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002726 case Primitive::kPrimBoolean:
2727 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002728 case Primitive::kPrimShort:
2729 case Primitive::kPrimInt:
2730 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002731 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002732 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002733 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002734 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002735 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002736 Address(CpuRegister(RSP), in.GetStackIndex()));
2737 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002738 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002739 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002740 }
2741 break;
2742
2743 default:
2744 LOG(FATAL) << "Unexpected type conversion from " << input_type
2745 << " to " << result_type;
2746 }
2747 break;
2748
Roland Levillain01a8d712014-11-14 16:27:39 +00002749 case Primitive::kPrimShort:
2750 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002751 case Primitive::kPrimLong:
2752 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002753 case Primitive::kPrimBoolean:
2754 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002755 case Primitive::kPrimByte:
2756 case Primitive::kPrimInt:
2757 case Primitive::kPrimChar:
2758 // Processing a Dex `int-to-short' instruction.
2759 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002760 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002761 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002762 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002763 Address(CpuRegister(RSP), in.GetStackIndex()));
2764 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002765 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002766 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002767 }
2768 break;
2769
2770 default:
2771 LOG(FATAL) << "Unexpected type conversion from " << input_type
2772 << " to " << result_type;
2773 }
2774 break;
2775
Roland Levillain946e1432014-11-11 17:35:19 +00002776 case Primitive::kPrimInt:
2777 switch (input_type) {
2778 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002779 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002780 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002781 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002782 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002783 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002784 Address(CpuRegister(RSP), in.GetStackIndex()));
2785 } else {
2786 DCHECK(in.IsConstant());
2787 DCHECK(in.GetConstant()->IsLongConstant());
2788 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002790 }
2791 break;
2792
Roland Levillain3f8f9362014-12-02 17:45:01 +00002793 case Primitive::kPrimFloat: {
2794 // Processing a Dex `float-to-int' instruction.
2795 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2796 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002797 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002798
2799 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002800 // if input >= (float)INT_MAX goto done
2801 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002802 __ j(kAboveEqual, &done);
2803 // if input == NaN goto nan
2804 __ j(kUnordered, &nan);
2805 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002806 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002807 __ jmp(&done);
2808 __ Bind(&nan);
2809 // output = 0
2810 __ xorl(output, output);
2811 __ Bind(&done);
2812 break;
2813 }
2814
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002815 case Primitive::kPrimDouble: {
2816 // Processing a Dex `double-to-int' instruction.
2817 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2818 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002819 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002820
2821 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002822 // if input >= (double)INT_MAX goto done
2823 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002824 __ j(kAboveEqual, &done);
2825 // if input == NaN goto nan
2826 __ j(kUnordered, &nan);
2827 // output = double-to-int-truncate(input)
2828 __ cvttsd2si(output, input);
2829 __ jmp(&done);
2830 __ Bind(&nan);
2831 // output = 0
2832 __ xorl(output, output);
2833 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002834 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002835 }
Roland Levillain946e1432014-11-11 17:35:19 +00002836
2837 default:
2838 LOG(FATAL) << "Unexpected type conversion from " << input_type
2839 << " to " << result_type;
2840 }
2841 break;
2842
Roland Levillaindff1f282014-11-05 14:15:05 +00002843 case Primitive::kPrimLong:
2844 switch (input_type) {
2845 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002846 case Primitive::kPrimBoolean:
2847 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002848 case Primitive::kPrimByte:
2849 case Primitive::kPrimShort:
2850 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002851 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002852 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002853 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002854 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002855 break;
2856
Roland Levillain624279f2014-12-04 11:54:28 +00002857 case Primitive::kPrimFloat: {
2858 // Processing a Dex `float-to-long' instruction.
2859 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2860 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002861 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002862
Mark Mendell92e83bf2015-05-07 11:25:03 -04002863 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002864 // if input >= (float)LONG_MAX goto done
2865 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002866 __ j(kAboveEqual, &done);
2867 // if input == NaN goto nan
2868 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002869 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002870 __ cvttss2si(output, input, true);
2871 __ jmp(&done);
2872 __ Bind(&nan);
2873 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002874 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002875 __ Bind(&done);
2876 break;
2877 }
2878
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002879 case Primitive::kPrimDouble: {
2880 // Processing a Dex `double-to-long' instruction.
2881 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2882 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002883 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002884
Mark Mendell92e83bf2015-05-07 11:25:03 -04002885 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002886 // if input >= (double)LONG_MAX goto done
2887 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002888 __ j(kAboveEqual, &done);
2889 // if input == NaN goto nan
2890 __ j(kUnordered, &nan);
2891 // output = double-to-long-truncate(input)
2892 __ cvttsd2si(output, input, true);
2893 __ jmp(&done);
2894 __ Bind(&nan);
2895 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002896 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002897 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002898 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002899 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002900
2901 default:
2902 LOG(FATAL) << "Unexpected type conversion from " << input_type
2903 << " to " << result_type;
2904 }
2905 break;
2906
Roland Levillain981e4542014-11-14 11:47:14 +00002907 case Primitive::kPrimChar:
2908 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002909 case Primitive::kPrimLong:
2910 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002911 case Primitive::kPrimBoolean:
2912 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002913 case Primitive::kPrimByte:
2914 case Primitive::kPrimShort:
2915 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002916 // Processing a Dex `int-to-char' instruction.
2917 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002918 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002919 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002920 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002921 Address(CpuRegister(RSP), in.GetStackIndex()));
2922 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002923 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002924 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002925 }
2926 break;
2927
2928 default:
2929 LOG(FATAL) << "Unexpected type conversion from " << input_type
2930 << " to " << result_type;
2931 }
2932 break;
2933
Roland Levillaindff1f282014-11-05 14:15:05 +00002934 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002935 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002936 case Primitive::kPrimBoolean:
2937 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002938 case Primitive::kPrimByte:
2939 case Primitive::kPrimShort:
2940 case Primitive::kPrimInt:
2941 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002942 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002943 if (in.IsRegister()) {
2944 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2945 } else if (in.IsConstant()) {
2946 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2947 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002948 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002949 } else {
2950 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2951 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2952 }
Roland Levillaincff13742014-11-17 14:32:17 +00002953 break;
2954
2955 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002956 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002957 if (in.IsRegister()) {
2958 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2959 } else if (in.IsConstant()) {
2960 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2961 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002962 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002963 } else {
2964 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2965 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2966 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002967 break;
2968
Roland Levillaincff13742014-11-17 14:32:17 +00002969 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002970 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002971 if (in.IsFpuRegister()) {
2972 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2973 } else if (in.IsConstant()) {
2974 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2975 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002976 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002977 } else {
2978 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2979 Address(CpuRegister(RSP), in.GetStackIndex()));
2980 }
Roland Levillaincff13742014-11-17 14:32:17 +00002981 break;
2982
2983 default:
2984 LOG(FATAL) << "Unexpected type conversion from " << input_type
2985 << " to " << result_type;
2986 };
2987 break;
2988
Roland Levillaindff1f282014-11-05 14:15:05 +00002989 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002990 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002991 case Primitive::kPrimBoolean:
2992 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002993 case Primitive::kPrimByte:
2994 case Primitive::kPrimShort:
2995 case Primitive::kPrimInt:
2996 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002997 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002998 if (in.IsRegister()) {
2999 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3000 } else if (in.IsConstant()) {
3001 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3002 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003003 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003004 } else {
3005 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3006 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3007 }
Roland Levillaincff13742014-11-17 14:32:17 +00003008 break;
3009
3010 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003011 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003012 if (in.IsRegister()) {
3013 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3014 } else if (in.IsConstant()) {
3015 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3016 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003017 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003018 } else {
3019 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3020 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3021 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003022 break;
3023
Roland Levillaincff13742014-11-17 14:32:17 +00003024 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003025 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003026 if (in.IsFpuRegister()) {
3027 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3028 } else if (in.IsConstant()) {
3029 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3030 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003031 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003032 } else {
3033 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3034 Address(CpuRegister(RSP), in.GetStackIndex()));
3035 }
Roland Levillaincff13742014-11-17 14:32:17 +00003036 break;
3037
3038 default:
3039 LOG(FATAL) << "Unexpected type conversion from " << input_type
3040 << " to " << result_type;
3041 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003042 break;
3043
3044 default:
3045 LOG(FATAL) << "Unexpected type conversion from " << input_type
3046 << " to " << result_type;
3047 }
3048}
3049
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003050void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003051 LocationSummary* locations =
3052 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003053 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003054 case Primitive::kPrimInt: {
3055 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003056 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003058 break;
3059 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003060
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003061 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003062 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003063 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003064 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003066 break;
3067 }
3068
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003069 case Primitive::kPrimDouble:
3070 case Primitive::kPrimFloat: {
3071 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003072 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003073 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003074 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003075 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003076
3077 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003078 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003079 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003080}
3081
3082void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3083 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003084 Location first = locations->InAt(0);
3085 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003086 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003087
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003088 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003089 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003090 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003091 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3092 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003093 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3094 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003095 } else {
3096 __ leal(out.AsRegister<CpuRegister>(), Address(
3097 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3098 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003099 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003100 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3101 __ addl(out.AsRegister<CpuRegister>(),
3102 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3103 } else {
3104 __ leal(out.AsRegister<CpuRegister>(), Address(
3105 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3106 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003107 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003108 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003109 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003110 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003111 break;
3112 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003113
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003114 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003115 if (second.IsRegister()) {
3116 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3117 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003118 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3119 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003120 } else {
3121 __ leaq(out.AsRegister<CpuRegister>(), Address(
3122 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3123 }
3124 } else {
3125 DCHECK(second.IsConstant());
3126 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3127 int32_t int32_value = Low32Bits(value);
3128 DCHECK_EQ(int32_value, value);
3129 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3130 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3131 } else {
3132 __ leaq(out.AsRegister<CpuRegister>(), Address(
3133 first.AsRegister<CpuRegister>(), int32_value));
3134 }
3135 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003136 break;
3137 }
3138
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003139 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003140 if (second.IsFpuRegister()) {
3141 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3142 } else if (second.IsConstant()) {
3143 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003144 codegen_->LiteralFloatAddress(
3145 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003146 } else {
3147 DCHECK(second.IsStackSlot());
3148 __ addss(first.AsFpuRegister<XmmRegister>(),
3149 Address(CpuRegister(RSP), second.GetStackIndex()));
3150 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003151 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003152 }
3153
3154 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003155 if (second.IsFpuRegister()) {
3156 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3157 } else if (second.IsConstant()) {
3158 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003159 codegen_->LiteralDoubleAddress(
3160 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003161 } else {
3162 DCHECK(second.IsDoubleStackSlot());
3163 __ addsd(first.AsFpuRegister<XmmRegister>(),
3164 Address(CpuRegister(RSP), second.GetStackIndex()));
3165 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003166 break;
3167 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003168
3169 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003170 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003171 }
3172}
3173
3174void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003175 LocationSummary* locations =
3176 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003177 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003178 case Primitive::kPrimInt: {
3179 locations->SetInAt(0, Location::RequiresRegister());
3180 locations->SetInAt(1, Location::Any());
3181 locations->SetOut(Location::SameAsFirstInput());
3182 break;
3183 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003184 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003185 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003186 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003187 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003188 break;
3189 }
Calin Juravle11351682014-10-23 15:38:15 +01003190 case Primitive::kPrimFloat:
3191 case Primitive::kPrimDouble: {
3192 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003193 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003194 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003195 break;
Calin Juravle11351682014-10-23 15:38:15 +01003196 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003197 default:
Calin Juravle11351682014-10-23 15:38:15 +01003198 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003199 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003200}
3201
3202void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3203 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003204 Location first = locations->InAt(0);
3205 Location second = locations->InAt(1);
3206 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003207 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003208 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003209 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003210 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003211 } else if (second.IsConstant()) {
3212 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003213 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003214 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003215 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003216 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003217 break;
3218 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003219 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003220 if (second.IsConstant()) {
3221 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3222 DCHECK(IsInt<32>(value));
3223 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3224 } else {
3225 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3226 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003227 break;
3228 }
3229
Calin Juravle11351682014-10-23 15:38:15 +01003230 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003231 if (second.IsFpuRegister()) {
3232 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3233 } else if (second.IsConstant()) {
3234 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003235 codegen_->LiteralFloatAddress(
3236 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003237 } else {
3238 DCHECK(second.IsStackSlot());
3239 __ subss(first.AsFpuRegister<XmmRegister>(),
3240 Address(CpuRegister(RSP), second.GetStackIndex()));
3241 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003242 break;
Calin Juravle11351682014-10-23 15:38:15 +01003243 }
3244
3245 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003246 if (second.IsFpuRegister()) {
3247 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3248 } else if (second.IsConstant()) {
3249 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003250 codegen_->LiteralDoubleAddress(
3251 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003252 } else {
3253 DCHECK(second.IsDoubleStackSlot());
3254 __ subsd(first.AsFpuRegister<XmmRegister>(),
3255 Address(CpuRegister(RSP), second.GetStackIndex()));
3256 }
Calin Juravle11351682014-10-23 15:38:15 +01003257 break;
3258 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003259
3260 default:
Calin Juravle11351682014-10-23 15:38:15 +01003261 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003262 }
3263}
3264
Calin Juravle34bacdf2014-10-07 20:23:36 +01003265void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3266 LocationSummary* locations =
3267 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3268 switch (mul->GetResultType()) {
3269 case Primitive::kPrimInt: {
3270 locations->SetInAt(0, Location::RequiresRegister());
3271 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003272 if (mul->InputAt(1)->IsIntConstant()) {
3273 // Can use 3 operand multiply.
3274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3275 } else {
3276 locations->SetOut(Location::SameAsFirstInput());
3277 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003278 break;
3279 }
3280 case Primitive::kPrimLong: {
3281 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003282 locations->SetInAt(1, Location::Any());
3283 if (mul->InputAt(1)->IsLongConstant() &&
3284 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003285 // Can use 3 operand multiply.
3286 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3287 } else {
3288 locations->SetOut(Location::SameAsFirstInput());
3289 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003290 break;
3291 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003292 case Primitive::kPrimFloat:
3293 case Primitive::kPrimDouble: {
3294 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003295 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003296 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003297 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003298 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003299
3300 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003301 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003302 }
3303}
3304
3305void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3306 LocationSummary* locations = mul->GetLocations();
3307 Location first = locations->InAt(0);
3308 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003309 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003310 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003311 case Primitive::kPrimInt:
3312 // The constant may have ended up in a register, so test explicitly to avoid
3313 // problems where the output may not be the same as the first operand.
3314 if (mul->InputAt(1)->IsIntConstant()) {
3315 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3316 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3317 } else if (second.IsRegister()) {
3318 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003319 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003320 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003321 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003322 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003323 __ imull(first.AsRegister<CpuRegister>(),
3324 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003325 }
3326 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003327 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003328 // The constant may have ended up in a register, so test explicitly to avoid
3329 // problems where the output may not be the same as the first operand.
3330 if (mul->InputAt(1)->IsLongConstant()) {
3331 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3332 if (IsInt<32>(value)) {
3333 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3334 Immediate(static_cast<int32_t>(value)));
3335 } else {
3336 // Have to use the constant area.
3337 DCHECK(first.Equals(out));
3338 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3339 }
3340 } else if (second.IsRegister()) {
3341 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003342 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003343 } else {
3344 DCHECK(second.IsDoubleStackSlot());
3345 DCHECK(first.Equals(out));
3346 __ imulq(first.AsRegister<CpuRegister>(),
3347 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003348 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003349 break;
3350 }
3351
Calin Juravleb5bfa962014-10-21 18:02:24 +01003352 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003353 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003354 if (second.IsFpuRegister()) {
3355 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3356 } else if (second.IsConstant()) {
3357 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003358 codegen_->LiteralFloatAddress(
3359 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003360 } else {
3361 DCHECK(second.IsStackSlot());
3362 __ mulss(first.AsFpuRegister<XmmRegister>(),
3363 Address(CpuRegister(RSP), second.GetStackIndex()));
3364 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003365 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003366 }
3367
3368 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003369 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003370 if (second.IsFpuRegister()) {
3371 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3372 } else if (second.IsConstant()) {
3373 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003374 codegen_->LiteralDoubleAddress(
3375 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003376 } else {
3377 DCHECK(second.IsDoubleStackSlot());
3378 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3379 Address(CpuRegister(RSP), second.GetStackIndex()));
3380 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003381 break;
3382 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003383
3384 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003385 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003386 }
3387}
3388
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003389void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3390 uint32_t stack_adjustment, bool is_float) {
3391 if (source.IsStackSlot()) {
3392 DCHECK(is_float);
3393 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3394 } else if (source.IsDoubleStackSlot()) {
3395 DCHECK(!is_float);
3396 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3397 } else {
3398 // Write the value to the temporary location on the stack and load to FP stack.
3399 if (is_float) {
3400 Location stack_temp = Location::StackSlot(temp_offset);
3401 codegen_->Move(stack_temp, source);
3402 __ flds(Address(CpuRegister(RSP), temp_offset));
3403 } else {
3404 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3405 codegen_->Move(stack_temp, source);
3406 __ fldl(Address(CpuRegister(RSP), temp_offset));
3407 }
3408 }
3409}
3410
3411void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3412 Primitive::Type type = rem->GetResultType();
3413 bool is_float = type == Primitive::kPrimFloat;
3414 size_t elem_size = Primitive::ComponentSize(type);
3415 LocationSummary* locations = rem->GetLocations();
3416 Location first = locations->InAt(0);
3417 Location second = locations->InAt(1);
3418 Location out = locations->Out();
3419
3420 // Create stack space for 2 elements.
3421 // TODO: enhance register allocator to ask for stack temporaries.
3422 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3423
3424 // Load the values to the FP stack in reverse order, using temporaries if needed.
3425 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3426 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3427
3428 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003429 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003430 __ Bind(&retry);
3431 __ fprem();
3432
3433 // Move FP status to AX.
3434 __ fstsw();
3435
3436 // And see if the argument reduction is complete. This is signaled by the
3437 // C2 FPU flag bit set to 0.
3438 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3439 __ j(kNotEqual, &retry);
3440
3441 // We have settled on the final value. Retrieve it into an XMM register.
3442 // Store FP top of stack to real stack.
3443 if (is_float) {
3444 __ fsts(Address(CpuRegister(RSP), 0));
3445 } else {
3446 __ fstl(Address(CpuRegister(RSP), 0));
3447 }
3448
3449 // Pop the 2 items from the FP stack.
3450 __ fucompp();
3451
3452 // Load the value from the stack into an XMM register.
3453 DCHECK(out.IsFpuRegister()) << out;
3454 if (is_float) {
3455 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3456 } else {
3457 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3458 }
3459
3460 // And remove the temporary stack space we allocated.
3461 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3462}
3463
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003464void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3465 DCHECK(instruction->IsDiv() || instruction->IsRem());
3466
3467 LocationSummary* locations = instruction->GetLocations();
3468 Location second = locations->InAt(1);
3469 DCHECK(second.IsConstant());
3470
3471 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3472 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003473 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474
3475 DCHECK(imm == 1 || imm == -1);
3476
3477 switch (instruction->GetResultType()) {
3478 case Primitive::kPrimInt: {
3479 if (instruction->IsRem()) {
3480 __ xorl(output_register, output_register);
3481 } else {
3482 __ movl(output_register, input_register);
3483 if (imm == -1) {
3484 __ negl(output_register);
3485 }
3486 }
3487 break;
3488 }
3489
3490 case Primitive::kPrimLong: {
3491 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003492 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003493 } else {
3494 __ movq(output_register, input_register);
3495 if (imm == -1) {
3496 __ negq(output_register);
3497 }
3498 }
3499 break;
3500 }
3501
3502 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003503 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003504 }
3505}
3506
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003507void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003508 LocationSummary* locations = instruction->GetLocations();
3509 Location second = locations->InAt(1);
3510
3511 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3512 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3513
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003514 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003515 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3516 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003517
3518 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3519
3520 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003521 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003522 __ testl(numerator, numerator);
3523 __ cmov(kGreaterEqual, tmp, numerator);
3524 int shift = CTZ(imm);
3525 __ sarl(tmp, Immediate(shift));
3526
3527 if (imm < 0) {
3528 __ negl(tmp);
3529 }
3530
3531 __ movl(output_register, tmp);
3532 } else {
3533 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3534 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3535
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003536 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003537 __ addq(rdx, numerator);
3538 __ testq(numerator, numerator);
3539 __ cmov(kGreaterEqual, rdx, numerator);
3540 int shift = CTZ(imm);
3541 __ sarq(rdx, Immediate(shift));
3542
3543 if (imm < 0) {
3544 __ negq(rdx);
3545 }
3546
3547 __ movq(output_register, rdx);
3548 }
3549}
3550
3551void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3552 DCHECK(instruction->IsDiv() || instruction->IsRem());
3553
3554 LocationSummary* locations = instruction->GetLocations();
3555 Location second = locations->InAt(1);
3556
3557 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3558 : locations->GetTemp(0).AsRegister<CpuRegister>();
3559 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3560 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3561 : locations->Out().AsRegister<CpuRegister>();
3562 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3563
3564 DCHECK_EQ(RAX, eax.AsRegister());
3565 DCHECK_EQ(RDX, edx.AsRegister());
3566 if (instruction->IsDiv()) {
3567 DCHECK_EQ(RAX, out.AsRegister());
3568 } else {
3569 DCHECK_EQ(RDX, out.AsRegister());
3570 }
3571
3572 int64_t magic;
3573 int shift;
3574
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003575 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003576 if (instruction->GetResultType() == Primitive::kPrimInt) {
3577 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3578
3579 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3580
3581 __ movl(numerator, eax);
3582
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003583 __ movl(eax, Immediate(magic));
3584 __ imull(numerator);
3585
3586 if (imm > 0 && magic < 0) {
3587 __ addl(edx, numerator);
3588 } else if (imm < 0 && magic > 0) {
3589 __ subl(edx, numerator);
3590 }
3591
3592 if (shift != 0) {
3593 __ sarl(edx, Immediate(shift));
3594 }
3595
3596 __ movl(eax, edx);
3597 __ shrl(edx, Immediate(31));
3598 __ addl(edx, eax);
3599
3600 if (instruction->IsRem()) {
3601 __ movl(eax, numerator);
3602 __ imull(edx, Immediate(imm));
3603 __ subl(eax, edx);
3604 __ movl(edx, eax);
3605 } else {
3606 __ movl(eax, edx);
3607 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003608 } else {
3609 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3610
3611 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3612
3613 CpuRegister rax = eax;
3614 CpuRegister rdx = edx;
3615
3616 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3617
3618 // Save the numerator.
3619 __ movq(numerator, rax);
3620
3621 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003622 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003623
3624 // RDX:RAX = magic * numerator
3625 __ imulq(numerator);
3626
3627 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003628 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003629 __ addq(rdx, numerator);
3630 } else if (imm < 0 && magic > 0) {
3631 // RDX -= numerator
3632 __ subq(rdx, numerator);
3633 }
3634
3635 // Shift if needed.
3636 if (shift != 0) {
3637 __ sarq(rdx, Immediate(shift));
3638 }
3639
3640 // RDX += 1 if RDX < 0
3641 __ movq(rax, rdx);
3642 __ shrq(rdx, Immediate(63));
3643 __ addq(rdx, rax);
3644
3645 if (instruction->IsRem()) {
3646 __ movq(rax, numerator);
3647
3648 if (IsInt<32>(imm)) {
3649 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3650 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003651 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003652 }
3653
3654 __ subq(rax, rdx);
3655 __ movq(rdx, rax);
3656 } else {
3657 __ movq(rax, rdx);
3658 }
3659 }
3660}
3661
Calin Juravlebacfec32014-11-14 15:54:36 +00003662void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3663 DCHECK(instruction->IsDiv() || instruction->IsRem());
3664 Primitive::Type type = instruction->GetResultType();
3665 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3666
3667 bool is_div = instruction->IsDiv();
3668 LocationSummary* locations = instruction->GetLocations();
3669
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003670 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3671 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003672
Roland Levillain271ab9c2014-11-27 15:23:57 +00003673 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003674 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003675
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003676 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003677 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003678
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003679 if (imm == 0) {
3680 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3681 } else if (imm == 1 || imm == -1) {
3682 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003683 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003684 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003685 } else {
3686 DCHECK(imm <= -2 || imm >= 2);
3687 GenerateDivRemWithAnyConstant(instruction);
3688 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003689 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003690 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003691 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003692 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003693 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003694
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003695 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3696 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3697 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3698 // so it's safe to just use negl instead of more complex comparisons.
3699 if (type == Primitive::kPrimInt) {
3700 __ cmpl(second_reg, Immediate(-1));
3701 __ j(kEqual, slow_path->GetEntryLabel());
3702 // edx:eax <- sign-extended of eax
3703 __ cdq();
3704 // eax = quotient, edx = remainder
3705 __ idivl(second_reg);
3706 } else {
3707 __ cmpq(second_reg, Immediate(-1));
3708 __ j(kEqual, slow_path->GetEntryLabel());
3709 // rdx:rax <- sign-extended of rax
3710 __ cqo();
3711 // rax = quotient, rdx = remainder
3712 __ idivq(second_reg);
3713 }
3714 __ Bind(slow_path->GetExitLabel());
3715 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003716}
3717
Calin Juravle7c4954d2014-10-28 16:57:40 +00003718void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3719 LocationSummary* locations =
3720 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3721 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003722 case Primitive::kPrimInt:
3723 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003724 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003725 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003726 locations->SetOut(Location::SameAsFirstInput());
3727 // Intel uses edx:eax as the dividend.
3728 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003729 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3730 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3731 // output and request another temp.
3732 if (div->InputAt(1)->IsConstant()) {
3733 locations->AddTemp(Location::RequiresRegister());
3734 }
Calin Juravled0d48522014-11-04 16:40:20 +00003735 break;
3736 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003737
Calin Juravle7c4954d2014-10-28 16:57:40 +00003738 case Primitive::kPrimFloat:
3739 case Primitive::kPrimDouble: {
3740 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003741 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003742 locations->SetOut(Location::SameAsFirstInput());
3743 break;
3744 }
3745
3746 default:
3747 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3748 }
3749}
3750
3751void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3752 LocationSummary* locations = div->GetLocations();
3753 Location first = locations->InAt(0);
3754 Location second = locations->InAt(1);
3755 DCHECK(first.Equals(locations->Out()));
3756
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003757 Primitive::Type type = div->GetResultType();
3758 switch (type) {
3759 case Primitive::kPrimInt:
3760 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003761 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003762 break;
3763 }
3764
Calin Juravle7c4954d2014-10-28 16:57:40 +00003765 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003766 if (second.IsFpuRegister()) {
3767 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3768 } else if (second.IsConstant()) {
3769 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003770 codegen_->LiteralFloatAddress(
3771 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003772 } else {
3773 DCHECK(second.IsStackSlot());
3774 __ divss(first.AsFpuRegister<XmmRegister>(),
3775 Address(CpuRegister(RSP), second.GetStackIndex()));
3776 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003777 break;
3778 }
3779
3780 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003781 if (second.IsFpuRegister()) {
3782 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3783 } else if (second.IsConstant()) {
3784 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003785 codegen_->LiteralDoubleAddress(
3786 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003787 } else {
3788 DCHECK(second.IsDoubleStackSlot());
3789 __ divsd(first.AsFpuRegister<XmmRegister>(),
3790 Address(CpuRegister(RSP), second.GetStackIndex()));
3791 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003792 break;
3793 }
3794
3795 default:
3796 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3797 }
3798}
3799
Calin Juravlebacfec32014-11-14 15:54:36 +00003800void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003801 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003802 LocationSummary* locations =
3803 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003804
3805 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003806 case Primitive::kPrimInt:
3807 case Primitive::kPrimLong: {
3808 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003809 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003810 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3811 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003812 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3813 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3814 // output and request another temp.
3815 if (rem->InputAt(1)->IsConstant()) {
3816 locations->AddTemp(Location::RequiresRegister());
3817 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003818 break;
3819 }
3820
3821 case Primitive::kPrimFloat:
3822 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003823 locations->SetInAt(0, Location::Any());
3824 locations->SetInAt(1, Location::Any());
3825 locations->SetOut(Location::RequiresFpuRegister());
3826 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003827 break;
3828 }
3829
3830 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003831 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003832 }
3833}
3834
3835void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3836 Primitive::Type type = rem->GetResultType();
3837 switch (type) {
3838 case Primitive::kPrimInt:
3839 case Primitive::kPrimLong: {
3840 GenerateDivRemIntegral(rem);
3841 break;
3842 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003843 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003844 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003845 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003846 break;
3847 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003848 default:
3849 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3850 }
3851}
3852
Calin Juravled0d48522014-11-04 16:40:20 +00003853void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003854 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003855 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003856}
3857
3858void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003859 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003860 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3861 codegen_->AddSlowPath(slow_path);
3862
3863 LocationSummary* locations = instruction->GetLocations();
3864 Location value = locations->InAt(0);
3865
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003866 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003867 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003868 case Primitive::kPrimByte:
3869 case Primitive::kPrimChar:
3870 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003871 case Primitive::kPrimInt: {
3872 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003873 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003874 __ j(kEqual, slow_path->GetEntryLabel());
3875 } else if (value.IsStackSlot()) {
3876 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3877 __ j(kEqual, slow_path->GetEntryLabel());
3878 } else {
3879 DCHECK(value.IsConstant()) << value;
3880 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003881 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003882 }
3883 }
3884 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003885 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003886 case Primitive::kPrimLong: {
3887 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003888 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003889 __ j(kEqual, slow_path->GetEntryLabel());
3890 } else if (value.IsDoubleStackSlot()) {
3891 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3892 __ j(kEqual, slow_path->GetEntryLabel());
3893 } else {
3894 DCHECK(value.IsConstant()) << value;
3895 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003896 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003897 }
3898 }
3899 break;
3900 }
3901 default:
3902 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003903 }
Calin Juravled0d48522014-11-04 16:40:20 +00003904}
3905
Calin Juravle9aec02f2014-11-18 23:06:35 +00003906void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3907 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3908
3909 LocationSummary* locations =
3910 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3911
3912 switch (op->GetResultType()) {
3913 case Primitive::kPrimInt:
3914 case Primitive::kPrimLong: {
3915 locations->SetInAt(0, Location::RequiresRegister());
3916 // The shift count needs to be in CL.
3917 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3918 locations->SetOut(Location::SameAsFirstInput());
3919 break;
3920 }
3921 default:
3922 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3923 }
3924}
3925
3926void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3927 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3928
3929 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003930 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003931 Location second = locations->InAt(1);
3932
3933 switch (op->GetResultType()) {
3934 case Primitive::kPrimInt: {
3935 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003936 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003937 if (op->IsShl()) {
3938 __ shll(first_reg, second_reg);
3939 } else if (op->IsShr()) {
3940 __ sarl(first_reg, second_reg);
3941 } else {
3942 __ shrl(first_reg, second_reg);
3943 }
3944 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003945 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003946 if (op->IsShl()) {
3947 __ shll(first_reg, imm);
3948 } else if (op->IsShr()) {
3949 __ sarl(first_reg, imm);
3950 } else {
3951 __ shrl(first_reg, imm);
3952 }
3953 }
3954 break;
3955 }
3956 case Primitive::kPrimLong: {
3957 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003958 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003959 if (op->IsShl()) {
3960 __ shlq(first_reg, second_reg);
3961 } else if (op->IsShr()) {
3962 __ sarq(first_reg, second_reg);
3963 } else {
3964 __ shrq(first_reg, second_reg);
3965 }
3966 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003967 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003968 if (op->IsShl()) {
3969 __ shlq(first_reg, imm);
3970 } else if (op->IsShr()) {
3971 __ sarq(first_reg, imm);
3972 } else {
3973 __ shrq(first_reg, imm);
3974 }
3975 }
3976 break;
3977 }
3978 default:
3979 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003980 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003981 }
3982}
3983
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003984void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3985 LocationSummary* locations =
3986 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3987
3988 switch (ror->GetResultType()) {
3989 case Primitive::kPrimInt:
3990 case Primitive::kPrimLong: {
3991 locations->SetInAt(0, Location::RequiresRegister());
3992 // The shift count needs to be in CL (unless it is a constant).
3993 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3994 locations->SetOut(Location::SameAsFirstInput());
3995 break;
3996 }
3997 default:
3998 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3999 UNREACHABLE();
4000 }
4001}
4002
4003void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4004 LocationSummary* locations = ror->GetLocations();
4005 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4006 Location second = locations->InAt(1);
4007
4008 switch (ror->GetResultType()) {
4009 case Primitive::kPrimInt:
4010 if (second.IsRegister()) {
4011 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4012 __ rorl(first_reg, second_reg);
4013 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004014 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004015 __ rorl(first_reg, imm);
4016 }
4017 break;
4018 case Primitive::kPrimLong:
4019 if (second.IsRegister()) {
4020 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4021 __ rorq(first_reg, second_reg);
4022 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004023 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004024 __ rorq(first_reg, imm);
4025 }
4026 break;
4027 default:
4028 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4029 UNREACHABLE();
4030 }
4031}
4032
Calin Juravle9aec02f2014-11-18 23:06:35 +00004033void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4034 HandleShift(shl);
4035}
4036
4037void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4038 HandleShift(shl);
4039}
4040
4041void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4042 HandleShift(shr);
4043}
4044
4045void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4046 HandleShift(shr);
4047}
4048
4049void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4050 HandleShift(ushr);
4051}
4052
4053void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4054 HandleShift(ushr);
4055}
4056
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004057void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004058 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004059 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004060 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004061 if (instruction->IsStringAlloc()) {
4062 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4063 } else {
4064 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4065 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4066 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004067 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004068}
4069
4070void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004071 // Note: if heap poisoning is enabled, the entry point takes cares
4072 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004073 if (instruction->IsStringAlloc()) {
4074 // String is allocated through StringFactory. Call NewEmptyString entry point.
4075 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004076 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004077 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4078 __ call(Address(temp, code_offset.SizeValue()));
4079 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4080 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004081 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004082 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4083 DCHECK(!codegen_->IsLeafMethod());
4084 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004085}
4086
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004087void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4088 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004089 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004090 InvokeRuntimeCallingConvention calling_convention;
4091 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004092 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004093 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004094 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004095}
4096
4097void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
4098 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004099 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
4100 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004101 // Note: if heap poisoning is enabled, the entry point takes cares
4102 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004103 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004104 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004105
4106 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004107}
4108
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004109void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004110 LocationSummary* locations =
4111 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004112 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4113 if (location.IsStackSlot()) {
4114 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4115 } else if (location.IsDoubleStackSlot()) {
4116 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4117 }
4118 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004119}
4120
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004121void InstructionCodeGeneratorX86_64::VisitParameterValue(
4122 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004123 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004124}
4125
4126void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4127 LocationSummary* locations =
4128 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4129 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4130}
4131
4132void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4133 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4134 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004135}
4136
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004137void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4138 LocationSummary* locations =
4139 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4140 locations->SetInAt(0, Location::RequiresRegister());
4141 locations->SetOut(Location::RequiresRegister());
4142}
4143
4144void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4145 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004146 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004147 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004148 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004149 __ movq(locations->Out().AsRegister<CpuRegister>(),
4150 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004151 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004152 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004153 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004154 __ movq(locations->Out().AsRegister<CpuRegister>(),
4155 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4156 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004157 __ movq(locations->Out().AsRegister<CpuRegister>(),
4158 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004159 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004160}
4161
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004162void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004163 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004164 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004165 locations->SetInAt(0, Location::RequiresRegister());
4166 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004167}
4168
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004169void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4170 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004171 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4172 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004173 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004174 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004175 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004176 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004177 break;
4178
4179 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004180 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004181 break;
4182
4183 default:
4184 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4185 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004186}
4187
David Brazdil66d126e2015-04-03 16:02:44 +01004188void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4189 LocationSummary* locations =
4190 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4191 locations->SetInAt(0, Location::RequiresRegister());
4192 locations->SetOut(Location::SameAsFirstInput());
4193}
4194
4195void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004196 LocationSummary* locations = bool_not->GetLocations();
4197 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4198 locations->Out().AsRegister<CpuRegister>().AsRegister());
4199 Location out = locations->Out();
4200 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4201}
4202
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004203void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004204 LocationSummary* locations =
4205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004206 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004207 locations->SetInAt(i, Location::Any());
4208 }
4209 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004210}
4211
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004212void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004213 LOG(FATAL) << "Unimplemented";
4214}
4215
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004216void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004217 /*
4218 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004219 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004220 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4221 */
4222 switch (kind) {
4223 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004224 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004225 break;
4226 }
4227 case MemBarrierKind::kAnyStore:
4228 case MemBarrierKind::kLoadAny:
4229 case MemBarrierKind::kStoreStore: {
4230 // nop
4231 break;
4232 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004233 case MemBarrierKind::kNTStoreStore:
4234 // Non-Temporal Store/Store needs an explicit fence.
4235 MemoryFence(/* non-temporal */ true);
4236 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004237 }
4238}
4239
4240void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4241 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4242
Roland Levillain0d5a2812015-11-13 10:07:31 +00004243 bool object_field_get_with_read_barrier =
4244 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004245 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004246 new (GetGraph()->GetArena()) LocationSummary(instruction,
4247 object_field_get_with_read_barrier ?
4248 LocationSummary::kCallOnSlowPath :
4249 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004250 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004251 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004252 }
Calin Juravle52c48962014-12-16 17:02:57 +00004253 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004254 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4255 locations->SetOut(Location::RequiresFpuRegister());
4256 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004257 // The output overlaps for an object field get when read barriers
4258 // are enabled: we do not want the move to overwrite the object's
4259 // location, as we need it to emit the read barrier.
4260 locations->SetOut(
4261 Location::RequiresRegister(),
4262 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004263 }
Calin Juravle52c48962014-12-16 17:02:57 +00004264}
4265
4266void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4267 const FieldInfo& field_info) {
4268 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4269
4270 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004271 Location base_loc = locations->InAt(0);
4272 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004273 Location out = locations->Out();
4274 bool is_volatile = field_info.IsVolatile();
4275 Primitive::Type field_type = field_info.GetFieldType();
4276 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4277
4278 switch (field_type) {
4279 case Primitive::kPrimBoolean: {
4280 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4281 break;
4282 }
4283
4284 case Primitive::kPrimByte: {
4285 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4286 break;
4287 }
4288
4289 case Primitive::kPrimShort: {
4290 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4291 break;
4292 }
4293
4294 case Primitive::kPrimChar: {
4295 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4296 break;
4297 }
4298
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004299 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004300 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4301 break;
4302 }
4303
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004304 case Primitive::kPrimNot: {
4305 // /* HeapReference<Object> */ out = *(base + offset)
4306 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004307 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004308 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004309 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004310 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004311 if (is_volatile) {
4312 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4313 }
4314 } else {
4315 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4316 codegen_->MaybeRecordImplicitNullCheck(instruction);
4317 if (is_volatile) {
4318 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4319 }
4320 // If read barriers are enabled, emit read barriers other than
4321 // Baker's using a slow path (and also unpoison the loaded
4322 // reference, if heap poisoning is enabled).
4323 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4324 }
4325 break;
4326 }
4327
Calin Juravle52c48962014-12-16 17:02:57 +00004328 case Primitive::kPrimLong: {
4329 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4330 break;
4331 }
4332
4333 case Primitive::kPrimFloat: {
4334 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4335 break;
4336 }
4337
4338 case Primitive::kPrimDouble: {
4339 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4340 break;
4341 }
4342
4343 case Primitive::kPrimVoid:
4344 LOG(FATAL) << "Unreachable type " << field_type;
4345 UNREACHABLE();
4346 }
4347
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004348 if (field_type == Primitive::kPrimNot) {
4349 // Potential implicit null checks, in the case of reference
4350 // fields, are handled in the previous switch statement.
4351 } else {
4352 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004353 }
Roland Levillain4d027112015-07-01 15:41:14 +01004354
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004355 if (is_volatile) {
4356 if (field_type == Primitive::kPrimNot) {
4357 // Memory barriers, in the case of references, are also handled
4358 // in the previous switch statement.
4359 } else {
4360 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4361 }
Roland Levillain4d027112015-07-01 15:41:14 +01004362 }
Calin Juravle52c48962014-12-16 17:02:57 +00004363}
4364
4365void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4366 const FieldInfo& field_info) {
4367 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4368
4369 LocationSummary* locations =
4370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004371 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004372 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004373 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004374 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004375
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004376 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004377 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004378 if (is_volatile) {
4379 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4380 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4381 } else {
4382 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4383 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004384 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004385 if (is_volatile) {
4386 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4387 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4388 } else {
4389 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4390 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004391 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004392 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004393 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004394 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004395 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004396 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4397 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004398 locations->AddTemp(Location::RequiresRegister());
4399 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400}
4401
Calin Juravle52c48962014-12-16 17:02:57 +00004402void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004403 const FieldInfo& field_info,
4404 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004405 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4406
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004407 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004408 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4409 Location value = locations->InAt(1);
4410 bool is_volatile = field_info.IsVolatile();
4411 Primitive::Type field_type = field_info.GetFieldType();
4412 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4413
4414 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004415 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004416 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004417
Mark Mendellea5af682015-10-22 17:35:49 -04004418 bool maybe_record_implicit_null_check_done = false;
4419
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420 switch (field_type) {
4421 case Primitive::kPrimBoolean:
4422 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004423 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004424 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004425 __ movb(Address(base, offset), Immediate(v));
4426 } else {
4427 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4428 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429 break;
4430 }
4431
4432 case Primitive::kPrimShort:
4433 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004434 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004435 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004436 __ movw(Address(base, offset), Immediate(v));
4437 } else {
4438 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4439 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004440 break;
4441 }
4442
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004443 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004444 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004445 if (value.IsConstant()) {
4446 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004447 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4448 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4449 // Note: if heap poisoning is enabled, no need to poison
4450 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004451 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004452 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004453 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4454 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4455 __ movl(temp, value.AsRegister<CpuRegister>());
4456 __ PoisonHeapReference(temp);
4457 __ movl(Address(base, offset), temp);
4458 } else {
4459 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4460 }
Mark Mendell40741f32015-04-20 22:10:34 -04004461 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004462 break;
4463 }
4464
4465 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004466 if (value.IsConstant()) {
4467 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004468 codegen_->MoveInt64ToAddress(Address(base, offset),
4469 Address(base, offset + sizeof(int32_t)),
4470 v,
4471 instruction);
4472 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004473 } else {
4474 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4475 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476 break;
4477 }
4478
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004479 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004480 if (value.IsConstant()) {
4481 int32_t v =
4482 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4483 __ movl(Address(base, offset), Immediate(v));
4484 } else {
4485 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4486 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004487 break;
4488 }
4489
4490 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004491 if (value.IsConstant()) {
4492 int64_t v =
4493 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4494 codegen_->MoveInt64ToAddress(Address(base, offset),
4495 Address(base, offset + sizeof(int32_t)),
4496 v,
4497 instruction);
4498 maybe_record_implicit_null_check_done = true;
4499 } else {
4500 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4501 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004502 break;
4503 }
4504
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505 case Primitive::kPrimVoid:
4506 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004507 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004508 }
Calin Juravle52c48962014-12-16 17:02:57 +00004509
Mark Mendellea5af682015-10-22 17:35:49 -04004510 if (!maybe_record_implicit_null_check_done) {
4511 codegen_->MaybeRecordImplicitNullCheck(instruction);
4512 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004513
4514 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4515 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4516 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004517 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004518 }
4519
Calin Juravle52c48962014-12-16 17:02:57 +00004520 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004521 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004522 }
4523}
4524
4525void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4526 HandleFieldSet(instruction, instruction->GetFieldInfo());
4527}
4528
4529void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004530 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004531}
4532
4533void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004534 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004535}
4536
4537void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004538 HandleFieldGet(instruction, instruction->GetFieldInfo());
4539}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004540
Calin Juravle52c48962014-12-16 17:02:57 +00004541void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4542 HandleFieldGet(instruction);
4543}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004544
Calin Juravle52c48962014-12-16 17:02:57 +00004545void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4546 HandleFieldGet(instruction, instruction->GetFieldInfo());
4547}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004548
Calin Juravle52c48962014-12-16 17:02:57 +00004549void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4550 HandleFieldSet(instruction, instruction->GetFieldInfo());
4551}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004552
Calin Juravle52c48962014-12-16 17:02:57 +00004553void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004554 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555}
4556
Calin Juravlee460d1d2015-09-29 04:52:17 +01004557void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4558 HUnresolvedInstanceFieldGet* instruction) {
4559 FieldAccessCallingConventionX86_64 calling_convention;
4560 codegen_->CreateUnresolvedFieldLocationSummary(
4561 instruction, instruction->GetFieldType(), calling_convention);
4562}
4563
4564void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4565 HUnresolvedInstanceFieldGet* instruction) {
4566 FieldAccessCallingConventionX86_64 calling_convention;
4567 codegen_->GenerateUnresolvedFieldAccess(instruction,
4568 instruction->GetFieldType(),
4569 instruction->GetFieldIndex(),
4570 instruction->GetDexPc(),
4571 calling_convention);
4572}
4573
4574void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4575 HUnresolvedInstanceFieldSet* instruction) {
4576 FieldAccessCallingConventionX86_64 calling_convention;
4577 codegen_->CreateUnresolvedFieldLocationSummary(
4578 instruction, instruction->GetFieldType(), calling_convention);
4579}
4580
4581void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4582 HUnresolvedInstanceFieldSet* instruction) {
4583 FieldAccessCallingConventionX86_64 calling_convention;
4584 codegen_->GenerateUnresolvedFieldAccess(instruction,
4585 instruction->GetFieldType(),
4586 instruction->GetFieldIndex(),
4587 instruction->GetDexPc(),
4588 calling_convention);
4589}
4590
4591void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4592 HUnresolvedStaticFieldGet* instruction) {
4593 FieldAccessCallingConventionX86_64 calling_convention;
4594 codegen_->CreateUnresolvedFieldLocationSummary(
4595 instruction, instruction->GetFieldType(), calling_convention);
4596}
4597
4598void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4599 HUnresolvedStaticFieldGet* instruction) {
4600 FieldAccessCallingConventionX86_64 calling_convention;
4601 codegen_->GenerateUnresolvedFieldAccess(instruction,
4602 instruction->GetFieldType(),
4603 instruction->GetFieldIndex(),
4604 instruction->GetDexPc(),
4605 calling_convention);
4606}
4607
4608void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4609 HUnresolvedStaticFieldSet* instruction) {
4610 FieldAccessCallingConventionX86_64 calling_convention;
4611 codegen_->CreateUnresolvedFieldLocationSummary(
4612 instruction, instruction->GetFieldType(), calling_convention);
4613}
4614
4615void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4616 HUnresolvedStaticFieldSet* instruction) {
4617 FieldAccessCallingConventionX86_64 calling_convention;
4618 codegen_->GenerateUnresolvedFieldAccess(instruction,
4619 instruction->GetFieldType(),
4620 instruction->GetFieldIndex(),
4621 instruction->GetDexPc(),
4622 calling_convention);
4623}
4624
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004625void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004626 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4627 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4628 ? Location::RequiresRegister()
4629 : Location::Any();
4630 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004631}
4632
Calin Juravle2ae48182016-03-16 14:05:09 +00004633void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4634 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004635 return;
4636 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004637 LocationSummary* locations = instruction->GetLocations();
4638 Location obj = locations->InAt(0);
4639
4640 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004641 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004642}
4643
Calin Juravle2ae48182016-03-16 14:05:09 +00004644void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004645 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004646 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004647
4648 LocationSummary* locations = instruction->GetLocations();
4649 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004650
4651 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004652 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004653 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004654 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004655 } else {
4656 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004657 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004658 __ jmp(slow_path->GetEntryLabel());
4659 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660 }
4661 __ j(kEqual, slow_path->GetEntryLabel());
4662}
4663
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004664void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004665 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004666}
4667
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004668void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004669 bool object_array_get_with_read_barrier =
4670 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004671 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004672 new (GetGraph()->GetArena()) LocationSummary(instruction,
4673 object_array_get_with_read_barrier ?
4674 LocationSummary::kCallOnSlowPath :
4675 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004676 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004677 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004678 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004679 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004680 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004681 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4682 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4683 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004684 // The output overlaps for an object array get when read barriers
4685 // are enabled: we do not want the move to overwrite the array's
4686 // location, as we need it to emit the read barrier.
4687 locations->SetOut(
4688 Location::RequiresRegister(),
4689 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004690 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004691}
4692
4693void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4694 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004695 Location obj_loc = locations->InAt(0);
4696 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004697 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004698 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004699 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004700
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004701 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004702 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004703 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004704 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004705 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706 break;
4707 }
4708
4709 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004710 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004711 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004712 break;
4713 }
4714
4715 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004716 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004717 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718 break;
4719 }
4720
4721 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004722 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004723 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4724 // Branch cases into compressed and uncompressed for each index's type.
4725 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4726 NearLabel done, not_compressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004727 __ testl(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004728 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004729 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4730 "Expecting 0=compressed, 1=uncompressed");
4731 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004732 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4733 __ jmp(&done);
4734 __ Bind(&not_compressed);
4735 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4736 __ Bind(&done);
4737 } else {
4738 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4739 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004740 break;
4741 }
4742
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004743 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004744 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004745 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004746 break;
4747 }
4748
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004749 case Primitive::kPrimNot: {
4750 static_assert(
4751 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4752 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004753 // /* HeapReference<Object> */ out =
4754 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4755 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004756 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004757 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004758 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004759 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004760 } else {
4761 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004762 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4763 codegen_->MaybeRecordImplicitNullCheck(instruction);
4764 // If read barriers are enabled, emit read barriers other than
4765 // Baker's using a slow path (and also unpoison the loaded
4766 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004767 if (index.IsConstant()) {
4768 uint32_t offset =
4769 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004770 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4771 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004772 codegen_->MaybeGenerateReadBarrierSlow(
4773 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4774 }
4775 }
4776 break;
4777 }
4778
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004779 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004780 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004781 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004782 break;
4783 }
4784
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004785 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004786 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004787 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004788 break;
4789 }
4790
4791 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004792 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004793 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004794 break;
4795 }
4796
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004797 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004798 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004799 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004800 }
Roland Levillain4d027112015-07-01 15:41:14 +01004801
4802 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004803 // Potential implicit null checks, in the case of reference
4804 // arrays, are handled in the previous switch statement.
4805 } else {
4806 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004807 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004808}
4809
4810void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004811 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004812
4813 bool needs_write_barrier =
4814 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004815 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004816
Nicolas Geoffray39468442014-09-02 15:17:15 +01004817 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004818 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004819 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004820 LocationSummary::kCallOnSlowPath :
4821 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004822
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004823 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004824 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4825 if (Primitive::IsFloatingPointType(value_type)) {
4826 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004827 } else {
4828 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4829 }
4830
4831 if (needs_write_barrier) {
4832 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004833 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004834 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004835 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004836}
4837
4838void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4839 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004840 Location array_loc = locations->InAt(0);
4841 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004843 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004844 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004845 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004846 bool needs_write_barrier =
4847 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004848 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4849 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4850 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004851
4852 switch (value_type) {
4853 case Primitive::kPrimBoolean:
4854 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004855 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004856 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004857 if (value.IsRegister()) {
4858 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004859 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004860 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004861 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004862 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004863 break;
4864 }
4865
4866 case Primitive::kPrimShort:
4867 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004868 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004869 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004870 if (value.IsRegister()) {
4871 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004872 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004873 DCHECK(value.IsConstant()) << value;
4874 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004875 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004876 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877 break;
4878 }
4879
4880 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004881 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004882 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004883
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004884 if (!value.IsRegister()) {
4885 // Just setting null.
4886 DCHECK(instruction->InputAt(2)->IsNullConstant());
4887 DCHECK(value.IsConstant()) << value;
4888 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004889 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004890 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004891 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004892 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004893 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004894
4895 DCHECK(needs_write_barrier);
4896 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004897 // We cannot use a NearLabel for `done`, as its range may be too
4898 // short when Baker read barriers are enabled.
4899 Label done;
4900 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004901 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004902 Location temp_loc = locations->GetTemp(0);
4903 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004904 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004905 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4906 codegen_->AddSlowPath(slow_path);
4907 if (instruction->GetValueCanBeNull()) {
4908 __ testl(register_value, register_value);
4909 __ j(kNotEqual, &not_null);
4910 __ movl(address, Immediate(0));
4911 codegen_->MaybeRecordImplicitNullCheck(instruction);
4912 __ jmp(&done);
4913 __ Bind(&not_null);
4914 }
4915
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004916 // Note that when Baker read barriers are enabled, the type
4917 // checks are performed without read barriers. This is fine,
4918 // even in the case where a class object is in the from-space
4919 // after the flip, as a comparison involving such a type would
4920 // not produce a false positive; it may of course produce a
4921 // false negative, in which case we would take the ArraySet
4922 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004923
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004924 // /* HeapReference<Class> */ temp = array->klass_
4925 __ movl(temp, Address(array, class_offset));
4926 codegen_->MaybeRecordImplicitNullCheck(instruction);
4927 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004928
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004929 // /* HeapReference<Class> */ temp = temp->component_type_
4930 __ movl(temp, Address(temp, component_offset));
4931 // If heap poisoning is enabled, no need to unpoison `temp`
4932 // nor the object reference in `register_value->klass`, as
4933 // we are comparing two poisoned references.
4934 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004935
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004936 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4937 __ j(kEqual, &do_put);
4938 // If heap poisoning is enabled, the `temp` reference has
4939 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004940 __ MaybeUnpoisonHeapReference(temp);
4941
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004942 // If heap poisoning is enabled, no need to unpoison the
4943 // heap reference loaded below, as it is only used for a
4944 // comparison with null.
4945 __ cmpl(Address(temp, super_offset), Immediate(0));
4946 __ j(kNotEqual, slow_path->GetEntryLabel());
4947 __ Bind(&do_put);
4948 } else {
4949 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004950 }
4951 }
4952
4953 if (kPoisonHeapReferences) {
4954 __ movl(temp, register_value);
4955 __ PoisonHeapReference(temp);
4956 __ movl(address, temp);
4957 } else {
4958 __ movl(address, register_value);
4959 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004960 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004961 codegen_->MaybeRecordImplicitNullCheck(instruction);
4962 }
4963
4964 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4965 codegen_->MarkGCCard(
4966 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4967 __ Bind(&done);
4968
4969 if (slow_path != nullptr) {
4970 __ Bind(slow_path->GetExitLabel());
4971 }
4972
4973 break;
4974 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004975
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004976 case Primitive::kPrimInt: {
4977 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004978 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004979 if (value.IsRegister()) {
4980 __ movl(address, value.AsRegister<CpuRegister>());
4981 } else {
4982 DCHECK(value.IsConstant()) << value;
4983 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4984 __ movl(address, Immediate(v));
4985 }
4986 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004987 break;
4988 }
4989
4990 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004991 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004992 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004993 if (value.IsRegister()) {
4994 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004995 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004996 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004997 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004998 Address address_high =
4999 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005000 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001 }
5002 break;
5003 }
5004
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005005 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005006 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005007 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005008 if (value.IsFpuRegister()) {
5009 __ movss(address, value.AsFpuRegister<XmmRegister>());
5010 } else {
5011 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005012 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005013 __ movl(address, Immediate(v));
5014 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005015 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005016 break;
5017 }
5018
5019 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005020 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005021 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005022 if (value.IsFpuRegister()) {
5023 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5024 codegen_->MaybeRecordImplicitNullCheck(instruction);
5025 } else {
5026 int64_t v =
5027 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005028 Address address_high =
5029 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005030 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5031 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005032 break;
5033 }
5034
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005035 case Primitive::kPrimVoid:
5036 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005037 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005038 }
5039}
5040
5041void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005042 LocationSummary* locations =
5043 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005044 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005045 if (!instruction->IsEmittedAtUseSite()) {
5046 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5047 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005048}
5049
5050void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005051 if (instruction->IsEmittedAtUseSite()) {
5052 return;
5053 }
5054
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005055 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005056 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005057 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5058 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005059 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005060 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005061 // Mask out most significant bit in case the array is String's array of char.
5062 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005063 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005064 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005065}
5066
5067void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005068 RegisterSet caller_saves = RegisterSet::Empty();
5069 InvokeRuntimeCallingConvention calling_convention;
5070 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5071 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5072 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005073 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005074 HInstruction* length = instruction->InputAt(1);
5075 if (!length->IsEmittedAtUseSite()) {
5076 locations->SetInAt(1, Location::RegisterOrConstant(length));
5077 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078}
5079
5080void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5081 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005082 Location index_loc = locations->InAt(0);
5083 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005084 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005085
Mark Mendell99dbd682015-04-22 16:18:52 -04005086 if (length_loc.IsConstant()) {
5087 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5088 if (index_loc.IsConstant()) {
5089 // BCE will remove the bounds check if we are guarenteed to pass.
5090 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5091 if (index < 0 || index >= length) {
5092 codegen_->AddSlowPath(slow_path);
5093 __ jmp(slow_path->GetEntryLabel());
5094 } else {
5095 // Some optimization after BCE may have generated this, and we should not
5096 // generate a bounds check if it is a valid range.
5097 }
5098 return;
5099 }
5100
5101 // We have to reverse the jump condition because the length is the constant.
5102 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5103 __ cmpl(index_reg, Immediate(length));
5104 codegen_->AddSlowPath(slow_path);
5105 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005106 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005107 HInstruction* array_length = instruction->InputAt(1);
5108 if (array_length->IsEmittedAtUseSite()) {
5109 // Address the length field in the array.
5110 DCHECK(array_length->IsArrayLength());
5111 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5112 Location array_loc = array_length->GetLocations()->InAt(0);
5113 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005114 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005115 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5116 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005117 CpuRegister length_reg = CpuRegister(TMP);
5118 __ movl(length_reg, array_len);
5119 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005120 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005121 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005122 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005123 // Checking the bound for general case:
5124 // Array of char or String's array when the compression feature off.
5125 if (index_loc.IsConstant()) {
5126 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5127 __ cmpl(array_len, Immediate(value));
5128 } else {
5129 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5130 }
5131 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005132 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005133 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005134 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005135 }
5136 codegen_->AddSlowPath(slow_path);
5137 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005138 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005139}
5140
5141void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5142 CpuRegister card,
5143 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005144 CpuRegister value,
5145 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005146 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005147 if (value_can_be_null) {
5148 __ testl(value, value);
5149 __ j(kEqual, &is_null);
5150 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005151 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005152 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 __ movq(temp, object);
5154 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005155 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005156 if (value_can_be_null) {
5157 __ Bind(&is_null);
5158 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159}
5160
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005161void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005162 LOG(FATAL) << "Unimplemented";
5163}
5164
5165void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005166 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5167}
5168
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005169void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005170 LocationSummary* locations =
5171 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005172 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005173}
5174
5175void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005176 HBasicBlock* block = instruction->GetBlock();
5177 if (block->GetLoopInformation() != nullptr) {
5178 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5179 // The back edge will generate the suspend check.
5180 return;
5181 }
5182 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5183 // The goto will generate the suspend check.
5184 return;
5185 }
5186 GenerateSuspendCheck(instruction, nullptr);
5187}
5188
5189void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5190 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005191 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005192 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5193 if (slow_path == nullptr) {
5194 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5195 instruction->SetSlowPath(slow_path);
5196 codegen_->AddSlowPath(slow_path);
5197 if (successor != nullptr) {
5198 DCHECK(successor->IsLoopHeader());
5199 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5200 }
5201 } else {
5202 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5203 }
5204
Andreas Gampe542451c2016-07-26 09:02:02 -07005205 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005206 /* no_rip */ true),
5207 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005208 if (successor == nullptr) {
5209 __ j(kNotEqual, slow_path->GetEntryLabel());
5210 __ Bind(slow_path->GetReturnLabel());
5211 } else {
5212 __ j(kEqual, codegen_->GetLabelOf(successor));
5213 __ jmp(slow_path->GetEntryLabel());
5214 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005215}
5216
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005217X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5218 return codegen_->GetAssembler();
5219}
5220
5221void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005222 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005223 Location source = move->GetSource();
5224 Location destination = move->GetDestination();
5225
5226 if (source.IsRegister()) {
5227 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005228 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005229 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005230 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005231 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005232 } else {
5233 DCHECK(destination.IsDoubleStackSlot());
5234 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005235 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005236 }
5237 } else if (source.IsStackSlot()) {
5238 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005239 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005240 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005241 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005242 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005243 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005244 } else {
5245 DCHECK(destination.IsStackSlot());
5246 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5247 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5248 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005249 } else if (source.IsDoubleStackSlot()) {
5250 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005251 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005252 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005253 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005254 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5255 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005256 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005257 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005258 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5259 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5260 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005261 } else if (source.IsConstant()) {
5262 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005263 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5264 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005265 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005266 if (value == 0) {
5267 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5268 } else {
5269 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5270 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005271 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005272 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005273 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005274 }
5275 } else if (constant->IsLongConstant()) {
5276 int64_t value = constant->AsLongConstant()->GetValue();
5277 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005278 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005279 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005280 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005281 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005282 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005283 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005284 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005285 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005286 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005287 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005288 } else {
5289 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005290 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005291 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5292 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005293 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005294 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005295 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005296 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005297 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005298 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005299 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005300 } else {
5301 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005302 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005303 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005304 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005305 } else if (source.IsFpuRegister()) {
5306 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005307 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005308 } else if (destination.IsStackSlot()) {
5309 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005310 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005311 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005312 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005313 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005314 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005315 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005316 }
5317}
5318
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005319void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005320 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005321 __ movl(Address(CpuRegister(RSP), mem), reg);
5322 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005323}
5324
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005325void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005326 ScratchRegisterScope ensure_scratch(
5327 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5328
5329 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5330 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5331 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5332 Address(CpuRegister(RSP), mem2 + stack_offset));
5333 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5334 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5335 CpuRegister(ensure_scratch.GetRegister()));
5336}
5337
Mark Mendell8a1c7282015-06-29 15:41:28 -04005338void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5339 __ movq(CpuRegister(TMP), reg1);
5340 __ movq(reg1, reg2);
5341 __ movq(reg2, CpuRegister(TMP));
5342}
5343
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005344void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5345 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5346 __ movq(Address(CpuRegister(RSP), mem), reg);
5347 __ movq(reg, CpuRegister(TMP));
5348}
5349
5350void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5351 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005352 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005353
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005354 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5355 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5356 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5357 Address(CpuRegister(RSP), mem2 + stack_offset));
5358 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5359 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5360 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005361}
5362
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005363void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5364 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5365 __ movss(Address(CpuRegister(RSP), mem), reg);
5366 __ movd(reg, CpuRegister(TMP));
5367}
5368
5369void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5370 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5371 __ movsd(Address(CpuRegister(RSP), mem), reg);
5372 __ movd(reg, CpuRegister(TMP));
5373}
5374
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005375void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005376 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005377 Location source = move->GetSource();
5378 Location destination = move->GetDestination();
5379
5380 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005381 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005382 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005383 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005384 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005385 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005386 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005387 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5388 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005389 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005390 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005391 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005392 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5393 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005394 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005395 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5396 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5397 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005398 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005399 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005400 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005401 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005402 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005403 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005404 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005405 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005406 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005407 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005408 }
5409}
5410
5411
5412void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5413 __ pushq(CpuRegister(reg));
5414}
5415
5416
5417void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5418 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005419}
5420
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005421void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005422 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005423 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5424 Immediate(mirror::Class::kStatusInitialized));
5425 __ j(kLess, slow_path->GetEntryLabel());
5426 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005427 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005428}
5429
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005430HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5431 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005432 switch (desired_class_load_kind) {
5433 case HLoadClass::LoadKind::kReferrersClass:
5434 break;
5435 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5436 DCHECK(!GetCompilerOptions().GetCompilePic());
5437 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5438 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5439 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5440 DCHECK(GetCompilerOptions().GetCompilePic());
5441 break;
5442 case HLoadClass::LoadKind::kBootImageAddress:
5443 break;
5444 case HLoadClass::LoadKind::kDexCacheAddress:
5445 DCHECK(Runtime::Current()->UseJitCompilation());
5446 break;
5447 case HLoadClass::LoadKind::kDexCachePcRelative:
5448 DCHECK(!Runtime::Current()->UseJitCompilation());
5449 break;
5450 case HLoadClass::LoadKind::kDexCacheViaMethod:
5451 break;
5452 }
5453 return desired_class_load_kind;
5454}
5455
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005456void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005457 if (cls->NeedsAccessCheck()) {
5458 InvokeRuntimeCallingConvention calling_convention;
5459 CodeGenerator::CreateLoadClassLocationSummary(
5460 cls,
5461 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5462 Location::RegisterLocation(RAX),
5463 /* code_generator_supports_read_barrier */ true);
5464 return;
5465 }
5466
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005467 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5468 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005469 ? LocationSummary::kCallOnSlowPath
5470 : LocationSummary::kNoCall;
5471 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005472 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005473 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005474 }
5475
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005476 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5477 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5478 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5479 locations->SetInAt(0, Location::RequiresRegister());
5480 }
5481 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005482}
5483
5484void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005485 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005486 if (cls->NeedsAccessCheck()) {
5487 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005488 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005489 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005490 return;
5491 }
5492
Roland Levillain0d5a2812015-11-13 10:07:31 +00005493 Location out_loc = locations->Out();
5494 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005495
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005496 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5497 ? kWithoutReadBarrier
5498 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005499 bool generate_null_check = false;
5500 switch (cls->GetLoadKind()) {
5501 case HLoadClass::LoadKind::kReferrersClass: {
5502 DCHECK(!cls->CanCallRuntime());
5503 DCHECK(!cls->MustGenerateClinitCheck());
5504 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5505 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5506 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005507 cls,
5508 out_loc,
5509 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005510 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005511 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005512 break;
5513 }
5514 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005515 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005516 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5517 codegen_->RecordTypePatch(cls);
5518 break;
5519 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005520 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005521 DCHECK_NE(cls->GetAddress(), 0u);
5522 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5523 __ movl(out, Immediate(address)); // Zero-extended.
5524 codegen_->RecordSimplePatch();
5525 break;
5526 }
5527 case HLoadClass::LoadKind::kDexCacheAddress: {
5528 DCHECK_NE(cls->GetAddress(), 0u);
5529 // /* GcRoot<mirror::Class> */ out = *address
5530 if (IsUint<32>(cls->GetAddress())) {
5531 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005532 GenerateGcRootFieldLoad(cls,
5533 out_loc,
5534 address,
Roland Levillain00468f32016-10-27 18:02:48 +01005535 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005536 read_barrier_option);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005537 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005538 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5539 __ movq(out, Immediate(cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005540 GenerateGcRootFieldLoad(cls,
5541 out_loc,
5542 Address(out, 0),
Roland Levillain00468f32016-10-27 18:02:48 +01005543 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005544 read_barrier_option);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005545 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005546 generate_null_check = !cls->IsInDexCache();
5547 break;
5548 }
5549 case HLoadClass::LoadKind::kDexCachePcRelative: {
5550 uint32_t offset = cls->GetDexCacheElementOffset();
5551 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5552 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5553 /* no_rip */ false);
5554 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005555 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005556 generate_null_check = !cls->IsInDexCache();
5557 break;
5558 }
5559 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5560 // /* GcRoot<mirror::Class>[] */ out =
5561 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5562 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5563 __ movq(out,
5564 Address(current_method,
5565 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5566 // /* GcRoot<mirror::Class> */ out = out[type_index]
5567 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005568 cls,
5569 out_loc,
5570 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
Roland Levillain00468f32016-10-27 18:02:48 +01005571 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005572 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005573 generate_null_check = !cls->IsInDexCache();
5574 break;
5575 }
5576 default:
5577 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5578 UNREACHABLE();
5579 }
5580
5581 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5582 DCHECK(cls->CanCallRuntime());
5583 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5584 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5585 codegen_->AddSlowPath(slow_path);
5586 if (generate_null_check) {
5587 __ testl(out, out);
5588 __ j(kEqual, slow_path->GetEntryLabel());
5589 }
5590 if (cls->MustGenerateClinitCheck()) {
5591 GenerateClassInitializationCheck(slow_path, out);
5592 } else {
5593 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005594 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005595 }
5596}
5597
5598void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5599 LocationSummary* locations =
5600 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5601 locations->SetInAt(0, Location::RequiresRegister());
5602 if (check->HasUses()) {
5603 locations->SetOut(Location::SameAsFirstInput());
5604 }
5605}
5606
5607void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005608 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005609 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005610 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005611 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005612 GenerateClassInitializationCheck(slow_path,
5613 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005614}
5615
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005616HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5617 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005618 switch (desired_string_load_kind) {
5619 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5620 DCHECK(!GetCompilerOptions().GetCompilePic());
5621 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5622 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5623 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5624 DCHECK(GetCompilerOptions().GetCompilePic());
5625 break;
5626 case HLoadString::LoadKind::kBootImageAddress:
5627 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005628 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005629 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005630 break;
5631 case HLoadString::LoadKind::kDexCacheViaMethod:
5632 break;
5633 }
5634 return desired_string_load_kind;
5635}
5636
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005637void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00005638 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
5639 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5640 ? LocationSummary::kCallOnMainOnly
5641 : LocationSummary::kCallOnSlowPath)
5642 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005643 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005644 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005645 locations->SetOut(Location::RegisterLocation(RAX));
5646 } else {
5647 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005648 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5649 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5650 // Rely on the pResolveString and/or marking to save everything.
5651 // Custom calling convention: RAX serves as both input and output.
5652 RegisterSet caller_saves = RegisterSet::Empty();
5653 caller_saves.Add(Location::RegisterLocation(RAX));
5654 locations->SetCustomSlowPathCallerSaves(caller_saves);
5655 } else {
5656 // For non-Baker read barrier we have a temp-clobbering call.
5657 }
5658 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005659 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005660}
5661
5662void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005663 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005664 Location out_loc = locations->Out();
5665 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005666
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005667 switch (load->GetLoadKind()) {
5668 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005669 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005670 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005671 return; // No dex cache slow path.
5672 }
5673 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005674 DCHECK_NE(load->GetAddress(), 0u);
5675 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5676 __ movl(out, Immediate(address)); // Zero-extended.
5677 codegen_->RecordSimplePatch();
5678 return; // No dex cache slow path.
5679 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005680 case HLoadString::LoadKind::kBssEntry: {
5681 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5682 /* no_rip */ false);
5683 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5684 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005685 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005686 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5687 codegen_->AddSlowPath(slow_path);
5688 __ testl(out, out);
5689 __ j(kEqual, slow_path->GetEntryLabel());
5690 __ Bind(slow_path->GetExitLabel());
5691 return;
5692 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005693 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005694 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005695 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005696
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005697 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005698 // Custom calling convention: RAX serves as both input and output.
5699 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex()));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005700 codegen_->InvokeRuntime(kQuickResolveString,
5701 load,
5702 load->GetDexPc());
5703 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005704}
5705
David Brazdilcb1c0552015-08-04 16:22:25 +01005706static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005707 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005708 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005709}
5710
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005711void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5712 LocationSummary* locations =
5713 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5714 locations->SetOut(Location::RequiresRegister());
5715}
5716
5717void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005718 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5719}
5720
5721void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5722 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5723}
5724
5725void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5726 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005727}
5728
5729void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5730 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005731 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005732 InvokeRuntimeCallingConvention calling_convention;
5733 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5734}
5735
5736void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005737 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005738 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005739}
5740
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005741static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5742 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005743 // We need a temporary for holding the iftable length.
5744 return true;
5745 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005746 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005747 !kUseBakerReadBarrier &&
5748 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005749 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5750 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5751}
5752
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005753static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5754 return kEmitCompilerReadBarrier &&
5755 !kUseBakerReadBarrier &&
5756 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5757 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5758 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5759}
5760
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005761void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005762 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005763 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005764 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005765 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005766 case TypeCheckKind::kExactCheck:
5767 case TypeCheckKind::kAbstractClassCheck:
5768 case TypeCheckKind::kClassHierarchyCheck:
5769 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005770 call_kind =
5771 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005772 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005773 break;
5774 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005775 case TypeCheckKind::kUnresolvedCheck:
5776 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005777 call_kind = LocationSummary::kCallOnSlowPath;
5778 break;
5779 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005780
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005781 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005782 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005783 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005784 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005785 locations->SetInAt(0, Location::RequiresRegister());
5786 locations->SetInAt(1, Location::Any());
5787 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5788 locations->SetOut(Location::RequiresRegister());
5789 // When read barriers are enabled, we need a temporary register for
5790 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005791 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005792 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005793 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005794}
5795
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005796void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005797 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005798 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005799 Location obj_loc = locations->InAt(0);
5800 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005801 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005802 Location out_loc = locations->Out();
5803 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005804 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005805 locations->GetTemp(0) :
5806 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005807 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005808 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5809 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5810 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005811 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005812 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005813
5814 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005815 // Avoid null check if we know obj is not null.
5816 if (instruction->MustDoNullCheck()) {
5817 __ testl(obj, obj);
5818 __ j(kEqual, &zero);
5819 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005820
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005821 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005822 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005823 // /* HeapReference<Class> */ out = obj->klass_
5824 GenerateReferenceLoadTwoRegisters(instruction,
5825 out_loc,
5826 obj_loc,
5827 class_offset,
5828 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 if (cls.IsRegister()) {
5830 __ cmpl(out, cls.AsRegister<CpuRegister>());
5831 } else {
5832 DCHECK(cls.IsStackSlot()) << cls;
5833 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5834 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005835 if (zero.IsLinked()) {
5836 // Classes must be equal for the instanceof to succeed.
5837 __ j(kNotEqual, &zero);
5838 __ movl(out, Immediate(1));
5839 __ jmp(&done);
5840 } else {
5841 __ setcc(kEqual, out);
5842 // setcc only sets the low byte.
5843 __ andl(out, Immediate(1));
5844 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005845 break;
5846 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005847
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005848 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005849 // /* HeapReference<Class> */ out = obj->klass_
5850 GenerateReferenceLoadTwoRegisters(instruction,
5851 out_loc,
5852 obj_loc,
5853 class_offset,
5854 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005855 // If the class is abstract, we eagerly fetch the super class of the
5856 // object to avoid doing a comparison we know will fail.
5857 NearLabel loop, success;
5858 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005859 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005860 GenerateReferenceLoadOneRegister(instruction,
5861 out_loc,
5862 super_offset,
5863 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005864 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005865 __ testl(out, out);
5866 // If `out` is null, we use it for the result, and jump to `done`.
5867 __ j(kEqual, &done);
5868 if (cls.IsRegister()) {
5869 __ cmpl(out, cls.AsRegister<CpuRegister>());
5870 } else {
5871 DCHECK(cls.IsStackSlot()) << cls;
5872 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5873 }
5874 __ j(kNotEqual, &loop);
5875 __ movl(out, Immediate(1));
5876 if (zero.IsLinked()) {
5877 __ jmp(&done);
5878 }
5879 break;
5880 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005881
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005883 // /* HeapReference<Class> */ out = obj->klass_
5884 GenerateReferenceLoadTwoRegisters(instruction,
5885 out_loc,
5886 obj_loc,
5887 class_offset,
5888 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 // Walk over the class hierarchy to find a match.
5890 NearLabel loop, success;
5891 __ Bind(&loop);
5892 if (cls.IsRegister()) {
5893 __ cmpl(out, cls.AsRegister<CpuRegister>());
5894 } else {
5895 DCHECK(cls.IsStackSlot()) << cls;
5896 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5897 }
5898 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005899 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005900 GenerateReferenceLoadOneRegister(instruction,
5901 out_loc,
5902 super_offset,
5903 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005904 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005905 __ testl(out, out);
5906 __ j(kNotEqual, &loop);
5907 // If `out` is null, we use it for the result, and jump to `done`.
5908 __ jmp(&done);
5909 __ Bind(&success);
5910 __ movl(out, Immediate(1));
5911 if (zero.IsLinked()) {
5912 __ jmp(&done);
5913 }
5914 break;
5915 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005916
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005917 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005918 // /* HeapReference<Class> */ out = obj->klass_
5919 GenerateReferenceLoadTwoRegisters(instruction,
5920 out_loc,
5921 obj_loc,
5922 class_offset,
5923 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005924 // Do an exact check.
5925 NearLabel exact_check;
5926 if (cls.IsRegister()) {
5927 __ cmpl(out, cls.AsRegister<CpuRegister>());
5928 } else {
5929 DCHECK(cls.IsStackSlot()) << cls;
5930 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5931 }
5932 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005933 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005934 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005935 GenerateReferenceLoadOneRegister(instruction,
5936 out_loc,
5937 component_offset,
5938 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005939 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005940 __ testl(out, out);
5941 // If `out` is null, we use it for the result, and jump to `done`.
5942 __ j(kEqual, &done);
5943 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5944 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005945 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005946 __ movl(out, Immediate(1));
5947 __ jmp(&done);
5948 break;
5949 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005950
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005951 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005952 // No read barrier since the slow path will retry upon failure.
5953 // /* HeapReference<Class> */ out = obj->klass_
5954 GenerateReferenceLoadTwoRegisters(instruction,
5955 out_loc,
5956 obj_loc,
5957 class_offset,
5958 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 if (cls.IsRegister()) {
5960 __ cmpl(out, cls.AsRegister<CpuRegister>());
5961 } else {
5962 DCHECK(cls.IsStackSlot()) << cls;
5963 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5964 }
5965 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005966 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5967 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 codegen_->AddSlowPath(slow_path);
5969 __ j(kNotEqual, slow_path->GetEntryLabel());
5970 __ movl(out, Immediate(1));
5971 if (zero.IsLinked()) {
5972 __ jmp(&done);
5973 }
5974 break;
5975 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005976
Calin Juravle98893e12015-10-02 21:05:03 +01005977 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005978 case TypeCheckKind::kInterfaceCheck: {
5979 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005980 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005981 // cases.
5982 //
5983 // We cannot directly call the InstanceofNonTrivial runtime
5984 // entry point without resorting to a type checking slow path
5985 // here (i.e. by calling InvokeRuntime directly), as it would
5986 // require to assign fixed registers for the inputs of this
5987 // HInstanceOf instruction (following the runtime calling
5988 // convention), which might be cluttered by the potential first
5989 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005990 //
5991 // TODO: Introduce a new runtime entry point taking the object
5992 // to test (instead of its class) as argument, and let it deal
5993 // with the read barrier issues. This will let us refactor this
5994 // case of the `switch` code as it was previously (with a direct
5995 // call to the runtime not using a type checking slow path).
5996 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005997 DCHECK(locations->OnlyCallsOnSlowPath());
5998 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5999 /* is_fatal */ false);
6000 codegen_->AddSlowPath(slow_path);
6001 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006002 if (zero.IsLinked()) {
6003 __ jmp(&done);
6004 }
6005 break;
6006 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006007 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006008
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006009 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006010 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006011 __ xorl(out, out);
6012 }
6013
6014 if (done.IsLinked()) {
6015 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006016 }
6017
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006018 if (slow_path != nullptr) {
6019 __ Bind(slow_path->GetExitLabel());
6020 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006021}
6022
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006023static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006024 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025 case TypeCheckKind::kExactCheck:
6026 case TypeCheckKind::kAbstractClassCheck:
6027 case TypeCheckKind::kClassHierarchyCheck:
6028 case TypeCheckKind::kArrayObjectCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006029 return !throws_into_catch && !kEmitCompilerReadBarrier;
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006030 case TypeCheckKind::kInterfaceCheck:
6031 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006032 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033 case TypeCheckKind::kUnresolvedCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006034 return false;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006035 }
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006036 LOG(FATAL) << "Unreachable";
6037 UNREACHABLE();
6038}
6039
6040void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
6041 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6042 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6043 bool is_fatal_slow_path = IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch);
6044 LocationSummary::CallKind call_kind = is_fatal_slow_path
6045 ? LocationSummary::kNoCall
6046 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6048 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006049 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6050 // Require a register for the interface check since there is a loop that compares the class to
6051 // a memory address.
6052 locations->SetInAt(1, Location::RequiresRegister());
6053 } else {
6054 locations->SetInAt(1, Location::Any());
6055 }
6056
Roland Levillain0d5a2812015-11-13 10:07:31 +00006057 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6058 locations->AddTemp(Location::RequiresRegister());
6059 // When read barriers are enabled, we need an additional temporary
6060 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006061 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006063 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006064}
6065
6066void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006067 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006068 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006069 Location obj_loc = locations->InAt(0);
6070 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006071 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006072 Location temp_loc = locations->GetTemp(0);
6073 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006074 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006075 locations->GetTemp(1) :
6076 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006077 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6078 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6079 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6080 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6081 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6082 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006083 const uint32_t object_array_data_offset =
6084 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006085
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006086 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6087 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6088 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006089 bool is_type_check_slow_path_fatal =
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006090 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006091 SlowPathCode* type_check_slow_path =
6092 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6093 is_type_check_slow_path_fatal);
6094 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006095
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006096
6097 NearLabel done;
6098 // Avoid null check if we know obj is not null.
6099 if (instruction->MustDoNullCheck()) {
6100 __ testl(obj, obj);
6101 __ j(kEqual, &done);
6102 }
6103
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006105 case TypeCheckKind::kExactCheck:
6106 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006107 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006108 GenerateReferenceLoadTwoRegisters(instruction,
6109 temp_loc,
6110 obj_loc,
6111 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006112 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006113 if (cls.IsRegister()) {
6114 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6115 } else {
6116 DCHECK(cls.IsStackSlot()) << cls;
6117 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6118 }
6119 // Jump to slow path for throwing the exception or doing a
6120 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006121 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006122 break;
6123 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006124
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006125 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006126 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006127 GenerateReferenceLoadTwoRegisters(instruction,
6128 temp_loc,
6129 obj_loc,
6130 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006131 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006132 // If the class is abstract, we eagerly fetch the super class of the
6133 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006134 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006135 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006136 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006137 GenerateReferenceLoadOneRegister(instruction,
6138 temp_loc,
6139 super_offset,
6140 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006141 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006142
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006143 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6144 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006146 // Otherwise, compare the classes.
6147 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148 if (cls.IsRegister()) {
6149 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6150 } else {
6151 DCHECK(cls.IsStackSlot()) << cls;
6152 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6153 }
6154 __ j(kNotEqual, &loop);
6155 break;
6156 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006157
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006159 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006160 GenerateReferenceLoadTwoRegisters(instruction,
6161 temp_loc,
6162 obj_loc,
6163 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006164 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006166 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167 __ Bind(&loop);
6168 if (cls.IsRegister()) {
6169 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6170 } else {
6171 DCHECK(cls.IsStackSlot()) << cls;
6172 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6173 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006174 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006175
Roland Levillain0d5a2812015-11-13 10:07:31 +00006176 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006177 GenerateReferenceLoadOneRegister(instruction,
6178 temp_loc,
6179 super_offset,
6180 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006181 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006182
6183 // If the class reference currently in `temp` is not null, jump
6184 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006185 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006186 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006187 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189 break;
6190 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006191
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006192 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006193 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006194 GenerateReferenceLoadTwoRegisters(instruction,
6195 temp_loc,
6196 obj_loc,
6197 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006198 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006199 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006200 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006201 if (cls.IsRegister()) {
6202 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6203 } else {
6204 DCHECK(cls.IsStackSlot()) << cls;
6205 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6206 }
6207 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006208
6209 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006210 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006211 GenerateReferenceLoadOneRegister(instruction,
6212 temp_loc,
6213 component_offset,
6214 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006215 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006216
6217 // If the component type is not null (i.e. the object is indeed
6218 // an array), jump to label `check_non_primitive_component_type`
6219 // to further check that this component type is not a primitive
6220 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006222 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006223 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006224 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006225 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006226 break;
6227 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006228
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006229 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006230 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006231 //
6232 // We cannot directly call the CheckCast runtime entry point
6233 // without resorting to a type checking slow path here (i.e. by
6234 // calling InvokeRuntime directly), as it would require to
6235 // assign fixed registers for the inputs of this HInstanceOf
6236 // instruction (following the runtime calling convention), which
6237 // might be cluttered by the potential first read barrier
6238 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006239 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006240 break;
6241 }
6242
6243 case TypeCheckKind::kInterfaceCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006244 // Fast path for the interface check. We always go slow path for heap poisoning since
6245 // unpoisoning cls would require an extra temp.
6246 if (!kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006247 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6248 // doing this.
6249 // /* HeapReference<Class> */ temp = obj->klass_
6250 GenerateReferenceLoadTwoRegisters(instruction,
6251 temp_loc,
6252 obj_loc,
6253 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006254 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006255
6256 // /* HeapReference<Class> */ temp = temp->iftable_
6257 GenerateReferenceLoadTwoRegisters(instruction,
6258 temp_loc,
6259 temp_loc,
6260 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006261 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006262 // Null iftable means it is empty.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006263 __ testl(temp, temp);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006264 __ j(kZero, type_check_slow_path->GetEntryLabel());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006265
6266 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006267 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006268
6269 NearLabel start_loop;
6270 __ Bind(&start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006271 __ cmpl(cls.AsRegister<CpuRegister>(), Address(temp, object_array_data_offset));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006272 __ j(kEqual, &done); // Return if same class.
6273 // Go to next interface.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006274 __ addl(temp, Immediate(2 * kHeapReferenceSize));
6275 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006276 __ j(kNotZero, &start_loop);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006277 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006279 break;
6280 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006281
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006282 if (done.IsLinked()) {
6283 __ Bind(&done);
6284 }
6285
Roland Levillain0d5a2812015-11-13 10:07:31 +00006286 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006287}
6288
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006289void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6290 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006291 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006292 InvokeRuntimeCallingConvention calling_convention;
6293 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6294}
6295
6296void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006297 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006298 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006299 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006300 if (instruction->IsEnter()) {
6301 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6302 } else {
6303 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6304 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006305}
6306
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006307void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6308void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6309void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6310
6311void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6312 LocationSummary* locations =
6313 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6314 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6315 || instruction->GetResultType() == Primitive::kPrimLong);
6316 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006317 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006318 locations->SetOut(Location::SameAsFirstInput());
6319}
6320
6321void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6322 HandleBitwiseOperation(instruction);
6323}
6324
6325void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6326 HandleBitwiseOperation(instruction);
6327}
6328
6329void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6330 HandleBitwiseOperation(instruction);
6331}
6332
6333void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6334 LocationSummary* locations = instruction->GetLocations();
6335 Location first = locations->InAt(0);
6336 Location second = locations->InAt(1);
6337 DCHECK(first.Equals(locations->Out()));
6338
6339 if (instruction->GetResultType() == Primitive::kPrimInt) {
6340 if (second.IsRegister()) {
6341 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006342 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006343 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006344 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006345 } else {
6346 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006347 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006348 }
6349 } else if (second.IsConstant()) {
6350 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6351 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006352 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006353 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006354 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006355 } else {
6356 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006357 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006358 }
6359 } else {
6360 Address address(CpuRegister(RSP), second.GetStackIndex());
6361 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006362 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006363 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006364 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006365 } else {
6366 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006367 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006368 }
6369 }
6370 } else {
6371 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006372 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6373 bool second_is_constant = false;
6374 int64_t value = 0;
6375 if (second.IsConstant()) {
6376 second_is_constant = true;
6377 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006378 }
Mark Mendell40741f32015-04-20 22:10:34 -04006379 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006380
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006381 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006382 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006383 if (is_int32_value) {
6384 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6385 } else {
6386 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6387 }
6388 } else if (second.IsDoubleStackSlot()) {
6389 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006390 } else {
6391 __ andq(first_reg, second.AsRegister<CpuRegister>());
6392 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006393 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006394 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006395 if (is_int32_value) {
6396 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6397 } else {
6398 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6399 }
6400 } else if (second.IsDoubleStackSlot()) {
6401 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006402 } else {
6403 __ orq(first_reg, second.AsRegister<CpuRegister>());
6404 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006405 } else {
6406 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006407 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006408 if (is_int32_value) {
6409 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6410 } else {
6411 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6412 }
6413 } else if (second.IsDoubleStackSlot()) {
6414 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006415 } else {
6416 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6417 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006418 }
6419 }
6420}
6421
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006422void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6423 HInstruction* instruction,
6424 Location out,
6425 uint32_t offset,
6426 Location maybe_temp,
6427 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006428 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006429 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006430 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006431 if (kUseBakerReadBarrier) {
6432 // Load with fast path based Baker's read barrier.
6433 // /* HeapReference<Object> */ out = *(out + offset)
6434 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006435 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006436 } else {
6437 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006438 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006439 // in the following move operation, as we will need it for the
6440 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006441 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006442 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006443 // /* HeapReference<Object> */ out = *(out + offset)
6444 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006445 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006446 }
6447 } else {
6448 // Plain load with no read barrier.
6449 // /* HeapReference<Object> */ out = *(out + offset)
6450 __ movl(out_reg, Address(out_reg, offset));
6451 __ MaybeUnpoisonHeapReference(out_reg);
6452 }
6453}
6454
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006455void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6456 HInstruction* instruction,
6457 Location out,
6458 Location obj,
6459 uint32_t offset,
6460 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006461 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6462 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006463 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006464 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006465 if (kUseBakerReadBarrier) {
6466 // Load with fast path based Baker's read barrier.
6467 // /* HeapReference<Object> */ out = *(obj + offset)
6468 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006469 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006470 } else {
6471 // Load with slow path based read barrier.
6472 // /* HeapReference<Object> */ out = *(obj + offset)
6473 __ movl(out_reg, Address(obj_reg, offset));
6474 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6475 }
6476 } else {
6477 // Plain load with no read barrier.
6478 // /* HeapReference<Object> */ out = *(obj + offset)
6479 __ movl(out_reg, Address(obj_reg, offset));
6480 __ MaybeUnpoisonHeapReference(out_reg);
6481 }
6482}
6483
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006484void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6485 HInstruction* instruction,
6486 Location root,
6487 const Address& address,
6488 Label* fixup_label,
6489 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006490 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006491 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006492 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006493 if (kUseBakerReadBarrier) {
6494 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6495 // Baker's read barrier are used:
6496 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006497 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006498 // if (Thread::Current()->GetIsGcMarking()) {
6499 // root = ReadBarrier::Mark(root)
6500 // }
6501
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006502 // /* GcRoot<mirror::Object> */ root = *address
6503 __ movl(root_reg, address);
6504 if (fixup_label != nullptr) {
6505 __ Bind(fixup_label);
6506 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006507 static_assert(
6508 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6509 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6510 "have different sizes.");
6511 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6512 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6513 "have different sizes.");
6514
Vladimir Marko953437b2016-08-24 08:30:46 +00006515 // Slow path marking the GC root `root`.
6516 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006517 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006518 codegen_->AddSlowPath(slow_path);
6519
Andreas Gampe542451c2016-07-26 09:02:02 -07006520 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006521 /* no_rip */ true),
6522 Immediate(0));
6523 __ j(kNotEqual, slow_path->GetEntryLabel());
6524 __ Bind(slow_path->GetExitLabel());
6525 } else {
6526 // GC root loaded through a slow path for read barriers other
6527 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006528 // /* GcRoot<mirror::Object>* */ root = address
6529 __ leaq(root_reg, address);
6530 if (fixup_label != nullptr) {
6531 __ Bind(fixup_label);
6532 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006533 // /* mirror::Object* */ root = root->Read()
6534 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6535 }
6536 } else {
6537 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006538 // /* GcRoot<mirror::Object> */ root = *address
6539 __ movl(root_reg, address);
6540 if (fixup_label != nullptr) {
6541 __ Bind(fixup_label);
6542 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006543 // Note that GC roots are not affected by heap poisoning, thus we
6544 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006545 }
6546}
6547
6548void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6549 Location ref,
6550 CpuRegister obj,
6551 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006552 bool needs_null_check) {
6553 DCHECK(kEmitCompilerReadBarrier);
6554 DCHECK(kUseBakerReadBarrier);
6555
6556 // /* HeapReference<Object> */ ref = *(obj + offset)
6557 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006558 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006559}
6560
6561void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6562 Location ref,
6563 CpuRegister obj,
6564 uint32_t data_offset,
6565 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006566 bool needs_null_check) {
6567 DCHECK(kEmitCompilerReadBarrier);
6568 DCHECK(kUseBakerReadBarrier);
6569
Roland Levillain3d312422016-06-23 13:53:42 +01006570 static_assert(
6571 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6572 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006573 // /* HeapReference<Object> */ ref =
6574 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006575 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006576 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006577}
6578
6579void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6580 Location ref,
6581 CpuRegister obj,
6582 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006583 bool needs_null_check,
6584 bool always_update_field,
6585 CpuRegister* temp1,
6586 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006587 DCHECK(kEmitCompilerReadBarrier);
6588 DCHECK(kUseBakerReadBarrier);
6589
6590 // In slow path based read barriers, the read barrier call is
6591 // inserted after the original load. However, in fast path based
6592 // Baker's read barriers, we need to perform the load of
6593 // mirror::Object::monitor_ *before* the original reference load.
6594 // This load-load ordering is required by the read barrier.
6595 // The fast path/slow path (for Baker's algorithm) should look like:
6596 //
6597 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6598 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6599 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006600 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006601 // if (is_gray) {
6602 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6603 // }
6604 //
6605 // Note: the original implementation in ReadBarrier::Barrier is
6606 // slightly more complex as:
6607 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006608 // the high-bits of rb_state, which are expected to be all zeroes
6609 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6610 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006611 // - it performs additional checks that we do not do here for
6612 // performance reasons.
6613
6614 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006615 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6616
Vladimir Marko953437b2016-08-24 08:30:46 +00006617 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006618 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6619 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006620 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6621 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6622 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6623
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006624 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006625 // ref = ReadBarrier::Mark(ref);
6626 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6627 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006628 if (needs_null_check) {
6629 MaybeRecordImplicitNullCheck(instruction);
6630 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006631
6632 // Load fence to prevent load-load reordering.
6633 // Note that this is a no-op, thanks to the x86-64 memory model.
6634 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6635
6636 // The actual reference load.
6637 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006638 __ movl(ref_reg, src); // Flags are unaffected.
6639
6640 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6641 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006642 SlowPathCode* slow_path;
6643 if (always_update_field) {
6644 DCHECK(temp1 != nullptr);
6645 DCHECK(temp2 != nullptr);
6646 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
6647 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6648 } else {
6649 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6650 instruction, ref, /* unpoison_ref_before_marking */ true);
6651 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006652 AddSlowPath(slow_path);
6653
6654 // We have done the "if" of the gray bit check above, now branch based on the flags.
6655 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006656
6657 // Object* ref = ref_addr->AsMirrorPtr()
6658 __ MaybeUnpoisonHeapReference(ref_reg);
6659
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006660 __ Bind(slow_path->GetExitLabel());
6661}
6662
6663void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6664 Location out,
6665 Location ref,
6666 Location obj,
6667 uint32_t offset,
6668 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006669 DCHECK(kEmitCompilerReadBarrier);
6670
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006671 // Insert a slow path based read barrier *after* the reference load.
6672 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006673 // If heap poisoning is enabled, the unpoisoning of the loaded
6674 // reference will be carried out by the runtime within the slow
6675 // path.
6676 //
6677 // Note that `ref` currently does not get unpoisoned (when heap
6678 // poisoning is enabled), which is alright as the `ref` argument is
6679 // not used by the artReadBarrierSlow entry point.
6680 //
6681 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6682 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6683 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6684 AddSlowPath(slow_path);
6685
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 __ jmp(slow_path->GetEntryLabel());
6687 __ Bind(slow_path->GetExitLabel());
6688}
6689
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006690void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6691 Location out,
6692 Location ref,
6693 Location obj,
6694 uint32_t offset,
6695 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006696 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006697 // Baker's read barriers shall be handled by the fast path
6698 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6699 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006700 // If heap poisoning is enabled, unpoisoning will be taken care of
6701 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006702 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703 } else if (kPoisonHeapReferences) {
6704 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6705 }
6706}
6707
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006708void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6709 Location out,
6710 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 DCHECK(kEmitCompilerReadBarrier);
6712
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006713 // Insert a slow path based read barrier *after* the GC root load.
6714 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 // Note that GC roots are not affected by heap poisoning, so we do
6716 // not need to do anything special for this here.
6717 SlowPathCode* slow_path =
6718 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6719 AddSlowPath(slow_path);
6720
Roland Levillain0d5a2812015-11-13 10:07:31 +00006721 __ jmp(slow_path->GetEntryLabel());
6722 __ Bind(slow_path->GetExitLabel());
6723}
6724
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006725void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006726 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006727 LOG(FATAL) << "Unreachable";
6728}
6729
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006730void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006731 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006732 LOG(FATAL) << "Unreachable";
6733}
6734
Mark Mendellfe57faa2015-09-18 09:26:15 -04006735// Simple implementation of packed switch - generate cascaded compare/jumps.
6736void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6737 LocationSummary* locations =
6738 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6739 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006740 locations->AddTemp(Location::RequiresRegister());
6741 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006742}
6743
6744void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6745 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006746 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006747 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006748 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6749 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6750 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006751 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6752
6753 // Should we generate smaller inline compare/jumps?
6754 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6755 // Figure out the correct compare values and jump conditions.
6756 // Handle the first compare/branch as a special case because it might
6757 // jump to the default case.
6758 DCHECK_GT(num_entries, 2u);
6759 Condition first_condition;
6760 uint32_t index;
6761 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6762 if (lower_bound != 0) {
6763 first_condition = kLess;
6764 __ cmpl(value_reg_in, Immediate(lower_bound));
6765 __ j(first_condition, codegen_->GetLabelOf(default_block));
6766 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6767
6768 index = 1;
6769 } else {
6770 // Handle all the compare/jumps below.
6771 first_condition = kBelow;
6772 index = 0;
6773 }
6774
6775 // Handle the rest of the compare/jumps.
6776 for (; index + 1 < num_entries; index += 2) {
6777 int32_t compare_to_value = lower_bound + index + 1;
6778 __ cmpl(value_reg_in, Immediate(compare_to_value));
6779 // Jump to successors[index] if value < case_value[index].
6780 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6781 // Jump to successors[index + 1] if value == case_value[index + 1].
6782 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6783 }
6784
6785 if (index != num_entries) {
6786 // There are an odd number of entries. Handle the last one.
6787 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006788 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006789 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6790 }
6791
6792 // And the default for any other value.
6793 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6794 __ jmp(codegen_->GetLabelOf(default_block));
6795 }
6796 return;
6797 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006798
6799 // Remove the bias, if needed.
6800 Register value_reg_out = value_reg_in.AsRegister();
6801 if (lower_bound != 0) {
6802 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6803 value_reg_out = temp_reg.AsRegister();
6804 }
6805 CpuRegister value_reg(value_reg_out);
6806
6807 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006808 __ cmpl(value_reg, Immediate(num_entries - 1));
6809 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006810
Mark Mendell9c86b482015-09-18 13:36:07 -04006811 // We are in the range of the table.
6812 // Load the address of the jump table in the constant area.
6813 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006814
Mark Mendell9c86b482015-09-18 13:36:07 -04006815 // Load the (signed) offset from the jump table.
6816 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6817
6818 // Add the offset to the address of the table base.
6819 __ addq(temp_reg, base_reg);
6820
6821 // And jump.
6822 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006823}
6824
Aart Bikc5d47542016-01-27 17:00:35 -08006825void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6826 if (value == 0) {
6827 __ xorl(dest, dest);
6828 } else {
6829 __ movl(dest, Immediate(value));
6830 }
6831}
6832
Mark Mendell92e83bf2015-05-07 11:25:03 -04006833void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6834 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006835 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006836 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006837 } else if (IsUint<32>(value)) {
6838 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006839 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6840 } else {
6841 __ movq(dest, Immediate(value));
6842 }
6843}
6844
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006845void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6846 if (value == 0) {
6847 __ xorps(dest, dest);
6848 } else {
6849 __ movss(dest, LiteralInt32Address(value));
6850 }
6851}
6852
6853void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6854 if (value == 0) {
6855 __ xorpd(dest, dest);
6856 } else {
6857 __ movsd(dest, LiteralInt64Address(value));
6858 }
6859}
6860
6861void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6862 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6863}
6864
6865void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6866 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6867}
6868
Aart Bika19616e2016-02-01 18:57:58 -08006869void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6870 if (value == 0) {
6871 __ testl(dest, dest);
6872 } else {
6873 __ cmpl(dest, Immediate(value));
6874 }
6875}
6876
6877void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6878 if (IsInt<32>(value)) {
6879 if (value == 0) {
6880 __ testq(dest, dest);
6881 } else {
6882 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6883 }
6884 } else {
6885 // Value won't fit in an int.
6886 __ cmpq(dest, LiteralInt64Address(value));
6887 }
6888}
6889
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006890void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6891 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006892 GenerateIntCompare(lhs_reg, rhs);
6893}
6894
6895void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006896 if (rhs.IsConstant()) {
6897 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006898 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006899 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006900 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006901 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006902 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006903 }
6904}
6905
6906void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6907 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6908 if (rhs.IsConstant()) {
6909 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6910 Compare64BitValue(lhs_reg, value);
6911 } else if (rhs.IsDoubleStackSlot()) {
6912 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6913 } else {
6914 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6915 }
6916}
6917
6918Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6919 Location index,
6920 ScaleFactor scale,
6921 uint32_t data_offset) {
6922 return index.IsConstant() ?
6923 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6924 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6925}
6926
Mark Mendellcfa410b2015-05-25 16:02:44 -04006927void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6928 DCHECK(dest.IsDoubleStackSlot());
6929 if (IsInt<32>(value)) {
6930 // Can move directly as an int32 constant.
6931 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6932 Immediate(static_cast<int32_t>(value)));
6933 } else {
6934 Load64BitValue(CpuRegister(TMP), value);
6935 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6936 }
6937}
6938
Mark Mendell9c86b482015-09-18 13:36:07 -04006939/**
6940 * Class to handle late fixup of offsets into constant area.
6941 */
6942class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6943 public:
6944 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6945 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6946
6947 protected:
6948 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6949
6950 CodeGeneratorX86_64* codegen_;
6951
6952 private:
6953 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6954 // Patch the correct offset for the instruction. We use the address of the
6955 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6956 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6957 int32_t relative_position = constant_offset - pos;
6958
6959 // Patch in the right value.
6960 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6961 }
6962
6963 // Location in constant area that the fixup refers to.
6964 size_t offset_into_constant_area_;
6965};
6966
6967/**
6968 t * Class to handle late fixup of offsets to a jump table that will be created in the
6969 * constant area.
6970 */
6971class JumpTableRIPFixup : public RIPFixup {
6972 public:
6973 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6974 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6975
6976 void CreateJumpTable() {
6977 X86_64Assembler* assembler = codegen_->GetAssembler();
6978
6979 // Ensure that the reference to the jump table has the correct offset.
6980 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6981 SetOffset(offset_in_constant_table);
6982
6983 // Compute the offset from the start of the function to this jump table.
6984 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6985
6986 // Populate the jump table with the correct values for the jump table.
6987 int32_t num_entries = switch_instr_->GetNumEntries();
6988 HBasicBlock* block = switch_instr_->GetBlock();
6989 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6990 // The value that we want is the target offset - the position of the table.
6991 for (int32_t i = 0; i < num_entries; i++) {
6992 HBasicBlock* b = successors[i];
6993 Label* l = codegen_->GetLabelOf(b);
6994 DCHECK(l->IsBound());
6995 int32_t offset_to_block = l->Position() - current_table_offset;
6996 assembler->AppendInt32(offset_to_block);
6997 }
6998 }
6999
7000 private:
7001 const HPackedSwitch* switch_instr_;
7002};
7003
Mark Mendellf55c3e02015-03-26 21:07:46 -04007004void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7005 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007006 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007007 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7008 // 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 -04007009 assembler->Align(4, 0);
7010 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007011
7012 // Populate any jump tables.
7013 for (auto jump_table : fixups_to_jump_tables_) {
7014 jump_table->CreateJumpTable();
7015 }
7016
7017 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007018 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007019 }
7020
7021 // And finish up.
7022 CodeGenerator::Finalize(allocator);
7023}
7024
Mark Mendellf55c3e02015-03-26 21:07:46 -04007025Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
7026 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7027 return Address::RIP(fixup);
7028}
7029
7030Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
7031 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7032 return Address::RIP(fixup);
7033}
7034
7035Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
7036 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7037 return Address::RIP(fixup);
7038}
7039
7040Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
7041 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7042 return Address::RIP(fixup);
7043}
7044
Andreas Gampe85b62f22015-09-09 13:15:38 -07007045// TODO: trg as memory.
7046void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7047 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007048 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007049 return;
7050 }
7051
7052 DCHECK_NE(type, Primitive::kPrimVoid);
7053
7054 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7055 if (trg.Equals(return_loc)) {
7056 return;
7057 }
7058
7059 // Let the parallel move resolver take care of all of this.
7060 HParallelMove parallel_move(GetGraph()->GetArena());
7061 parallel_move.AddMove(return_loc, trg, type, nullptr);
7062 GetMoveResolver()->EmitNativeCode(&parallel_move);
7063}
7064
Mark Mendell9c86b482015-09-18 13:36:07 -04007065Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7066 // Create a fixup to be used to create and address the jump table.
7067 JumpTableRIPFixup* table_fixup =
7068 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7069
7070 // We have to populate the jump tables.
7071 fixups_to_jump_tables_.push_back(table_fixup);
7072 return Address::RIP(table_fixup);
7073}
7074
Mark Mendellea5af682015-10-22 17:35:49 -04007075void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7076 const Address& addr_high,
7077 int64_t v,
7078 HInstruction* instruction) {
7079 if (IsInt<32>(v)) {
7080 int32_t v_32 = v;
7081 __ movq(addr_low, Immediate(v_32));
7082 MaybeRecordImplicitNullCheck(instruction);
7083 } else {
7084 // Didn't fit in a register. Do it in pieces.
7085 int32_t low_v = Low32Bits(v);
7086 int32_t high_v = High32Bits(v);
7087 __ movl(addr_low, Immediate(low_v));
7088 MaybeRecordImplicitNullCheck(instruction);
7089 __ movl(addr_high, Immediate(high_v));
7090 }
7091}
7092
Roland Levillain4d027112015-07-01 15:41:14 +01007093#undef __
7094
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007095} // namespace x86_64
7096} // namespace art