blob: bf1c42ae8e07f927e6c42c6952aa0b7a6eb708bc [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 {
Aart Bikb13c65b2017-03-21 20:14:07 -0700143 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000144 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700146 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100147 x86_64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000148 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700149 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100150 if (successor_ == nullptr) {
151 __ jmp(GetReturnLabel());
152 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000153 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100154 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155 }
156
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 Label* GetReturnLabel() {
158 DCHECK(successor_ == nullptr);
159 return &return_label_;
160 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100162 HBasicBlock* GetSuccessor() const {
163 return successor_;
164 }
165
Alexandre Rames9931f312015-06-19 14:47:01 +0100166 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
167
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100169 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170 Label return_label_;
171
172 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
173};
174
Andreas Gampe85b62f22015-09-09 13:15:38 -0700175class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100177 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000178 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000180 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100181 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000182 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000184 if (instruction_->CanThrowIntoCatchBlock()) {
185 // Live registers will be restored in the catch block if caught.
186 SaveLiveRegisters(codegen, instruction_->GetLocations());
187 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400188 // Are we using an array length from memory?
189 HInstruction* array_length = instruction_->InputAt(1);
190 Location length_loc = locations->InAt(1);
191 InvokeRuntimeCallingConvention calling_convention;
192 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
193 // Load the array length into our temporary.
194 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
195 Location array_loc = array_length->GetLocations()->InAt(0);
196 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
197 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
198 // Check for conflicts with index.
199 if (length_loc.Equals(locations->InAt(0))) {
200 // We know we aren't using parameter 2.
201 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
202 }
203 __ movl(length_loc.AsRegister<CpuRegister>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700204 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100205 __ shrl(length_loc.AsRegister<CpuRegister>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700206 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400207 }
208
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000209 // We're moving two locations to locations that could overlap, so we need a parallel
210 // move resolver.
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000211 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100212 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000213 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100214 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400215 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100216 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
217 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100218 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
219 ? kQuickThrowStringBounds
220 : kQuickThrowArrayBounds;
221 x86_64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100222 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000223 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100224 }
225
Alexandre Rames8158f282015-08-07 10:26:17 +0100226 bool IsFatal() const OVERRIDE { return true; }
227
Alexandre Rames9931f312015-06-19 14:47:01 +0100228 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
229
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100230 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100231 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
232};
233
Andreas Gampe85b62f22015-09-09 13:15:38 -0700234class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236 LoadClassSlowPathX86_64(HLoadClass* cls,
237 HInstruction* at,
238 uint32_t dex_pc,
239 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000240 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
242 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100243
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000244 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000245 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000246 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100247 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100248
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000249 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250
Vladimir Markoea4c1262017-02-06 19:59:33 +0000251 // Custom calling convention: RAX serves as both input and output.
252 __ movl(CpuRegister(RAX), Immediate(cls_->GetTypeIndex().index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100253 x86_64_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000254 instruction_,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000255 dex_pc_,
256 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000257 if (do_clinit_) {
258 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
259 } else {
260 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
261 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100262
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000264 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000265 if (out.IsValid()) {
266 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000267 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268 }
269
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000270 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000271 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
272 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
273 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
274 DCHECK(out.IsValid());
275 __ movl(Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false),
276 locations->Out().AsRegister<CpuRegister>());
277 Label* fixup_label = x86_64_codegen->NewTypeBssEntryPatch(cls_);
278 __ Bind(fixup_label);
279 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100280 __ jmp(GetExitLabel());
281 }
282
Alexandre Rames9931f312015-06-19 14:47:01 +0100283 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
284
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100285 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286 // The class this slow path will load.
287 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100288
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000289 // The dex PC of `at_`.
290 const uint32_t dex_pc_;
291
292 // Whether to initialize the class.
293 const bool do_clinit_;
294
295 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100296};
297
Vladimir Markoaad75c62016-10-03 08:46:48 +0000298class LoadStringSlowPathX86_64 : public SlowPathCode {
299 public:
300 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : SlowPathCode(instruction) {}
301
302 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
303 LocationSummary* locations = instruction_->GetLocations();
304 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
305
306 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
307 __ Bind(GetEntryLabel());
308 SaveLiveRegisters(codegen, locations);
309
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000310 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100311 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000312 __ movl(CpuRegister(RAX), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000313 x86_64_codegen->InvokeRuntime(kQuickResolveString,
314 instruction_,
315 instruction_->GetDexPc(),
316 this);
317 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
318 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
319 RestoreLiveRegisters(codegen, locations);
320
321 // Store the resolved String to the BSS entry.
322 __ movl(Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false),
323 locations->Out().AsRegister<CpuRegister>());
324 Label* fixup_label = x86_64_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
325 __ Bind(fixup_label);
326
327 __ jmp(GetExitLabel());
328 }
329
330 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
331
332 private:
333 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
334};
335
Andreas Gampe85b62f22015-09-09 13:15:38 -0700336class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000338 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000339 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000341 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100343 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 DCHECK(instruction_->IsCheckCast()
345 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
Roland Levillain0d5a2812015-11-13 10:07:31 +0000347 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000349
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000350 if (!is_fatal_) {
351 SaveLiveRegisters(codegen, locations);
352 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353
354 // We're moving two locations to locations that could overlap, so we need a parallel
355 // move resolver.
356 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800357 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
359 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000363 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100364 x86_64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800365 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000366 } else {
367 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800368 x86_64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
369 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000370 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 if (!is_fatal_) {
373 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000374 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000375 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000377 RestoreLiveRegisters(codegen, locations);
378 __ jmp(GetExitLabel());
379 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000380 }
381
Alexandre Rames9931f312015-06-19 14:47:01 +0100382 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
383
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000384 bool IsFatal() const OVERRIDE { return is_fatal_; }
385
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000386 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000387 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000388
389 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
390};
391
Andreas Gampe85b62f22015-09-09 13:15:38 -0700392class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393 public:
Aart Bik42249c32016-01-07 15:33:50 -0800394 explicit DeoptimizationSlowPathX86_64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000395 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000398 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100400 LocationSummary* locations = instruction_->GetLocations();
401 SaveLiveRegisters(codegen, locations);
402 InvokeRuntimeCallingConvention calling_convention;
403 x86_64_codegen->Load32BitValue(
404 CpuRegister(calling_convention.GetRegisterAt(0)),
405 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100406 x86_64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100407 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700408 }
409
Alexandre Rames9931f312015-06-19 14:47:01 +0100410 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
411
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700412 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700413 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
414};
415
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416class ArraySetSlowPathX86_64 : public SlowPathCode {
417 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000418 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100419
420 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
421 LocationSummary* locations = instruction_->GetLocations();
422 __ Bind(GetEntryLabel());
423 SaveLiveRegisters(codegen, locations);
424
425 InvokeRuntimeCallingConvention calling_convention;
426 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
427 parallel_move.AddMove(
428 locations->InAt(0),
429 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
430 Primitive::kPrimNot,
431 nullptr);
432 parallel_move.AddMove(
433 locations->InAt(1),
434 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
435 Primitive::kPrimInt,
436 nullptr);
437 parallel_move.AddMove(
438 locations->InAt(2),
439 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
440 Primitive::kPrimNot,
441 nullptr);
442 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
443
Roland Levillain0d5a2812015-11-13 10:07:31 +0000444 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100445 x86_64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000446 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100447 RestoreLiveRegisters(codegen, locations);
448 __ jmp(GetExitLabel());
449 }
450
451 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
452
453 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100454 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
455};
456
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100457// Slow path marking an object reference `ref` during a read
458// barrier. The field `obj.field` in the object `obj` holding this
459// reference does not get updated by this slow path after marking (see
460// ReadBarrierMarkAndUpdateFieldSlowPathX86_64 below for that).
461//
462// This means that after the execution of this slow path, `ref` will
463// always be up-to-date, but `obj.field` may not; i.e., after the
464// flip, `ref` will be a to-space reference, but `obj.field` will
465// probably still be a from-space reference (unless it gets updated by
466// another thread, or if another thread installed another object
467// reference (different from `ref`) in `obj.field`).
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000468class ReadBarrierMarkSlowPathX86_64 : public SlowPathCode {
469 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100470 ReadBarrierMarkSlowPathX86_64(HInstruction* instruction,
471 Location ref,
472 bool unpoison_ref_before_marking)
473 : SlowPathCode(instruction),
474 ref_(ref),
475 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000476 DCHECK(kEmitCompilerReadBarrier);
477 }
478
479 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86_64"; }
480
481 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
482 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100483 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
484 Register ref_reg = ref_cpu_reg.AsRegister();
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000485 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100486 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000487 DCHECK(instruction_->IsInstanceFieldGet() ||
488 instruction_->IsStaticFieldGet() ||
489 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100490 instruction_->IsArraySet() ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000491 instruction_->IsLoadClass() ||
492 instruction_->IsLoadString() ||
493 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100494 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100495 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
496 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000497 << "Unexpected instruction in read barrier marking slow path: "
498 << instruction_->DebugName();
499
500 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100501 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000502 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000504 }
Roland Levillain4359e612016-07-20 11:32:19 +0100505 // No need to save live registers; it's taken care of by the
506 // entrypoint. Also, there is no need to update the stack mask,
507 // as this runtime call will not trigger a garbage collection.
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000508 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100509 DCHECK_NE(ref_reg, RSP);
510 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100511 // "Compact" slow path, saving two moves.
512 //
513 // Instead of using the standard runtime calling convention (input
514 // and output in R0):
515 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100516 // RDI <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100517 // RAX <- ReadBarrierMark(RDI)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100518 // ref <- RAX
Roland Levillain02b75802016-07-13 11:54:35 +0100519 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100520 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100521 // of a dedicated entrypoint:
522 //
523 // rX <- ReadBarrierMarkRegX(rX)
524 //
525 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100526 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100527 // This runtime call does not require a stack map.
528 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000529 __ jmp(GetExitLabel());
530 }
531
532 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100533 // The location (register) of the marked object reference.
534 const Location ref_;
535 // Should the reference in `ref_` be unpoisoned prior to marking it?
536 const bool unpoison_ref_before_marking_;
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000537
538 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86_64);
539};
540
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100541// Slow path marking an object reference `ref` during a read barrier,
542// and if needed, atomically updating the field `obj.field` in the
543// object `obj` holding this reference after marking (contrary to
544// ReadBarrierMarkSlowPathX86_64 above, which never tries to update
545// `obj.field`).
546//
547// This means that after the execution of this slow path, both `ref`
548// and `obj.field` will be up-to-date; i.e., after the flip, both will
549// hold the same to-space reference (unless another thread installed
550// another object reference (different from `ref`) in `obj.field`).
551class ReadBarrierMarkAndUpdateFieldSlowPathX86_64 : public SlowPathCode {
552 public:
553 ReadBarrierMarkAndUpdateFieldSlowPathX86_64(HInstruction* instruction,
554 Location ref,
555 CpuRegister obj,
556 const Address& field_addr,
557 bool unpoison_ref_before_marking,
558 CpuRegister temp1,
559 CpuRegister temp2)
560 : SlowPathCode(instruction),
561 ref_(ref),
562 obj_(obj),
563 field_addr_(field_addr),
564 unpoison_ref_before_marking_(unpoison_ref_before_marking),
565 temp1_(temp1),
566 temp2_(temp2) {
567 DCHECK(kEmitCompilerReadBarrier);
568 }
569
570 const char* GetDescription() const OVERRIDE {
571 return "ReadBarrierMarkAndUpdateFieldSlowPathX86_64";
572 }
573
574 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
575 LocationSummary* locations = instruction_->GetLocations();
576 CpuRegister ref_cpu_reg = ref_.AsRegister<CpuRegister>();
577 Register ref_reg = ref_cpu_reg.AsRegister();
578 DCHECK(locations->CanCall());
579 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
580 // This slow path is only used by the UnsafeCASObject intrinsic.
581 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
582 << "Unexpected instruction in read barrier marking and field updating slow path: "
583 << instruction_->DebugName();
584 DCHECK(instruction_->GetLocations()->Intrinsified());
585 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
586
587 __ Bind(GetEntryLabel());
588 if (unpoison_ref_before_marking_) {
589 // Object* ref = ref_addr->AsMirrorPtr()
590 __ MaybeUnpoisonHeapReference(ref_cpu_reg);
591 }
592
593 // Save the old (unpoisoned) reference.
594 __ movl(temp1_, ref_cpu_reg);
595
596 // No need to save live registers; it's taken care of by the
597 // entrypoint. Also, there is no need to update the stack mask,
598 // as this runtime call will not trigger a garbage collection.
599 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
600 DCHECK_NE(ref_reg, RSP);
601 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
602 // "Compact" slow path, saving two moves.
603 //
604 // Instead of using the standard runtime calling convention (input
605 // and output in R0):
606 //
607 // RDI <- ref
608 // RAX <- ReadBarrierMark(RDI)
609 // ref <- RAX
610 //
611 // we just use rX (the register containing `ref`) as input and output
612 // of a dedicated entrypoint:
613 //
614 // rX <- ReadBarrierMarkRegX(rX)
615 //
616 int32_t entry_point_offset =
617 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(ref_reg);
618 // This runtime call does not require a stack map.
619 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
620
621 // If the new reference is different from the old reference,
622 // update the field in the holder (`*field_addr`).
623 //
624 // Note that this field could also hold a different object, if
625 // another thread had concurrently changed it. In that case, the
626 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
627 // operation below would abort the CAS, leaving the field as-is.
628 NearLabel done;
629 __ cmpl(temp1_, ref_cpu_reg);
630 __ j(kEqual, &done);
631
632 // Update the the holder's field atomically. This may fail if
633 // mutator updates before us, but it's OK. This is achived
634 // using a strong compare-and-set (CAS) operation with relaxed
635 // memory synchronization ordering, where the expected value is
636 // the old reference and the desired value is the new reference.
637 // This operation is implemented with a 32-bit LOCK CMPXLCHG
638 // instruction, which requires the expected value (the old
639 // reference) to be in EAX. Save RAX beforehand, and move the
640 // expected value (stored in `temp1_`) into EAX.
641 __ movq(temp2_, CpuRegister(RAX));
642 __ movl(CpuRegister(RAX), temp1_);
643
644 // Convenience aliases.
645 CpuRegister base = obj_;
646 CpuRegister expected = CpuRegister(RAX);
647 CpuRegister value = ref_cpu_reg;
648
649 bool base_equals_value = (base.AsRegister() == value.AsRegister());
650 Register value_reg = ref_reg;
651 if (kPoisonHeapReferences) {
652 if (base_equals_value) {
653 // If `base` and `value` are the same register location, move
654 // `value_reg` to a temporary register. This way, poisoning
655 // `value_reg` won't invalidate `base`.
656 value_reg = temp1_.AsRegister();
657 __ movl(CpuRegister(value_reg), base);
658 }
659
660 // Check that the register allocator did not assign the location
661 // of `expected` (RAX) to `value` nor to `base`, so that heap
662 // poisoning (when enabled) works as intended below.
663 // - If `value` were equal to `expected`, both references would
664 // be poisoned twice, meaning they would not be poisoned at
665 // all, as heap poisoning uses address negation.
666 // - If `base` were equal to `expected`, poisoning `expected`
667 // would invalidate `base`.
668 DCHECK_NE(value_reg, expected.AsRegister());
669 DCHECK_NE(base.AsRegister(), expected.AsRegister());
670
671 __ PoisonHeapReference(expected);
672 __ PoisonHeapReference(CpuRegister(value_reg));
673 }
674
675 __ LockCmpxchgl(field_addr_, CpuRegister(value_reg));
676
677 // If heap poisoning is enabled, we need to unpoison the values
678 // that were poisoned earlier.
679 if (kPoisonHeapReferences) {
680 if (base_equals_value) {
681 // `value_reg` has been moved to a temporary register, no need
682 // to unpoison it.
683 } else {
684 __ UnpoisonHeapReference(CpuRegister(value_reg));
685 }
686 // No need to unpoison `expected` (RAX), as it is be overwritten below.
687 }
688
689 // Restore RAX.
690 __ movq(CpuRegister(RAX), temp2_);
691
692 __ Bind(&done);
693 __ jmp(GetExitLabel());
694 }
695
696 private:
697 // The location (register) of the marked object reference.
698 const Location ref_;
699 // The register containing the object holding the marked object reference field.
700 const CpuRegister obj_;
701 // The address of the marked reference field. The base of this address must be `obj_`.
702 const Address field_addr_;
703
704 // Should the reference in `ref_` be unpoisoned prior to marking it?
705 const bool unpoison_ref_before_marking_;
706
707 const CpuRegister temp1_;
708 const CpuRegister temp2_;
709
710 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86_64);
711};
712
Roland Levillain0d5a2812015-11-13 10:07:31 +0000713// Slow path generating a read barrier for a heap reference.
714class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
715 public:
716 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
717 Location out,
718 Location ref,
719 Location obj,
720 uint32_t offset,
721 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000722 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000723 out_(out),
724 ref_(ref),
725 obj_(obj),
726 offset_(offset),
727 index_(index) {
728 DCHECK(kEmitCompilerReadBarrier);
729 // If `obj` is equal to `out` or `ref`, it means the initial
730 // object has been overwritten by (or after) the heap object
731 // reference load to be instrumented, e.g.:
732 //
733 // __ movl(out, Address(out, offset));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000734 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000735 //
736 // In that case, we have lost the information about the original
737 // object, and the emitted read barrier cannot work properly.
738 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
739 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
740}
741
742 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
743 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
744 LocationSummary* locations = instruction_->GetLocations();
745 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
746 DCHECK(locations->CanCall());
747 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
Roland Levillain3d312422016-06-23 13:53:42 +0100748 DCHECK(instruction_->IsInstanceFieldGet() ||
749 instruction_->IsStaticFieldGet() ||
750 instruction_->IsArrayGet() ||
751 instruction_->IsInstanceOf() ||
752 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700753 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000754 << "Unexpected instruction in read barrier for heap reference slow path: "
755 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000756
757 __ Bind(GetEntryLabel());
758 SaveLiveRegisters(codegen, locations);
759
760 // We may have to change the index's value, but as `index_` is a
761 // constant member (like other "inputs" of this slow path),
762 // introduce a copy of it, `index`.
763 Location index = index_;
764 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100765 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000766 if (instruction_->IsArrayGet()) {
767 // Compute real offset and store it in index_.
768 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
769 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
770 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
771 // We are about to change the value of `index_reg` (see the
772 // calls to art::x86_64::X86_64Assembler::shll and
773 // art::x86_64::X86_64Assembler::AddImmediate below), but it
774 // has not been saved by the previous call to
775 // art::SlowPathCode::SaveLiveRegisters, as it is a
776 // callee-save register --
777 // art::SlowPathCode::SaveLiveRegisters does not consider
778 // callee-save registers, as it has been designed with the
779 // assumption that callee-save registers are supposed to be
780 // handled by the called function. So, as a callee-save
781 // register, `index_reg` _would_ eventually be saved onto
782 // the stack, but it would be too late: we would have
783 // changed its value earlier. Therefore, we manually save
784 // it here into another freely available register,
785 // `free_reg`, chosen of course among the caller-save
786 // registers (as a callee-save `free_reg` register would
787 // exhibit the same problem).
788 //
789 // Note we could have requested a temporary register from
790 // the register allocator instead; but we prefer not to, as
791 // this is a slow path, and we know we can find a
792 // caller-save register that is available.
793 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
794 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
795 index_reg = free_reg;
796 index = Location::RegisterLocation(index_reg);
797 } else {
798 // The initial register stored in `index_` has already been
799 // saved in the call to art::SlowPathCode::SaveLiveRegisters
800 // (as it is not a callee-save register), so we can freely
801 // use it.
802 }
803 // Shifting the index value contained in `index_reg` by the
804 // scale factor (2) cannot overflow in practice, as the
805 // runtime is unable to allocate object arrays with a size
806 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
807 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
808 static_assert(
809 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
810 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
811 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
812 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100813 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
814 // intrinsics, `index_` is not shifted by a scale factor of 2
815 // (as in the case of ArrayGet), as it is actually an offset
816 // to an object field within an object.
817 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000818 DCHECK(instruction_->GetLocations()->Intrinsified());
819 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
820 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
821 << instruction_->AsInvoke()->GetIntrinsic();
822 DCHECK_EQ(offset_, 0U);
823 DCHECK(index_.IsRegister());
824 }
825 }
826
827 // We're moving two or three locations to locations that could
828 // overlap, so we need a parallel move resolver.
829 InvokeRuntimeCallingConvention calling_convention;
830 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
831 parallel_move.AddMove(ref_,
832 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
833 Primitive::kPrimNot,
834 nullptr);
835 parallel_move.AddMove(obj_,
836 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
837 Primitive::kPrimNot,
838 nullptr);
839 if (index.IsValid()) {
840 parallel_move.AddMove(index,
841 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
842 Primitive::kPrimInt,
843 nullptr);
844 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
845 } else {
846 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
847 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
848 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100849 x86_64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000850 instruction_,
851 instruction_->GetDexPc(),
852 this);
853 CheckEntrypointTypes<
854 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
855 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
856
857 RestoreLiveRegisters(codegen, locations);
858 __ jmp(GetExitLabel());
859 }
860
861 const char* GetDescription() const OVERRIDE {
862 return "ReadBarrierForHeapReferenceSlowPathX86_64";
863 }
864
865 private:
866 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
867 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
868 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
869 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
870 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
871 return static_cast<CpuRegister>(i);
872 }
873 }
874 // We shall never fail to find a free caller-save register, as
875 // there are more than two core caller-save registers on x86-64
876 // (meaning it is possible to find one which is different from
877 // `ref` and `obj`).
878 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
879 LOG(FATAL) << "Could not find a free caller-save register";
880 UNREACHABLE();
881 }
882
Roland Levillain0d5a2812015-11-13 10:07:31 +0000883 const Location out_;
884 const Location ref_;
885 const Location obj_;
886 const uint32_t offset_;
887 // An additional location containing an index to an array.
888 // Only used for HArrayGet and the UnsafeGetObject &
889 // UnsafeGetObjectVolatile intrinsics.
890 const Location index_;
891
892 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
893};
894
895// Slow path generating a read barrier for a GC root.
896class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
897 public:
898 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000899 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000900 DCHECK(kEmitCompilerReadBarrier);
901 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000902
903 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
904 LocationSummary* locations = instruction_->GetLocations();
905 DCHECK(locations->CanCall());
906 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain1e7f8db2015-12-15 10:54:19 +0000907 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
908 << "Unexpected instruction in read barrier for GC root slow path: "
909 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000910
911 __ Bind(GetEntryLabel());
912 SaveLiveRegisters(codegen, locations);
913
914 InvokeRuntimeCallingConvention calling_convention;
915 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
916 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100917 x86_64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000918 instruction_,
919 instruction_->GetDexPc(),
920 this);
921 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
922 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
923
924 RestoreLiveRegisters(codegen, locations);
925 __ jmp(GetExitLabel());
926 }
927
928 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
929
930 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000931 const Location out_;
932 const Location root_;
933
934 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
935};
936
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100937#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100938// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
939#define __ down_cast<X86_64Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100940
Roland Levillain4fa13f62015-07-06 18:11:54 +0100941inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700942 switch (cond) {
943 case kCondEQ: return kEqual;
944 case kCondNE: return kNotEqual;
945 case kCondLT: return kLess;
946 case kCondLE: return kLessEqual;
947 case kCondGT: return kGreater;
948 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700949 case kCondB: return kBelow;
950 case kCondBE: return kBelowEqual;
951 case kCondA: return kAbove;
952 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700953 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100954 LOG(FATAL) << "Unreachable";
955 UNREACHABLE();
956}
957
Aart Bike9f37602015-10-09 11:15:55 -0700958// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100959inline Condition X86_64FPCondition(IfCondition cond) {
960 switch (cond) {
961 case kCondEQ: return kEqual;
962 case kCondNE: return kNotEqual;
963 case kCondLT: return kBelow;
964 case kCondLE: return kBelowEqual;
965 case kCondGT: return kAbove;
966 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700967 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100968 };
969 LOG(FATAL) << "Unreachable";
970 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700971}
972
Vladimir Markodc151b22015-10-15 18:02:30 +0100973HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
974 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100975 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000976 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +0100977}
978
Serguei Katkov288c7a82016-05-16 11:53:15 +0600979Location CodeGeneratorX86_64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
980 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800981 // All registers are assumed to be correctly set up.
Vladimir Marko58155012015-08-19 12:49:41 +0000982 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
983 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100984 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +0000985 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100986 uint32_t offset =
987 GetThreadOffset<kX86_64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
988 __ gs()->movq(temp.AsRegister<CpuRegister>(), Address::Absolute(offset, /* no_rip */ true));
Vladimir Marko58155012015-08-19 12:49:41 +0000989 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100990 }
Vladimir Marko58155012015-08-19 12:49:41 +0000991 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000992 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +0000993 break;
Vladimir Marko65979462017-05-19 17:25:12 +0100994 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
995 DCHECK(GetCompilerOptions().IsBootImage());
996 __ leal(temp.AsRegister<CpuRegister>(),
997 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
998 RecordBootMethodPatch(invoke);
999 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001000 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Vladimir Marko2d73f332017-03-16 15:55:49 +00001001 Load64BitValue(temp.AsRegister<CpuRegister>(), invoke->GetMethodAddress());
Vladimir Marko58155012015-08-19 12:49:41 +00001002 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001003 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
Vladimir Marko58155012015-08-19 12:49:41 +00001004 __ movq(temp.AsRegister<CpuRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001005 Address::Absolute(kDummy32BitOffset, /* no_rip */ false));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001006 // Bind a new fixup label at the end of the "movl" insn.
1007 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001008 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset));
Vladimir Marko58155012015-08-19 12:49:41 +00001009 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001010 }
Vladimir Marko58155012015-08-19 12:49:41 +00001011 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001012 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00001013 Register method_reg;
1014 CpuRegister reg = temp.AsRegister<CpuRegister>();
1015 if (current_method.IsRegister()) {
1016 method_reg = current_method.AsRegister<Register>();
1017 } else {
1018 DCHECK(invoke->GetLocations()->Intrinsified());
1019 DCHECK(!current_method.IsValid());
1020 method_reg = reg.AsRegister();
1021 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
1022 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001023 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01001024 __ movq(reg,
1025 Address(CpuRegister(method_reg),
1026 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01001027 // temp = temp[index_in_cache];
1028 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
1029 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00001030 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
1031 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01001032 }
Vladimir Marko58155012015-08-19 12:49:41 +00001033 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06001034 return callee_method;
1035}
1036
1037void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
1038 Location temp) {
1039 // All registers are assumed to be correctly set up.
1040 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00001041
1042 switch (invoke->GetCodePtrLocation()) {
1043 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
1044 __ call(&frame_entry_label_);
1045 break;
Vladimir Marko58155012015-08-19 12:49:41 +00001046 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
1047 // (callee_method + offset_of_quick_compiled_code)()
1048 __ call(Address(callee_method.AsRegister<CpuRegister>(),
1049 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001050 kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +00001051 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001052 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001053
1054 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001055}
1056
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001057void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
1058 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
1059 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1060 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001061
1062 // Use the calling convention instead of the location of the receiver, as
1063 // intrinsics may have put the receiver in a different register. In the intrinsics
1064 // slow path, the arguments have been moved to the right place, so here we are
1065 // guaranteed that the receiver is the first register of the calling convention.
1066 InvokeDexCallingConvention calling_convention;
1067 Register receiver = calling_convention.GetRegisterAt(0);
1068
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001069 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001070 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001071 __ movl(temp, Address(CpuRegister(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001072 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00001073 // Instead of simply (possibly) unpoisoning `temp` here, we should
1074 // emit a read barrier for the previous class reference load.
1075 // However this is not required in practice, as this is an
1076 // intermediate/temporary reference and because the current
1077 // concurrent copying collector keeps the from-space memory
1078 // intact/accessible until the end of the marking phase (the
1079 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001080 __ MaybeUnpoisonHeapReference(temp);
1081 // temp = temp->GetMethodAt(method_offset);
1082 __ movq(temp, Address(temp, method_offset));
1083 // call temp->GetEntryPoint();
1084 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07001085 kX86_64PointerSize).SizeValue()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001086}
1087
Vladimir Marko65979462017-05-19 17:25:12 +01001088void CodeGeneratorX86_64::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
1089 boot_image_method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
1090 invoke->GetTargetMethod().dex_method_index);
1091 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001092}
1093
Vladimir Marko1998cd02017-01-13 13:02:58 +00001094void CodeGeneratorX86_64::RecordBootTypePatch(HLoadClass* load_class) {
1095 boot_image_type_patches_.emplace_back(load_class->GetDexFile(),
1096 load_class->GetTypeIndex().index_);
1097 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001098}
1099
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001100Label* CodeGeneratorX86_64::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001101 type_bss_entry_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex().index_);
1102 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001103}
1104
Vladimir Marko65979462017-05-19 17:25:12 +01001105void CodeGeneratorX86_64::RecordBootStringPatch(HLoadString* load_string) {
1106 DCHECK(GetCompilerOptions().IsBootImage());
1107 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
1108 __ Bind(&string_patches_.back().label);
1109}
1110
Vladimir Markoaad75c62016-10-03 08:46:48 +00001111Label* CodeGeneratorX86_64::NewStringBssEntryPatch(HLoadString* load_string) {
1112 DCHECK(!GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001113 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001114 return &string_patches_.back().label;
1115}
1116
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001117Label* CodeGeneratorX86_64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
1118 uint32_t element_offset) {
1119 // Add a patch entry and return the label.
1120 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
1121 return &pc_relative_dex_cache_patches_.back().label;
1122}
1123
Vladimir Markoaad75c62016-10-03 08:46:48 +00001124// The label points to the end of the "movl" or another instruction but the literal offset
1125// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
1126constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
1127
1128template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1129inline void CodeGeneratorX86_64::EmitPcRelativeLinkerPatches(
1130 const ArenaDeque<PatchInfo<Label>>& infos,
1131 ArenaVector<LinkerPatch>* linker_patches) {
1132 for (const PatchInfo<Label>& info : infos) {
1133 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
1134 linker_patches->push_back(
1135 Factory(literal_offset, &info.dex_file, info.label.Position(), info.index));
1136 }
1137}
1138
Vladimir Marko58155012015-08-19 12:49:41 +00001139void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1140 DCHECK(linker_patches->empty());
1141 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001142 pc_relative_dex_cache_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001143 boot_image_method_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001144 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001145 type_bss_entry_patches_.size() +
1146 string_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00001147 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001148 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1149 linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001150 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko65979462017-05-19 17:25:12 +01001151 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(boot_image_method_patches_,
1152 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001153 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
1154 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001155 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01001156 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01001157 DCHECK(boot_image_method_patches_.empty());
Vladimir Marko764d4542017-05-16 10:31:41 +01001158 DCHECK(boot_image_type_patches_.empty());
1159 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001160 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001161 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1162 linker_patches);
1163 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00001164}
1165
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001166void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001167 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001168}
1169
1170void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001171 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001172}
1173
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001174size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1175 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
1176 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001177}
1178
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001179size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1180 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1181 return kX86_64WordSize;
1182}
1183
1184size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001185 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001186 __ movups(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001187 } else {
1188 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
1189 }
1190 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001191}
1192
1193size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001194 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001195 __ movups(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001196 } else {
1197 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
1198 }
1199 return GetFloatingPointSpillSlotSize();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001200}
1201
Calin Juravle175dc732015-08-25 15:42:32 +01001202void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1203 HInstruction* instruction,
1204 uint32_t dex_pc,
1205 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001206 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001207 GenerateInvokeRuntime(GetThreadOffset<kX86_64PointerSize>(entrypoint).Int32Value());
1208 if (EntrypointRequiresStackMap(entrypoint)) {
1209 RecordPcInfo(instruction, dex_pc, slow_path);
1210 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001211}
1212
Roland Levillaindec8f632016-07-22 17:10:06 +01001213void CodeGeneratorX86_64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1214 HInstruction* instruction,
1215 SlowPathCode* slow_path) {
1216 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001217 GenerateInvokeRuntime(entry_point_offset);
1218}
1219
1220void CodeGeneratorX86_64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001221 __ gs()->call(Address::Absolute(entry_point_offset, /* no_rip */ true));
1222}
1223
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001224static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001225// Use a fake return address register to mimic Quick.
1226static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -04001227CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001228 const X86_64InstructionSetFeatures& isa_features,
1229 const CompilerOptions& compiler_options,
1230 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +00001231 : CodeGenerator(graph,
1232 kNumberOfCpuRegisters,
1233 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001234 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001235 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1236 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001237 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001238 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1239 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001240 compiler_options,
1241 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001242 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001243 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001244 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001245 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001246 assembler_(graph->GetArena()),
Mark Mendellf55c3e02015-03-26 21:07:46 -04001247 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +00001248 constant_area_start_(0),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001249 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001250 boot_image_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001251 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1252 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001253 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001254 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001255 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1256 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001257 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
1258}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001260InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
1261 CodeGeneratorX86_64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001262 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001263 assembler_(codegen->GetAssembler()),
1264 codegen_(codegen) {}
1265
David Brazdil58282f42016-01-14 12:45:10 +00001266void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001267 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001268 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001269
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001270 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001271 blocked_core_registers_[TMP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001272}
1273
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001274static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001275 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001276}
David Srbecky9d8606d2015-04-12 09:35:32 +01001277
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001278static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001279 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001280}
1281
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001282void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001283 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001284 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001285 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -07001286 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001287 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001288
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001289 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001290 __ testq(CpuRegister(RAX), Address(
1291 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001292 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001293 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +00001294
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001295 if (HasEmptyFrame()) {
1296 return;
1297 }
1298
Nicolas Geoffray98893962015-01-21 12:32:32 +00001299 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001300 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001301 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001302 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001303 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1304 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001305 }
1306 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001307
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001308 int adjust = GetFrameSize() - GetCoreSpillSize();
1309 __ subq(CpuRegister(RSP), Immediate(adjust));
1310 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001311 uint32_t xmm_spill_location = GetFpuSpillStart();
1312 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001313
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001314 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1315 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001316 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1317 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1318 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001319 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001320 }
1321
Mingyao Yang063fc772016-08-02 11:02:54 -07001322 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1323 // Initialize should_deoptimize flag to 0.
1324 __ movl(Address(CpuRegister(RSP), xmm_spill_location - kShouldDeoptimizeFlagSize),
1325 Immediate(0));
1326 }
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);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001735 InvokeRuntimeCallingConvention calling_convention;
1736 RegisterSet caller_saves = RegisterSet::Empty();
1737 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1738 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001739 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001740 locations->SetInAt(0, Location::Any());
1741 }
1742}
1743
1744void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001745 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86_64>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001746 GenerateTestAndBranch<Label>(deoptimize,
1747 /* condition_input_index */ 0,
1748 slow_path->GetEntryLabel(),
1749 /* false_target */ nullptr);
1750}
1751
Mingyao Yang063fc772016-08-02 11:02:54 -07001752void LocationsBuilderX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1753 LocationSummary* locations = new (GetGraph()->GetArena())
1754 LocationSummary(flag, LocationSummary::kNoCall);
1755 locations->SetOut(Location::RequiresRegister());
1756}
1757
1758void InstructionCodeGeneratorX86_64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1759 __ movl(flag->GetLocations()->Out().AsRegister<CpuRegister>(),
1760 Address(CpuRegister(RSP), codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1761}
1762
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001763static bool SelectCanUseCMOV(HSelect* select) {
1764 // There are no conditional move instructions for XMMs.
1765 if (Primitive::IsFloatingPointType(select->GetType())) {
1766 return false;
1767 }
1768
1769 // A FP condition doesn't generate the single CC that we need.
1770 HInstruction* condition = select->GetCondition();
1771 if (condition->IsCondition() &&
1772 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType())) {
1773 return false;
1774 }
1775
1776 // We can generate a CMOV for this Select.
1777 return true;
1778}
1779
David Brazdil74eb1b22015-12-14 11:44:01 +00001780void LocationsBuilderX86_64::VisitSelect(HSelect* select) {
1781 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1782 if (Primitive::IsFloatingPointType(select->GetType())) {
1783 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001784 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001785 } else {
1786 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001787 if (SelectCanUseCMOV(select)) {
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001788 if (select->InputAt(1)->IsConstant()) {
1789 locations->SetInAt(1, Location::RequiresRegister());
1790 } else {
1791 locations->SetInAt(1, Location::Any());
1792 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001793 } else {
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001794 locations->SetInAt(1, Location::Any());
1795 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001796 }
1797 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1798 locations->SetInAt(2, Location::RequiresRegister());
1799 }
1800 locations->SetOut(Location::SameAsFirstInput());
1801}
1802
1803void InstructionCodeGeneratorX86_64::VisitSelect(HSelect* select) {
1804 LocationSummary* locations = select->GetLocations();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001805 if (SelectCanUseCMOV(select)) {
1806 // If both the condition and the source types are integer, we can generate
1807 // a CMOV to implement Select.
1808 CpuRegister value_false = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001809 Location value_true_loc = locations->InAt(1);
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001810 DCHECK(locations->InAt(0).Equals(locations->Out()));
1811
1812 HInstruction* select_condition = select->GetCondition();
1813 Condition cond = kNotEqual;
1814
1815 // Figure out how to test the 'condition'.
1816 if (select_condition->IsCondition()) {
1817 HCondition* condition = select_condition->AsCondition();
1818 if (!condition->IsEmittedAtUseSite()) {
1819 // This was a previously materialized condition.
1820 // Can we use the existing condition code?
1821 if (AreEflagsSetFrom(condition, select)) {
1822 // Materialization was the previous instruction. Condition codes are right.
1823 cond = X86_64IntegerCondition(condition->GetCondition());
1824 } else {
1825 // No, we have to recreate the condition code.
1826 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1827 __ testl(cond_reg, cond_reg);
1828 }
1829 } else {
1830 GenerateCompareTest(condition);
1831 cond = X86_64IntegerCondition(condition->GetCondition());
1832 }
1833 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001834 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001835 CpuRegister cond_reg = locations->InAt(2).AsRegister<CpuRegister>();
1836 __ testl(cond_reg, cond_reg);
1837 }
1838
1839 // If the condition is true, overwrite the output, which already contains false.
1840 // Generate the correct sized CMOV.
Mark Mendelldee1b9a2016-02-12 14:36:51 -05001841 bool is_64_bit = Primitive::Is64BitType(select->GetType());
1842 if (value_true_loc.IsRegister()) {
1843 __ cmov(cond, value_false, value_true_loc.AsRegister<CpuRegister>(), is_64_bit);
1844 } else {
1845 __ cmov(cond,
1846 value_false,
1847 Address(CpuRegister(RSP), value_true_loc.GetStackIndex()), is_64_bit);
1848 }
Mark Mendell7c0b44f2016-02-01 10:08:35 -05001849 } else {
1850 NearLabel false_target;
1851 GenerateTestAndBranch<NearLabel>(select,
1852 /* condition_input_index */ 2,
1853 /* true_target */ nullptr,
1854 &false_target);
1855 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1856 __ Bind(&false_target);
1857 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001858}
1859
David Srbecky0cf44932015-12-09 14:09:59 +00001860void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1861 new (GetGraph()->GetArena()) LocationSummary(info);
1862}
1863
David Srbeckyd28f4a02016-03-14 17:14:24 +00001864void InstructionCodeGeneratorX86_64::VisitNativeDebugInfo(HNativeDebugInfo*) {
1865 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001866}
1867
1868void CodeGeneratorX86_64::GenerateNop() {
1869 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001870}
1871
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001872void LocationsBuilderX86_64::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001873 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001874 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001875 // Handle the long/FP comparisons made in instruction simplification.
1876 switch (cond->InputAt(0)->GetType()) {
1877 case Primitive::kPrimLong:
1878 locations->SetInAt(0, Location::RequiresRegister());
1879 locations->SetInAt(1, Location::Any());
1880 break;
1881 case Primitive::kPrimFloat:
1882 case Primitive::kPrimDouble:
1883 locations->SetInAt(0, Location::RequiresFpuRegister());
1884 locations->SetInAt(1, Location::Any());
1885 break;
1886 default:
1887 locations->SetInAt(0, Location::RequiresRegister());
1888 locations->SetInAt(1, Location::Any());
1889 break;
1890 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001891 if (!cond->IsEmittedAtUseSite()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001892 locations->SetOut(Location::RequiresRegister());
1893 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001894}
1895
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001896void InstructionCodeGeneratorX86_64::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001897 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001898 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001899 }
Mark Mendellc4701932015-04-10 13:18:51 -04001900
1901 LocationSummary* locations = cond->GetLocations();
1902 Location lhs = locations->InAt(0);
1903 Location rhs = locations->InAt(1);
1904 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Mark Mendell152408f2015-12-31 12:28:50 -05001905 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001906
1907 switch (cond->InputAt(0)->GetType()) {
1908 default:
1909 // Integer case.
1910
1911 // Clear output register: setcc only sets the low byte.
1912 __ xorl(reg, reg);
1913
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001914 codegen_->GenerateIntCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001915 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001916 return;
1917 case Primitive::kPrimLong:
1918 // Clear output register: setcc only sets the low byte.
1919 __ xorl(reg, reg);
1920
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001921 codegen_->GenerateLongCompare(lhs, rhs);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001922 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001923 return;
1924 case Primitive::kPrimFloat: {
1925 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1926 if (rhs.IsConstant()) {
1927 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1928 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1929 } else if (rhs.IsStackSlot()) {
1930 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1931 } else {
1932 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1933 }
1934 GenerateFPJumps(cond, &true_label, &false_label);
1935 break;
1936 }
1937 case Primitive::kPrimDouble: {
1938 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1939 if (rhs.IsConstant()) {
1940 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1941 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1942 } else if (rhs.IsDoubleStackSlot()) {
1943 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1944 } else {
1945 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1946 }
1947 GenerateFPJumps(cond, &true_label, &false_label);
1948 break;
1949 }
1950 }
1951
1952 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001953 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001954
Roland Levillain4fa13f62015-07-06 18:11:54 +01001955 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001956 __ Bind(&false_label);
1957 __ xorl(reg, reg);
1958 __ jmp(&done_label);
1959
Roland Levillain4fa13f62015-07-06 18:11:54 +01001960 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001961 __ Bind(&true_label);
1962 __ movl(reg, Immediate(1));
1963 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001964}
1965
1966void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001976}
1977
1978void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001980}
1981
1982void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001984}
1985
1986void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001988}
1989
1990void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001992}
1993
1994void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001996}
1997
1998void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002000}
2001
2002void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002004}
2005
2006void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002008}
2009
2010void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 HandleCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002012}
2013
Aart Bike9f37602015-10-09 11:15:55 -07002014void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002016}
2017
2018void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002020}
2021
2022void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002024}
2025
2026void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002028}
2029
2030void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002031 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002032}
2033
2034void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002035 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002036}
2037
2038void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002039 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002040}
2041
2042void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002043 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002044}
2045
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002046void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002047 LocationSummary* locations =
2048 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002049 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002050 case Primitive::kPrimBoolean:
2051 case Primitive::kPrimByte:
2052 case Primitive::kPrimShort:
2053 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002054 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00002055 case Primitive::kPrimLong: {
2056 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002057 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2059 break;
2060 }
2061 case Primitive::kPrimFloat:
2062 case Primitive::kPrimDouble: {
2063 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04002064 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00002065 locations->SetOut(Location::RequiresRegister());
2066 break;
2067 }
2068 default:
2069 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2070 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002071}
2072
2073void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002074 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002075 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002076 Location left = locations->InAt(0);
2077 Location right = locations->InAt(1);
2078
Mark Mendell0c9497d2015-08-21 09:30:05 -04002079 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00002080 Primitive::Type type = compare->InputAt(0)->GetType();
Aart Bika19616e2016-02-01 18:57:58 -08002081 Condition less_cond = kLess;
2082
Calin Juravleddb7df22014-11-25 20:56:51 +00002083 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002084 case Primitive::kPrimBoolean:
2085 case Primitive::kPrimByte:
2086 case Primitive::kPrimShort:
2087 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002088 case Primitive::kPrimInt: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002089 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002090 break;
2091 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002092 case Primitive::kPrimLong: {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002093 codegen_->GenerateLongCompare(left, right);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002094 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00002095 }
2096 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04002097 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2098 if (right.IsConstant()) {
2099 float value = right.GetConstant()->AsFloatConstant()->GetValue();
2100 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
2101 } else if (right.IsStackSlot()) {
2102 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2103 } else {
2104 __ ucomiss(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 }
2110 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04002111 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
2112 if (right.IsConstant()) {
2113 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
2114 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
2115 } else if (right.IsDoubleStackSlot()) {
2116 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
2117 } else {
2118 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
2119 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002120 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08002121 less_cond = kBelow; // ucomis{s,d} sets CF
Calin Juravleddb7df22014-11-25 20:56:51 +00002122 break;
2123 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002124 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002125 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002126 }
Aart Bika19616e2016-02-01 18:57:58 -08002127
Calin Juravleddb7df22014-11-25 20:56:51 +00002128 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00002129 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08002130 __ j(less_cond, &less);
Calin Juravlefd861242014-11-25 20:56:51 +00002131
Calin Juravle91debbc2014-11-26 19:01:09 +00002132 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00002133 __ movl(out, Immediate(1));
2134 __ jmp(&done);
2135
2136 __ Bind(&less);
2137 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002138
2139 __ Bind(&done);
2140}
2141
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002142void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002143 LocationSummary* locations =
2144 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002145 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002146}
2147
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002148void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002149 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002150}
2151
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002152void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
2153 LocationSummary* locations =
2154 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2155 locations->SetOut(Location::ConstantLocation(constant));
2156}
2157
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002158void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002159 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002160}
2161
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002162void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002163 LocationSummary* locations =
2164 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002165 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002166}
2167
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002168void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002169 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002170}
2171
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002172void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
2173 LocationSummary* locations =
2174 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2175 locations->SetOut(Location::ConstantLocation(constant));
2176}
2177
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002178void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002179 // Will be generated at use site.
2180}
2181
2182void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
2183 LocationSummary* locations =
2184 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2185 locations->SetOut(Location::ConstantLocation(constant));
2186}
2187
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002188void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
2189 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002190 // Will be generated at use site.
2191}
2192
Igor Murashkind01745e2017-04-05 16:40:31 -07002193void LocationsBuilderX86_64::VisitConstructorFence(HConstructorFence* constructor_fence) {
2194 constructor_fence->SetLocations(nullptr);
2195}
2196
2197void InstructionCodeGeneratorX86_64::VisitConstructorFence(
2198 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2199 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2200}
2201
Calin Juravle27df7582015-04-17 19:12:31 +01002202void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2203 memory_barrier->SetLocations(nullptr);
2204}
2205
2206void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002207 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002208}
2209
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002210void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
2211 ret->SetLocations(nullptr);
2212}
2213
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002214void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002215 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002216}
2217
2218void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002219 LocationSummary* locations =
2220 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002221 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:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002229 locations->SetInAt(0, Location::RegisterLocation(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:
Mark Mendell40741f32015-04-20 22:10:34 -04002234 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002235 break;
2236
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002237 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002238 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002239 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002240}
2241
2242void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
2243 if (kIsDebugBuild) {
2244 switch (ret->InputAt(0)->GetType()) {
2245 case Primitive::kPrimBoolean:
2246 case Primitive::kPrimByte:
2247 case Primitive::kPrimChar:
2248 case Primitive::kPrimShort:
2249 case Primitive::kPrimInt:
2250 case Primitive::kPrimNot:
2251 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002252 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002253 break;
2254
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002255 case Primitive::kPrimFloat:
2256 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002257 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002258 XMM0);
2259 break;
2260
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002261 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002262 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002263 }
2264 }
2265 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002266}
2267
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002268Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
2269 switch (type) {
2270 case Primitive::kPrimBoolean:
2271 case Primitive::kPrimByte:
2272 case Primitive::kPrimChar:
2273 case Primitive::kPrimShort:
2274 case Primitive::kPrimInt:
2275 case Primitive::kPrimNot:
2276 case Primitive::kPrimLong:
2277 return Location::RegisterLocation(RAX);
2278
2279 case Primitive::kPrimVoid:
2280 return Location::NoLocation();
2281
2282 case Primitive::kPrimDouble:
2283 case Primitive::kPrimFloat:
2284 return Location::FpuRegisterLocation(XMM0);
2285 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002286
2287 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002288}
2289
2290Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2291 return Location::RegisterLocation(kMethodRegisterArgument);
2292}
2293
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002294Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002295 switch (type) {
2296 case Primitive::kPrimBoolean:
2297 case Primitive::kPrimByte:
2298 case Primitive::kPrimChar:
2299 case Primitive::kPrimShort:
2300 case Primitive::kPrimInt:
2301 case Primitive::kPrimNot: {
2302 uint32_t index = gp_index_++;
2303 stack_index_++;
2304 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002305 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002306 } else {
2307 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2308 }
2309 }
2310
2311 case Primitive::kPrimLong: {
2312 uint32_t index = gp_index_;
2313 stack_index_ += 2;
2314 if (index < calling_convention.GetNumberOfRegisters()) {
2315 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002316 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002317 } else {
2318 gp_index_ += 2;
2319 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2320 }
2321 }
2322
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002323 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002324 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002325 stack_index_++;
2326 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002327 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002328 } else {
2329 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2330 }
2331 }
2332
2333 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002334 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002335 stack_index_ += 2;
2336 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002337 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002338 } else {
2339 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2340 }
2341 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002342
2343 case Primitive::kPrimVoid:
2344 LOG(FATAL) << "Unexpected parameter type " << type;
2345 break;
2346 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002347 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002348}
2349
Calin Juravle175dc732015-08-25 15:42:32 +01002350void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2351 // The trampoline uses the same calling convention as dex calling conventions,
2352 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2353 // the method_idx.
2354 HandleInvoke(invoke);
2355}
2356
2357void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2358 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2359}
2360
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002361void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002362 // Explicit clinit checks triggered by static invokes must have been pruned by
2363 // art::PrepareForRegisterAllocation.
2364 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002365
Mark Mendellfb8d2792015-03-31 22:16:59 -04002366 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002367 if (intrinsic.TryDispatch(invoke)) {
2368 return;
2369 }
2370
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002371 HandleInvoke(invoke);
2372}
2373
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002374static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2375 if (invoke->GetLocations()->Intrinsified()) {
2376 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2377 intrinsic.Dispatch(invoke);
2378 return true;
2379 }
2380 return false;
2381}
2382
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002383void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002384 // Explicit clinit checks triggered by static invokes must have been pruned by
2385 // art::PrepareForRegisterAllocation.
2386 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002387
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002388 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2389 return;
2390 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002391
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002392 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002393 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002394 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002395 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002396}
2397
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002398void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002399 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002400 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002401}
2402
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002403void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002404 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002405 if (intrinsic.TryDispatch(invoke)) {
2406 return;
2407 }
2408
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002409 HandleInvoke(invoke);
2410}
2411
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002412void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002413 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2414 return;
2415 }
2416
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002417 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002418 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002419 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002420}
2421
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002422void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2423 HandleInvoke(invoke);
2424 // Add the hidden argument.
2425 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2426}
2427
2428void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2429 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002430 LocationSummary* locations = invoke->GetLocations();
2431 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2432 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002433 Location receiver = locations->InAt(0);
2434 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2435
Roland Levillain0d5a2812015-11-13 10:07:31 +00002436 // Set the hidden argument. This is safe to do this here, as RAX
2437 // won't be modified thereafter, before the `call` instruction.
2438 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002439 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002440
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002441 if (receiver.IsStackSlot()) {
2442 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002443 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002444 __ movl(temp, Address(temp, class_offset));
2445 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002446 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002447 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002448 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002449 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002450 // Instead of simply (possibly) unpoisoning `temp` here, we should
2451 // emit a read barrier for the previous class reference load.
2452 // However this is not required in practice, as this is an
2453 // intermediate/temporary reference and because the current
2454 // concurrent copying collector keeps the from-space memory
2455 // intact/accessible until the end of the marking phase (the
2456 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002457 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002458 // temp = temp->GetAddressOfIMT()
2459 __ movq(temp,
2460 Address(temp, mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
2461 // temp = temp->GetImtEntryAt(method_offset);
2462 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002463 invoke->GetImtIndex(), kX86_64PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002464 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002465 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002466 // call temp->GetEntryPoint();
Andreas Gampe542451c2016-07-26 09:02:02 -07002467 __ call(Address(
2468 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002469
2470 DCHECK(!codegen_->IsLeafMethod());
2471 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2472}
2473
Orion Hodsonac141392017-01-13 11:53:47 +00002474void LocationsBuilderX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2475 HandleInvoke(invoke);
2476}
2477
2478void InstructionCodeGeneratorX86_64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2479 codegen_->GenerateInvokePolymorphicCall(invoke);
2480}
2481
Roland Levillain88cb1752014-10-20 16:36:47 +01002482void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2483 LocationSummary* locations =
2484 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2485 switch (neg->GetResultType()) {
2486 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002487 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002488 locations->SetInAt(0, Location::RequiresRegister());
2489 locations->SetOut(Location::SameAsFirstInput());
2490 break;
2491
Roland Levillain88cb1752014-10-20 16:36:47 +01002492 case Primitive::kPrimFloat:
2493 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002494 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002495 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002496 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2501 }
2502}
2503
2504void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2505 LocationSummary* locations = neg->GetLocations();
2506 Location out = locations->Out();
2507 Location in = locations->InAt(0);
2508 switch (neg->GetResultType()) {
2509 case Primitive::kPrimInt:
2510 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002511 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002512 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002513 break;
2514
2515 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002516 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002517 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002518 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002519 break;
2520
Roland Levillain5368c212014-11-27 15:03:41 +00002521 case Primitive::kPrimFloat: {
2522 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002523 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002524 // Implement float negation with an exclusive or with value
2525 // 0x80000000 (mask for bit 31, representing the sign of a
2526 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002527 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002528 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002529 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002530 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002531
Roland Levillain5368c212014-11-27 15:03:41 +00002532 case Primitive::kPrimDouble: {
2533 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002534 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002535 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002536 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002537 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002538 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002539 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002540 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002541 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002542
2543 default:
2544 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2545 }
2546}
2547
Roland Levillaindff1f282014-11-05 14:15:05 +00002548void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2549 LocationSummary* locations =
2550 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2551 Primitive::Type result_type = conversion->GetResultType();
2552 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002553 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002554
David Brazdilb2bd1c52015-03-25 11:17:37 +00002555 // The Java language does not allow treating boolean as an integral type but
2556 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002557
Roland Levillaindff1f282014-11-05 14:15:05 +00002558 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002559 case Primitive::kPrimByte:
2560 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002561 case Primitive::kPrimLong:
2562 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002563 case Primitive::kPrimBoolean:
2564 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002565 case Primitive::kPrimShort:
2566 case Primitive::kPrimInt:
2567 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002568 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002569 locations->SetInAt(0, Location::Any());
2570 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2571 break;
2572
2573 default:
2574 LOG(FATAL) << "Unexpected type conversion from " << input_type
2575 << " to " << result_type;
2576 }
2577 break;
2578
Roland Levillain01a8d712014-11-14 16:27:39 +00002579 case Primitive::kPrimShort:
2580 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002581 case Primitive::kPrimLong:
2582 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002583 case Primitive::kPrimBoolean:
2584 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002585 case Primitive::kPrimByte:
2586 case Primitive::kPrimInt:
2587 case Primitive::kPrimChar:
2588 // Processing a Dex `int-to-short' instruction.
2589 locations->SetInAt(0, Location::Any());
2590 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2591 break;
2592
2593 default:
2594 LOG(FATAL) << "Unexpected type conversion from " << input_type
2595 << " to " << result_type;
2596 }
2597 break;
2598
Roland Levillain946e1432014-11-11 17:35:19 +00002599 case Primitive::kPrimInt:
2600 switch (input_type) {
2601 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002602 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002603 locations->SetInAt(0, Location::Any());
2604 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2605 break;
2606
2607 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002608 // Processing a Dex `float-to-int' instruction.
2609 locations->SetInAt(0, Location::RequiresFpuRegister());
2610 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002611 break;
2612
Roland Levillain946e1432014-11-11 17:35:19 +00002613 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002614 // Processing a Dex `double-to-int' instruction.
2615 locations->SetInAt(0, Location::RequiresFpuRegister());
2616 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002617 break;
2618
2619 default:
2620 LOG(FATAL) << "Unexpected type conversion from " << input_type
2621 << " to " << result_type;
2622 }
2623 break;
2624
Roland Levillaindff1f282014-11-05 14:15:05 +00002625 case Primitive::kPrimLong:
2626 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002627 case Primitive::kPrimBoolean:
2628 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002629 case Primitive::kPrimByte:
2630 case Primitive::kPrimShort:
2631 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002632 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002633 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002634 // TODO: We would benefit from a (to-be-implemented)
2635 // Location::RegisterOrStackSlot requirement for this input.
2636 locations->SetInAt(0, Location::RequiresRegister());
2637 locations->SetOut(Location::RequiresRegister());
2638 break;
2639
2640 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002641 // Processing a Dex `float-to-long' instruction.
2642 locations->SetInAt(0, Location::RequiresFpuRegister());
2643 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002644 break;
2645
Roland Levillaindff1f282014-11-05 14:15:05 +00002646 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002647 // Processing a Dex `double-to-long' instruction.
2648 locations->SetInAt(0, Location::RequiresFpuRegister());
2649 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002650 break;
2651
2652 default:
2653 LOG(FATAL) << "Unexpected type conversion from " << input_type
2654 << " to " << result_type;
2655 }
2656 break;
2657
Roland Levillain981e4542014-11-14 11:47:14 +00002658 case Primitive::kPrimChar:
2659 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002660 case Primitive::kPrimLong:
2661 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002662 case Primitive::kPrimBoolean:
2663 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002664 case Primitive::kPrimByte:
2665 case Primitive::kPrimShort:
2666 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002667 // Processing a Dex `int-to-char' instruction.
2668 locations->SetInAt(0, Location::Any());
2669 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2670 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::kPrimFloat:
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-float' 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 Levillain6d0e4832014-11-27 18:31:21 +00002692 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002693 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002694 locations->SetOut(Location::RequiresFpuRegister());
2695 break;
2696
Roland Levillaincff13742014-11-17 14:32:17 +00002697 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002698 // Processing a Dex `double-to-float' 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 };
2707 break;
2708
Roland Levillaindff1f282014-11-05 14:15:05 +00002709 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002710 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002711 case Primitive::kPrimBoolean:
2712 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002713 case Primitive::kPrimByte:
2714 case Primitive::kPrimShort:
2715 case Primitive::kPrimInt:
2716 case Primitive::kPrimChar:
2717 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002718 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002719 locations->SetOut(Location::RequiresFpuRegister());
2720 break;
2721
2722 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002723 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002724 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002725 locations->SetOut(Location::RequiresFpuRegister());
2726 break;
2727
Roland Levillaincff13742014-11-17 14:32:17 +00002728 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002729 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002730 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002731 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002732 break;
2733
2734 default:
2735 LOG(FATAL) << "Unexpected type conversion from " << input_type
2736 << " to " << result_type;
2737 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002738 break;
2739
2740 default:
2741 LOG(FATAL) << "Unexpected type conversion from " << input_type
2742 << " to " << result_type;
2743 }
2744}
2745
2746void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2747 LocationSummary* locations = conversion->GetLocations();
2748 Location out = locations->Out();
2749 Location in = locations->InAt(0);
2750 Primitive::Type result_type = conversion->GetResultType();
2751 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002752 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002753 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002754 case Primitive::kPrimByte:
2755 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002756 case Primitive::kPrimLong:
2757 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002758 case Primitive::kPrimBoolean:
2759 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002760 case Primitive::kPrimShort:
2761 case Primitive::kPrimInt:
2762 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002763 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002764 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002765 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002766 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002767 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002768 Address(CpuRegister(RSP), in.GetStackIndex()));
2769 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002771 Immediate(static_cast<int8_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain51d3fc42014-11-13 14:11:42 +00002772 }
2773 break;
2774
2775 default:
2776 LOG(FATAL) << "Unexpected type conversion from " << input_type
2777 << " to " << result_type;
2778 }
2779 break;
2780
Roland Levillain01a8d712014-11-14 16:27:39 +00002781 case Primitive::kPrimShort:
2782 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002783 case Primitive::kPrimLong:
2784 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002785 case Primitive::kPrimBoolean:
2786 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002787 case Primitive::kPrimByte:
2788 case Primitive::kPrimInt:
2789 case Primitive::kPrimChar:
2790 // Processing a Dex `int-to-short' instruction.
2791 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002792 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002793 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002794 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002795 Address(CpuRegister(RSP), in.GetStackIndex()));
2796 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002797 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002798 Immediate(static_cast<int16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain01a8d712014-11-14 16:27:39 +00002799 }
2800 break;
2801
2802 default:
2803 LOG(FATAL) << "Unexpected type conversion from " << input_type
2804 << " to " << result_type;
2805 }
2806 break;
2807
Roland Levillain946e1432014-11-11 17:35:19 +00002808 case Primitive::kPrimInt:
2809 switch (input_type) {
2810 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002811 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002812 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002814 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002815 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002816 Address(CpuRegister(RSP), in.GetStackIndex()));
2817 } else {
2818 DCHECK(in.IsConstant());
2819 DCHECK(in.GetConstant()->IsLongConstant());
2820 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002821 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002822 }
2823 break;
2824
Roland Levillain3f8f9362014-12-02 17:45:01 +00002825 case Primitive::kPrimFloat: {
2826 // Processing a Dex `float-to-int' instruction.
2827 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2828 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002829 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002830
2831 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002832 // if input >= (float)INT_MAX goto done
2833 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002834 __ j(kAboveEqual, &done);
2835 // if input == NaN goto nan
2836 __ j(kUnordered, &nan);
2837 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002838 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002839 __ jmp(&done);
2840 __ Bind(&nan);
2841 // output = 0
2842 __ xorl(output, output);
2843 __ Bind(&done);
2844 break;
2845 }
2846
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002847 case Primitive::kPrimDouble: {
2848 // Processing a Dex `double-to-int' instruction.
2849 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2850 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002851 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002852
2853 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002854 // if input >= (double)INT_MAX goto done
2855 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002856 __ j(kAboveEqual, &done);
2857 // if input == NaN goto nan
2858 __ j(kUnordered, &nan);
2859 // output = double-to-int-truncate(input)
2860 __ cvttsd2si(output, input);
2861 __ jmp(&done);
2862 __ Bind(&nan);
2863 // output = 0
2864 __ xorl(output, output);
2865 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002866 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002867 }
Roland Levillain946e1432014-11-11 17:35:19 +00002868
2869 default:
2870 LOG(FATAL) << "Unexpected type conversion from " << input_type
2871 << " to " << result_type;
2872 }
2873 break;
2874
Roland Levillaindff1f282014-11-05 14:15:05 +00002875 case Primitive::kPrimLong:
2876 switch (input_type) {
2877 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002878 case Primitive::kPrimBoolean:
2879 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002880 case Primitive::kPrimByte:
2881 case Primitive::kPrimShort:
2882 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002883 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002884 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002885 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002886 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002887 break;
2888
Roland Levillain624279f2014-12-04 11:54:28 +00002889 case Primitive::kPrimFloat: {
2890 // Processing a Dex `float-to-long' instruction.
2891 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2892 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002893 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002894
Mark Mendell92e83bf2015-05-07 11:25:03 -04002895 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002896 // if input >= (float)LONG_MAX goto done
2897 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002898 __ j(kAboveEqual, &done);
2899 // if input == NaN goto nan
2900 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002901 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002902 __ cvttss2si(output, input, true);
2903 __ jmp(&done);
2904 __ Bind(&nan);
2905 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002906 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002907 __ Bind(&done);
2908 break;
2909 }
2910
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002911 case Primitive::kPrimDouble: {
2912 // Processing a Dex `double-to-long' instruction.
2913 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2914 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002915 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002916
Mark Mendell92e83bf2015-05-07 11:25:03 -04002917 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002918 // if input >= (double)LONG_MAX goto done
2919 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002920 __ j(kAboveEqual, &done);
2921 // if input == NaN goto nan
2922 __ j(kUnordered, &nan);
2923 // output = double-to-long-truncate(input)
2924 __ cvttsd2si(output, input, true);
2925 __ jmp(&done);
2926 __ Bind(&nan);
2927 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002928 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002929 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002930 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002931 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002932
2933 default:
2934 LOG(FATAL) << "Unexpected type conversion from " << input_type
2935 << " to " << result_type;
2936 }
2937 break;
2938
Roland Levillain981e4542014-11-14 11:47:14 +00002939 case Primitive::kPrimChar:
2940 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002941 case Primitive::kPrimLong:
2942 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002943 case Primitive::kPrimBoolean:
2944 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002945 case Primitive::kPrimByte:
2946 case Primitive::kPrimShort:
2947 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002948 // Processing a Dex `int-to-char' instruction.
2949 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002950 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Vladimir Markob52bbde2016-02-12 12:06:05 +00002951 } else if (in.IsStackSlot() || in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002952 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002953 Address(CpuRegister(RSP), in.GetStackIndex()));
2954 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002955 __ movl(out.AsRegister<CpuRegister>(),
Vladimir Markob52bbde2016-02-12 12:06:05 +00002956 Immediate(static_cast<uint16_t>(Int64FromConstant(in.GetConstant()))));
Roland Levillain981e4542014-11-14 11:47:14 +00002957 }
2958 break;
2959
2960 default:
2961 LOG(FATAL) << "Unexpected type conversion from " << input_type
2962 << " to " << result_type;
2963 }
2964 break;
2965
Roland Levillaindff1f282014-11-05 14:15:05 +00002966 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002967 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002968 case Primitive::kPrimBoolean:
2969 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002970 case Primitive::kPrimByte:
2971 case Primitive::kPrimShort:
2972 case Primitive::kPrimInt:
2973 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002974 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002975 if (in.IsRegister()) {
2976 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2977 } else if (in.IsConstant()) {
2978 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2979 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05002980 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002981 } else {
2982 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2983 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2984 }
Roland Levillaincff13742014-11-17 14:32:17 +00002985 break;
2986
2987 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002988 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002989 if (in.IsRegister()) {
2990 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2991 } else if (in.IsConstant()) {
2992 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2993 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Pavel Vyssotski4c858cd2016-03-16 13:59:53 +06002994 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04002995 } else {
2996 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2997 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2998 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002999 break;
3000
Roland Levillaincff13742014-11-17 14:32:17 +00003001 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003002 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003003 if (in.IsFpuRegister()) {
3004 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3005 } else if (in.IsConstant()) {
3006 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
3007 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003008 codegen_->Load32BitValue(dest, static_cast<float>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003009 } else {
3010 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
3011 Address(CpuRegister(RSP), in.GetStackIndex()));
3012 }
Roland Levillaincff13742014-11-17 14:32:17 +00003013 break;
3014
3015 default:
3016 LOG(FATAL) << "Unexpected type conversion from " << input_type
3017 << " to " << result_type;
3018 };
3019 break;
3020
Roland Levillaindff1f282014-11-05 14:15:05 +00003021 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003022 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003023 case Primitive::kPrimBoolean:
3024 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003025 case Primitive::kPrimByte:
3026 case Primitive::kPrimShort:
3027 case Primitive::kPrimInt:
3028 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003029 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003030 if (in.IsRegister()) {
3031 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
3032 } else if (in.IsConstant()) {
3033 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
3034 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003035 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003036 } else {
3037 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3038 Address(CpuRegister(RSP), in.GetStackIndex()), false);
3039 }
Roland Levillaincff13742014-11-17 14:32:17 +00003040 break;
3041
3042 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003043 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003044 if (in.IsRegister()) {
3045 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
3046 } else if (in.IsConstant()) {
3047 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
3048 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003049 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003050 } else {
3051 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
3052 Address(CpuRegister(RSP), in.GetStackIndex()), true);
3053 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003054 break;
3055
Roland Levillaincff13742014-11-17 14:32:17 +00003056 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003057 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04003058 if (in.IsFpuRegister()) {
3059 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
3060 } else if (in.IsConstant()) {
3061 float v = in.GetConstant()->AsFloatConstant()->GetValue();
3062 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05003063 codegen_->Load64BitValue(dest, static_cast<double>(v));
Mark Mendell40741f32015-04-20 22:10:34 -04003064 } else {
3065 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
3066 Address(CpuRegister(RSP), in.GetStackIndex()));
3067 }
Roland Levillaincff13742014-11-17 14:32:17 +00003068 break;
3069
3070 default:
3071 LOG(FATAL) << "Unexpected type conversion from " << input_type
3072 << " to " << result_type;
3073 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003074 break;
3075
3076 default:
3077 LOG(FATAL) << "Unexpected type conversion from " << input_type
3078 << " to " << result_type;
3079 }
3080}
3081
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003082void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003083 LocationSummary* locations =
3084 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003085 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003086 case Primitive::kPrimInt: {
3087 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003088 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003090 break;
3091 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003092
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003093 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003094 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05003095 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04003096 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05003097 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003098 break;
3099 }
3100
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003101 case Primitive::kPrimDouble:
3102 case Primitive::kPrimFloat: {
3103 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003104 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003105 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003106 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003107 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003108
3109 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003110 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003111 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003112}
3113
3114void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
3115 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003116 Location first = locations->InAt(0);
3117 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003118 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01003119
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003120 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003121 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003122 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003123 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3124 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003125 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3126 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003127 } else {
3128 __ leal(out.AsRegister<CpuRegister>(), Address(
3129 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3130 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003131 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003132 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3133 __ addl(out.AsRegister<CpuRegister>(),
3134 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3135 } else {
3136 __ leal(out.AsRegister<CpuRegister>(), Address(
3137 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
3138 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003139 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00003140 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003141 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003142 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003143 break;
3144 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003145
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003146 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05003147 if (second.IsRegister()) {
3148 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3149 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003150 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3151 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05003152 } else {
3153 __ leaq(out.AsRegister<CpuRegister>(), Address(
3154 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
3155 }
3156 } else {
3157 DCHECK(second.IsConstant());
3158 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3159 int32_t int32_value = Low32Bits(value);
3160 DCHECK_EQ(int32_value, value);
3161 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3162 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
3163 } else {
3164 __ leaq(out.AsRegister<CpuRegister>(), Address(
3165 first.AsRegister<CpuRegister>(), int32_value));
3166 }
3167 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003168 break;
3169 }
3170
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003171 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003172 if (second.IsFpuRegister()) {
3173 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3174 } else if (second.IsConstant()) {
3175 __ addss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003176 codegen_->LiteralFloatAddress(
3177 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003178 } else {
3179 DCHECK(second.IsStackSlot());
3180 __ addss(first.AsFpuRegister<XmmRegister>(),
3181 Address(CpuRegister(RSP), second.GetStackIndex()));
3182 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003183 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003184 }
3185
3186 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003187 if (second.IsFpuRegister()) {
3188 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3189 } else if (second.IsConstant()) {
3190 __ addsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003191 codegen_->LiteralDoubleAddress(
3192 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003193 } else {
3194 DCHECK(second.IsDoubleStackSlot());
3195 __ addsd(first.AsFpuRegister<XmmRegister>(),
3196 Address(CpuRegister(RSP), second.GetStackIndex()));
3197 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003198 break;
3199 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003200
3201 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003202 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003203 }
3204}
3205
3206void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003207 LocationSummary* locations =
3208 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003209 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003210 case Primitive::kPrimInt: {
3211 locations->SetInAt(0, Location::RequiresRegister());
3212 locations->SetInAt(1, Location::Any());
3213 locations->SetOut(Location::SameAsFirstInput());
3214 break;
3215 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003216 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003217 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04003218 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003219 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003220 break;
3221 }
Calin Juravle11351682014-10-23 15:38:15 +01003222 case Primitive::kPrimFloat:
3223 case Primitive::kPrimDouble: {
3224 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003225 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01003226 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003227 break;
Calin Juravle11351682014-10-23 15:38:15 +01003228 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003229 default:
Calin Juravle11351682014-10-23 15:38:15 +01003230 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003231 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003232}
3233
3234void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
3235 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003236 Location first = locations->InAt(0);
3237 Location second = locations->InAt(1);
3238 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003239 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003240 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003241 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003242 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01003243 } else if (second.IsConstant()) {
3244 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003246 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003248 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003249 break;
3250 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003251 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003252 if (second.IsConstant()) {
3253 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3254 DCHECK(IsInt<32>(value));
3255 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
3256 } else {
3257 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
3258 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003259 break;
3260 }
3261
Calin Juravle11351682014-10-23 15:38:15 +01003262 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003263 if (second.IsFpuRegister()) {
3264 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3265 } else if (second.IsConstant()) {
3266 __ subss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003267 codegen_->LiteralFloatAddress(
3268 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003269 } else {
3270 DCHECK(second.IsStackSlot());
3271 __ subss(first.AsFpuRegister<XmmRegister>(),
3272 Address(CpuRegister(RSP), second.GetStackIndex()));
3273 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003274 break;
Calin Juravle11351682014-10-23 15:38:15 +01003275 }
3276
3277 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003278 if (second.IsFpuRegister()) {
3279 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3280 } else if (second.IsConstant()) {
3281 __ subsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003282 codegen_->LiteralDoubleAddress(
3283 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003284 } else {
3285 DCHECK(second.IsDoubleStackSlot());
3286 __ subsd(first.AsFpuRegister<XmmRegister>(),
3287 Address(CpuRegister(RSP), second.GetStackIndex()));
3288 }
Calin Juravle11351682014-10-23 15:38:15 +01003289 break;
3290 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003291
3292 default:
Calin Juravle11351682014-10-23 15:38:15 +01003293 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003294 }
3295}
3296
Calin Juravle34bacdf2014-10-07 20:23:36 +01003297void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3298 LocationSummary* locations =
3299 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3300 switch (mul->GetResultType()) {
3301 case Primitive::kPrimInt: {
3302 locations->SetInAt(0, Location::RequiresRegister());
3303 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003304 if (mul->InputAt(1)->IsIntConstant()) {
3305 // Can use 3 operand multiply.
3306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3307 } else {
3308 locations->SetOut(Location::SameAsFirstInput());
3309 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003310 break;
3311 }
3312 case Primitive::kPrimLong: {
3313 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003314 locations->SetInAt(1, Location::Any());
3315 if (mul->InputAt(1)->IsLongConstant() &&
3316 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003317 // Can use 3 operand multiply.
3318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3319 } else {
3320 locations->SetOut(Location::SameAsFirstInput());
3321 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003322 break;
3323 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003324 case Primitive::kPrimFloat:
3325 case Primitive::kPrimDouble: {
3326 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003327 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003328 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003329 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003330 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003331
3332 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003333 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003334 }
3335}
3336
3337void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3338 LocationSummary* locations = mul->GetLocations();
3339 Location first = locations->InAt(0);
3340 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003341 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003342 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003343 case Primitive::kPrimInt:
3344 // The constant may have ended up in a register, so test explicitly to avoid
3345 // problems where the output may not be the same as the first operand.
3346 if (mul->InputAt(1)->IsIntConstant()) {
3347 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3348 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3349 } else if (second.IsRegister()) {
3350 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003351 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003352 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003353 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003354 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003355 __ imull(first.AsRegister<CpuRegister>(),
3356 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003357 }
3358 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003359 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003360 // The constant may have ended up in a register, so test explicitly to avoid
3361 // problems where the output may not be the same as the first operand.
3362 if (mul->InputAt(1)->IsLongConstant()) {
3363 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3364 if (IsInt<32>(value)) {
3365 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3366 Immediate(static_cast<int32_t>(value)));
3367 } else {
3368 // Have to use the constant area.
3369 DCHECK(first.Equals(out));
3370 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3371 }
3372 } else if (second.IsRegister()) {
3373 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003374 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003375 } else {
3376 DCHECK(second.IsDoubleStackSlot());
3377 DCHECK(first.Equals(out));
3378 __ imulq(first.AsRegister<CpuRegister>(),
3379 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003380 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003381 break;
3382 }
3383
Calin Juravleb5bfa962014-10-21 18:02:24 +01003384 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003385 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003386 if (second.IsFpuRegister()) {
3387 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3388 } else if (second.IsConstant()) {
3389 __ mulss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003390 codegen_->LiteralFloatAddress(
3391 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003392 } else {
3393 DCHECK(second.IsStackSlot());
3394 __ mulss(first.AsFpuRegister<XmmRegister>(),
3395 Address(CpuRegister(RSP), second.GetStackIndex()));
3396 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003397 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003398 }
3399
3400 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003401 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003402 if (second.IsFpuRegister()) {
3403 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3404 } else if (second.IsConstant()) {
3405 __ mulsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003406 codegen_->LiteralDoubleAddress(
3407 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003408 } else {
3409 DCHECK(second.IsDoubleStackSlot());
3410 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3411 Address(CpuRegister(RSP), second.GetStackIndex()));
3412 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003413 break;
3414 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003415
3416 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003417 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003418 }
3419}
3420
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003421void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3422 uint32_t stack_adjustment, bool is_float) {
3423 if (source.IsStackSlot()) {
3424 DCHECK(is_float);
3425 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3426 } else if (source.IsDoubleStackSlot()) {
3427 DCHECK(!is_float);
3428 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3429 } else {
3430 // Write the value to the temporary location on the stack and load to FP stack.
3431 if (is_float) {
3432 Location stack_temp = Location::StackSlot(temp_offset);
3433 codegen_->Move(stack_temp, source);
3434 __ flds(Address(CpuRegister(RSP), temp_offset));
3435 } else {
3436 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3437 codegen_->Move(stack_temp, source);
3438 __ fldl(Address(CpuRegister(RSP), temp_offset));
3439 }
3440 }
3441}
3442
3443void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3444 Primitive::Type type = rem->GetResultType();
3445 bool is_float = type == Primitive::kPrimFloat;
3446 size_t elem_size = Primitive::ComponentSize(type);
3447 LocationSummary* locations = rem->GetLocations();
3448 Location first = locations->InAt(0);
3449 Location second = locations->InAt(1);
3450 Location out = locations->Out();
3451
3452 // Create stack space for 2 elements.
3453 // TODO: enhance register allocator to ask for stack temporaries.
3454 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3455
3456 // Load the values to the FP stack in reverse order, using temporaries if needed.
3457 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3458 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3459
3460 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003461 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003462 __ Bind(&retry);
3463 __ fprem();
3464
3465 // Move FP status to AX.
3466 __ fstsw();
3467
3468 // And see if the argument reduction is complete. This is signaled by the
3469 // C2 FPU flag bit set to 0.
3470 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3471 __ j(kNotEqual, &retry);
3472
3473 // We have settled on the final value. Retrieve it into an XMM register.
3474 // Store FP top of stack to real stack.
3475 if (is_float) {
3476 __ fsts(Address(CpuRegister(RSP), 0));
3477 } else {
3478 __ fstl(Address(CpuRegister(RSP), 0));
3479 }
3480
3481 // Pop the 2 items from the FP stack.
3482 __ fucompp();
3483
3484 // Load the value from the stack into an XMM register.
3485 DCHECK(out.IsFpuRegister()) << out;
3486 if (is_float) {
3487 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3488 } else {
3489 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3490 }
3491
3492 // And remove the temporary stack space we allocated.
3493 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3494}
3495
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003496void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3497 DCHECK(instruction->IsDiv() || instruction->IsRem());
3498
3499 LocationSummary* locations = instruction->GetLocations();
3500 Location second = locations->InAt(1);
3501 DCHECK(second.IsConstant());
3502
3503 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3504 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003505 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506
3507 DCHECK(imm == 1 || imm == -1);
3508
3509 switch (instruction->GetResultType()) {
3510 case Primitive::kPrimInt: {
3511 if (instruction->IsRem()) {
3512 __ xorl(output_register, output_register);
3513 } else {
3514 __ movl(output_register, input_register);
3515 if (imm == -1) {
3516 __ negl(output_register);
3517 }
3518 }
3519 break;
3520 }
3521
3522 case Primitive::kPrimLong: {
3523 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003524 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 } else {
3526 __ movq(output_register, input_register);
3527 if (imm == -1) {
3528 __ negq(output_register);
3529 }
3530 }
3531 break;
3532 }
3533
3534 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003535 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003536 }
3537}
3538
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003539void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540 LocationSummary* locations = instruction->GetLocations();
3541 Location second = locations->InAt(1);
3542
3543 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3544 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3545
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003546 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003547 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3548 uint64_t abs_imm = AbsOrMin(imm);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003549
3550 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3551
3552 if (instruction->GetResultType() == Primitive::kPrimInt) {
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003553 __ leal(tmp, Address(numerator, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554 __ testl(numerator, numerator);
3555 __ cmov(kGreaterEqual, tmp, numerator);
3556 int shift = CTZ(imm);
3557 __ sarl(tmp, Immediate(shift));
3558
3559 if (imm < 0) {
3560 __ negl(tmp);
3561 }
3562
3563 __ movl(output_register, tmp);
3564 } else {
3565 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3566 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3567
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003568 codegen_->Load64BitValue(rdx, abs_imm - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003569 __ addq(rdx, numerator);
3570 __ testq(numerator, numerator);
3571 __ cmov(kGreaterEqual, rdx, numerator);
3572 int shift = CTZ(imm);
3573 __ sarq(rdx, Immediate(shift));
3574
3575 if (imm < 0) {
3576 __ negq(rdx);
3577 }
3578
3579 __ movq(output_register, rdx);
3580 }
3581}
3582
3583void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3584 DCHECK(instruction->IsDiv() || instruction->IsRem());
3585
3586 LocationSummary* locations = instruction->GetLocations();
3587 Location second = locations->InAt(1);
3588
3589 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3590 : locations->GetTemp(0).AsRegister<CpuRegister>();
3591 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3592 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3593 : locations->Out().AsRegister<CpuRegister>();
3594 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3595
3596 DCHECK_EQ(RAX, eax.AsRegister());
3597 DCHECK_EQ(RDX, edx.AsRegister());
3598 if (instruction->IsDiv()) {
3599 DCHECK_EQ(RAX, out.AsRegister());
3600 } else {
3601 DCHECK_EQ(RDX, out.AsRegister());
3602 }
3603
3604 int64_t magic;
3605 int shift;
3606
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003607 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003608 if (instruction->GetResultType() == Primitive::kPrimInt) {
3609 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3610
3611 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3612
3613 __ movl(numerator, eax);
3614
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 __ movl(eax, Immediate(magic));
3616 __ imull(numerator);
3617
3618 if (imm > 0 && magic < 0) {
3619 __ addl(edx, numerator);
3620 } else if (imm < 0 && magic > 0) {
3621 __ subl(edx, numerator);
3622 }
3623
3624 if (shift != 0) {
3625 __ sarl(edx, Immediate(shift));
3626 }
3627
3628 __ movl(eax, edx);
3629 __ shrl(edx, Immediate(31));
3630 __ addl(edx, eax);
3631
3632 if (instruction->IsRem()) {
3633 __ movl(eax, numerator);
3634 __ imull(edx, Immediate(imm));
3635 __ subl(eax, edx);
3636 __ movl(edx, eax);
3637 } else {
3638 __ movl(eax, edx);
3639 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003640 } else {
3641 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3642
3643 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3644
3645 CpuRegister rax = eax;
3646 CpuRegister rdx = edx;
3647
3648 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3649
3650 // Save the numerator.
3651 __ movq(numerator, rax);
3652
3653 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003654 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003655
3656 // RDX:RAX = magic * numerator
3657 __ imulq(numerator);
3658
3659 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003660 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003661 __ addq(rdx, numerator);
3662 } else if (imm < 0 && magic > 0) {
3663 // RDX -= numerator
3664 __ subq(rdx, numerator);
3665 }
3666
3667 // Shift if needed.
3668 if (shift != 0) {
3669 __ sarq(rdx, Immediate(shift));
3670 }
3671
3672 // RDX += 1 if RDX < 0
3673 __ movq(rax, rdx);
3674 __ shrq(rdx, Immediate(63));
3675 __ addq(rdx, rax);
3676
3677 if (instruction->IsRem()) {
3678 __ movq(rax, numerator);
3679
3680 if (IsInt<32>(imm)) {
3681 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3682 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003683 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003684 }
3685
3686 __ subq(rax, rdx);
3687 __ movq(rdx, rax);
3688 } else {
3689 __ movq(rax, rdx);
3690 }
3691 }
3692}
3693
Calin Juravlebacfec32014-11-14 15:54:36 +00003694void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3695 DCHECK(instruction->IsDiv() || instruction->IsRem());
3696 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003697 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Calin Juravlebacfec32014-11-14 15:54:36 +00003698
3699 bool is_div = instruction->IsDiv();
3700 LocationSummary* locations = instruction->GetLocations();
3701
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003702 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3703 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003704
Roland Levillain271ab9c2014-11-27 15:23:57 +00003705 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003706 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003707
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003708 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003709 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003710
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003711 if (imm == 0) {
3712 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3713 } else if (imm == 1 || imm == -1) {
3714 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003715 } else if (instruction->IsDiv() && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003716 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003717 } else {
3718 DCHECK(imm <= -2 || imm >= 2);
3719 GenerateDivRemWithAnyConstant(instruction);
3720 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003721 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003722 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003723 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
David Srbecky9cd6d372016-02-09 15:24:47 +00003724 instruction, out.AsRegister(), type, is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003725 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003726
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003727 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3728 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3729 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3730 // so it's safe to just use negl instead of more complex comparisons.
3731 if (type == Primitive::kPrimInt) {
3732 __ cmpl(second_reg, Immediate(-1));
3733 __ j(kEqual, slow_path->GetEntryLabel());
3734 // edx:eax <- sign-extended of eax
3735 __ cdq();
3736 // eax = quotient, edx = remainder
3737 __ idivl(second_reg);
3738 } else {
3739 __ cmpq(second_reg, Immediate(-1));
3740 __ j(kEqual, slow_path->GetEntryLabel());
3741 // rdx:rax <- sign-extended of rax
3742 __ cqo();
3743 // rax = quotient, rdx = remainder
3744 __ idivq(second_reg);
3745 }
3746 __ Bind(slow_path->GetExitLabel());
3747 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003748}
3749
Calin Juravle7c4954d2014-10-28 16:57:40 +00003750void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3751 LocationSummary* locations =
3752 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3753 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003754 case Primitive::kPrimInt:
3755 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003756 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003757 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003758 locations->SetOut(Location::SameAsFirstInput());
3759 // Intel uses edx:eax as the dividend.
3760 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003761 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3762 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3763 // output and request another temp.
3764 if (div->InputAt(1)->IsConstant()) {
3765 locations->AddTemp(Location::RequiresRegister());
3766 }
Calin Juravled0d48522014-11-04 16:40:20 +00003767 break;
3768 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003769
Calin Juravle7c4954d2014-10-28 16:57:40 +00003770 case Primitive::kPrimFloat:
3771 case Primitive::kPrimDouble: {
3772 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003773 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003774 locations->SetOut(Location::SameAsFirstInput());
3775 break;
3776 }
3777
3778 default:
3779 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3780 }
3781}
3782
3783void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3784 LocationSummary* locations = div->GetLocations();
3785 Location first = locations->InAt(0);
3786 Location second = locations->InAt(1);
3787 DCHECK(first.Equals(locations->Out()));
3788
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003789 Primitive::Type type = div->GetResultType();
3790 switch (type) {
3791 case Primitive::kPrimInt:
3792 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003793 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003794 break;
3795 }
3796
Calin Juravle7c4954d2014-10-28 16:57:40 +00003797 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003798 if (second.IsFpuRegister()) {
3799 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3800 } else if (second.IsConstant()) {
3801 __ divss(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003802 codegen_->LiteralFloatAddress(
3803 second.GetConstant()->AsFloatConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003804 } else {
3805 DCHECK(second.IsStackSlot());
3806 __ divss(first.AsFpuRegister<XmmRegister>(),
3807 Address(CpuRegister(RSP), second.GetStackIndex()));
3808 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003809 break;
3810 }
3811
3812 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003813 if (second.IsFpuRegister()) {
3814 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3815 } else if (second.IsConstant()) {
3816 __ divsd(first.AsFpuRegister<XmmRegister>(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00003817 codegen_->LiteralDoubleAddress(
3818 second.GetConstant()->AsDoubleConstant()->GetValue()));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003819 } else {
3820 DCHECK(second.IsDoubleStackSlot());
3821 __ divsd(first.AsFpuRegister<XmmRegister>(),
3822 Address(CpuRegister(RSP), second.GetStackIndex()));
3823 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003824 break;
3825 }
3826
3827 default:
3828 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3829 }
3830}
3831
Calin Juravlebacfec32014-11-14 15:54:36 +00003832void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003833 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003834 LocationSummary* locations =
3835 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003836
3837 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003838 case Primitive::kPrimInt:
3839 case Primitive::kPrimLong: {
3840 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003841 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003842 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3843 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003844 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3845 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3846 // output and request another temp.
3847 if (rem->InputAt(1)->IsConstant()) {
3848 locations->AddTemp(Location::RequiresRegister());
3849 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003850 break;
3851 }
3852
3853 case Primitive::kPrimFloat:
3854 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003855 locations->SetInAt(0, Location::Any());
3856 locations->SetInAt(1, Location::Any());
3857 locations->SetOut(Location::RequiresFpuRegister());
3858 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003859 break;
3860 }
3861
3862 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003863 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003864 }
3865}
3866
3867void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3868 Primitive::Type type = rem->GetResultType();
3869 switch (type) {
3870 case Primitive::kPrimInt:
3871 case Primitive::kPrimLong: {
3872 GenerateDivRemIntegral(rem);
3873 break;
3874 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003875 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003876 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003877 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003878 break;
3879 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003880 default:
3881 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3882 }
3883}
3884
Calin Juravled0d48522014-11-04 16:40:20 +00003885void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003886 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003887 locations->SetInAt(0, Location::Any());
Calin Juravled0d48522014-11-04 16:40:20 +00003888}
3889
3890void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003891 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003892 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3893 codegen_->AddSlowPath(slow_path);
3894
3895 LocationSummary* locations = instruction->GetLocations();
3896 Location value = locations->InAt(0);
3897
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003898 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003899 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003900 case Primitive::kPrimByte:
3901 case Primitive::kPrimChar:
3902 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003903 case Primitive::kPrimInt: {
3904 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003905 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003906 __ j(kEqual, slow_path->GetEntryLabel());
3907 } else if (value.IsStackSlot()) {
3908 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3909 __ j(kEqual, slow_path->GetEntryLabel());
3910 } else {
3911 DCHECK(value.IsConstant()) << value;
3912 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003913 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003914 }
3915 }
3916 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003917 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003918 case Primitive::kPrimLong: {
3919 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003920 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003921 __ j(kEqual, slow_path->GetEntryLabel());
3922 } else if (value.IsDoubleStackSlot()) {
3923 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3924 __ j(kEqual, slow_path->GetEntryLabel());
3925 } else {
3926 DCHECK(value.IsConstant()) << value;
3927 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003928 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003929 }
3930 }
3931 break;
3932 }
3933 default:
3934 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003935 }
Calin Juravled0d48522014-11-04 16:40:20 +00003936}
3937
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3939 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3940
3941 LocationSummary* locations =
3942 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3943
3944 switch (op->GetResultType()) {
3945 case Primitive::kPrimInt:
3946 case Primitive::kPrimLong: {
3947 locations->SetInAt(0, Location::RequiresRegister());
3948 // The shift count needs to be in CL.
3949 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3950 locations->SetOut(Location::SameAsFirstInput());
3951 break;
3952 }
3953 default:
3954 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3955 }
3956}
3957
3958void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3959 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3960
3961 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003962 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003963 Location second = locations->InAt(1);
3964
3965 switch (op->GetResultType()) {
3966 case Primitive::kPrimInt: {
3967 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003968 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003969 if (op->IsShl()) {
3970 __ shll(first_reg, second_reg);
3971 } else if (op->IsShr()) {
3972 __ sarl(first_reg, second_reg);
3973 } else {
3974 __ shrl(first_reg, second_reg);
3975 }
3976 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003977 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003978 if (op->IsShl()) {
3979 __ shll(first_reg, imm);
3980 } else if (op->IsShr()) {
3981 __ sarl(first_reg, imm);
3982 } else {
3983 __ shrl(first_reg, imm);
3984 }
3985 }
3986 break;
3987 }
3988 case Primitive::kPrimLong: {
3989 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003990 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003991 if (op->IsShl()) {
3992 __ shlq(first_reg, second_reg);
3993 } else if (op->IsShr()) {
3994 __ sarq(first_reg, second_reg);
3995 } else {
3996 __ shrq(first_reg, second_reg);
3997 }
3998 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003999 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004000 if (op->IsShl()) {
4001 __ shlq(first_reg, imm);
4002 } else if (op->IsShr()) {
4003 __ sarq(first_reg, imm);
4004 } else {
4005 __ shrq(first_reg, imm);
4006 }
4007 }
4008 break;
4009 }
4010 default:
4011 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
Vladimir Marko351dddf2015-12-11 16:34:46 +00004012 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004013 }
4014}
4015
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004016void LocationsBuilderX86_64::VisitRor(HRor* ror) {
4017 LocationSummary* locations =
4018 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4019
4020 switch (ror->GetResultType()) {
4021 case Primitive::kPrimInt:
4022 case Primitive::kPrimLong: {
4023 locations->SetInAt(0, Location::RequiresRegister());
4024 // The shift count needs to be in CL (unless it is a constant).
4025 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, ror->InputAt(1)));
4026 locations->SetOut(Location::SameAsFirstInput());
4027 break;
4028 }
4029 default:
4030 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4031 UNREACHABLE();
4032 }
4033}
4034
4035void InstructionCodeGeneratorX86_64::VisitRor(HRor* ror) {
4036 LocationSummary* locations = ror->GetLocations();
4037 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
4038 Location second = locations->InAt(1);
4039
4040 switch (ror->GetResultType()) {
4041 case Primitive::kPrimInt:
4042 if (second.IsRegister()) {
4043 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4044 __ rorl(first_reg, second_reg);
4045 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004046 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004047 __ rorl(first_reg, imm);
4048 }
4049 break;
4050 case Primitive::kPrimLong:
4051 if (second.IsRegister()) {
4052 CpuRegister second_reg = second.AsRegister<CpuRegister>();
4053 __ rorq(first_reg, second_reg);
4054 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004055 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004056 __ rorq(first_reg, imm);
4057 }
4058 break;
4059 default:
4060 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4061 UNREACHABLE();
4062 }
4063}
4064
Calin Juravle9aec02f2014-11-18 23:06:35 +00004065void LocationsBuilderX86_64::VisitShl(HShl* shl) {
4066 HandleShift(shl);
4067}
4068
4069void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
4070 HandleShift(shl);
4071}
4072
4073void LocationsBuilderX86_64::VisitShr(HShr* shr) {
4074 HandleShift(shr);
4075}
4076
4077void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
4078 HandleShift(shr);
4079}
4080
4081void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
4082 HandleShift(ushr);
4083}
4084
4085void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
4086 HandleShift(ushr);
4087}
4088
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004089void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004090 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004091 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004092 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004093 if (instruction->IsStringAlloc()) {
4094 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4095 } else {
4096 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004097 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004098 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004099}
4100
4101void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004102 // Note: if heap poisoning is enabled, the entry point takes cares
4103 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004104 if (instruction->IsStringAlloc()) {
4105 // String is allocated through StringFactory. Call NewEmptyString entry point.
4106 CpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004107 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004108 __ gs()->movq(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString), /* no_rip */ true));
4109 __ call(Address(temp, code_offset.SizeValue()));
4110 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4111 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004112 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004113 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004114 DCHECK(!codegen_->IsLeafMethod());
4115 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004116}
4117
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004118void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
4119 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004120 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004121 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004122 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004123 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4124 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004125}
4126
4127void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004128 // Note: if heap poisoning is enabled, the entry point takes cares
4129 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004130 QuickEntrypointEnum entrypoint =
4131 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4132 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004133 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004134 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004135}
4136
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004137void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004138 LocationSummary* locations =
4139 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004140 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4141 if (location.IsStackSlot()) {
4142 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4143 } else if (location.IsDoubleStackSlot()) {
4144 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4145 }
4146 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004147}
4148
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004149void InstructionCodeGeneratorX86_64::VisitParameterValue(
4150 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004151 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004152}
4153
4154void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
4155 LocationSummary* locations =
4156 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4157 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4158}
4159
4160void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
4161 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4162 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004163}
4164
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004165void LocationsBuilderX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4166 LocationSummary* locations =
4167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4168 locations->SetInAt(0, Location::RequiresRegister());
4169 locations->SetOut(Location::RequiresRegister());
4170}
4171
4172void InstructionCodeGeneratorX86_64::VisitClassTableGet(HClassTableGet* instruction) {
4173 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004174 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004175 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004176 instruction->GetIndex(), kX86_64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004177 __ movq(locations->Out().AsRegister<CpuRegister>(),
4178 Address(locations->InAt(0).AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004179 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004180 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004181 instruction->GetIndex(), kX86_64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004182 __ movq(locations->Out().AsRegister<CpuRegister>(),
4183 Address(locations->InAt(0).AsRegister<CpuRegister>(),
4184 mirror::Class::ImtPtrOffset(kX86_64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004185 __ movq(locations->Out().AsRegister<CpuRegister>(),
4186 Address(locations->Out().AsRegister<CpuRegister>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004187 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004188}
4189
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004190void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004191 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004192 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004193 locations->SetInAt(0, Location::RequiresRegister());
4194 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004195}
4196
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004197void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
4198 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004199 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4200 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004201 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004202 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004203 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004204 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004205 break;
4206
4207 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004208 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004209 break;
4210
4211 default:
4212 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4213 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004214}
4215
David Brazdil66d126e2015-04-03 16:02:44 +01004216void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
4217 LocationSummary* locations =
4218 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4219 locations->SetInAt(0, Location::RequiresRegister());
4220 locations->SetOut(Location::SameAsFirstInput());
4221}
4222
4223void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004224 LocationSummary* locations = bool_not->GetLocations();
4225 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
4226 locations->Out().AsRegister<CpuRegister>().AsRegister());
4227 Location out = locations->Out();
4228 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
4229}
4230
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004231void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004232 LocationSummary* locations =
4233 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004234 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004235 locations->SetInAt(i, Location::Any());
4236 }
4237 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004238}
4239
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004240void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004241 LOG(FATAL) << "Unimplemented";
4242}
4243
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004244void CodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004245 /*
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004246 * According to the JSR-133 Cookbook, for x86-64 only StoreLoad/AnyAny barriers need memory fence.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004247 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86-64 memory model.
Calin Juravle52c48962014-12-16 17:02:57 +00004248 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4249 */
4250 switch (kind) {
4251 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004252 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004253 break;
4254 }
4255 case MemBarrierKind::kAnyStore:
4256 case MemBarrierKind::kLoadAny:
4257 case MemBarrierKind::kStoreStore: {
4258 // nop
4259 break;
4260 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004261 case MemBarrierKind::kNTStoreStore:
4262 // Non-Temporal Store/Store needs an explicit fence.
4263 MemoryFence(/* non-temporal */ true);
4264 break;
Calin Juravle52c48962014-12-16 17:02:57 +00004265 }
4266}
4267
4268void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
4269 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4270
Roland Levillain0d5a2812015-11-13 10:07:31 +00004271 bool object_field_get_with_read_barrier =
4272 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004273 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004274 new (GetGraph()->GetArena()) LocationSummary(instruction,
4275 object_field_get_with_read_barrier ?
4276 LocationSummary::kCallOnSlowPath :
4277 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004278 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004279 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004280 }
Calin Juravle52c48962014-12-16 17:02:57 +00004281 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004282 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4283 locations->SetOut(Location::RequiresFpuRegister());
4284 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004285 // The output overlaps for an object field get when read barriers
4286 // are enabled: we do not want the move to overwrite the object's
4287 // location, as we need it to emit the read barrier.
4288 locations->SetOut(
4289 Location::RequiresRegister(),
4290 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004291 }
Calin Juravle52c48962014-12-16 17:02:57 +00004292}
4293
4294void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
4295 const FieldInfo& field_info) {
4296 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4297
4298 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004299 Location base_loc = locations->InAt(0);
4300 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00004301 Location out = locations->Out();
4302 bool is_volatile = field_info.IsVolatile();
4303 Primitive::Type field_type = field_info.GetFieldType();
4304 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4305
4306 switch (field_type) {
4307 case Primitive::kPrimBoolean: {
4308 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4309 break;
4310 }
4311
4312 case Primitive::kPrimByte: {
4313 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
4314 break;
4315 }
4316
4317 case Primitive::kPrimShort: {
4318 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4319 break;
4320 }
4321
4322 case Primitive::kPrimChar: {
4323 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
4324 break;
4325 }
4326
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004327 case Primitive::kPrimInt: {
Calin Juravle52c48962014-12-16 17:02:57 +00004328 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4329 break;
4330 }
4331
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004332 case Primitive::kPrimNot: {
4333 // /* HeapReference<Object> */ out = *(base + offset)
4334 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004335 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004336 // CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004337 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004338 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004339 if (is_volatile) {
4340 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4341 }
4342 } else {
4343 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
4344 codegen_->MaybeRecordImplicitNullCheck(instruction);
4345 if (is_volatile) {
4346 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4347 }
4348 // If read barriers are enabled, emit read barriers other than
4349 // Baker's using a slow path (and also unpoison the loaded
4350 // reference, if heap poisoning is enabled).
4351 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4352 }
4353 break;
4354 }
4355
Calin Juravle52c48962014-12-16 17:02:57 +00004356 case Primitive::kPrimLong: {
4357 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
4358 break;
4359 }
4360
4361 case Primitive::kPrimFloat: {
4362 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4363 break;
4364 }
4365
4366 case Primitive::kPrimDouble: {
4367 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
4368 break;
4369 }
4370
4371 case Primitive::kPrimVoid:
4372 LOG(FATAL) << "Unreachable type " << field_type;
4373 UNREACHABLE();
4374 }
4375
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004376 if (field_type == Primitive::kPrimNot) {
4377 // Potential implicit null checks, in the case of reference
4378 // fields, are handled in the previous switch statement.
4379 } else {
4380 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004381 }
Roland Levillain4d027112015-07-01 15:41:14 +01004382
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004383 if (is_volatile) {
4384 if (field_type == Primitive::kPrimNot) {
4385 // Memory barriers, in the case of references, are also handled
4386 // in the previous switch statement.
4387 } else {
4388 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4389 }
Roland Levillain4d027112015-07-01 15:41:14 +01004390 }
Calin Juravle52c48962014-12-16 17:02:57 +00004391}
4392
4393void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4394 const FieldInfo& field_info) {
4395 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4396
4397 LocationSummary* locations =
4398 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004399 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004400 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004401 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004402 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004403
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004404 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004405 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004406 if (is_volatile) {
4407 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4408 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4409 } else {
4410 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4411 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004412 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004413 if (is_volatile) {
4414 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4415 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4416 } else {
4417 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4418 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004419 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004420 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004421 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004422 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004423 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004424 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4425 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004426 locations->AddTemp(Location::RequiresRegister());
4427 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428}
4429
Calin Juravle52c48962014-12-16 17:02:57 +00004430void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004431 const FieldInfo& field_info,
4432 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004433 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4434
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004435 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004436 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4437 Location value = locations->InAt(1);
4438 bool is_volatile = field_info.IsVolatile();
4439 Primitive::Type field_type = field_info.GetFieldType();
4440 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4441
4442 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004443 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004444 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004445
Mark Mendellea5af682015-10-22 17:35:49 -04004446 bool maybe_record_implicit_null_check_done = false;
4447
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448 switch (field_type) {
4449 case Primitive::kPrimBoolean:
4450 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004451 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004452 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004453 __ movb(Address(base, offset), Immediate(v));
4454 } else {
4455 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4456 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004457 break;
4458 }
4459
4460 case Primitive::kPrimShort:
4461 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004462 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004463 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004464 __ movw(Address(base, offset), Immediate(v));
4465 } else {
4466 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4467 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004468 break;
4469 }
4470
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004471 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004472 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004473 if (value.IsConstant()) {
4474 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004475 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4476 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4477 // Note: if heap poisoning is enabled, no need to poison
4478 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004479 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004480 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004481 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4482 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4483 __ movl(temp, value.AsRegister<CpuRegister>());
4484 __ PoisonHeapReference(temp);
4485 __ movl(Address(base, offset), temp);
4486 } else {
4487 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4488 }
Mark Mendell40741f32015-04-20 22:10:34 -04004489 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004490 break;
4491 }
4492
4493 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004494 if (value.IsConstant()) {
4495 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004496 codegen_->MoveInt64ToAddress(Address(base, offset),
4497 Address(base, offset + sizeof(int32_t)),
4498 v,
4499 instruction);
4500 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004501 } else {
4502 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4503 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504 break;
4505 }
4506
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004507 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004508 if (value.IsConstant()) {
4509 int32_t v =
4510 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4511 __ movl(Address(base, offset), Immediate(v));
4512 } else {
4513 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4514 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004515 break;
4516 }
4517
4518 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004519 if (value.IsConstant()) {
4520 int64_t v =
4521 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4522 codegen_->MoveInt64ToAddress(Address(base, offset),
4523 Address(base, offset + sizeof(int32_t)),
4524 v,
4525 instruction);
4526 maybe_record_implicit_null_check_done = true;
4527 } else {
4528 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4529 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004530 break;
4531 }
4532
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004533 case Primitive::kPrimVoid:
4534 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004535 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004536 }
Calin Juravle52c48962014-12-16 17:02:57 +00004537
Mark Mendellea5af682015-10-22 17:35:49 -04004538 if (!maybe_record_implicit_null_check_done) {
4539 codegen_->MaybeRecordImplicitNullCheck(instruction);
4540 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004541
4542 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4543 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4544 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004545 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004546 }
4547
Calin Juravle52c48962014-12-16 17:02:57 +00004548 if (is_volatile) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004549 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004550 }
4551}
4552
4553void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4554 HandleFieldSet(instruction, instruction->GetFieldInfo());
4555}
4556
4557void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004558 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004559}
4560
4561void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004562 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004563}
4564
4565void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004566 HandleFieldGet(instruction, instruction->GetFieldInfo());
4567}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004568
Calin Juravle52c48962014-12-16 17:02:57 +00004569void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4570 HandleFieldGet(instruction);
4571}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004572
Calin Juravle52c48962014-12-16 17:02:57 +00004573void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4574 HandleFieldGet(instruction, instruction->GetFieldInfo());
4575}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004576
Calin Juravle52c48962014-12-16 17:02:57 +00004577void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4578 HandleFieldSet(instruction, instruction->GetFieldInfo());
4579}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004580
Calin Juravle52c48962014-12-16 17:02:57 +00004581void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004582 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004583}
4584
Calin Juravlee460d1d2015-09-29 04:52:17 +01004585void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4586 HUnresolvedInstanceFieldGet* instruction) {
4587 FieldAccessCallingConventionX86_64 calling_convention;
4588 codegen_->CreateUnresolvedFieldLocationSummary(
4589 instruction, instruction->GetFieldType(), calling_convention);
4590}
4591
4592void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4593 HUnresolvedInstanceFieldGet* instruction) {
4594 FieldAccessCallingConventionX86_64 calling_convention;
4595 codegen_->GenerateUnresolvedFieldAccess(instruction,
4596 instruction->GetFieldType(),
4597 instruction->GetFieldIndex(),
4598 instruction->GetDexPc(),
4599 calling_convention);
4600}
4601
4602void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4603 HUnresolvedInstanceFieldSet* instruction) {
4604 FieldAccessCallingConventionX86_64 calling_convention;
4605 codegen_->CreateUnresolvedFieldLocationSummary(
4606 instruction, instruction->GetFieldType(), calling_convention);
4607}
4608
4609void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4610 HUnresolvedInstanceFieldSet* instruction) {
4611 FieldAccessCallingConventionX86_64 calling_convention;
4612 codegen_->GenerateUnresolvedFieldAccess(instruction,
4613 instruction->GetFieldType(),
4614 instruction->GetFieldIndex(),
4615 instruction->GetDexPc(),
4616 calling_convention);
4617}
4618
4619void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4620 HUnresolvedStaticFieldGet* instruction) {
4621 FieldAccessCallingConventionX86_64 calling_convention;
4622 codegen_->CreateUnresolvedFieldLocationSummary(
4623 instruction, instruction->GetFieldType(), calling_convention);
4624}
4625
4626void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4627 HUnresolvedStaticFieldGet* instruction) {
4628 FieldAccessCallingConventionX86_64 calling_convention;
4629 codegen_->GenerateUnresolvedFieldAccess(instruction,
4630 instruction->GetFieldType(),
4631 instruction->GetFieldIndex(),
4632 instruction->GetDexPc(),
4633 calling_convention);
4634}
4635
4636void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4637 HUnresolvedStaticFieldSet* instruction) {
4638 FieldAccessCallingConventionX86_64 calling_convention;
4639 codegen_->CreateUnresolvedFieldLocationSummary(
4640 instruction, instruction->GetFieldType(), calling_convention);
4641}
4642
4643void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4644 HUnresolvedStaticFieldSet* instruction) {
4645 FieldAccessCallingConventionX86_64 calling_convention;
4646 codegen_->GenerateUnresolvedFieldAccess(instruction,
4647 instruction->GetFieldType(),
4648 instruction->GetFieldIndex(),
4649 instruction->GetDexPc(),
4650 calling_convention);
4651}
4652
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004653void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004654 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4655 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
4656 ? Location::RequiresRegister()
4657 : Location::Any();
4658 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004659}
4660
Calin Juravle2ae48182016-03-16 14:05:09 +00004661void CodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4662 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004663 return;
4664 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004665 LocationSummary* locations = instruction->GetLocations();
4666 Location obj = locations->InAt(0);
4667
4668 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004669 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004670}
4671
Calin Juravle2ae48182016-03-16 14:05:09 +00004672void CodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004673 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004674 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004675
4676 LocationSummary* locations = instruction->GetLocations();
4677 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004678
4679 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004680 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004681 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004682 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004683 } else {
4684 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004685 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004686 __ jmp(slow_path->GetEntryLabel());
4687 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004688 }
4689 __ j(kEqual, slow_path->GetEntryLabel());
4690}
4691
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004692void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004693 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004694}
4695
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004696void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004697 bool object_array_get_with_read_barrier =
4698 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004699 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004700 new (GetGraph()->GetArena()) LocationSummary(instruction,
4701 object_array_get_with_read_barrier ?
4702 LocationSummary::kCallOnSlowPath :
4703 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004704 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004705 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004706 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004707 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004708 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004709 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4710 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4711 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004712 // The output overlaps for an object array get when read barriers
4713 // are enabled: we do not want the move to overwrite the array's
4714 // location, as we need it to emit the read barrier.
4715 locations->SetOut(
4716 Location::RequiresRegister(),
4717 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004718 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004719}
4720
4721void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4722 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004723 Location obj_loc = locations->InAt(0);
4724 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004725 Location index = locations->InAt(1);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004726 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004727 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004728
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004729 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004730 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004731 case Primitive::kPrimBoolean: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004732 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004733 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004734 break;
4735 }
4736
4737 case Primitive::kPrimByte: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004738 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004739 __ movsxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004740 break;
4741 }
4742
4743 case Primitive::kPrimShort: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004744 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004745 __ movsxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004746 break;
4747 }
4748
4749 case Primitive::kPrimChar: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004750 CpuRegister out = out_loc.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07004751 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
4752 // Branch cases into compressed and uncompressed for each index's type.
4753 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4754 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00004755 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07004756 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004757 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4758 "Expecting 0=compressed, 1=uncompressed");
4759 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07004760 __ movzxb(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_1, data_offset));
4761 __ jmp(&done);
4762 __ Bind(&not_compressed);
4763 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4764 __ Bind(&done);
4765 } else {
4766 __ movzxw(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_2, data_offset));
4767 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768 break;
4769 }
4770
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004771 case Primitive::kPrimInt: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004772 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004773 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004774 break;
4775 }
4776
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004777 case Primitive::kPrimNot: {
4778 static_assert(
4779 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4780 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004781 // /* HeapReference<Object> */ out =
4782 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4783 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004784 // Note that a potential implicit null check is handled in this
Roland Levillaina1aa3b12016-10-26 13:03:38 +01004785 // CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier call.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004786 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004787 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004788 } else {
4789 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004790 __ movl(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
4791 codegen_->MaybeRecordImplicitNullCheck(instruction);
4792 // If read barriers are enabled, emit read barriers other than
4793 // Baker's using a slow path (and also unpoison the loaded
4794 // reference, if heap poisoning is enabled).
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004795 if (index.IsConstant()) {
4796 uint32_t offset =
4797 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004798 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4799 } else {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004800 codegen_->MaybeGenerateReadBarrierSlow(
4801 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4802 }
4803 }
4804 break;
4805 }
4806
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004807 case Primitive::kPrimLong: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004808 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004809 __ movq(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004810 break;
4811 }
4812
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004813 case Primitive::kPrimFloat: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004814 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004815 __ movss(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004816 break;
4817 }
4818
4819 case Primitive::kPrimDouble: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004820 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004821 __ movsd(out, CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004822 break;
4823 }
4824
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004825 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004826 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004827 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004828 }
Roland Levillain4d027112015-07-01 15:41:14 +01004829
4830 if (type == Primitive::kPrimNot) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00004831 // Potential implicit null checks, in the case of reference
4832 // arrays, are handled in the previous switch statement.
4833 } else {
4834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004835 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004836}
4837
4838void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004839 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004840
4841 bool needs_write_barrier =
4842 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004843 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004844
Nicolas Geoffray39468442014-09-02 15:17:15 +01004845 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004847 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004848 LocationSummary::kCallOnSlowPath :
4849 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004850
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004851 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004852 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4853 if (Primitive::IsFloatingPointType(value_type)) {
4854 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004855 } else {
4856 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4857 }
4858
4859 if (needs_write_barrier) {
4860 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004861 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004862 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004863 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004864}
4865
4866void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4867 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004868 Location array_loc = locations->InAt(0);
4869 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004870 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004871 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004872 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004873 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004874 bool needs_write_barrier =
4875 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004876 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4877 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4878 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879
4880 switch (value_type) {
4881 case Primitive::kPrimBoolean:
4882 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004883 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004884 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004885 if (value.IsRegister()) {
4886 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004887 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004888 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004889 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004890 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004891 break;
4892 }
4893
4894 case Primitive::kPrimShort:
4895 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004896 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004897 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004898 if (value.IsRegister()) {
4899 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004901 DCHECK(value.IsConstant()) << value;
4902 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004903 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004904 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004905 break;
4906 }
4907
4908 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004909 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004910 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004911
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004912 if (!value.IsRegister()) {
4913 // Just setting null.
4914 DCHECK(instruction->InputAt(2)->IsNullConstant());
4915 DCHECK(value.IsConstant()) << value;
4916 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004917 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004918 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004919 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004920 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004921 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004922
4923 DCHECK(needs_write_barrier);
4924 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004925 // We cannot use a NearLabel for `done`, as its range may be too
4926 // short when Baker read barriers are enabled.
4927 Label done;
4928 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004929 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004930 Location temp_loc = locations->GetTemp(0);
4931 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004932 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004933 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4934 codegen_->AddSlowPath(slow_path);
4935 if (instruction->GetValueCanBeNull()) {
4936 __ testl(register_value, register_value);
4937 __ j(kNotEqual, &not_null);
4938 __ movl(address, Immediate(0));
4939 codegen_->MaybeRecordImplicitNullCheck(instruction);
4940 __ jmp(&done);
4941 __ Bind(&not_null);
4942 }
4943
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004944 // Note that when Baker read barriers are enabled, the type
4945 // checks are performed without read barriers. This is fine,
4946 // even in the case where a class object is in the from-space
4947 // after the flip, as a comparison involving such a type would
4948 // not produce a false positive; it may of course produce a
4949 // false negative, in which case we would take the ArraySet
4950 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004951
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004952 // /* HeapReference<Class> */ temp = array->klass_
4953 __ movl(temp, Address(array, class_offset));
4954 codegen_->MaybeRecordImplicitNullCheck(instruction);
4955 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004956
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004957 // /* HeapReference<Class> */ temp = temp->component_type_
4958 __ movl(temp, Address(temp, component_offset));
4959 // If heap poisoning is enabled, no need to unpoison `temp`
4960 // nor the object reference in `register_value->klass`, as
4961 // we are comparing two poisoned references.
4962 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004963
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004964 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4965 __ j(kEqual, &do_put);
4966 // If heap poisoning is enabled, the `temp` reference has
4967 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004968 __ MaybeUnpoisonHeapReference(temp);
4969
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004970 // If heap poisoning is enabled, no need to unpoison the
4971 // heap reference loaded below, as it is only used for a
4972 // comparison with null.
4973 __ cmpl(Address(temp, super_offset), Immediate(0));
4974 __ j(kNotEqual, slow_path->GetEntryLabel());
4975 __ Bind(&do_put);
4976 } else {
4977 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004978 }
4979 }
4980
4981 if (kPoisonHeapReferences) {
4982 __ movl(temp, register_value);
4983 __ PoisonHeapReference(temp);
4984 __ movl(address, temp);
4985 } else {
4986 __ movl(address, register_value);
4987 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004988 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004989 codegen_->MaybeRecordImplicitNullCheck(instruction);
4990 }
4991
4992 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4993 codegen_->MarkGCCard(
4994 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4995 __ Bind(&done);
4996
4997 if (slow_path != nullptr) {
4998 __ Bind(slow_path->GetExitLabel());
4999 }
5000
5001 break;
5002 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005003
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005004 case Primitive::kPrimInt: {
5005 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005006 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005007 if (value.IsRegister()) {
5008 __ movl(address, value.AsRegister<CpuRegister>());
5009 } else {
5010 DCHECK(value.IsConstant()) << value;
5011 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5012 __ movl(address, Immediate(v));
5013 }
5014 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005015 break;
5016 }
5017
5018 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005019 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005020 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005021 if (value.IsRegister()) {
5022 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04005023 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005024 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005025 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005026 Address address_high =
5027 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005028 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005029 }
5030 break;
5031 }
5032
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005033 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005034 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005035 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005036 if (value.IsFpuRegister()) {
5037 __ movss(address, value.AsFpuRegister<XmmRegister>());
5038 } else {
5039 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005040 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
Mark Mendellea5af682015-10-22 17:35:49 -04005041 __ movl(address, Immediate(v));
5042 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005043 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005044 break;
5045 }
5046
5047 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005048 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005049 Address address = CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04005050 if (value.IsFpuRegister()) {
5051 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5052 codegen_->MaybeRecordImplicitNullCheck(instruction);
5053 } else {
5054 int64_t v =
5055 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005056 Address address_high =
5057 CodeGeneratorX86_64::ArrayAddress(array, index, TIMES_8, offset + sizeof(int32_t));
Mark Mendellea5af682015-10-22 17:35:49 -04005058 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
5059 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005060 break;
5061 }
5062
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063 case Primitive::kPrimVoid:
5064 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005065 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066 }
5067}
5068
5069void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005070 LocationSummary* locations =
5071 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005072 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005073 if (!instruction->IsEmittedAtUseSite()) {
5074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5075 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076}
5077
5078void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005079 if (instruction->IsEmittedAtUseSite()) {
5080 return;
5081 }
5082
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005084 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005085 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5086 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005088 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005089 // Mask out most significant bit in case the array is String's array of char.
5090 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005091 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005092 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093}
5094
5095void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005096 RegisterSet caller_saves = RegisterSet::Empty();
5097 InvokeRuntimeCallingConvention calling_convention;
5098 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5099 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5100 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005101 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005102 HInstruction* length = instruction->InputAt(1);
5103 if (!length->IsEmittedAtUseSite()) {
5104 locations->SetInAt(1, Location::RegisterOrConstant(length));
5105 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005106}
5107
5108void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5109 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005110 Location index_loc = locations->InAt(0);
5111 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005112 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005113
Mark Mendell99dbd682015-04-22 16:18:52 -04005114 if (length_loc.IsConstant()) {
5115 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5116 if (index_loc.IsConstant()) {
5117 // BCE will remove the bounds check if we are guarenteed to pass.
5118 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5119 if (index < 0 || index >= length) {
5120 codegen_->AddSlowPath(slow_path);
5121 __ jmp(slow_path->GetEntryLabel());
5122 } else {
5123 // Some optimization after BCE may have generated this, and we should not
5124 // generate a bounds check if it is a valid range.
5125 }
5126 return;
5127 }
5128
5129 // We have to reverse the jump condition because the length is the constant.
5130 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5131 __ cmpl(index_reg, Immediate(length));
5132 codegen_->AddSlowPath(slow_path);
5133 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005134 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005135 HInstruction* array_length = instruction->InputAt(1);
5136 if (array_length->IsEmittedAtUseSite()) {
5137 // Address the length field in the array.
5138 DCHECK(array_length->IsArrayLength());
5139 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5140 Location array_loc = array_length->GetLocations()->InAt(0);
5141 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005142 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005143 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5144 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005145 CpuRegister length_reg = CpuRegister(TMP);
5146 __ movl(length_reg, array_len);
5147 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005148 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005149 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005150 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005151 // Checking the bound for general case:
5152 // Array of char or String's array when the compression feature off.
5153 if (index_loc.IsConstant()) {
5154 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5155 __ cmpl(array_len, Immediate(value));
5156 } else {
5157 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5158 }
5159 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005160 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005161 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005162 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005163 }
5164 codegen_->AddSlowPath(slow_path);
5165 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005166 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167}
5168
5169void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5170 CpuRegister card,
5171 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005172 CpuRegister value,
5173 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005174 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005175 if (value_can_be_null) {
5176 __ testl(value, value);
5177 __ j(kEqual, &is_null);
5178 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005179 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005180 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005181 __ movq(temp, object);
5182 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005183 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005184 if (value_can_be_null) {
5185 __ Bind(&is_null);
5186 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187}
5188
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005189void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005190 LOG(FATAL) << "Unimplemented";
5191}
5192
5193void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005194 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5195}
5196
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005197void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005198 LocationSummary* locations =
5199 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005200 // In suspend check slow path, usually there are no caller-save registers at all.
5201 // If SIMD instructions are present, however, we force spilling all live SIMD
5202 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005203 locations->SetCustomSlowPathCallerSaves(
5204 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005205}
5206
5207void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005208 HBasicBlock* block = instruction->GetBlock();
5209 if (block->GetLoopInformation() != nullptr) {
5210 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5211 // The back edge will generate the suspend check.
5212 return;
5213 }
5214 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5215 // The goto will generate the suspend check.
5216 return;
5217 }
5218 GenerateSuspendCheck(instruction, nullptr);
5219}
5220
5221void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5222 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005223 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005224 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5225 if (slow_path == nullptr) {
5226 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5227 instruction->SetSlowPath(slow_path);
5228 codegen_->AddSlowPath(slow_path);
5229 if (successor != nullptr) {
5230 DCHECK(successor->IsLoopHeader());
5231 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5232 }
5233 } else {
5234 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5235 }
5236
Andreas Gampe542451c2016-07-26 09:02:02 -07005237 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005238 /* no_rip */ true),
5239 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005240 if (successor == nullptr) {
5241 __ j(kNotEqual, slow_path->GetEntryLabel());
5242 __ Bind(slow_path->GetReturnLabel());
5243 } else {
5244 __ j(kEqual, codegen_->GetLabelOf(successor));
5245 __ jmp(slow_path->GetEntryLabel());
5246 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005247}
5248
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005249X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5250 return codegen_->GetAssembler();
5251}
5252
5253void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005254 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005255 Location source = move->GetSource();
5256 Location destination = move->GetDestination();
5257
5258 if (source.IsRegister()) {
5259 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005260 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005261 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005262 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005263 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005264 } else {
5265 DCHECK(destination.IsDoubleStackSlot());
5266 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005267 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005268 }
5269 } else if (source.IsStackSlot()) {
5270 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005271 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005272 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005273 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005274 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005275 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005276 } else {
5277 DCHECK(destination.IsStackSlot());
5278 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5279 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5280 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005281 } else if (source.IsDoubleStackSlot()) {
5282 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005283 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005284 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005285 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005286 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5287 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005288 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005289 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005290 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5291 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5292 }
Aart Bik5576f372017-03-23 16:17:37 -07005293 } else if (source.IsSIMDStackSlot()) {
5294 DCHECK(destination.IsFpuRegister());
5295 __ movups(destination.AsFpuRegister<XmmRegister>(),
5296 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005297 } else if (source.IsConstant()) {
5298 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005299 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5300 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005301 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005302 if (value == 0) {
5303 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5304 } else {
5305 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5306 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005307 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005308 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005309 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005310 }
5311 } else if (constant->IsLongConstant()) {
5312 int64_t value = constant->AsLongConstant()->GetValue();
5313 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005314 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005315 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005316 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005317 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005318 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005319 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005320 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005321 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005322 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005323 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005324 } else {
5325 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005326 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005327 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5328 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005329 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005330 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005331 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005332 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005333 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005334 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005335 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005336 } else {
5337 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005338 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005339 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005340 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005341 } else if (source.IsFpuRegister()) {
5342 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005343 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005344 } else if (destination.IsStackSlot()) {
5345 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005346 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005347 } else if (destination.IsDoubleStackSlot()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005348 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005349 source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005350 } else {
5351 DCHECK(destination.IsSIMDStackSlot());
5352 __ movups(Address(CpuRegister(RSP), destination.GetStackIndex()),
5353 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005354 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005355 }
5356}
5357
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005358void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005359 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005360 __ movl(Address(CpuRegister(RSP), mem), reg);
5361 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005362}
5363
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005364void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005365 ScratchRegisterScope ensure_scratch(
5366 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5367
5368 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5369 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5370 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5371 Address(CpuRegister(RSP), mem2 + stack_offset));
5372 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5373 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5374 CpuRegister(ensure_scratch.GetRegister()));
5375}
5376
Mark Mendell8a1c7282015-06-29 15:41:28 -04005377void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5378 __ movq(CpuRegister(TMP), reg1);
5379 __ movq(reg1, reg2);
5380 __ movq(reg2, CpuRegister(TMP));
5381}
5382
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005383void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5384 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5385 __ movq(Address(CpuRegister(RSP), mem), reg);
5386 __ movq(reg, CpuRegister(TMP));
5387}
5388
5389void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5390 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005391 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005392
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005393 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5394 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5395 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5396 Address(CpuRegister(RSP), mem2 + stack_offset));
5397 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5398 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5399 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005400}
5401
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005402void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5403 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5404 __ movss(Address(CpuRegister(RSP), mem), reg);
5405 __ movd(reg, CpuRegister(TMP));
5406}
5407
5408void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5409 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5410 __ movsd(Address(CpuRegister(RSP), mem), reg);
5411 __ movd(reg, CpuRegister(TMP));
5412}
5413
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005414void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005415 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005416 Location source = move->GetSource();
5417 Location destination = move->GetDestination();
5418
5419 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005420 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005421 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005422 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005423 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005424 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005425 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005426 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5427 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005428 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005429 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005430 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005431 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5432 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005433 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005434 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5435 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5436 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005437 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005438 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005439 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005440 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005441 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005442 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005443 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005444 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005445 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005446 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005447 }
5448}
5449
5450
5451void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5452 __ pushq(CpuRegister(reg));
5453}
5454
5455
5456void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5457 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005458}
5459
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005460void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005461 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005462 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5463 Immediate(mirror::Class::kStatusInitialized));
5464 __ j(kLess, slow_path->GetEntryLabel());
5465 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005466 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005467}
5468
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005469HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5470 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005471 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005472 case HLoadClass::LoadKind::kInvalid:
5473 LOG(FATAL) << "UNREACHABLE";
5474 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005475 case HLoadClass::LoadKind::kReferrersClass:
5476 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005477 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005478 case HLoadClass::LoadKind::kBssEntry:
5479 DCHECK(!Runtime::Current()->UseJitCompilation());
5480 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005481 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005482 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005483 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005484 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005485 case HLoadClass::LoadKind::kDexCacheViaMethod:
5486 break;
5487 }
5488 return desired_class_load_kind;
5489}
5490
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005491void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005492 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5493 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005494 // Custom calling convention: RAX serves as both input and output.
Vladimir Marko41559982017-01-06 14:04:23 +00005495 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005496 cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005497 Location::RegisterLocation(RAX),
Vladimir Marko41559982017-01-06 14:04:23 +00005498 Location::RegisterLocation(RAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005499 return;
5500 }
Vladimir Marko41559982017-01-06 14:04:23 +00005501 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005502
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005503 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5504 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005505 ? LocationSummary::kCallOnSlowPath
5506 : LocationSummary::kNoCall;
5507 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005508 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005509 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005510 }
5511
Vladimir Marko41559982017-01-06 14:04:23 +00005512 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005513 locations->SetInAt(0, Location::RequiresRegister());
5514 }
5515 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005516 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5517 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5518 // Rely on the type resolution and/or initialization to save everything.
5519 // Custom calling convention: RAX serves as both input and output.
5520 RegisterSet caller_saves = RegisterSet::Empty();
5521 caller_saves.Add(Location::RegisterLocation(RAX));
5522 locations->SetCustomSlowPathCallerSaves(caller_saves);
5523 } else {
5524 // For non-Baker read barrier we have a temp-clobbering call.
5525 }
5526 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005527}
5528
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005529Label* CodeGeneratorX86_64::NewJitRootClassPatch(const DexFile& dex_file,
5530 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005531 Handle<mirror::Class> handle) {
5532 jit_class_roots_.Overwrite(
5533 TypeReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005534 // Add a patch entry and return the label.
5535 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
5536 PatchInfo<Label>* info = &jit_class_patches_.back();
5537 return &info->label;
5538}
5539
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005540// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5541// move.
5542void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005543 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5544 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5545 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005546 return;
5547 }
Vladimir Marko41559982017-01-06 14:04:23 +00005548 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005549
Vladimir Marko41559982017-01-06 14:04:23 +00005550 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005551 Location out_loc = locations->Out();
5552 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005553
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005554 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5555 ? kWithoutReadBarrier
5556 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005557 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005558 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005559 case HLoadClass::LoadKind::kReferrersClass: {
5560 DCHECK(!cls->CanCallRuntime());
5561 DCHECK(!cls->MustGenerateClinitCheck());
5562 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5563 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5564 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005565 cls,
5566 out_loc,
5567 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01005568 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005569 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005570 break;
5571 }
5572 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005573 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005574 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005575 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Marko1998cd02017-01-13 13:02:58 +00005576 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005577 break;
5578 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005579 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005580 uint32_t address = dchecked_integral_cast<uint32_t>(
5581 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5582 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005583 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005584 break;
5585 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005586 case HLoadClass::LoadKind::kBssEntry: {
5587 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5588 /* no_rip */ false);
5589 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
5590 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5591 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
5592 generate_null_check = true;
5593 break;
5594 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005595 case HLoadClass::LoadKind::kJitTableAddress: {
5596 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5597 /* no_rip */ true);
5598 Label* fixup_label =
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005599 codegen_->NewJitRootClassPatch(cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005600 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00005601 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005602 break;
5603 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005604 default:
5605 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5606 UNREACHABLE();
5607 }
5608
5609 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5610 DCHECK(cls->CanCallRuntime());
5611 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5612 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5613 codegen_->AddSlowPath(slow_path);
5614 if (generate_null_check) {
5615 __ testl(out, out);
5616 __ j(kEqual, slow_path->GetEntryLabel());
5617 }
5618 if (cls->MustGenerateClinitCheck()) {
5619 GenerateClassInitializationCheck(slow_path, out);
5620 } else {
5621 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005622 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005623 }
5624}
5625
5626void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5627 LocationSummary* locations =
5628 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5629 locations->SetInAt(0, Location::RequiresRegister());
5630 if (check->HasUses()) {
5631 locations->SetOut(Location::SameAsFirstInput());
5632 }
5633}
5634
5635void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005636 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005637 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005638 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005639 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005640 GenerateClassInitializationCheck(slow_path,
5641 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005642}
5643
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005644HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5645 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005646 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005647 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005648 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005649 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005650 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005651 case HLoadString::LoadKind::kJitTableAddress:
5652 DCHECK(Runtime::Current()->UseJitCompilation());
5653 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005654 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005655 case HLoadString::LoadKind::kDexCacheViaMethod:
5656 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005657 }
5658 return desired_string_load_kind;
5659}
5660
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005661void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005662 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005663 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005664 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005665 locations->SetOut(Location::RegisterLocation(RAX));
5666 } else {
5667 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005668 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5669 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005670 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005671 // Custom calling convention: RAX serves as both input and output.
5672 RegisterSet caller_saves = RegisterSet::Empty();
5673 caller_saves.Add(Location::RegisterLocation(RAX));
5674 locations->SetCustomSlowPathCallerSaves(caller_saves);
5675 } else {
5676 // For non-Baker read barrier we have a temp-clobbering call.
5677 }
5678 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005679 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005680}
5681
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005682Label* CodeGeneratorX86_64::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005683 dex::StringIndex dex_index,
5684 Handle<mirror::String> handle) {
5685 jit_string_roots_.Overwrite(
5686 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005687 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005688 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005689 PatchInfo<Label>* info = &jit_string_patches_.back();
5690 return &info->label;
5691}
5692
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005693// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5694// move.
5695void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005696 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005697 Location out_loc = locations->Out();
5698 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005699
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005700 switch (load->GetLoadKind()) {
5701 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005702 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005703 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005704 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005705 return; // No dex cache slow path.
5706 }
5707 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005708 uint32_t address = dchecked_integral_cast<uint32_t>(
5709 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5710 DCHECK_NE(address, 0u);
Colin Cross0bd97172017-03-15 16:33:27 -07005711 __ movl(out, Immediate(static_cast<int32_t>(address))); // Zero-extended.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005712 return; // No dex cache slow path.
5713 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005714 case HLoadString::LoadKind::kBssEntry: {
5715 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5716 /* no_rip */ false);
5717 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
5718 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005719 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005720 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5721 codegen_->AddSlowPath(slow_path);
5722 __ testl(out, out);
5723 __ j(kEqual, slow_path->GetEntryLabel());
5724 __ Bind(slow_path->GetExitLabel());
5725 return;
5726 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005727 case HLoadString::LoadKind::kJitTableAddress: {
5728 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5729 /* no_rip */ true);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005730 Label* fixup_label = codegen_->NewJitRootStringPatch(
5731 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005732 // /* GcRoot<mirror::String> */ out = *address
5733 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
5734 return;
5735 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005736 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005737 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005738 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005739
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005740 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005741 // Custom calling convention: RAX serves as both input and output.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005742 __ movl(CpuRegister(RAX), Immediate(load->GetStringIndex().index_));
Christina Wadsworthabb341b2016-08-31 16:29:44 -07005743 codegen_->InvokeRuntime(kQuickResolveString,
5744 load,
5745 load->GetDexPc());
5746 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005747}
5748
David Brazdilcb1c0552015-08-04 16:22:25 +01005749static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005750 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005751 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005752}
5753
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005754void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5755 LocationSummary* locations =
5756 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5757 locations->SetOut(Location::RequiresRegister());
5758}
5759
5760void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005761 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5762}
5763
5764void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5765 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5766}
5767
5768void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5769 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005770}
5771
5772void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5773 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005774 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005775 InvokeRuntimeCallingConvention calling_convention;
5776 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5777}
5778
5779void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01005780 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005781 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005782}
5783
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005784static bool CheckCastTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5785 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07005786 // We need a temporary for holding the iftable length.
5787 return true;
5788 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005789 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005790 !kUseBakerReadBarrier &&
5791 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005792 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5793 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5794}
5795
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005796static bool InstanceOfTypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5797 return kEmitCompilerReadBarrier &&
5798 !kUseBakerReadBarrier &&
5799 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5800 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5801 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5802}
5803
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005804void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005805 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005806 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005807 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005808 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005809 case TypeCheckKind::kExactCheck:
5810 case TypeCheckKind::kAbstractClassCheck:
5811 case TypeCheckKind::kClassHierarchyCheck:
5812 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005813 call_kind =
5814 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005815 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005816 break;
5817 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005818 case TypeCheckKind::kUnresolvedCheck:
5819 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005820 call_kind = LocationSummary::kCallOnSlowPath;
5821 break;
5822 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005823
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005824 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005825 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005826 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005827 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005828 locations->SetInAt(0, Location::RequiresRegister());
5829 locations->SetInAt(1, Location::Any());
5830 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5831 locations->SetOut(Location::RequiresRegister());
5832 // When read barriers are enabled, we need a temporary register for
5833 // some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005834 if (InstanceOfTypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005835 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005836 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005837}
5838
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005839void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005840 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005841 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005842 Location obj_loc = locations->InAt(0);
5843 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005844 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005845 Location out_loc = locations->Out();
5846 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00005847 Location maybe_temp_loc = InstanceOfTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005848 locations->GetTemp(0) :
5849 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005850 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5852 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5853 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005854 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005855 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005856
5857 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005858 // Avoid null check if we know obj is not null.
5859 if (instruction->MustDoNullCheck()) {
5860 __ testl(obj, obj);
5861 __ j(kEqual, &zero);
5862 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005863
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005864 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005865 case TypeCheckKind::kExactCheck: {
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 (cls.IsRegister()) {
5873 __ cmpl(out, cls.AsRegister<CpuRegister>());
5874 } else {
5875 DCHECK(cls.IsStackSlot()) << cls;
5876 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5877 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005878 if (zero.IsLinked()) {
5879 // Classes must be equal for the instanceof to succeed.
5880 __ j(kNotEqual, &zero);
5881 __ movl(out, Immediate(1));
5882 __ jmp(&done);
5883 } else {
5884 __ setcc(kEqual, out);
5885 // setcc only sets the low byte.
5886 __ andl(out, Immediate(1));
5887 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005888 break;
5889 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005890
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005891 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005892 // /* HeapReference<Class> */ out = obj->klass_
5893 GenerateReferenceLoadTwoRegisters(instruction,
5894 out_loc,
5895 obj_loc,
5896 class_offset,
5897 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005898 // If the class is abstract, we eagerly fetch the super class of the
5899 // object to avoid doing a comparison we know will fail.
5900 NearLabel loop, success;
5901 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005902 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005903 GenerateReferenceLoadOneRegister(instruction,
5904 out_loc,
5905 super_offset,
5906 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005907 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005908 __ testl(out, out);
5909 // If `out` is null, we use it for the result, and jump to `done`.
5910 __ j(kEqual, &done);
5911 if (cls.IsRegister()) {
5912 __ cmpl(out, cls.AsRegister<CpuRegister>());
5913 } else {
5914 DCHECK(cls.IsStackSlot()) << cls;
5915 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5916 }
5917 __ j(kNotEqual, &loop);
5918 __ movl(out, Immediate(1));
5919 if (zero.IsLinked()) {
5920 __ jmp(&done);
5921 }
5922 break;
5923 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005924
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005926 // /* HeapReference<Class> */ out = obj->klass_
5927 GenerateReferenceLoadTwoRegisters(instruction,
5928 out_loc,
5929 obj_loc,
5930 class_offset,
5931 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005932 // Walk over the class hierarchy to find a match.
5933 NearLabel loop, success;
5934 __ Bind(&loop);
5935 if (cls.IsRegister()) {
5936 __ cmpl(out, cls.AsRegister<CpuRegister>());
5937 } else {
5938 DCHECK(cls.IsStackSlot()) << cls;
5939 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5940 }
5941 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005942 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005943 GenerateReferenceLoadOneRegister(instruction,
5944 out_loc,
5945 super_offset,
5946 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005947 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005948 __ testl(out, out);
5949 __ j(kNotEqual, &loop);
5950 // If `out` is null, we use it for the result, and jump to `done`.
5951 __ jmp(&done);
5952 __ Bind(&success);
5953 __ movl(out, Immediate(1));
5954 if (zero.IsLinked()) {
5955 __ jmp(&done);
5956 }
5957 break;
5958 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005959
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005960 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005961 // /* HeapReference<Class> */ out = obj->klass_
5962 GenerateReferenceLoadTwoRegisters(instruction,
5963 out_loc,
5964 obj_loc,
5965 class_offset,
5966 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005967 // Do an exact check.
5968 NearLabel exact_check;
5969 if (cls.IsRegister()) {
5970 __ cmpl(out, cls.AsRegister<CpuRegister>());
5971 } else {
5972 DCHECK(cls.IsStackSlot()) << cls;
5973 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5974 }
5975 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005976 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005977 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005978 GenerateReferenceLoadOneRegister(instruction,
5979 out_loc,
5980 component_offset,
5981 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005982 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005983 __ testl(out, out);
5984 // If `out` is null, we use it for the result, and jump to `done`.
5985 __ j(kEqual, &done);
5986 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5987 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005988 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005989 __ movl(out, Immediate(1));
5990 __ jmp(&done);
5991 break;
5992 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005993
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005994 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08005995 // No read barrier since the slow path will retry upon failure.
5996 // /* HeapReference<Class> */ out = obj->klass_
5997 GenerateReferenceLoadTwoRegisters(instruction,
5998 out_loc,
5999 obj_loc,
6000 class_offset,
6001 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006002 if (cls.IsRegister()) {
6003 __ cmpl(out, cls.AsRegister<CpuRegister>());
6004 } else {
6005 DCHECK(cls.IsStackSlot()) << cls;
6006 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
6007 }
6008 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006009 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6010 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006011 codegen_->AddSlowPath(slow_path);
6012 __ j(kNotEqual, slow_path->GetEntryLabel());
6013 __ movl(out, Immediate(1));
6014 if (zero.IsLinked()) {
6015 __ jmp(&done);
6016 }
6017 break;
6018 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006019
Calin Juravle98893e12015-10-02 21:05:03 +01006020 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006021 case TypeCheckKind::kInterfaceCheck: {
6022 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006023 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006024 // cases.
6025 //
6026 // We cannot directly call the InstanceofNonTrivial runtime
6027 // entry point without resorting to a type checking slow path
6028 // here (i.e. by calling InvokeRuntime directly), as it would
6029 // require to assign fixed registers for the inputs of this
6030 // HInstanceOf instruction (following the runtime calling
6031 // convention), which might be cluttered by the potential first
6032 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006033 //
6034 // TODO: Introduce a new runtime entry point taking the object
6035 // to test (instead of its class) as argument, and let it deal
6036 // with the read barrier issues. This will let us refactor this
6037 // case of the `switch` code as it was previously (with a direct
6038 // call to the runtime not using a type checking slow path).
6039 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006040 DCHECK(locations->OnlyCallsOnSlowPath());
6041 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6042 /* is_fatal */ false);
6043 codegen_->AddSlowPath(slow_path);
6044 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006045 if (zero.IsLinked()) {
6046 __ jmp(&done);
6047 }
6048 break;
6049 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006050 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006051
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006052 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006053 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006054 __ xorl(out, out);
6055 }
6056
6057 if (done.IsLinked()) {
6058 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006059 }
6060
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006061 if (slow_path != nullptr) {
6062 __ Bind(slow_path->GetExitLabel());
6063 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006064}
6065
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006066static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006067 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006068 case TypeCheckKind::kExactCheck:
6069 case TypeCheckKind::kAbstractClassCheck:
6070 case TypeCheckKind::kClassHierarchyCheck:
6071 case TypeCheckKind::kArrayObjectCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006072 return !throws_into_catch && !kEmitCompilerReadBarrier;
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006073 case TypeCheckKind::kInterfaceCheck:
6074 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006075 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006076 case TypeCheckKind::kUnresolvedCheck:
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006077 return false;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006078 }
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006079 LOG(FATAL) << "Unreachable";
6080 UNREACHABLE();
6081}
6082
6083void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
6084 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6085 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6086 bool is_fatal_slow_path = IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch);
6087 LocationSummary::CallKind call_kind = is_fatal_slow_path
6088 ? LocationSummary::kNoCall
6089 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006090 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6091 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006092 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6093 // Require a register for the interface check since there is a loop that compares the class to
6094 // a memory address.
6095 locations->SetInAt(1, Location::RequiresRegister());
6096 } else {
6097 locations->SetInAt(1, Location::Any());
6098 }
6099
Roland Levillain0d5a2812015-11-13 10:07:31 +00006100 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
6101 locations->AddTemp(Location::RequiresRegister());
6102 // When read barriers are enabled, we need an additional temporary
6103 // register for some cases.
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006104 if (CheckCastTypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006105 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006106 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006107}
6108
6109void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006110 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006111 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006112 Location obj_loc = locations->InAt(0);
6113 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006114 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006115 Location temp_loc = locations->GetTemp(0);
6116 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray53b07bd2016-11-05 15:09:19 +00006117 Location maybe_temp2_loc = CheckCastTypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006118 locations->GetTemp(1) :
6119 Location::NoLocation();
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006120 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6121 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6122 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6123 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6124 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6125 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006126 const uint32_t object_array_data_offset =
6127 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006128
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006129 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6130 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6131 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006132 bool is_type_check_slow_path_fatal =
Andreas Gampeb5f3d812016-11-04 19:25:20 -07006133 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134 SlowPathCode* type_check_slow_path =
6135 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
6136 is_type_check_slow_path_fatal);
6137 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006138
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006139
6140 NearLabel done;
6141 // Avoid null check if we know obj is not null.
6142 if (instruction->MustDoNullCheck()) {
6143 __ testl(obj, obj);
6144 __ j(kEqual, &done);
6145 }
6146
Roland Levillain0d5a2812015-11-13 10:07:31 +00006147 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148 case TypeCheckKind::kExactCheck:
6149 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006150 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006151 GenerateReferenceLoadTwoRegisters(instruction,
6152 temp_loc,
6153 obj_loc,
6154 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006155 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156 if (cls.IsRegister()) {
6157 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6158 } else {
6159 DCHECK(cls.IsStackSlot()) << cls;
6160 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6161 }
6162 // Jump to slow path for throwing the exception or doing a
6163 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006164 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 break;
6166 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006167
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006169 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006170 GenerateReferenceLoadTwoRegisters(instruction,
6171 temp_loc,
6172 obj_loc,
6173 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006174 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006175 // If the class is abstract, we eagerly fetch the super class of the
6176 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006177 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006178 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006179 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006180 GenerateReferenceLoadOneRegister(instruction,
6181 temp_loc,
6182 super_offset,
6183 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006184 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006185
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006186 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6187 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006188 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006189 // Otherwise, compare the classes.
6190 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006191 if (cls.IsRegister()) {
6192 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6193 } else {
6194 DCHECK(cls.IsStackSlot()) << cls;
6195 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6196 }
6197 __ j(kNotEqual, &loop);
6198 break;
6199 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006200
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201 case TypeCheckKind::kClassHierarchyCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006202 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006203 GenerateReferenceLoadTwoRegisters(instruction,
6204 temp_loc,
6205 obj_loc,
6206 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006207 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006208 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006209 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006210 __ Bind(&loop);
6211 if (cls.IsRegister()) {
6212 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6213 } else {
6214 DCHECK(cls.IsStackSlot()) << cls;
6215 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6216 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006217 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006218
Roland Levillain0d5a2812015-11-13 10:07:31 +00006219 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006220 GenerateReferenceLoadOneRegister(instruction,
6221 temp_loc,
6222 super_offset,
6223 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006224 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006225
6226 // If the class reference currently in `temp` is not null, jump
6227 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006229 __ j(kNotZero, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006230 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006231 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232 break;
6233 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006234
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006235 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006236 // /* HeapReference<Class> */ temp = obj->klass_
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006237 GenerateReferenceLoadTwoRegisters(instruction,
6238 temp_loc,
6239 obj_loc,
6240 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006241 kWithoutReadBarrier);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006242 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006243 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006244 if (cls.IsRegister()) {
6245 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6246 } else {
6247 DCHECK(cls.IsStackSlot()) << cls;
6248 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6249 }
6250 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006251
6252 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006253 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006254 GenerateReferenceLoadOneRegister(instruction,
6255 temp_loc,
6256 component_offset,
6257 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006258 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006259
6260 // If the component type is not null (i.e. the object is indeed
6261 // an array), jump to label `check_non_primitive_component_type`
6262 // to further check that this component type is not a primitive
6263 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006265 // Otherwise, jump to the slow path to throw the exception.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006266 __ j(kZero, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006267 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006268 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006269 break;
6270 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006271
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006272 case TypeCheckKind::kUnresolvedCheck: {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006273 // We always go into the type check slow path for the unresolved case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006274 //
6275 // We cannot directly call the CheckCast runtime entry point
6276 // without resorting to a type checking slow path here (i.e. by
6277 // calling InvokeRuntime directly), as it would require to
6278 // assign fixed registers for the inputs of this HInstanceOf
6279 // instruction (following the runtime calling convention), which
6280 // might be cluttered by the potential first read barrier
6281 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006282 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006283 break;
6284 }
6285
6286 case TypeCheckKind::kInterfaceCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006287 // Fast path for the interface check. We always go slow path for heap poisoning since
6288 // unpoisoning cls would require an extra temp.
6289 if (!kPoisonHeapReferences) {
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006290 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6291 // doing this.
6292 // /* HeapReference<Class> */ temp = obj->klass_
6293 GenerateReferenceLoadTwoRegisters(instruction,
6294 temp_loc,
6295 obj_loc,
6296 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006297 kWithoutReadBarrier);
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006298
6299 // /* HeapReference<Class> */ temp = temp->iftable_
6300 GenerateReferenceLoadTwoRegisters(instruction,
6301 temp_loc,
6302 temp_loc,
6303 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006304 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006305 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006306 __ movl(maybe_temp2_loc.AsRegister<CpuRegister>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006307 // Loop through the iftable and check if any class matches.
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006308 NearLabel start_loop;
6309 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006310 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006311 __ subl(maybe_temp2_loc.AsRegister<CpuRegister>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006312 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6313 // Go to next interface if the classes do not match.
6314 __ cmpl(cls.AsRegister<CpuRegister>(),
6315 CodeGeneratorX86_64::ArrayAddress(temp,
6316 maybe_temp2_loc,
6317 TIMES_4,
6318 object_array_data_offset));
6319 __ j(kNotEqual, &start_loop); // Return if same class.
6320 } else {
6321 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartiercdba73b2016-11-03 19:23:06 -07006322 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 break;
6324 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006325
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006326 if (done.IsLinked()) {
6327 __ Bind(&done);
6328 }
6329
Roland Levillain0d5a2812015-11-13 10:07:31 +00006330 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006331}
6332
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006333void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6334 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006335 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006336 InvokeRuntimeCallingConvention calling_convention;
6337 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6338}
6339
6340void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006341 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006342 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006343 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006344 if (instruction->IsEnter()) {
6345 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6346 } else {
6347 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6348 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006349}
6350
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006351void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6352void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6353void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6354
6355void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6356 LocationSummary* locations =
6357 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6358 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6359 || instruction->GetResultType() == Primitive::kPrimLong);
6360 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006361 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006362 locations->SetOut(Location::SameAsFirstInput());
6363}
6364
6365void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6366 HandleBitwiseOperation(instruction);
6367}
6368
6369void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6370 HandleBitwiseOperation(instruction);
6371}
6372
6373void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6374 HandleBitwiseOperation(instruction);
6375}
6376
6377void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6378 LocationSummary* locations = instruction->GetLocations();
6379 Location first = locations->InAt(0);
6380 Location second = locations->InAt(1);
6381 DCHECK(first.Equals(locations->Out()));
6382
6383 if (instruction->GetResultType() == Primitive::kPrimInt) {
6384 if (second.IsRegister()) {
6385 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006386 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006387 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006388 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006389 } else {
6390 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006391 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006392 }
6393 } else if (second.IsConstant()) {
6394 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6395 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006396 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006397 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006398 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006399 } else {
6400 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006401 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006402 }
6403 } else {
6404 Address address(CpuRegister(RSP), second.GetStackIndex());
6405 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006406 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006407 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006408 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006409 } else {
6410 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006411 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006412 }
6413 }
6414 } else {
6415 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006416 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6417 bool second_is_constant = false;
6418 int64_t value = 0;
6419 if (second.IsConstant()) {
6420 second_is_constant = true;
6421 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006422 }
Mark Mendell40741f32015-04-20 22:10:34 -04006423 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006424
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006425 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006426 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006427 if (is_int32_value) {
6428 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6429 } else {
6430 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6431 }
6432 } else if (second.IsDoubleStackSlot()) {
6433 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006434 } else {
6435 __ andq(first_reg, second.AsRegister<CpuRegister>());
6436 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006437 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006438 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006439 if (is_int32_value) {
6440 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6441 } else {
6442 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6443 }
6444 } else if (second.IsDoubleStackSlot()) {
6445 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006446 } else {
6447 __ orq(first_reg, second.AsRegister<CpuRegister>());
6448 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006449 } else {
6450 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006451 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006452 if (is_int32_value) {
6453 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6454 } else {
6455 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6456 }
6457 } else if (second.IsDoubleStackSlot()) {
6458 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006459 } else {
6460 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6461 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006462 }
6463 }
6464}
6465
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006466void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(
6467 HInstruction* instruction,
6468 Location out,
6469 uint32_t offset,
6470 Location maybe_temp,
6471 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006472 CpuRegister out_reg = out.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006473 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006474 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006475 if (kUseBakerReadBarrier) {
6476 // Load with fast path based Baker's read barrier.
6477 // /* HeapReference<Object> */ out = *(out + offset)
6478 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006479 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006480 } else {
6481 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006482 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006483 // in the following move operation, as we will need it for the
6484 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006485 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006486 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006487 // /* HeapReference<Object> */ out = *(out + offset)
6488 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006489 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006490 }
6491 } else {
6492 // Plain load with no read barrier.
6493 // /* HeapReference<Object> */ out = *(out + offset)
6494 __ movl(out_reg, Address(out_reg, offset));
6495 __ MaybeUnpoisonHeapReference(out_reg);
6496 }
6497}
6498
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006499void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(
6500 HInstruction* instruction,
6501 Location out,
6502 Location obj,
6503 uint32_t offset,
6504 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006505 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6506 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006507 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006508 CHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006509 if (kUseBakerReadBarrier) {
6510 // Load with fast path based Baker's read barrier.
6511 // /* HeapReference<Object> */ out = *(obj + offset)
6512 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006513 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006514 } else {
6515 // Load with slow path based read barrier.
6516 // /* HeapReference<Object> */ out = *(obj + offset)
6517 __ movl(out_reg, Address(obj_reg, offset));
6518 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6519 }
6520 } else {
6521 // Plain load with no read barrier.
6522 // /* HeapReference<Object> */ out = *(obj + offset)
6523 __ movl(out_reg, Address(obj_reg, offset));
6524 __ MaybeUnpoisonHeapReference(out_reg);
6525 }
6526}
6527
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006528void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(
6529 HInstruction* instruction,
6530 Location root,
6531 const Address& address,
6532 Label* fixup_label,
6533 ReadBarrierOption read_barrier_option) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006534 CpuRegister root_reg = root.AsRegister<CpuRegister>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006535 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006536 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006537 if (kUseBakerReadBarrier) {
6538 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6539 // Baker's read barrier are used:
6540 //
Roland Levillaind966ce72017-02-09 16:20:14 +00006541 // root = obj.field;
6542 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6543 // if (temp != null) {
6544 // root = temp(root)
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006545 // }
6546
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006547 // /* GcRoot<mirror::Object> */ root = *address
6548 __ movl(root_reg, address);
6549 if (fixup_label != nullptr) {
6550 __ Bind(fixup_label);
6551 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006552 static_assert(
6553 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6554 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6555 "have different sizes.");
6556 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6557 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6558 "have different sizes.");
6559
Vladimir Marko953437b2016-08-24 08:30:46 +00006560 // Slow path marking the GC root `root`.
6561 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006562 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006563 codegen_->AddSlowPath(slow_path);
6564
Roland Levillaind966ce72017-02-09 16:20:14 +00006565 // Test the `Thread::Current()->pReadBarrierMarkReg ## root.reg()` entrypoint.
6566 const int32_t entry_point_offset =
6567 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(root.reg());
6568 __ gs()->cmpl(Address::Absolute(entry_point_offset, /* no_rip */ true), Immediate(0));
6569 // The entrypoint is null when the GC is not marking.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006570 __ j(kNotEqual, slow_path->GetEntryLabel());
6571 __ Bind(slow_path->GetExitLabel());
6572 } else {
6573 // GC root loaded through a slow path for read barriers other
6574 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006575 // /* GcRoot<mirror::Object>* */ root = address
6576 __ leaq(root_reg, address);
6577 if (fixup_label != nullptr) {
6578 __ Bind(fixup_label);
6579 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006580 // /* mirror::Object* */ root = root->Read()
6581 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6582 }
6583 } else {
6584 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006585 // /* GcRoot<mirror::Object> */ root = *address
6586 __ movl(root_reg, address);
6587 if (fixup_label != nullptr) {
6588 __ Bind(fixup_label);
6589 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006590 // Note that GC roots are not affected by heap poisoning, thus we
6591 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006592 }
6593}
6594
6595void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6596 Location ref,
6597 CpuRegister obj,
6598 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006599 bool needs_null_check) {
6600 DCHECK(kEmitCompilerReadBarrier);
6601 DCHECK(kUseBakerReadBarrier);
6602
6603 // /* HeapReference<Object> */ ref = *(obj + offset)
6604 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006605 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006606}
6607
6608void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6609 Location ref,
6610 CpuRegister obj,
6611 uint32_t data_offset,
6612 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006613 bool needs_null_check) {
6614 DCHECK(kEmitCompilerReadBarrier);
6615 DCHECK(kUseBakerReadBarrier);
6616
Roland Levillain3d312422016-06-23 13:53:42 +01006617 static_assert(
6618 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6619 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006620 // /* HeapReference<Object> */ ref =
6621 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006622 Address src = CodeGeneratorX86_64::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006623 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006624}
6625
6626void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6627 Location ref,
6628 CpuRegister obj,
6629 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006630 bool needs_null_check,
6631 bool always_update_field,
6632 CpuRegister* temp1,
6633 CpuRegister* temp2) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006634 DCHECK(kEmitCompilerReadBarrier);
6635 DCHECK(kUseBakerReadBarrier);
6636
6637 // In slow path based read barriers, the read barrier call is
6638 // inserted after the original load. However, in fast path based
6639 // Baker's read barriers, we need to perform the load of
6640 // mirror::Object::monitor_ *before* the original reference load.
6641 // This load-load ordering is required by the read barrier.
6642 // The fast path/slow path (for Baker's algorithm) should look like:
6643 //
6644 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6645 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6646 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006647 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006648 // if (is_gray) {
6649 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6650 // }
6651 //
6652 // Note: the original implementation in ReadBarrier::Barrier is
6653 // slightly more complex as:
6654 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006655 // the high-bits of rb_state, which are expected to be all zeroes
6656 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6657 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006658 // - it performs additional checks that we do not do here for
6659 // performance reasons.
6660
6661 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006662 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6663
Vladimir Marko953437b2016-08-24 08:30:46 +00006664 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006665 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6666 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00006667 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6668 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6669 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6670
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006671 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00006672 // ref = ReadBarrier::Mark(ref);
6673 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6674 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006675 if (needs_null_check) {
6676 MaybeRecordImplicitNullCheck(instruction);
6677 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006678
6679 // Load fence to prevent load-load reordering.
6680 // Note that this is a no-op, thanks to the x86-64 memory model.
6681 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6682
6683 // The actual reference load.
6684 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006685 __ movl(ref_reg, src); // Flags are unaffected.
6686
6687 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6688 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006689 SlowPathCode* slow_path;
6690 if (always_update_field) {
6691 DCHECK(temp1 != nullptr);
6692 DCHECK(temp2 != nullptr);
6693 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86_64(
6694 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp1, *temp2);
6695 } else {
6696 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6697 instruction, ref, /* unpoison_ref_before_marking */ true);
6698 }
Vladimir Marko953437b2016-08-24 08:30:46 +00006699 AddSlowPath(slow_path);
6700
6701 // We have done the "if" of the gray bit check above, now branch based on the flags.
6702 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006703
6704 // Object* ref = ref_addr->AsMirrorPtr()
6705 __ MaybeUnpoisonHeapReference(ref_reg);
6706
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006707 __ Bind(slow_path->GetExitLabel());
6708}
6709
6710void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6711 Location out,
6712 Location ref,
6713 Location obj,
6714 uint32_t offset,
6715 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716 DCHECK(kEmitCompilerReadBarrier);
6717
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006718 // Insert a slow path based read barrier *after* the reference load.
6719 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006720 // If heap poisoning is enabled, the unpoisoning of the loaded
6721 // reference will be carried out by the runtime within the slow
6722 // path.
6723 //
6724 // Note that `ref` currently does not get unpoisoned (when heap
6725 // poisoning is enabled), which is alright as the `ref` argument is
6726 // not used by the artReadBarrierSlow entry point.
6727 //
6728 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6729 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6730 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6731 AddSlowPath(slow_path);
6732
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733 __ jmp(slow_path->GetEntryLabel());
6734 __ Bind(slow_path->GetExitLabel());
6735}
6736
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006737void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6738 Location out,
6739 Location ref,
6740 Location obj,
6741 uint32_t offset,
6742 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006743 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006744 // Baker's read barriers shall be handled by the fast path
6745 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6746 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006747 // If heap poisoning is enabled, unpoisoning will be taken care of
6748 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006749 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006750 } else if (kPoisonHeapReferences) {
6751 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6752 }
6753}
6754
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006755void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6756 Location out,
6757 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006758 DCHECK(kEmitCompilerReadBarrier);
6759
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006760 // Insert a slow path based read barrier *after* the GC root load.
6761 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006762 // Note that GC roots are not affected by heap poisoning, so we do
6763 // not need to do anything special for this here.
6764 SlowPathCode* slow_path =
6765 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6766 AddSlowPath(slow_path);
6767
Roland Levillain0d5a2812015-11-13 10:07:31 +00006768 __ jmp(slow_path->GetEntryLabel());
6769 __ Bind(slow_path->GetExitLabel());
6770}
6771
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006772void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006773 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006774 LOG(FATAL) << "Unreachable";
6775}
6776
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006777void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006778 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006779 LOG(FATAL) << "Unreachable";
6780}
6781
Mark Mendellfe57faa2015-09-18 09:26:15 -04006782// Simple implementation of packed switch - generate cascaded compare/jumps.
6783void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6784 LocationSummary* locations =
6785 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6786 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006787 locations->AddTemp(Location::RequiresRegister());
6788 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006789}
6790
6791void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6792 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006793 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006794 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006795 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6796 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6797 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006798 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6799
6800 // Should we generate smaller inline compare/jumps?
6801 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6802 // Figure out the correct compare values and jump conditions.
6803 // Handle the first compare/branch as a special case because it might
6804 // jump to the default case.
6805 DCHECK_GT(num_entries, 2u);
6806 Condition first_condition;
6807 uint32_t index;
6808 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6809 if (lower_bound != 0) {
6810 first_condition = kLess;
6811 __ cmpl(value_reg_in, Immediate(lower_bound));
6812 __ j(first_condition, codegen_->GetLabelOf(default_block));
6813 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6814
6815 index = 1;
6816 } else {
6817 // Handle all the compare/jumps below.
6818 first_condition = kBelow;
6819 index = 0;
6820 }
6821
6822 // Handle the rest of the compare/jumps.
6823 for (; index + 1 < num_entries; index += 2) {
6824 int32_t compare_to_value = lower_bound + index + 1;
6825 __ cmpl(value_reg_in, Immediate(compare_to_value));
6826 // Jump to successors[index] if value < case_value[index].
6827 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6828 // Jump to successors[index + 1] if value == case_value[index + 1].
6829 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6830 }
6831
6832 if (index != num_entries) {
6833 // There are an odd number of entries. Handle the last one.
6834 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006835 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006836 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6837 }
6838
6839 // And the default for any other value.
6840 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6841 __ jmp(codegen_->GetLabelOf(default_block));
6842 }
6843 return;
6844 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006845
6846 // Remove the bias, if needed.
6847 Register value_reg_out = value_reg_in.AsRegister();
6848 if (lower_bound != 0) {
6849 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6850 value_reg_out = temp_reg.AsRegister();
6851 }
6852 CpuRegister value_reg(value_reg_out);
6853
6854 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006855 __ cmpl(value_reg, Immediate(num_entries - 1));
6856 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006857
Mark Mendell9c86b482015-09-18 13:36:07 -04006858 // We are in the range of the table.
6859 // Load the address of the jump table in the constant area.
6860 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006861
Mark Mendell9c86b482015-09-18 13:36:07 -04006862 // Load the (signed) offset from the jump table.
6863 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6864
6865 // Add the offset to the address of the table base.
6866 __ addq(temp_reg, base_reg);
6867
6868 // And jump.
6869 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006870}
6871
Aart Bikc5d47542016-01-27 17:00:35 -08006872void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6873 if (value == 0) {
6874 __ xorl(dest, dest);
6875 } else {
6876 __ movl(dest, Immediate(value));
6877 }
6878}
6879
Mark Mendell92e83bf2015-05-07 11:25:03 -04006880void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6881 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006882 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006883 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006884 } else if (IsUint<32>(value)) {
6885 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006886 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6887 } else {
6888 __ movq(dest, Immediate(value));
6889 }
6890}
6891
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006892void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6893 if (value == 0) {
6894 __ xorps(dest, dest);
6895 } else {
6896 __ movss(dest, LiteralInt32Address(value));
6897 }
6898}
6899
6900void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6901 if (value == 0) {
6902 __ xorpd(dest, dest);
6903 } else {
6904 __ movsd(dest, LiteralInt64Address(value));
6905 }
6906}
6907
6908void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6909 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6910}
6911
6912void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6913 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6914}
6915
Aart Bika19616e2016-02-01 18:57:58 -08006916void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6917 if (value == 0) {
6918 __ testl(dest, dest);
6919 } else {
6920 __ cmpl(dest, Immediate(value));
6921 }
6922}
6923
6924void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6925 if (IsInt<32>(value)) {
6926 if (value == 0) {
6927 __ testq(dest, dest);
6928 } else {
6929 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6930 }
6931 } else {
6932 // Value won't fit in an int.
6933 __ cmpq(dest, LiteralInt64Address(value));
6934 }
6935}
6936
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006937void CodeGeneratorX86_64::GenerateIntCompare(Location lhs, Location rhs) {
6938 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
jessicahandojo4877b792016-09-08 19:49:13 -07006939 GenerateIntCompare(lhs_reg, rhs);
6940}
6941
6942void CodeGeneratorX86_64::GenerateIntCompare(CpuRegister lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006943 if (rhs.IsConstant()) {
6944 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07006945 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006946 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07006947 __ cmpl(lhs, Address(CpuRegister(RSP), rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006948 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006949 __ cmpl(lhs, rhs.AsRegister<CpuRegister>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006950 }
6951}
6952
6953void CodeGeneratorX86_64::GenerateLongCompare(Location lhs, Location rhs) {
6954 CpuRegister lhs_reg = lhs.AsRegister<CpuRegister>();
6955 if (rhs.IsConstant()) {
6956 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
6957 Compare64BitValue(lhs_reg, value);
6958 } else if (rhs.IsDoubleStackSlot()) {
6959 __ cmpq(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
6960 } else {
6961 __ cmpq(lhs_reg, rhs.AsRegister<CpuRegister>());
6962 }
6963}
6964
6965Address CodeGeneratorX86_64::ArrayAddress(CpuRegister obj,
6966 Location index,
6967 ScaleFactor scale,
6968 uint32_t data_offset) {
6969 return index.IsConstant() ?
6970 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
6971 Address(obj, index.AsRegister<CpuRegister>(), scale, data_offset);
6972}
6973
Mark Mendellcfa410b2015-05-25 16:02:44 -04006974void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6975 DCHECK(dest.IsDoubleStackSlot());
6976 if (IsInt<32>(value)) {
6977 // Can move directly as an int32 constant.
6978 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6979 Immediate(static_cast<int32_t>(value)));
6980 } else {
6981 Load64BitValue(CpuRegister(TMP), value);
6982 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6983 }
6984}
6985
Mark Mendell9c86b482015-09-18 13:36:07 -04006986/**
6987 * Class to handle late fixup of offsets into constant area.
6988 */
6989class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6990 public:
6991 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6992 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6993
6994 protected:
6995 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6996
6997 CodeGeneratorX86_64* codegen_;
6998
6999 private:
7000 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7001 // Patch the correct offset for the instruction. We use the address of the
7002 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
7003 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7004 int32_t relative_position = constant_offset - pos;
7005
7006 // Patch in the right value.
7007 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7008 }
7009
7010 // Location in constant area that the fixup refers to.
7011 size_t offset_into_constant_area_;
7012};
7013
7014/**
7015 t * Class to handle late fixup of offsets to a jump table that will be created in the
7016 * constant area.
7017 */
7018class JumpTableRIPFixup : public RIPFixup {
7019 public:
7020 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
7021 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
7022
7023 void CreateJumpTable() {
7024 X86_64Assembler* assembler = codegen_->GetAssembler();
7025
7026 // Ensure that the reference to the jump table has the correct offset.
7027 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7028 SetOffset(offset_in_constant_table);
7029
7030 // Compute the offset from the start of the function to this jump table.
7031 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
7032
7033 // Populate the jump table with the correct values for the jump table.
7034 int32_t num_entries = switch_instr_->GetNumEntries();
7035 HBasicBlock* block = switch_instr_->GetBlock();
7036 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7037 // The value that we want is the target offset - the position of the table.
7038 for (int32_t i = 0; i < num_entries; i++) {
7039 HBasicBlock* b = successors[i];
7040 Label* l = codegen_->GetLabelOf(b);
7041 DCHECK(l->IsBound());
7042 int32_t offset_to_block = l->Position() - current_table_offset;
7043 assembler->AppendInt32(offset_to_block);
7044 }
7045 }
7046
7047 private:
7048 const HPackedSwitch* switch_instr_;
7049};
7050
Mark Mendellf55c3e02015-03-26 21:07:46 -04007051void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
7052 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04007053 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04007054 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7055 // 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 -04007056 assembler->Align(4, 0);
7057 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04007058
7059 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007060 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell9c86b482015-09-18 13:36:07 -04007061 jump_table->CreateJumpTable();
7062 }
7063
7064 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04007065 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04007066 }
7067
7068 // And finish up.
7069 CodeGenerator::Finalize(allocator);
7070}
7071
Mark Mendellf55c3e02015-03-26 21:07:46 -04007072Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
7073 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7074 return Address::RIP(fixup);
7075}
7076
7077Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
7078 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7079 return Address::RIP(fixup);
7080}
7081
7082Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
7083 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7084 return Address::RIP(fixup);
7085}
7086
7087Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
7088 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7089 return Address::RIP(fixup);
7090}
7091
Andreas Gampe85b62f22015-09-09 13:15:38 -07007092// TODO: trg as memory.
7093void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7094 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00007095 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007096 return;
7097 }
7098
7099 DCHECK_NE(type, Primitive::kPrimVoid);
7100
7101 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
7102 if (trg.Equals(return_loc)) {
7103 return;
7104 }
7105
7106 // Let the parallel move resolver take care of all of this.
7107 HParallelMove parallel_move(GetGraph()->GetArena());
7108 parallel_move.AddMove(return_loc, trg, type, nullptr);
7109 GetMoveResolver()->EmitNativeCode(&parallel_move);
7110}
7111
Mark Mendell9c86b482015-09-18 13:36:07 -04007112Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
7113 // Create a fixup to be used to create and address the jump table.
7114 JumpTableRIPFixup* table_fixup =
7115 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7116
7117 // We have to populate the jump tables.
7118 fixups_to_jump_tables_.push_back(table_fixup);
7119 return Address::RIP(table_fixup);
7120}
7121
Mark Mendellea5af682015-10-22 17:35:49 -04007122void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
7123 const Address& addr_high,
7124 int64_t v,
7125 HInstruction* instruction) {
7126 if (IsInt<32>(v)) {
7127 int32_t v_32 = v;
7128 __ movq(addr_low, Immediate(v_32));
7129 MaybeRecordImplicitNullCheck(instruction);
7130 } else {
7131 // Didn't fit in a register. Do it in pieces.
7132 int32_t low_v = Low32Bits(v);
7133 int32_t high_v = High32Bits(v);
7134 __ movl(addr_low, Immediate(low_v));
7135 MaybeRecordImplicitNullCheck(instruction);
7136 __ movl(addr_high, Immediate(high_v));
7137 }
7138}
7139
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007140void CodeGeneratorX86_64::PatchJitRootUse(uint8_t* code,
7141 const uint8_t* roots_data,
7142 const PatchInfo<Label>& info,
7143 uint64_t index_in_table) const {
7144 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7145 uintptr_t address =
7146 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7147 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7148 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7149 dchecked_integral_cast<uint32_t>(address);
7150}
7151
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007152void CodeGeneratorX86_64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7153 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007154 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007155 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007156 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007157 uint64_t index_in_table = it->second;
7158 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007159 }
7160
7161 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007162 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007163 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7164 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007165 uint64_t index_in_table = it->second;
7166 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007167 }
7168}
7169
Roland Levillain4d027112015-07-01 15:41:14 +01007170#undef __
7171
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01007172} // namespace x86_64
7173} // namespace art