blob: 9e424adfb7945e45d4abaf937835c0fc56943a1c [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 Geoffray132d8362016-11-16 09:19:42 +00001261 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1262 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001263 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1264}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001265
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001266InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1267 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001268 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001269 assembler_(codegen->GetAssembler()),
1270 codegen_(codegen) {}
1271
David Brazdil58282f42016-01-14 12:45:10 +00001272void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001273 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001274 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001275
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001276 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001277 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001278}
1279
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001280static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001281 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001282}
David Srbecky9d8606d2015-04-12 09:35:32 +01001283
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001284static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001285 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001286}
1287
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001288void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001289 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001290 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001291 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001292 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001293 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001294
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001295 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001296 __ testq(CpuRegister(RAX), Address(
1297 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001298 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001299 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001300
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001301 if (HasEmptyFrame()) {
1302 return;
1303 }
1304
Nicolas Geoffray98893962015-01-21 12:32:32 +00001305 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001306 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001307 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001308 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001309 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1310 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001311 }
1312 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001313
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001314 int adjust = GetFrameSize() - GetCoreSpillSize();
1315 __ subq(CpuRegister(RSP), Immediate(adjust));
1316 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001317 uint32_t xmm_spill_location = GetFpuSpillStart();
1318 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001319
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001320 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1321 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001322 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1323 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1324 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001325 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001326 }
1327
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001328 // Save the current method if we need it. Note that we do not
1329 // do this in HCurrentMethod, as the instruction might have been removed
1330 // in the SSA graph.
1331 if (RequiresCurrentMethod()) {
1332 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
1333 CpuRegister(kMethodRegisterArgument));
1334 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001335}
1336
1337void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001338 __ cfi().RememberState();
1339 if (!HasEmptyFrame()) {
1340 uint32_t xmm_spill_location = GetFpuSpillStart();
1341 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1342 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1343 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1344 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1345 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1346 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1347 }
1348 }
1349
1350 int adjust = GetFrameSize() - GetCoreSpillSize();
1351 __ addq(CpuRegister(RSP), Immediate(adjust));
1352 __ cfi().AdjustCFAOffset(-adjust);
1353
1354 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1355 Register reg = kCoreCalleeSaves[i];
1356 if (allocated_registers_.ContainsCoreRegister(reg)) {
1357 __ popq(CpuRegister(reg));
1358 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1359 __ cfi().Restore(DWARFReg(reg));
1360 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001361 }
1362 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001363 __ ret();
1364 __ cfi().RestoreState();
1365 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001366}
1367
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001368void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1369 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001370}
1371
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001372void CodeGeneratorX86_64::Move(Location destination, Location source) {
1373 if (source.Equals(destination)) {
1374 return;
1375 }
1376 if (destination.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001377 CpuRegister dest = destination.AsRegister<CpuRegister>();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001378 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001379 __ movq(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001380 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001381 __ movd(dest, source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001382 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001383 __ movl(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
1384 } else if (source.IsConstant()) {
1385 HConstant* constant = source.GetConstant();
1386 if (constant->IsLongConstant()) {
1387 Load64BitValue(dest, constant->AsLongConstant()->GetValue());
1388 } else {
1389 Load32BitValue(dest, GetInt32ValueOf(constant));
1390 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001391 } else {
1392 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001393 __ movq(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001394 }
1395 } else if (destination.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001396 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001397 if (source.IsRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001398 __ movd(dest, source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001399 } else if (source.IsFpuRegister()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001400 __ movaps(dest, source.AsFpuRegister<XmmRegister>());
1401 } else if (source.IsConstant()) {
1402 HConstant* constant = source.GetConstant();
1403 int64_t value = CodeGenerator::GetInt64ValueOf(constant);
1404 if (constant->IsFloatConstant()) {
1405 Load32BitValue(dest, static_cast<int32_t>(value));
1406 } else {
1407 Load64BitValue(dest, value);
1408 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001409 } else if (source.IsStackSlot()) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001410 __ movss(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001411 } else {
1412 DCHECK(source.IsDoubleStackSlot());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001413 __ movsd(dest, Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001414 }
1415 } else if (destination.IsStackSlot()) {
1416 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001417 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001418 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001419 } else if (source.IsFpuRegister()) {
1420 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001421 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001422 } else if (source.IsConstant()) {
1423 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001424 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001425 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001426 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001427 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001428 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1429 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001430 }
1431 } else {
1432 DCHECK(destination.IsDoubleStackSlot());
1433 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001434 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001435 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001436 } else if (source.IsFpuRegister()) {
1437 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001438 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001439 } else if (source.IsConstant()) {
1440 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001441 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1442 int64_t value = GetInt64ValueOf(constant);
Mark Mendellcfa410b2015-05-25 16:02:44 -04001443 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001444 } else {
1445 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001446 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1447 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001448 }
1449 }
1450}
1451
Calin Juravle175dc732015-08-25 15:42:32 +01001452void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1453 DCHECK(location.IsRegister());
1454 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1455}
1456
Calin Juravlee460d1d2015-09-29 04:52:17 +01001457void CodeGeneratorX86_64::MoveLocation(
1458 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1459 Move(dst, src);
1460}
1461
1462void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1463 if (location.IsRegister()) {
1464 locations->AddTemp(location);
1465 } else {
1466 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1467 }
1468}
1469
David Brazdilfc6a86a2015-06-26 10:33:45 +00001470void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001471 DCHECK(!successor->IsExitBlock());
1472
1473 HBasicBlock* block = got->GetBlock();
1474 HInstruction* previous = got->GetPrevious();
1475
1476 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001477 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001478 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1479 return;
1480 }
1481
1482 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1483 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1484 }
1485 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001486 __ jmp(codegen_->GetLabelOf(successor));
1487 }
1488}
1489
David Brazdilfc6a86a2015-06-26 10:33:45 +00001490void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1491 got->SetLocations(nullptr);
1492}
1493
1494void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1495 HandleGoto(got, got->GetSuccessor());
1496}
1497
1498void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1499 try_boundary->SetLocations(nullptr);
1500}
1501
1502void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1503 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1504 if (!successor->IsExitBlock()) {
1505 HandleGoto(try_boundary, successor);
1506 }
1507}
1508
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001509void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1510 exit->SetLocations(nullptr);
1511}
1512
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001513void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001514}
1515
Mark Mendell152408f2015-12-31 12:28:50 -05001516template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001517void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001518 LabelType* true_label,
1519 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001520 if (cond->IsFPConditionTrueIfNaN()) {
1521 __ j(kUnordered, true_label);
1522 } else if (cond->IsFPConditionFalseIfNaN()) {
1523 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001524 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001525 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001526}
1527
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001528void InstructionCodeGeneratorX86_64::GenerateCompareTest(HCondition* condition) {
Mark Mendellc4701932015-04-10 13:18:51 -04001529 LocationSummary* locations = condition->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001530
Mark Mendellc4701932015-04-10 13:18:51 -04001531 Location left = locations->InAt(0);
1532 Location right = locations->InAt(1);
Mark Mendellc4701932015-04-10 13:18:51 -04001533 Primitive::Type type = condition->InputAt(0)->GetType();
1534 switch (type) {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001535 case Primitive::kPrimBoolean:
1536 case Primitive::kPrimByte:
1537 case Primitive::kPrimChar:
1538 case Primitive::kPrimShort:
1539 case Primitive::kPrimInt:
1540 case Primitive::kPrimNot: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001541 codegen_->GenerateIntCompare(left, right);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001542 break;
1543 }
Mark Mendellc4701932015-04-10 13:18:51 -04001544 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001545 codegen_->GenerateLongCompare(left, right);
Mark Mendellc4701932015-04-10 13:18:51 -04001546 break;
1547 }
1548 case Primitive::kPrimFloat: {
1549 if (right.IsFpuRegister()) {
1550 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1551 } else if (right.IsConstant()) {
1552 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1553 codegen_->LiteralFloatAddress(
1554 right.GetConstant()->AsFloatConstant()->GetValue()));
1555 } else {
1556 DCHECK(right.IsStackSlot());
1557 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1558 Address(CpuRegister(RSP), right.GetStackIndex()));
1559 }
Mark Mendellc4701932015-04-10 13:18:51 -04001560 break;
1561 }
1562 case Primitive::kPrimDouble: {
1563 if (right.IsFpuRegister()) {
1564 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1565 } else if (right.IsConstant()) {
1566 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1567 codegen_->LiteralDoubleAddress(
1568 right.GetConstant()->AsDoubleConstant()->GetValue()));
1569 } else {
1570 DCHECK(right.IsDoubleStackSlot());
1571 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1572 Address(CpuRegister(RSP), right.GetStackIndex()));
1573 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001574 break;
1575 }
1576 default:
1577 LOG(FATAL) << "Unexpected condition type " << type;
1578 }
1579}
1580
1581template<class LabelType>
1582void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1583 LabelType* true_target_in,
1584 LabelType* false_target_in) {
1585 // Generated branching requires both targets to be explicit. If either of the
1586 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1587 LabelType fallthrough_target;
1588 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1589 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1590
1591 // Generate the comparison to set the CC.
1592 GenerateCompareTest(condition);
1593
1594 // Now generate the correct jump(s).
1595 Primitive::Type type = condition->InputAt(0)->GetType();
1596 switch (type) {
1597 case Primitive::kPrimLong: {
1598 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
1599 break;
1600 }
1601 case Primitive::kPrimFloat: {
1602 GenerateFPJumps(condition, true_target, false_target);
1603 break;
1604 }
1605 case Primitive::kPrimDouble: {
Mark Mendellc4701932015-04-10 13:18:51 -04001606 GenerateFPJumps(condition, true_target, false_target);
1607 break;
1608 }
1609 default:
1610 LOG(FATAL) << "Unexpected condition type " << type;
1611 }
1612
David Brazdil0debae72015-11-12 18:37:00 +00001613 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001614 __ jmp(false_target);
1615 }
David Brazdil0debae72015-11-12 18:37:00 +00001616
1617 if (fallthrough_target.IsLinked()) {
1618 __ Bind(&fallthrough_target);
1619 }
Mark Mendellc4701932015-04-10 13:18:51 -04001620}
1621
David Brazdil0debae72015-11-12 18:37:00 +00001622static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1623 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1624 // are set only strictly before `branch`. We can't use the eflags on long
1625 // conditions if they are materialized due to the complex branching.
1626 return cond->IsCondition() &&
1627 cond->GetNext() == branch &&
1628 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1629}
1630
Mark Mendell152408f2015-12-31 12:28:50 -05001631template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001632void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001633 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001634 LabelType* true_target,
1635 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001636 HInstruction* cond = instruction->InputAt(condition_input_index);
1637
1638 if (true_target == nullptr && false_target == nullptr) {
1639 // Nothing to do. The code always falls through.
1640 return;
1641 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001642 // Constant condition, statically compared against "true" (integer value 1).
1643 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001644 if (true_target != nullptr) {
1645 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001646 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001647 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001648 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001649 if (false_target != nullptr) {
1650 __ jmp(false_target);
1651 }
1652 }
1653 return;
1654 }
1655
1656 // The following code generates these patterns:
1657 // (1) true_target == nullptr && false_target != nullptr
1658 // - opposite condition true => branch to false_target
1659 // (2) true_target != nullptr && false_target == nullptr
1660 // - condition true => branch to true_target
1661 // (3) true_target != nullptr && false_target != nullptr
1662 // - condition true => branch to true_target
1663 // - branch to false_target
1664 if (IsBooleanValueOrMaterializedCondition(cond)) {
1665 if (AreEflagsSetFrom(cond, instruction)) {
1666 if (true_target == nullptr) {
1667 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1668 } else {
1669 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1670 }
1671 } else {
1672 // Materialized condition, compare against 0.
1673 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1674 if (lhs.IsRegister()) {
1675 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1676 } else {
1677 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1678 }
1679 if (true_target == nullptr) {
1680 __ j(kEqual, false_target);
1681 } else {
1682 __ j(kNotEqual, true_target);
1683 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001684 }
1685 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001686 // Condition has not been materialized, use its inputs as the
1687 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001688 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001689
David Brazdil0debae72015-11-12 18:37:00 +00001690 // If this is a long or FP comparison that has been folded into
1691 // the HCondition, generate the comparison directly.
1692 Primitive::Type type = condition->InputAt(0)->GetType();
1693 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1694 GenerateCompareTestAndBranch(condition, true_target, false_target);
1695 return;
1696 }
1697
1698 Location lhs = condition->GetLocations()->InAt(0);
1699 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001700 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001701 if (true_target == nullptr) {
1702 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1703 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001704 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001705 }
Dave Allison20dfc792014-06-16 20:44:29 -07001706 }
David Brazdil0debae72015-11-12 18:37:00 +00001707
1708 // If neither branch falls through (case 3), the conditional branch to `true_target`
1709 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1710 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001711 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001712 }
1713}
1714
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001715void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001716 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1717 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001718 locations->SetInAt(0, Location::Any());
1719 }
1720}
1721
1722void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001723 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1724 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1725 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1726 nullptr : codegen_->GetLabelOf(true_successor);
1727 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1728 nullptr : codegen_->GetLabelOf(false_successor);
1729 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001730}
1731
1732void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1733 LocationSummary* locations = new (GetGraph()->GetArena())
1734 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001735 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001736 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001737 locations->SetInAt(0, Location::Any());
1738 }
1739}
1740
1741void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001742 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001743 GenerateTestAndBranch<Label>(deoptimize,
1744 /* condition_input_index */ 0,
1745 slow_path->GetEntryLabel(),
1746 /* false_target */ nullptr);
1747}
1748
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001749static bool SelectCanUseCMOV(HSelect* select) {
1750 // There are no conditional move instructions for XMMs.
1751 if (Primitive::IsFloatingPointType(select->GetType())) {
1752 return false;
1753 }
1754
1755 // A FP condition doesn't generate the single CC that we need.
1756 HInstruction* condition = select->GetCondition();
1757 if (condition->IsCondition() &&
1758 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1759 return false;
1760 }
1761
1762 // We can generate a CMOV for this Select.
1763 return true;
1764}
1765
David Brazdil74eb1b22015-12-14 11:44:01 +00001766void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1767 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1768 if (Primitive::IsFloatingPointType(select->GetType())) {
1769 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001770 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001771 } else {
1772 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001773 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001774 if (select->InputAt(1)->IsConstant()) {
1775 locations->SetInAt(1, Location::RequiresRegister());
1776 } else {
1777 locations->SetInAt(1, Location::Any());
1778 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001779 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001780 locations->SetInAt(1, Location::Any());
1781 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001782 }
1783 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1784 locations->SetInAt(2, Location::RequiresRegister());
1785 }
1786 locations->SetOut(Location::SameAsFirstInput());
1787}
1788
1789void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1790 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001791 if (SelectCanUseCMOV(select)) {
1792 // If both the condition and the source types are integer, we can generate
1793 // a CMOV to implement Select.
1794 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001795 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001796 DCHECK(locations->InAt(0).Equals(locations->Out()));
1797
1798 HInstruction* select_condition = select->GetCondition();
1799 Condition cond = kNotEqual;
1800
1801 // Figure out how to test the 'condition'.
1802 if (select_condition->IsCondition()) {
1803 HCondition* condition = select_condition->AsCondition();
1804 if (!condition->IsEmittedAtUseSite()) {
1805 // This was a previously materialized condition.
1806 // Can we use the existing condition code?
1807 if (AreEflagsSetFrom(condition, select)) {
1808 // Materialization was the previous instruction. Condition codes are right.
1809 cond = X86_64IntegerCondition(condition->GetCondition());
1810 } else {
1811 // No, we have to recreate the condition code.
1812 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1813 __ testl(cond_reg, cond_reg);
1814 }
1815 } else {
1816 GenerateCompareTest(condition);
1817 cond = X86_64IntegerCondition(condition->GetCondition());
1818 }
1819 } else {
1820 // Must be a boolean condition, which needs to be compared to 0.
1821 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1822 __ testl(cond_reg, cond_reg);
1823 }
1824
1825 // If the condition is true, overwrite the output, which already contains false.
1826 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001827 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1828 if (value_true_loc.IsRegister()) {
1829 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1830 } else {
1831 __ cmov(cond,
1832 value_false,
1833 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1834 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001835 } else {
1836 NearLabel false_target;
1837 GenerateTestAndBranch<NearLabel>(select,
1838 /* condition_input_index */ 2,
1839 /* true_target */ nullptr,
1840 &false_target);
1841 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1842 __ Bind(&false_target);
1843 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001844}
1845
David Srbecky0cf44932015-12-09 14:09:59 +00001846void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1847 new (GetGraph()->GetArena()) LocationSummary(info);
1848}
1849
David Srbeckyd28f4a02016-03-14 17:14:24 +00001850void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1851 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001852}
1853
1854void CodeGeneratorX86_64::GenerateNop() {
1855 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001856}
1857
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001858void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001859 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001860 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001861 // Handle the long/FP comparisons made in instruction simplification.
1862 switch (cond->InputAt(0)->GetType()) {
1863 case Primitive::kPrimLong:
1864 locations->SetInAt(0, Location::RequiresRegister());
1865 locations->SetInAt(1, Location::Any());
1866 break;
1867 case Primitive::kPrimFloat:
1868 case Primitive::kPrimDouble:
1869 locations->SetInAt(0, Location::RequiresFpuRegister());
1870 locations->SetInAt(1, Location::Any());
1871 break;
1872 default:
1873 locations->SetInAt(0, Location::RequiresRegister());
1874 locations->SetInAt(1, Location::Any());
1875 break;
1876 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001877 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001878 locations->SetOut(Location::RequiresRegister());
1879 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001880}
1881
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001882void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001883 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001884 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001885 }
Mark Mendellc4701932015-04-10 13:18:51 -04001886
1887 LocationSummary* locations = cond->GetLocations();
1888 Location lhs = locations->InAt(0);
1889 Location rhs = locations->InAt(1);
1890 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001891 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001892
1893 switch (cond->InputAt(0)->GetType()) {
1894 default:
1895 // Integer case.
1896
1897 // Clear output register: setcc only sets the low byte.
1898 __ xorl(reg, reg);
1899
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001900 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001901 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001902 return;
1903 case Primitive::kPrimLong:
1904 // Clear output register: setcc only sets the low byte.
1905 __ xorl(reg, reg);
1906
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001907 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001908 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001909 return;
1910 case Primitive::kPrimFloat: {
1911 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1912 if (rhs.IsConstant()) {
1913 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1914 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1915 } else if (rhs.IsStackSlot()) {
1916 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1917 } else {
1918 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1919 }
1920 GenerateFPJumps(cond, &true_label, &false_label);
1921 break;
1922 }
1923 case Primitive::kPrimDouble: {
1924 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1925 if (rhs.IsConstant()) {
1926 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1927 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1928 } else if (rhs.IsDoubleStackSlot()) {
1929 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1930 } else {
1931 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1932 }
1933 GenerateFPJumps(cond, &true_label, &false_label);
1934 break;
1935 }
1936 }
1937
1938 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001939 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001940
Roland Levillain4fa13f62015-07-06 18:11:54 +01001941 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001942 __ Bind(&false_label);
1943 __ xorl(reg, reg);
1944 __ jmp(&done_label);
1945
Roland Levillain4fa13f62015-07-06 18:11:54 +01001946 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001947 __ Bind(&true_label);
1948 __ movl(reg, Immediate(1));
1949 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001950}
1951
1952void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001954}
1955
1956void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001957 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001958}
1959
1960void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001962}
1963
1964void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001966}
1967
1968void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001970}
1971
1972void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001973 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001974}
1975
1976void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001978}
1979
1980void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001982}
1983
1984void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001985 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001986}
1987
1988void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001989 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001990}
1991
1992void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001993 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001994}
1995
1996void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001997 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001998}
1999
Aart Bike9f37602015-10-09 11:15:55 -07002000void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002001 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002002}
2003
2004void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002005 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002006}
2007
2008void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002009 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002010}
2011
2012void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002013 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002014}
2015
2016void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002017 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002018}
2019
2020void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002021 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002022}
2023
2024void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002025 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002026}
2027
2028void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002029 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002030}
2031
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002032void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002033 LocationSummary* locations =
2034 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002035 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002036 case Primitive::kPrimBoolean:
2037 case Primitive::kPrimByte:
2038 case Primitive::kPrimShort:
2039 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002040 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00002041 case Primitive::kPrimLong: {
2042 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002043 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2045 break;
2046 }
2047 case Primitive::kPrimFloat:
2048 case Primitive::kPrimDouble: {
2049 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002050 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002051 locations->SetOut(Location::RequiresRegister());
2052 break;
2053 }
2054 default:
2055 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2056 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002057}
2058
2059void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002060 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002061 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002062 Location left = locations->InAt(0);
2063 Location right = locations->InAt(1);
2064
Mark Mendell0c9497d2015-08-21 09:30:05 -04002065 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00002066 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002067 Condition less_cond = kLess;
2068
Calin Juravleddb7df22014-11-25 20:56:51 +00002069 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002070 case Primitive::kPrimBoolean:
2071 case Primitive::kPrimByte:
2072 case Primitive::kPrimShort:
2073 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002074 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002075 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002076 break;
2077 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002078 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002079 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002080 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002081 }
2082 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04002083 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2084 if (right.IsConstant()) {
2085 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2086 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2087 } else if (right.IsStackSlot()) {
2088 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2089 } else {
2090 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
2091 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002092 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002093 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002094 break;
2095 }
2096 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04002097 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2098 if (right.IsConstant()) {
2099 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2100 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2101 } else if (right.IsDoubleStackSlot()) {
2102 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2103 } else {
2104 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2105 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002106 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002107 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002108 break;
2109 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002110 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002111 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002112 }
Aart Bika19616e2016-02-01 18:57:58 -08002113
Calin Juravleddb7df22014-11-25 20:56:51 +00002114 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002115 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002116 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002117
Calin Juravle91debbc2014-11-26 19:01:09 +00002118 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002119 __ movl(out, Immediate(1));
2120 __ jmp(&done);
2121
2122 __ Bind(&less);
2123 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002124
2125 __ Bind(&done);
2126}
2127
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002128void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002129 LocationSummary* locations =
2130 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002131 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002132}
2133
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002134void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002135 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002136}
2137
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002138void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2139 LocationSummary* locations =
2140 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2141 locations->SetOut(Location::ConstantLocation(constant));
2142}
2143
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002144void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002145 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002146}
2147
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002148void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002149 LocationSummary* locations =
2150 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002151 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002152}
2153
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002154void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002155 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002156}
2157
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002158void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2159 LocationSummary* locations =
2160 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2161 locations->SetOut(Location::ConstantLocation(constant));
2162}
2163
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002164void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002165 // Will be generated at use site.
2166}
2167
2168void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2169 LocationSummary* locations =
2170 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2171 locations->SetOut(Location::ConstantLocation(constant));
2172}
2173
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002174void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2175 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002176 // Will be generated at use site.
2177}
2178
Calin Juravle27df7582015-04-17 19:12:31 +01002179void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2180 memory_barrier->SetLocations(nullptr);
2181}
2182
2183void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002184 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002185}
2186
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002187void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2188 ret->SetLocations(nullptr);
2189}
2190
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002191void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002192 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002193}
2194
2195void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002196 LocationSummary* locations =
2197 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002198 switch (ret->InputAt(0)->GetType()) {
2199 case Primitive::kPrimBoolean:
2200 case Primitive::kPrimByte:
2201 case Primitive::kPrimChar:
2202 case Primitive::kPrimShort:
2203 case Primitive::kPrimInt:
2204 case Primitive::kPrimNot:
2205 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002206 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002207 break;
2208
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002209 case Primitive::kPrimFloat:
2210 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04002211 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002212 break;
2213
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002214 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002215 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002216 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002217}
2218
2219void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2220 if (kIsDebugBuild) {
2221 switch (ret->InputAt(0)->GetType()) {
2222 case Primitive::kPrimBoolean:
2223 case Primitive::kPrimByte:
2224 case Primitive::kPrimChar:
2225 case Primitive::kPrimShort:
2226 case Primitive::kPrimInt:
2227 case Primitive::kPrimNot:
2228 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002229 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002230 break;
2231
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002232 case Primitive::kPrimFloat:
2233 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002235 XMM0);
2236 break;
2237
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002238 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002239 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002240 }
2241 }
2242 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002243}
2244
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002245Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2246 switch (type) {
2247 case Primitive::kPrimBoolean:
2248 case Primitive::kPrimByte:
2249 case Primitive::kPrimChar:
2250 case Primitive::kPrimShort:
2251 case Primitive::kPrimInt:
2252 case Primitive::kPrimNot:
2253 case Primitive::kPrimLong:
2254 return Location::RegisterLocation(RAX);
2255
2256 case Primitive::kPrimVoid:
2257 return Location::NoLocation();
2258
2259 case Primitive::kPrimDouble:
2260 case Primitive::kPrimFloat:
2261 return Location::FpuRegisterLocation(XMM0);
2262 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002263
2264 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002265}
2266
2267Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2268 return Location::RegisterLocation(kMethodRegisterArgument);
2269}
2270
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002271Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002272 switch (type) {
2273 case Primitive::kPrimBoolean:
2274 case Primitive::kPrimByte:
2275 case Primitive::kPrimChar:
2276 case Primitive::kPrimShort:
2277 case Primitive::kPrimInt:
2278 case Primitive::kPrimNot: {
2279 uint32_t index = gp_index_++;
2280 stack_index_++;
2281 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002282 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002283 } else {
2284 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2285 }
2286 }
2287
2288 case Primitive::kPrimLong: {
2289 uint32_t index = gp_index_;
2290 stack_index_ += 2;
2291 if (index < calling_convention.GetNumberOfRegisters()) {
2292 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002293 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002294 } else {
2295 gp_index_ += 2;
2296 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2297 }
2298 }
2299
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002300 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002301 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002302 stack_index_++;
2303 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002304 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002305 } else {
2306 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2307 }
2308 }
2309
2310 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002311 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002312 stack_index_ += 2;
2313 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002314 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002315 } else {
2316 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2317 }
2318 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002319
2320 case Primitive::kPrimVoid:
2321 LOG(FATAL) << "Unexpected parameter type " << type;
2322 break;
2323 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002324 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002325}
2326
Calin Juravle175dc732015-08-25 15:42:32 +01002327void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2328 // The trampoline uses the same calling convention as dex calling conventions,
2329 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2330 // the method_idx.
2331 HandleInvoke(invoke);
2332}
2333
2334void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2335 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2336}
2337
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002338void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002339 // Explicit clinit checks triggered by static invokes must have been pruned by
2340 // art::PrepareForRegisterAllocation.
2341 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002342
Mark Mendellfb8d2792015-03-31 22:16:59 -04002343 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002344 if (intrinsic.TryDispatch(invoke)) {
2345 return;
2346 }
2347
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002348 HandleInvoke(invoke);
2349}
2350
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002351static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2352 if (invoke->GetLocations()->Intrinsified()) {
2353 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2354 intrinsic.Dispatch(invoke);
2355 return true;
2356 }
2357 return false;
2358}
2359
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002360void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002361 // Explicit clinit checks triggered by static invokes must have been pruned by
2362 // art::PrepareForRegisterAllocation.
2363 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002364
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002365 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2366 return;
2367 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002368
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002369 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002370 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002371 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002372 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002373}
2374
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002375void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002376 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002377 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002378}
2379
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002380void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002381 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002382 if (intrinsic.TryDispatch(invoke)) {
2383 return;
2384 }
2385
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002386 HandleInvoke(invoke);
2387}
2388
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002389void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002390 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2391 return;
2392 }
2393
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002394 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002395 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002396 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002397}
2398
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002399void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2400 HandleInvoke(invoke);
2401 // Add the hidden argument.
2402 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2403}
2404
2405void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2406 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002407 LocationSummary* locations = invoke->GetLocations();
2408 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2409 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002410 Location receiver = locations->InAt(0);
2411 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2412
Roland Levillain0d5a2812015-11-13 10:07:31 +00002413 // Set the hidden argument. This is safe to do this here, as RAX
2414 // won't be modified thereafter, before the `call` instruction.
2415 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002416 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002417
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002418 if (receiver.IsStackSlot()) {
2419 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002420 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002421 __ movl(temp, Address(temp, class_offset));
2422 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002423 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002424 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002425 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002426 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002427 // Instead of simply (possibly) unpoisoning `temp` here, we should
2428 // emit a read barrier for the previous class reference load.
2429 // However this is not required in practice, as this is an
2430 // intermediate/temporary reference and because the current
2431 // concurrent copying collector keeps the from-space memory
2432 // intact/accessible until the end of the marking phase (the
2433 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002434 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002435 // temp = temp->GetAddressOfIMT()
2436 __ movq(temp,
2437 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2438 // temp = temp->GetImtEntryAt(method_offset);
2439 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002440 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002441 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002442 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002443 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002444 __ call(Address(
2445 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002446
2447 DCHECK(!codegen_->IsLeafMethod());
2448 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2449}
2450
Roland Levillain88cb1752014-10-20 16:36:47 +01002451void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2452 LocationSummary* locations =
2453 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2454 switch (neg->GetResultType()) {
2455 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002456 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002457 locations->SetInAt(0, Location::RequiresRegister());
2458 locations->SetOut(Location::SameAsFirstInput());
2459 break;
2460
Roland Levillain88cb1752014-10-20 16:36:47 +01002461 case Primitive::kPrimFloat:
2462 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002463 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002464 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002465 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002466 break;
2467
2468 default:
2469 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2470 }
2471}
2472
2473void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2474 LocationSummary* locations = neg->GetLocations();
2475 Location out = locations->Out();
2476 Location in = locations->InAt(0);
2477 switch (neg->GetResultType()) {
2478 case Primitive::kPrimInt:
2479 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002480 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002481 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002482 break;
2483
2484 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002485 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002486 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002487 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002488 break;
2489
Roland Levillain5368c212014-11-27 15:03:41 +00002490 case Primitive::kPrimFloat: {
2491 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002492 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002493 // Implement float negation with an exclusive or with value
2494 // 0x80000000 (mask for bit 31, representing the sign of a
2495 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002496 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002497 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002498 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002499 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002500
Roland Levillain5368c212014-11-27 15:03:41 +00002501 case Primitive::kPrimDouble: {
2502 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002503 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002504 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002505 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002506 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002507 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002508 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002509 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002510 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002511
2512 default:
2513 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2514 }
2515}
2516
Roland Levillaindff1f282014-11-05 14:15:05 +00002517void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2518 LocationSummary* locations =
2519 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2520 Primitive::Type result_type = conversion->GetResultType();
2521 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002522 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002523
David Brazdilb2bd1c52015-03-25 11:17:37 +00002524 // The Java language does not allow treating boolean as an integral type but
2525 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002526
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002528 case Primitive::kPrimByte:
2529 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002530 case Primitive::kPrimLong:
2531 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002532 case Primitive::kPrimBoolean:
2533 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002534 case Primitive::kPrimShort:
2535 case Primitive::kPrimInt:
2536 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002537 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002538 locations->SetInAt(0, Location::Any());
2539 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2540 break;
2541
2542 default:
2543 LOG(FATAL) << "Unexpected type conversion from " << input_type
2544 << " to " << result_type;
2545 }
2546 break;
2547
Roland Levillain01a8d712014-11-14 16:27:39 +00002548 case Primitive::kPrimShort:
2549 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002550 case Primitive::kPrimLong:
2551 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002552 case Primitive::kPrimBoolean:
2553 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002554 case Primitive::kPrimByte:
2555 case Primitive::kPrimInt:
2556 case Primitive::kPrimChar:
2557 // Processing a Dex `int-to-short' instruction.
2558 locations->SetInAt(0, Location::Any());
2559 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2560 break;
2561
2562 default:
2563 LOG(FATAL) << "Unexpected type conversion from " << input_type
2564 << " to " << result_type;
2565 }
2566 break;
2567
Roland Levillain946e1432014-11-11 17:35:19 +00002568 case Primitive::kPrimInt:
2569 switch (input_type) {
2570 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002571 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002572 locations->SetInAt(0, Location::Any());
2573 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2574 break;
2575
2576 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002577 // Processing a Dex `float-to-int' instruction.
2578 locations->SetInAt(0, Location::RequiresFpuRegister());
2579 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002580 break;
2581
Roland Levillain946e1432014-11-11 17:35:19 +00002582 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002583 // Processing a Dex `double-to-int' instruction.
2584 locations->SetInAt(0, Location::RequiresFpuRegister());
2585 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002586 break;
2587
2588 default:
2589 LOG(FATAL) << "Unexpected type conversion from " << input_type
2590 << " to " << result_type;
2591 }
2592 break;
2593
Roland Levillaindff1f282014-11-05 14:15:05 +00002594 case Primitive::kPrimLong:
2595 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002596 case Primitive::kPrimBoolean:
2597 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002598 case Primitive::kPrimByte:
2599 case Primitive::kPrimShort:
2600 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002601 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002602 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002603 // TODO: We would benefit from a (to-be-implemented)
2604 // Location::RegisterOrStackSlot requirement for this input.
2605 locations->SetInAt(0, Location::RequiresRegister());
2606 locations->SetOut(Location::RequiresRegister());
2607 break;
2608
2609 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002610 // Processing a Dex `float-to-long' instruction.
2611 locations->SetInAt(0, Location::RequiresFpuRegister());
2612 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002613 break;
2614
Roland Levillaindff1f282014-11-05 14:15:05 +00002615 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002616 // Processing a Dex `double-to-long' instruction.
2617 locations->SetInAt(0, Location::RequiresFpuRegister());
2618 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002619 break;
2620
2621 default:
2622 LOG(FATAL) << "Unexpected type conversion from " << input_type
2623 << " to " << result_type;
2624 }
2625 break;
2626
Roland Levillain981e4542014-11-14 11:47:14 +00002627 case Primitive::kPrimChar:
2628 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002629 case Primitive::kPrimLong:
2630 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002631 case Primitive::kPrimBoolean:
2632 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002633 case Primitive::kPrimByte:
2634 case Primitive::kPrimShort:
2635 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002636 // Processing a Dex `int-to-char' instruction.
2637 locations->SetInAt(0, Location::Any());
2638 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2639 break;
2640
2641 default:
2642 LOG(FATAL) << "Unexpected type conversion from " << input_type
2643 << " to " << result_type;
2644 }
2645 break;
2646
Roland Levillaindff1f282014-11-05 14:15:05 +00002647 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002648 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002649 case Primitive::kPrimBoolean:
2650 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002651 case Primitive::kPrimByte:
2652 case Primitive::kPrimShort:
2653 case Primitive::kPrimInt:
2654 case Primitive::kPrimChar:
2655 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002656 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002657 locations->SetOut(Location::RequiresFpuRegister());
2658 break;
2659
2660 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002661 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002662 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002663 locations->SetOut(Location::RequiresFpuRegister());
2664 break;
2665
Roland Levillaincff13742014-11-17 14:32:17 +00002666 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002667 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002668 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002669 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002670 break;
2671
2672 default:
2673 LOG(FATAL) << "Unexpected type conversion from " << input_type
2674 << " to " << result_type;
2675 };
2676 break;
2677
Roland Levillaindff1f282014-11-05 14:15:05 +00002678 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002679 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002680 case Primitive::kPrimBoolean:
2681 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002682 case Primitive::kPrimByte:
2683 case Primitive::kPrimShort:
2684 case Primitive::kPrimInt:
2685 case Primitive::kPrimChar:
2686 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002687 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002688 locations->SetOut(Location::RequiresFpuRegister());
2689 break;
2690
2691 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002692 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002693 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002694 locations->SetOut(Location::RequiresFpuRegister());
2695 break;
2696
Roland Levillaincff13742014-11-17 14:32:17 +00002697 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002698 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002699 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002700 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002701 break;
2702
2703 default:
2704 LOG(FATAL) << "Unexpected type conversion from " << input_type
2705 << " to " << result_type;
2706 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002707 break;
2708
2709 default:
2710 LOG(FATAL) << "Unexpected type conversion from " << input_type
2711 << " to " << result_type;
2712 }
2713}
2714
2715void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2716 LocationSummary* locations = conversion->GetLocations();
2717 Location out = locations->Out();
2718 Location in = locations->InAt(0);
2719 Primitive::Type result_type = conversion->GetResultType();
2720 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002721 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002722 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002723 case Primitive::kPrimByte:
2724 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002725 case Primitive::kPrimLong:
2726 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002727 case Primitive::kPrimBoolean:
2728 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002729 case Primitive::kPrimShort:
2730 case Primitive::kPrimInt:
2731 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002732 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002733 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002735 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002736 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002737 Address(CpuRegister(RSP), in.GetStackIndex()));
2738 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002739 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002740 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002741 }
2742 break;
2743
2744 default:
2745 LOG(FATAL) << "Unexpected type conversion from " << input_type
2746 << " to " << result_type;
2747 }
2748 break;
2749
Roland Levillain01a8d712014-11-14 16:27:39 +00002750 case Primitive::kPrimShort:
2751 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002752 case Primitive::kPrimLong:
2753 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002754 case Primitive::kPrimBoolean:
2755 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002756 case Primitive::kPrimByte:
2757 case Primitive::kPrimInt:
2758 case Primitive::kPrimChar:
2759 // Processing a Dex `int-to-short' instruction.
2760 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002761 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002762 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002763 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002764 Address(CpuRegister(RSP), in.GetStackIndex()));
2765 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002766 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002767 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002768 }
2769 break;
2770
2771 default:
2772 LOG(FATAL) << "Unexpected type conversion from " << input_type
2773 << " to " << result_type;
2774 }
2775 break;
2776
Roland Levillain946e1432014-11-11 17:35:19 +00002777 case Primitive::kPrimInt:
2778 switch (input_type) {
2779 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002780 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002781 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002782 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002783 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002785 Address(CpuRegister(RSP), in.GetStackIndex()));
2786 } else {
2787 DCHECK(in.IsConstant());
2788 DCHECK(in.GetConstant()->IsLongConstant());
2789 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002790 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002791 }
2792 break;
2793
Roland Levillain3f8f9362014-12-02 17:45:01 +00002794 case Primitive::kPrimFloat: {
2795 // Processing a Dex `float-to-int' instruction.
2796 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2797 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002798 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002799
2800 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002801 // if input >= (float)INT_MAX goto done
2802 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002803 __ j(kAboveEqual, &done);
2804 // if input == NaN goto nan
2805 __ j(kUnordered, &nan);
2806 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002807 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002808 __ jmp(&done);
2809 __ Bind(&nan);
2810 // output = 0
2811 __ xorl(output, output);
2812 __ Bind(&done);
2813 break;
2814 }
2815
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002816 case Primitive::kPrimDouble: {
2817 // Processing a Dex `double-to-int' instruction.
2818 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2819 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002820 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002821
2822 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002823 // if input >= (double)INT_MAX goto done
2824 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002825 __ j(kAboveEqual, &done);
2826 // if input == NaN goto nan
2827 __ j(kUnordered, &nan);
2828 // output = double-to-int-truncate(input)
2829 __ cvttsd2si(output, input);
2830 __ jmp(&done);
2831 __ Bind(&nan);
2832 // output = 0
2833 __ xorl(output, output);
2834 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002835 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002836 }
Roland Levillain946e1432014-11-11 17:35:19 +00002837
2838 default:
2839 LOG(FATAL) << "Unexpected type conversion from " << input_type
2840 << " to " << result_type;
2841 }
2842 break;
2843
Roland Levillaindff1f282014-11-05 14:15:05 +00002844 case Primitive::kPrimLong:
2845 switch (input_type) {
2846 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002847 case Primitive::kPrimBoolean:
2848 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002849 case Primitive::kPrimByte:
2850 case Primitive::kPrimShort:
2851 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002852 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002853 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002854 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002855 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002856 break;
2857
Roland Levillain624279f2014-12-04 11:54:28 +00002858 case Primitive::kPrimFloat: {
2859 // Processing a Dex `float-to-long' instruction.
2860 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2861 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002862 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002863
Mark Mendell92e83bf2015-05-07 11:25:03 -04002864 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002865 // if input >= (float)LONG_MAX goto done
2866 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002867 __ j(kAboveEqual, &done);
2868 // if input == NaN goto nan
2869 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002870 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002871 __ cvttss2si(output, input, true);
2872 __ jmp(&done);
2873 __ Bind(&nan);
2874 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002875 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002876 __ Bind(&done);
2877 break;
2878 }
2879
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002880 case Primitive::kPrimDouble: {
2881 // Processing a Dex `double-to-long' instruction.
2882 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2883 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002884 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002885
Mark Mendell92e83bf2015-05-07 11:25:03 -04002886 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002887 // if input >= (double)LONG_MAX goto done
2888 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002889 __ j(kAboveEqual, &done);
2890 // if input == NaN goto nan
2891 __ j(kUnordered, &nan);
2892 // output = double-to-long-truncate(input)
2893 __ cvttsd2si(output, input, true);
2894 __ jmp(&done);
2895 __ Bind(&nan);
2896 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002897 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002898 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002899 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002900 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002901
2902 default:
2903 LOG(FATAL) << "Unexpected type conversion from " << input_type
2904 << " to " << result_type;
2905 }
2906 break;
2907
Roland Levillain981e4542014-11-14 11:47:14 +00002908 case Primitive::kPrimChar:
2909 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002910 case Primitive::kPrimLong:
2911 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002912 case Primitive::kPrimBoolean:
2913 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002914 case Primitive::kPrimByte:
2915 case Primitive::kPrimShort:
2916 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002917 // Processing a Dex `int-to-char' instruction.
2918 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002919 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002920 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002921 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002922 Address(CpuRegister(RSP), in.GetStackIndex()));
2923 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002924 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002925 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002926 }
2927 break;
2928
2929 default:
2930 LOG(FATAL) << "Unexpected type conversion from " << input_type
2931 << " to " << result_type;
2932 }
2933 break;
2934
Roland Levillaindff1f282014-11-05 14:15:05 +00002935 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002936 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002937 case Primitive::kPrimBoolean:
2938 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002939 case Primitive::kPrimByte:
2940 case Primitive::kPrimShort:
2941 case Primitive::kPrimInt:
2942 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002943 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002944 if (in.IsRegister()) {
2945 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2946 } else if (in.IsConstant()) {
2947 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2948 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002949 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002950 } else {
2951 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2952 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2953 }
Roland Levillaincff13742014-11-17 14:32:17 +00002954 break;
2955
2956 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002957 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002958 if (in.IsRegister()) {
2959 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2960 } else if (in.IsConstant()) {
2961 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2962 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002963 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002964 } else {
2965 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2966 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2967 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002968 break;
2969
Roland Levillaincff13742014-11-17 14:32:17 +00002970 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002971 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002972 if (in.IsFpuRegister()) {
2973 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2974 } else if (in.IsConstant()) {
2975 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2976 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002977 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002978 } else {
2979 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2980 Address(CpuRegister(RSP), in.GetStackIndex()));
2981 }
Roland Levillaincff13742014-11-17 14:32:17 +00002982 break;
2983
2984 default:
2985 LOG(FATAL) << "Unexpected type conversion from " << input_type
2986 << " to " << result_type;
2987 };
2988 break;
2989
Roland Levillaindff1f282014-11-05 14:15:05 +00002990 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002991 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002992 case Primitive::kPrimBoolean:
2993 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002994 case Primitive::kPrimByte:
2995 case Primitive::kPrimShort:
2996 case Primitive::kPrimInt:
2997 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002998 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002999 if (in.IsRegister()) {
3000 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3001 } else if (in.IsConstant()) {
3002 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3003 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003004 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003005 } else {
3006 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3007 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3008 }
Roland Levillaincff13742014-11-17 14:32:17 +00003009 break;
3010
3011 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003012 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003013 if (in.IsRegister()) {
3014 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3015 } else if (in.IsConstant()) {
3016 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3017 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003018 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003019 } else {
3020 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3021 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3022 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003023 break;
3024
Roland Levillaincff13742014-11-17 14:32:17 +00003025 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003026 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003027 if (in.IsFpuRegister()) {
3028 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3029 } else if (in.IsConstant()) {
3030 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3031 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003032 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003033 } else {
3034 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3035 Address(CpuRegister(RSP), in.GetStackIndex()));
3036 }
Roland Levillaincff13742014-11-17 14:32:17 +00003037 break;
3038
3039 default:
3040 LOG(FATAL) << "Unexpected type conversion from " << input_type
3041 << " to " << result_type;
3042 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003043 break;
3044
3045 default:
3046 LOG(FATAL) << "Unexpected type conversion from " << input_type
3047 << " to " << result_type;
3048 }
3049}
3050
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003051void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003052 LocationSummary* locations =
3053 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003054 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003055 case Primitive::kPrimInt: {
3056 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003057 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003059 break;
3060 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003061
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003062 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003063 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003064 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003065 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003067 break;
3068 }
3069
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003070 case Primitive::kPrimDouble:
3071 case Primitive::kPrimFloat: {
3072 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003073 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003074 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003075 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003076 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003077
3078 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003079 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003080 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003081}
3082
3083void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3084 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003085 Location first = locations->InAt(0);
3086 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003087 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003088
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003089 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003090 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003091 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003092 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3093 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003094 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3095 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003096 } else {
3097 __ leal(out.AsRegister<CpuRegister>(), Address(
3098 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3099 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003100 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003101 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3102 __ addl(out.AsRegister<CpuRegister>(),
3103 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3104 } else {
3105 __ leal(out.AsRegister<CpuRegister>(), Address(
3106 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3107 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003108 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003109 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003110 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003111 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003112 break;
3113 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003114
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003115 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003116 if (second.IsRegister()) {
3117 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3118 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003119 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3120 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003121 } else {
3122 __ leaq(out.AsRegister<CpuRegister>(), Address(
3123 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3124 }
3125 } else {
3126 DCHECK(second.IsConstant());
3127 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3128 int32_t int32_value = Low32Bits(value);
3129 DCHECK_EQ(int32_value, value);
3130 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3131 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3132 } else {
3133 __ leaq(out.AsRegister<CpuRegister>(), Address(
3134 first.AsRegister<CpuRegister>(), int32_value));
3135 }
3136 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003137 break;
3138 }
3139
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003140 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003141 if (second.IsFpuRegister()) {
3142 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3143 } else if (second.IsConstant()) {
3144 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003145 codegen_->LiteralFloatAddress(
3146 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003147 } else {
3148 DCHECK(second.IsStackSlot());
3149 __ addss(first.AsFpuRegister<XmmRegister>(),
3150 Address(CpuRegister(RSP), second.GetStackIndex()));
3151 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003152 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003153 }
3154
3155 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003156 if (second.IsFpuRegister()) {
3157 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3158 } else if (second.IsConstant()) {
3159 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003160 codegen_->LiteralDoubleAddress(
3161 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003162 } else {
3163 DCHECK(second.IsDoubleStackSlot());
3164 __ addsd(first.AsFpuRegister<XmmRegister>(),
3165 Address(CpuRegister(RSP), second.GetStackIndex()));
3166 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003167 break;
3168 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003169
3170 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003171 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003172 }
3173}
3174
3175void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003176 LocationSummary* locations =
3177 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003178 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003179 case Primitive::kPrimInt: {
3180 locations->SetInAt(0, Location::RequiresRegister());
3181 locations->SetInAt(1, Location::Any());
3182 locations->SetOut(Location::SameAsFirstInput());
3183 break;
3184 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003185 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003186 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003187 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003188 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003189 break;
3190 }
Calin Juravle11351682014-10-23 15:38:15 +01003191 case Primitive::kPrimFloat:
3192 case Primitive::kPrimDouble: {
3193 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003194 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003195 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003196 break;
Calin Juravle11351682014-10-23 15:38:15 +01003197 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003198 default:
Calin Juravle11351682014-10-23 15:38:15 +01003199 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003200 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003201}
3202
3203void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3204 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003205 Location first = locations->InAt(0);
3206 Location second = locations->InAt(1);
3207 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003208 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003209 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003210 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003211 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003212 } else if (second.IsConstant()) {
3213 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003215 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003216 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003217 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003218 break;
3219 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003220 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003221 if (second.IsConstant()) {
3222 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3223 DCHECK(IsInt<32>(value));
3224 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3225 } else {
3226 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3227 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003228 break;
3229 }
3230
Calin Juravle11351682014-10-23 15:38:15 +01003231 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003232 if (second.IsFpuRegister()) {
3233 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3234 } else if (second.IsConstant()) {
3235 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003236 codegen_->LiteralFloatAddress(
3237 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003238 } else {
3239 DCHECK(second.IsStackSlot());
3240 __ subss(first.AsFpuRegister<XmmRegister>(),
3241 Address(CpuRegister(RSP), second.GetStackIndex()));
3242 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003243 break;
Calin Juravle11351682014-10-23 15:38:15 +01003244 }
3245
3246 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003247 if (second.IsFpuRegister()) {
3248 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3249 } else if (second.IsConstant()) {
3250 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003251 codegen_->LiteralDoubleAddress(
3252 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003253 } else {
3254 DCHECK(second.IsDoubleStackSlot());
3255 __ subsd(first.AsFpuRegister<XmmRegister>(),
3256 Address(CpuRegister(RSP), second.GetStackIndex()));
3257 }
Calin Juravle11351682014-10-23 15:38:15 +01003258 break;
3259 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003260
3261 default:
Calin Juravle11351682014-10-23 15:38:15 +01003262 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003263 }
3264}
3265
Calin Juravle34bacdf2014-10-07 20:23:36 +01003266void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3267 LocationSummary* locations =
3268 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3269 switch (mul->GetResultType()) {
3270 case Primitive::kPrimInt: {
3271 locations->SetInAt(0, Location::RequiresRegister());
3272 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003273 if (mul->InputAt(1)->IsIntConstant()) {
3274 // Can use 3 operand multiply.
3275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3276 } else {
3277 locations->SetOut(Location::SameAsFirstInput());
3278 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003279 break;
3280 }
3281 case Primitive::kPrimLong: {
3282 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003283 locations->SetInAt(1, Location::Any());
3284 if (mul->InputAt(1)->IsLongConstant() &&
3285 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003286 // Can use 3 operand multiply.
3287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3288 } else {
3289 locations->SetOut(Location::SameAsFirstInput());
3290 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003291 break;
3292 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003293 case Primitive::kPrimFloat:
3294 case Primitive::kPrimDouble: {
3295 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003296 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003297 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003298 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003299 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003300
3301 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003302 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003303 }
3304}
3305
3306void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3307 LocationSummary* locations = mul->GetLocations();
3308 Location first = locations->InAt(0);
3309 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003310 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003311 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003312 case Primitive::kPrimInt:
3313 // The constant may have ended up in a register, so test explicitly to avoid
3314 // problems where the output may not be the same as the first operand.
3315 if (mul->InputAt(1)->IsIntConstant()) {
3316 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3317 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3318 } else if (second.IsRegister()) {
3319 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003320 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003321 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003322 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003323 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003324 __ imull(first.AsRegister<CpuRegister>(),
3325 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003326 }
3327 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003328 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003329 // The constant may have ended up in a register, so test explicitly to avoid
3330 // problems where the output may not be the same as the first operand.
3331 if (mul->InputAt(1)->IsLongConstant()) {
3332 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3333 if (IsInt<32>(value)) {
3334 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3335 Immediate(static_cast<int32_t>(value)));
3336 } else {
3337 // Have to use the constant area.
3338 DCHECK(first.Equals(out));
3339 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3340 }
3341 } else if (second.IsRegister()) {
3342 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003343 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003344 } else {
3345 DCHECK(second.IsDoubleStackSlot());
3346 DCHECK(first.Equals(out));
3347 __ imulq(first.AsRegister<CpuRegister>(),
3348 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003349 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003350 break;
3351 }
3352
Calin Juravleb5bfa962014-10-21 18:02:24 +01003353 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003354 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003355 if (second.IsFpuRegister()) {
3356 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3357 } else if (second.IsConstant()) {
3358 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003359 codegen_->LiteralFloatAddress(
3360 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003361 } else {
3362 DCHECK(second.IsStackSlot());
3363 __ mulss(first.AsFpuRegister<XmmRegister>(),
3364 Address(CpuRegister(RSP), second.GetStackIndex()));
3365 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003366 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003367 }
3368
3369 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003370 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003371 if (second.IsFpuRegister()) {
3372 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3373 } else if (second.IsConstant()) {
3374 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003375 codegen_->LiteralDoubleAddress(
3376 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003377 } else {
3378 DCHECK(second.IsDoubleStackSlot());
3379 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3380 Address(CpuRegister(RSP), second.GetStackIndex()));
3381 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003382 break;
3383 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003384
3385 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003386 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003387 }
3388}
3389
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003390void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3391 uint32_t stack_adjustment, bool is_float) {
3392 if (source.IsStackSlot()) {
3393 DCHECK(is_float);
3394 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3395 } else if (source.IsDoubleStackSlot()) {
3396 DCHECK(!is_float);
3397 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3398 } else {
3399 // Write the value to the temporary location on the stack and load to FP stack.
3400 if (is_float) {
3401 Location stack_temp = Location::StackSlot(temp_offset);
3402 codegen_->Move(stack_temp, source);
3403 __ flds(Address(CpuRegister(RSP), temp_offset));
3404 } else {
3405 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3406 codegen_->Move(stack_temp, source);
3407 __ fldl(Address(CpuRegister(RSP), temp_offset));
3408 }
3409 }
3410}
3411
3412void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3413 Primitive::Type type = rem->GetResultType();
3414 bool is_float = type == Primitive::kPrimFloat;
3415 size_t elem_size = Primitive::ComponentSize(type);
3416 LocationSummary* locations = rem->GetLocations();
3417 Location first = locations->InAt(0);
3418 Location second = locations->InAt(1);
3419 Location out = locations->Out();
3420
3421 // Create stack space for 2 elements.
3422 // TODO: enhance register allocator to ask for stack temporaries.
3423 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3424
3425 // Load the values to the FP stack in reverse order, using temporaries if needed.
3426 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3427 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3428
3429 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003430 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003431 __ Bind(&retry);
3432 __ fprem();
3433
3434 // Move FP status to AX.
3435 __ fstsw();
3436
3437 // And see if the argument reduction is complete. This is signaled by the
3438 // C2 FPU flag bit set to 0.
3439 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3440 __ j(kNotEqual, &retry);
3441
3442 // We have settled on the final value. Retrieve it into an XMM register.
3443 // Store FP top of stack to real stack.
3444 if (is_float) {
3445 __ fsts(Address(CpuRegister(RSP), 0));
3446 } else {
3447 __ fstl(Address(CpuRegister(RSP), 0));
3448 }
3449
3450 // Pop the 2 items from the FP stack.
3451 __ fucompp();
3452
3453 // Load the value from the stack into an XMM register.
3454 DCHECK(out.IsFpuRegister()) << out;
3455 if (is_float) {
3456 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3457 } else {
3458 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3459 }
3460
3461 // And remove the temporary stack space we allocated.
3462 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3463}
3464
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003465void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3466 DCHECK(instruction->IsDiv() || instruction->IsRem());
3467
3468 LocationSummary* locations = instruction->GetLocations();
3469 Location second = locations->InAt(1);
3470 DCHECK(second.IsConstant());
3471
3472 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3473 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003474 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003475
3476 DCHECK(imm == 1 || imm == -1);
3477
3478 switch (instruction->GetResultType()) {
3479 case Primitive::kPrimInt: {
3480 if (instruction->IsRem()) {
3481 __ xorl(output_register, output_register);
3482 } else {
3483 __ movl(output_register, input_register);
3484 if (imm == -1) {
3485 __ negl(output_register);
3486 }
3487 }
3488 break;
3489 }
3490
3491 case Primitive::kPrimLong: {
3492 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003493 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003494 } else {
3495 __ movq(output_register, input_register);
3496 if (imm == -1) {
3497 __ negq(output_register);
3498 }
3499 }
3500 break;
3501 }
3502
3503 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003504 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003505 }
3506}
3507
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003508void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509 LocationSummary* locations = instruction->GetLocations();
3510 Location second = locations->InAt(1);
3511
3512 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3513 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3514
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003515 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003516 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3517 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003518
3519 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3520
3521 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003522 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003523 __ testl(numerator, numerator);
3524 __ cmov(kGreaterEqual, tmp, numerator);
3525 int shift = CTZ(imm);
3526 __ sarl(tmp, Immediate(shift));
3527
3528 if (imm < 0) {
3529 __ negl(tmp);
3530 }
3531
3532 __ movl(output_register, tmp);
3533 } else {
3534 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3535 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3536
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003537 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003538 __ addq(rdx, numerator);
3539 __ testq(numerator, numerator);
3540 __ cmov(kGreaterEqual, rdx, numerator);
3541 int shift = CTZ(imm);
3542 __ sarq(rdx, Immediate(shift));
3543
3544 if (imm < 0) {
3545 __ negq(rdx);
3546 }
3547
3548 __ movq(output_register, rdx);
3549 }
3550}
3551
3552void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3553 DCHECK(instruction->IsDiv() || instruction->IsRem());
3554
3555 LocationSummary* locations = instruction->GetLocations();
3556 Location second = locations->InAt(1);
3557
3558 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3559 : locations->GetTemp(0).AsRegister<CpuRegister>();
3560 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3561 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3562 : locations->Out().AsRegister<CpuRegister>();
3563 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3564
3565 DCHECK_EQ(RAX, eax.AsRegister());
3566 DCHECK_EQ(RDX, edx.AsRegister());
3567 if (instruction->IsDiv()) {
3568 DCHECK_EQ(RAX, out.AsRegister());
3569 } else {
3570 DCHECK_EQ(RDX, out.AsRegister());
3571 }
3572
3573 int64_t magic;
3574 int shift;
3575
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003576 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003577 if (instruction->GetResultType() == Primitive::kPrimInt) {
3578 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3579
3580 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3581
3582 __ movl(numerator, eax);
3583
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003584 __ movl(eax, Immediate(magic));
3585 __ imull(numerator);
3586
3587 if (imm > 0 && magic < 0) {
3588 __ addl(edx, numerator);
3589 } else if (imm < 0 && magic > 0) {
3590 __ subl(edx, numerator);
3591 }
3592
3593 if (shift != 0) {
3594 __ sarl(edx, Immediate(shift));
3595 }
3596
3597 __ movl(eax, edx);
3598 __ shrl(edx, Immediate(31));
3599 __ addl(edx, eax);
3600
3601 if (instruction->IsRem()) {
3602 __ movl(eax, numerator);
3603 __ imull(edx, Immediate(imm));
3604 __ subl(eax, edx);
3605 __ movl(edx, eax);
3606 } else {
3607 __ movl(eax, edx);
3608 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003609 } else {
3610 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3611
3612 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3613
3614 CpuRegister rax = eax;
3615 CpuRegister rdx = edx;
3616
3617 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3618
3619 // Save the numerator.
3620 __ movq(numerator, rax);
3621
3622 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003623 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003624
3625 // RDX:RAX = magic * numerator
3626 __ imulq(numerator);
3627
3628 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003629 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003630 __ addq(rdx, numerator);
3631 } else if (imm < 0 && magic > 0) {
3632 // RDX -= numerator
3633 __ subq(rdx, numerator);
3634 }
3635
3636 // Shift if needed.
3637 if (shift != 0) {
3638 __ sarq(rdx, Immediate(shift));
3639 }
3640
3641 // RDX += 1 if RDX < 0
3642 __ movq(rax, rdx);
3643 __ shrq(rdx, Immediate(63));
3644 __ addq(rdx, rax);
3645
3646 if (instruction->IsRem()) {
3647 __ movq(rax, numerator);
3648
3649 if (IsInt<32>(imm)) {
3650 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3651 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003652 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003653 }
3654
3655 __ subq(rax, rdx);
3656 __ movq(rdx, rax);
3657 } else {
3658 __ movq(rax, rdx);
3659 }
3660 }
3661}
3662
Calin Juravlebacfec32014-11-14 15:54:36 +00003663void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3664 DCHECK(instruction->IsDiv() || instruction->IsRem());
3665 Primitive::Type type = instruction->GetResultType();
3666 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3667
3668 bool is_div = instruction->IsDiv();
3669 LocationSummary* locations = instruction->GetLocations();
3670
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003671 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3672 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003673
Roland Levillain271ab9c2014-11-27 15:23:57 +00003674 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003675 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003676
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003677 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003678 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003679
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003680 if (imm == 0) {
3681 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3682 } else if (imm == 1 || imm == -1) {
3683 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003684 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003685 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003686 } else {
3687 DCHECK(imm <= -2 || imm >= 2);
3688 GenerateDivRemWithAnyConstant(instruction);
3689 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003690 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003691 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003692 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003693 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003694 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003695
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003696 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3697 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3698 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3699 // so it's safe to just use negl instead of more complex comparisons.
3700 if (type == Primitive::kPrimInt) {
3701 __ cmpl(second_reg, Immediate(-1));
3702 __ j(kEqual, slow_path->GetEntryLabel());
3703 // edx:eax <- sign-extended of eax
3704 __ cdq();
3705 // eax = quotient, edx = remainder
3706 __ idivl(second_reg);
3707 } else {
3708 __ cmpq(second_reg, Immediate(-1));
3709 __ j(kEqual, slow_path->GetEntryLabel());
3710 // rdx:rax <- sign-extended of rax
3711 __ cqo();
3712 // rax = quotient, rdx = remainder
3713 __ idivq(second_reg);
3714 }
3715 __ Bind(slow_path->GetExitLabel());
3716 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003717}
3718
Calin Juravle7c4954d2014-10-28 16:57:40 +00003719void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3720 LocationSummary* locations =
3721 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3722 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003723 case Primitive::kPrimInt:
3724 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003725 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003726 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003727 locations->SetOut(Location::SameAsFirstInput());
3728 // Intel uses edx:eax as the dividend.
3729 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003730 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3731 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3732 // output and request another temp.
3733 if (div->InputAt(1)->IsConstant()) {
3734 locations->AddTemp(Location::RequiresRegister());
3735 }
Calin Juravled0d48522014-11-04 16:40:20 +00003736 break;
3737 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003738
Calin Juravle7c4954d2014-10-28 16:57:40 +00003739 case Primitive::kPrimFloat:
3740 case Primitive::kPrimDouble: {
3741 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003742 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003743 locations->SetOut(Location::SameAsFirstInput());
3744 break;
3745 }
3746
3747 default:
3748 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3749 }
3750}
3751
3752void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3753 LocationSummary* locations = div->GetLocations();
3754 Location first = locations->InAt(0);
3755 Location second = locations->InAt(1);
3756 DCHECK(first.Equals(locations->Out()));
3757
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003758 Primitive::Type type = div->GetResultType();
3759 switch (type) {
3760 case Primitive::kPrimInt:
3761 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003762 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003763 break;
3764 }
3765
Calin Juravle7c4954d2014-10-28 16:57:40 +00003766 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003767 if (second.IsFpuRegister()) {
3768 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3769 } else if (second.IsConstant()) {
3770 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003771 codegen_->LiteralFloatAddress(
3772 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003773 } else {
3774 DCHECK(second.IsStackSlot());
3775 __ divss(first.AsFpuRegister<XmmRegister>(),
3776 Address(CpuRegister(RSP), second.GetStackIndex()));
3777 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003778 break;
3779 }
3780
3781 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003782 if (second.IsFpuRegister()) {
3783 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3784 } else if (second.IsConstant()) {
3785 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003786 codegen_->LiteralDoubleAddress(
3787 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003788 } else {
3789 DCHECK(second.IsDoubleStackSlot());
3790 __ divsd(first.AsFpuRegister<XmmRegister>(),
3791 Address(CpuRegister(RSP), second.GetStackIndex()));
3792 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003793 break;
3794 }
3795
3796 default:
3797 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3798 }
3799}
3800
Calin Juravlebacfec32014-11-14 15:54:36 +00003801void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003802 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003803 LocationSummary* locations =
3804 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003805
3806 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003807 case Primitive::kPrimInt:
3808 case Primitive::kPrimLong: {
3809 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003810 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003811 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3812 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003813 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3814 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3815 // output and request another temp.
3816 if (rem->InputAt(1)->IsConstant()) {
3817 locations->AddTemp(Location::RequiresRegister());
3818 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003819 break;
3820 }
3821
3822 case Primitive::kPrimFloat:
3823 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003824 locations->SetInAt(0, Location::Any());
3825 locations->SetInAt(1, Location::Any());
3826 locations->SetOut(Location::RequiresFpuRegister());
3827 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003828 break;
3829 }
3830
3831 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003832 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003833 }
3834}
3835
3836void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3837 Primitive::Type type = rem->GetResultType();
3838 switch (type) {
3839 case Primitive::kPrimInt:
3840 case Primitive::kPrimLong: {
3841 GenerateDivRemIntegral(rem);
3842 break;
3843 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003844 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003845 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003846 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003847 break;
3848 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003849 default:
3850 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3851 }
3852}
3853
Calin Juravled0d48522014-11-04 16:40:20 +00003854void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003855 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003856 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003857}
3858
3859void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003860 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003861 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3862 codegen_->AddSlowPath(slow_path);
3863
3864 LocationSummary* locations = instruction->GetLocations();
3865 Location value = locations->InAt(0);
3866
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003867 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003868 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003869 case Primitive::kPrimByte:
3870 case Primitive::kPrimChar:
3871 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003872 case Primitive::kPrimInt: {
3873 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003874 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003875 __ j(kEqual, slow_path->GetEntryLabel());
3876 } else if (value.IsStackSlot()) {
3877 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3878 __ j(kEqual, slow_path->GetEntryLabel());
3879 } else {
3880 DCHECK(value.IsConstant()) << value;
3881 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003882 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003883 }
3884 }
3885 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003886 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003887 case Primitive::kPrimLong: {
3888 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003889 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003890 __ j(kEqual, slow_path->GetEntryLabel());
3891 } else if (value.IsDoubleStackSlot()) {
3892 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3893 __ j(kEqual, slow_path->GetEntryLabel());
3894 } else {
3895 DCHECK(value.IsConstant()) << value;
3896 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003897 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003898 }
3899 }
3900 break;
3901 }
3902 default:
3903 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003904 }
Calin Juravled0d48522014-11-04 16:40:20 +00003905}
3906
Calin Juravle9aec02f2014-11-18 23:06:35 +00003907void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3908 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3909
3910 LocationSummary* locations =
3911 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3912
3913 switch (op->GetResultType()) {
3914 case Primitive::kPrimInt:
3915 case Primitive::kPrimLong: {
3916 locations->SetInAt(0, Location::RequiresRegister());
3917 // The shift count needs to be in CL.
3918 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3919 locations->SetOut(Location::SameAsFirstInput());
3920 break;
3921 }
3922 default:
3923 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3924 }
3925}
3926
3927void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3928 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3929
3930 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003931 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003932 Location second = locations->InAt(1);
3933
3934 switch (op->GetResultType()) {
3935 case Primitive::kPrimInt: {
3936 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003937 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938 if (op->IsShl()) {
3939 __ shll(first_reg, second_reg);
3940 } else if (op->IsShr()) {
3941 __ sarl(first_reg, second_reg);
3942 } else {
3943 __ shrl(first_reg, second_reg);
3944 }
3945 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003946 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003947 if (op->IsShl()) {
3948 __ shll(first_reg, imm);
3949 } else if (op->IsShr()) {
3950 __ sarl(first_reg, imm);
3951 } else {
3952 __ shrl(first_reg, imm);
3953 }
3954 }
3955 break;
3956 }
3957 case Primitive::kPrimLong: {
3958 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003959 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003960 if (op->IsShl()) {
3961 __ shlq(first_reg, second_reg);
3962 } else if (op->IsShr()) {
3963 __ sarq(first_reg, second_reg);
3964 } else {
3965 __ shrq(first_reg, second_reg);
3966 }
3967 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003968 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003969 if (op->IsShl()) {
3970 __ shlq(first_reg, imm);
3971 } else if (op->IsShr()) {
3972 __ sarq(first_reg, imm);
3973 } else {
3974 __ shrq(first_reg, imm);
3975 }
3976 }
3977 break;
3978 }
3979 default:
3980 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00003981 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003982 }
3983}
3984
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003985void LocationsBuilderX86_64::VisitRor(HRor* ror) {
3986 LocationSummary* locations =
3987 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3988
3989 switch (ror->GetResultType()) {
3990 case Primitive::kPrimInt:
3991 case Primitive::kPrimLong: {
3992 locations->SetInAt(0, Location::RequiresRegister());
3993 // The shift count needs to be in CL (unless it is a constant).
3994 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
3995 locations->SetOut(Location::SameAsFirstInput());
3996 break;
3997 }
3998 default:
3999 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4000 UNREACHABLE();
4001 }
4002}
4003
4004void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4005 LocationSummary* locations = ror->GetLocations();
4006 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4007 Location second = locations->InAt(1);
4008
4009 switch (ror->GetResultType()) {
4010 case Primitive::kPrimInt:
4011 if (second.IsRegister()) {
4012 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4013 __ rorl(first_reg, second_reg);
4014 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004015 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004016 __ rorl(first_reg, imm);
4017 }
4018 break;
4019 case Primitive::kPrimLong:
4020 if (second.IsRegister()) {
4021 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4022 __ rorq(first_reg, second_reg);
4023 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004024 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004025 __ rorq(first_reg, imm);
4026 }
4027 break;
4028 default:
4029 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4030 UNREACHABLE();
4031 }
4032}
4033
Calin Juravle9aec02f2014-11-18 23:06:35 +00004034void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4035 HandleShift(shl);
4036}
4037
4038void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4039 HandleShift(shl);
4040}
4041
4042void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4043 HandleShift(shr);
4044}
4045
4046void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4047 HandleShift(shr);
4048}
4049
4050void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4051 HandleShift(ushr);
4052}
4053
4054void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4055 HandleShift(ushr);
4056}
4057
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004058void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004059 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004060 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004061 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004062 if (instruction->IsStringAlloc()) {
4063 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4064 } else {
4065 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4066 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4067 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004068 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004069}
4070
4071void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004072 // Note: if heap poisoning is enabled, the entry point takes cares
4073 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004074 if (instruction->IsStringAlloc()) {
4075 // String is allocated through StringFactory. Call NewEmptyString entry point.
4076 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004077 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004078 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4079 __ call(Address(temp, code_offset.SizeValue()));
4080 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4081 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004082 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004083 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4084 DCHECK(!codegen_->IsLeafMethod());
4085 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004086}
4087
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004088void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4089 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004090 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004091 InvokeRuntimeCallingConvention calling_convention;
4092 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004093 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004094 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004095 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004096}
4097
4098void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
4099 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004100 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
4101 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004102 // Note: if heap poisoning is enabled, the entry point takes cares
4103 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004104 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004105 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004106
4107 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004108}
4109
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004110void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004111 LocationSummary* locations =
4112 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004113 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4114 if (location.IsStackSlot()) {
4115 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4116 } else if (location.IsDoubleStackSlot()) {
4117 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4118 }
4119 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004120}
4121
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004122void InstructionCodeGeneratorX86_64::VisitParameterValue(
4123 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004124 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004125}
4126
4127void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4128 LocationSummary* locations =
4129 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4130 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4131}
4132
4133void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4134 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4135 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004136}
4137
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004138void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4139 LocationSummary* locations =
4140 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4141 locations->SetInAt(0, Location::RequiresRegister());
4142 locations->SetOut(Location::RequiresRegister());
4143}
4144
4145void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4146 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004147 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004148 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004149 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004150 __ movq(locations->Out().AsRegister<CpuRegister>(),
4151 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004152 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004153 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004154 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004155 __ movq(locations->Out().AsRegister<CpuRegister>(),
4156 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4157 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004158 __ movq(locations->Out().AsRegister<CpuRegister>(),
4159 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004160 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004161}
4162
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004163void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004164 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004165 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004166 locations->SetInAt(0, Location::RequiresRegister());
4167 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004168}
4169
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004170void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4171 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004172 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4173 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004174 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004175 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004176 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004177 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004178 break;
4179
4180 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004181 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004182 break;
4183
4184 default:
4185 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4186 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004187}
4188
David Brazdil66d126e2015-04-03 16:02:44 +01004189void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4190 LocationSummary* locations =
4191 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4192 locations->SetInAt(0, Location::RequiresRegister());
4193 locations->SetOut(Location::SameAsFirstInput());
4194}
4195
4196void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004197 LocationSummary* locations = bool_not->GetLocations();
4198 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4199 locations->Out().AsRegister<CpuRegister>().AsRegister());
4200 Location out = locations->Out();
4201 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4202}
4203
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004204void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004205 LocationSummary* locations =
4206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004207 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004208 locations->SetInAt(i, Location::Any());
4209 }
4210 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004211}
4212
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004213void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004214 LOG(FATAL) << "Unimplemented";
4215}
4216
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004217void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004218 /*
4219 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004220 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004221 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4222 */
4223 switch (kind) {
4224 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004225 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004226 break;
4227 }
4228 case MemBarrierKind::kAnyStore:
4229 case MemBarrierKind::kLoadAny:
4230 case MemBarrierKind::kStoreStore: {
4231 // nop
4232 break;
4233 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004234 case MemBarrierKind::kNTStoreStore:
4235 // Non-Temporal Store/Store needs an explicit fence.
4236 MemoryFence(/* non-temporal */ true);
4237 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004238 }
4239}
4240
4241void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4242 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4243
Roland Levillain0d5a2812015-11-13 10:07:31 +00004244 bool object_field_get_with_read_barrier =
4245 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004246 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004247 new (GetGraph()->GetArena()) LocationSummary(instruction,
4248 object_field_get_with_read_barrier ?
4249 LocationSummary::kCallOnSlowPath :
4250 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004251 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004252 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004253 }
Calin Juravle52c48962014-12-16 17:02:57 +00004254 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004255 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4256 locations->SetOut(Location::RequiresFpuRegister());
4257 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004258 // The output overlaps for an object field get when read barriers
4259 // are enabled: we do not want the move to overwrite the object's
4260 // location, as we need it to emit the read barrier.
4261 locations->SetOut(
4262 Location::RequiresRegister(),
4263 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004264 }
Calin Juravle52c48962014-12-16 17:02:57 +00004265}
4266
4267void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4268 const FieldInfo& field_info) {
4269 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4270
4271 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004272 Location base_loc = locations->InAt(0);
4273 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004274 Location out = locations->Out();
4275 bool is_volatile = field_info.IsVolatile();
4276 Primitive::Type field_type = field_info.GetFieldType();
4277 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4278
4279 switch (field_type) {
4280 case Primitive::kPrimBoolean: {
4281 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4282 break;
4283 }
4284
4285 case Primitive::kPrimByte: {
4286 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4287 break;
4288 }
4289
4290 case Primitive::kPrimShort: {
4291 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4292 break;
4293 }
4294
4295 case Primitive::kPrimChar: {
4296 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4297 break;
4298 }
4299
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004300 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004301 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4302 break;
4303 }
4304
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004305 case Primitive::kPrimNot: {
4306 // /* HeapReference<Object> */ out = *(base + offset)
4307 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004308 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004309 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004310 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004311 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004312 if (is_volatile) {
4313 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4314 }
4315 } else {
4316 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4317 codegen_->MaybeRecordImplicitNullCheck(instruction);
4318 if (is_volatile) {
4319 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4320 }
4321 // If read barriers are enabled, emit read barriers other than
4322 // Baker's using a slow path (and also unpoison the loaded
4323 // reference, if heap poisoning is enabled).
4324 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4325 }
4326 break;
4327 }
4328
Calin Juravle52c48962014-12-16 17:02:57 +00004329 case Primitive::kPrimLong: {
4330 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4331 break;
4332 }
4333
4334 case Primitive::kPrimFloat: {
4335 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4336 break;
4337 }
4338
4339 case Primitive::kPrimDouble: {
4340 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4341 break;
4342 }
4343
4344 case Primitive::kPrimVoid:
4345 LOG(FATAL) << "Unreachable type " << field_type;
4346 UNREACHABLE();
4347 }
4348
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004349 if (field_type == Primitive::kPrimNot) {
4350 // Potential implicit null checks, in the case of reference
4351 // fields, are handled in the previous switch statement.
4352 } else {
4353 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004354 }
Roland Levillain4d027112015-07-01 15:41:14 +01004355
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004356 if (is_volatile) {
4357 if (field_type == Primitive::kPrimNot) {
4358 // Memory barriers, in the case of references, are also handled
4359 // in the previous switch statement.
4360 } else {
4361 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4362 }
Roland Levillain4d027112015-07-01 15:41:14 +01004363 }
Calin Juravle52c48962014-12-16 17:02:57 +00004364}
4365
4366void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4367 const FieldInfo& field_info) {
4368 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4369
4370 LocationSummary* locations =
4371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004372 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004373 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004374 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004375 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004376
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004377 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004378 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004379 if (is_volatile) {
4380 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4381 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4382 } else {
4383 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4384 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004385 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004386 if (is_volatile) {
4387 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4388 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4389 } else {
4390 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4391 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004392 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004393 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004394 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004395 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004396 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004397 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4398 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004399 locations->AddTemp(Location::RequiresRegister());
4400 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004401}
4402
Calin Juravle52c48962014-12-16 17:02:57 +00004403void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004404 const FieldInfo& field_info,
4405 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004406 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4407
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004408 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004409 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4410 Location value = locations->InAt(1);
4411 bool is_volatile = field_info.IsVolatile();
4412 Primitive::Type field_type = field_info.GetFieldType();
4413 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4414
4415 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004416 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004417 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004418
Mark Mendellea5af682015-10-22 17:35:49 -04004419 bool maybe_record_implicit_null_check_done = false;
4420
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004421 switch (field_type) {
4422 case Primitive::kPrimBoolean:
4423 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004424 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004425 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004426 __ movb(Address(base, offset), Immediate(v));
4427 } else {
4428 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4429 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004430 break;
4431 }
4432
4433 case Primitive::kPrimShort:
4434 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004435 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004436 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004437 __ movw(Address(base, offset), Immediate(v));
4438 } else {
4439 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4440 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004441 break;
4442 }
4443
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004444 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004445 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004446 if (value.IsConstant()) {
4447 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004448 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4449 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4450 // Note: if heap poisoning is enabled, no need to poison
4451 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004452 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004453 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004454 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4455 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4456 __ movl(temp, value.AsRegister<CpuRegister>());
4457 __ PoisonHeapReference(temp);
4458 __ movl(Address(base, offset), temp);
4459 } else {
4460 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4461 }
Mark Mendell40741f32015-04-20 22:10:34 -04004462 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004463 break;
4464 }
4465
4466 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004467 if (value.IsConstant()) {
4468 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004469 codegen_->MoveInt64ToAddress(Address(base, offset),
4470 Address(base, offset + sizeof(int32_t)),
4471 v,
4472 instruction);
4473 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004474 } else {
4475 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4476 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477 break;
4478 }
4479
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004480 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004481 if (value.IsConstant()) {
4482 int32_t v =
4483 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4484 __ movl(Address(base, offset), Immediate(v));
4485 } else {
4486 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4487 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004488 break;
4489 }
4490
4491 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004492 if (value.IsConstant()) {
4493 int64_t v =
4494 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4495 codegen_->MoveInt64ToAddress(Address(base, offset),
4496 Address(base, offset + sizeof(int32_t)),
4497 v,
4498 instruction);
4499 maybe_record_implicit_null_check_done = true;
4500 } else {
4501 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4502 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004503 break;
4504 }
4505
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506 case Primitive::kPrimVoid:
4507 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004508 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004509 }
Calin Juravle52c48962014-12-16 17:02:57 +00004510
Mark Mendellea5af682015-10-22 17:35:49 -04004511 if (!maybe_record_implicit_null_check_done) {
4512 codegen_->MaybeRecordImplicitNullCheck(instruction);
4513 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004514
4515 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4516 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4517 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004518 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004519 }
4520
Calin Juravle52c48962014-12-16 17:02:57 +00004521 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004522 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004523 }
4524}
4525
4526void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4527 HandleFieldSet(instruction, instruction->GetFieldInfo());
4528}
4529
4530void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004531 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004532}
4533
4534void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004535 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004536}
4537
4538void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004539 HandleFieldGet(instruction, instruction->GetFieldInfo());
4540}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004541
Calin Juravle52c48962014-12-16 17:02:57 +00004542void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4543 HandleFieldGet(instruction);
4544}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004545
Calin Juravle52c48962014-12-16 17:02:57 +00004546void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4547 HandleFieldGet(instruction, instruction->GetFieldInfo());
4548}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549
Calin Juravle52c48962014-12-16 17:02:57 +00004550void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4551 HandleFieldSet(instruction, instruction->GetFieldInfo());
4552}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004553
Calin Juravle52c48962014-12-16 17:02:57 +00004554void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004555 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004556}
4557
Calin Juravlee460d1d2015-09-29 04:52:17 +01004558void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4559 HUnresolvedInstanceFieldGet* instruction) {
4560 FieldAccessCallingConventionX86_64 calling_convention;
4561 codegen_->CreateUnresolvedFieldLocationSummary(
4562 instruction, instruction->GetFieldType(), calling_convention);
4563}
4564
4565void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4566 HUnresolvedInstanceFieldGet* instruction) {
4567 FieldAccessCallingConventionX86_64 calling_convention;
4568 codegen_->GenerateUnresolvedFieldAccess(instruction,
4569 instruction->GetFieldType(),
4570 instruction->GetFieldIndex(),
4571 instruction->GetDexPc(),
4572 calling_convention);
4573}
4574
4575void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4576 HUnresolvedInstanceFieldSet* instruction) {
4577 FieldAccessCallingConventionX86_64 calling_convention;
4578 codegen_->CreateUnresolvedFieldLocationSummary(
4579 instruction, instruction->GetFieldType(), calling_convention);
4580}
4581
4582void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4583 HUnresolvedInstanceFieldSet* instruction) {
4584 FieldAccessCallingConventionX86_64 calling_convention;
4585 codegen_->GenerateUnresolvedFieldAccess(instruction,
4586 instruction->GetFieldType(),
4587 instruction->GetFieldIndex(),
4588 instruction->GetDexPc(),
4589 calling_convention);
4590}
4591
4592void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4593 HUnresolvedStaticFieldGet* instruction) {
4594 FieldAccessCallingConventionX86_64 calling_convention;
4595 codegen_->CreateUnresolvedFieldLocationSummary(
4596 instruction, instruction->GetFieldType(), calling_convention);
4597}
4598
4599void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4600 HUnresolvedStaticFieldGet* instruction) {
4601 FieldAccessCallingConventionX86_64 calling_convention;
4602 codegen_->GenerateUnresolvedFieldAccess(instruction,
4603 instruction->GetFieldType(),
4604 instruction->GetFieldIndex(),
4605 instruction->GetDexPc(),
4606 calling_convention);
4607}
4608
4609void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4610 HUnresolvedStaticFieldSet* instruction) {
4611 FieldAccessCallingConventionX86_64 calling_convention;
4612 codegen_->CreateUnresolvedFieldLocationSummary(
4613 instruction, instruction->GetFieldType(), calling_convention);
4614}
4615
4616void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4617 HUnresolvedStaticFieldSet* instruction) {
4618 FieldAccessCallingConventionX86_64 calling_convention;
4619 codegen_->GenerateUnresolvedFieldAccess(instruction,
4620 instruction->GetFieldType(),
4621 instruction->GetFieldIndex(),
4622 instruction->GetDexPc(),
4623 calling_convention);
4624}
4625
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004626void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004627 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4628 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4629 ? Location::RequiresRegister()
4630 : Location::Any();
4631 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004632}
4633
Calin Juravle2ae48182016-03-16 14:05:09 +00004634void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4635 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004636 return;
4637 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004638 LocationSummary* locations = instruction->GetLocations();
4639 Location obj = locations->InAt(0);
4640
4641 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004642 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004643}
4644
Calin Juravle2ae48182016-03-16 14:05:09 +00004645void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004646 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004647 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004648
4649 LocationSummary* locations = instruction->GetLocations();
4650 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004651
4652 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004653 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004654 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004656 } else {
4657 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004658 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004659 __ jmp(slow_path->GetEntryLabel());
4660 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004661 }
4662 __ j(kEqual, slow_path->GetEntryLabel());
4663}
4664
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004665void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004666 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004667}
4668
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004669void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004670 bool object_array_get_with_read_barrier =
4671 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004672 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004673 new (GetGraph()->GetArena()) LocationSummary(instruction,
4674 object_array_get_with_read_barrier ?
4675 LocationSummary::kCallOnSlowPath :
4676 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004677 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004678 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004679 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004680 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004681 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004682 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4683 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4684 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004685 // The output overlaps for an object array get when read barriers
4686 // are enabled: we do not want the move to overwrite the array's
4687 // location, as we need it to emit the read barrier.
4688 locations->SetOut(
4689 Location::RequiresRegister(),
4690 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004691 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004692}
4693
4694void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4695 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004696 Location obj_loc = locations->InAt(0);
4697 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004698 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004699 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004700 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004701
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004702 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004703 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004705 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004706 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004707 break;
4708 }
4709
4710 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004711 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004712 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004713 break;
4714 }
4715
4716 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004717 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004718 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004719 break;
4720 }
4721
4722 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004723 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004724 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4725 // Branch cases into compressed and uncompressed for each index's type.
4726 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4727 NearLabel done, not_compressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004728 __ testl(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004729 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004730 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4731 "Expecting 0=compressed, 1=uncompressed");
4732 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004733 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4734 __ jmp(&done);
4735 __ Bind(&not_compressed);
4736 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4737 __ Bind(&done);
4738 } else {
4739 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4740 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004741 break;
4742 }
4743
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004744 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004745 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004746 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004747 break;
4748 }
4749
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004750 case Primitive::kPrimNot: {
4751 static_assert(
4752 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4753 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004754 // /* HeapReference<Object> */ out =
4755 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4756 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004757 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004758 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004759 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004760 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004761 } else {
4762 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004763 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4764 codegen_->MaybeRecordImplicitNullCheck(instruction);
4765 // If read barriers are enabled, emit read barriers other than
4766 // Baker's using a slow path (and also unpoison the loaded
4767 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004768 if (index.IsConstant()) {
4769 uint32_t offset =
4770 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004771 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4772 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004773 codegen_->MaybeGenerateReadBarrierSlow(
4774 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4775 }
4776 }
4777 break;
4778 }
4779
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004780 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004781 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004782 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004783 break;
4784 }
4785
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004786 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004787 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004788 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004789 break;
4790 }
4791
4792 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004793 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004794 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004795 break;
4796 }
4797
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004798 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004799 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004800 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 }
Roland Levillain4d027112015-07-01 15:41:14 +01004802
4803 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004804 // Potential implicit null checks, in the case of reference
4805 // arrays, are handled in the previous switch statement.
4806 } else {
4807 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004808 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004809}
4810
4811void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004812 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004813
4814 bool needs_write_barrier =
4815 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004816 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004817
Nicolas Geoffray39468442014-09-02 15:17:15 +01004818 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004819 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004820 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004821 LocationSummary::kCallOnSlowPath :
4822 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004823
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004824 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004825 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4826 if (Primitive::IsFloatingPointType(value_type)) {
4827 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004828 } else {
4829 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4830 }
4831
4832 if (needs_write_barrier) {
4833 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004834 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004835 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004836 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004837}
4838
4839void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4840 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004841 Location array_loc = locations->InAt(0);
4842 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004843 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004844 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004845 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004846 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004847 bool needs_write_barrier =
4848 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004849 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4850 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4851 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004852
4853 switch (value_type) {
4854 case Primitive::kPrimBoolean:
4855 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004856 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004857 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004858 if (value.IsRegister()) {
4859 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004860 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004861 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004862 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004863 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004864 break;
4865 }
4866
4867 case Primitive::kPrimShort:
4868 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004869 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004870 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004871 if (value.IsRegister()) {
4872 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004873 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004874 DCHECK(value.IsConstant()) << value;
4875 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004876 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004877 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004878 break;
4879 }
4880
4881 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004882 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004883 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004884
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004885 if (!value.IsRegister()) {
4886 // Just setting null.
4887 DCHECK(instruction->InputAt(2)->IsNullConstant());
4888 DCHECK(value.IsConstant()) << value;
4889 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004890 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004891 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004892 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004893 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004894 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004895
4896 DCHECK(needs_write_barrier);
4897 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004898 // We cannot use a NearLabel for `done`, as its range may be too
4899 // short when Baker read barriers are enabled.
4900 Label done;
4901 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004902 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004903 Location temp_loc = locations->GetTemp(0);
4904 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004905 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004906 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4907 codegen_->AddSlowPath(slow_path);
4908 if (instruction->GetValueCanBeNull()) {
4909 __ testl(register_value, register_value);
4910 __ j(kNotEqual, &not_null);
4911 __ movl(address, Immediate(0));
4912 codegen_->MaybeRecordImplicitNullCheck(instruction);
4913 __ jmp(&done);
4914 __ Bind(&not_null);
4915 }
4916
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004917 // Note that when Baker read barriers are enabled, the type
4918 // checks are performed without read barriers. This is fine,
4919 // even in the case where a class object is in the from-space
4920 // after the flip, as a comparison involving such a type would
4921 // not produce a false positive; it may of course produce a
4922 // false negative, in which case we would take the ArraySet
4923 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004924
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004925 // /* HeapReference<Class> */ temp = array->klass_
4926 __ movl(temp, Address(array, class_offset));
4927 codegen_->MaybeRecordImplicitNullCheck(instruction);
4928 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004929
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004930 // /* HeapReference<Class> */ temp = temp->component_type_
4931 __ movl(temp, Address(temp, component_offset));
4932 // If heap poisoning is enabled, no need to unpoison `temp`
4933 // nor the object reference in `register_value->klass`, as
4934 // we are comparing two poisoned references.
4935 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004936
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004937 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4938 __ j(kEqual, &do_put);
4939 // If heap poisoning is enabled, the `temp` reference has
4940 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004941 __ MaybeUnpoisonHeapReference(temp);
4942
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004943 // If heap poisoning is enabled, no need to unpoison the
4944 // heap reference loaded below, as it is only used for a
4945 // comparison with null.
4946 __ cmpl(Address(temp, super_offset), Immediate(0));
4947 __ j(kNotEqual, slow_path->GetEntryLabel());
4948 __ Bind(&do_put);
4949 } else {
4950 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004951 }
4952 }
4953
4954 if (kPoisonHeapReferences) {
4955 __ movl(temp, register_value);
4956 __ PoisonHeapReference(temp);
4957 __ movl(address, temp);
4958 } else {
4959 __ movl(address, register_value);
4960 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004961 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004962 codegen_->MaybeRecordImplicitNullCheck(instruction);
4963 }
4964
4965 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4966 codegen_->MarkGCCard(
4967 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4968 __ Bind(&done);
4969
4970 if (slow_path != nullptr) {
4971 __ Bind(slow_path->GetExitLabel());
4972 }
4973
4974 break;
4975 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004976
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004977 case Primitive::kPrimInt: {
4978 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004979 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004980 if (value.IsRegister()) {
4981 __ movl(address, value.AsRegister<CpuRegister>());
4982 } else {
4983 DCHECK(value.IsConstant()) << value;
4984 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4985 __ movl(address, Immediate(v));
4986 }
4987 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004988 break;
4989 }
4990
4991 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004992 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004993 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004994 if (value.IsRegister()) {
4995 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004996 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004997 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004998 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004999 Address address_high =
5000 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005001 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005002 }
5003 break;
5004 }
5005
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005006 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005007 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005008 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005009 if (value.IsFpuRegister()) {
5010 __ movss(address, value.AsFpuRegister<XmmRegister>());
5011 } else {
5012 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005013 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005014 __ movl(address, Immediate(v));
5015 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005016 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005017 break;
5018 }
5019
5020 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005021 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005022 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005023 if (value.IsFpuRegister()) {
5024 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5025 codegen_->MaybeRecordImplicitNullCheck(instruction);
5026 } else {
5027 int64_t v =
5028 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005029 Address address_high =
5030 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005031 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5032 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005033 break;
5034 }
5035
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036 case Primitive::kPrimVoid:
5037 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005038 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039 }
5040}
5041
5042void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005043 LocationSummary* locations =
5044 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005045 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005046 if (!instruction->IsEmittedAtUseSite()) {
5047 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5048 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005049}
5050
5051void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005052 if (instruction->IsEmittedAtUseSite()) {
5053 return;
5054 }
5055
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005056 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005057 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005058 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5059 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005061 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005062 // Mask out most significant bit in case the array is String's array of char.
5063 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005064 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005065 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066}
5067
5068void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005069 RegisterSet caller_saves = RegisterSet::Empty();
5070 InvokeRuntimeCallingConvention calling_convention;
5071 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5072 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5073 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005074 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005075 HInstruction* length = instruction->InputAt(1);
5076 if (!length->IsEmittedAtUseSite()) {
5077 locations->SetInAt(1, Location::RegisterOrConstant(length));
5078 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005079}
5080
5081void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5082 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005083 Location index_loc = locations->InAt(0);
5084 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005085 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086
Mark Mendell99dbd682015-04-22 16:18:52 -04005087 if (length_loc.IsConstant()) {
5088 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5089 if (index_loc.IsConstant()) {
5090 // BCE will remove the bounds check if we are guarenteed to pass.
5091 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5092 if (index < 0 || index >= length) {
5093 codegen_->AddSlowPath(slow_path);
5094 __ jmp(slow_path->GetEntryLabel());
5095 } else {
5096 // Some optimization after BCE may have generated this, and we should not
5097 // generate a bounds check if it is a valid range.
5098 }
5099 return;
5100 }
5101
5102 // We have to reverse the jump condition because the length is the constant.
5103 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5104 __ cmpl(index_reg, Immediate(length));
5105 codegen_->AddSlowPath(slow_path);
5106 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005107 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005108 HInstruction* array_length = instruction->InputAt(1);
5109 if (array_length->IsEmittedAtUseSite()) {
5110 // Address the length field in the array.
5111 DCHECK(array_length->IsArrayLength());
5112 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5113 Location array_loc = array_length->GetLocations()->InAt(0);
5114 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005115 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005116 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5117 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005118 CpuRegister length_reg = CpuRegister(TMP);
5119 __ movl(length_reg, array_len);
5120 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005121 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005122 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005123 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005124 // Checking the bound for general case:
5125 // Array of char or String's array when the compression feature off.
5126 if (index_loc.IsConstant()) {
5127 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5128 __ cmpl(array_len, Immediate(value));
5129 } else {
5130 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5131 }
5132 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005133 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005134 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005135 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005136 }
5137 codegen_->AddSlowPath(slow_path);
5138 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005139 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005140}
5141
5142void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5143 CpuRegister card,
5144 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005145 CpuRegister value,
5146 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005147 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005148 if (value_can_be_null) {
5149 __ testl(value, value);
5150 __ j(kEqual, &is_null);
5151 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005152 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005153 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154 __ movq(temp, object);
5155 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005156 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005157 if (value_can_be_null) {
5158 __ Bind(&is_null);
5159 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160}
5161
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005162void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005163 LOG(FATAL) << "Unimplemented";
5164}
5165
5166void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005167 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5168}
5169
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005170void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005171 LocationSummary* locations =
5172 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005173 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005174}
5175
5176void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005177 HBasicBlock* block = instruction->GetBlock();
5178 if (block->GetLoopInformation() != nullptr) {
5179 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5180 // The back edge will generate the suspend check.
5181 return;
5182 }
5183 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5184 // The goto will generate the suspend check.
5185 return;
5186 }
5187 GenerateSuspendCheck(instruction, nullptr);
5188}
5189
5190void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5191 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005192 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005193 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5194 if (slow_path == nullptr) {
5195 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5196 instruction->SetSlowPath(slow_path);
5197 codegen_->AddSlowPath(slow_path);
5198 if (successor != nullptr) {
5199 DCHECK(successor->IsLoopHeader());
5200 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5201 }
5202 } else {
5203 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5204 }
5205
Andreas Gampe542451c2016-07-26 09:02:02 -07005206 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005207 /* no_rip */ true),
5208 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005209 if (successor == nullptr) {
5210 __ j(kNotEqual, slow_path->GetEntryLabel());
5211 __ Bind(slow_path->GetReturnLabel());
5212 } else {
5213 __ j(kEqual, codegen_->GetLabelOf(successor));
5214 __ jmp(slow_path->GetEntryLabel());
5215 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005216}
5217
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005218X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5219 return codegen_->GetAssembler();
5220}
5221
5222void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005223 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005224 Location source = move->GetSource();
5225 Location destination = move->GetDestination();
5226
5227 if (source.IsRegister()) {
5228 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005229 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005230 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005231 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005232 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005233 } else {
5234 DCHECK(destination.IsDoubleStackSlot());
5235 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005236 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005237 }
5238 } else if (source.IsStackSlot()) {
5239 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005240 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005241 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005242 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005243 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005244 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005245 } else {
5246 DCHECK(destination.IsStackSlot());
5247 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5248 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5249 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005250 } else if (source.IsDoubleStackSlot()) {
5251 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005252 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005253 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005254 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005255 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5256 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005257 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005258 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005259 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5260 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5261 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005262 } else if (source.IsConstant()) {
5263 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005264 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5265 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005266 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005267 if (value == 0) {
5268 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5269 } else {
5270 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5271 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005272 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005273 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005274 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005275 }
5276 } else if (constant->IsLongConstant()) {
5277 int64_t value = constant->AsLongConstant()->GetValue();
5278 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005279 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005280 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005281 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005282 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005283 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005284 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005285 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005286 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005287 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005288 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005289 } else {
5290 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005291 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005292 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5293 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005294 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005295 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005296 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005297 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005298 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005299 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005300 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005301 } else {
5302 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005303 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005304 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005305 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005306 } else if (source.IsFpuRegister()) {
5307 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005308 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005309 } else if (destination.IsStackSlot()) {
5310 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005311 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005312 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005313 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005314 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005315 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005316 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005317 }
5318}
5319
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005320void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005321 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005322 __ movl(Address(CpuRegister(RSP), mem), reg);
5323 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005324}
5325
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005326void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005327 ScratchRegisterScope ensure_scratch(
5328 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5329
5330 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5331 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5332 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5333 Address(CpuRegister(RSP), mem2 + stack_offset));
5334 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5335 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5336 CpuRegister(ensure_scratch.GetRegister()));
5337}
5338
Mark Mendell8a1c7282015-06-29 15:41:28 -04005339void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5340 __ movq(CpuRegister(TMP), reg1);
5341 __ movq(reg1, reg2);
5342 __ movq(reg2, CpuRegister(TMP));
5343}
5344
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005345void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5346 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5347 __ movq(Address(CpuRegister(RSP), mem), reg);
5348 __ movq(reg, CpuRegister(TMP));
5349}
5350
5351void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5352 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005353 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005354
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005355 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5356 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5357 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5358 Address(CpuRegister(RSP), mem2 + stack_offset));
5359 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5360 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5361 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005362}
5363
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005364void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5365 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5366 __ movss(Address(CpuRegister(RSP), mem), reg);
5367 __ movd(reg, CpuRegister(TMP));
5368}
5369
5370void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5371 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5372 __ movsd(Address(CpuRegister(RSP), mem), reg);
5373 __ movd(reg, CpuRegister(TMP));
5374}
5375
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005376void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005377 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005378 Location source = move->GetSource();
5379 Location destination = move->GetDestination();
5380
5381 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005382 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005383 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005384 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005385 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005386 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005387 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005388 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5389 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005390 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005391 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005392 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005393 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5394 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005395 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005396 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5397 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5398 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005399 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005400 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005401 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005402 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005403 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005404 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005405 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005406 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005407 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005408 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005409 }
5410}
5411
5412
5413void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5414 __ pushq(CpuRegister(reg));
5415}
5416
5417
5418void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5419 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005420}
5421
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005422void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005423 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005424 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5425 Immediate(mirror::Class::kStatusInitialized));
5426 __ j(kLess, slow_path->GetEntryLabel());
5427 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005428 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005429}
5430
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005431HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5432 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005433 switch (desired_class_load_kind) {
5434 case HLoadClass::LoadKind::kReferrersClass:
5435 break;
5436 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5437 DCHECK(!GetCompilerOptions().GetCompilePic());
5438 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5439 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5440 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5441 DCHECK(GetCompilerOptions().GetCompilePic());
5442 break;
5443 case HLoadClass::LoadKind::kBootImageAddress:
5444 break;
5445 case HLoadClass::LoadKind::kDexCacheAddress:
5446 DCHECK(Runtime::Current()->UseJitCompilation());
5447 break;
5448 case HLoadClass::LoadKind::kDexCachePcRelative:
5449 DCHECK(!Runtime::Current()->UseJitCompilation());
5450 break;
5451 case HLoadClass::LoadKind::kDexCacheViaMethod:
5452 break;
5453 }
5454 return desired_class_load_kind;
5455}
5456
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005457void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005458 if (cls->NeedsAccessCheck()) {
5459 InvokeRuntimeCallingConvention calling_convention;
5460 CodeGenerator::CreateLoadClassLocationSummary(
5461 cls,
5462 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5463 Location::RegisterLocation(RAX),
5464 /* code_generator_supports_read_barrier */ true);
5465 return;
5466 }
5467
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005468 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5469 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005470 ? LocationSummary::kCallOnSlowPath
5471 : LocationSummary::kNoCall;
5472 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005473 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005474 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005475 }
5476
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005477 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5478 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5479 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5480 locations->SetInAt(0, Location::RequiresRegister());
5481 }
5482 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005483}
5484
5485void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005486 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005487 if (cls->NeedsAccessCheck()) {
5488 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01005489 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005490 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005491 return;
5492 }
5493
Roland Levillain0d5a2812015-11-13 10:07:31 +00005494 Location out_loc = locations->Out();
5495 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005496
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005497 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5498 ? kWithoutReadBarrier
5499 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005500 bool generate_null_check = false;
5501 switch (cls->GetLoadKind()) {
5502 case HLoadClass::LoadKind::kReferrersClass: {
5503 DCHECK(!cls->CanCallRuntime());
5504 DCHECK(!cls->MustGenerateClinitCheck());
5505 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5506 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5507 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005508 cls,
5509 out_loc,
5510 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005511 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005512 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005513 break;
5514 }
5515 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005516 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005517 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5518 codegen_->RecordTypePatch(cls);
5519 break;
5520 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005521 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005522 DCHECK_NE(cls->GetAddress(), 0u);
5523 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5524 __ movl(out, Immediate(address)); // Zero-extended.
5525 codegen_->RecordSimplePatch();
5526 break;
5527 }
5528 case HLoadClass::LoadKind::kDexCacheAddress: {
5529 DCHECK_NE(cls->GetAddress(), 0u);
5530 // /* GcRoot<mirror::Class> */ out = *address
5531 if (IsUint<32>(cls->GetAddress())) {
5532 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005533 GenerateGcRootFieldLoad(cls,
5534 out_loc,
5535 address,
Roland Levillain00468f32016-10-27 18:02:48 +01005536 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005537 read_barrier_option);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005538 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005539 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5540 __ movq(out, Immediate(cls->GetAddress()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005541 GenerateGcRootFieldLoad(cls,
5542 out_loc,
5543 Address(out, 0),
Roland Levillain00468f32016-10-27 18:02:48 +01005544 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005545 read_barrier_option);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005546 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005547 generate_null_check = !cls->IsInDexCache();
5548 break;
5549 }
5550 case HLoadClass::LoadKind::kDexCachePcRelative: {
5551 uint32_t offset = cls->GetDexCacheElementOffset();
5552 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5553 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5554 /* no_rip */ false);
5555 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005556 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005557 generate_null_check = !cls->IsInDexCache();
5558 break;
5559 }
5560 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5561 // /* GcRoot<mirror::Class>[] */ out =
5562 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5563 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5564 __ movq(out,
5565 Address(current_method,
5566 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5567 // /* GcRoot<mirror::Class> */ out = out[type_index]
5568 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005569 cls,
5570 out_loc,
5571 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
Roland Levillain00468f32016-10-27 18:02:48 +01005572 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005573 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005574 generate_null_check = !cls->IsInDexCache();
5575 break;
5576 }
5577 default:
5578 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5579 UNREACHABLE();
5580 }
5581
5582 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5583 DCHECK(cls->CanCallRuntime());
5584 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5585 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5586 codegen_->AddSlowPath(slow_path);
5587 if (generate_null_check) {
5588 __ testl(out, out);
5589 __ j(kEqual, slow_path->GetEntryLabel());
5590 }
5591 if (cls->MustGenerateClinitCheck()) {
5592 GenerateClassInitializationCheck(slow_path, out);
5593 } else {
5594 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005595 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005596 }
5597}
5598
5599void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5600 LocationSummary* locations =
5601 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5602 locations->SetInAt(0, Location::RequiresRegister());
5603 if (check->HasUses()) {
5604 locations->SetOut(Location::SameAsFirstInput());
5605 }
5606}
5607
5608void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005609 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005610 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005611 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005612 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005613 GenerateClassInitializationCheck(slow_path,
5614 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005615}
5616
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005617HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5618 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005619 switch (desired_string_load_kind) {
5620 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5621 DCHECK(!GetCompilerOptions().GetCompilePic());
5622 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5623 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5624 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5625 DCHECK(GetCompilerOptions().GetCompilePic());
5626 break;
5627 case HLoadString::LoadKind::kBootImageAddress:
5628 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005629 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005630 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005631 break;
5632 case HLoadString::LoadKind::kDexCacheViaMethod:
5633 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005634 case HLoadString::LoadKind::kJitTableAddress:
5635 DCHECK(Runtime::Current()->UseJitCompilation());
5636 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005637 }
5638 return desired_string_load_kind;
5639}
5640
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005641void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005642 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
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
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005662Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file, uint32_t dex_index) {
5663 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index), /* placeholder */ 0u);
5664 // Add a patch entry and return the label.
5665 jit_string_patches_.emplace_back(dex_file, dex_index);
5666 PatchInfo<Label>* info = &jit_string_patches_.back();
5667 return &info->label;
5668}
5669
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005670void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005671 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005672 Location out_loc = locations->Out();
5673 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005674
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005675 switch (load->GetLoadKind()) {
5676 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005677 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005678 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005679 return; // No dex cache slow path.
5680 }
5681 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005682 DCHECK_NE(load->GetAddress(), 0u);
5683 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5684 __ movl(out, Immediate(address)); // Zero-extended.
5685 codegen_->RecordSimplePatch();
5686 return; // No dex cache slow path.
5687 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005688 case HLoadString::LoadKind::kBssEntry: {
5689 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5690 /* no_rip */ false);
5691 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5692 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005693 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005694 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5695 codegen_->AddSlowPath(slow_path);
5696 __ testl(out, out);
5697 __ j(kEqual, slow_path->GetEntryLabel());
5698 __ Bind(slow_path->GetExitLabel());
5699 return;
5700 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005701 case HLoadString::LoadKind::kJitTableAddress: {
5702 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5703 /* no_rip */ true);
5704 Label* fixup_label =
5705 codegen_->NewJitRootStringPatch(load->GetDexFile(), load->GetStringIndex());
5706 // /* GcRoot<mirror::String> */ out = *address
5707 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
5708 return;
5709 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005710 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005711 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005712 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005713
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005714 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005715 // Custom calling convention: RAX serves as both input and output.
5716 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex()));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005717 codegen_->InvokeRuntime(kQuickResolveString,
5718 load,
5719 load->GetDexPc());
5720 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005721}
5722
David Brazdilcb1c0552015-08-04 16:22:25 +01005723static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005724 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005725 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005726}
5727
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005728void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5729 LocationSummary* locations =
5730 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5731 locations->SetOut(Location::RequiresRegister());
5732}
5733
5734void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005735 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5736}
5737
5738void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5739 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5740}
5741
5742void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5743 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005744}
5745
5746void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5747 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005748 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005749 InvokeRuntimeCallingConvention calling_convention;
5750 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5751}
5752
5753void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005754 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005755 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005756}
5757
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005758static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5759 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005760 // We need a temporary for holding the iftable length.
5761 return true;
5762 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005763 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005764 !kUseBakerReadBarrier &&
5765 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005766 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5767 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5768}
5769
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005770static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5771 return kEmitCompilerReadBarrier &&
5772 !kUseBakerReadBarrier &&
5773 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5774 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5775 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5776}
5777
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005778void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005779 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005780 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005781 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005782 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005783 case TypeCheckKind::kExactCheck:
5784 case TypeCheckKind::kAbstractClassCheck:
5785 case TypeCheckKind::kClassHierarchyCheck:
5786 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005787 call_kind =
5788 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005789 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005790 break;
5791 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005792 case TypeCheckKind::kUnresolvedCheck:
5793 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005794 call_kind = LocationSummary::kCallOnSlowPath;
5795 break;
5796 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005797
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005798 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005799 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005800 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005801 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005802 locations->SetInAt(0, Location::RequiresRegister());
5803 locations->SetInAt(1, Location::Any());
5804 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5805 locations->SetOut(Location::RequiresRegister());
5806 // When read barriers are enabled, we need a temporary register for
5807 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005808 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005809 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005810 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005811}
5812
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005813void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005814 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005815 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005816 Location obj_loc = locations->InAt(0);
5817 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005818 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005819 Location out_loc = locations->Out();
5820 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005821 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005822 locations->GetTemp(0) :
5823 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005824 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005825 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5826 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5827 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005828 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005830
5831 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005832 // Avoid null check if we know obj is not null.
5833 if (instruction->MustDoNullCheck()) {
5834 __ testl(obj, obj);
5835 __ j(kEqual, &zero);
5836 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005837
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005838 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005839 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005840 // /* HeapReference<Class> */ out = obj->klass_
5841 GenerateReferenceLoadTwoRegisters(instruction,
5842 out_loc,
5843 obj_loc,
5844 class_offset,
5845 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005846 if (cls.IsRegister()) {
5847 __ cmpl(out, cls.AsRegister<CpuRegister>());
5848 } else {
5849 DCHECK(cls.IsStackSlot()) << cls;
5850 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5851 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005852 if (zero.IsLinked()) {
5853 // Classes must be equal for the instanceof to succeed.
5854 __ j(kNotEqual, &zero);
5855 __ movl(out, Immediate(1));
5856 __ jmp(&done);
5857 } else {
5858 __ setcc(kEqual, out);
5859 // setcc only sets the low byte.
5860 __ andl(out, Immediate(1));
5861 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005862 break;
5863 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005864
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005865 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005866 // /* HeapReference<Class> */ out = obj->klass_
5867 GenerateReferenceLoadTwoRegisters(instruction,
5868 out_loc,
5869 obj_loc,
5870 class_offset,
5871 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005872 // If the class is abstract, we eagerly fetch the super class of the
5873 // object to avoid doing a comparison we know will fail.
5874 NearLabel loop, success;
5875 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005876 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005877 GenerateReferenceLoadOneRegister(instruction,
5878 out_loc,
5879 super_offset,
5880 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005881 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 __ testl(out, out);
5883 // If `out` is null, we use it for the result, and jump to `done`.
5884 __ j(kEqual, &done);
5885 if (cls.IsRegister()) {
5886 __ cmpl(out, cls.AsRegister<CpuRegister>());
5887 } else {
5888 DCHECK(cls.IsStackSlot()) << cls;
5889 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5890 }
5891 __ j(kNotEqual, &loop);
5892 __ movl(out, Immediate(1));
5893 if (zero.IsLinked()) {
5894 __ jmp(&done);
5895 }
5896 break;
5897 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005898
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005899 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005900 // /* HeapReference<Class> */ out = obj->klass_
5901 GenerateReferenceLoadTwoRegisters(instruction,
5902 out_loc,
5903 obj_loc,
5904 class_offset,
5905 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005906 // Walk over the class hierarchy to find a match.
5907 NearLabel loop, success;
5908 __ Bind(&loop);
5909 if (cls.IsRegister()) {
5910 __ cmpl(out, cls.AsRegister<CpuRegister>());
5911 } else {
5912 DCHECK(cls.IsStackSlot()) << cls;
5913 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5914 }
5915 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005916 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005917 GenerateReferenceLoadOneRegister(instruction,
5918 out_loc,
5919 super_offset,
5920 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005921 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005922 __ testl(out, out);
5923 __ j(kNotEqual, &loop);
5924 // If `out` is null, we use it for the result, and jump to `done`.
5925 __ jmp(&done);
5926 __ Bind(&success);
5927 __ movl(out, Immediate(1));
5928 if (zero.IsLinked()) {
5929 __ jmp(&done);
5930 }
5931 break;
5932 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005933
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005934 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005935 // /* HeapReference<Class> */ out = obj->klass_
5936 GenerateReferenceLoadTwoRegisters(instruction,
5937 out_loc,
5938 obj_loc,
5939 class_offset,
5940 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005941 // Do an exact check.
5942 NearLabel exact_check;
5943 if (cls.IsRegister()) {
5944 __ cmpl(out, cls.AsRegister<CpuRegister>());
5945 } else {
5946 DCHECK(cls.IsStackSlot()) << cls;
5947 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5948 }
5949 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005950 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005951 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005952 GenerateReferenceLoadOneRegister(instruction,
5953 out_loc,
5954 component_offset,
5955 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005956 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957 __ testl(out, out);
5958 // If `out` is null, we use it for the result, and jump to `done`.
5959 __ j(kEqual, &done);
5960 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5961 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005962 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005963 __ movl(out, Immediate(1));
5964 __ jmp(&done);
5965 break;
5966 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005967
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005969 // No read barrier since the slow path will retry upon failure.
5970 // /* HeapReference<Class> */ out = obj->klass_
5971 GenerateReferenceLoadTwoRegisters(instruction,
5972 out_loc,
5973 obj_loc,
5974 class_offset,
5975 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005976 if (cls.IsRegister()) {
5977 __ cmpl(out, cls.AsRegister<CpuRegister>());
5978 } else {
5979 DCHECK(cls.IsStackSlot()) << cls;
5980 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5981 }
5982 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005983 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5984 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005985 codegen_->AddSlowPath(slow_path);
5986 __ j(kNotEqual, slow_path->GetEntryLabel());
5987 __ movl(out, Immediate(1));
5988 if (zero.IsLinked()) {
5989 __ jmp(&done);
5990 }
5991 break;
5992 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005993
Calin Juravle98893e12015-10-02 21:05:03 +01005994 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005995 case TypeCheckKind::kInterfaceCheck: {
5996 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005997 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 // cases.
5999 //
6000 // We cannot directly call the InstanceofNonTrivial runtime
6001 // entry point without resorting to a type checking slow path
6002 // here (i.e. by calling InvokeRuntime directly), as it would
6003 // require to assign fixed registers for the inputs of this
6004 // HInstanceOf instruction (following the runtime calling
6005 // convention), which might be cluttered by the potential first
6006 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006007 //
6008 // TODO: Introduce a new runtime entry point taking the object
6009 // to test (instead of its class) as argument, and let it deal
6010 // with the read barrier issues. This will let us refactor this
6011 // case of the `switch` code as it was previously (with a direct
6012 // call to the runtime not using a type checking slow path).
6013 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006014 DCHECK(locations->OnlyCallsOnSlowPath());
6015 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6016 /* is_fatal */ false);
6017 codegen_->AddSlowPath(slow_path);
6018 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006019 if (zero.IsLinked()) {
6020 __ jmp(&done);
6021 }
6022 break;
6023 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006024 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006025
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006026 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006027 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006028 __ xorl(out, out);
6029 }
6030
6031 if (done.IsLinked()) {
6032 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006033 }
6034
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006035 if (slow_path != nullptr) {
6036 __ Bind(slow_path->GetExitLabel());
6037 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006038}
6039
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006040static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006041 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006042 case TypeCheckKind::kExactCheck:
6043 case TypeCheckKind::kAbstractClassCheck:
6044 case TypeCheckKind::kClassHierarchyCheck:
6045 case TypeCheckKind::kArrayObjectCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006046 return !throws_into_catch && !kEmitCompilerReadBarrier;
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006047 case TypeCheckKind::kInterfaceCheck:
6048 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006049 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006050 case TypeCheckKind::kUnresolvedCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006051 return false;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006052 }
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006053 LOG(FATAL) << "Unreachable";
6054 UNREACHABLE();
6055}
6056
6057void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
6058 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6059 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6060 bool is_fatal_slow_path = IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch);
6061 LocationSummary::CallKind call_kind = is_fatal_slow_path
6062 ? LocationSummary::kNoCall
6063 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006064 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6065 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006066 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6067 // Require a register for the interface check since there is a loop that compares the class to
6068 // a memory address.
6069 locations->SetInAt(1, Location::RequiresRegister());
6070 } else {
6071 locations->SetInAt(1, Location::Any());
6072 }
6073
Roland Levillain0d5a2812015-11-13 10:07:31 +00006074 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6075 locations->AddTemp(Location::RequiresRegister());
6076 // When read barriers are enabled, we need an additional temporary
6077 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006078 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006079 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006081}
6082
6083void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006084 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006085 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006086 Location obj_loc = locations->InAt(0);
6087 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006088 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006089 Location temp_loc = locations->GetTemp(0);
6090 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006091 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006092 locations->GetTemp(1) :
6093 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006094 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6095 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6096 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6097 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6098 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6099 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006100 const uint32_t object_array_data_offset =
6101 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006102
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006103 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6104 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6105 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106 bool is_type_check_slow_path_fatal =
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006107 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006108 SlowPathCode* type_check_slow_path =
6109 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6110 is_type_check_slow_path_fatal);
6111 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006112
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006113
6114 NearLabel done;
6115 // Avoid null check if we know obj is not null.
6116 if (instruction->MustDoNullCheck()) {
6117 __ testl(obj, obj);
6118 __ j(kEqual, &done);
6119 }
6120
Roland Levillain0d5a2812015-11-13 10:07:31 +00006121 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006122 case TypeCheckKind::kExactCheck:
6123 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006124 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006125 GenerateReferenceLoadTwoRegisters(instruction,
6126 temp_loc,
6127 obj_loc,
6128 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006129 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006130 if (cls.IsRegister()) {
6131 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6132 } else {
6133 DCHECK(cls.IsStackSlot()) << cls;
6134 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6135 }
6136 // Jump to slow path for throwing the exception or doing a
6137 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006138 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006139 break;
6140 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006141
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006143 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006144 GenerateReferenceLoadTwoRegisters(instruction,
6145 temp_loc,
6146 obj_loc,
6147 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006148 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149 // If the class is abstract, we eagerly fetch the super class of the
6150 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006151 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006153 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006154 GenerateReferenceLoadOneRegister(instruction,
6155 temp_loc,
6156 super_offset,
6157 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006158 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006159
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006160 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6161 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006162 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006163 // Otherwise, compare the classes.
6164 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 if (cls.IsRegister()) {
6166 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6167 } else {
6168 DCHECK(cls.IsStackSlot()) << cls;
6169 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6170 }
6171 __ j(kNotEqual, &loop);
6172 break;
6173 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006174
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006175 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006176 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006177 GenerateReferenceLoadTwoRegisters(instruction,
6178 temp_loc,
6179 obj_loc,
6180 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006181 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006183 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184 __ Bind(&loop);
6185 if (cls.IsRegister()) {
6186 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6187 } else {
6188 DCHECK(cls.IsStackSlot()) << cls;
6189 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6190 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006191 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006192
Roland Levillain0d5a2812015-11-13 10:07:31 +00006193 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006194 GenerateReferenceLoadOneRegister(instruction,
6195 temp_loc,
6196 super_offset,
6197 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006198 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006199
6200 // If the class reference currently in `temp` is not null, jump
6201 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006203 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006204 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006205 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 break;
6207 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006208
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006209 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006210 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006211 GenerateReferenceLoadTwoRegisters(instruction,
6212 temp_loc,
6213 obj_loc,
6214 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006215 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006216 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006217 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006218 if (cls.IsRegister()) {
6219 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6220 } else {
6221 DCHECK(cls.IsStackSlot()) << cls;
6222 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6223 }
6224 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006225
6226 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006227 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006228 GenerateReferenceLoadOneRegister(instruction,
6229 temp_loc,
6230 component_offset,
6231 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006232 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006233
6234 // If the component type is not null (i.e. the object is indeed
6235 // an array), jump to label `check_non_primitive_component_type`
6236 // to further check that this component type is not a primitive
6237 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006238 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006239 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006240 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006241 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006242 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 break;
6244 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006245
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006246 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006247 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006248 //
6249 // We cannot directly call the CheckCast runtime entry point
6250 // without resorting to a type checking slow path here (i.e. by
6251 // calling InvokeRuntime directly), as it would require to
6252 // assign fixed registers for the inputs of this HInstanceOf
6253 // instruction (following the runtime calling convention), which
6254 // might be cluttered by the potential first read barrier
6255 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006256 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006257 break;
6258 }
6259
6260 case TypeCheckKind::kInterfaceCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006261 // Fast path for the interface check. We always go slow path for heap poisoning since
6262 // unpoisoning cls would require an extra temp.
6263 if (!kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006264 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6265 // doing this.
6266 // /* HeapReference<Class> */ temp = obj->klass_
6267 GenerateReferenceLoadTwoRegisters(instruction,
6268 temp_loc,
6269 obj_loc,
6270 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006271 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006272
6273 // /* HeapReference<Class> */ temp = temp->iftable_
6274 GenerateReferenceLoadTwoRegisters(instruction,
6275 temp_loc,
6276 temp_loc,
6277 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006278 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006279 // Null iftable means it is empty.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006280 __ testl(temp, temp);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006281 __ j(kZero, type_check_slow_path->GetEntryLabel());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006282
6283 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006284 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006285
6286 NearLabel start_loop;
6287 __ Bind(&start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006288 __ cmpl(cls.AsRegister<CpuRegister>(), Address(temp, object_array_data_offset));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006289 __ j(kEqual, &done); // Return if same class.
6290 // Go to next interface.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006291 __ addl(temp, Immediate(2 * kHeapReferenceSize));
6292 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006293 __ j(kNotZero, &start_loop);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006294 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006295 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006296 break;
6297 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006298
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006299 if (done.IsLinked()) {
6300 __ Bind(&done);
6301 }
6302
Roland Levillain0d5a2812015-11-13 10:07:31 +00006303 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006304}
6305
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006306void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6307 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006308 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006309 InvokeRuntimeCallingConvention calling_convention;
6310 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6311}
6312
6313void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006314 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006315 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006316 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006317 if (instruction->IsEnter()) {
6318 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6319 } else {
6320 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6321 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006322}
6323
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006324void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6325void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6326void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6327
6328void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6329 LocationSummary* locations =
6330 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6331 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6332 || instruction->GetResultType() == Primitive::kPrimLong);
6333 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006334 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006335 locations->SetOut(Location::SameAsFirstInput());
6336}
6337
6338void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6339 HandleBitwiseOperation(instruction);
6340}
6341
6342void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6343 HandleBitwiseOperation(instruction);
6344}
6345
6346void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6347 HandleBitwiseOperation(instruction);
6348}
6349
6350void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6351 LocationSummary* locations = instruction->GetLocations();
6352 Location first = locations->InAt(0);
6353 Location second = locations->InAt(1);
6354 DCHECK(first.Equals(locations->Out()));
6355
6356 if (instruction->GetResultType() == Primitive::kPrimInt) {
6357 if (second.IsRegister()) {
6358 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006359 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006360 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006361 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006362 } else {
6363 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006364 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006365 }
6366 } else if (second.IsConstant()) {
6367 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6368 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006369 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006370 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006371 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006372 } else {
6373 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006374 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006375 }
6376 } else {
6377 Address address(CpuRegister(RSP), second.GetStackIndex());
6378 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006379 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006380 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006381 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006382 } else {
6383 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006384 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006385 }
6386 }
6387 } else {
6388 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006389 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6390 bool second_is_constant = false;
6391 int64_t value = 0;
6392 if (second.IsConstant()) {
6393 second_is_constant = true;
6394 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006395 }
Mark Mendell40741f32015-04-20 22:10:34 -04006396 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006397
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006398 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006399 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006400 if (is_int32_value) {
6401 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6402 } else {
6403 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6404 }
6405 } else if (second.IsDoubleStackSlot()) {
6406 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006407 } else {
6408 __ andq(first_reg, second.AsRegister<CpuRegister>());
6409 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006410 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006411 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006412 if (is_int32_value) {
6413 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6414 } else {
6415 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6416 }
6417 } else if (second.IsDoubleStackSlot()) {
6418 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006419 } else {
6420 __ orq(first_reg, second.AsRegister<CpuRegister>());
6421 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006422 } else {
6423 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006424 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006425 if (is_int32_value) {
6426 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6427 } else {
6428 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6429 }
6430 } else if (second.IsDoubleStackSlot()) {
6431 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006432 } else {
6433 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6434 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006435 }
6436 }
6437}
6438
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006439void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6440 HInstruction* instruction,
6441 Location out,
6442 uint32_t offset,
6443 Location maybe_temp,
6444 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006445 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006446 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006447 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006448 if (kUseBakerReadBarrier) {
6449 // Load with fast path based Baker's read barrier.
6450 // /* HeapReference<Object> */ out = *(out + offset)
6451 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006452 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006453 } else {
6454 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006455 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006456 // in the following move operation, as we will need it for the
6457 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006458 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006459 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006460 // /* HeapReference<Object> */ out = *(out + offset)
6461 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006462 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006463 }
6464 } else {
6465 // Plain load with no read barrier.
6466 // /* HeapReference<Object> */ out = *(out + offset)
6467 __ movl(out_reg, Address(out_reg, offset));
6468 __ MaybeUnpoisonHeapReference(out_reg);
6469 }
6470}
6471
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006472void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6473 HInstruction* instruction,
6474 Location out,
6475 Location obj,
6476 uint32_t offset,
6477 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006478 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6479 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006480 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006481 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006482 if (kUseBakerReadBarrier) {
6483 // Load with fast path based Baker's read barrier.
6484 // /* HeapReference<Object> */ out = *(obj + offset)
6485 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006486 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006487 } else {
6488 // Load with slow path based read barrier.
6489 // /* HeapReference<Object> */ out = *(obj + offset)
6490 __ movl(out_reg, Address(obj_reg, offset));
6491 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6492 }
6493 } else {
6494 // Plain load with no read barrier.
6495 // /* HeapReference<Object> */ out = *(obj + offset)
6496 __ movl(out_reg, Address(obj_reg, offset));
6497 __ MaybeUnpoisonHeapReference(out_reg);
6498 }
6499}
6500
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006501void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6502 HInstruction* instruction,
6503 Location root,
6504 const Address& address,
6505 Label* fixup_label,
6506 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006507 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006508 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006509 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006510 if (kUseBakerReadBarrier) {
6511 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6512 // Baker's read barrier are used:
6513 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006514 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006515 // if (Thread::Current()->GetIsGcMarking()) {
6516 // root = ReadBarrier::Mark(root)
6517 // }
6518
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006519 // /* GcRoot<mirror::Object> */ root = *address
6520 __ movl(root_reg, address);
6521 if (fixup_label != nullptr) {
6522 __ Bind(fixup_label);
6523 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006524 static_assert(
6525 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6526 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6527 "have different sizes.");
6528 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6529 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6530 "have different sizes.");
6531
Vladimir Marko953437b2016-08-24 08:30:46 +00006532 // Slow path marking the GC root `root`.
6533 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006534 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006535 codegen_->AddSlowPath(slow_path);
6536
Andreas Gampe542451c2016-07-26 09:02:02 -07006537 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006538 /* no_rip */ true),
6539 Immediate(0));
6540 __ j(kNotEqual, slow_path->GetEntryLabel());
6541 __ Bind(slow_path->GetExitLabel());
6542 } else {
6543 // GC root loaded through a slow path for read barriers other
6544 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006545 // /* GcRoot<mirror::Object>* */ root = address
6546 __ leaq(root_reg, address);
6547 if (fixup_label != nullptr) {
6548 __ Bind(fixup_label);
6549 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006550 // /* mirror::Object* */ root = root->Read()
6551 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6552 }
6553 } else {
6554 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006555 // /* GcRoot<mirror::Object> */ root = *address
6556 __ movl(root_reg, address);
6557 if (fixup_label != nullptr) {
6558 __ Bind(fixup_label);
6559 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006560 // Note that GC roots are not affected by heap poisoning, thus we
6561 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006562 }
6563}
6564
6565void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6566 Location ref,
6567 CpuRegister obj,
6568 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006569 bool needs_null_check) {
6570 DCHECK(kEmitCompilerReadBarrier);
6571 DCHECK(kUseBakerReadBarrier);
6572
6573 // /* HeapReference<Object> */ ref = *(obj + offset)
6574 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006575 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006576}
6577
6578void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6579 Location ref,
6580 CpuRegister obj,
6581 uint32_t data_offset,
6582 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006583 bool needs_null_check) {
6584 DCHECK(kEmitCompilerReadBarrier);
6585 DCHECK(kUseBakerReadBarrier);
6586
Roland Levillain3d312422016-06-23 13:53:42 +01006587 static_assert(
6588 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6589 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006590 // /* HeapReference<Object> */ ref =
6591 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006592 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006593 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006594}
6595
6596void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6597 Location ref,
6598 CpuRegister obj,
6599 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006600 bool needs_null_check,
6601 bool always_update_field,
6602 CpuRegister* temp1,
6603 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006604 DCHECK(kEmitCompilerReadBarrier);
6605 DCHECK(kUseBakerReadBarrier);
6606
6607 // In slow path based read barriers, the read barrier call is
6608 // inserted after the original load. However, in fast path based
6609 // Baker's read barriers, we need to perform the load of
6610 // mirror::Object::monitor_ *before* the original reference load.
6611 // This load-load ordering is required by the read barrier.
6612 // The fast path/slow path (for Baker's algorithm) should look like:
6613 //
6614 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6615 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6616 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006617 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006618 // if (is_gray) {
6619 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6620 // }
6621 //
6622 // Note: the original implementation in ReadBarrier::Barrier is
6623 // slightly more complex as:
6624 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006625 // the high-bits of rb_state, which are expected to be all zeroes
6626 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6627 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006628 // - it performs additional checks that we do not do here for
6629 // performance reasons.
6630
6631 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006632 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6633
Vladimir Marko953437b2016-08-24 08:30:46 +00006634 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006635 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6636 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006637 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6638 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6639 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6640
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006641 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006642 // ref = ReadBarrier::Mark(ref);
6643 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6644 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006645 if (needs_null_check) {
6646 MaybeRecordImplicitNullCheck(instruction);
6647 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006648
6649 // Load fence to prevent load-load reordering.
6650 // Note that this is a no-op, thanks to the x86-64 memory model.
6651 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6652
6653 // The actual reference load.
6654 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006655 __ movl(ref_reg, src); // Flags are unaffected.
6656
6657 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6658 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006659 SlowPathCode* slow_path;
6660 if (always_update_field) {
6661 DCHECK(temp1 != nullptr);
6662 DCHECK(temp2 != nullptr);
6663 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
6664 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6665 } else {
6666 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6667 instruction, ref, /* unpoison_ref_before_marking */ true);
6668 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006669 AddSlowPath(slow_path);
6670
6671 // We have done the "if" of the gray bit check above, now branch based on the flags.
6672 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006673
6674 // Object* ref = ref_addr->AsMirrorPtr()
6675 __ MaybeUnpoisonHeapReference(ref_reg);
6676
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006677 __ Bind(slow_path->GetExitLabel());
6678}
6679
6680void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6681 Location out,
6682 Location ref,
6683 Location obj,
6684 uint32_t offset,
6685 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 DCHECK(kEmitCompilerReadBarrier);
6687
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006688 // Insert a slow path based read barrier *after* the reference load.
6689 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006690 // If heap poisoning is enabled, the unpoisoning of the loaded
6691 // reference will be carried out by the runtime within the slow
6692 // path.
6693 //
6694 // Note that `ref` currently does not get unpoisoned (when heap
6695 // poisoning is enabled), which is alright as the `ref` argument is
6696 // not used by the artReadBarrierSlow entry point.
6697 //
6698 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6699 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6700 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6701 AddSlowPath(slow_path);
6702
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703 __ jmp(slow_path->GetEntryLabel());
6704 __ Bind(slow_path->GetExitLabel());
6705}
6706
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006707void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6708 Location out,
6709 Location ref,
6710 Location obj,
6711 uint32_t offset,
6712 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006713 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006714 // Baker's read barriers shall be handled by the fast path
6715 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6716 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006717 // If heap poisoning is enabled, unpoisoning will be taken care of
6718 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006719 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006720 } else if (kPoisonHeapReferences) {
6721 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6722 }
6723}
6724
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006725void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6726 Location out,
6727 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006728 DCHECK(kEmitCompilerReadBarrier);
6729
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006730 // Insert a slow path based read barrier *after* the GC root load.
6731 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006732 // Note that GC roots are not affected by heap poisoning, so we do
6733 // not need to do anything special for this here.
6734 SlowPathCode* slow_path =
6735 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6736 AddSlowPath(slow_path);
6737
Roland Levillain0d5a2812015-11-13 10:07:31 +00006738 __ jmp(slow_path->GetEntryLabel());
6739 __ Bind(slow_path->GetExitLabel());
6740}
6741
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006742void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006743 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006744 LOG(FATAL) << "Unreachable";
6745}
6746
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006747void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006748 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006749 LOG(FATAL) << "Unreachable";
6750}
6751
Mark Mendellfe57faa2015-09-18 09:26:15 -04006752// Simple implementation of packed switch - generate cascaded compare/jumps.
6753void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6754 LocationSummary* locations =
6755 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6756 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006757 locations->AddTemp(Location::RequiresRegister());
6758 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006759}
6760
6761void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6762 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006763 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006764 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006765 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6766 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6767 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006768 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6769
6770 // Should we generate smaller inline compare/jumps?
6771 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6772 // Figure out the correct compare values and jump conditions.
6773 // Handle the first compare/branch as a special case because it might
6774 // jump to the default case.
6775 DCHECK_GT(num_entries, 2u);
6776 Condition first_condition;
6777 uint32_t index;
6778 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6779 if (lower_bound != 0) {
6780 first_condition = kLess;
6781 __ cmpl(value_reg_in, Immediate(lower_bound));
6782 __ j(first_condition, codegen_->GetLabelOf(default_block));
6783 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6784
6785 index = 1;
6786 } else {
6787 // Handle all the compare/jumps below.
6788 first_condition = kBelow;
6789 index = 0;
6790 }
6791
6792 // Handle the rest of the compare/jumps.
6793 for (; index + 1 < num_entries; index += 2) {
6794 int32_t compare_to_value = lower_bound + index + 1;
6795 __ cmpl(value_reg_in, Immediate(compare_to_value));
6796 // Jump to successors[index] if value < case_value[index].
6797 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6798 // Jump to successors[index + 1] if value == case_value[index + 1].
6799 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6800 }
6801
6802 if (index != num_entries) {
6803 // There are an odd number of entries. Handle the last one.
6804 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006805 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006806 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6807 }
6808
6809 // And the default for any other value.
6810 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6811 __ jmp(codegen_->GetLabelOf(default_block));
6812 }
6813 return;
6814 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006815
6816 // Remove the bias, if needed.
6817 Register value_reg_out = value_reg_in.AsRegister();
6818 if (lower_bound != 0) {
6819 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6820 value_reg_out = temp_reg.AsRegister();
6821 }
6822 CpuRegister value_reg(value_reg_out);
6823
6824 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006825 __ cmpl(value_reg, Immediate(num_entries - 1));
6826 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006827
Mark Mendell9c86b482015-09-18 13:36:07 -04006828 // We are in the range of the table.
6829 // Load the address of the jump table in the constant area.
6830 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006831
Mark Mendell9c86b482015-09-18 13:36:07 -04006832 // Load the (signed) offset from the jump table.
6833 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6834
6835 // Add the offset to the address of the table base.
6836 __ addq(temp_reg, base_reg);
6837
6838 // And jump.
6839 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006840}
6841
Aart Bikc5d47542016-01-27 17:00:35 -08006842void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6843 if (value == 0) {
6844 __ xorl(dest, dest);
6845 } else {
6846 __ movl(dest, Immediate(value));
6847 }
6848}
6849
Mark Mendell92e83bf2015-05-07 11:25:03 -04006850void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6851 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006852 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006853 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006854 } else if (IsUint<32>(value)) {
6855 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006856 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6857 } else {
6858 __ movq(dest, Immediate(value));
6859 }
6860}
6861
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006862void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6863 if (value == 0) {
6864 __ xorps(dest, dest);
6865 } else {
6866 __ movss(dest, LiteralInt32Address(value));
6867 }
6868}
6869
6870void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6871 if (value == 0) {
6872 __ xorpd(dest, dest);
6873 } else {
6874 __ movsd(dest, LiteralInt64Address(value));
6875 }
6876}
6877
6878void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6879 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6880}
6881
6882void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6883 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6884}
6885
Aart Bika19616e2016-02-01 18:57:58 -08006886void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6887 if (value == 0) {
6888 __ testl(dest, dest);
6889 } else {
6890 __ cmpl(dest, Immediate(value));
6891 }
6892}
6893
6894void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6895 if (IsInt<32>(value)) {
6896 if (value == 0) {
6897 __ testq(dest, dest);
6898 } else {
6899 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6900 }
6901 } else {
6902 // Value won't fit in an int.
6903 __ cmpq(dest, LiteralInt64Address(value));
6904 }
6905}
6906
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006907void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6908 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006909 GenerateIntCompare(lhs_reg, rhs);
6910}
6911
6912void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006913 if (rhs.IsConstant()) {
6914 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006915 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006916 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006917 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006918 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006919 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006920 }
6921}
6922
6923void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6924 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6925 if (rhs.IsConstant()) {
6926 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6927 Compare64BitValue(lhs_reg, value);
6928 } else if (rhs.IsDoubleStackSlot()) {
6929 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6930 } else {
6931 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6932 }
6933}
6934
6935Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6936 Location index,
6937 ScaleFactor scale,
6938 uint32_t data_offset) {
6939 return index.IsConstant() ?
6940 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6941 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6942}
6943
Mark Mendellcfa410b2015-05-25 16:02:44 -04006944void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6945 DCHECK(dest.IsDoubleStackSlot());
6946 if (IsInt<32>(value)) {
6947 // Can move directly as an int32 constant.
6948 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6949 Immediate(static_cast<int32_t>(value)));
6950 } else {
6951 Load64BitValue(CpuRegister(TMP), value);
6952 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6953 }
6954}
6955
Mark Mendell9c86b482015-09-18 13:36:07 -04006956/**
6957 * Class to handle late fixup of offsets into constant area.
6958 */
6959class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6960 public:
6961 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6962 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6963
6964 protected:
6965 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6966
6967 CodeGeneratorX86_64* codegen_;
6968
6969 private:
6970 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6971 // Patch the correct offset for the instruction. We use the address of the
6972 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6973 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6974 int32_t relative_position = constant_offset - pos;
6975
6976 // Patch in the right value.
6977 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6978 }
6979
6980 // Location in constant area that the fixup refers to.
6981 size_t offset_into_constant_area_;
6982};
6983
6984/**
6985 t * Class to handle late fixup of offsets to a jump table that will be created in the
6986 * constant area.
6987 */
6988class JumpTableRIPFixup : public RIPFixup {
6989 public:
6990 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6991 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6992
6993 void CreateJumpTable() {
6994 X86_64Assembler* assembler = codegen_->GetAssembler();
6995
6996 // Ensure that the reference to the jump table has the correct offset.
6997 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6998 SetOffset(offset_in_constant_table);
6999
7000 // Compute the offset from the start of the function to this jump table.
7001 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7002
7003 // Populate the jump table with the correct values for the jump table.
7004 int32_t num_entries = switch_instr_->GetNumEntries();
7005 HBasicBlock* block = switch_instr_->GetBlock();
7006 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7007 // The value that we want is the target offset - the position of the table.
7008 for (int32_t i = 0; i < num_entries; i++) {
7009 HBasicBlock* b = successors[i];
7010 Label* l = codegen_->GetLabelOf(b);
7011 DCHECK(l->IsBound());
7012 int32_t offset_to_block = l->Position() - current_table_offset;
7013 assembler->AppendInt32(offset_to_block);
7014 }
7015 }
7016
7017 private:
7018 const HPackedSwitch* switch_instr_;
7019};
7020
Mark Mendellf55c3e02015-03-26 21:07:46 -04007021void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7022 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007023 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007024 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7025 // 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 -04007026 assembler->Align(4, 0);
7027 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007028
7029 // Populate any jump tables.
7030 for (auto jump_table : fixups_to_jump_tables_) {
7031 jump_table->CreateJumpTable();
7032 }
7033
7034 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007035 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007036 }
7037
7038 // And finish up.
7039 CodeGenerator::Finalize(allocator);
7040}
7041
Mark Mendellf55c3e02015-03-26 21:07:46 -04007042Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
7043 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7044 return Address::RIP(fixup);
7045}
7046
7047Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
7048 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7049 return Address::RIP(fixup);
7050}
7051
7052Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
7053 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7054 return Address::RIP(fixup);
7055}
7056
7057Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
7058 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7059 return Address::RIP(fixup);
7060}
7061
Andreas Gampe85b62f22015-09-09 13:15:38 -07007062// TODO: trg as memory.
7063void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7064 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007065 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007066 return;
7067 }
7068
7069 DCHECK_NE(type, Primitive::kPrimVoid);
7070
7071 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7072 if (trg.Equals(return_loc)) {
7073 return;
7074 }
7075
7076 // Let the parallel move resolver take care of all of this.
7077 HParallelMove parallel_move(GetGraph()->GetArena());
7078 parallel_move.AddMove(return_loc, trg, type, nullptr);
7079 GetMoveResolver()->EmitNativeCode(&parallel_move);
7080}
7081
Mark Mendell9c86b482015-09-18 13:36:07 -04007082Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7083 // Create a fixup to be used to create and address the jump table.
7084 JumpTableRIPFixup* table_fixup =
7085 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7086
7087 // We have to populate the jump tables.
7088 fixups_to_jump_tables_.push_back(table_fixup);
7089 return Address::RIP(table_fixup);
7090}
7091
Mark Mendellea5af682015-10-22 17:35:49 -04007092void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7093 const Address& addr_high,
7094 int64_t v,
7095 HInstruction* instruction) {
7096 if (IsInt<32>(v)) {
7097 int32_t v_32 = v;
7098 __ movq(addr_low, Immediate(v_32));
7099 MaybeRecordImplicitNullCheck(instruction);
7100 } else {
7101 // Didn't fit in a register. Do it in pieces.
7102 int32_t low_v = Low32Bits(v);
7103 int32_t high_v = High32Bits(v);
7104 __ movl(addr_low, Immediate(low_v));
7105 MaybeRecordImplicitNullCheck(instruction);
7106 __ movl(addr_high, Immediate(high_v));
7107 }
7108}
7109
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007110void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7111 for (const PatchInfo<Label>& info : jit_string_patches_) {
7112 const auto& it = jit_string_roots_.find(StringReference(&info.dex_file, info.index));
7113 DCHECK(it != jit_string_roots_.end());
7114 size_t index_in_table = it->second;
7115 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7116 uintptr_t address =
7117 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7118 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7119 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7120 dchecked_integral_cast<uint32_t>(address);
7121 }
7122}
7123
Roland Levillain4d027112015-07-01 15:41:14 +01007124#undef __
7125
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007126} // namespace x86_64
7127} // namespace art